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 a triangle fan.
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 Cube2
046   @see Cube4
047*/
048public class Cube3 extends Cube
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 a triangle fan of four triangles in each face.
054   */
055   public Cube3( )
056   {
057      this(1, 1, 1);
058   }
059
060
061   /**
062      Create a cube with its center at the origin, having edge
063      length 2, with its corners at {@code (±1, ±1, ±1)}, and
064      with each of the cube's faces containing a triangle fan
065      with the given number of triangles along each of the x,
066      y, and z directions.
067      <p>
068      There must be at least one triangle along each direction.
069
070      @param xCount  number of triangles along the x-direction
071      @param yCount  number of triangles along the y-direction
072      @param zCount  number of triangles along the z-direction
073      @throws IllegalArgumentException if {@code xCount} is less than 1
074      @throws IllegalArgumentException if {@code yCount} is less than 1
075      @throws IllegalArgumentException if {@code zCount} is less than 1
076   */
077   public Cube3(int xCount, int yCount, int zCount)
078   {
079      // create the basic cube with 8 vertices and 12 edges
080      super( String.format("Cube3(%d,%d,%d)", xCount, yCount, zCount) );
081
082      if (xCount < 1)
083         throw new IllegalArgumentException("xCount must be greater than or equal to 1");
084      if (yCount < 1)
085         throw new IllegalArgumentException("yCount must be greater than or equal to 1");
086      if (zCount < 1)
087         throw new IllegalArgumentException("zCount must be greater than or equal to 1");
088
089      int index = 8;
090
091      final double xStep = 2.0 / xCount,
092                   yStep = 2.0 / yCount,
093                   zStep = 2.0 / zCount;
094
095      addVertex(new Vertex( 0,  0,  1),   // center front
096                new Vertex( 0,  0, -1),   // center back
097                new Vertex( 0,  1,  0),   // center top
098                new Vertex( 0, -1,  0),   // center bottom
099                new Vertex( 1,  0,  0),   // center right
100                new Vertex(-1,  0,  0));  // center left
101      final int centerFront  = index,
102                centerBack   = index + 1,
103                centerTop    = index + 2,
104                centerBottom = index + 3,
105                centerRight  = index + 4,
106                centerLeft   = index + 5;
107      index += 6;
108
109      // Triangles along all four edges parallel to the x-axis.
110      double x = -1.0;
111      for (int i = 0; i <= xCount; ++i)
112      {
113         addVertex(new Vertex(x,  1,  1),
114                   new Vertex(x, -1,  1),
115                   new Vertex(x,  1, -1),
116                   new Vertex(x, -1, -1));
117         // front face, top and bottom edges
118         addPrimitive(new LineSegment(index+0, centerFront),
119                      new LineSegment(index+1, centerFront));
120         // back face, top and bottom edges
121         addPrimitive(new LineSegment(index+2, centerBack),
122                      new LineSegment(index+3, centerBack));
123         // top face, front and back edges
124         addPrimitive(new LineSegment(index+0, centerTop),
125                      new LineSegment(index+2, centerTop));
126         // bottom face, front and back edges
127         addPrimitive(new LineSegment(index+1, centerBottom),
128                      new LineSegment(index+3, centerBottom));
129         x += xStep;
130         index += 4;
131      }
132
133      // Triangles along all four edges parallel to the y-axis.
134      double y = -1.0;
135      for (int i = 0; i <= yCount; ++i)
136      {
137         addVertex(new Vertex( 1, y,  1),
138                   new Vertex(-1, y,  1),
139                   new Vertex( 1, y, -1),
140                   new Vertex(-1, y, -1));
141         // front face, right and left edges
142         addPrimitive(new LineSegment(index+0, centerFront),
143                      new LineSegment(index+1, centerFront));
144         // back face, right and left edges
145         addPrimitive(new LineSegment(index+2, centerBack),
146                      new LineSegment(index+3, centerBack));
147         // right face, front and back edges
148         addPrimitive(new LineSegment(index+0, centerRight),
149                      new LineSegment(index+2, centerRight));
150         // left face, front and back edges
151         addPrimitive(new LineSegment(index+1, centerLeft),
152                      new LineSegment(index+3, centerLeft));
153         y += yStep;
154         index += 4;
155      }
156
157      // Triangles along all four edges parallel to the z-axis.
158      double z = -1.0;
159      for (int i = 0; i <= zCount; ++i)
160      {
161         addVertex(new Vertex( 1,  1, z),
162                   new Vertex(-1,  1, z),
163                   new Vertex( 1, -1, z),
164                   new Vertex(-1, -1, z));
165         // top face, right and left edges
166         addPrimitive(new LineSegment(index+0, centerTop),
167                      new LineSegment(index+1, centerTop));
168         // bottom face, right and left edges
169         addPrimitive(new LineSegment(index+2, centerBottom),
170                      new LineSegment(index+3, centerBottom));
171         // right face, top and bottom edges
172         addPrimitive(new LineSegment(index+0, centerRight),
173                      new LineSegment(index+2, centerRight));
174         // left face, top and bottom edges
175         addPrimitive(new LineSegment(index+1, centerLeft),
176                      new LineSegment(index+3, centerLeft));
177         z += zStep;
178         index += 4;
179      }
180   }
181}//Cube3