You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2007/09/10 16:40:43 UTC

svn commit: r574265 - in /commons/proper/math/trunk/src/test/org/apache/commons/math/geometry: RotationOrderTest.java RotationTest.java Vector3DTest.java

Author: luc
Date: Mon Sep 10 07:40:42 2007
New Revision: 574265

URL: http://svn.apache.org/viewvc?rev=574265&view=rev
Log:
improved test coverage

Added:
    commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationOrderTest.java   (with props)
Modified:
    commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationTest.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java

Added: commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationOrderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationOrderTest.java?rev=574265&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationOrderTest.java (added)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationOrderTest.java Mon Sep 10 07:40:42 2007
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.geometry;
+
+import java.lang.reflect.Field;
+
+import org.apache.commons.math.geometry.RotationOrder;
+
+import junit.framework.*;
+
+public class RotationOrderTest
+  extends TestCase {
+
+  public RotationOrderTest(String name) {
+    super(name);
+  }
+
+  public void testName() {
+
+    RotationOrder[] orders = {
+      RotationOrder.XYZ, RotationOrder.XZY, RotationOrder.YXZ,
+      RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX,
+      RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY,
+      RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ
+    };
+
+    for (int i = 0; i < orders.length; ++i) {
+      assertEquals(getFieldName(orders[i]), orders[i].toString());
+    }
+
+  }
+
+  private String getFieldName(RotationOrder order) {
+    try {
+      Field[] fields = RotationOrder.class.getFields();
+      for (int i = 0; i < fields.length; ++i) {
+        if (fields[i].get(null) == order) {
+          return fields[i].getName();
+        }
+      }
+    } catch (IllegalAccessException iae) {
+      // ignored
+    }
+    return "unknown";
+  }
+
+  public static Test suite() {
+    return new TestSuite(RotationOrderTest.class);
+  }
+
+}

Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationOrderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationTest.java?rev=574265&r1=574264&r2=574265&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/RotationTest.java Mon Sep 10 07:40:42 2007
@@ -80,6 +80,17 @@
     checkVector(r.getAxis(), Vector3D.plusJ);
     checkAngle(r.getAngle(), Math.PI);
 
+    checkVector(new Rotation().getAxis(), Vector3D.plusI);
+
+  }
+
+  public void testRevert() {
+    Rotation r = new Rotation(0.001, 0.36, 0.48, 0.8, true);
+    Rotation reverted = r.revert();
+    checkRotation(r.applyTo(reverted), 1, 0, 0, 0);
+    checkRotation(reverted.applyTo(r), 1, 0, 0, 0);
+    assertEquals(r.getAngle(), reverted.getAngle(), 1.0e-12);
+    assertEquals(-1, Vector3D.dotProduct(r.getAxis(), reverted.getAxis()), 1.0e-12);
   }
 
   public void testVectorOnePair() {
@@ -91,6 +102,14 @@
 
     checkAngle(new Rotation(u, u.negate()).getAngle(), Math.PI);
 
+    try {
+        new Rotation(u, new Vector3D());
+        fail("an exception should have been thrown");
+      } catch (ArithmeticException e) {
+      } catch (Exception e) {
+        fail("unexpected exception");
+    }
+
   }
 
   public void testVectorTwoPairs() {
@@ -112,11 +131,93 @@
     }
     checkAngle(r.getAngle(), Math.PI);
 
+    double sqrt = Math.sqrt(2) / 2;
+    r = new Rotation(Vector3D.plusI,  Vector3D.plusJ,
+                     new Vector3D(0.5, 0.5,  sqrt),
+                     new Vector3D(0.5, 0.5, -sqrt));
+    checkRotation(r, sqrt, 0.5, 0.5, 0);
+
+    r = new Rotation(u1, u2, u1, Vector3D.crossProduct(u1, u2));
+    checkRotation(r, sqrt, -sqrt, 0, 0);
+
+    checkRotation(new Rotation(u1, u2, u1, u2), 1, 0, 0, 0);
+
+    try {
+        new Rotation(u1, u2, new Vector3D(), v2);
+        fail("an exception should have been thrown");
+      } catch (ArithmeticException e) {
+      } catch (Exception e) {
+        fail("unexpected exception");
+    }
+
   }
 
   public void testMatrix()
     throws NotARotationMatrixException {
 
+    try {
+      new Rotation(new double[][] {
+                     { 0.0, 1.0, 0.0 },
+                     { 1.0, 0.0, 0.0 }
+                   }, 1.0e-7);
+    } catch (NotARotationMatrixException nrme) {
+      // expected behavior
+    } catch (Exception e) {
+      fail("wrong exception caught: " + e.getMessage());
+    }
+
+    try {
+      new Rotation(new double[][] {
+                     {  0.445888,  0.797184, -0.407040 },
+                     {  0.821760, -0.184320,  0.539200 },
+                     { -0.354816,  0.574912,  0.737280 }
+                   }, 1.0e-7);
+    } catch (NotARotationMatrixException nrme) {
+      // expected behavior
+    } catch (Exception e) {
+      fail("wrong exception caught: " + e.getMessage());
+    }
+
+    try {
+        new Rotation(new double[][] {
+                       {  0.4,  0.8, -0.4 },
+                       { -0.4,  0.6,  0.7 },
+                       {  0.8, -0.2,  0.5 }
+                     }, 1.0e-15);
+      } catch (NotARotationMatrixException nrme) {
+        // expected behavior
+      } catch (Exception e) {
+        fail("wrong exception caught: " + e.getMessage());
+      }
+
+    checkRotation(new Rotation(new double[][] {
+                                 {  0.445888,  0.797184, -0.407040 },
+                                 { -0.354816,  0.574912,  0.737280 },
+                                 {  0.821760, -0.184320,  0.539200 }
+                               }, 1.0e-10),
+                  0.8, 0.288, 0.384, 0.36);
+
+    checkRotation(new Rotation(new double[][] {
+                                 {  0.539200,  0.737280,  0.407040 },
+                                 {  0.184320, -0.574912,  0.797184 },
+                                 {  0.821760, -0.354816, -0.445888 }
+                              }, 1.0e-10),
+                  0.36, 0.8, 0.288, 0.384);
+
+    checkRotation(new Rotation(new double[][] {
+                                 { -0.445888,  0.797184, -0.407040 },
+                                 {  0.354816,  0.574912,  0.737280 },
+                                 {  0.821760,  0.184320, -0.539200 }
+                               }, 1.0e-10),
+                  0.384, 0.36, 0.8, 0.288);
+
+    checkRotation(new Rotation(new double[][] {
+                                 { -0.539200,  0.737280,  0.407040 },
+                                 { -0.184320, -0.574912,  0.797184 },
+                                 {  0.821760,  0.354816,  0.445888 }
+                               }, 1.0e-10),
+                  0.288, 0.384, 0.36, 0.8);
+
     double[][] m1 = { { 0.0, 1.0, 0.0 },
                       { 0.0, 0.0, 1.0 },
                       { 1.0, 0.0, 0.0 } };
@@ -209,17 +310,11 @@
       RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX
     };
 
-    RotationOrder[] EulerOrders = {
-      RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY,
-      RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ
-    };
-
     for (int i = 0; i < CardanOrders.length; ++i) {
       for (double alpha1 = 0.1; alpha1 < 6.2; alpha1 += 0.3) {
         for (double alpha2 = -1.55; alpha2 < 1.55; alpha2 += 0.3) {
           for (double alpha3 = 0.1; alpha3 < 6.2; alpha3 += 0.3) {
-            Rotation r = new Rotation(CardanOrders[i],
-                                      alpha1, alpha2, alpha3);
+            Rotation r = new Rotation(CardanOrders[i], alpha1, alpha2, alpha3);
             double[] angles = r.getAngles(CardanOrders[i]);
             checkAngle(angles[0], alpha1);
             checkAngle(angles[1], alpha2);
@@ -229,6 +324,11 @@
       }
     }
 
+    RotationOrder[] EulerOrders = {
+            RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY,
+            RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ
+          };
+
     for (int i = 0; i < EulerOrders.length; ++i) {
       for (double alpha1 = 0.1; alpha1 < 6.2; alpha1 += 0.3) {
         for (double alpha2 = 0.05; alpha2 < 3.1; alpha2 += 0.3) {
@@ -246,7 +346,54 @@
 
   }
 
+  public void testSingularities()
+    throws CardanEulerSingularityException {
+
+    RotationOrder[] CardanOrders = {
+      RotationOrder.XYZ, RotationOrder.XZY, RotationOrder.YXZ,
+      RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX
+    };
+
+    double[] singularCardanAngle = { Math.PI / 2, -Math.PI / 2 };
+    for (int i = 0; i < CardanOrders.length; ++i) {
+      for (int j = 0; j < singularCardanAngle.length; ++j) {
+        Rotation r = new Rotation(CardanOrders[i], 0.1, singularCardanAngle[j], 0.3);
+        try {
+          r.getAngles(CardanOrders[i]);
+          fail("an exception should have been caught");
+        } catch (CardanEulerSingularityException cese) {
+          // expected behavior
+        } catch (Exception e) {
+          fail("wrong exception caught: " + e.getMessage());
+        }
+      }
+    }
+
+    RotationOrder[] EulerOrders = {
+            RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY,
+            RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ
+          };
+
+    double[] singularEulerAngle = { 0, Math.PI };
+    for (int i = 0; i < EulerOrders.length; ++i) {
+      for (int j = 0; j < singularEulerAngle.length; ++j) {
+        Rotation r = new Rotation(EulerOrders[i], 0.1, singularEulerAngle[j], 0.3);
+        try {
+          r.getAngles(EulerOrders[i]);
+          fail("an exception should have been caught");
+        } catch (CardanEulerSingularityException cese) {
+          // expected behavior
+        } catch (Exception e) {
+          fail("wrong exception caught: " + e.getMessage());
+        }
+      }
+    }
+
+
+  }
+
   public void testQuaternion() {
+
     Rotation r1 = new Rotation(new Vector3D(2, -3, 5), 1.7);
     double n = 23.5;
     Rotation r2 = new Rotation(n * r1.getQ0(), n * r1.getQ1(),
@@ -260,6 +407,10 @@
         }
       }
     }
+
+    r1 = new Rotation( 0.288,  0.384,  0.36,  0.8, false);
+    checkRotation(r1, -r1.getQ0(), -r1.getQ1(), -r1.getQ2(), -r1.getQ3());
+
   }
 
   public void testCompose() {
@@ -341,6 +492,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);
+  }
+
+  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);
   }
 
   public static Test suite() {

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java?rev=574265&r1=574264&r2=574265&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/geometry/Vector3DTest.java Mon Sep 10 07:40:42 2007
@@ -28,6 +28,24 @@
     super(name);
   }
 
+  public void testConstructors() {
+      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),
+                  2, 0, 3);
+      checkVector(new Vector3D(2, Vector3D.plusI,
+                               5, Vector3D.plusJ,
+                              -3, Vector3D.minusK),
+                  2, 5, 3);
+      checkVector(new Vector3D(2, Vector3D.plusI,
+                               5, Vector3D.plusJ,
+                               5, Vector3D.minusJ,
+                               -3, Vector3D.minusK),
+                  2, 0, 3);
+  }
+
   public void testCoordinates() {
     Vector3D v = new Vector3D(1, 2, 3);
     assertTrue(Math.abs(v.getX() - 1) < 1.0e-12);
@@ -46,9 +64,10 @@
     Vector3D v1 = new Vector3D(1, 2, 3);
     Vector3D v2 = new Vector3D(-3, -2, -1);
     v1 = v1.subtract(v2);
-    checkVector(v1, new Vector3D(4, 4, 4));
+    checkVector(v1, 4, 4, 4);
 
-    checkVector(v2.subtract(v1), new Vector3D(-7, -6, -5));
+    checkVector(v2.subtract(v1), -7, -6, -5);
+    checkVector(v2.subtract(3, v1), -15, -14, -13);
 
   }
 
@@ -56,18 +75,19 @@
     Vector3D v1 = new Vector3D(1, 2, 3);
     Vector3D v2 = new Vector3D(-3, -2, -1);
     v1 = v1.add(v2);
-    checkVector(v1, new Vector3D(-2, 0, 2));
+    checkVector(v1, -2, 0, 2);
 
-    checkVector(v2.add(v1), new Vector3D(-5, -2, 1));
+    checkVector(v2.add(v1), -5, -2, 1);
+    checkVector(v2.add(3, v1), -9, -2, 5);
 
   }
 
   public void testScalarProduct() {
     Vector3D v = new Vector3D(1, 2, 3);
     v = v.multiply(3);
-    checkVector(v, new Vector3D(3, 6, 9));
+    checkVector(v, 3, 6, 9);
 
-    checkVector(v.multiply(0.5), new Vector3D(1.5, 3, 4.5));
+    checkVector(v.multiply(0.5), 1.5, 3, 4.5);
 
   }
 
@@ -78,7 +98,7 @@
     assertTrue(Math.abs(Vector3D.dotProduct(v1, v2) - 11) < 1.0e-12);
 
     Vector3D v3 = Vector3D.crossProduct(v1, v2);
-    checkVector(v3, new Vector3D(3, -10, -1));
+    checkVector(v3, 3, -10, -1);
 
     assertTrue(Math.abs(Vector3D.dotProduct(v1, v3)) < 1.0e-12);
     assertTrue(Math.abs(Vector3D.dotProduct(v2, v3)) < 1.0e-12);
@@ -111,8 +131,59 @@
 
   }
 
-  private void checkVector(Vector3D v1, Vector3D v2) {
-    assertTrue(v1.subtract(v2).getNorm() < 1.0e-12);
+  public void testNormalize() {
+    assertEquals(1.0, new Vector3D(5, -4, 2).normalize().getNorm(), 1.0e-12);
+    try {
+        new Vector3D().normalize();
+        fail("an exception should have been thrown");
+    } catch (ArithmeticException ae) {
+        // expected behavior
+    } catch (Exception e) {
+        fail("wrong exception caught: " + e.getMessage());
+    }
+  }
+
+  public void testOrthogonal() {
+      Vector3D v1 = new Vector3D(0.1, 2.5, 1.3);
+      assertEquals(0.0, Vector3D.dotProduct(v1, v1.orthogonal()), 1.0e-12);
+      Vector3D v2 = new Vector3D(2.3, -0.003, 7.6);
+      assertEquals(0.0, Vector3D.dotProduct(v2, v2.orthogonal()), 1.0e-12);
+      Vector3D v3 = new Vector3D(-1.7, 1.4, 0.2);
+      assertEquals(0.0, Vector3D.dotProduct(v3, v3.orthogonal()), 1.0e-12);
+      try {
+          new Vector3D(0, 0, 0).orthogonal();
+          fail("an exception should have been thrown");
+      } catch (ArithmeticException ae) {
+          // expected behavior
+      } catch (Exception e) {
+          fail("wrong exception caught: " + e.getMessage());
+      }
+  }
+
+  public void testAngle() {
+     assertEquals(0.22572612855273393616, 
+                  Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(4, 5, 6)),
+                  1.0e-12);
+     assertEquals(7.98595620686106654517199e-8, 
+                  Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(2, 4, 6.000001)),
+                  1.0e-12);
+     assertEquals(3.14159257373023116985197793156, 
+                  Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(-2, -4, -6.000001)),
+                  1.0e-12);
+     try {
+         Vector3D.angle(new Vector3D(), Vector3D.plusI);
+         fail("an exception should have been thrown");
+     } catch (ArithmeticException ae) {
+         // expected behavior
+     } catch (Exception e) {
+         fail("wrong exception caught: " + e.getMessage());
+     }
+  }
+
+  private void checkVector(Vector3D v, double x, double y, double z) {
+      assertEquals(x, v.getX(), 1.0e-12);
+      assertEquals(y, v.getY(), 1.0e-12);
+      assertEquals(z, v.getZ(), 1.0e-12);
   }
   
   public static Test suite() {