2 * $RCSfile: AxisAngle4d.java,v $
4 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * This code is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 only, as
9 * published by the Free Software Foundation. Sun designates this
10 * particular file as subject to the "Classpath" exception as provided
11 * by Sun in the LICENSE file that accompanied this code.
13 * This code is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * version 2 for more details (a copy is included in the LICENSE file that
17 * accompanied this code).
19 * You should have received a copy of the GNU General Public License version
20 * 2 along with this work; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24 * CA 95054 USA or visit www.sun.com if you need additional information or
28 * $Date: 2008/02/28 20:18:50 $
32 package javax.vecmath;
35 * A four-element axis angle represented by double-precision floating point
36 * x,y,z,angle components. An axis angle is a rotation of angle (radians)
37 * about the vector (x,y,z).
40 public class AxisAngle4d implements java.io.Serializable, Cloneable {
43 // Compatible with 1.1
44 static final long serialVersionUID = 3644296204459140589L;
62 * The angle of rotation in radians.
67 final static double EPS = 1.0e-12;
70 * Constructs and initializes an AxisAngle4d from the specified
72 * @param x the x coordinate
73 * @param y the y coordinate
74 * @param z the z coordinate
75 * @param angle the angle of rotation in radians
77 public AxisAngle4d(double x, double y, double z, double angle)
87 * Constructs and initializes an AxisAngle4d from the components
88 * contained in the array.
89 * @param a the array of length 4 containing x,y,z,angle in order
91 public AxisAngle4d(double[] a)
99 * Constructs and initializes an AxisAngle4d from the specified AxisAngle4d.
100 * @param a1 the AxisAngle4d containing the initialization x y z angle data
102 public AxisAngle4d(AxisAngle4d a1)
107 this.angle = a1.angle;
112 * Constructs and initializes an AxisAngle4d from the specified
114 * @param a1 the AxisAngle4f containing the initialization x y z angle data
116 public AxisAngle4d(AxisAngle4f a1)
121 this.angle = a1.angle;
126 * Constructs and initializes an AxisAngle4d from the specified
128 * @param axis the axis
129 * @param angle the angle of rotation in radian
133 public AxisAngle4d(Vector3d axis, double angle) {
142 * Constructs and initializes an AxisAngle4d to (0,0,1,0).
154 * Sets the value of this axis angle to the specified x,y,z,angle.
155 * @param x the x coordinate
156 * @param y the y coordinate
157 * @param z the z coordinate
158 * @param angle the angle of rotation in radians
160 public final void set(double x, double y, double z, double angle)
170 * Sets the value of this axis angle to the specified x,y,z,angle.
171 * @param a the array of length 4 containing x,y,z,angle in order
173 public final void set(double[] a)
183 * Sets the value of this axis angle to the value of axis angle a1.
184 * @param a1 the axis angle to be copied
186 public final void set(AxisAngle4d a1)
191 this.angle = a1.angle;
196 * Sets the value of this axis angle to the value of axis angle a1.
197 * @param a1 the axis angle to be copied
199 public final void set(AxisAngle4f a1)
204 this.angle = a1.angle;
209 * Sets the value of this AxisAngle4d to the specified
211 * @param axis the axis
212 * @param angle the angle of rotation in radians
216 public final void set(Vector3d axis, double angle) {
225 * Gets the value of this axis angle and places it into the array a of
226 * length four in x,y,z,angle order.
227 * @param a the array of length four
229 public final void get(double[] a)
239 * Sets the value of this axis-angle to the rotational component of
241 * If the specified matrix has no rotational component, the value
242 * of this AxisAngle4d is set to an angle of 0 about an axis of (0,1,0).
244 * @param m1 the matrix4f
246 public final void set(Matrix4f m1)
248 Matrix3d m3d = new Matrix3d();
252 x = (float)(m3d.m21 - m3d.m12);
253 y = (float)(m3d.m02 - m3d.m20);
254 z = (float)(m3d.m10 - m3d.m01);
255 double mag = x*x + y*y + z*z;
258 mag = Math.sqrt(mag);
259 double sin = 0.5*mag;
260 double cos = 0.5*(m3d.m00 + m3d.m11 + m3d.m22 - 1.0);
262 angle = (float)Math.atan2(sin, cos);
264 double invMag = 1.0/mag;
278 * Sets the value of this axis-angle to the rotational component of
280 * If the specified matrix has no rotational component, the value
281 * of this AxisAngle4d is set to an angle of 0 about an axis of (0,1,0).
283 * @param m1 the matrix4d
285 public final void set(Matrix4d m1)
287 Matrix3d m3d = new Matrix3d();
291 x = (float)(m3d.m21 - m3d.m12);
292 y = (float)(m3d.m02 - m3d.m20);
293 z = (float)(m3d.m10 - m3d.m01);
295 double mag = x*x + y*y + z*z;
298 mag = Math.sqrt(mag);
300 double sin = 0.5*mag;
301 double cos = 0.5*(m3d.m00 + m3d.m11 + m3d.m22 - 1.0);
302 angle = (float)Math.atan2(sin, cos);
304 double invMag = 1.0/mag;
318 * Sets the value of this axis-angle to the rotational component of
320 * If the specified matrix has no rotational component, the value
321 * of this AxisAngle4d is set to an angle of 0 about an axis of (0,1,0).
322 * @param m1 the matrix3f
324 public final void set(Matrix3f m1)
326 x = (float)(m1.m21 - m1.m12);
327 y = (float)(m1.m02 - m1.m20);
328 z = (float)(m1.m10 - m1.m01);
329 double mag = x*x + y*y + z*z;
332 mag = Math.sqrt(mag);
334 double sin = 0.5*mag;
335 double cos = 0.5*(m1.m00 + m1.m11 + m1.m22 - 1.0);
336 angle = (float)Math.atan2(sin, cos);
338 double invMag = 1.0/mag;
352 * Sets the value of this axis-angle to the rotational component of
354 * If the specified matrix has no rotational component, the value
355 * of this AxisAngle4d is set to an angle of 0 about an axis of (0,1,0).
356 * @param m1 the matrix3d
358 public final void set(Matrix3d m1)
360 x = (float)(m1.m21 - m1.m12);
361 y = (float)(m1.m02 - m1.m20);
362 z = (float)(m1.m10 - m1.m01);
364 double mag = x*x + y*y + z*z;
367 mag = Math.sqrt(mag);
369 double sin = 0.5*mag;
370 double cos = 0.5*(m1.m00 + m1.m11 + m1.m22 - 1.0);
372 angle = (float)Math.atan2(sin, cos);
374 double invMag = 1.0/mag;
390 * Sets the value of this axis-angle to the rotational equivalent
391 * of the passed quaternion.
392 * If the specified quaternion has no rotational component, the value
393 * of this AxisAngle4d is set to an angle of 0 about an axis of (0,1,0).
394 * @param q1 the Quat4f
396 public final void set(Quat4f q1)
398 double mag = q1.x*q1.x + q1.y*q1.y + q1.z*q1.z;
401 mag = Math.sqrt(mag);
402 double invMag = 1.0/mag;
407 angle = 2.0*Math.atan2(mag, q1.w);
418 * Sets the value of this axis-angle to the rotational equivalent
419 * of the passed quaternion.
420 * If the specified quaternion has no rotational component, the value
421 * of this AxisAngle4d is set to an angle of 0 about an axis of (0,1,0).
422 * @param q1 the Quat4d
424 public final void set(Quat4d q1)
426 double mag = q1.x*q1.x + q1.y*q1.y + q1.z*q1.z;
429 mag = Math.sqrt(mag);
430 double invMag = 1.0/mag;
435 angle = 2.0*Math.atan2(mag, q1.w);
446 * Returns a string that contains the values of this AxisAngle4d.
447 * The form is (x,y,z,angle).
448 * @return the String representation
450 public String toString() {
451 return "(" + this.x + ", " + this.y + ", " + this.z + ", " + this.angle + ")";
456 * Returns true if all of the data members of AxisAngle4d a1 are
457 * equal to the corresponding data members in this AxisAngle4d.
458 * @param a1 the axis-angle with which the comparison is made
459 * @return true or false
461 public boolean equals(AxisAngle4d a1)
464 return(this.x == a1.x && this.y == a1.y && this.z == a1.z
465 && this.angle == a1.angle);
467 catch (NullPointerException e2) {return false;}
471 * Returns true if the Object o1 is of type AxisAngle4d and all of the
472 * data members of o1 are equal to the corresponding data members in
474 * @param o1 the object with which the comparison is made
475 * @return true or false
477 public boolean equals(Object o1)
480 AxisAngle4d a2 = (AxisAngle4d) o1;
481 return(this.x == a2.x && this.y == a2.y && this.z == a2.z
482 && this.angle == a2.angle);
484 catch (NullPointerException e2) {return false;}
485 catch (ClassCastException e1) {return false;}
491 * Returns true if the L-infinite distance between this axis-angle
492 * and axis-angle a1 is less than or equal to the epsilon parameter,
493 * otherwise returns false. The L-infinite
494 * distance is equal to
495 * MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2), abs(angle1-angle2)].
496 * @param a1 the axis-angle to be compared to this axis-angle
497 * @param epsilon the threshold value
499 public boolean epsilonEquals(AxisAngle4d a1, double epsilon)
504 if((diff<0?-diff:diff) > epsilon) return false;
507 if((diff<0?-diff:diff) > epsilon) return false;
510 if((diff<0?-diff:diff) > epsilon) return false;
512 diff = angle - a1.angle;
513 if((diff<0?-diff:diff) > epsilon) return false;
520 * Returns a hash code value based on the data values in this
521 * object. Two different AxisAngle4d objects with identical data values
522 * (i.e., AxisAngle4d.equals returns true) will return the same hash
523 * code value. Two objects with different data members may return the
524 * same hash value, although this is not likely.
525 * @return the integer hash code value
527 public int hashCode() {
529 bits = 31L * bits + VecMathUtil.doubleToLongBits(x);
530 bits = 31L * bits + VecMathUtil.doubleToLongBits(y);
531 bits = 31L * bits + VecMathUtil.doubleToLongBits(z);
532 bits = 31L * bits + VecMathUtil.doubleToLongBits(angle);
533 return (int) (bits ^ (bits >> 32));
537 * Creates a new object of the same class as this object.
539 * @return a clone of this instance.
540 * @exception OutOfMemoryError if there is not enough memory.
541 * @see java.lang.Cloneable
544 public Object clone() {
545 // Since there are no arrays we can just use Object.clone()
547 return super.clone();
548 } catch (CloneNotSupportedException e) {
549 // this shouldn't happen, since we are Cloneable
550 throw new InternalError();
556 * Get the axis angle, in radians.<br>
557 * An axis angle is a rotation angle about the vector (x,y,z).
559 * @return the angle, in radians.
563 public final double getAngle() {
569 * Set the axis angle, in radians.<br>
570 * An axis angle is a rotation angle about the vector (x,y,z).
572 * @param angle The angle to set, in radians.
576 public final void setAngle(double angle) {
582 * Get value of <i>x</i> coordinate.
584 * @return the <i>x</i> coordinate.
588 public double getX() {
594 * Set a new value for <i>x</i> coordinate.
596 * @param x the <i>x</i> coordinate.
600 public final void setX(double x) {
606 * Get value of <i>y</i> coordinate.
608 * @return the <i>y</i> coordinate.
612 public final double getY() {
618 * Set a new value for <i>y</i> coordinate.
620 * @param y the <i>y</i> coordinate.
624 public final void setY(double y) {
630 * Get value of <i>z</i> coordinate.
632 * @return the <i>z</i> coordinate.
636 public double getZ() {
642 * Set a new value for <i>z</i> coordinate.
644 * @param z the <i>z</i> coordinate.
648 public final void setZ(double z) {