commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r665700 - in /commons/proper/math/branches/MATH_2_0/src: java/org/apache/commons/math/geometry/ site/xdoc/ test/org/apache/commons/math/geometry/
Date Mon, 09 Jun 2008 14:17:21 GMT
Author: luc
Date: Mon Jun  9 07:17:21 2008
New Revision: 665700

URL: http://svn.apache.org/viewvc?rev=665700&view=rev
Log:
Improved usability of the geometry package.
>From a developer point of view, all the changes are related.
>From a user point of view, they are independent changes.

 - added several convenience methods for Vector3D and Rotation
 - replaced public no argument constructors with IDENTITY or ZERO
   static instances for immutable classes Vector3D and Rotation
 - fixed inconsistencies in the naming scheme for static fields in
   Vector3D and Rotation with respect to the overall library

Modified:
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Rotation.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/RotationOrder.java
    commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Vector3D.java
    commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml
    commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/RotationTest.java
    commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/Vector3DTest.java

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Rotation.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Rotation.java?rev=665700&r1=665699&r2=665700&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Rotation.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Rotation.java
Mon Jun  9 07:17:21 2008
@@ -88,14 +88,23 @@
 
 public class Rotation implements Serializable {
 
-  /** Build the identity rotation.
-   */
-  public Rotation() {
-    q0 = 1;
-    q1 = 0;
-    q2 = 0;
-    q3 = 0;
-  }
+  /** Identity rotation. */
+  public static final Rotation IDENTITY = new Rotation(1.0, 0.0, 0.0, 0.0, false);
+
+  /** Serializable version identifier */
+  private static final long serialVersionUID = -2153622329907944313L;
+
+  /** Scalar coordinate of the quaternion. */
+  private final double q0;
+
+  /** First coordinate of the vectorial part of the quaternion. */
+  private final double q1;
+
+  /** Second coordinate of the vectorial part of the quaternion. */
+  private final double q2;
+
+  /** Third coordinate of the vectorial part of the quaternion. */
+  private final double q3;
 
   /** Build a rotation from the quaternion coordinates.
    * <p>A rotation can be built from a <em>normalized</em> quaternion,
@@ -592,8 +601,8 @@
       // (-r) (Vector3D.plusI) coordinates are :
       // cos (psi) cos (theta), -sin (psi) cos (theta), sin (theta)
       // and we can choose to have theta in the interval [-PI/2 ; +PI/2]
-      Vector3D v1 = applyTo(Vector3D.plusK);
-      Vector3D v2 = applyInverseTo(Vector3D.plusI);
+      Vector3D v1 = applyTo(Vector3D.PLUS_K);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_I);
       if  ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) {
         throw new CardanEulerSingularityException(true);
       }
@@ -610,8 +619,8 @@
       // (-r) (Vector3D.plusI) coordinates are :
       // cos (theta) cos (psi), -sin (psi), sin (theta) cos (psi)
       // and we can choose to have psi in the interval [-PI/2 ; +PI/2]
-      Vector3D v1 = applyTo(Vector3D.plusJ);
-      Vector3D v2 = applyInverseTo(Vector3D.plusI);
+      Vector3D v1 = applyTo(Vector3D.PLUS_J);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_I);
       if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) {
         throw new CardanEulerSingularityException(true);
       }
@@ -628,8 +637,8 @@
       // (-r) (Vector3D.plusJ) coordinates are :
       // sin (psi) cos (phi), cos (psi) cos (phi), -sin (phi)
       // and we can choose to have phi in the interval [-PI/2 ; +PI/2]
-      Vector3D v1 = applyTo(Vector3D.plusK);
-      Vector3D v2 = applyInverseTo(Vector3D.plusJ);
+      Vector3D v1 = applyTo(Vector3D.PLUS_K);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_J);
       if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) {
         throw new CardanEulerSingularityException(true);
       }
@@ -646,8 +655,8 @@
       // (-r) (Vector3D.plusJ) coordinates are :
       // sin (psi), cos (phi) cos (psi), -sin (phi) cos (psi)
       // and we can choose to have psi in the interval [-PI/2 ; +PI/2]
-      Vector3D v1 = applyTo(Vector3D.plusI);
-      Vector3D v2 = applyInverseTo(Vector3D.plusJ);
+      Vector3D v1 = applyTo(Vector3D.PLUS_I);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_J);
       if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) {
         throw new CardanEulerSingularityException(true);
       }
@@ -664,8 +673,8 @@
       // (-r) (Vector3D.plusK) coordinates are :
       // -sin (theta) cos (phi), sin (phi), cos (theta) cos (phi)
       // and we can choose to have phi in the interval [-PI/2 ; +PI/2]
-      Vector3D v1 = applyTo(Vector3D.plusJ);
-      Vector3D v2 = applyInverseTo(Vector3D.plusK);
+      Vector3D v1 = applyTo(Vector3D.PLUS_J);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_K);
       if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) {
         throw new CardanEulerSingularityException(true);
       }
@@ -682,8 +691,8 @@
       // (-r) (Vector3D.plusK) coordinates are :
       // -sin (theta), sin (phi) cos (theta), cos (phi) cos (theta)
       // and we can choose to have theta in the interval [-PI/2 ; +PI/2]
-      Vector3D v1 = applyTo(Vector3D.plusI);
-      Vector3D v2 = applyInverseTo(Vector3D.plusK);
+      Vector3D v1 = applyTo(Vector3D.PLUS_I);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_K);
       if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) {
         throw new CardanEulerSingularityException(true);
       }
@@ -700,8 +709,8 @@
       // (-r) (Vector3D.plusI) coordinates are :
       // cos (theta), sin (theta) sin (phi2), sin (theta) cos (phi2)
       // and we can choose to have theta in the interval [0 ; PI]
-      Vector3D v1 = applyTo(Vector3D.plusI);
-      Vector3D v2 = applyInverseTo(Vector3D.plusI);
+      Vector3D v1 = applyTo(Vector3D.PLUS_I);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_I);
       if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) {
         throw new CardanEulerSingularityException(false);
       }
@@ -718,8 +727,8 @@
       // (-r) (Vector3D.plusI) coordinates are :
       // cos (psi), -sin (psi) cos (phi2), sin (psi) sin (phi2)
       // and we can choose to have psi in the interval [0 ; PI]
-      Vector3D v1 = applyTo(Vector3D.plusI);
-      Vector3D v2 = applyInverseTo(Vector3D.plusI);
+      Vector3D v1 = applyTo(Vector3D.PLUS_I);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_I);
       if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) {
         throw new CardanEulerSingularityException(false);
       }
@@ -736,8 +745,8 @@
       // (-r) (Vector3D.plusJ) coordinates are :
       // sin (phi) sin (theta2), cos (phi), -sin (phi) cos (theta2)
       // and we can choose to have phi in the interval [0 ; PI]
-      Vector3D v1 = applyTo(Vector3D.plusJ);
-      Vector3D v2 = applyInverseTo(Vector3D.plusJ);
+      Vector3D v1 = applyTo(Vector3D.PLUS_J);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_J);
       if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) {
         throw new CardanEulerSingularityException(false);
       }
@@ -754,8 +763,8 @@
       // (-r) (Vector3D.plusJ) coordinates are :
       // sin (psi) cos (theta2), cos (psi), sin (psi) sin (theta2)
       // and we can choose to have psi in the interval [0 ; PI]
-      Vector3D v1 = applyTo(Vector3D.plusJ);
-      Vector3D v2 = applyInverseTo(Vector3D.plusJ);
+      Vector3D v1 = applyTo(Vector3D.PLUS_J);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_J);
       if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) {
         throw new CardanEulerSingularityException(false);
       }
@@ -772,8 +781,8 @@
       // (-r) (Vector3D.plusK) coordinates are :
       // sin (phi) sin (psi2), sin (phi) cos (psi2), cos (phi)
       // and we can choose to have phi in the interval [0 ; PI]
-      Vector3D v1 = applyTo(Vector3D.plusK);
-      Vector3D v2 = applyInverseTo(Vector3D.plusK);
+      Vector3D v1 = applyTo(Vector3D.PLUS_K);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_K);
       if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) {
         throw new CardanEulerSingularityException(false);
       }
@@ -790,8 +799,8 @@
       // (-r) (Vector3D.plusK) coordinates are :
       // -sin (theta) cos (psi2), sin (theta) sin (psi2), cos (theta)
       // and we can choose to have theta in the interval [0 ; PI]
-      Vector3D v1 = applyTo(Vector3D.plusK);
-      Vector3D v2 = applyInverseTo(Vector3D.plusK);
+      Vector3D v1 = applyTo(Vector3D.PLUS_K);
+      Vector3D v2 = applyInverseTo(Vector3D.PLUS_K);
       if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) {
         throw new CardanEulerSingularityException(false);
       }
@@ -1017,19 +1026,32 @@
                                           });
   }
 
-  /** Scalar coordinate of the quaternion. */
-  private final double q0;
-
-  /** First coordinate of the vectorial part of the quaternion. */
-  private final double q1;
-
-  /** Second coordinate of the vectorial part of the quaternion. */
-  private final double q2;
-
-  /** Third coordinate of the vectorial part of the quaternion. */
-  private final double q3;
-
-  /** Serializable version identifier */
-  private static final long serialVersionUID = 8225864499430109352L;
+  /** Compute the <i>distance</i> between two rotations.
+   * <p>The <i>distance</i> is intended here as a way to check if two
+   * rotations are almost similar (i.e. they transform vectors the same way)
+   * or very different. It is mathematically defined as the angle of
+   * the rotation r that prepended to one of the rotations gives the other
+   * one:</p>
+   * <pre>
+   *        r<sub>1</sub>(r) = r<sub>2</sub>
+   * </pre>
+   * <p>This distance is an angle between 0 and &pi;. Its value is the smallest
+   * possible upper bound of the angle in radians between r<sub>1</sub>(v)
+   * and r<sub>2</sub>(v) for all possible vectors v. This upper bound is
+   * reached for some v. The distance is equal to 0 if and only if the two
+   * rotations are identical.</p>
+   * <p>Comparing two rotations should always be done using this value rather
+   * than for example comparing the components of the quaternions. It is much
+   * more stable, and has a geometric meaning. Also comparing quaternions
+   * components is error prone since for example quaternions (0.36, 0.48, -0.48, -0.64)
+   * and (-0.36, -0.48, 0.48, 0.64) represent exactly the same rotation despite
+   * their components are different (they are exact opposites).</p>
+   * @param r1 first rotation
+   * @param r2 second rotation
+   * @return <i>distance</i> between r1 and r2
+   */
+  public static double distance(Rotation r1, Rotation r2) {
+      return r1.applyInverseTo(r2).getAngle();
+  }
 
 }

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/RotationOrder.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/RotationOrder.java?rev=665700&r1=665699&r2=665700&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/RotationOrder.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/RotationOrder.java
Mon Jun  9 07:17:21 2008
@@ -80,84 +80,84 @@
    * around Z
    */
   public static final RotationOrder XYZ =
-    new RotationOrder("XYZ", Vector3D.plusI, Vector3D.plusJ, Vector3D.plusK);
+    new RotationOrder("XYZ", Vector3D.PLUS_I, Vector3D.PLUS_J, Vector3D.PLUS_K);
 
   /** Set of Cardan angles.
    * this ordered set of rotations is around X, then around Z, then
    * around Y
    */
   public static final RotationOrder XZY =
-    new RotationOrder("XZY", Vector3D.plusI, Vector3D.plusK, Vector3D.plusJ);
+    new RotationOrder("XZY", Vector3D.PLUS_I, Vector3D.PLUS_K, Vector3D.PLUS_J);
 
   /** Set of Cardan angles.
    * this ordered set of rotations is around Y, then around X, then
    * around Z
    */
   public static final RotationOrder YXZ =
-    new RotationOrder("YXZ", Vector3D.plusJ, Vector3D.plusI, Vector3D.plusK);
+    new RotationOrder("YXZ", Vector3D.PLUS_J, Vector3D.PLUS_I, Vector3D.PLUS_K);
 
   /** Set of Cardan angles.
    * this ordered set of rotations is around Y, then around Z, then
    * around X
    */
   public static final RotationOrder YZX =
-    new RotationOrder("YZX", Vector3D.plusJ, Vector3D.plusK, Vector3D.plusI);
+    new RotationOrder("YZX", Vector3D.PLUS_J, Vector3D.PLUS_K, Vector3D.PLUS_I);
 
   /** Set of Cardan angles.
    * this ordered set of rotations is around Z, then around X, then
    * around Y
    */
   public static final RotationOrder ZXY =
