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