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 03e4ff45785d9a2ec9e6982a5da99c5dcf3411c2
Author: Matt Juntunen <[email protected]>
AuthorDate: Sun Jul 29 15:10:22 2018 -0400

    GEOMETRY-9: moving magnitude methods to main Vector interface; simplifying 
1D magnitude/normalize methods
---
 .../org/apache/commons/geometry/core/Vector.java   | 23 +++++++++++
 .../geometry/euclidean/EuclideanVector.java        | 23 -----------
 .../commons/geometry/euclidean/oned/Point1D.java   |  3 ++
 .../commons/geometry/euclidean/oned/Vector1D.java  | 48 ++++++++++++----------
 .../geometry/euclidean/threed/Vector3D.java        | 10 ++---
 .../commons/geometry/euclidean/twod/Vector2D.java  |  8 ++--
 .../geometry/euclidean/oned/Point1DTest.java       |  1 +
 .../geometry/euclidean/oned/Vector1DTest.java      |  1 +
 8 files changed, 63 insertions(+), 54 deletions(-)

diff --git 
a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/Vector.java
 
b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/Vector.java
index 03bda8f..618757a 100644
--- 
a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/Vector.java
+++ 
b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/Vector.java
@@ -63,6 +63,29 @@ public interface Vector<V extends Vector<V>> extends Spatial 
{
      */
     double getNormInf();
 
+    /** Returns the magnitude (i.e. length) of the vector. This is
+     * the same value as returned by {@link #getNorm()}.
+     * @return the magnitude, or length, of the vector
+     * @see #getNorm()
+     */
+    double getMagnitude();
+
+    /** Returns the squared magnitude of the vector. This is the
+     * same value as returned by {@link #getNormSq()}.
+     * @return the squared magnitude of the vector
+     * @see #getMagnitude()
+     * @see #getNormSq()
+     */
+    double getMagnitudeSq();
+
+    /** Returns a vector with the same direction but with the given
+     * magnitude. This is equivalent to calling {@code 
vec.normalize().scalarMultiply(mag)}
+     * but without the intermediate vector.
+     * @param magnitude The vector magnitude
+     * @return a vector with the same direction as the current instance but 
the given magnitude
+     */
+    V withMagnitude(double magnitude);
+
     /** Add a vector to the instance.
      * @param v vector to add
      * @return a new vector
diff --git 
a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/EuclideanVector.java
 
b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/EuclideanVector.java
index 3d8874b..0622c89 100644
--- 
a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/EuclideanVector.java
+++ 
b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/EuclideanVector.java
@@ -32,27 +32,4 @@ public interface EuclideanVector<P extends EuclideanPoint<P, 
V>, V extends Eucli
      * @return point with the same coordinates as this vector
      */
     P asPoint();
-
-    /** Returns the magnitude (i.e. length) of the vector. This is
-     * the same value as returned by {@link #getNorm()}.
-     * @return the magnitude, or length, of the vector
-     * @see #getNorm()
-     */
-    double getMagnitude();
-
-    /** Returns the squared magnitude of the vector. This is the
-     * same value as returned by {@link #getNormSq()}.
-     * @return the squared magnitude of the vector
-     * @see #getMagnitude()
-     * @see #getNormSq()
-     */
-    double getMagnitudeSq();
-
-    /** Returns a vector with the same direction but with the given
-     * magnitude. This is equivalent to calling {@code 
vec.normalize().scalarMultiply(mag)}
-     * but without the intermediate vector.
-     * @param magnitude The vector magnitude
-     * @return a vector with the same direction as the current instance but 
the given magnitude
-     */
-    V withMagnitude(double magnitude);
 }
diff --git 
a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Point1D.java
 
b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Point1D.java
index 1e6603e..af324c1 100644
--- 
a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Point1D.java
+++ 
b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/oned/Point1D.java
@@ -32,6 +32,9 @@ public final class Point1D extends Cartesian1D implements 
EuclideanPoint<Point1D
     /** Unit (coordinates: 1). */
     public static final Point1D ONE  = new Point1D(1.0);
 
+    /** Negative unit (coordinates: 1). */
+    public static final Point1D MINUS_ONE  = new Point1D(-1.0);
+
     // CHECKSTYLE: stop ConstantName
     /** A vector with all coordinates set to NaN. */
     public static final Point1D NaN = new Point1D(Double.NaN);
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 8ed1194..207f949 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,6 +33,9 @@ public final class Vector1D extends Cartesian1D implements 
EuclideanVector<Point
     /** Unit vector (coordinates: 1). */
     public static final Vector1D ONE  = new Vector1D(1.0);
 
