001/*
002 * Renderer Models. The MIT License.
003 * Copyright (c) 2022 rlkraft@pnw.edu
004 * See LICENSE for details.
005*/
006
007package renderer.models_L;
008
009import renderer.scene.*;
010import renderer.scene.primitives.*;
011
012/**
013   Create a wireframe model of a cube with its center
014   at the origin, having edge length 2, and with its
015   corners at {@code (±1, ±1, ±1)}.
016<p>
017   This version of the cube model has each face of
018   the cube cut up by an n by m grid of lines.
019<p>
020   Here is a picture showing how the cube's eight
021   corners are labeled.
022<pre>{@code
023                  v4=(-1,1,-1)
024                  +---------------------+ v5=(1,1,-1)
025                 /|                    /|
026                / |                   / |
027               /  |                  /  |
028              /   |                 /   |
029             /    |                /    |
030         v7 +---------------------+ v6  |
031            |     |               |     |
032            |     |               |     |
033            |     | v0=(-1,-1,-1) |     |
034            |     +---------------|-----+ v1=(1,-1,-1)
035            |    /                |    /
036            |   /                 |   /
037            |  /                  |  /
038            | /                   | /
039            |/                    |/
040            +---------------------+
041            v3=(-1,-1,1)          v2=(1,-1,1)
042}</pre>
043
044   @see Cube
045   @see Cube3
046   @see Cube4
047*/
048public class Cube2 extends Model
049{
050   /**
051      Create a cube with its center at the origin, having edge
052      length 2, with its corners at {@code (±1, ±1, ±1)}. and
053      with two perpendicular grid lines going across the middle
054      of each of the cube's faces.
055   */
056   public Cube2( )
057   {
058      this(1, 1, 1);
059   }
060
061
062   /**
063      Create a cube with its center at the origin, having edge
064      length 2, with its corners at {@code (±1, ±1, ±1)}, and
065      with each of the cube's faces containing the given number
066      of grid lines parallel to the x, y, and z directions.
067
068      @param xGrid  number of grid lines perpendicular to the x-axis
069      @param yGrid  number of grid lines perpendicular to the y-axis
070      @param zGrid  number of grid lines perpendicular to the z-axis
071      @throws IllegalArgumentException if {@code xGrid} is less than 0
072      @throws IllegalArgumentException if {@code yGrid} is less than 0
073      @throws IllegalArgumentException if {@code zGrid} is less than 0
074   */
075   public Cube2(final int xGrid, final int yGrid, final int zGrid)
076   {
077      super(String.format("Cube2(%d,%d,%d)", xGrid, yGrid, zGrid));
078
079      if (xGrid < 0)
080         throw new IllegalArgumentException("xGrid must be greater than or equal to 0");
081      if (yGrid < 0)
082         throw new IllegalArgumentException("yGrid must be greater than or equal to 0");
083      if (zGrid < 0)
084         throw new IllegalArgumentException("zGrid must be greater than or equal to 0");
085
086      final double xStep = 2.0 / (1 + xGrid),
087                   yStep = 2.0 / (1 + yGrid),
088                   zStep = 2.0 / (1 + zGrid);
089
090      // Grid lines perpendicular to the x-axis.
091      double x = -1.0;
092      for (int i = 0; i <= xGrid + 1; ++i)
093      {
094         final int start = vertexList.size();
095
096         // Start at the top, front edge, go down the front face, and around the cube.
097         double y = 1.0;
098         for (int j = 0; j <= yGrid; ++j)
099         {
100            addVertex( new Vertex(x, y, 1.0) );
101            y -= yStep;
102         }
103         double z = 1.0;
104         for (int j = 0; j <= zGrid; ++j)
105         {
106            addVertex( new Vertex(x, -1.0, z) );
107            z -= zStep;
108         }
109         y = -1.0;
110         for (int j = 0; j <= yGrid; ++j)
111         {
112            addVertex( new Vertex(x, y, -1.0) );
113            y += yStep;
114         }
115         z = -1.0;
116         for (int j = 0; j <= zGrid + 1; ++j)
117         {
118            addVertex( new Vertex(x, 1.0, z) );
119            z += zStep;
120         }
121         final int stop = vertexList.size();
122         // Note: stop - start =  2*yGrid + 2*zGrid + 5
123
124         for (int j = start; j < stop - 1; ++j)
125         {
126            addPrimitive(new LineSegment(j, j+1));
127         }
128
129         x += xStep;
130      }
131
132      // Grid lines perpendicular to the y-axis.
133      double y = -1.0;
134      for (int i = 0; i <= yGrid + 1; ++i)
135      {
136         final int start = vertexList.size();
137
138         // Start at the front, right edge, go left across the front face, and around the cube.
139         double x2 = 1.0;
140         for (int j = 0; j <= xGrid; ++j)
141         {
142            addVertex( new Vertex(x2, y, 1.0) );
143            x2 -= xStep;
144         }
145         double z = 1.0;
146         for (int j = 0; j <= zGrid; ++j)
147         {
148            addVertex( new Vertex(-1.0, y, z) );
149            z -= zStep;
150         }
151         x2 = -1.0;
152         for (int j = 0; j <= xGrid; ++j)
153         {
154            addVertex( new Vertex(x2, y, -1.0) );
155            x2 += xStep;
156         }
157         z = -1.0;
158         for (int j = 0; j <= zGrid + 1; ++j)
159         {
160            addVertex( new Vertex(1.0, y, z) );
161            z += zStep;
162         }
163         final int stop = vertexList.size();
164         // Note: stop - start =  2*xGrid + 2*zGrid + 5
165
166         for (int j = start; j < stop - 1; ++j)
167         {
168            addPrimitive(new LineSegment(j, j+1));
169         }
170
171         y += yStep;
172      }
173
174      // Grid lines perpendicular to the z-axis.
175      double z = -1.0;
176      for (int i = 0; i <= zGrid + 1; ++i)
177      {
178         final int start = vertexList.size();
179
180         // Start at the top, right edge, go left across the top face, and around the cube.
181         double x2 = 1.0;
182         for (int j = 0; j <= xGrid; ++j)
183         {
184            addVertex( new Vertex(x2, 1.0, z) );
185            x2 -= xStep;
186         }
187         double y2 = 1.0;
188         for (int j = 0; j <= yGrid; ++j)
189         {
190            addVertex( new Vertex(-1.0, y2, z) );
191            y2 -= yStep;
192         }
193         x2 = -1.0;
194         for (int j = 0; j <= xGrid; ++j)
195         {
196            addVertex( new Vertex(x2, -1.0, z) );
197            x2 += xStep;
198         }
199         y2 = -1.0;
200         for (int j = 0; j <= yGrid + 1; ++j)
201         {
202            addVertex( new Vertex(1.0, y2, z) );
203            y2 += yStep;
204         }
205         final int stop = vertexList.size();
206         // Note: stop - start =  2*xGrid + 2*yGrid + 5
207
208         for (int j = start; j < stop - 1; ++j)
209         {
210            addPrimitive(new LineSegment(j, j+1));
211         }
212
213         z += zStep;
214      }
215   }
216}//Cube2