001/*
002
003*/
004
005package renderer.scene;
006
007import java.util.Random;
008import java.awt.Color;
009
010/**
011    A {@code Vertex} object has three doubles which represent the
012    coordinates of a point in 3-dimensional space.
013<p>
014    When a {@code Vertex} object is created in a client program,
015    before the {@code Vertex} object moves down the graphics rendering
016    pipeline, the coordinates in the {@code Vertex} will be in
017    "camera coordinates".
018<p>
019    As a {@code Vertex} object moves down the graphics rendering
020    pipeline, the coordinates in the {@code Vertex} will be transformed
021    from one coordinate system to another.
022<p>
023    A {@code Vertex} object also has a {@link Color} (or "shade"). The
024    color of a {@link LineSegment} is interpolated from the colors at
025    its two endpoints.
026*/
027public class Vertex
028{
029   public double x, y, z;
030   public float  r = 1.0f, g = 1.0f, b = 1.0f;  // color at this vertex
031
032
033   /**
034      Construct a default {@code Vertex}.
035   */
036   public Vertex()
037   {
038      set(0.0, 0.0, 0.0);
039   }
040
041
042   /**
043      Construct a new {@code Vertex} using the given
044      {@code x}, {@code y}, and {@code z} coordinates.
045
046      @param x  x-coordinate of the new {@code Vertex}
047      @param y  y-coordinate of the new {@code Vertex}
048      @param z  z-coordinate of the new {@code Vertex}
049   */
050   public Vertex(double x, double y, double z)
051   {
052      set(x, y, z);
053   }
054
055
056   /**
057      Construct a new {@code Vertex} with the given coordinates
058      and the given {@link Color}.
059
060      @param x  x-coordinate of the new {@code Vertex}
061      @param y  y-coordinate of the new {@code Vertex}
062      @param z  z-coordinate of the new {@code Vertex}
063      @param c  {@link Color} of the new {@code Vertex}
064   */
065   public Vertex(double x, double y, double z, Color c)
066   {
067      set(x, y, z);
068      setColor(c);
069   }
070
071
072   /**
073      Construct a new {@code Vertex} that is a copy of another {@code Vertex}.
074
075      @param v  {@code Vertex} to make a copy of
076   */
077   public Vertex(Vertex v)
078   {
079      set(v.x, v.y, v.z);
080      setColor(v);
081   }
082
083
084   /**
085      Set the coordinates of this {@code Vertex}.
086
087      @param x  new x-coordinate for this {@code Vertex}
088      @param y  new y-coordinate for this {@code Vertex}
089      @param z  new z-coordinate for this {@code Vertex}
090   */
091   public void set(double x, double y, double z)
092   {
093      this.x = x;
094      this.y = y;
095      this.z = z;
096   }
097
098
099   /**
100      Get the {@link Color} of this {@code Vertex}.
101
102      @return the {@link Color} of this {@code Vertex} object
103   */
104   public Color getColor()
105   {
106      return new Color(this.r, this.g, this.b);
107   }
108
109
110   /**
111      Set the {@link Color} of this {@code Vertex}
112      using floats between 0 and 1.
113
114      @param r  red color value for this {@code Vertex} as a float between 0 and 1
115      @param g  green color value for this {@code Vertex} as a float between 0 and 1
116      @param b  blue color value for this {@code Vertex} as a float between 0 and 1
117   */
118   public void setColor(float r, float g, float b)
119   {
120      this.r = r;
121      this.g = g;
122      this.b = b;
123
124      if ( (r < 0.0) || (r > 1.0)
125        || (g < 0.0) || (g > 1.0)
126        || (b < 0.0) || (b > 1.0) )
127      {
128         System.err.println("ERROR! Invalid float color for vertex");
129         System.err.print( this.toString() );
130         System.err.printf("<r,g,b> = <% .5f  % .5f  % .5f>\n", r, g, b);
131         Thread.dumpStack();
132       //System.err.println(Arrays.toString(Thread.currentThread().getStackTrace()));
133         System.exit(-1);
134      }
135   }
136
137
138   /**
139      Set the {@link Color} of this {@code Vertex}
140      using ints between 0 and 255.
141
142      @param r  red color value for this {@code Vertex} as an integer between 0 and 255
143      @param g  green color value for this {@code Vertex} as an integer between 0 and 255
144      @param b  blue color value for this {@code Vertex} as an integer between 0 and 255
145   */
146   public void setColor(int r, int g, int b)
147   {
148      this.r = ((float)r)/(float)255;
149      this.g = ((float)g)/(float)255;
150      this.b = ((float)b)/(float)255;
151
152      if ( (r < 0) || (r > 255)
153        || (g < 0) || (g > 255)
154        || (b < 0) || (b > 255))
155      {
156         System.err.println("ERROR! Invalid int color for vertex");
157         System.err.print( this.toString() );
158         System.err.printf("<r,g,b> = <%d  %d  %d>\n", r, g, b);
159         Thread.dumpStack();
160       //System.err.println(Arrays.toString(Thread.currentThread().getStackTrace()));
161         System.exit(-1);
162      }
163   }
164
165
166   /**
167      Set the {@link Color} of this {@code Vertex}
168      using a {@link Color} object.
169
170      @param c  {@link Color} for this {@code Vertex} object
171   */
172   public void setColor(Color c)
173   {
174      setColor(c.getRed(), c.getGreen(), c.getBlue());
175   }
176
177
178   /**
179      Set the {@link Color} of this {@code Vertex}
180      using the colors from another {@code Vertex}.
181
182      @param v  {@code Vertex} object to get color values from
183   */
184   public void setColor(Vertex v)
185   {
186      // copy the color from v to this vertex
187      setColor(v.r, v.g, v.b);
188   }
189
190
191   /**
192      Set the {@link Color} of this {@code Vertex} to a random color.
193   */
194   public void setColorRandom()
195   {
196      Random generator = new Random();
197      float r = generator.nextFloat();
198      float g = generator.nextFloat();
199      float b = generator.nextFloat();
200      setColor(r, g, b);
201   }
202
203
204   /**
205      For debugging.
206
207      @return {@link String} representation of this {@code Vertex} object
208   */
209   @Override
210   public String toString()
211   {
212      // Here is one way to get programmable precision and width.
213      int p = 5;     // the precision for the following format string
214      int t = p + 4; // the width for the following format string
215      String format =      "(x,y,z) = (% "+t+"."+p+"f  % "+t+"."+p+"f  % "+t+"."+p+"f)\n";
216      return String.format( format, x, y, z);
217    //return String.format("(x,y,z) = (% .5f  % .5f  % .5f)", x, y, z);
218   }
219}