+    /** Negation of unit vector (coordinates: -1). */
+    public static final Vector1D MINUS_ONE = new Vector1D(-1.0);
+
     // CHECKSTYLE: stop ConstantName
     /** A vector with all coordinates set to NaN. */
     public static final Vector1D NaN = new Vector1D(Double.NaN);
@@ -49,6 +52,9 @@ public final class Vector1D extends Cartesian1D implements 
EuclideanVector<Point
     /** Serializable UID. */
     private static final long serialVersionUID = 20180710L;
 
+    /** Error message when a norm is zero. */
+    private static final String ZERO_NORM_MSG = "Norm is zero";
+
     /** Factory for delegating instance creation. */
     private static DoubleFunction1N<Vector1D> FACTORY = new 
DoubleFunction1N<Vector1D>() {
 
@@ -117,9 +123,14 @@ public final class Vector1D extends Cartesian1D implements 
EuclideanVector<Point
     /** {@inheritDoc} */
     @Override
     public Vector1D withMagnitude(double magnitude) {
-        final double invNorm = 1.0 / nonZeroNorm();
-
-        return new Vector1D(magnitude * getX() * invNorm);
+        final double x = getX();
+        if (x > 0.0) {
+            return new Vector1D(magnitude);
+        }
+        else if (x < 0.0) {
+            return new Vector1D(-magnitude);
+        }
+        throw new IllegalStateException(ZERO_NORM_MSG);
     }
 
     /** {@inheritDoc} */
@@ -155,7 +166,14 @@ public final class Vector1D extends Cartesian1D implements 
EuclideanVector<Point
     /** {@inheritDoc} */
     @Override
     public Vector1D normalize() throws IllegalStateException {
-        return scalarMultiply(1.0 / nonZeroNorm());
+        final double x = getX();
+        if (x > 0.0) {
+            return ONE;
+        }
+        else if (x < 0.0) {
+            return MINUS_ONE;
+        }
+        throw new IllegalStateException(ZERO_NORM_MSG);
     }
 
     /** {@inheritDoc} */
@@ -167,26 +185,25 @@ public final class Vector1D extends Cartesian1D 
implements EuclideanVector<Point
     /** {@inheritDoc} */
     @Override
     public double distance1(Vector1D v) {
-        return distance(v);
+        return Vectors.norm1(getX() - v.getX());
     }
 
     /** {@inheritDoc} */
     @Override
     public double distance(Vector1D v) {
-        return Math.abs(v.getX() - getX());
+        return Vectors.norm(getX() - v.getX());
     }
 
     /** {@inheritDoc} */
     @Override
     public double distanceInf(Vector1D v) {
-        return distance(v);
+        return Vectors.normInf(getX() - v.getX());
     }
 
     /** {@inheritDoc} */
     @Override
     public double distanceSq(Vector1D v) {
-        final double dx = v.getX() - getX();
-        return dx * dx;
+        return Vectors.normSq(getX() - v.getX());
     }
 
     /** {@inheritDoc} */
@@ -246,19 +263,6 @@ public final class Vector1D extends Cartesian1D implements 
EuclideanVector<Point
         return false;
     }
 
