package NET.worlds.scape; import NET.worlds.console.Console; import NET.worlds.console.Main; import java.io.IOException; import java.util.Vector; public class Transform extends SuperRoot { private static Vector recycled = new Vector(); private int transformID; private float xScale; private float yScale; private float zScale; private static Object classCookie = new Object(); static { nativeInit(); } public static native void nativeInit(); public static Transform make() { assert Main.isMainThread(); int last = recycled.size() - 1; if (last == -1) { return new Transform(); } else { Transform ret = recycled.elementAt(last); recycled.removeElementAt(last); return ret; } } public void recycle() { if (recycled.size() < 100) { this.makeIdentity(); recycled.addElement(this); } } protected Transform() { this.makeIdentity(); } public native float getX(); public native float getY(); public native float getZ(); public Point3Temp getPosition() { return Point3Temp.make(this.getX(), this.getY(), this.getZ()); } public void setZ(float z) { this.moveTo(this.getX(), this.getY(), z); } public float getScaleX() { return this.xScale; } public float getScaleY() { return this.yScale; } public float getScaleZ() { return this.zScale; } public Point3Temp getScale() { return Point3Temp.make(this.getScaleX(), this.getScaleY(), this.getScaleZ()); } public float getTotalScale() { return (this.getScaleX() + this.getScaleY() + this.getScaleZ()) / 3.0F; } public native float getYaw(); public native float getPitch(); public native float getSpin(Point3Temp var1); protected void noteTransformChange() { } public Transform raise(float z) { return this.moveBy(0.0F, 0.0F, z); } public Transform lower(float z) { return this.moveBy(0.0F, 0.0F, -z); } public Transform yaw(float a) { return this.spin(0.0F, 0.0F, 1.0F, a); } public Transform roll(float a) { return this.spin(0.0F, 1.0F, 0.0F, a); } public Transform pitch(float a) { return this.spin(1.0F, 0.0F, 0.0F, a); } public Transform scale(float s) { return this.scale(s, s, s); } @Override protected void finalize() { this.nativeFinalize(); super.finalize(); } public native Transform pre(Transform var1); public Transform post(Transform t) { Point3Temp v = t.getScale(); if (!this.checkPostScale(v)) { return this; } else { this.xScale = this.xScale * v.x; this.yScale = this.yScale * v.y; this.zScale = this.zScale * v.z; return this.postHelper(t); } } private boolean checkPostScale(Point3Temp v) { if (this.getSpin(Point3Temp.make()) != 0.0F && (v.x != v.y || v.y != v.z)) { Console.println(Console.message("non-uniform")); return false; } else { return true; } } private native Transform postHelper(Transform var1); public native Transform makeIdentity(); public Transform moveBy(Point3Temp p) { return this.moveBy(p.x, p.y, p.z); } public native Transform moveBy(float var1, float var2, float var3); public Transform moveTo(Point3Temp p) { return this.moveTo(p.x, p.y, p.z); } public native Transform moveTo(float var1, float var2, float var3); public native Transform premoveBy(float var1, float var2, float var3); public native Transform scale(float var1, float var2, float var3); public Transform scale(Point3Temp s) { return this.scale(s.x, s.y, s.z); } public void setScale(float x, float y, float z) { this.scale(x / this.xScale, y / this.yScale, z / this.zScale); } public void setScale(Point3Temp s) { this.setScale(s.x, s.y, s.z); } public Transform postscale(float x, float y, float z) { return this.postscale(Point3Temp.make(x, y, z)); } public Transform postscale(Point3Temp s) { if (!this.checkPostScale(s)) { return this; } else { this.xScale = this.xScale * s.x; this.yScale = this.yScale * s.y; this.zScale = this.zScale * s.z; return this.postscaleHelper(s.x, s.y, s.z); } } private native Transform postscaleHelper(float var1, float var2, float var3); public Transform worldScale(float x, float y, float z) { return this.worldScale(Point3Temp.make(x, y, z)); } public Transform worldScale(Point3Temp s) { return !this.checkPostScale(s) ? this : this.scale(s); } public native Transform spin(float var1, float var2, float var3, float var4); public Transform spin(Point3Temp p, float a) { return this.spin(p.x, p.y, p.z, a); } public Transform postspin(Point3Temp axis, float a) { return this.postspin(axis.x, axis.y, axis.z, a); } public native Transform postspin(float var1, float var2, float var3, float var4); public Transform worldSpin(float x, float y, float z, float a) { return this.spin(this.worldVecToObjectVec(Point3Temp.make(x, y, z)), a); } private native void nativeFinalize(); public Transform getTransform() { Transform t = make(); t.setTransform(this); return t; } public native void setTransform(Transform var1); public native Transform invert(); public Transform getObjectToWorldMatrix() { return this.getTransform(); } private Point3Temp worldVecToObjectVec(Point3Temp worldVec) { Transform inv = this.getObjectToWorldMatrix().invert(); Point3Temp p = Point3Temp.make(worldVec).vectorTimes(inv).times(this.getScale()); inv.recycle(); return p; } @Override public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException { Object ret = null; switch (index - offset) { case 0: if (mode == 0) { ret = TransformPropertyEditor.make(new Property(this, index, "Transform")); } else if (mode == 1) { ret = this.getTransform(); } else if (mode == 2) { this.setTransform((Transform)value); } break; default: ret = super.properties(index, offset + 1, mode, value); } return ret; } @Override public void saveState(Saver s) throws IOException { s.saveVersion(2, classCookie); super.saveState(s); s.saveFloat(this.xScale); s.saveFloat(this.yScale); s.saveFloat(this.zScale); float[] guts = this.getGuts(); for (int i = 0; i < 16; i++) { s.saveFloat(guts[i]); } } @Override public void restoreState(Restorer r) throws IOException, TooNewException { switch (r.restoreVersion(classCookie)) { case 1: super.restoreState(r); case 0: this.xScale = this.yScale = this.zScale = r.restoreFloat(); float[] guts = new float[16]; for (int i = 0; i < 16; i++) { guts[i] = r.restoreFloat(); } this.setGuts(guts); break; case 2: super.restoreState(r); this.xScale = r.restoreFloat(); this.yScale = r.restoreFloat(); this.zScale = r.restoreFloat(); float[] guts = new float[16]; for (int i = 0; i < 16; i++) { guts[i] = r.restoreFloat(); } this.setGuts(guts); break; default: throw new TooNewException(); } } private native float[] getGuts(); public native boolean isTransformEqual(Transform var1); private native void setGuts(float[] var1); public String toTransformSubstring() { Point3Temp axis = Point3Temp.make(); float angle = this.getSpin(axis); Point3Temp pos = this.getPosition(); return angle == 0.0F && this.xScale == 1.0F && this.yScale == 1.0F && this.zScale == 1.0F && pos.x == 0.0F && pos.y == 0.0F && pos.z == 0.0F ? "[identity]" : "[pos (" + pos + "), scale (" + (this.xScale == this.yScale && this.yScale == this.zScale ? "" + this.xScale : this.xScale + "," + this.yScale + "," + this.zScale) + "), rot (" + axis + "@" + angle + ")]"; } @Override public String toString() { return this.getName() + this.toTransformSubstring(); } public Transform printGuts() { float[] arr = this.getGuts(); assert arr.length == 16; String[] strs = new String[16]; int maxStr = 0; for (int i = 0; i < 16; i++) { strs[i] = Float.toString(arr[i]); maxStr = Math.max(maxStr, strs[i].length()); } for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { String s = strs[j * 4 + k]; int pad = maxStr - s.length() + 1; System.out.print(s); while (pad-- != 0) { System.out.print(" "); } } System.out.println(""); } return this; } }