import java.lang.Math.*; /** * A Matrix class for transforming the WireFrameObject * * @author Nisit Patel * @version 0.1, 1 May, 2003 * */ class TransformMatrix { private float theMatrix[][]; TransformMatrix() { this.theMatrix = new float[4][4]; } public void createZRotationMatrix(double rotation) { theMatrix[0][0] = (float) Math.cos(Math.toRadians(rotation)); theMatrix[0][1] = (float) -(Math.sin(Math.toRadians(rotation))); theMatrix[0][2] = 0; theMatrix[0][3] = 0; theMatrix[1][0] = (float) Math.sin(Math.toRadians(rotation)); theMatrix[1][1] = (float) Math.cos(Math.toRadians(rotation)); theMatrix[1][2] = 0; theMatrix[1][3] = 0; theMatrix[2][0] = 0; theMatrix[2][1] = 0; theMatrix[2][2] = 1; theMatrix[2][3] = 0; theMatrix[3][0] = 0; theMatrix[3][1] = 0; theMatrix[3][2] = 0; theMatrix[3][3] = 1; } public void createYRotationMatrix(double rotation) { theMatrix[0][0] = (float) Math.cos(Math.toRadians(rotation)); theMatrix[0][1] = 0; theMatrix[0][2] = (float) Math.sin(Math.toRadians(rotation)); theMatrix[0][3] = 0; theMatrix[1][0] = 0; theMatrix[1][1] = 1; theMatrix[1][2] = 0; theMatrix[1][3] = 0; theMatrix[2][0] = (float) -(Math.sin(Math.toRadians(rotation))); theMatrix[2][1] = 0; theMatrix[2][2] = (float) Math.cos(Math.toRadians(rotation)); theMatrix[2][3] = 0; theMatrix[3][0] = 0; theMatrix[3][1] = 0; theMatrix[3][2] = 0; theMatrix[3][3] = 1; } public void createXRotationMatrix(double rotation) { theMatrix[0][0] = 1; theMatrix[0][1] = 0; theMatrix[0][2] = 0; theMatrix[0][3] = 0; theMatrix[1][0] = 0; theMatrix[1][1] = (float) Math.cos(Math.toRadians(rotation)); theMatrix[1][2] = (float) -(Math.sin(Math.toRadians(rotation))); theMatrix[1][3] = 0; theMatrix[2][0] = 0; theMatrix[2][1] = (float) Math.sin(Math.toRadians(rotation)); theMatrix[2][2] = (float) Math.cos(Math.toRadians(rotation)); theMatrix[2][3] = 0; theMatrix[3][0] = 0; theMatrix[3][1] = 0; theMatrix[3][2] = 0; theMatrix[3][3] = 1; } public void createTranslateToOriginMatrix(WireFrame theWireFrame) { // Takes the first point, and creates a matrix that will move // the entire wireframe relative to that point WirePointVector newOrigin = ((WireLine) theWireFrame.myLines.get(0)).getStartPoint(); theMatrix[0][0] = 1; theMatrix[0][1] = 0; theMatrix[0][2] = 0; theMatrix[0][3] = 0 - newOrigin.getX(); theMatrix[1][0] = 0; theMatrix[1][1] = 1; theMatrix[1][2] = 0; theMatrix[1][3] = 0 - newOrigin.getY(); theMatrix[2][0] = 0; theMatrix[2][1] = 0; theMatrix[2][2] = 1; theMatrix[2][3] = 0 - newOrigin.getZ(); theMatrix[3][0] = 0; theMatrix[3][1] = 0; theMatrix[3][2] = 0; theMatrix[3][3] = 1; } public void createTranslateFromOriginMatrix(WireFrame theWireFrame) { WirePointVector newOrigin = ((WireLine) theWireFrame.myLines.get(0)).getStartPoint(); theMatrix[0][0] = 1; theMatrix[0][1] = 0; theMatrix[0][2] = 0; theMatrix[0][3] = newOrigin.getX(); theMatrix[1][0] = 0; theMatrix[1][1] = 1; theMatrix[1][2] = 0; theMatrix[1][3] = newOrigin.getY(); theMatrix[2][0] = 0; theMatrix[2][1] = 0; theMatrix[2][2] = 1; theMatrix[2][3] = newOrigin.getZ(); theMatrix[3][0] = 0; theMatrix[3][1] = 0; theMatrix[3][2] = 0; theMatrix[3][3] = 1; } public WirePointVector dot(WirePointVector vector) { return new WirePointVector( ((theMatrix[0][0] * vector.getX()) + (theMatrix[0][1] * vector.getY()) + (theMatrix[0][2] * vector.getZ()) + (theMatrix[0][3] * vector.getT())), ((theMatrix[1][0] * vector.getX()) + (theMatrix[1][1] * vector.getY()) + (theMatrix[1][2] * vector.getZ()) + (theMatrix[1][3] * vector.getT())), ((theMatrix[2][0] * vector.getX()) + (theMatrix[2][1] * vector.getY()) + (theMatrix[2][2] * vector.getZ()) + (theMatrix[2][3] * vector.getT())), ((theMatrix[3][0] * vector.getX()) + (theMatrix[3][1] * vector.getY()) + (theMatrix[3][2] * vector.getZ()) + (theMatrix[3][3] * vector.getT()))); } public WireFrame dotWireFrame(WireFrame theWireFrame) { WireFrame newFrame = new WireFrame(); for (int i = 0; i < theWireFrame.numberOfLines(); i++) { WireLine tempLine = (WireLine) theWireFrame.myLines.get(i); WirePointVector newStart = this.dot(tempLine.getStartPoint()); WirePointVector newEnd = this.dot(tempLine.getEndPoint()); WireLine transformedLine = new WireLine(newStart, newEnd); newFrame.addLine(transformedLine); } return newFrame; } public WireFrame createSideViewFrame(WireFrame theWireFrame) { // Rotate 90 degrees in the Y axis this.createYRotationMatrix(90); // Create the new WireFrame WireFrame sideView = this.dotWireFrame(theWireFrame); // Return the new WireFrame return sideView; } public WireFrame createTopViewFrame(WireFrame theWireFrame) { // Rotate 90 degrees in the Y axis this.createXRotationMatrix(90); // Create the new WireFrame WireFrame topView = this.dotWireFrame(theWireFrame); // Return the new WireFrame return topView; } public void printMatrix() { System.out.println("[" + theMatrix[0][0] + " " + theMatrix[0][1] + " " + theMatrix[0][2] + " " + theMatrix[0][3]); System.out.println(" " + theMatrix[1][0] + " " + theMatrix[1][1] + " " + theMatrix[1][2] + " " + theMatrix[1][3]); System.out.println(" " + theMatrix[2][0] + " " + theMatrix[2][1] + " " + theMatrix[2][2] + " " + theMatrix[2][3]); System.out.println(" " + theMatrix[3][0] + " " + theMatrix[3][1] + " " + theMatrix[3][2] + " " + theMatrix[3][3] + "]"); } }