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