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 8cefbe8bca10d14502e9b7baf0f6685ffa667029 Author: Matt Juntunen <matt.juntu...@hotmail.com> AuthorDate: Mon Sep 17 00:41:33 2018 -0400 GEOMETRY-17: adding public EuclideanVector#getRealNonZeroNorm() method --- .../geometry/euclidean/EuclideanVector.java | 9 +++++ .../geometry/euclidean/internal/Vectors.java | 12 +++---- .../commons/geometry/euclidean/oned/Vector1D.java | 19 +++++++---- .../commons/geometry/euclidean/threed/Plane.java | 6 ++-- .../geometry/euclidean/threed/Rotation.java | 11 ++----- .../geometry/euclidean/threed/Vector3D.java | 25 +++++++------- .../commons/geometry/euclidean/twod/Vector2D.java | 23 ++++++------- .../geometry/euclidean/internal/VectorsTest.java | 38 +++++++++++----------- .../geometry/euclidean/oned/Vector1DTest.java | 21 ++++++++++++ .../geometry/euclidean/threed/Vector3DTest.java | 21 ++++++++++++ .../geometry/euclidean/twod/Vector2DTest.java | 21 ++++++++++++ 11 files changed, 136 insertions(+), 70 deletions(-) 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 ad4f21b..acac512 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 @@ -17,6 +17,7 @@ package org.apache.commons.geometry.euclidean; import org.apache.commons.geometry.core.Vector; +import org.apache.commons.geometry.core.exception.IllegalNormException; /** Represents a vector in a Euclidean space of any dimension. * @@ -44,4 +45,12 @@ public interface EuclideanVector<P extends EuclideanPoint<P, V>, V extends Eucli * @return interpolated or extrapolated vector */ V lerp(V v, double t); + + /** Returns the vector norm value, throwing an {@link IllegalNormException} if the value + * is not real (ie, NaN or infinite) or zero. Obtaining a vector norm value and ensuring + * that it meets this criteria is such a common operation that it is given its own method. + * @return the vector norm value, guaranteed to be real and non-zero + * @throws IllegalNormException if the vector norm is zero, NaN, or infinite + */ + double getRealNonZeroNorm(); } diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/internal/Vectors.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/internal/Vectors.java index 31ec162..1cd456f 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/internal/Vectors.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/internal/Vectors.java @@ -25,27 +25,27 @@ public final class Vectors { /** Private constructor. */ private Vectors() {} - /** Returns true if the given value is finite (ie, not NaN or inifinite) + /** Returns true if the given value is real (ie, not NaN or inifinite) * and not equal to zero. * @param value the value to test * @return true if {@code value} is not NaN, infinite, or zero; otherwise * false */ - public static boolean isFiniteNonZero(final double value) { + public static boolean isRealNonZero(final double value) { return Double.isFinite(value) && value != 0.0; } - /** Throws an {@link IllegalNormException} if the given norm value - * is not finite (ie, NaN or infinite) or zero. The argument is returned + /** Throws an {@link IllegalNormException} if the given norm value + * is not real (ie, not NaN or infinite) or zero. The argument is returned * to allow this method to be called inline. * @param norm vector norm value * @return the validated norm value * @throws IllegalNormException if the given norm value is NaN, infinite, * or zero */ - public static double ensureFiniteNonZeroNorm(final double norm) throws IllegalNormException { - if (!isFiniteNonZero(norm)) { + public static double ensureRealNonZeroNorm(final double norm) throws IllegalNormException { + if (!isRealNonZero(norm)) { throw new IllegalNormException(norm); } 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 393f0a3..dccf46a 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 @@ -105,13 +105,19 @@ public class Vector1D extends Cartesian1D implements EuclideanVector<Point1D, Ve /** {@inheritDoc} */ @Override public Vector1D withNorm(double magnitude) { - Vectors.ensureFiniteNonZeroNorm(getNorm()); + getRealNonZeroNorm(); // validate our norm value return (getX() > 0.0)? new Vector1D(magnitude) : new Vector1D(-magnitude); } /** {@inheritDoc} */ @Override + public double getRealNonZeroNorm() { + return Vectors.ensureRealNonZeroNorm(getNorm()); + } + + /** {@inheritDoc} */ + @Override public Vector1D add(Vector1D v) { return new Vector1D(getX() + v.getX()); } @@ -187,7 +193,7 @@ public class Vector1D extends Cartesian1D implements EuclideanVector<Point1D, Ve */ @Override public Vector1D project(final Vector1D base) { - Vectors.ensureFiniteNonZeroNorm(base.getNorm()); + base.getRealNonZeroNorm(); // validate the base norm return this; } @@ -197,7 +203,7 @@ public class Vector1D extends Cartesian1D implements EuclideanVector<Point1D, Ve */ @Override public Vector1D reject(final Vector1D base) { - Vectors.ensureFiniteNonZeroNorm(base.getNorm()); + base.getRealNonZeroNorm(); // validate the base norm return Vector1D.ZERO; } @@ -208,8 +214,9 @@ public class Vector1D extends Cartesian1D implements EuclideanVector<Point1D, Ve */ @Override public double angle(final Vector1D v) { - Vectors.ensureFiniteNonZeroNorm(getNorm()); - Vectors.ensureFiniteNonZeroNorm(v.getNorm()); + // validate the norm values + getRealNonZeroNorm(); + v.getRealNonZeroNorm(); final double sig1 = Math.signum(getX()); final double sig2 = Math.signum(v.getX()); @@ -283,7 +290,7 @@ public class Vector1D extends Cartesian1D implements EuclideanVector<Point1D, Ve * @throws IllegalNormException if the norm of the given value is zero, NaN, or infinite */ public static Vector1D normalize(final double x) { - Vectors.ensureFiniteNonZeroNorm(Vectors.norm(x)); + Vectors.ensureRealNonZeroNorm(Vectors.norm(x)); return (x > 0.0) ? ONE : MINUS_ONE; } diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Plane.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Plane.java index 424fbb3..1313c90 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Plane.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Plane.java @@ -16,9 +16,9 @@ */ package org.apache.commons.geometry.euclidean.threed; +import org.apache.commons.geometry.core.exception.IllegalNormException; import org.apache.commons.geometry.core.partitioning.Embedding; import org.apache.commons.geometry.core.partitioning.Hyperplane; -import org.apache.commons.geometry.euclidean.internal.Vectors; import org.apache.commons.geometry.euclidean.oned.Point1D; import org.apache.commons.geometry.euclidean.twod.Point2D; import org.apache.commons.geometry.euclidean.twod.PolygonsSet; @@ -139,10 +139,10 @@ public class Plane implements Hyperplane<Point3D>, Embedding<Point3D, Point2D> { /** Set the normal vactor. * @param normal normal direction to the plane (will be copied) - * @exception IllegalArgumentException if the normal norm is too close to zero + * @exception IllegalNormException if the normal norm zero, NaN, or infinite */ private void setNormal(final Vector3D normal) { - final double norm = Vectors.ensureFiniteNonZeroNorm(normal.getNorm()); + final double norm = normal.getRealNonZeroNorm(); w = Vector3D.linearCombination(1.0 / norm, normal); } diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Rotation.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Rotation.java index 466aedc..285b34a 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Rotation.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Rotation.java @@ -21,7 +21,6 @@ import java.io.Serializable; import org.apache.commons.geometry.core.exception.GeometryException; import org.apache.commons.geometry.core.exception.IllegalNormException; -import org.apache.commons.geometry.euclidean.internal.Vectors; import org.apache.commons.numbers.arrays.LinearCombination; /** @@ -173,10 +172,7 @@ public class Rotation implements Serializable { public Rotation(final Vector3D axis, final double angle, final RotationConvention convention) throws IllegalNormException { - double norm = axis.getNorm(); - if (!Vectors.isFiniteNonZero(norm)) { - throw new IllegalNormException("Illegal norm for rotation axis: " + norm); - } + double norm = axis.getRealNonZeroNorm(); double halfAngle = convention == RotationConvention.VECTOR_OPERATOR ? -0.5 * angle : +0.5 * angle; double coeff = Math.sin(halfAngle) / norm; @@ -320,10 +316,7 @@ public class Rotation implements Serializable { */ public Rotation(Vector3D u, Vector3D v) { - double normProduct = u.getNorm() * v.getNorm(); - if (!Vectors.isFiniteNonZero(normProduct)) { - throw new IllegalNormException("Illegal norm for rotation defining vector: " + normProduct); - } + double normProduct = u.getRealNonZeroNorm() * v.getRealNonZeroNorm(); double dot = u.dotProduct(v); 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 5dfea76..9e81e61 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 @@ -121,7 +121,7 @@ public class Vector3D extends Cartesian3D implements EuclideanVector<Point3D, Ve /** {@inheritDoc} */ @Override public Vector3D withNorm(double magnitude) { - final double invNorm = 1.0 / getFiniteNonZeroNorm(); + final double invNorm = 1.0 / getRealNonZeroNorm(); return new Vector3D( magnitude * getX() * invNorm, @@ -132,6 +132,12 @@ public class Vector3D extends Cartesian3D implements EuclideanVector<Point3D, Ve /** {@inheritDoc} */ @Override + public double getRealNonZeroNorm() { + return Vectors.ensureRealNonZeroNorm(getNorm()); + } + + /** {@inheritDoc} */ + @Override public Vector3D add(Vector3D v) { return new Vector3D( getX() + v.getX(), @@ -199,7 +205,7 @@ public class Vector3D extends Cartesian3D implements EuclideanVector<Point3D, Ve * or infinite */ public Vector3D orthogonal() { - double threshold = 0.6 * getFiniteNonZeroNorm(); + double threshold = 0.6 * getRealNonZeroNorm(); final double x = getX(); final double y = getY(); @@ -238,7 +244,7 @@ public class Vector3D extends Cartesian3D implements EuclideanVector<Point3D, Ve */ @Override public double angle(Vector3D v) { - double normProduct = getFiniteNonZeroNorm() * v.getFiniteNonZeroNorm(); + double normProduct = getRealNonZeroNorm() * v.getRealNonZeroNorm(); double dot = dotProduct(v); double threshold = normProduct * 0.9999; @@ -386,15 +392,6 @@ public class Vector3D extends Cartesian3D implements EuclideanVector<Point3D, Ve return false; } - /** Returns the vector norm, throwing an IllegalNormException if the norm is zero, - * NaN, or infinite. - * @return the finite, non-zero norm value - * @throws IllegalNormException if the norm is zero, NaN, or infinite - */ - private double getFiniteNonZeroNorm() { - return Vectors.ensureFiniteNonZeroNorm(getNorm()); - } - /** Returns a component of the current instance relative to the given base * vector. If {@code reject} is true, the vector rejection is returned; otherwise, * the projection is returned. @@ -416,7 +413,7 @@ public class Vector3D extends Cartesian3D implements EuclideanVector<Point3D, Ve // directly. This will produce the same error result as checking the actual norm since // Math.sqrt(0.0) == 0.0, Math.sqrt(Double.NaN) == Double.NaN and // Math.sqrt(Double.POSITIVE_INFINITY) == Double.POSITIVE_INFINITY. - final double baseMagSq = Vectors.ensureFiniteNonZeroNorm(base.getNormSq()); + final double baseMagSq = Vectors.ensureRealNonZeroNorm(base.getNormSq()); final double scale = aDotB / baseMagSq; @@ -472,7 +469,7 @@ public class Vector3D extends Cartesian3D implements EuclideanVector<Point3D, Ve * @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.ensureFiniteNonZeroNorm(Vectors.norm(x, y, z)); + final double norm = Vectors.ensureRealNonZeroNorm(Vectors.norm(x, y, z)); final double invNorm = 1.0 / norm; return new UnitVector(x * invNorm, y * invNorm, z * invNorm); 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 4b4b903..6f78508 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 @@ -111,7 +111,7 @@ public class Vector2D extends Cartesian2D implements EuclideanVector<Point2D, Ve /** {@inheritDoc} */ @Override public Vector2D withNorm(double magnitude) { - final double invNorm = 1.0 / getFiniteNonZeroNorm(); + final double invNorm = 1.0 / getRealNonZeroNorm(); return new Vector2D( magnitude * getX() * invNorm, @@ -121,6 +121,12 @@ public class Vector2D extends Cartesian2D implements EuclideanVector<Point2D, Ve /** {@inheritDoc} */ @Override + public double getRealNonZeroNorm() { + return Vectors.ensureRealNonZeroNorm(getNorm()); + } + + /** {@inheritDoc} */ + @Override public Vector2D add(Vector2D v) { return new Vector2D(getX() + v.getX(), getY() + v.getY()); } @@ -212,7 +218,7 @@ public class Vector2D extends Cartesian2D implements EuclideanVector<Point2D, Ve */ @Override public double angle(Vector2D v) { - double normProduct = getFiniteNonZeroNorm() * v.getFiniteNonZeroNorm(); + double normProduct = getRealNonZeroNorm() * v.getRealNonZeroNorm(); double dot = dotProduct(v); double threshold = normProduct * 0.9999; @@ -308,15 +314,6 @@ public class Vector2D extends Cartesian2D implements EuclideanVector<Point2D, Ve return false; } - /** Returns the vector norm, throwing an IllegalNormException if the norm is zero, - * NaN, or infinite. - * @return the finite, non-zero norm value - * @throws IllegalNormException if the norm is zero, NaN, or infinite - */ - private double getFiniteNonZeroNorm() { - return Vectors.ensureFiniteNonZeroNorm(getNorm()); - } - /** Returns a component of the current instance relative to the given base * vector. If {@code reject} is true, the vector rejection is returned; otherwise, * the projection is returned. @@ -331,7 +328,7 @@ public class Vector2D extends Cartesian2D implements EuclideanVector<Point2D, Ve private Vector2D getComponent(Vector2D base, boolean reject) { final double aDotB = dotProduct(base); - final double baseMag = Vectors.ensureFiniteNonZeroNorm(base.getNorm()); + final double baseMag = Vectors.ensureRealNonZeroNorm(base.getNorm()); final double scale = aDotB / (baseMag * baseMag); @@ -382,7 +379,7 @@ public class Vector2D extends Cartesian2D implements EuclideanVector<Point2D, Ve * @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.ensureFiniteNonZeroNorm(Vectors.norm(x, y)); + final double norm = Vectors.ensureRealNonZeroNorm(Vectors.norm(x, y)); final double invNorm = 1.0 / norm; return new UnitVector(x * invNorm, y * invNorm); diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/internal/VectorsTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/internal/VectorsTest.java index 2d38b03..bf2c745 100644 --- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/internal/VectorsTest.java +++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/internal/VectorsTest.java @@ -29,35 +29,35 @@ public class VectorsTest { @Test public void testIsFiniteNonZero() { // act/assert - Assert.assertTrue(Vectors.isFiniteNonZero(1e-20)); - Assert.assertTrue(Vectors.isFiniteNonZero(1e20)); - Assert.assertTrue(Vectors.isFiniteNonZero(-1e-20)); - Assert.assertTrue(Vectors.isFiniteNonZero(-1e20)); - - Assert.assertFalse(Vectors.isFiniteNonZero(0.0)); - Assert.assertFalse(Vectors.isFiniteNonZero(Double.NaN)); - Assert.assertFalse(Vectors.isFiniteNonZero(Double.POSITIVE_INFINITY)); - Assert.assertFalse(Vectors.isFiniteNonZero(Double.NEGATIVE_INFINITY)); + Assert.assertTrue(Vectors.isRealNonZero(1e-20)); + Assert.assertTrue(Vectors.isRealNonZero(1e20)); + Assert.assertTrue(Vectors.isRealNonZero(-1e-20)); + Assert.assertTrue(Vectors.isRealNonZero(-1e20)); + + Assert.assertFalse(Vectors.isRealNonZero(0.0)); + Assert.assertFalse(Vectors.isRealNonZero(Double.NaN)); + Assert.assertFalse(Vectors.isRealNonZero(Double.POSITIVE_INFINITY)); + Assert.assertFalse(Vectors.isRealNonZero(Double.NEGATIVE_INFINITY)); } @Test public void testEnsureFiniteNonZeroNorm() { // act/assert - Assert.assertEquals(1.0, Vectors.ensureFiniteNonZeroNorm(1.0), EPS); - Assert.assertEquals(23.12, Vectors.ensureFiniteNonZeroNorm(23.12), EPS); - Assert.assertEquals(2e-12, Vectors.ensureFiniteNonZeroNorm(2e-12), EPS); + Assert.assertEquals(1.0, Vectors.ensureRealNonZeroNorm(1.0), EPS); + Assert.assertEquals(23.12, Vectors.ensureRealNonZeroNorm(23.12), EPS); + Assert.assertEquals(2e-12, Vectors.ensureRealNonZeroNorm(2e-12), EPS); - Assert.assertEquals(-1.0, Vectors.ensureFiniteNonZeroNorm(-1.0), EPS); - Assert.assertEquals(-23.12, Vectors.ensureFiniteNonZeroNorm(-23.12), EPS); - Assert.assertEquals(-2e-12, Vectors.ensureFiniteNonZeroNorm(-2e-12), EPS); + Assert.assertEquals(-1.0, Vectors.ensureRealNonZeroNorm(-1.0), EPS); + Assert.assertEquals(-23.12, Vectors.ensureRealNonZeroNorm(-23.12), EPS); + Assert.assertEquals(-2e-12, Vectors.ensureRealNonZeroNorm(-2e-12), EPS); - GeometryTestUtils.assertThrows(() -> Vectors.ensureFiniteNonZeroNorm(0.0), + GeometryTestUtils.assertThrows(() -> Vectors.ensureRealNonZeroNorm(0.0), IllegalNormException.class, "Illegal norm: 0.0"); - GeometryTestUtils.assertThrows(() -> Vectors.ensureFiniteNonZeroNorm(Double.NaN), + GeometryTestUtils.assertThrows(() -> Vectors.ensureRealNonZeroNorm(Double.NaN), IllegalNormException.class, "Illegal norm: NaN"); - GeometryTestUtils.assertThrows(() -> Vectors.ensureFiniteNonZeroNorm(Double.POSITIVE_INFINITY), + GeometryTestUtils.assertThrows(() -> Vectors.ensureRealNonZeroNorm(Double.POSITIVE_INFINITY), IllegalNormException.class, "Illegal norm: Infinity"); - GeometryTestUtils.assertThrows(() -> Vectors.ensureFiniteNonZeroNorm(Double.NEGATIVE_INFINITY), + GeometryTestUtils.assertThrows(() -> Vectors.ensureRealNonZeroNorm(Double.NEGATIVE_INFINITY), IllegalNormException.class, "Illegal norm: -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 8e1b797..048909e 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 @@ -150,6 +150,27 @@ public class Vector1DTest { } @Test + public void testGetRealNonZeroNorm() { + // act/assert + Assert.assertEquals(1.0, Vector1D.ONE.getNorm(), TEST_TOLERANCE); + Assert.assertEquals(3.0, Vector1D.of(3).getNorm(), TEST_TOLERANCE); + Assert.assertEquals(3.0, Vector1D.of(-3).getNorm(), TEST_TOLERANCE); + } + + @Test + public void testGetRealNonZeroNorm_illegalNorm() { + // act/assert + GeometryTestUtils.assertThrows(() -> Vector1D.ZERO.getRealNonZeroNorm(), + IllegalNormException.class); + GeometryTestUtils.assertThrows(() -> Vector1D.NaN.getRealNonZeroNorm(), + IllegalNormException.class); + GeometryTestUtils.assertThrows(() -> Vector1D.POSITIVE_INFINITY.getRealNonZeroNorm(), + IllegalNormException.class); + GeometryTestUtils.assertThrows(() -> Vector1D.NEGATIVE_INFINITY.getRealNonZeroNorm(), + IllegalNormException.class); + } + + @Test public void testAdd() { // arrange Vector1D v1 = Vector1D.of(1); 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 0997f57..5cee125 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 @@ -192,6 +192,27 @@ public class Vector3DTest { } @Test + public void testGetRealNonZeroNorm() { + // act/assert + Assert.assertEquals(1.0, Vector3D.PLUS_X.getNorm(), EPS); + Assert.assertEquals(Math.sqrt(50), Vector3D.of(3, 4, 5).getNorm(), EPS); + Assert.assertEquals(Math.sqrt(50), Vector3D.of(-3, -4, -5).getNorm(), EPS); + } + + @Test + public void testGetRealNonZeroNorm_illegalNorm() { + // act/assert + GeometryTestUtils.assertThrows(() -> Vector3D.ZERO.getRealNonZeroNorm(), + IllegalNormException.class); + GeometryTestUtils.assertThrows(() -> Vector3D.NaN.getRealNonZeroNorm(), + IllegalNormException.class); + GeometryTestUtils.assertThrows(() -> Vector3D.POSITIVE_INFINITY.getRealNonZeroNorm(), + IllegalNormException.class); + GeometryTestUtils.assertThrows(() -> Vector3D.NEGATIVE_INFINITY.getRealNonZeroNorm(), + IllegalNormException.class); + } + + @Test public void testAdd() { // arrange Vector3D v1 = Vector3D.of(1, 2, 3); 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 183756f..f46fc74 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 @@ -170,6 +170,27 @@ public class Vector2DTest { } @Test + public void testGetRealNonZeroNorm() { + // act/assert + Assert.assertEquals(1.0, Vector2D.PLUS_X.getNorm(), EPS); + Assert.assertEquals(5.0, Vector2D.of(3, 4).getNorm(), EPS); + Assert.assertEquals(5.0, Vector2D.of(-3, -4).getNorm(), EPS); + } + + @Test + public void testGetRealNonZeroNorm_illegalNorm() { + // act/assert + GeometryTestUtils.assertThrows(() -> Vector2D.ZERO.getRealNonZeroNorm(), + IllegalNormException.class); + GeometryTestUtils.assertThrows(() -> Vector2D.NaN.getRealNonZeroNorm(), + IllegalNormException.class); + GeometryTestUtils.assertThrows(() -> Vector2D.POSITIVE_INFINITY.getRealNonZeroNorm(), + IllegalNormException.class); + GeometryTestUtils.assertThrows(() -> Vector2D.NEGATIVE_INFINITY.getRealNonZeroNorm(), + IllegalNormException.class); + } + + @Test public void testAdd() { // arrange Vector2D v1 = Vector2D.of(-1, 2);