Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Shape.java
- // Easel
- //
- // Created by Philip Rhodes on 7/26/05.
- // Copyright 2005 __MyCompanyName__. All rights reserved.
- //
- /** The shape class is the parent class for all shapes. It maintains
- * the vertices and triangles for a shape as arrays of floats and ints,
- * respectively. It also maintains matrices for rotation, scaling, and
- * translation in object space. These transformations are applied in
- * the following order: scaling, translation, x rotation, y rotation,
- * and finally z rotation.
- */
- public abstract class Shape {
- protected float [] vertices;
- protected int [] triangles;
- protected RotationMatrixX rx;
- protected RotationMatrixY ry;
- protected RotationMatrixZ rz;
- protected TranslationMatrix tr;
- protected ScalingMatrix s;
- protected boolean matrixChanged = true;
- /** Create a shape with the specified number of vertices and triangles.
- * All transformation matrices are set to the identity matrix.
- */
- protected Shape(int numVertices, int numTriangles){
- this.vertices = new float[numVertices * 3];
- this.triangles = new int[numTriangles * 3];
- this.rx = new RotationMatrixX();
- this.ry = new RotationMatrixY();
- this.rz = new RotationMatrixZ();
- this.tr = new TranslationMatrix();
- this.s = new ScalingMatrix();
- }
- /** Create a shape with the specified number of vertices and triangles.
- * Transformation matrices are created with the specified values for
- * scaling, translation, and rotation.
- */
- protected Shape(int numVertices, int numTriangles,
- double xrot, double yrot, double zrot,
- double dx, double dy, double dz,
- double sx, double sy, double sz ){
- this.vertices = new float[numVertices * 3];
- this.triangles = new int[numTriangles * 3];
- this.rx = new RotationMatrixX(xrot);
- this.ry = new RotationMatrixY(yrot);
- this.rz = new RotationMatrixZ(zrot);
- this.tr = new TranslationMatrix(dx, dy, dz);
- this.s = new ScalingMatrix(sx, sy, sz);
- }
- /** Set the triangle specified by the first parameter to the vertex
- * indices vn0, vn1, and vn2.
- */
- public void setTriangle(int tNum, int vn0, int vn1, int vn2){
- this.triangles[3 * tNum] = vn0;
- this.triangles[3 * tNum + 1] = vn1;
- this.triangles[3 * tNum + 2] = vn2;
- }
- /** Set the triangle specified by the first parameter to the vertex
- * indices contained in the array.
- */
- public void setTriangle(int tNum, int vNums[]){
- this.triangles[3 * tNum] = vNums[0];
- this.triangles[3 * tNum + 1] = vNums[1];
- this.triangles[3 * tNum + 2] = vNums[2];
- }
- /** Set the vertex specified by the first parameter to the value of
- * the given coordinates.
- */
- public void setVertex(int vNum, float xcoord, float ycoord, float zcoord){
- this.vertices[3 * vNum] = xcoord;
- this.vertices[3 * vNum + 1] = ycoord;
- this.vertices[3 * vNum + 2] = zcoord;
- }
- /** Set the vertex specified by the first parameter to the value of
- * the given coordinates. This version takes coordinates as doubles
- * for convenience.
- */
- public void setVertex(int vNum, double xcoord, double ycoord, double zcoord){
- this.vertices[3 * vNum] = (float) xcoord;
- this.vertices[3 * vNum + 1] = (float) ycoord;
- this.vertices[3 * vNum + 2] = (float) zcoord;
- }
- /** Set the vertex specified by the first parameter to the values
- * in the given array.
- */
- public void setVertex(int vNum, float [] vertex){
- this.vertices[3 * vNum] = vertex[0];
- this.vertices[3 * vNum + 1] = vertex[1];
- this.vertices[3 * vNum + 2] = vertex[2];
- }
- /** Set the parameter matrix equal to the object space transformation matrix
- * for this shape.
- */
- public Matrix getMatrix(Matrix m){
- m.copy(this.s);
- m.lMult(this.tr);
- m.lMult(this.rx);
- m.lMult(this.ry);
- m.lMult(this.rz);
- this.matrixChanged = false;
- return m;
- }
- /** Returns true if any of the methods that modify the object space
- * space transformation matrix have been called since the last call to
- * getMatrix().
- */
- public boolean matrixChanged(){
- return this.matrixChanged;
- }
- /** Set the object space x rotation to the specified value (in radians).
- * This method causes matrixChanged() to return true until
- * the next call to getMatrix().
- */
- public void setXRotation(double theta){
- this.rx.setAngle(theta);
- this.matrixChanged = true;
- }
- /** Set the object space y rotation to the specified value (in radians).
- * This method causes matrixChanged() to return true until
- * the next call to getMatrix().
- */
- public void setYRotation(double theta){
- this.ry.setAngle(theta);
- this.matrixChanged = true;
- }
- /** Set the object space z rotation to the specified value (in radians).
- * This method causes matrixChanged() to return true until
- * the next call to getMatrix().
- */
- public void setZRotation(double theta){
- this.rz.setAngle(theta);
- this.matrixChanged = true;
- }
- /** Set the object space translation to the specified values.
- * This method causes matrixChanged() to return true until
- * the next call to getMatrix().
- */
- public void setTranslation(double dx, double dy, double dz){
- this.tr.setDistances(dx, dy, dz);
- this.matrixChanged = true;
- }
- /** Set the object space scaling to the specified values.
- * This method causes matrixChanged() to return true until
- * the next call to getMatrix().
- */
- public void setScales(double sx, double sy, double sz){
- this.s.setScales(sx, sy, sz);
- this.matrixChanged = true;
- }
- /** Return the number of triangles in this shape. */
- public int numTriangles(){
- return this.triangles.length / 3;
- }
- /** Return the number of vertices in this shape. */
- public int numVertices(){
- return this.vertices.length / 3;
- }
- /** Given an array of nine floats, loads the coordinates of the three
- * vertices of the specified triangle into the array. A reference to
- * the array is returned as a convenience.
- */
- public float [] getTriangleVertices(int tNum, float [] vertices){
- if(vertices.length < 9) {
- System.out.println("Shape.java: getTriangleVertices(int, float[]): vertices argument must have 9 elements.");
- System.exit(1);
- }
- vertices[0] = this.vertices[ 3 * this.triangles[tNum * 3] ];
- vertices[1] = this.vertices[ 3 * this.triangles[tNum * 3] + 1 ];
- vertices[2] = this.vertices[ 3 * this.triangles[tNum * 3] + 2 ];
- vertices[3] = this.vertices[ 3 * this.triangles[tNum * 3 + 1] ];
- vertices[4] = this.vertices[ 3 * this.triangles[tNum * 3 + 1] + 1 ];
- vertices[5] = this.vertices[ 3 * this.triangles[tNum * 3 + 1] + 2 ];
- vertices[6] = this.vertices[ 3 * this.triangles[tNum * 3 + 2] ];
- vertices[7] = this.vertices[ 3 * this.triangles[tNum * 3 + 2] + 1 ];
- vertices[8] = this.vertices[ 3 * this.triangles[tNum * 3 + 2] + 2 ];
- return vertices;
- }
- /** Copy the shape vertex coordinates into the array parameter. The array
- * is written to starting at the index 3*starVnum, and copying continues
- * until all vertices for this shape have been copied. It is the
- * caller's responsibility to provide an array with sufficient space.
- * A reference to the array parameter is returned as a convenience.
- */
- public float [] getAllVertices(float [] vertices, int startVnum){
- int n = this.vertices.length;
- for(int i=0; i< n; i++){
- vertices[ 3 * startVnum + i] = this.vertices[i];
- }
- return vertices;
- }
- /** Copy the shape vertex indices (numbers) into the array parameter. The array
- * is written to starting at the index 3*startVnum, and copying continues
- * until all vertex numbers for this shape have been copied. It is the
- * caller's responsibility to provide an array with sufficient space. For each
- * triangle index, the value of startVnum will be added before it is placed
- * in the array parameter. A reference to the array parameter is returned
- * as a convenience.
- */
- public int [] getAllTriangles(int [] triangles, int startTnum, int startVnum){
- int n = this.triangles.length;
- for(int i=0; i< n; i++){
- triangles[ 3 * startTnum + i] = this.triangles[i] + startVnum;
- }
- return triangles;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement