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 2019/09/27 14:35:10 UTC

[commons-geometry] branch master updated (2447eaf -> 677308a)

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

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


    from 2447eaf  Enable CheckStyle (based on the configuration for "Commons RNG").
     new 42fb4dc  GEOMETRY-61: Improvement of "normalize" functionality for class "Vector1D".
     new 8f863c4  Javadoc.
     new 65d0228  GEOMETRY-61: Improvement of "normalize" functionality for class "Vector2D".
     new a009c78  GEOMETRY-61: Improvement of "normalize" functionality for class "Vector3D".
     new 68e17fa  GEOMETRY-61: Advertize specific characteristics of returned instance.
     new 677308a  GEOMETRY-61: Avoid redundant creation.

The 6 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:
 .../euclidean/oned/AffineTransformMatrix1D.java    |  2 +-
 .../commons/geometry/euclidean/oned/Vector1D.java  | 67 ++++++++++------
 .../euclidean/threed/AffineTransformMatrix3D.java  |  2 +-
 .../geometry/euclidean/threed/Vector3D.java        | 93 ++++++++++++++--------
 .../threed/rotation/QuaternionRotation.java        |  2 +-
 .../euclidean/twod/AffineTransformMatrix2D.java    |  2 +-
 .../commons/geometry/euclidean/twod/Line.java      |  2 +-
 .../commons/geometry/euclidean/twod/Vector2D.java  | 85 ++++++++++++--------
 .../geometry/euclidean/oned/Vector1DTest.java      | 12 +--
 .../geometry/euclidean/threed/PlaneTest.java       | 14 ++--
 .../geometry/euclidean/threed/Vector3DTest.java    | 12 +--
 .../geometry/euclidean/twod/Vector2DTest.java      | 12 +--
 12 files changed, 183 insertions(+), 122 deletions(-)


[commons-geometry] 03/06: GEOMETRY-61: Improvement of "normalize" functionality for class "Vector2D".

Posted by er...@apache.org.
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 65d0228f1dab8c4f37a06f96270ab0f01a440b79
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Sep 27 13:29:18 2019 +0200

    GEOMETRY-61: Improvement of "normalize" functionality for class "Vector2D".
    
    "Unit" class defines factory methods "from" to create a normalized instance.
---
 .../euclidean/twod/AffineTransformMatrix2D.java    |  2 +-
 .../commons/geometry/euclidean/twod/Line.java      |  2 +-
 .../commons/geometry/euclidean/twod/Vector2D.java  | 77 +++++++++++++---------
 .../geometry/euclidean/twod/Vector2DTest.java      | 12 ++--
 4 files changed, 55 insertions(+), 38 deletions(-)

diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/AffineTransformMatrix2D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/AffineTransformMatrix2D.java
index 2192f43..ae80335 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/AffineTransformMatrix2D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/AffineTransformMatrix2D.java
@@ -164,7 +164,7 @@ public final class AffineTransformMatrix2D implements AffineTransformMatrix<Vect
      */
     @Override
     public Vector2D applyDirection(final Vector2D vec) {
-        return applyVector(vec, Vector2D::normalize);
+        return applyVector(vec, Vector2D.Unit::from);
     }
 
     /** Apply a translation to the current instance, returning the result as a new transform.
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Line.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Line.java
index 2fc9446..3ac9d9d 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Line.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Line.java
@@ -420,7 +420,7 @@ public final class Line implements Hyperplane<Vector2D>, Embedding<Vector2D, Vec
      *      abscissa (x) axis.
      */
     public static Line fromPointAndAngle(final Vector2D pt, final double angle, final DoublePrecisionContext precision) {
-        final Vector2D dir = Vector2D.normalize(Math.cos(angle), Math.sin(angle));
+        final Vector2D.Unit dir = Vector2D.Unit.from(Math.cos(angle), Math.sin(angle));
         return fromPointAndDirection(pt, dir, precision);
     }
 
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java
index 1b7c797..85dac32 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java
@@ -33,16 +33,16 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
     public static final Vector2D ZERO   = new Vector2D(0, 0);
 
     /** Unit vector pointing in the direction of the positive x-axis. */
-    public static final Vector2D PLUS_X = new UnitVector(1, 0);
+    public static final Vector2D PLUS_X = Unit.PLUS_X;
 
     /** Unit vector pointing in the direction of the negative x-axis. */
-    public static final Vector2D MINUS_X = new UnitVector(-1, 0);
+    public static final Vector2D MINUS_X = Unit.MINUS_X;
 
     /** Unit vector pointing in the direction of the positive y-axis. */
-    public static final Vector2D PLUS_Y = new UnitVector(0, 1);
+    public static final Vector2D PLUS_Y = Unit.PLUS_Y;
 
     /** Unit vector pointing in the direction of the negative y-axis. */
-    public static final Vector2D MINUS_Y = new UnitVector(0, -1);
+    public static final Vector2D MINUS_Y = Unit.MINUS_Y;
 
     // CHECKSTYLE: stop ConstantName
     /** A vector with all coordinates set to NaN. */
@@ -123,10 +123,7 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
     /** {@inheritDoc} */
     @Override
     public Vector2D directionTo(Vector2D v) {
-        return normalize(
-                    v.x - x,
-                    v.y - y
-                );
+        return vectorTo(v).normalize();
     }
 
     /** {@inheritDoc} */
@@ -197,7 +194,7 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
     /** {@inheritDoc} */
     @Override
     public Vector2D normalize() {
-        return normalize(x, y);
+        return Unit.from(x, y);
     }
 
     /** {@inheritDoc} */
@@ -273,13 +270,13 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
      */
     @Override
     public Vector2D orthogonal() {
-        return normalize(-y, x);
+        return Unit.from(-y, x);
     }
 
     /** {@inheritDoc} */
     @Override
     public Vector2D orthogonal(Vector2D dir) {
-        return dir.getComponent(this, true, Vector2D::normalize);
+        return dir.getComponent(this, true, Vector2D.Unit::from);
     }
 
     /** Compute the signed area of the parallelogram with sides formed by this instance
@@ -432,19 +429,6 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
         return new Vector2D(v[0], v[1]);
     }
 
-    /** Returns a normalized vector derived from the given values.
-     * @param x abscissa (first coordinate value)
-     * @param y ordinate (second coordinate value)
-     * @return normalized vector instance
-     * @throws IllegalNormException if the norm of the given values is zero, NaN, or infinite
-     */
-    public static Vector2D normalize(final double x, final double y) {
-        final double norm = Vectors.checkedNorm(Vectors.norm(x, y));
-        final double invNorm = 1.0 / norm;
-
-        return new UnitVector(x * invNorm, y * invNorm);
-    }
-
     /** Parses the given string and returns a new vector instance. The expected string
      * format is the same as that returned by {@link #toString()}.
      * @param str the string to parse
@@ -531,10 +515,19 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
                 LinearCombination.value(a1, v1.y, a2, v2.y, a3, v3.y, a4, v4.y));
     }
 
-    /** Private class used to represent unit vectors. This allows optimizations to be performed for certain
-     * operations.
+    /**
+     * Represents unit vectors.
+     * This allows optimizations to be performed for certain operations.
      */
-    private static final class UnitVector extends Vector2D {
+    public static final class Unit extends Vector2D {
+        /** Unit vector (coordinates: 1, 0). */
+        static final Unit PLUS_X  = new Unit(1d, 0d);
+        /** Negation of unit vector (coordinates: -1, 0). */
+        static final Unit MINUS_X = new Unit(-1d, 0d);
+        /** Unit vector (coordinates: 0, 1). */
+        static final Unit PLUS_Y  = new Unit(0d, 1d);
+        /** Negation of unit vector (coordinates: 0, -1). */
+        static final Unit MINUS_Y = new Unit(0d, -1d);
 
         /** Serializable version identifier */
         private static final long serialVersionUID = 20180903L;
@@ -544,10 +537,34 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
          * @param x abscissa (first coordinate value)
          * @param y abscissa (second coordinate value)
          */
-        private UnitVector(final double x, final double y) {
+        private Unit(final double x, final double y) {
             super(x, y);
         }
 
+        /**
+         * Creates a normalized vector.
+         *
+         * @param x Vector coordinate.
+         * @param y Vector coordinate.
+         * @return a vector whose norm is 1.
+         * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
+         */
+        public static Unit from(double x, double y) {
+            final double invNorm = 1 / Vectors.checkedNorm(Vectors.norm(x, y));
+            return new Unit(x * invNorm, y * invNorm);
+        }
+
+        /**
+         * Creates a normalized vector.
+         *
+         * @param v Vector.
+         * @return a vector whose norm is 1.
+         * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
+         */
+        public static Unit from(Vector2D v) {
+            return from(v.getX(), v.getY());
+        }
+
         /** {@inheritDoc} */
         @Override
         public double norm() {
@@ -574,8 +591,8 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
 
         /** {@inheritDoc} */
         @Override
-        public UnitVector negate() {
-            return new UnitVector(-getX(), -getY());
+        public Unit negate() {
+            return new Unit(-getX(), -getY());
         }
     }
 }
diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/Vector2DTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/Vector2DTest.java
index d619342..70adf0b 100644
--- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/Vector2DTest.java
+++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/Vector2DTest.java
@@ -956,19 +956,19 @@ public class Vector2DTest {
         double invSqrt2 = 1.0 / Math.sqrt(2.0);
 
         // act/assert
-        checkVector(Vector2D.normalize(2.0, -2.0), invSqrt2, -invSqrt2);
-        checkVector(Vector2D.normalize(-4.0, 4.0), -invSqrt2, invSqrt2);
+        checkVector(Vector2D.Unit.from(2.0, -2.0), invSqrt2, -invSqrt2);
+        checkVector(Vector2D.Unit.from(-4.0, 4.0), -invSqrt2, invSqrt2);
     }
 
     @Test
     public void testNormalize_static_illegalNorm() {
-        GeometryTestUtils.assertThrows(() -> Vector2D.normalize(0.0, 0.0),
+        GeometryTestUtils.assertThrows(() -> Vector2D.Unit.from(0.0, 0.0),
                 IllegalNormException.class);
-        GeometryTestUtils.assertThrows(() -> Vector2D.normalize(Double.NaN, 1.0),
+        GeometryTestUtils.assertThrows(() -> Vector2D.Unit.from(Double.NaN, 1.0),
                 IllegalNormException.class);
-        GeometryTestUtils.assertThrows(() -> Vector2D.normalize(1.0, Double.NEGATIVE_INFINITY),
+        GeometryTestUtils.assertThrows(() -> Vector2D.Unit.from(1.0, Double.NEGATIVE_INFINITY),
                 IllegalNormException.class);
-        GeometryTestUtils.assertThrows(() -> Vector2D.normalize(1.0, Double.POSITIVE_INFINITY),
+        GeometryTestUtils.assertThrows(() -> Vector2D.Unit.from(1.0, Double.POSITIVE_INFINITY),
                 IllegalNormException.class);
     }
 


[commons-geometry] 06/06: GEOMETRY-61: Avoid redundant creation.

Posted by er...@apache.org.
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 677308ac7d1ee8f8a78686d3aea5b80f4d909302
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Sep 27 14:22:14 2019 +0200

    GEOMETRY-61: Avoid redundant creation.
---
 .../java/org/apache/commons/geometry/euclidean/oned/Vector1D.java     | 4 +++-
 .../java/org/apache/commons/geometry/euclidean/threed/Vector3D.java   | 4 +++-
 .../java/org/apache/commons/geometry/euclidean/twod/Vector2D.java     | 4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
index 827afde..896e7db 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
@@ -415,7 +415,9 @@ public class Vector1D extends EuclideanVector<Vector1D> {
          * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
          */
         public static Unit from(Vector1D v) {
-            return from(v.getX());
+            return v instanceof Unit ?
+                (Unit) v :
+                from(v.getX());
         }
 
         /** {@inheritDoc} */
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java
index dc73b4c..03a85e2 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java
@@ -632,7 +632,9 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
          * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
          */
         public static Unit from(Vector3D v) {
-            return from(v.getX(), v.getY(), v.getZ());
+            return v instanceof Unit ?
+                (Unit) v :
+                from(v.getX(), v.getY(), v.getZ());
         }
 
         /** {@inheritDoc} */
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java
index 0daf927..b0d6665 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java
@@ -562,7 +562,9 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
          * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
          */
         public static Unit from(Vector2D v) {
-            return from(v.getX(), v.getY());
+            return v instanceof Unit ?
+                (Unit) v :
+                from(v.getX(), v.getY());
         }
 
         /** {@inheritDoc} */


[commons-geometry] 01/06: GEOMETRY-61: Improvement of "normalize" functionality for class "Vector1D".

Posted by er...@apache.org.
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 42fb4dc84e61da07d5f32e2304bdeccc540b9a4d
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Wed Sep 25 15:01:12 2019 +0200

    GEOMETRY-61: Improvement of "normalize" functionality for class "Vector1D".
    
    "Unit" class defines factory methods "from" to create a normalized instance.
---
 .../euclidean/oned/AffineTransformMatrix1D.java    |  2 +-
 .../commons/geometry/euclidean/oned/Vector1D.java  | 54 ++++++++++++++--------
 .../geometry/euclidean/oned/Vector1DTest.java      | 12 ++---
 3 files changed, 42 insertions(+), 26 deletions(-)

diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/AffineTransformMatrix1D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/AffineTransformMatrix1D.java
index 1a6aaf9..045c893 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/AffineTransformMatrix1D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/AffineTransformMatrix1D.java
@@ -108,7 +108,7 @@ public final class AffineTransformMatrix1D implements AffineTransformMatrix<Vect
      */
     @Override
     public Vector1D applyDirection(final Vector1D vec) {
-        return applyVector(vec, Vector1D::normalize);
+        return applyVector(vec, Vector1D.Unit::from);
     }
 
     /** Get a new transform containing the result of applying a translation logically after
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
index 4be9515..a750879 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
@@ -33,10 +33,10 @@ public class Vector1D extends EuclideanVector<Vector1D> {
     public static final Vector1D ZERO = new Vector1D(0.0);
 
     /** Unit vector (coordinates: 1). */
-    public static final Vector1D ONE  = new UnitVector(1.0);
+    public static final Vector1D ONE  = Unit.ONE;
 
     /** Negation of unit vector (coordinates: -1). */
-    public static final Vector1D MINUS_ONE = new UnitVector(-1.0);
+    public static final Vector1D MINUS_ONE = Unit.MINUS_ONE;
 
     // CHECKSTYLE: stop ConstantName
     /** A vector with all coordinates set to NaN. */
@@ -99,7 +99,7 @@ public class Vector1D extends EuclideanVector<Vector1D> {
     /** {@inheritDoc} */
     @Override
     public Vector1D directionTo(Vector1D v) {
-        return normalize(v.x - x);
+        return vectorTo(v).normalize();
     }
 
     /** {@inheritDoc} */
@@ -166,7 +166,7 @@ public class Vector1D extends EuclideanVector<Vector1D> {
     /** {@inheritDoc} */
     @Override
     public Vector1D normalize() {
-        return normalize(x);
+        return Unit.from(x);
     }
 
     /** {@inheritDoc} */
@@ -291,17 +291,6 @@ public class Vector1D extends EuclideanVector<Vector1D> {
         return new Vector1D(x);
     }
 
-    /** Returns a normalized vector derived from the given value.
-     * @param x abscissa (first coordinate value)
-     * @return normalized vector instance
-     * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
-     */
-    public static Vector1D normalize(final double x) {
-        Vectors.checkedNorm(Vectors.norm(x));
-
-        return (x > 0.0) ? ONE : MINUS_ONE;
-    }
-
     /** Parses the given string and returns a new vector instance. The expected string
      * format is the same as that returned by {@link #toString()}.
      * @param str the string to parse
@@ -388,7 +377,11 @@ public class Vector1D extends EuclideanVector<Vector1D> {
     /** Private class used to represent unit vectors. This allows optimizations to be performed for certain
      * operations.
      */
-    private static final class UnitVector extends Vector1D {
+    public static final class Unit extends Vector1D {
+        /** Unit vector (coordinates: 1). */
+        static final Unit ONE  = new Unit(1d);
+        /** Negation of unit vector (coordinates: -1). */
+        static final Unit MINUS_ONE = new Unit(-1d);
 
         /** Serializable version identifier */
         private static final long serialVersionUID = 20180903L;
@@ -397,10 +390,33 @@ public class Vector1D extends EuclideanVector<Vector1D> {
          * values represent a normalized vector.
          * @param x abscissa (first coordinate value)
          */
-        private UnitVector(final double x) {
+        private Unit(final double x) {
             super(x);
         }
 
+        /**
+         * Creates a normalized vector.
+         *
+         * @param v Vector.
+         * @return a vector whose norm is 1.
+         * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
+         */
+        public static Unit from(double x) {
+            Vectors.checkedNorm(Vectors.norm(x));
+            return x > 0 ? ONE : MINUS_ONE;
+        }
+
+        /**
+         * Creates a normalized vector.
+         *
+         * @param v Vector.
+         * @return a vector whose norm is 1.
+         * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
+         */
+        public static Unit from(Vector1D v) {
+            return from(v.getX());
+        }
+
         /** {@inheritDoc} */
         @Override
         public double norm() {
@@ -427,8 +443,8 @@ public class Vector1D extends EuclideanVector<Vector1D> {
 
         /** {@inheritDoc} */
         @Override
-        public UnitVector negate() {
-            return new UnitVector(-getX());
+        public Vector1D negate() {
+            return this == ONE ? MINUS_ONE : ONE;
         }
     }
 }
diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Vector1DTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Vector1DTest.java
index 4583d8b..1b58077 100644
--- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Vector1DTest.java
+++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Vector1DTest.java
@@ -622,19 +622,19 @@ public class Vector1DTest {
     @Test
     public void testNormalize_static() {
         // act/assert
-        checkVector(Vector1D.normalize(2.0), 1);
-        checkVector(Vector1D.normalize(-4.0), -1);
+        checkVector(Vector1D.Unit.from(2.0), 1);
+        checkVector(Vector1D.Unit.from(-4.0), -1);
     }
 
     @Test
     public void testNormalize_static_illegalNorm() {
-        GeometryTestUtils.assertThrows(() -> Vector1D.normalize(0.0),
+        GeometryTestUtils.assertThrows(() -> Vector1D.Unit.from(0.0),
                 IllegalNormException.class);
-        GeometryTestUtils.assertThrows(() -> Vector1D.normalize(Double.NaN),
+        GeometryTestUtils.assertThrows(() -> Vector1D.Unit.from(Double.NaN),
                 IllegalNormException.class);
-        GeometryTestUtils.assertThrows(() -> Vector1D.normalize(Double.NEGATIVE_INFINITY),
+        GeometryTestUtils.assertThrows(() -> Vector1D.Unit.from(Double.NEGATIVE_INFINITY),
                 IllegalNormException.class);
-        GeometryTestUtils.assertThrows(() -> Vector1D.normalize(Double.POSITIVE_INFINITY),
+        GeometryTestUtils.assertThrows(() -> Vector1D.Unit.from(Double.POSITIVE_INFINITY),
                 IllegalNormException.class);
     }
 


[commons-geometry] 02/06: Javadoc.

Posted by er...@apache.org.
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 8f863c4fff877e1c7aa9f7a8f19962488589b0fd
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Sep 27 13:27:25 2019 +0200

    Javadoc.
---
 .../java/org/apache/commons/geometry/euclidean/oned/Vector1D.java  | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
index a750879..7ff4a9d 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
@@ -374,8 +374,9 @@ public class Vector1D extends EuclideanVector<Vector1D> {
                 LinearCombination.value(a1, v1.x, a2, v2.x, a3, v3.x, a4, v4.x));
     }
 
-    /** Private class used to represent unit vectors. This allows optimizations to be performed for certain
-     * operations.
+    /**
+     * Represent unit vectors.
+     * This allows optimizations to be performed for certain operations.
      */
     public static final class Unit extends Vector1D {
         /** Unit vector (coordinates: 1). */
@@ -397,7 +398,7 @@ public class Vector1D extends EuclideanVector<Vector1D> {
         /**
          * Creates a normalized vector.
          *
-         * @param v Vector.
+         * @param v Vector coordinate.
          * @return a vector whose norm is 1.
          * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
          */


[commons-geometry] 04/06: GEOMETRY-61: Improvement of "normalize" functionality for class "Vector3D".

Posted by er...@apache.org.
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 a009c78f19acf0b6feb9473a2de3ec8513710bf7
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Sep 27 13:58:31 2019 +0200

    GEOMETRY-61: Improvement of "normalize" functionality for class "Vector3D".
    
    "Unit" class defines factory methods "from" to create a normalized instance.
---
 .../euclidean/threed/AffineTransformMatrix3D.java  |  2 +-
 .../geometry/euclidean/threed/Vector3D.java        | 85 ++++++++++++++--------
 .../threed/rotation/QuaternionRotation.java        |  2 +-
 .../geometry/euclidean/threed/PlaneTest.java       | 14 ++--
 .../geometry/euclidean/threed/Vector3DTest.java    | 12 +--
 5 files changed, 68 insertions(+), 47 deletions(-)

diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/AffineTransformMatrix3D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/AffineTransformMatrix3D.java
index cd25f7b..92d2b2a 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/AffineTransformMatrix3D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/AffineTransformMatrix3D.java
@@ -199,7 +199,7 @@ public final class AffineTransformMatrix3D implements AffineTransformMatrix<Vect
      */
     @Override
     public Vector3D applyDirection(final Vector3D vec) {
-        return applyVector(vec, Vector3D::normalize);
+        return applyVector(vec, Vector3D.Unit::from);
     }
 
     /** Apply a translation to the current instance, returning the result as a new transform.
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java
index e0f86f3..5aa4610 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java
@@ -34,22 +34,22 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
     public static final Vector3D ZERO   = new Vector3D(0, 0, 0);
 
     /** First canonical vector (coordinates: 1, 0, 0). */
-    public static final Vector3D PLUS_X = new UnitVector(1, 0, 0);
+    public static final Vector3D PLUS_X = Unit.PLUS_X;
 
     /** Opposite of the first canonical vector (coordinates: -1, 0, 0). */
-    public static final Vector3D MINUS_X = new UnitVector(-1, 0, 0);
+    public static final Vector3D MINUS_X = Unit.MINUS_X;
 
     /** Second canonical vector (coordinates: 0, 1, 0). */
-    public static final Vector3D PLUS_Y = new UnitVector(0, 1, 0);
+    public static final Vector3D PLUS_Y = Unit.PLUS_Y;
 
     /** Opposite of the second canonical vector (coordinates: 0, -1, 0). */
-    public static final Vector3D MINUS_Y = new UnitVector(0, -1, 0);
+    public static final Vector3D MINUS_Y = Unit.MINUS_Y;
 
     /** Third canonical vector (coordinates: 0, 0, 1). */
-    public static final Vector3D PLUS_Z = new UnitVector(0, 0, 1);
+    public static final Vector3D PLUS_Z = Unit.PLUS_Z;
 
     /** Opposite of the third canonical vector (coordinates: 0, 0, -1).  */
-    public static final Vector3D MINUS_Z = new UnitVector(0, 0, -1);
+    public static final Vector3D MINUS_Z = Unit.MINUS_Z;
 
     // CHECKSTYLE: stop ConstantName
     /** A vector with all coordinates set to NaN. */
@@ -149,10 +149,7 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
     /** {@inheritDoc} */
     @Override
     public Vector3D directionTo(Vector3D v) {
-        return normalize(
-                v.x - x,
-                v.y - y,
-                v.z - z);
+        return vectorTo(v).normalize();
     }
 
     /** {@inheritDoc} */
@@ -234,7 +231,7 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
     /** {@inheritDoc} */
     @Override
     public Vector3D normalize() {
-        return normalize(x, y, z);
+        return Unit.from(x, y, z);
     }
 
     /** {@inheritDoc} */
@@ -348,7 +345,7 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
     /** {@inheritDoc} */
     @Override
     public Vector3D orthogonal(Vector3D dir) {
-        return dir.getComponent(this, true, Vector3D::normalize);
+        return dir.getComponent(this, true, Vector3D.Unit::from);
     }
 
     /** Compute the cross-product of the instance with another vector.
@@ -493,20 +490,6 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
         return new Vector3D(v[0], v[1], v[2]);
     }
 
-    /** Returns a normalized vector derived from the given values.
-     * @param x abscissa (first coordinate value)
-     * @param y ordinate (second coordinate value)
-     * @param z height (third coordinate value)
-     * @return normalized vector instance
-     * @throws IllegalNormException if the norm of the given values is zero, NaN, or infinite
-     */
-    public static Vector3D normalize(final double x, final double y, final double z) {
-        final double norm = Vectors.checkedNorm(Vectors.norm(x, y, z));
-        final double invNorm = 1.0 / norm;
-
-        return new UnitVector(x * invNorm, y * invNorm, z * invNorm);
-    }
-
     /** Parses the given string and returns a new vector instance. The expected string
      * format is the same as that returned by {@link #toString()}.
      * @param str the string to parse
@@ -596,10 +579,23 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
                 LinearCombination.value(a1, v1.z, a2, v2.z, a3, v3.z, a4, v4.z));
     }
 
-    /** Private class used to represent unit vectors. This allows optimizations to be performed for certain
-     * operations.
+    /**
+     * Represents unit vectors.
+     * This allows optimizations to be performed for certain operations.
      */
-    private static final class UnitVector extends Vector3D {
+    public static final class Unit extends Vector3D {
+        /** Unit vector (coordinates: 1, 0, 0). */
+        static final Unit PLUS_X  = new Unit(1d, 0d, 0d);
+        /** Negation of unit vector (coordinates: -1, 0, 0). */
+        static final Unit MINUS_X = new Unit(-1d, 0d, 0d);
+        /** Unit vector (coordinates: 0, 1, 0). */
+        static final Unit PLUS_Y  = new Unit(0d, 1d, 0d);
+        /** Negation of unit vector (coordinates: 0, -1, 0). */
+        static final Unit MINUS_Y = new Unit(0d, -1d, 0d);
+        /** Unit vector (coordinates: 0, 0, 1). */
+        static final Unit PLUS_Z  = new Unit(0d, 0d, 1d);
+        /** Negation of unit vector (coordinates: 0, 0, -1). */
+        static final Unit MINUS_Z = new Unit(0d, 0d, -1d);
 
         /** Serializable version identifier */
         private static final long serialVersionUID = 20180903L;
@@ -610,10 +606,35 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
          * @param y ordinate (second coordinate value)
          * @param z height (third coordinate value)
          */
-        private UnitVector(final double x, final double y, final double z) {
+        private Unit(final double x, final double y, final double z) {
             super(x, y, z);
         }
 
+        /**
+         * Creates a normalized vector.
+         *
+         * @param x Vector coordinate.
+         * @param y Vector coordinate.
+         * @param z Vector coordinate.
+         * @return a vector whose norm is 1.
+         * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
+         */
+        public static Unit from(double x, double y, double z) {
+            final double invNorm = 1 / Vectors.checkedNorm(Vectors.norm(x, y, z));
+            return new Unit(x * invNorm, y * invNorm, z * invNorm);
+        }
+
+        /**
+         * Creates a normalized vector.
+         *
+         * @param v Vector.
+         * @return a vector whose norm is 1.
+         * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite
+         */
+        public static Unit from(Vector3D v) {
+            return from(v.getX(), v.getY(), v.getZ());
+        }
+
         /** {@inheritDoc} */
         @Override
         public double norm() {
@@ -640,8 +661,8 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
 
         /** {@inheritDoc} */
         @Override
-        public UnitVector negate() {
-            return new UnitVector(-getX(), -getY(), -getZ());
+        public Unit negate() {
+            return new Unit(-getX(), -getY(), -getZ());
         }
     }
 }
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 957ff56..2e67ce5 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
@@ -91,7 +91,7 @@ public final class QuaternionRotation implements Rotation3D, Serializable {
         // the most straightforward way to check if we have a normalizable
         // vector is to just try to normalize it and see if we fail
         try {
-            return Vector3D.normalize(quat.getX(), quat.getY(), quat.getZ());
+            return Vector3D.Unit.from(quat.getX(), quat.getY(), quat.getZ());
         }
         catch (IllegalNormException exc) {
             return Vector3D.PLUS_X;
diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PlaneTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PlaneTest.java
index af76826..9757ce3 100644
--- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PlaneTest.java
+++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PlaneTest.java
@@ -280,22 +280,22 @@ public class PlaneTest {
                 origin, Vector3D.MINUS_Z, Vector3D.PLUS_Y);
 
         checkPlane(Plane.fromPoints(rotate(pts, 2), TEST_PRECISION),
-                origin, Vector3D.normalize(0, 1, -1), Vector3D.normalize(0, 1, 1));
+                origin, Vector3D.Unit.from(0, 1, -1), Vector3D.Unit.from(0, 1, 1));
 
         checkPlane(Plane.fromPoints(rotate(pts, 3), TEST_PRECISION),
-                origin, Vector3D.normalize(0, 1, 1), Vector3D.normalize(0, -1, 1));
+                origin, Vector3D.Unit.from(0, 1, 1), Vector3D.Unit.from(0, -1, 1));
 
         checkPlane(Plane.fromPoints(rotate(pts, 4), TEST_PRECISION),
-                origin, Vector3D.normalize(0, -1, -0.5), Vector3D.normalize(0, 0.5, -1));
+                origin, Vector3D.Unit.from(0, -1, -0.5), Vector3D.Unit.from(0, 0.5, -1));
         checkPlane(Plane.fromPoints(rotate(pts, 5), TEST_PRECISION),
-                origin, Vector3D.normalize(0, -1, -0.5), Vector3D.normalize(0, 0.5, -1));
+                origin, Vector3D.Unit.from(0, -1, -0.5), Vector3D.Unit.from(0, 0.5, -1));
 
         checkPlane(Plane.fromPoints(rotate(pts, 6), TEST_PRECISION),
-                origin, Vector3D.normalize(0, -1, 0.5), Vector3D.normalize(0, -0.5, -1));
+                origin, Vector3D.Unit.from(0, -1, 0.5), Vector3D.Unit.from(0, -0.5, -1));
         checkPlane(Plane.fromPoints(rotate(pts, 7), TEST_PRECISION),
-                origin, Vector3D.normalize(0, -1, 0.5), Vector3D.normalize(0, -0.5, -1));
+                origin, Vector3D.Unit.from(0, -1, 0.5), Vector3D.Unit.from(0, -0.5, -1));
         checkPlane(Plane.fromPoints(rotate(pts, 8), TEST_PRECISION),
-                origin, Vector3D.normalize(0, -1, 0.5), Vector3D.normalize(0, -0.5, -1));
+                origin, Vector3D.Unit.from(0, -1, 0.5), Vector3D.Unit.from(0, -0.5, -1));
 
         checkPlane(Plane.fromPoints(rotate(pts, 9), TEST_PRECISION),
                 origin, Vector3D.PLUS_Z, Vector3D.MINUS_Y);
diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/Vector3DTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/Vector3DTest.java
index c4b5de3..f876dff 100644
--- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/Vector3DTest.java
+++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/Vector3DTest.java
@@ -1140,19 +1140,19 @@ public class Vector3DTest {
         double invSqrt3 = 1.0 / Math.sqrt(3.0);
 
         // act/assert
-        checkVector(Vector3D.normalize(2.0, -2.0, 2.0), invSqrt3, -invSqrt3, invSqrt3);
-        checkVector(Vector3D.normalize(-4.0, 4.0, -4.0), -invSqrt3, invSqrt3, -invSqrt3);
+        checkVector(Vector3D.Unit.from(2.0, -2.0, 2.0), invSqrt3, -invSqrt3, invSqrt3);
+        checkVector(Vector3D.Unit.from(-4.0, 4.0, -4.0), -invSqrt3, invSqrt3, -invSqrt3);
     }
 
     @Test
     public void testNormalize_static_illegalNorm() {
-        GeometryTestUtils.assertThrows(() -> Vector3D.normalize(0.0, 0.0, 0.0),
+        GeometryTestUtils.assertThrows(() -> Vector3D.Unit.from(0.0, 0.0, 0.0),
                 IllegalNormException.class);
-        GeometryTestUtils.assertThrows(() -> Vector3D.normalize(Double.NaN, 1.0, 1.0),
+        GeometryTestUtils.assertThrows(() -> Vector3D.Unit.from(Double.NaN, 1.0, 1.0),
                 IllegalNormException.class);
-        GeometryTestUtils.assertThrows(() -> Vector3D.normalize(1.0, Double.NEGATIVE_INFINITY, 1.0),
+        GeometryTestUtils.assertThrows(() -> Vector3D.Unit.from(1.0, Double.NEGATIVE_INFINITY, 1.0),
                 IllegalNormException.class);
-        GeometryTestUtils.assertThrows(() -> Vector3D.normalize(1.0, 1.0, Double.POSITIVE_INFINITY),
+        GeometryTestUtils.assertThrows(() -> Vector3D.Unit.from(1.0, 1.0, Double.POSITIVE_INFINITY),
                 IllegalNormException.class);
     }
 


[commons-geometry] 05/06: GEOMETRY-61: Advertize specific characteristics of returned instance.

Posted by er...@apache.org.
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 68e17fa9fbc9cb8f00da5ee0849d1e6f176f89c9
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Fri Sep 27 14:15:12 2019 +0200

    GEOMETRY-61: Advertize specific characteristics of returned instance.
---
 .../java/org/apache/commons/geometry/euclidean/oned/Vector1D.java   | 6 +++---
 .../java/org/apache/commons/geometry/euclidean/threed/Vector3D.java | 6 +++---
 .../java/org/apache/commons/geometry/euclidean/twod/Vector2D.java   | 6 +++---
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
index 7ff4a9d..827afde 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Vector1D.java
@@ -98,7 +98,7 @@ public class Vector1D extends EuclideanVector<Vector1D> {
 
     /** {@inheritDoc} */
     @Override
-    public Vector1D directionTo(Vector1D v) {
+    public Unit directionTo(Vector1D v) {
         return vectorTo(v).normalize();
     }
 
@@ -165,7 +165,7 @@ public class Vector1D extends EuclideanVector<Vector1D> {
 
     /** {@inheritDoc} */
     @Override
-    public Vector1D normalize() {
+    public Unit normalize() {
         return Unit.from(x);
     }
 
@@ -432,7 +432,7 @@ public class Vector1D extends EuclideanVector<Vector1D> {
 
         /** {@inheritDoc} */
         @Override
-        public Vector1D normalize() {
+        public Unit normalize() {
             return this;
         }
 
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java
index 5aa4610..dc73b4c 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java
@@ -148,7 +148,7 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
 
     /** {@inheritDoc} */
     @Override
-    public Vector3D directionTo(Vector3D v) {
+    public Unit directionTo(Vector3D v) {
         return vectorTo(v).normalize();
     }
 
@@ -230,7 +230,7 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
 
     /** {@inheritDoc} */
     @Override
-    public Vector3D normalize() {
+    public Unit normalize() {
         return Unit.from(x, y, z);
     }
 
@@ -649,7 +649,7 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
 
         /** {@inheritDoc} */
         @Override
-        public Vector3D normalize() {
+        public Unit normalize() {
             return this;
         }
 
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java
index 85dac32..0daf927 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/Vector2D.java
@@ -122,7 +122,7 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
 
     /** {@inheritDoc} */
     @Override
-    public Vector2D directionTo(Vector2D v) {
+    public Unit directionTo(Vector2D v) {
         return vectorTo(v).normalize();
     }
 
@@ -193,7 +193,7 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
 
     /** {@inheritDoc} */
     @Override
-    public Vector2D normalize() {
+    public Unit normalize() {
         return Unit.from(x, y);
     }
 
@@ -579,7 +579,7 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
 
         /** {@inheritDoc} */
         @Override
-        public Vector2D normalize() {
+        public Unit normalize() {
             return this;
         }