-    /** Returns the vector norm, throwing an IllegalStateException if the norm 
is zero.
-     * @return the non-zero norm value
-     * @throws IllegalStateException if the norm is zero
-     */
-    private double nonZeroNorm() throws IllegalStateException {
-        final double n = getNorm();
-        if (n == 0) {
-            throw new IllegalStateException("Norm is zero");
-        }
-
-        return n;
-    }
-
     /** Returns a vector with the given coordinate value.
      * @param x vector coordinate
      * @return vector instance
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 50fee39..cac3b23 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
@@ -135,7 +135,7 @@ public final class Vector3D extends Cartesian3D implements 
EuclideanVector<Point
     /** {@inheritDoc} */
     @Override
     public Vector3D withMagnitude(double magnitude) {
-        final double invNorm = 1.0 / nonZeroNorm();
+        final double invNorm = 1.0 / getNonZeroNorm();
 
         return new Vector3D(
                     magnitude * getX() * invNorm,
@@ -193,7 +193,7 @@ public final class Vector3D extends Cartesian3D implements 
EuclideanVector<Point
     /** {@inheritDoc} */
     @Override
     public Vector3D normalize() throws IllegalStateException {
-        return scalarMultiply(1 / nonZeroNorm());
+        return scalarMultiply(1.0 / getNonZeroNorm());
     }
 
     /** Get a vector orthogonal to the instance.
@@ -212,7 +212,7 @@ public final class Vector3D extends Cartesian3D implements 
EuclideanVector<Point
      * @exception IllegalStateException if the norm of the instance is zero
      */
     public Vector3D orthogonal() throws IllegalStateException {
-        double threshold = 0.6 * nonZeroNorm();
+        double threshold = 0.6 * getNonZeroNorm();
 
         final double x = getX();
         final double y = getY();
@@ -240,7 +240,7 @@ public final class Vector3D extends Cartesian3D implements 
EuclideanVector<Point
      * @exception IllegalStateException if either vector has a zero norm
      */
     public double angle(Vector3D v) throws IllegalStateException {
-        double normProduct = nonZeroNorm() * v.nonZeroNorm();
+        double normProduct = getNonZeroNorm() * v.getNonZeroNorm();
 
         double dot = dotProduct(v);
         double threshold = normProduct * 0.9999;
@@ -380,7 +380,7 @@ public final class Vector3D extends Cartesian3D implements 
EuclideanVector<Point
      * @return the non-zero norm value
      * @throws IllegalStateException if the norm is zero
      */
-    private double nonZeroNorm() throws IllegalStateException {
+    private double getNonZeroNorm() throws IllegalStateException {
         final double n = getNorm();
         if (n == 0) {
             throw new IllegalStateException("Norm is zero");
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 a81dcd7..52a16f3 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
@@ -135,7 +135,7 @@ public final class Vector2D extends Cartesian2D implements 
EuclideanVector<Point
     /** {@inheritDoc} */
     @Override
     public Vector2D withMagnitude(double magnitude) {
-        final double invNorm = 1.0 / nonZeroNorm();
+        final double invNorm = 1.0 / getNonZeroNorm();
 
         return new Vector2D(
                     magnitude * getX() * invNorm,
@@ -176,7 +176,7 @@ public final class Vector2D extends Cartesian2D implements 
EuclideanVector<Point
     /** {@inheritDoc} */
     @Override
     public Vector2D normalize() throws IllegalStateException {
-        return scalarMultiply(1.0 / nonZeroNorm());
+        return scalarMultiply(1.0 / getNonZeroNorm());
     }
 
     /** {@inheritDoc} */
@@ -228,7 +228,7 @@ public final class Vector2D extends Cartesian2D implements 
EuclideanVector<Point
      * @exception IllegalStateException if either vector has a zero norm
      */
     public double angle(Vector2D v) throws IllegalArgumentException {
-        double normProduct = nonZeroNorm() * v.nonZeroNorm();
+        double normProduct = getNonZeroNorm() * v.getNonZeroNorm();
 
         double dot = dotProduct(v);
         double threshold = normProduct * 0.9999;
@@ -328,7 +328,7 @@ public final class Vector2D extends Cartesian2D implements 
EuclideanVector<Point
      * @return the non-zero norm value
      * @throws IllegalStateException if the norm is zero
      */
-    private double nonZeroNorm() throws IllegalStateException {
+    private double getNonZeroNorm() throws IllegalStateException {
         final double n = getNorm();
         if (n == 0) {
             throw new IllegalStateException("Norm is zero");
diff --git 
a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Point1DTest.java
 
b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Point1DTest.java
index 4b65983..8233d1c 100644
--- 
a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Point1DTest.java
+++ 
b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/oned/Point1DTest.java
@@ -32,6 +32,7 @@ public class Point1DTest {
         // act/assert
         checkPoint(Point1D.ZERO, 0.0);
         checkPoint(Point1D.ONE, 1.0);
+        checkPoint(Point1D.MINUS_ONE, -1.0);
         checkPoint(Point1D.NaN, Double.NaN);
         checkPoint(Point1D.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
         checkPoint(Point1D.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
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 cce8abe..2ac2cc7 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
@@ -31,6 +31,7 @@ public class Vector1DTest {
         // act/assert
         checkVector(Vector1D.ZERO, 0.0);
         checkVector(Vector1D.ONE, 1.0);
+        checkVector(Vector1D.MINUS_ONE, -1.0);
         checkVector(Vector1D.NaN, Double.NaN);
         checkVector(Vector1D.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
         checkVector(Vector1D.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);

Reply via email to