You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2018/12/16 00:51:29 UTC

[commons-geometry] branch feature_GEOMETRY-14 updated (ef516df -> 437c7c4)

This is an automated email from the ASF dual-hosted git repository.

erans pushed a change to branch feature_GEOMETRY-14
in repository https://gitbox.apache.org/repos/asf/commons-geometry.git.


    from ef516df  Typo in unit test class name.
     new 1a4d38f  GEOMETRY-14: Removed array input/output.
     new b0dde35  Reuse existing instance.
     new 437c7c4  GEOMETRY-14: Use class "Slerp" from "Commons Numbers".

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../threed/rotation/QuaternionRotation.java        |  58 +++------
 .../geometry/euclidean/threed/rotation/Slerp.java  | 144 ---------------------
 .../threed/rotation/QuaternionRotationTest.java    |  64 ++-------
 3 files changed, 25 insertions(+), 241 deletions(-)
 delete mode 100644 commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/Slerp.java


[commons-geometry] 01/03: GEOMETRY-14: Removed array input/output.

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch feature_GEOMETRY-14
in repository https://gitbox.apache.org/repos/asf/commons-geometry.git

commit 1a4d38ff8d28e9292344940aee71ffb3fa782e73
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Sun Dec 16 00:36:06 2018 +0100

    GEOMETRY-14: Removed array input/output.
---
 .../threed/rotation/QuaternionRotation.java        | 53 ++++++----------------
 .../threed/rotation/QuaternionRotationTest.java    | 52 ---------------------
 2 files changed, 13 insertions(+), 92 deletions(-)

diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java
index 714d1c4..dcd7ca1 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java
@@ -71,7 +71,6 @@ public final class QuaternionRotation implements Serializable {
         this.quat = quat.positivePolarForm();
     }
 
-
     /** Get the underlying quaternion instance.
      * @return the quaternion instance
      */
@@ -80,16 +79,6 @@ public final class QuaternionRotation implements Serializable {
     }
 
     /**
-     * Get the quaternion coordinates as an array in the order
-     * {@code [w, x, y, z]}.
-     *
-     * @return the quaternion coordinates in the order {@code [w, x, y, z]}
-     */
-    public double[] toArray() {
-        return new double[] { quat.getW(), quat.getX(), quat.getY(), quat.getZ() };
-    }
-
-    /**
      * Get the axis of rotation as a normalized {@link Vector3D}. The rotation axis
      * is not well defined when the rotation is the identity rotation, ie it has a
      * rotation angle of zero. In this case, the vector representing the positive
@@ -521,7 +510,7 @@ public final class QuaternionRotation implements Serializable {
      * Create a new instance from the given quaternion values. The inputs are
      * normalized and converted to positive polar form (ie, with w &gt;= 0).
      *
-     * @param w quaterion scalar component
+     * @param w quaternion scalar component
      * @param x first quaternion vectorial component
      * @param y second quaternion vectorial component
      * @param z third quaternion vectorial component
@@ -531,25 +520,13 @@ public final class QuaternionRotation implements Serializable {
      * @see Quaternion#normalize()
      * @see Quaternion#positivePolarForm()
      */