-    new RotationOrder("ZXY", Vector3D.plusK, Vector3D.plusI, Vector3D.plusJ);
+    new RotationOrder("ZXY", Vector3D.PLUS_K, Vector3D.PLUS_I, Vector3D.PLUS_J);
 
   /** Set of Cardan angles.
    * this ordered set of rotations is around Z, then around Y, then
    * around X
    */
   public static final RotationOrder ZYX =
-    new RotationOrder("ZYX", Vector3D.plusK, Vector3D.plusJ, Vector3D.plusI);
+    new RotationOrder("ZYX", Vector3D.PLUS_K, Vector3D.PLUS_J, Vector3D.PLUS_I);
 
   /** Set of Euler angles.
    * this ordered set of rotations is around X, then around Y, then
    * around X
    */
   public static final RotationOrder XYX =
-    new RotationOrder("XYX", Vector3D.plusI, Vector3D.plusJ, Vector3D.plusI);
+    new RotationOrder("XYX", Vector3D.PLUS_I, Vector3D.PLUS_J, Vector3D.PLUS_I);
 
   /** Set of Euler angles.
    * this ordered set of rotations is around X, then around Z, then
    * around X
    */
   public static final RotationOrder XZX =
-    new RotationOrder("XZX", Vector3D.plusI, Vector3D.plusK, Vector3D.plusI);
+    new RotationOrder("XZX", Vector3D.PLUS_I, Vector3D.PLUS_K, Vector3D.PLUS_I);
 
   /** Set of Euler angles.
    * this ordered set of rotations is around Y, then around X, then
    * around Y
    */
   public static final RotationOrder YXY =
