44125657deb957cf1dad8a4e00d3b3e0bdd9f488
[jalviewjs.git] / src / javajs / util / T3.java
1 /*
2    Copyright (C) 1997,1998,1999
3    Kenji Hiranabe, Eiwa System Management, Inc.
4
5    This program is free software.
6    Implemented by Kenji Hiranabe(hiranabe@esm.co.jp),
7    conforming to the Java(TM) 3D API specification by Sun Microsystems.
8
9    Permission to use, copy, modify, distribute and sell this software
10    and its documentation for any purpose is hereby granted without fee,
11    provided that the above copyright notice appear in all copies and
12    that both that copyright notice and this permission notice appear
13    in supporting documentation. Kenji Hiranabe and Eiwa System Management,Inc.
14    makes no representations about the suitability of this software for any
15    purpose.  It is provided "AS IS" with NO WARRANTY.
16 */
17 package javajs.util;
18
19 import java.io.Serializable;
20
21 import javajs.api.JSONEncodable;
22
23 /**
24  * A generic 3 element tuple that is represented by single precision floating
25  * point x,y and z coordinates.
26  * 
27  * @version specification 1.1, implementation $Revision: 1.10 $, $Date:
28  *          2006/09/08 20:20:20 $
29  * @author Kenji hiranabe
30  * 
31  * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012
32  * for unique constructor and method names
33  * for the optimization of compiled JavaScript using Java2Script
34  */
35 public abstract class T3 implements JSONEncodable, Serializable {
36
37   public float x, y, z;
38
39   /**
40    * @j2sIgnore
41    * 
42    */
43   public T3() {
44   }
45
46   /**
47    * Sets the value of this tuple to the specified xyz coordinates.
48    * 
49    * @param x
50    *        the x coordinate
51    * @param y
52    *        the y coordinate
53    * @param z
54    *        the z coordinate
55    */
56   public final void set(float x, float y, float z) {
57     this.x = x;
58     this.y = y;
59     this.z = z;
60   }
61
62   /**
63    * Sets the value of this tuple from the 3 values specified in the array.
64    * 
65    * @param t
66    *        the array of length 3 containing xyz in order
67    */
68   public final void setA(float t[]) {
69     // ArrayIndexOutOfBounds is thrown if t.length < 3
70     x = t[0];
71     y = t[1];
72     z = t[2];
73   }
74
75   /**
76    * Sets the value of this tuple to the value of the Tuple3f argument.
77    * 
78    * @param t1
79    *        the tuple to be copied
80    */
81   public final void setT(T3 t1) {
82     x = t1.x;
83     y = t1.y;
84     z = t1.z;
85   }
86
87   /**
88    * Sets the value of this tuple to the vector sum of tuples t1 and t2.
89    * 
90    * @param t1
91    *        the first tuple
92    * @param t2
93    *        the second tuple
94    */
95   public final void add2(T3 t1, T3 t2) {
96     x = t1.x + t2.x;
97     y = t1.y + t2.y;
98     z = t1.z + t2.z;
99   }
100
101   /**
102    * Sets the value of this tuple to the vector sum of itself and tuple t1.
103    * 
104    * @param t1
105    *        the other tuple
106    */
107   public final void add(T3 t1) {
108     x += t1.x;
109     y += t1.y;
110     z += t1.z;
111   }
112
113   /**
114    * Computes the square of the distance between this point and point p1.
115    * 
116    * @param p1
117    *        the other point
118    * @return the square of distance between these two points as a float
119    */
120   public final float distanceSquared(T3 p1) {
121     double dx = x - p1.x;
122     double dy = y - p1.y;
123     double dz = z - p1.z;
124     return (float) (dx * dx + dy * dy + dz * dz);
125   }
126
127   /**
128    * Returns the distance between this point and point p1.
129    * 
130    * @param p1
131    *        the other point
132    * @return the distance between these two points
133    */
134   public final float distance(T3 p1) {
135     return (float) Math.sqrt(distanceSquared(p1));
136   }
137
138   /**
139    * Sets the value of this tuple to the vector difference of tuple t1 and t2
140    * (this = t1 - t2).
141    * 
142    * @param t1
143    *        the first tuple
144    * @param t2
145    *        the second tuple
146    */
147   public final void sub2(T3 t1, T3 t2) {
148     x = t1.x - t2.x;
149     y = t1.y - t2.y;
150     z = t1.z - t2.z;
151   }
152
153   /**
154    * Sets the value of this tuple to the vector difference of itself and tuple
155    * t1 (this = this - t1).
156    * 
157    * @param t1
158    *        the other tuple
159    */
160   public final void sub(T3 t1) {
161     x -= t1.x;
162     y -= t1.y;
163     z -= t1.z;
164   }
165
166   /**
167    * Sets the value of this tuple to the scalar multiplication of itself.
168    * 
169    * @param s
170    *        the scalar value
171    */
172   public final void scale(float s) {
173     x *= s;
174     y *= s;
175     z *= s;
176   }
177
178   /**
179    * Add {a b c}
180    * 
181    * @param a
182    * @param b
183    * @param c
184    */
185   public final void add3(float a, float b, float c) {
186     x += a;
187     y += b;
188     z += c;
189   }
190
191   
192   /**
193    * {x*p.x, y*p.y, z*p.z)  used for three-way scaling
194    * 
195    * @param p
196    */
197   public final void scaleT(T3 p) {
198     x *= p.x;
199     y *= p.y;
200     z *= p.z;
201   }
202
203   
204   /**
205    * Sets the value of this tuple to the scalar multiplication of tuple t1 and
206    * then adds tuple t2 (this = s*t1 + t2).
207    * 
208    * @param s
209    *        the scalar value
210    * @param t1
211    *        the tuple to be multipled
212    * @param t2
213    *        the tuple to be added
214    */
215   public final void scaleAdd2(float s, T3 t1, T3 t2) {
216     x = s * t1.x + t2.x;
217     y = s * t1.y + t2.y;
218     z = s * t1.z + t2.z;
219   }
220
221   
222   /**
223    * average of two tuples
224    * 
225    * @param a
226    * @param b
227    */
228   public void ave(T3 a, T3 b) {
229     x = (a.x + b.x) / 2f;
230     y = (a.y + b.y) / 2f;
231     z = (a.z + b.z) / 2f; 
232   }
233
234   /**
235    * Vector dot product. Was in Vector3f; more useful here, though.
236    * 
237    * @param v
238    *        the other vector
239    * @return this.dot.v
240    */
241   public final float dot(T3 v) {
242     return x * v.x + y * v.y + z * v.z;
243   }
244
245   /**
246    * Returns the squared length of this vector.
247    * Was in Vector3f; more useful here, though.
248    * 
249    * @return the squared length of this vector
250    */
251   public final float lengthSquared() {
252     return x * x + y * y + z * z;
253   }
254
255   /**
256    * Returns the length of this vector.
257    * Was in Vector3f; more useful here, though.
258    * 
259    * @return the length of this vector
260    */
261   public final float length() {
262     return (float) Math.sqrt(lengthSquared());
263   }
264
265   /**
266    * Normalizes this vector in place.
267    * Was in Vector3f; more useful here, though.
268    */
269   public final void normalize() {
270     double d = length();
271
272     // zero-div may occur.
273     x /= d;
274     y /= d;
275     z /= d;
276   }
277
278   /**
279    * Sets this tuple to be the vector cross product of vectors v1 and v2.
280    * 
281    * @param v1
282    *        the first vector
283    * @param v2
284    *        the second vector
285    */
286   public final void cross(T3 v1, T3 v2) {
287     set(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y
288         - v1.y * v2.x);
289   }
290
291   /**
292    * Returns a hash number based on the data values in this object. Two
293    * different Tuple3f objects with identical data values (ie, returns true for
294    * equals(Tuple3f) ) will return the same hash number. Two vectors with
295    * different data members may return the same hash value, although this is not
296    * likely.
297    */
298   @Override
299   public int hashCode() {
300     long bits = 1L;
301     bits = 31L * bits + floatToIntBits0(x);
302     bits = 31L * bits + floatToIntBits0(y);
303     bits = 31L * bits + floatToIntBits0(z);
304     return (int) (bits ^ (bits >> 32));
305   }
306
307   public static int floatToIntBits0(float f) {
308     return (f == 0 ? 0 : Float.floatToIntBits(f));
309   }
310
311   /**
312    * Returns true if all of the data members of Tuple3f t1 are equal to the
313    * corresponding data members in this
314    * 
315    * @param t1
316    *        the vector with which the comparison is made.
317    */
318   @Override
319   public boolean equals(Object t1) {
320     if (!(t1 instanceof T3))
321       return false;
322     T3 t2 = (T3) t1;
323     return (x == t2.x && y == t2.y && z == t2.z);
324   }
325
326   /**
327    * Returns a string that contains the values of this Tuple3f. The form is
328    * (x,y,z).
329    * 
330    * @return the String representation
331    */
332   @Override
333   public String toString() {
334     return "{" + x + ", " + y + ", " + z + "}";
335   }
336   
337   @Override
338   public String toJSON() {
339     return "[" + x + "," + y + "," + z + "]";
340   }
341 }