001/*
002
003*/
004
005package renderer.models;
006import  renderer.scene.*;
007
008/**
009   Create a wireframe model of a cube with its center
010   at the origin, having edge length 2, and with its
011   corners at {@code (±1, ±1, ±1)}.
012<p>
013   This version of the cube model has the top and bottom
014   faces of the cube cut up by a triangle fan and the
015   front, back, right, and left faces cut up by a grid
016   of perpendicular lines..
017<p>
018   Here is a picture showing how the cube's eight corners
019   are labeled.
020<pre>{@code
021                  v[4]
022                   +-----------------+ v[5]
023                  /|                /|
024                /  |              /  |
025              /    |            /    |
026            /      |          /      |
027      v[7] +-----------------+ v[6]  |
028           |       |         |       |               y
029           |       |         |       |               |
030           |       |         |       |               |
031           |  v[0] +---------|-------+ v[1]          |
032           |      /          |      /                |
033           |    /            |    /                  +----. x
034           |  /              |  /                   /
035           |/                |/                    /
036           +-----------------+                    /
037          v[3]              v[2]                 z
038}</pre>
039
040   @see Cube
041   @see Cube2
042   @see Cube3
043*/
044public class Cube4 extends Cube
045{
046   /**
047      Create a cube with its center at the origin, having edge
048      length 2, with its corners at {@code (±1, ±1, ±1)}. with
049      a triangle fan of eight triangles in top and bottom faces,
050      and two perpendicular lines cutting each of the front,
051      back, right, and left faces.
052   */
053   public Cube4( )
054   {
055      this(2, 1, 2);
056   }
057
058
059   /**
060      Create a cube with its center at the origin, having edge
061      length 2, with its corners at {@code (±1, ±1, ±1)}, and
062      with the top and bottom faces containing a triangle fan
063      with the given number of triangles along each of the x,
064      and z directions.
065      <p>
066      There must be at least one triangle along the x and z directions.
067
068      @param xCount  number of triangles along the x-direction
069      @param yGrid   number of grid lines perpendicular to the y-axis
070      @param zCount  number of triangles along the z-direction
071   */
072   public Cube4(int xCount, int yGrid, int zCount)
073   {
074      super();  // create the basic cube with 12 edges
075
076      if (xCount < 1) xCount = 1;
077      if (yGrid  < 0) yGrid  = 0;
078      if (zCount < 1) zCount = 1;
079
080      double xStep = 2.0 / xCount;
081      double yStep = 2.0 / (1 + yGrid);
082      double zStep = 2.0 / zCount;
083
084      // Triangles along all four edges parallel to the x-axis.
085      double x = -1.0;
086      for (int i = 0; i <= xCount; i++)
087      {  // top face, front and back edges
088         addLineSegment(new LineSegment(new Vertex(x,  1,  1), new Vertex(0,  1,  0)));
089         addLineSegment(new LineSegment(new Vertex(x,  1, -1), new Vertex(0,  1,  0)));
090         // bottom face, front and back edges
091         addLineSegment(new LineSegment(new Vertex(x, -1,  1), new Vertex(0, -1,  0)));
092         addLineSegment(new LineSegment(new Vertex(x, -1, -1), new Vertex(0, -1,  0)));
093         x += xStep;
094      }
095
096      // Grid lines perpendicular to the x-axis.
097      x = -1.0;
098      for (int i = 0; i < xCount; i++)
099      {
100         x += xStep;
101         // front and back faces only
102         addLineSegment(new LineSegment(new Vertex(x,  1,  1), new Vertex(x, -1,  1)));
103         addLineSegment(new LineSegment(new Vertex(x, -1, -1), new Vertex(x,  1, -1)));
104      }
105
106      // Grid lines perpendicular to the y-axis.
107      double y = -1.0;
108      for (int i = 0; i < yGrid; i++)
109      {
110         y += yStep;
111         // Start at the front, right edge, go left across the front face, and around the cube.
112         addLineSegment(new LineSegment(new Vertex( 1, y,  1), new Vertex(-1, y,  1)));
113         addLineSegment(new LineSegment(new Vertex(-1, y,  1), new Vertex(-1, y, -1)));
114         addLineSegment(new LineSegment(new Vertex(-1, y, -1), new Vertex( 1, y, -1)));
115         addLineSegment(new LineSegment(new Vertex( 1, y, -1), new Vertex( 1, y,  1)));
116      }
117
118      // Grid lines perpendicular to the z-axis.
119      double z = -1.0;
120      for (int i = 0; i < zCount; i++)
121      {
122         z += zStep;
123         // left and right faces only
124         addLineSegment(new LineSegment(new Vertex(-1,  1, z), new Vertex(-1, -1, z)));
125         addLineSegment(new LineSegment(new Vertex( 1, -1, z), new Vertex( 1,  1, z)));
126      }
127
128      // Triangles along all four edges parallel to the z-axis.
129      z = -1.0;
130      for (int i = 0; i <= zCount; i++)
131      {  // top face, right and left edges
132         addLineSegment(new LineSegment(new Vertex( 1,  1, z), new Vertex(0,  1,  0)));
133         addLineSegment(new LineSegment(new Vertex(-1,  1, z), new Vertex(0,  1,  0)));
134         // bottom face, right and left edges
135         addLineSegment(new LineSegment(new Vertex( 1, -1, z), new Vertex(0, -1,  0)));
136         addLineSegment(new LineSegment(new Vertex(-1, -1, z), new Vertex(0, -1,  0)));
137         z += zStep;
138      }
139   }
140}//Cube4