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