package myGraphics.Kinematics; import myGraphics.Shape3d.*; import myGraphics.VectorGrafix.*; import myGraphics.Perspective.*; import java.awt.Graphics; import java.awt.Color; public class Model extends myGraphics.Shape3d.Shape { public Transformation3d defaultTrans; // default transformation public double Tx, Ty, Tz; // current translation relative to parent public double Rx, Ry, Rz; // current rotation relative to parent public Vector3d maxRot; // maximum rotation angles in (x, y, z) public Vector3d minRot; // minimum rotation angles in (x, y, z) public static final int MAX_CHILD = 20; protected int childCount = 0; public Model child[]; public Model() { super(); defaultTrans = new Transformation3d(); Tx = 0.0; Ty = 0.0; Tz = 0.0; Rx = 0.0; Ry = 0.0; Rz = 0.0; childCount = 0; child = new Model[MAX_CHILD]; // assume no constraints on rotation maxRot = new Vector3d(2 * Math.PI, 2 * Math.PI, 2 * Math.PI); minRot = maxRot.negate(); } public Model(Shape sourceShape) { super(sourceShape); defaultTrans = sourceShape.trans.copy(); Tx = 0.0; Ty = 0.0; Tz = 0.0; Rx = 0.0; Ry = 0.0; Rz = 0.0; childCount = 0; child = new Model[MAX_CHILD]; // assume no constraints on rotation maxRot = new Vector3d(2 * Math.PI, 2 * Math.PI, 2 * Math.PI); minRot = maxRot.negate(); } public Model(Transformation3d dTrans) { super(); defaultTrans = dTrans.copy(); trans = dTrans.copy(); Tx = 0.0; Ty = 0.0; Tz = 0.0; Rx = 0.0; Ry = 0.0; Rz = 0.0; childCount = 0; child = new Model[MAX_CHILD]; // assume no constraints on rotation maxRot = new Vector3d(2 * Math.PI, 2 * Math.PI, 2 * Math.PI); minRot = maxRot.negate(); } public Model(Shape sourceShape, Transformation3d dTrans) { super(sourceShape); defaultTrans = dTrans.copy(); Tx = 0.0; Ty = 0.0; Tz = 0.0; Rx = 0.0; Ry = 0.0; Rz = 0.0; childCount = 0; child = new Model[MAX_CHILD]; // assume no constraints on rotation maxRot = new Vector3d(2 * Math.PI, 2 * Math.PI, 2 * Math.PI); minRot = maxRot.negate(); } public Model copyModel() { Model target = new Model(); /* general Shape members */ target.crossSection = crossSection.copy(); target.profile = profile.copy(); target.refPoint = refPoint.copy(); target.trans = trans.copy(); target.crossColor = crossColor; target.proColor = proColor; /* specific Model members */ target.defaultTrans = defaultTrans.copy(); target.Tx = Tx; target.Ty = Ty; target.Tz = Tz; target.Rx = Rx; target.Ry = Ry; target.Rz = Rz; for (int i = 0; i < childCount; i++) { target.child[i] = child[i].copyModel(); } target.maxRot = maxRot.copy(); target.minRot = minRot.copy(); return target; } public boolean attachChild(Model fetus) { if (childCount < MAX_CHILD - 1) { child[childCount] = fetus; // link to the new child (not a copy) childCount++; return true; // successful } else { return false; // unsuccessful - too many children } } public boolean attachChild(Model fetus, double theTx, double theTy, double theTz) { if (attachChild(fetus)) { /* Set the relative translation of the new child */ fetus.Tx = theTx; fetus.Ty = theTy; fetus.Tz = theTz; return true; } else { return false; } } public void renderModel(Graphics g, Camera c, Viewport v, StackMachine s) { /* Recursively render this model and all of its children */ s.translate(Tx, Ty, Tz); s.rotate(Rx, Ry, Rz); s.render(this, g, c, v); for (int i = 0; i < childCount; i++) { if (childCount > 1) { s.push(); } this.child[i].renderModel(g, c, v, s); if (childCount > 1) { s.pop(); } } } public void setRConstraints(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) { /* Set the rotational constraints of this model */ minRot = new Vector3d(minX, minY, minZ); maxRot = new Vector3d(maxX, maxY, maxZ); } }