JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / javajs / util / A4.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 import javajs.api.JSONEncodable;\r
22 import javajs.util.T3;\r
23 \r
24 \r
25 \r
26 /**\r
27  * A 4 element axis angle represented by single precision floating point\r
28  * x,y,z,angle components. An axis angle is a rotation of angle (radians) about\r
29  * the vector (x,y,z).\r
30  * \r
31  * @version specification 1.1, implementation $Revision: 1.9 $, $Date:\r
32  *          2006/07/28 17:01:32 $\r
33  * @author Kenji hiranabe\r
34  * \r
35  * additions by Bob Hanson hansonr@stolaf.edu 9/30/2012\r
36  * for unique constructor and method names\r
37  * for the optimization of compiled JavaScript using Java2Script\r
38  */\r
39 public class A4 implements JSONEncodable, Serializable {\r
40 \r
41   /*\r
42    * I assumed that the length of the axis vector is not significant.\r
43    */\r
44 \r
45   /**\r
46    * The x coordinate.\r
47    */\r
48   public float x;\r
49 \r
50   /**\r
51    * The y coordinate.\r
52    */\r
53   public float y;\r
54 \r
55   /**\r
56    * The z coordinate.\r
57    */\r
58   public float z;\r
59 \r
60   /**\r
61    * The angle.\r
62    */\r
63   public float angle;\r
64 \r
65   /**\r
66    * Constructs and initializes a AxisAngle4f to (0,0,1,0).\r
67    */\r
68   public A4() {\r
69     z = 1.0f;\r
70   }\r
71 \r
72   /**\r
73    * Constructs and initializes an AxisAngle4f from the specified x, y, z, and\r
74    * angle.\r
75    * \r
76    * @param x\r
77    *        the x coordinate\r
78    * @param y\r
79    *        the y coordinate\r
80    * @param z\r
81    *        the z coordinate\r
82    * @param angle\r
83    *        the angle.\r
84    * @return a\r
85    */\r
86   public static A4 new4(float x, float y, float z, float angle) {\r
87     A4 a = new A4();\r
88     a.set4(x, y, z, angle);\r
89     return a;\r
90   }\r
91 \r
92   /**\r
93    * Constructs and initializes a AxisAngle4f from the specified AxisAngle4f.\r
94    * \r
95    * @param a1\r
96    *        the AxisAngle4f containing the initialization x y z angle data\r
97    * @return a\r
98    */\r
99   public static A4 newAA(A4 a1) {\r
100     A4 a = new A4();\r
101     a.set4(a1.x, a1.y, a1.z, a1.angle);\r
102     return a;\r
103   }\r
104 \r
105   /**\r
106    * Constructs and initializes an AxisAngle4f from the specified axis and\r
107    * angle.\r
108    * \r
109    * @param axis\r
110    *        the axis\r
111    * @param angle\r
112    *        the angle\r
113    * @return a\r
114    */\r
115   public static A4 newVA(V3 axis, float angle) {\r
116     A4 a = new A4();\r
117     a.setVA(axis, angle);\r
118     return a;\r
119   }\r
120 \r
121   /**\r
122    * Sets the value of this AxisAngle4f to the specified axis and angle.\r
123    * \r
124    * @param axis\r
125    *        the axis\r
126    * @param angle\r
127    *        the angle\r
128    * @since Java 3D 1.2\r
129    */\r
130   public final void setVA(V3 axis, float angle) {\r
131     x = axis.x;\r
132     y = axis.y;\r
133     z = axis.z;\r
134     this.angle = angle;\r
135   }\r
136 \r
137   /**\r
138    * Sets the value of this axis angle to the specified x,y,z,angle.\r
139    * \r
140    * @param x\r
141    *        the x coordinate\r
142    * @param y\r
143    *        the y coordinate\r
144    * @param z\r
145    *        the z coordinate\r
146    * @param angle\r
147    *        the angle\r
148    */\r
149   public final void set4(float x, float y, float z, float angle) {\r
150     this.x = x;\r
151     this.y = y;\r
152     this.z = z;\r
153     this.angle = angle;\r
154   }\r
155 \r
156   /**\r
157    * Sets the value of this axis angle to the value of axis angle t1.\r
158    * \r
159    * @param a\r
160    *        the axis angle to be copied\r
161    */\r
162   public final void setAA(A4 a) {\r
163     x = a.x;\r
164     y = a.y;\r
165     z = a.z;\r
166     angle = a.angle;\r
167   }\r
168 \r
169 \r
170   /**\r
171    * Sets the value of this axis-angle to the rotational component of the passed\r
172    * matrix.\r
173    * \r
174    * @param m1\r
175    *        the matrix3f\r
176    */\r
177   public final void setM(M3 m1) {\r
178     setFromMat(m1.m00, m1.m01, m1.m02, m1.m10, m1.m11, m1.m12, m1.m20, m1.m21,\r
179         m1.m22);\r
180   }\r
181 \r
182   // helper method\r
183   private void setFromMat(double m00, double m01, double m02, double m10,\r
184                           double m11, double m12, double m20, double m21,\r
185                           double m22) {\r
186     // assuming M is normalized.\r
187 \r
188     double cos = (m00 + m11 + m22 - 1.0) * 0.5;\r
189     x = (float) (m21 - m12);\r
190     y = (float) (m02 - m20);\r
191     z = (float) (m10 - m01);\r
192     double sin = 0.5 * Math.sqrt(x * x + y * y + z * z);\r
193     if (sin == 0 && cos == 1) {\r
194       x = y = 0;\r
195       z = 1;\r
196       angle = 0;\r
197     } else {\r
198       angle = (float) Math.atan2(sin, cos);\r
199     }\r
200 \r
201     // no need to normalize\r
202     // x /= n;\r
203     // y /= n;\r
204     // z /= n;\r
205   }\r
206 \r
207   /**\r
208    * Returns a hash number based on the data values in this object. Two\r
209    * different AxisAngle4f objects with identical data values (ie, returns true\r
210    * for equals(AxisAngle4f) ) will return the same hash number. Two vectors\r
211    * with different data members may return the same hash value, although this\r
212    * is not likely.\r
213    */\r
214   @Override\r
215   public int hashCode() {\r
216     return T3.floatToIntBits0(x) ^ T3.floatToIntBits0(y)\r
217         ^ T3.floatToIntBits0(z) ^ T3.floatToIntBits0(angle);\r
218   }\r
219 \r
220   /**\r
221    * Returns true if the Object o is of type AxisAngle4f and all of the data\r
222    * members of o1 are equal to the corresponding data members in this\r
223    * AxisAngle4f.\r
224    * \r
225    * @param o\r
226    *        the object with which the comparison is made.\r
227    * @return T/F\r
228    */\r
229   @Override\r
230   public boolean equals(Object o) {\r
231     if (!(o instanceof A4))\r
232       return false;\r
233     A4 a1 = (A4) o;\r
234     return x == a1.x && y == a1.y && z == a1.z && angle == a1.angle;\r
235   }\r
236 \r
237   /**\r
238    * Returns a string that contains the values of this AxisAngle4f. The form is\r
239    * (x,y,z,angle).\r
240    * \r
241    * @return the String representation\r
242    */\r
243   @Override\r
244   public String toString() {\r
245     return "(" + x + ", " + y + ", " + z + ", " + angle + ")";\r
246   }\r
247 \r
248   @Override\r
249   public String toJSON() {\r
250     return "[" + x + "," + y + "," + z + "," + (float) (angle * 180.0 / Math.PI) + "]";\r
251   }\r
252 }\r