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