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