-    new RotationOrder("YXY", Vector3D.plusJ, Vector3D.plusI, Vector3D.plusJ);
+    new RotationOrder("YXY", Vector3D.PLUS_J, Vector3D.PLUS_I, Vector3D.PLUS_J);
 
   /** Set of Euler angles.
    * this ordered set of rotations is around Y, then around Z, then
    * around Y
    */
   public static final RotationOrder YZY =
-    new RotationOrder("YZY", Vector3D.plusJ, Vector3D.plusK, Vector3D.plusJ);
+    new RotationOrder("YZY", Vector3D.PLUS_J, Vector3D.PLUS_K, Vector3D.PLUS_J);
 
   /** Set of Euler angles.
    * this ordered set of rotations is around Z, then around X, then
    * around Z
    */
   public static final RotationOrder ZXZ =
-    new RotationOrder("ZXZ", Vector3D.plusK, Vector3D.plusI, Vector3D.plusK);
+    new RotationOrder("ZXZ", Vector3D.PLUS_K, Vector3D.PLUS_I, Vector3D.PLUS_K);
 
   /** Set of Euler angles.
    * this ordered set of rotations is around Z, then around Y, then
    * around Z
    */
   public static final RotationOrder ZYZ =
-    new RotationOrder("ZYZ", Vector3D.plusK, Vector3D.plusJ, Vector3D.plusK);
+    new RotationOrder("ZYZ", Vector3D.PLUS_K, Vector3D.PLUS_J, Vector3D.PLUS_K);
 
   /** Name of the rotations order. */
   private final String name;

Modified: commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Vector3D.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Vector3D.java?rev=665700&r1=665699&r2=665700&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Vector3D.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/geometry/Vector3D.java
Mon Jun  9 07:17:21 2008
@@ -29,35 +29,38 @@
 public class Vector3D
   implements Serializable {
 
+  /** Null vector (coordinates: 0, 0, 0). */
+  public static final Vector3D ZERO   = new Vector3D(0, 0, 0);
+
   /** First canonical vector (coordinates: 1, 0, 0). */
-  public static final Vector3D plusI = new Vector3D(1, 0, 0);
+  public static final Vector3D PLUS_I = new Vector3D(1, 0, 0);
 
   /** Opposite of the first canonical vector (coordinates: -1, 0, 0). */
-  public static final Vector3D minusI = new Vector3D(-1, 0, 0);
+  public static final Vector3D MINUS_I = new Vector3D(-1, 0, 0);
 
   /** Second canonical vector (coordinates: 0, 1, 0). */
-  public static final Vector3D plusJ = new Vector3D(0, 1, 0);
+  public static final Vector3D PLUS_J = new Vector3D(0, 1, 0);
 
   /** Opposite of the second canonical vector (coordinates: 0, -1, 0). */
-  public static final Vector3D minusJ = new Vector3D(0, -1, 0);
+  public static final Vector3D MINUS_J = new Vector3D(0, -1, 0);
 
   /** Third canonical vector (coordinates: 0, 0, 1). */
-  public static final Vector3D plusK = new Vector3D(0, 0, 1);
+  public static final Vector3D PLUS_K = new Vector3D(0, 0, 1);
 
   /** Opposite of the third canonical vector (coordinates: 0, 0, -1).  */
-  public static final Vector3D minusK = new Vector3D(0, 0, -1);
+  public static final Vector3D MINUS_K = new Vector3D(0, 0, -1);
 
-  /** Null vector (coordinates: 0, 0, 0). */
-  public static final Vector3D zero   = new Vector3D(0, 0, 0);
+  /** Serializable version identifier */
+  private static final long serialVersionUID = -6155041477622120793L;
 
-  /** Simple constructor.
-   * Build a null vector.
-   */
-  public Vector3D() {
-    x = 0;
-    y = 0;
-    z = 0;
-  }
+  /** Abscissa. */
+  private final double x;
+
+  /** Ordinate. */
+  private final double y;
+
+  /** Height. */
+  private final double z;
 
   /** Simple constructor.
    * Build a vector from its coordinates
@@ -182,6 +185,13 @@
     return Math.sqrt (x * x + y * y + z * z);
   }
 
+  /** Get the square of the norm for the vector.
+   * @return square of the euclidian norm for the vector
+   */
+  public double getNormSq() {
+    return x * x + y * y + z * z;
+  }
+
   /** Get the azimuth of the vector.
    * @return azimuth (&alpha;) of the vector, between -&pi; and +&pi;
    * @see #Vector3D(double, double)
@@ -347,17 +357,34 @@
                         v1.x * v2.y - v1.y * v2.x);
   }
 
-  /** Abscissa. */
-  private final double x;
-
-  /** Ordinate. */
-  private final double y;
-
-  /** Height. */
-  private final double z;
-
-  /** Serializable version identifier */
-  private static final long serialVersionUID = -5721105387745193385L;
+  /** Compute the distance between two vectors.
+   * <p>Calling this method is equivalent to calling:
+   * <code>v1.subtract(v2).getNorm()</code> except that no intermediate
+   * vector is built</p>
+   * @param v1 first vector
+   * @param v2 second vector
+   * @return the distance between v1 and v2
+   */
+  public static double distance(Vector3D v1, Vector3D v2) {
+    final double dx = v2.x - v1.x;
+    final double dy = v2.y - v1.y;
+    final double dz = v2.z - v1.z;
+    return Math.sqrt(dx * dx + dy * dy + dz * dz);
+  }
 
+  /** Compute the square of the distance between two vectors.
+   * <p>Calling this method is equivalent to calling:
+   * <code>v1.subtract(v2).getNormSq()</code> except that no intermediate
+   * vector is built</p>
+   * @param v1 first vector
+   * @param v2 second vector
+   * @return the square of the distance between v1 and v2
+   */
+  public static double distanceSq(Vector3D v1, Vector3D v2) {
+    final double dx = v2.x - v1.x;
+    final double dy = v2.y - v1.y;
+    final double dz = v2.z - v1.z;
+    return dx * dx + dy * dy + dz * dz;
+  }
 
 }

Modified: commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml?rev=665700&r1=665699&r2=665700&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml Mon Jun  9 07:17:21 2008
@@ -39,6 +39,17 @@
   </properties>
   <body>
     <release version="2.0" date="TBD" description="TBD">
+      <action dev="luc" type="add">
+        Added several convenience methods for Vector3D and Rotation.
+      </action>
+      <action dev="luc" type="update">
+        Replaced public no argument constructors with IDENTITY or ZERO
+        static instances for immutable classes Vector3D and Rotation.
+      </action>
+      <action dev="luc" type="fix">
+        Fixed inconsistencies in the naming scheme for static fields in
+        Vector3D and Rotation with respect to the overall library.
+      </action>
       <action dev="luc" type="update" >
         Greatly improved RealMatrixImpl and BigMatrixImpl performances,
         both in terms of speed and in terms of temporary memory footprint.

Modified: commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/RotationTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/RotationTest.java?rev=665700&r1=665699&r2=665700&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/RotationTest.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/RotationTest.java
Mon Jun  9 07:17:21 2008
@@ -22,6 +22,7 @@
 import org.apache.commons.math.geometry.Rotation;
 import org.apache.commons.math.geometry.RotationOrder;
 import org.apache.commons.math.geometry.Vector3D;
+import org.apache.commons.math.util.MathUtils;
 
 import junit.framework.*;
 
@@ -34,22 +35,22 @@
 
   public void testIdentity() {
 
-    Rotation r = new Rotation();
-    checkVector(r.applyTo(Vector3D.plusI), Vector3D.plusI);
-    checkVector(r.applyTo(Vector3D.plusJ), Vector3D.plusJ);
-    checkVector(r.applyTo(Vector3D.plusK), Vector3D.plusK);
+    Rotation r = Rotation.IDENTITY;
+    checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_I);
+    checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_J);
+    checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_K);
     checkAngle(r.getAngle(), 0);
 
     r = new Rotation(-1, 0, 0, 0, false);
-    checkVector(r.applyTo(Vector3D.plusI), Vector3D.plusI);
-    checkVector(r.applyTo(Vector3D.plusJ), Vector3D.plusJ);
-    checkVector(r.applyTo(Vector3D.plusK), Vector3D.plusK);
+    checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_I);
+    checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_J);
+    checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_K);
     checkAngle(r.getAngle(), 0);
 
     r = new Rotation(42, 0, 0, 0, true);
-    checkVector(r.applyTo(Vector3D.plusI), Vector3D.plusI);
-    checkVector(r.applyTo(Vector3D.plusJ), Vector3D.plusJ);
-    checkVector(r.applyTo(Vector3D.plusK), Vector3D.plusK);
+    checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_I);
+    checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_J);
+    checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_K);
     checkAngle(r.getAngle(), 0);
 
   }
@@ -57,9 +58,9 @@
   public void testAxisAngle() {
 
     Rotation r = new Rotation(new Vector3D(10, 10, 10), 2 * Math.PI / 3);
-    checkVector(r.applyTo(Vector3D.plusI), Vector3D.plusJ);
-    checkVector(r.applyTo(Vector3D.plusJ), Vector3D.plusK);
-    checkVector(r.applyTo(Vector3D.plusK), Vector3D.plusI);
+    checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_J);
+    checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_K);
+    checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_I);
     double s = 1 / Math.sqrt(3);
     checkVector(r.getAxis(), new Vector3D(s, s, s));
     checkAngle(r.getAngle(), 2 * Math.PI / 3);
@@ -72,15 +73,15 @@
       fail("unexpected exception");
     }
 
-    r = new Rotation(Vector3D.plusK, 1.5 * Math.PI);
+    r = new Rotation(Vector3D.PLUS_K, 1.5 * Math.PI);
     checkVector(r.getAxis(), new Vector3D(0, 0, -1));
     checkAngle(r.getAngle(), 0.5 * Math.PI);
 
-    r = new Rotation(Vector3D.plusJ, Math.PI);
-    checkVector(r.getAxis(), Vector3D.plusJ);
+    r = new Rotation(Vector3D.PLUS_J, Math.PI);
+    checkVector(r.getAxis(), Vector3D.PLUS_J);
     checkAngle(r.getAngle(), Math.PI);
 
-    checkVector(new Rotation().getAxis(), Vector3D.plusI);
+    checkVector(Rotation.IDENTITY.getAxis(), Vector3D.PLUS_I);
 
   }
 
@@ -103,7 +104,7 @@
     checkAngle(new Rotation(u, u.negate()).getAngle(), Math.PI);
 
     try {
-        new Rotation(u, new Vector3D());
+        new Rotation(u, Vector3D.ZERO);
         fail("an exception should have been thrown");
       } catch (IllegalArgumentException e) {
         // expected behavior
@@ -120,20 +121,20 @@
     Vector3D v1 = new Vector3D(0, 0, 2);
     Vector3D v2 = new Vector3D(-2, 0, 2);
     Rotation r = new Rotation(u1, u2, v1, v2);
-    checkVector(r.applyTo(Vector3D.plusI), Vector3D.plusK);
-    checkVector(r.applyTo(Vector3D.plusJ), Vector3D.minusI);
+    checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_K);
+    checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.MINUS_I);
 
     r = new Rotation(u1, u2, u1.negate(), u2.negate());
     Vector3D axis = r.getAxis();
-    if (Vector3D.dotProduct(axis, Vector3D.plusK) > 0) {
-      checkVector(axis, Vector3D.plusK);
+    if (Vector3D.dotProduct(axis, Vector3D.PLUS_K) > 0) {
+      checkVector(axis, Vector3D.PLUS_K);
     } else {
-      checkVector(axis, Vector3D.minusK);
+      checkVector(axis, Vector3D.MINUS_K);
     }
     checkAngle(r.getAngle(), Math.PI);
 
     double sqrt = Math.sqrt(2) / 2;
-    r = new Rotation(Vector3D.plusI,  Vector3D.plusJ,
+    r = new Rotation(Vector3D.PLUS_I,  Vector3D.PLUS_J,
                      new Vector3D(0.5, 0.5,  sqrt),
                      new Vector3D(0.5, 0.5, -sqrt));
     checkRotation(r, sqrt, 0.5, 0.5, 0);
@@ -144,7 +145,7 @@
     checkRotation(new Rotation(u1, u2, u1, u2), 1, 0, 0, 0);
 
     try {
-        new Rotation(u1, u2, new Vector3D(), v2);
+        new Rotation(u1, u2, Vector3D.ZERO, v2);
         fail("an exception should have been thrown");
     } catch (IllegalArgumentException e) {
       // expected behavior
@@ -224,9 +225,9 @@
                       { 0.0, 0.0, 1.0 },
                       { 1.0, 0.0, 0.0 } };
     Rotation r = new Rotation(m1, 1.0e-7);
-    checkVector(r.applyTo(Vector3D.plusI), Vector3D.plusK);
-    checkVector(r.applyTo(Vector3D.plusJ), Vector3D.plusI);
-    checkVector(r.applyTo(Vector3D.plusK), Vector3D.plusJ);
+    checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_K);
+    checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_I);
+    checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_J);
 
     double[][] m2 = { { 0.83203, -0.55012, -0.07139 },
                       { 0.48293,  0.78164, -0.39474 },
@@ -277,11 +278,11 @@
       }
     }
 
-    checkVector(r.applyTo(Vector3D.plusI),
+    checkVector(r.applyTo(Vector3D.PLUS_I),
                 new Vector3D(m3[0][0], m3[1][0], m3[2][0]));
-    checkVector(r.applyTo(Vector3D.plusJ),
+    checkVector(r.applyTo(Vector3D.PLUS_J),
                 new Vector3D(m3[0][1], m3[1][1], m3[2][1]));
-    checkVector(r.applyTo(Vector3D.plusK),
+    checkVector(r.applyTo(Vector3D.PLUS_K),
                 new Vector3D(m3[0][2], m3[1][2], m3[2][2]));
 
     double[][] m4 = { { 1.0,  0.0,  0.0 },
@@ -463,7 +464,7 @@
       }
     }
 
-    r = new Rotation();
+    r = Rotation.IDENTITY;
     for (double lambda = 0; lambda < 6.2; lambda += 0.2) {
       for (double phi = -1.55; phi < 1.55; phi += 0.2) {
           Vector3D u = new Vector3D(Math.cos(lambda) * Math.cos(phi),
@@ -474,7 +475,7 @@
       }
     }
 
-    r = new Rotation(Vector3D.plusK, Math.PI);
+    r = new Rotation(Vector3D.PLUS_K, Math.PI);
     for (double lambda = 0; lambda < 6.2; lambda += 0.2) {
       for (double phi = -1.55; phi < 1.55; phi += 0.2) {
           Vector3D u = new Vector3D(Math.cos(lambda) * Math.cos(phi),
@@ -492,13 +493,11 @@
   }
 
   private void checkAngle(double a1, double a2) {
-    a2 -= 2 * Math.PI * Math.floor((a2 + Math.PI - a1) / (2 * Math.PI));
-    assertTrue(Math.abs(a1 - a2) < 1.0e-10);
+    assertEquals(a1, MathUtils.normalizeAngle(a2, a1), 1.0e-10);
   }
 
   private void checkRotation(Rotation r, double q0, double q1, double q2, double q3) {
-    Rotation reference = new Rotation(q0, q1, q2, q3, false);
-    assertEquals(0, r.applyInverseTo(reference).getAngle(), 1.0e-12);
+    assertEquals(0, Rotation.distance(r, new Rotation(q0, q1, q2, q3, false)), 1.0e-12);
   }
 
   public static Test suite() {

Modified: commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/Vector3DTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/Vector3DTest.java?rev=665700&r1=665699&r2=665700&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/Vector3DTest.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/geometry/Vector3DTest.java
Mon Jun  9 07:17:21 2008
@@ -32,17 +32,17 @@
       double r = Math.sqrt(2) /2;
       checkVector(new Vector3D(2, new Vector3D(Math.PI / 3, -Math.PI / 4)),
                   r, r * Math.sqrt(3), -2 * r);
-      checkVector(new Vector3D(2, Vector3D.plusI,
-                              -3, Vector3D.minusK),
+      checkVector(new Vector3D(2, Vector3D.PLUS_I,
+                              -3, Vector3D.MINUS_K),
                   2, 0, 3);
-      checkVector(new Vector3D(2, Vector3D.plusI,
-                               5, Vector3D.plusJ,
-                              -3, Vector3D.minusK),
+      checkVector(new Vector3D(2, Vector3D.PLUS_I,
+                               5, Vector3D.PLUS_J,
+                              -3, Vector3D.MINUS_K),
                   2, 5, 3);
-      checkVector(new Vector3D(2, Vector3D.plusI,
-                               5, Vector3D.plusJ,
-                               5, Vector3D.minusJ,
-                               -3, Vector3D.minusK),
+      checkVector(new Vector3D(2, Vector3D.PLUS_I,
+                               5, Vector3D.PLUS_J,
+                               5, Vector3D.MINUS_J,
+                               -3, Vector3D.MINUS_K),
                   2, 0, 3);
   }
 
@@ -54,7 +54,7 @@
   }
   
   public void testNorm() {
-    assertTrue(Math.abs(new Vector3D().getNorm()) < 1.0e-12);
+    assertTrue(Math.abs(Vector3D.ZERO.getNorm()) < 1.0e-12);
     assertTrue(Math.abs(new Vector3D(1, 2, 3).getNorm() - Math.sqrt(14))
                < 1.0e-12);
   }
@@ -107,12 +107,12 @@
 
   public void testAngular() {
 
-    assertEquals(0,           Vector3D.plusI.getAlpha(), 1.0e-10);
-    assertEquals(0,           Vector3D.plusI.getDelta(), 1.0e-10);
-    assertEquals(Math.PI / 2, Vector3D.plusJ.getAlpha(), 1.0e-10);
-    assertEquals(0,           Vector3D.plusJ.getDelta(), 1.0e-10);
-    assertEquals(0,           Vector3D.plusK.getAlpha(), 1.0e-10);
-    assertEquals(Math.PI / 2, Vector3D.plusK.getDelta(), 1.0e-10);
+    assertEquals(0,           Vector3D.PLUS_I.getAlpha(), 1.0e-10);
+    assertEquals(0,           Vector3D.PLUS_I.getDelta(), 1.0e-10);
+    assertEquals(Math.PI / 2, Vector3D.PLUS_J.getAlpha(), 1.0e-10);
+    assertEquals(0,           Vector3D.PLUS_J.getDelta(), 1.0e-10);
+    assertEquals(0,           Vector3D.PLUS_K.getAlpha(), 1.0e-10);
+    assertEquals(Math.PI / 2, Vector3D.PLUS_K.getDelta(), 1.0e-10);
 
     Vector3D u = new Vector3D(-1, 1, -1);
     assertEquals(3 * Math.PI /4, u.getAlpha(), 1.0e-10);
@@ -134,7 +134,7 @@
   public void testNormalize() {
     assertEquals(1.0, new Vector3D(5, -4, 2).normalize().getNorm(), 1.0e-12);
     try {
-        new Vector3D().normalize();
+        Vector3D.ZERO.normalize();
         fail("an exception should have been thrown");
     } catch (ArithmeticException ae) {
         // expected behavior
@@ -171,7 +171,7 @@
                   Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(-2, -4, -6.000001)),
                   1.0e-12);
      try {
-         Vector3D.angle(new Vector3D(), Vector3D.plusI);
+         Vector3D.angle(Vector3D.ZERO, Vector3D.PLUS_I);
          fail("an exception should have been thrown");
      } catch (ArithmeticException ae) {
          // expected behavior



Mime
View raw message