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