-    public static QuaternionRotation of(final double w, final double x, final double y, final double z) {
+    public static QuaternionRotation of(final double w,
+                                        final double x,
+                                        final double y,
+                                        final double z) {
         return of(Quaternion.of(w, x, y, z));
     }
 
-    /** Create a new instance from the components in the given 4-element array. The
-     * components must be in the order {@code [w, x, y, z]}. The components are
-     * normalized.
-     *
-     * @param q quaternion component array
-     * @return new quaterion containing the normalized components
-     * @throws IllegalArgumentException if the array does not have 4 elements
-     */
-    public static QuaternionRotation of(final double[] q) {
-        if (q.length != 4) {
-            throw new IllegalArgumentException("Dimension mismatch: " + q.length + " != 4");
-        }
-        return QuaternionRotation.of(q[0], q[1], q[2], q[3]);
-    }
-
     /** Return an instance representing a rotation of zero.
      * @return instance representing a rotation of zero.
      */
@@ -626,12 +603,10 @@ public final class QuaternionRotation implements Serializable {
             // an arbitrary unit vector orthogonal to u1
             final Vector3D axis = u.orthogonal();
 
-            return of(
-                        0.0,
-                        axis.getX(),
-                        axis.getY(),
-                        axis.getZ()
-                    );
+            return of(0,
+                      axis.getX(),
+                      axis.getY(),
+                      axis.getZ());
         }
 
         // General case:
@@ -657,12 +632,10 @@ public final class QuaternionRotation implements Serializable {
         final double vectorialScaleFactor = 1.0 / (2.0 * w * normProduct);
         final Vector3D axis = u.crossProduct(v);
 
-        return of(
-                    w,
-                    vectorialScaleFactor * axis.getX(),
-                    vectorialScaleFactor * axis.getY(),
-                    vectorialScaleFactor * axis.getZ()
-                );
+        return of(w,
+                  vectorialScaleFactor * axis.getX(),
+                  vectorialScaleFactor * axis.getY(),
+                  vectorialScaleFactor * axis.getZ());
     }
 
     /** Return an instance that rotates the basis defined by the first two vectors into the basis
diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotationTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotationTest.java
index 4217278..023b007 100644
--- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotationTest.java
+++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotationTest.java
@@ -113,43 +113,6 @@ public class QuaternionRotationTest {
     }
 
     @Test
-    public void testOf_array() {
-        // act/assert
-        checkQuaternion(QuaternionRotation.of(new double[] { 1, 0, 0, 0 }), 1, 0, 0, 0);
-        checkQuaternion(QuaternionRotation.of(new double[] { -1, 0, 0, 0 }), 1, 0, 0, 0);
-        checkQuaternion(QuaternionRotation.of(new double[] { 0, 1, 0, 0 }), 0, 1, 0, 0);
-        checkQuaternion(QuaternionRotation.of(new double[] { 0, 0, 1, 0 }), 0, 0, 1, 0);
-        checkQuaternion(QuaternionRotation.of(new double[] { 0, 0, 0, 1 }), 0, 0, 0, 1);
-
-        checkQuaternion(QuaternionRotation.of(new double[] { 1, 1, 1, 1 }), 0.5, 0.5, 0.5, 0.5);
-        checkQuaternion(QuaternionRotation.of(new double[] { -1, -1, -1, -1 }), 0.5, 0.5, 0.5, 0.5);
-    }
-
-    @Test
-    public void testOf_array_illegalNorm() {
-        // act/assert
-        GeometryTestUtils.assertThrows(() -> QuaternionRotation.of(new double[] { 0, 0, 0, 0 }),
-                IllegalStateException.class);
-        GeometryTestUtils.assertThrows(() -> QuaternionRotation.of(new double[] { 1, 1, 1, Double.NaN }),
-                IllegalStateException.class);
-        GeometryTestUtils.assertThrows(() -> QuaternionRotation.of(new double[] { 1, 1, Double.POSITIVE_INFINITY, 1 }),
-                IllegalStateException.class);
-        GeometryTestUtils.assertThrows(() -> QuaternionRotation.of(new double[] { 1, Double.NEGATIVE_INFINITY, 1, 1 }),
-                IllegalStateException.class);
-        GeometryTestUtils.assertThrows(() -> QuaternionRotation.of(new double[] { Double.NaN, 1, 1, 1 }),
-                IllegalStateException.class);
-    }
-
-    @Test
-    public void testOf_array_invalidLength() {
-        // act/assert
-        GeometryTestUtils.assertThrows(() -> QuaternionRotation.of(new double[] { 1, 1, 1 }),
-                IllegalArgumentException.class, "Dimension mismatch: 3 != 4");
-        GeometryTestUtils.assertThrows(() -> QuaternionRotation.of(new double[] { 1, 1, 1, 1, 1 }),
-                IllegalArgumentException.class, "Dimension mismatch: 5 != 4");
-    }
-
-    @Test
     public void testIdentity() {
         // act
         QuaternionRotation q = QuaternionRotation.identity();
@@ -168,21 +131,6 @@ public class QuaternionRotationTest {
     }
 
     @Test
-    public void testToArray() {
-        // arrange
-        QuaternionRotation q = QuaternionRotation.of(1, 2, 3, 4);
-        double base = 1.0 / Math.sqrt(30);
-
-        // act
-        double[] arr = q.toArray();
-
-        // assert
-        Assert.assertArrayEquals(new double[] {
-                base, 2 * base, 3 * base, 4 * base
-        }, arr, EPS);
-    }
-
-    @Test
     public void testGetAxis()
     {
         // act/assert


[commons-geometry] 03/03: GEOMETRY-14: Use class "Slerp" from "Commons Numbers".

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch feature_GEOMETRY-14
in repository https://gitbox.apache.org/repos/asf/commons-geometry.git

commit 437c7c4976dde2757fb70aaae08fc86c30e017e7
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Sun Dec 16 01:42:32 2018 +0100

    GEOMETRY-14: Use class "Slerp" from "Commons Numbers".
---
 .../threed/rotation/QuaternionRotation.java        |   3 +-
 .../geometry/euclidean/threed/rotation/Slerp.java  | 144 ---------------------
 .../threed/rotation/QuaternionRotationTest.java    |  12 +-
 3 files changed, 11 insertions(+), 148 deletions(-)

diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java
index feaf9da..2dc4a58 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java
@@ -28,6 +28,7 @@ import org.apache.commons.geometry.euclidean.threed.AffineTransformMatrix3D;
 import org.apache.commons.geometry.euclidean.threed.Vector3D;
 import org.apache.commons.numbers.arrays.LinearCombination;
 import org.apache.commons.numbers.quaternion.Quaternion;
+import org.apache.commons.numbers.quaternion.Slerp;
 
 /**
  * Class using a unit-length quaternion to represent
@@ -202,7 +203,7 @@ public final class QuaternionRotation implements Serializable {
      * @return the transform.
      */
     public Slerp slerp(QuaternionRotation end) {
-        return new Slerp(getQuaternion(), end.getQuaternion());
+        return new Slerp(quat, end.quat);
     }
 
     /**
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/Slerp.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/Slerp.java
deleted file mode 100644
index b83d473..0000000
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/Slerp.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * 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.geometry.euclidean.threed.rotation;
-
-import java.util.function.DoubleFunction;
-import org.apache.commons.numbers.quaternion.Quaternion;
-
-/**
- * Perform spherical linear interpolation (<a href="https://en.wikipedia.org/wiki/Slerp">Slerp</a>).
- *
- * <p>
- * The <em>Slerp</em> algorithm is designed to interpolate smoothly between two rotations/orientations,
- * producing a constant-speed motion along an arc.
- * The original purpose of this algorithm was to animate 3D rotations.
- * </p>
- */
-public class Slerp implements DoubleFunction<Quaternion> {
-    /**
-     * Threshold max value for the dot product.
-     * If the quaternion dot product is greater than this value (i.e. the
-     * quaternions are very close to each other), then the quaternions are
-     * linearly interpolated instead of spherically interpolated.
-     */
-    private static final double MAX_DOT_THRESHOLD = 0.9995;
-    /** Start of the interpolation. */
-    private final Quaternion start;
-    /** End of the interpolation. */
-    private final Quaternion end;
-    /** Linear or spherical interpolation algorithm. */
-    private final DoubleFunction<Quaternion> algo;
-
-    /**
-     * @param start Start of the interpolation.
-     * @param end End of the interpolation.
-     */
-    public Slerp(Quaternion start,
-                 Quaternion end) {
-        this.start = start.positivePolarForm();
-
-        final Quaternion e = end.positivePolarForm();
-        double dot = this.start.dotProduct(e);
-
-        // If the dot product is negative, then the interpolation won't follow the shortest
-        // angular path between the two quaterions. In this case, invert the end quaternion
-        // to produce an equivalent rotation that will give us the path we want.
-        if (dot < 0) {
-            dot = -dot;
-            this.end = e.negate();
-        } else {
-            this.end = e;
-        }
-
-        algo = dot > MAX_DOT_THRESHOLD ?
-            new Linear() :
-            new Spherical(dot);
-    }
-
-    /**
-     * Performs the interpolation.
-     * The rotation returned by this method is controlled by the interpolation parameter, {@code t}.
-     * All other values are interpolated (or extrapolated if {@code t} is outside of the
-     * {@code [0, 1]} range).
-     *
-     * @param t Interpolation control parameter.
-     * When {@code t = 0}, a rotation equal to the start instance is returned.
-     * When {@code t = 1}, a rotation equal to the end instance is returned.
-     * @return an interpolated quaternion.
-     */
-    @Override
-    public Quaternion apply(double t) {
-        // Handle no-op cases.
-        if (t == 0) {
-            return start;
-        } else if (t == 1) {
-            return end;
-        }
-
-        return algo.apply(t);
-    }
-
-    /**
-     * Linear interpolation, used when the quaternions are too closely aligned. 
-     */
-    private class Linear implements DoubleFunction<Quaternion> {
-        /** Default constructor. */
-        Linear() {}
-
-        /** {@inheritDoc} */
-        @Override
-        public Quaternion apply(double t) {
-            final double f = 1 - t;
-            return Quaternion.of(f * start.getW() + t * end.getW(),
-                                 f * start.getX() + t * end.getX(),
-                                 f * start.getY() + t * end.getY(),
-                                 f * start.getZ() + t * end.getZ()).positivePolarForm();
-        }
-    }
-
-    /**
-     * Spherical interpolation, used whe the quaternions are too closely aligned. 
-     * When we may end up dividing by zero (cf. 1/sin(theta) term below).
-     * {@link Linear} interpolation must be used.
-     */
-    private class Spherical implements DoubleFunction<Quaternion> {
-        /** Angle of rotation. */
-        private final double theta;
-        /** Sine of {@link #theta}. */
-        private final double sinTheta;
-
-        /**
-         * @param dot Dot product of the start and end quaternions.
-         */
-        Spherical(double dot) {
-            theta = Math.acos(dot);
-            sinTheta = Math.sin(theta);
-        }
-
-        /** {@inheritDoc} */
-        @Override
-        public Quaternion apply(double t) {
-            final double f1 = Math.sin((1 - t) * theta) / sinTheta;
-            final double f2 = Math.sin(t * theta) / sinTheta;
-
-            return Quaternion.of(f1 * start.getW() + f2 * end.getW(),
-                                 f1 * start.getX() + f2 * end.getX(),
-                                 f1 * start.getY() + f2 * end.getY(),
-                                 f1 * start.getZ() + f2 * end.getZ()).positivePolarForm();
-        }
-    }
-}
diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotationTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotationTest.java
index 023b007..af2804c 100644
--- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotationTest.java
+++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotationTest.java
@@ -36,6 +36,7 @@ import org.apache.commons.geometry.euclidean.threed.rotation.QuaternionRotation;
 import org.apache.commons.numbers.angle.PlaneAngleRadians;
 import org.apache.commons.numbers.core.Precision;
 import org.apache.commons.numbers.quaternion.Quaternion;
+import org.apache.commons.numbers.quaternion.Slerp;
 import org.apache.commons.rng.UniformRandomProvider;
 import org.apache.commons.rng.simple.RandomSource;
 import org.junit.Assert;
@@ -613,7 +614,8 @@ public class QuaternionRotationTest {
             // make sure that we're steadily progressing to the end angle
             double angle = slerpVec.angle(startVec);
             Assert.assertTrue("Expected slerp angle to continuously increase; previous angle was " +
-                    prevAngle + " and new angle is " + angle, Precision.compareTo(angle, prevAngle, EPS) >= 0);
+                              prevAngle + " and new angle is " + angle,
+                              Precision.compareTo(angle, prevAngle, EPS) >= 0);
 
             prevAngle = angle;
         }
@@ -623,7 +625,7 @@ public class QuaternionRotationTest {
     public void testSlerp_followsShortestPath() {
         // arrange
         QuaternionRotation q1 = QuaternionRotation.fromAxisAngle(Vector3D.PLUS_Z, 0.75 * Geometry.PI);
-        QuaternionRotation q2 = QuaternionRotation.fromAxisAngle(Vector3D.PLUS_Z, - 0.75 * Geometry.PI);
+        QuaternionRotation q2 = QuaternionRotation.fromAxisAngle(Vector3D.PLUS_Z, -0.75 * Geometry.PI);
 
         // act
         QuaternionRotation result = QuaternionRotation.of(q1.slerp(q2).apply(0.5));
@@ -668,7 +670,11 @@ public class QuaternionRotationTest {
             QuaternionRotation result = QuaternionRotation.of(q1.slerp(q2).apply(t));
 
             // assert
-            Assert.assertEquals(1.0, Vectors.norm(result.getQuaternion().getX(), result.getQuaternion().getY(), result.getQuaternion().getZ(), result.getQuaternion().getW()), EPS);
+            Assert.assertEquals(1.0, Vectors.norm(result.getQuaternion().getX(),
+                                                  result.getQuaternion().getY(),
+                                                  result.getQuaternion().getZ(),
+                                                  result.getQuaternion().getW()),
+                                EPS);
         }
     }
 


[commons-geometry] 02/03: Reuse existing instance.

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch feature_GEOMETRY-14
in repository https://gitbox.apache.org/repos/asf/commons-geometry.git

commit b0dde35c7a5a1d0c5bd3e392a341f321ca39d2ac
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Sun Dec 16 00:39:22 2018 +0100

    Reuse existing instance.
---
 .../commons/geometry/euclidean/threed/rotation/QuaternionRotation.java  | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java
index dcd7ca1..feaf9da 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/rotation/QuaternionRotation.java
@@ -57,7 +57,7 @@ public final class QuaternionRotation implements Serializable {
     /** Instance used to represent the identity rotation, ie a rotation with
      * an angle of zero.
      */
-    private static final QuaternionRotation IDENTITY_INSTANCE = of(1, 0, 0, 0);
+    private static final QuaternionRotation IDENTITY_INSTANCE = of(Quaternion.ONE);
 
     /** Unit-length quaternion instance in positive polar form. */
     private final Quaternion quat;