JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / javajs / util / T3d.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 /**\r
22  * A generic 3 element tuple that is represented by double precision floating\r
23  * point x,y and z coordinates.\r
24  * \r
25  * @version specification 1.1, implementation $Revision: 1.9 $, $Date:\r
26  *          2006/07/28 17:01:32 $\r
27  * @author Kenji hiranabe\r
28  * \r
29  * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012\r
30  * for unique constructor and method names\r
31  * for the optimization of compiled JavaScript using Java2Script\r
32  */\r
33 public abstract class T3d implements Serializable {\r
34   /**\r
35    * The x coordinate.\r
36    */\r
37   public double x;\r
38 \r
39   /**\r
40    * The y coordinate.\r
41    */\r
42   public double y;\r
43 \r
44   /**\r
45    * The z coordinate.\r
46    */\r
47   public double z;\r
48 \r
49   /**\r
50    * Constructs and initializes a Tuple3d to (0,0,0).\r
51    */\r
52   public T3d() {\r
53   }\r
54 \r
55   /**\r
56    * Sets the value of this tuple to the specified xyz coordinates.\r
57    * \r
58    * @param x\r
59    *        the x coordinate\r
60    * @param y\r
61    *        the y coordinate\r
62    * @param z\r
63    *        the z coordinate\r
64    */\r
65   public final void set(double x, double y, double z) {\r
66     this.x = x;\r
67     this.y = y;\r
68     this.z = z;\r
69   }\r
70 \r
71   /**\r
72    * Sets the value of this tuple from the 3 values specified in the array.\r
73    * \r
74    * @param t\r
75    *        the array of length 3 containing xyz in order\r
76    */\r
77   public final void setA(double t[]) {\r
78     // ArrayIndexOutOfBounds is thrown if t.length < 3\r
79     x = t[0];\r
80     y = t[1];\r
81     z = t[2];\r
82   }\r
83 \r
84   /**\r
85    * Sets the value of this tuple to the value of the Tuple3d argument.\r
86    * \r
87    * @param t1\r
88    *        the tuple to be copied\r
89    */\r
90   public final void setT(T3d t1) {\r
91     x = t1.x;\r
92     y = t1.y;\r
93     z = t1.z;\r
94   }\r
95 \r
96   /**\r
97    * Sets the value of this tuple to the vector sum of tuples t1 and t2.\r
98    * \r
99    * @param t1\r
100    *        the first tuple\r
101    * @param t2\r
102    *        the second tuple\r
103    */\r
104   public final void add2(T3d t1, T3d t2) {\r
105     x = t1.x + t2.x;\r
106     y = t1.y + t2.y;\r
107     z = t1.z + t2.z;\r
108   }\r
109 \r
110   /**\r
111    * Sets the value of this tuple to the vector sum of itself and tuple t1.\r
112    * \r
113    * @param t1\r
114    *        the other tuple\r
115    */\r
116   public final void add(T3d t1) {\r
117     x += t1.x;\r
118     y += t1.y;\r
119     z += t1.z;\r
120   }\r
121 \r
122   /**\r
123    * Sets the value of this tuple to the vector difference of tuple t1 and t2\r
124    * (this = t1 - t2).\r
125    * \r
126    * @param t1\r
127    *        the first tuple\r
128    * @param t2\r
129    *        the second tuple\r
130    */\r
131   public final void sub2(T3d t1, T3d t2) {\r
132     x = t1.x - t2.x;\r
133     y = t1.y - t2.y;\r
134     z = t1.z - t2.z;\r
135   }\r
136 \r
137   /**\r
138    * Sets the value of this tuple to the vector difference of itself and tuple\r
139    * t1 (this = this - t1).\r
140    * \r
141    * @param t1\r
142    *        the other tuple\r
143    */\r
144   public final void sub(T3d t1) {\r
145     x -= t1.x;\r
146     y -= t1.y;\r
147     z -= t1.z;\r
148   }\r
149 \r
150   /**\r
151    * Sets the value of this tuple to the scalar multiplication of itself.\r
152    * \r
153    * @param s\r
154    *        the scalar value\r
155    */\r
156   public final void scale(double s) {\r
157     x *= s;\r
158     y *= s;\r
159     z *= s;\r
160   }\r
161 \r
162   /**\r
163    * Sets the value of this tuple to the scalar multiplication of tuple t1 and\r
164    * then adds tuple t2 (this = s*t1 + t2).\r
165    * \r
166    * @param s\r
167    *        the scalar value\r
168    * @param t1\r
169    *        the tuple to be multipled\r
170    * @param t2\r
171    *        the tuple to be added\r
172    */\r
173   public final void scaleAdd(double s, T3d t1, T3d t2) {\r
174     x = s * t1.x + t2.x;\r
175     y = s * t1.y + t2.y;\r
176     z = s * t1.z + t2.z;\r
177   }\r
178 \r
179   /**\r
180    * Returns a hash number based on the data values in this object. Two\r
181    * different Tuple3d objects with identical data values (ie, returns true for\r
182    * equals(Tuple3d) ) will return the same hash number. Two vectors with\r
183    * different data members may return the same hash value, although this is not\r
184    * likely.\r
185    */\r
186   @Override\r
187   public int hashCode() {\r
188     long xbits = doubleToLongBits0(x);\r
189     long ybits = doubleToLongBits0(y);\r
190     long zbits = doubleToLongBits0(z);\r
191     return (int) (xbits ^ (xbits >> 32) ^ ybits ^ (ybits >> 32) ^ zbits ^ (zbits >> 32));\r
192   }\r
193 \r
194   static long doubleToLongBits0(double d) {\r
195     // Check for +0 or -0\r
196     return (d == 0 ? 0 : Double.doubleToLongBits(d));\r
197   }\r
198 \r
199   /**\r
200    * Returns true if all of the data members of Tuple3d t1 are equal to the\r
201    * corresponding data members in this\r
202    * \r
203    * @param t1\r
204    *        the vector with which the comparison is made.\r
205    */\r
206   @Override\r
207   public boolean equals(Object t1) {\r
208     if (!(t1 instanceof T3d))\r
209       return false;\r
210     T3d t2 = (T3d) t1;\r
211     return (this.x == t2.x && this.y == t2.y && this.z == t2.z);\r
212   }\r
213 \r
214   /**\r
215    * Returns a string that contains the values of this Tuple3d. The form is\r
216    * (x,y,z).\r
217    * \r
218    * @return the String representation\r
219    */\r
220   @Override\r
221   public String toString() {\r
222     return "{" + x + ", " + y + ", " + z + "}";\r
223   }\r
224 \r
225 }\r