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