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 2020/03/07 04:26:08 UTC

[commons-geometry] 02/02: GEOMETRY 90: Use lambda.

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

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

commit f9013c99ec03f397940a5e061bb31d7f6375ae9e
Author: Gilles <gi...@gmail.com>
AuthorDate: Sat Mar 7 05:23:35 2020 +0100

    GEOMETRY 90: Use lambda.
    
    Closes #72.
---
 .../threed/rotation/QuaternionRotation.java        | 59 +++++++---------------
 .../threed/rotation/QuaternionRotationTest.java    |  9 ++--
 2 files changed, 24 insertions(+), 44 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 d48db5c..dab25da 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
@@ -26,6 +26,7 @@ import org.apache.commons.geometry.euclidean.threed.Vector3D;
 import org.apache.commons.numbers.angle.PlaneAngleRadians;
 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
@@ -255,16 +256,27 @@ public final class QuaternionRotation implements Rotation3D {
         return q.multiply(this);
     }
 
-    /** Create a {@link QuaternionRotation.Slerp} instance that can be used to perform spherical
-     * linear interpolation between this instance and the argument.
+    /**
+     * Creates a function that performs a
+     * <a href="https://en.wikipedia.org/wiki/Slerp">spherical
+     * linear interpolation</a> between this instance and the argument.
+     * <p>
+     * The argument to the function returned by this method is the
+     * interpolation parameter {@code t}.
+     * If {@code t = 0}, the rotation is equal to this instance.
+     * If {@code t = 1}, the rotation is equal to the {@code end} instance.
+     * All other values are interpolated (or extrapolated if {@code t} is
+     * outside of the {@code [0, 1]} range).
      *
      * @param end end value of the interpolation
-     * @return function that can be used to interpolate between this instance and
-     *      the argument
-     * @see <a href="https://en.wikipedia.org/wiki/Slerp">Slerp</a>
+     * @return a function that interpolates between this instance and the
+     * argument.
+     *
+     * @see org.apache.commons.numbers.quaternion.Slerp
      */
-    public QuaternionRotation.Slerp slerp(final QuaternionRotation end) {
-        return new QuaternionRotation.Slerp(this, end);
+    public DoubleFunction<QuaternionRotation> slerp(final QuaternionRotation end) {
+        final Slerp s = new Slerp(getQuaternion(), end.getQuaternion());
+        return t -> QuaternionRotation.of(s.apply(t));
     }
 
     /** Get a sequence of axis-angle rotations that produce an overall rotation equivalent to this instance.
@@ -827,37 +839,4 @@ public final class QuaternionRotation implements Rotation3D {
 
         return arr;
     }
-
-    /** Class used to perform spherical linear interpolation (ie, "Slerp") between quaternion rotations.
-     * This class serves as a rotation-specific wrapper around the generic
-     * {@link org.apache.commons.numbers.quaternion.Slerp} class.
-     * @see org.apache.commons.numbers.quaternion.Slerp
-     * @see <a href="https://en.wikipedia.org/wiki/Slerp">Slerp</a>
-     */
-    public static final class Slerp implements DoubleFunction<QuaternionRotation> {
-
-        /** Slerp instance that will perform the interpolation. */
-        private final org.apache.commons.numbers.quaternion.Slerp slerp;
-
-        /** Create a new instance that interpolates between the given start and end rotations.
-         * @param start start of the interpolation
-         * @param end end of the interpolation
-         */
-        public Slerp(final QuaternionRotation start, final QuaternionRotation end) {
-            this.slerp = new org.apache.commons.numbers.quaternion.Slerp(
-                    start.getQuaternion(), end.getQuaternion());
-        }
-
-        /** Perform the interpolation. The rotation returned by this method is controlled by the interpolation
-         * parameter, {@code t}. If {@code t = 0}, a rotation equal to the start instance is returned. If {@code t = 1},
-         * a rotation equal to the end instance is returned.  All other values are interpolated (or extrapolated if
-         * {@code t} is outside of the {@code [0, 1]} range).
-         * @param t interpolation control parameter
-         * @return an interpolated rotation
-         */
-        @Override
-        public QuaternionRotation apply(final double t) {
-            return QuaternionRotation.of(slerp.apply(t));
-        }
-    }
 }
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 d574331..efa2ad0 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
@@ -19,6 +19,7 @@ package org.apache.commons.geometry.euclidean.threed.rotation;
 import java.util.Arrays;
 import java.util.List;
 import java.util.function.UnaryOperator;
+import java.util.function.DoubleFunction;
 import java.util.stream.Collectors;
 
 import org.apache.commons.geometry.core.GeometryTestUtils;
@@ -549,7 +550,7 @@ public class QuaternionRotationTest {
         // arrange
         QuaternionRotation q0 = QuaternionRotation.fromAxisAngle(Vector3D.Unit.PLUS_Z, 0.0);
         QuaternionRotation q1 = QuaternionRotation.fromAxisAngle(Vector3D.Unit.PLUS_Z, PlaneAngleRadians.PI);
-        QuaternionRotation.Slerp fn = q0.slerp(q1);
+        DoubleFunction<QuaternionRotation> fn = q0.slerp(q1);
         Vector3D v = Vector3D.of(2, 0, 1);
 
         double sqrt2 = Math.sqrt(2);
@@ -601,7 +602,7 @@ public class QuaternionRotationTest {
     }
 
     private void checkSlerpCombination(QuaternionRotation start, QuaternionRotation end) {
-        QuaternionRotation.Slerp slerp = start.slerp(end);
+        DoubleFunction<QuaternionRotation> slerp = start.slerp(end);
         Vector3D vec = Vector3D.of(1, 1, 1).normalize();
 
         Vector3D startVec = start.apply(vec);
@@ -694,13 +695,13 @@ public class QuaternionRotationTest {
         QuaternionRotation q2 = QuaternionRotation.fromAxisAngle(Vector3D.Unit.PLUS_Z, 0.75 * PlaneAngleRadians.PI);
 
         // act/assert
-        QuaternionRotation.Slerp slerp12 = q1.slerp(q2);
+        DoubleFunction<QuaternionRotation> slerp12 = q1.slerp(q2);
         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_X, slerp12.apply(-4.5).apply(vec), EPS);
         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_X, slerp12.apply(-0.5).apply(vec), EPS);
         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_X, slerp12.apply(1.5).apply(vec), EPS);
         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_X, slerp12.apply(5.5).apply(vec), EPS);
 
-        QuaternionRotation.Slerp slerp21 = q2.slerp(q1);
+        DoubleFunction<QuaternionRotation> slerp21 = q2.slerp(q1);
         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_X, slerp21.apply(-4.5).apply(vec), EPS);
         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.MINUS_X, slerp21.apply(-0.5).apply(vec), EPS);
         EuclideanTestUtils.assertCoordinatesEqual(Vector3D.Unit.PLUS_X, slerp21.apply(1.5).apply(vec), EPS);