Advertisement
Luninariel

289 - Shape

Apr 8th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.95 KB | None | 0 0
  1. //
  2. //  Shape.java
  3. //  Easel
  4. //
  5. //  Created by Philip Rhodes on 7/26/05.
  6. //  Copyright 2005 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. /** The shape class is the parent class for all shapes.  It maintains
  10.  *  the vertices and triangles for a shape as arrays of floats and ints,
  11.  *  respectively. It also maintains matrices for rotation, scaling, and
  12.  *  translation in object space. These transformations are applied in
  13.  *  the following order: scaling, translation, x rotation, y rotation,
  14.  *  and finally z rotation.
  15.  */
  16. public abstract class Shape {
  17.  
  18.    protected float [] vertices;
  19.    protected int [] triangles;
  20.    
  21.    protected RotationMatrixX rx;
  22.    protected RotationMatrixY ry;
  23.    protected RotationMatrixZ rz;
  24.    
  25.    protected TranslationMatrix tr;
  26.    protected ScalingMatrix s;
  27.    
  28.    protected boolean matrixChanged = true;
  29.    
  30.    
  31.    /** Create a shape with the specified number of vertices and triangles.
  32.     *  All transformation matrices are set to the identity matrix.
  33.     */
  34.    protected Shape(int numVertices, int numTriangles){
  35.    
  36.       this.vertices = new float[numVertices * 3];
  37.       this.triangles = new int[numTriangles * 3];
  38.      
  39.       this.rx = new RotationMatrixX();
  40.       this.ry = new RotationMatrixY();
  41.       this.rz = new RotationMatrixZ();
  42.      
  43.       this.tr = new TranslationMatrix();
  44.       this.s  = new ScalingMatrix();
  45.      
  46.      
  47.    }
  48.  
  49.    /** Create a shape with the specified number of vertices and triangles.
  50.     *  Transformation matrices are created with the specified values for
  51.     *  scaling, translation, and rotation.
  52.     */
  53.    protected Shape(int numVertices, int numTriangles,
  54.                   double xrot, double yrot, double zrot,
  55.                   double dx, double dy, double dz,
  56.                   double sx, double sy, double sz ){
  57.    
  58.       this.vertices = new float[numVertices * 3];
  59.       this.triangles = new int[numTriangles * 3];
  60.      
  61.       this.rx = new RotationMatrixX(xrot);
  62.       this.ry = new RotationMatrixY(yrot);
  63.       this.rz = new RotationMatrixZ(zrot);
  64.      
  65.       this.tr = new TranslationMatrix(dx, dy, dz);
  66.       this.s  = new ScalingMatrix(sx, sy, sz);
  67.    }
  68.  
  69.    /** Set the triangle specified by the first parameter to the vertex
  70.      * indices vn0, vn1, and vn2.
  71.      */
  72.    public void setTriangle(int tNum, int vn0, int vn1, int vn2){
  73.    
  74.       this.triangles[3 * tNum]     = vn0;
  75.       this.triangles[3 * tNum + 1] = vn1;
  76.       this.triangles[3 * tNum + 2] = vn2;
  77.    }
  78.  
  79.    /** Set the triangle specified by the first parameter to the vertex
  80.      * indices contained in the array.
  81.      */
  82.    public void setTriangle(int tNum, int vNums[]){
  83.    
  84.       this.triangles[3 * tNum]     = vNums[0];
  85.       this.triangles[3 * tNum + 1] = vNums[1];
  86.       this.triangles[3 * tNum + 2] = vNums[2];
  87.    }
  88.    
  89.    /** Set the vertex specified by the first parameter to the value of
  90.      * the given coordinates.
  91.      */  
  92.    public void setVertex(int vNum, float xcoord, float ycoord, float zcoord){
  93.    
  94.       this.vertices[3 * vNum]     = xcoord;
  95.       this.vertices[3 * vNum + 1] = ycoord;
  96.       this.vertices[3 * vNum + 2] = zcoord;
  97.    }
  98.  
  99.    /** Set the vertex specified by the first parameter to the value of
  100.      * the given coordinates. This version takes coordinates as doubles
  101.      * for convenience.
  102.      */  
  103.    public void setVertex(int vNum, double xcoord, double ycoord, double zcoord){
  104.    
  105.       this.vertices[3  * vNum]    = (float) xcoord;
  106.       this.vertices[3 * vNum + 1] = (float) ycoord;
  107.       this.vertices[3 * vNum + 2] = (float) zcoord;
  108.    }
  109.  
  110.    
  111.    /** Set the vertex specified by the first parameter to the values
  112.      * in the given array.
  113.      */  
  114.    public void setVertex(int vNum, float [] vertex){
  115.    
  116.       this.vertices[3  * vNum]    = vertex[0];
  117.       this.vertices[3 * vNum + 1] = vertex[1];
  118.       this.vertices[3 * vNum + 2] = vertex[2];
  119.    }
  120.    
  121.    /** Set the parameter matrix equal to the object space transformation matrix
  122.      * for this shape.
  123.      */
  124.    public Matrix getMatrix(Matrix m){
  125.    
  126.       m.copy(this.s);
  127.       m.lMult(this.tr);
  128.       m.lMult(this.rx);
  129.       m.lMult(this.ry);
  130.       m.lMult(this.rz);
  131.      
  132.       this.matrixChanged = false;
  133.    
  134.       return m;
  135.    }
  136.    
  137.    /** Returns true if any of the methods that modify the object space
  138.     *  space transformation matrix have been called since the last call to
  139.     *  getMatrix().
  140.     */
  141.    public boolean matrixChanged(){
  142.    
  143.       return this.matrixChanged;
  144.    }
  145.    
  146.    /** Set the object space x rotation to the specified value (in radians).
  147.     *  This method causes matrixChanged() to return true until
  148.     *  the next call to getMatrix().
  149.    */
  150.    public void setXRotation(double theta){
  151.    
  152.       this.rx.setAngle(theta);
  153.       this.matrixChanged = true;
  154.    }
  155.    
  156.    /** Set the object space y rotation to the specified value (in radians).
  157.     *  This method causes matrixChanged() to return true until
  158.     *  the next call to getMatrix().
  159.    */
  160.    public void setYRotation(double theta){
  161.    
  162.       this.ry.setAngle(theta);
  163.       this.matrixChanged = true;
  164.    }
  165.  
  166.    /** Set the object space z rotation to the specified value (in radians).
  167.     *  This method causes matrixChanged() to return true until
  168.     *  the next call to getMatrix().
  169.    */
  170.    public void setZRotation(double theta){
  171.    
  172.       this.rz.setAngle(theta);
  173.       this.matrixChanged = true;
  174.    }
  175.  
  176.    /** Set the object space translation to the specified values.
  177.     *  This method causes matrixChanged() to return true until
  178.     *  the next call to getMatrix().
  179.    */  
  180.    public void setTranslation(double dx, double dy, double dz){
  181.    
  182.       this.tr.setDistances(dx, dy, dz);
  183.       this.matrixChanged = true;
  184.    }
  185.  
  186.    /** Set the object space scaling to the specified values.
  187.     *  This method causes matrixChanged() to return true until
  188.     *  the next call to getMatrix().
  189.    */  
  190.    public void setScales(double sx, double sy, double sz){
  191.    
  192.       this.s.setScales(sx, sy, sz);
  193.       this.matrixChanged = true;
  194.    }
  195.    
  196.    /** Return the number of triangles in this shape. */
  197.    public int numTriangles(){
  198.    
  199.       return this.triangles.length / 3;
  200.    }
  201.  
  202.    /** Return the number of vertices in this shape. */
  203.    public int numVertices(){
  204.    
  205.       return this.vertices.length / 3;
  206.    }
  207.    
  208.    /** Given an array of nine floats, loads the coordinates of the three
  209.     *  vertices of the specified triangle into the array. A reference to
  210.     *  the array is returned as a convenience.
  211.     */
  212.    public float [] getTriangleVertices(int tNum, float [] vertices){
  213.    
  214.       if(vertices.length < 9) {
  215.      
  216.          System.out.println("Shape.java: getTriangleVertices(int, float[]): vertices argument must have 9 elements.");
  217.          System.exit(1);
  218.       }
  219.      
  220.       vertices[0] = this.vertices[ 3 * this.triangles[tNum * 3]     ];
  221.       vertices[1] = this.vertices[ 3 * this.triangles[tNum * 3] + 1 ];
  222.       vertices[2] = this.vertices[ 3 * this.triangles[tNum * 3] + 2 ];
  223.  
  224.       vertices[3] = this.vertices[ 3 * this.triangles[tNum * 3 + 1]     ];
  225.       vertices[4] = this.vertices[ 3 * this.triangles[tNum * 3 + 1] + 1 ];
  226.       vertices[5] = this.vertices[ 3 * this.triangles[tNum * 3 + 1] + 2 ];
  227.  
  228.       vertices[6] = this.vertices[ 3 * this.triangles[tNum * 3 + 2]     ];
  229.       vertices[7] = this.vertices[ 3 * this.triangles[tNum * 3 + 2] + 1 ];
  230.       vertices[8] = this.vertices[ 3 * this.triangles[tNum * 3 + 2] + 2 ];
  231.      
  232.       return vertices;
  233.    }
  234.  
  235.    /** Copy the shape vertex coordinates into the array parameter. The array
  236.     *  is written to starting at the index 3*starVnum, and copying continues
  237.     *  until all vertices for this shape have been copied. It is the
  238.     *  caller's responsibility to provide an array with sufficient space.
  239.     *  A reference to the array parameter is returned as a convenience.
  240.     */
  241.    public float [] getAllVertices(float [] vertices, int startVnum){
  242.    
  243.       int n = this.vertices.length;
  244.    
  245.       for(int i=0; i< n; i++){
  246.      
  247.          vertices[ 3 * startVnum + i] = this.vertices[i];
  248.      
  249.       }
  250.    
  251.       return vertices;
  252.    }
  253.  
  254.  
  255.    /** Copy the shape vertex indices (numbers) into the array parameter. The array
  256.     *  is written to starting at the index 3*startVnum, and copying continues
  257.     *  until all vertex numbers for this shape have been copied. It is the
  258.     *  caller's responsibility to provide an array with sufficient space. For each
  259.     *  triangle index, the value of startVnum will be added before it is placed
  260.     *  in the array parameter. A reference to the array parameter is returned
  261.     *  as a convenience.
  262.     */
  263.    public int [] getAllTriangles(int [] triangles, int startTnum, int startVnum){
  264.    
  265.       int n = this.triangles.length;
  266.      
  267.       for(int i=0; i< n; i++){
  268.      
  269.          triangles[ 3 * startTnum + i] = this.triangles[i] + startVnum;
  270.      
  271.       }
  272.    
  273.       return triangles;
  274.    }
  275.    
  276.    
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement