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 <[email protected]> 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); }
