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 2018/11/09 11:52:38 UTC
[commons-geometry] 04/08: GEOMETRY-23: removing polar and spherical
methods from Vector2D and Vector3D; renaming Vector?D.ofArray to Vector?D.of
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 d59fb7215e191a3045e154111ae71b1e9e2bef1f
Author: Matt Juntunen <ma...@hotmail.com>
AuthorDate: Thu Nov 8 20:59:09 2018 -0500
GEOMETRY-23: removing polar and spherical methods from Vector2D and Vector3D; renaming Vector?D.ofArray to Vector?D.of
---
.../geometry/enclosing/WelzlEncloser3DTest.java | 4 +-
.../threed/enclosing/SphereGeneratorTest.java | 4 +-
.../twod/enclosing/DiskGeneratorTest.java | 4 +-
.../commons/geometry/euclidean/oned/Vector1D.java | 2 +-
.../commons/geometry/euclidean/package-info.java | 19 ++++-
.../euclidean/threed/SphericalCoordinates.java | 56 ++++++------
.../geometry/euclidean/threed/Vector3D.java | 22 +----
.../geometry/euclidean/twod/PolarCoordinates.java | 50 +++++------
.../commons/geometry/euclidean/twod/Vector2D.java | 20 +----
.../euclidean/threed/SphericalCoordinatesTest.java | 67 +++++++++------
.../geometry/euclidean/threed/Vector3DTest.java | 65 ++------------
.../euclidean/twod/PolarCoordinatesTest.java | 99 +++++++++++++---------
.../geometry/euclidean/twod/Vector2DTest.java | 69 ++-------------
.../commons/geometry/spherical/oned/S1Point.java | 2 +-
.../commons/geometry/spherical/twod/S2Point.java | 4 +-
.../geometry/spherical/twod/CircleTest.java | 10 +--
.../spherical/twod/SphericalPolygonsSetTest.java | 14 +--
17 files changed, 210 insertions(+), 301 deletions(-)
diff --git a/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/enclosing/WelzlEncloser3DTest.java b/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/enclosing/WelzlEncloser3DTest.java
index d113841..73b9718 100644
--- a/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/enclosing/WelzlEncloser3DTest.java
+++ b/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/enclosing/WelzlEncloser3DTest.java
@@ -108,13 +108,13 @@ public class WelzlEncloser3DTest {
// define the reference sphere we want to compute
double d = 25 * random.nextDouble();
double refRadius = 10 * random.nextDouble();
- Vector3D refCenter = Vector3D.linearCombination(d, Vector3D.ofArray(sr.nextVector()));
+ Vector3D refCenter = Vector3D.linearCombination(d, Vector3D.of(sr.nextVector()));
// set up a large sample inside the reference sphere
int nbPoints = random.nextInt(1000);
List<Vector3D> points = new ArrayList<>();
for (int i = 0; i < nbPoints; ++i) {
double r = refRadius * random.nextDouble();
- points.add(Vector3D.linearCombination(1.0, refCenter, r, Vector3D.ofArray(sr.nextVector())));
+ points.add(Vector3D.linearCombination(1.0, refCenter, r, Vector3D.of(sr.nextVector())));
}
// test we find a sphere at most as large as the one used for random drawings
diff --git a/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/euclidean/threed/enclosing/SphereGeneratorTest.java b/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/euclidean/threed/enclosing/SphereGeneratorTest.java
index cc29ec7..e6ccaf6 100644
--- a/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/euclidean/threed/enclosing/SphereGeneratorTest.java
+++ b/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/euclidean/threed/enclosing/SphereGeneratorTest.java
@@ -139,10 +139,10 @@ public class SphereGeneratorTest {
for (int i = 0; i < 100; ++i) {
double d = 25 * random.nextDouble();
double refRadius = 10 * random.nextDouble();
- Vector3D refCenter = Vector3D.linearCombination(d, Vector3D.ofArray(sr.nextVector()));
+ Vector3D refCenter = Vector3D.linearCombination(d, Vector3D.of(sr.nextVector()));
List<Vector3D> support = new ArrayList<>();
for (int j = 0; j < 5; ++j) {
- support.add(Vector3D.linearCombination(1.0, refCenter, refRadius, Vector3D.ofArray(sr.nextVector())));
+ support.add(Vector3D.linearCombination(1.0, refCenter, refRadius, Vector3D.of(sr.nextVector())));
}
EnclosingBall<Vector3D> sphere = new SphereGenerator().ballOnSupport(support);
Assert.assertEquals(0.0, refCenter.distance(sphere.getCenter()), 4e-7 * refRadius);
diff --git a/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/euclidean/twod/enclosing/DiskGeneratorTest.java b/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/euclidean/twod/enclosing/DiskGeneratorTest.java
index 966e0c9..375260f 100644
--- a/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/euclidean/twod/enclosing/DiskGeneratorTest.java
+++ b/commons-geometry-enclosing/src/test/java/org/apache/commons/geometry/euclidean/twod/enclosing/DiskGeneratorTest.java
@@ -106,10 +106,10 @@ public class DiskGeneratorTest {
for (int i = 0; i < 500; ++i) {
double d = 25 * random.nextDouble();
double refRadius = 10 * random.nextDouble();
- Vector2D refCenter = Vector2D.linearCombination(d, Vector2D.ofArray(sr.nextVector()));
+ Vector2D refCenter = Vector2D.linearCombination(d, Vector2D.of(sr.nextVector()));
List<Vector2D> support = new ArrayList<>();
for (int j = 0; j < 3; ++j) {
- support.add(Vector2D.linearCombination(1.0, refCenter, refRadius, Vector2D.ofArray(sr.nextVector())));
+ support.add(Vector2D.linearCombination(1.0, refCenter, refRadius, Vector2D.of(sr.nextVector())));
}
EnclosingBall<Vector2D> disk = new DiskGenerator().ballOnSupport(support);
Assert.assertEquals(0.0, refCenter.distance(disk.getCenter()), 3e-9 * refRadius);
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 58a0261..da4ce11 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
@@ -23,7 +23,7 @@ import org.apache.commons.geometry.euclidean.EuclideanVector;
import org.apache.commons.geometry.euclidean.internal.Vectors;
import org.apache.commons.numbers.arrays.LinearCombination;
-/** This class represents a vector in one-dimensional Euclidean space.
+/** This class represents vectors and points in one-dimensional Euclidean space.
* Instances of this class are guaranteed to be immutable.
*/
public class Vector1D extends EuclideanVector<Vector1D> {
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/package-info.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/package-info.java
index 334cea4..948b851 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/package-info.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/package-info.java
@@ -14,8 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
/**
- * This package is the top-level package for Euclidean geometry components.
+ * This is the top-level package for Euclidean geometry components.
*
* <h2>Definition</h2>
* <p>
@@ -31,7 +32,7 @@
*
* <h2>Points and Vectors</h2>
* <p>
- * As mentioned above, points and vectors are separate, distinct entities:
+ * As alluded to above, points and vectors are separate, distinct entities:
* points represent locations in a space and vectors represent displacements.
* This difference is the reason that commons-geometry has separate
* {@link org.apache.commons.geometry.core.Point Point} and
@@ -56,6 +57,20 @@
* "https://threejs.org/docs/index.html#api/en/math/Vector3">Three.js</a>.
* </p>
*
+ * <h2>Coordinate Systems</h2>
+ * <p>
+ * In general, geometric concepts are independent of the coordinate system
+ * used to represent them. For example, in 2-dimensional Euclidean space, the
+ * fact that two points may be subtracted to yield a displacement vector holds
+ * true regardless of whether the points are represented using Cartesian coordinates
+ * or polar coordinates. From this point of view, all coordinate systems can
+ * be considered equal. However, this library does <em>not</em> treat all systems
+ * equal. In order to keep the API lightweight and simple, all coordinates are
+ * assumed to be
+ * <a href="https://en.wikipedia.org/wiki/Cartesian_coordinate_system">Cartesian</a>
+ * unless explicitly noted otherwise.
+ * </p>
+ *
* @see <a href="https://en.wikipedia.org/wiki/Euclidean_space">Euclidean
* Space</a>
*/
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinates.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinates.java
index fc419d2..b863ec5 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinates.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinates.java
@@ -20,7 +20,6 @@ import java.io.Serializable;
import org.apache.commons.geometry.core.Geometry;
import org.apache.commons.geometry.core.Spatial;
-import org.apache.commons.geometry.core.internal.DoubleFunction3N;
import org.apache.commons.geometry.core.internal.SimpleTupleFormat;
import org.apache.commons.geometry.euclidean.twod.PolarCoordinates;
import org.apache.commons.numbers.angle.PlaneAngleRadians;
@@ -138,12 +137,12 @@ public final class SphericalCoordinates implements Spatial, Serializable {
return !isNaN() && (Double.isInfinite(radius) || Double.isInfinite(azimuth) || Double.isInfinite(polar));
}
- /** Convert this set of spherical coordinates to a 3 dimensional vector.
+ /** Convert this set of spherical coordinates to a Cartesian form.
* @return A 3-dimensional vector with an equivalent set of
- * coordinates.
+ * Cartesian coordinates.
*/
public Vector3D toVector() {
- return toCartesian(radius, azimuth, polar, Vector3D::of);
+ return toCartesian(radius, azimuth, polar);
}
/** Get a hashCode for this set of spherical coordinates.
@@ -221,7 +220,7 @@ public final class SphericalCoordinates implements Spatial, Serializable {
* @param z Z coordinate value
* @return a set of spherical coordinates equivalent to the given Cartesian coordinates
*/
- public static SphericalCoordinates ofCartesian(final double x, final double y, final double z) {
+ public static SphericalCoordinates fromCartesian(final double x, final double y, final double z) {
final double radius = Math.sqrt((x*x) + (y*y) + (z*z));
final double azimuth = Math.atan2(y, x);
@@ -231,6 +230,31 @@ public final class SphericalCoordinates implements Spatial, Serializable {
return new SphericalCoordinates(radius, azimuth, polar);
}
+ /** Convert the given set of Cartesian coordinates to spherical coordinates.
+ * @param vec vector containing Cartesian coordinates to convert
+ * @return a set of spherical coordinates equivalent to the given Cartesian coordinates
+ */
+ public static SphericalCoordinates fromCartesian(final Vector3D vec) {
+ return fromCartesian(vec.getX(), vec.getY(), vec.getZ());
+ }
+
+ /** Convert the given set of spherical coordinates to Cartesian coordinates.
+ * @param radius The spherical radius value.
+ * @param azimuth The spherical azimuth angle in radians.
+ * @param polar The spherical polar angle in radians.
+ * @return A 3-dimensional vector with an equivalent set of
+ * Cartesian coordinates.
+ */
+ public static Vector3D toCartesian(final double radius, final double azimuth, final double polar) {
+ final double xyLength = radius * Math.sin(polar);
+
+ final double x = xyLength * Math.cos(azimuth);
+ final double y = xyLength * Math.sin(azimuth);
+ final double z = radius * Math.cos(polar);
+
+ return Vector3D.of(x, y, z);
+ }
+
/** Parse the given string and return a new {@link SphericalCoordinates} instance. The parsed
* coordinate values are normalized as in the {@link #of(double, double, double)} method.
* The expected string format is the same as that returned by {@link #toString()}.
@@ -269,26 +293,4 @@ public final class SphericalCoordinates implements Spatial, Serializable {
return polar;
}
-
- /** Package private method to convert the given set of spherical coordinates to
- * Cartesian coordinates. The Cartesian coordinates are computed and passed to the given
- * factory instance. The factory's return value is returned.
- * @param <T> Factory return type.
- * @param radius The spherical radius value.
- * @param azimuth The spherical azimuth angle in radians.
- * @param polar The spherical polar angle in radians.
- * @param factory Factory instance that will be passed the
- * @return the value returned by the factory when passed Cartesian
- * coordinates equivalent to the given set of spherical coordinates.
- */
- static <T> T toCartesian(final double radius, final double azimuth, final double polar,
- DoubleFunction3N<T> factory) {
- final double xyLength = radius * Math.sin(polar);
-
- final double x = xyLength * Math.cos(azimuth);
- final double y = xyLength * Math.sin(azimuth);
- final double z = radius * Math.cos(polar);
-
- return factory.apply(x, y, z);
- }
}
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 674dd9f..4440884 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
@@ -24,7 +24,7 @@ import org.apache.commons.geometry.euclidean.MultiDimensionalEuclideanVector;
import org.apache.commons.geometry.euclidean.internal.Vectors;
import org.apache.commons.numbers.arrays.LinearCombination;
-/** This class represents a vector in three-dimensional Euclidean space.
+/** This class represents vectors and points in three-dimensional Euclidean space.
* Instances of this class are guaranteed to be immutable.
*/
public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
@@ -115,13 +115,6 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
return new double[] { x, y, z };
}
- /** Return an equivalent set of coordinates in spherical form.
- * @return an equivalent set of coordinates in spherical form.
- */
- public SphericalCoordinates toSpherical() {
- return SphericalCoordinates.ofCartesian(x, y, z);
- }
-
/** {@inheritDoc} */
@Override
public int getDimension() {
@@ -474,24 +467,13 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> {
* @return new vector
* @exception IllegalArgumentException if the array does not have 3 elements
*/
- public static Vector3D ofArray(double[] v) {
+ public static Vector3D of(double[] v) {
if (v.length != 3) {
throw new IllegalArgumentException("Dimension mismatch: " + v.length + " != 3");
}
return new Vector3D(v[0], v[1], v[2]);
}
- /** Create a vector from a set of spherical coordinates.
- * @param radius the spherical radius value
- * @param azimuth the angle in the x-y plane measured in radians counter-clockwise from the
- * positive x axis.
- * @param polar the angle with the positive z axis in radians.
- * @return a vector instance with the given set of spherical coordinates
- */
- public static Vector3D ofSpherical(double radius, double azimuth, double polar) {
- return SphericalCoordinates.toCartesian(radius, azimuth, polar, Vector3D::new);
- }
-
/** Returns a normalized vector derived from the given values.
* @param x abscissa (first coordinate value)
* @param y ordinate (second coordinate value)
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinates.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinates.java
index ee63d93..d46c947 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinates.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinates.java
@@ -20,7 +20,6 @@ import java.io.Serializable;
import org.apache.commons.geometry.core.Geometry;
import org.apache.commons.geometry.core.Spatial;
-import org.apache.commons.geometry.core.internal.DoubleFunction2N;
import org.apache.commons.geometry.core.internal.SimpleTupleFormat;
import org.apache.commons.numbers.angle.PlaneAngleRadians;
@@ -109,13 +108,12 @@ public final class PolarCoordinates implements Spatial, Serializable {
return !isNaN() && (Double.isInfinite(radius) || Double.isInfinite(azimuth));
}
- /** Convert this set of polar coordinates to a 2-dimensional
- * vector.
+ /** Convert this set of polar coordinates to Cartesian coordinates.
* @return A 2-dimensional vector with an equivalent set of
- * coordinates.
+ * coordinates in Cartesian form
*/
- public Vector2D toVector() {
- return toCartesian(radius, azimuth, Vector2D::of);
+ public Vector2D toCartesian() {
+ return toCartesian(radius, azimuth);
}
/** Get a hashCode for this set of polar coordinates.
@@ -188,13 +186,34 @@ public final class PolarCoordinates implements Spatial, Serializable {
* @param y Y coordinate value
* @return polar coordinates equivalent to the given Cartesian coordinates
*/
- public static PolarCoordinates ofCartesian(final double x, final double y) {
+ public static PolarCoordinates fromCartesian(final double x, final double y) {
final double azimuth = Math.atan2(y, x);
final double radius = Math.hypot(x, y);
return new PolarCoordinates(radius, azimuth);
}
+ /** Convert the given Cartesian coordinates to polar form.
+ * @param vec vector containing Cartesian coordinates
+ * @return polar coordinates equivalent to the given Cartesian coordinates
+ */
+ public static PolarCoordinates fromCartesian(final Vector2D vec) {
+ return fromCartesian(vec.getX(), vec.getY());
+ }
+
+ /** Convert the given polar coordinates to Cartesian form.
+ * @param radius Radius value.
+ * @param azimuth Azimuth angle in radians.
+ * @return A 2-dimensional vector with an equivalent set of
+ * coordinates in Cartesian form
+ */
+ public static Vector2D toCartesian(final double radius, final double azimuth) {
+ final double x = radius * Math.cos(azimuth);
+ final double y = radius * Math.sin(azimuth);
+
+ return Vector2D.of(x, y);
+ }
+
/** Parse the given string and return a new polar coordinates instance. The parsed
* coordinates are normalized as in the {@link #of(double, double)} method. The expected string
* format is the same as that returned by {@link #toString()}.
@@ -223,21 +242,4 @@ public final class PolarCoordinates implements Spatial, Serializable {
return azimuth;
}
-
- /** Package private method to convert the given set of polar coordinates to
- * Cartesian coordinates. The Cartesian coordinates are computed and passed to the given
- * factory instance. The factory's return value is returned.
- * @param radius Radius value
- * @param azimuth Azimuth value in radians
- * @param factory Factory instance that will be passed the computed Cartesian coordinates
- * @param <T> Type returned by the factory
- * @return the value returned by the factory when passed Cartesian
- * coordinates equivalent to the given set of polar coordinates.
- */
- static <T> T toCartesian(final double radius, final double azimuth, final DoubleFunction2N<T> factory) {
- final double x = radius * Math.cos(azimuth);
- final double y = radius * Math.sin(azimuth);
-
- return factory.apply(x, y);
- }
}
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 2000c6b..ba1cd64 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
@@ -23,7 +23,7 @@ import org.apache.commons.geometry.euclidean.MultiDimensionalEuclideanVector;
import org.apache.commons.geometry.euclidean.internal.Vectors;
import org.apache.commons.numbers.arrays.LinearCombination;
-/** This class represents a vector in two-dimensional Euclidean space.
+/** This class represents vectors and points in two-dimensional Euclidean space.
* Instances of this class are guaranteed to be immutable.
*/
public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
@@ -88,13 +88,6 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
return y;
}
- /** Return an equivalent set of coordinates in polar form.
- * @return An equivalent set of coordinates in polar form.
- */
- public PolarCoordinates toPolar() {
- return PolarCoordinates.ofCartesian(x, y);
- }
-
/** Get the coordinates for this instance as a dimension 2 array.
* @return coordinates for this instance
*/
@@ -422,22 +415,13 @@ public class Vector2D extends MultiDimensionalEuclideanVector<Vector2D> {
* @return new vector
* @exception IllegalArgumentException if the array does not have 2 elements
*/
- public static Vector2D ofArray(double[] v) {
+ public static Vector2D of(double[] v) {
if (v.length != 2) {
throw new IllegalArgumentException("Dimension mismatch: " + v.length + " != 2");
}
return new Vector2D(v[0], v[1]);
}
- /** Return a vector with coordinates equivalent to the given set of polar coordinates.
- * @param radius The polar coordinate radius value.
- * @param azimuth The polar coordinate azimuth angle in radians.
- * @return vector instance with coordinates equivalent to the given polar coordinates.
- */
- public static Vector2D ofPolar(final double radius, final double azimuth) {
- return PolarCoordinates.toCartesian(radius, azimuth, Vector2D::new);
- }
-
/** Returns a normalized vector derived from the given values.
* @param x abscissa (first coordinate value)
* @param y ordinate (second coordinate value)
diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinatesTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinatesTest.java
index 4f0c0f8..72a7c34 100644
--- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinatesTest.java
+++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/SphericalCoordinatesTest.java
@@ -19,7 +19,6 @@ package org.apache.commons.geometry.euclidean.threed;
import java.util.regex.Pattern;
import org.apache.commons.geometry.core.Geometry;
-import org.apache.commons.geometry.core.internal.DoubleFunction3N;
import org.junit.Assert;
import org.junit.Test;
@@ -114,24 +113,45 @@ public class SphericalCoordinatesTest {
}
@Test
- public void testOfCartesian() {
+ public void testFromCartesian_coordinates() {
// arrange
double sqrt3 = Math.sqrt(3);
// act/assert
- checkSpherical(SphericalCoordinates.ofCartesian(0, 0, 0), 0, 0, 0);
+ checkSpherical(SphericalCoordinates.fromCartesian(0, 0, 0), 0, 0, 0);
- checkSpherical(SphericalCoordinates.ofCartesian(0.1, 0, 0), 0.1, 0, Geometry.HALF_PI);
- checkSpherical(SphericalCoordinates.ofCartesian(-0.1, 0, 0), 0.1, Geometry.PI, Geometry.HALF_PI);
+ checkSpherical(SphericalCoordinates.fromCartesian(0.1, 0, 0), 0.1, 0, Geometry.HALF_PI);
+ checkSpherical(SphericalCoordinates.fromCartesian(-0.1, 0, 0), 0.1, Geometry.PI, Geometry.HALF_PI);
- checkSpherical(SphericalCoordinates.ofCartesian(0, 0.1, 0), 0.1, Geometry.HALF_PI, Geometry.HALF_PI);
- checkSpherical(SphericalCoordinates.ofCartesian(0, -0.1, 0), 0.1, Geometry.THREE_HALVES_PI, Geometry.HALF_PI);
+ checkSpherical(SphericalCoordinates.fromCartesian(0, 0.1, 0), 0.1, Geometry.HALF_PI, Geometry.HALF_PI);
+ checkSpherical(SphericalCoordinates.fromCartesian(0, -0.1, 0), 0.1, Geometry.THREE_HALVES_PI, Geometry.HALF_PI);
- checkSpherical(SphericalCoordinates.ofCartesian(0, 0, 0.1), 0.1, 0, 0);
- checkSpherical(SphericalCoordinates.ofCartesian(0, 0, -0.1), 0.1, 0, Geometry.PI);
+ checkSpherical(SphericalCoordinates.fromCartesian(0, 0, 0.1), 0.1, 0, 0);
+ checkSpherical(SphericalCoordinates.fromCartesian(0, 0, -0.1), 0.1, 0, Geometry.PI);
- checkSpherical(SphericalCoordinates.ofCartesian(1, 1, 1), sqrt3, QUARTER_PI, Math.acos(1 / sqrt3));
- checkSpherical(SphericalCoordinates.ofCartesian(-1, -1, -1), sqrt3, 1.25 * Geometry.PI, Math.acos(-1 / sqrt3));
+ checkSpherical(SphericalCoordinates.fromCartesian(1, 1, 1), sqrt3, QUARTER_PI, Math.acos(1 / sqrt3));
+ checkSpherical(SphericalCoordinates.fromCartesian(-1, -1, -1), sqrt3, 1.25 * Geometry.PI, Math.acos(-1 / sqrt3));
+ }
+
+ @Test
+ public void testFromCartesian_vector() {
+ // arrange
+ double sqrt3 = Math.sqrt(3);
+
+ // act/assert
+ checkSpherical(SphericalCoordinates.fromCartesian(Vector3D.of(0, 0, 0)), 0, 0, 0);
+
+ checkSpherical(SphericalCoordinates.fromCartesian(Vector3D.of(0.1, 0, 0)), 0.1, 0, Geometry.HALF_PI);
+ checkSpherical(SphericalCoordinates.fromCartesian(Vector3D.of(-0.1, 0, 0)), 0.1, Geometry.PI, Geometry.HALF_PI);
+
+ checkSpherical(SphericalCoordinates.fromCartesian(Vector3D.of(0, 0.1, 0)), 0.1, Geometry.HALF_PI, Geometry.HALF_PI);
+ checkSpherical(SphericalCoordinates.fromCartesian(Vector3D.of(0, -0.1, 0)), 0.1, Geometry.THREE_HALVES_PI, Geometry.HALF_PI);
+
+ checkSpherical(SphericalCoordinates.fromCartesian(Vector3D.of(0, 0, 0.1)), 0.1, 0, 0);
+ checkSpherical(SphericalCoordinates.fromCartesian(Vector3D.of(0, 0, -0.1)), 0.1, 0, Geometry.PI);
+
+ checkSpherical(SphericalCoordinates.fromCartesian(Vector3D.of(1, 1, 1)), sqrt3, QUARTER_PI, Math.acos(1 / sqrt3));
+ checkSpherical(SphericalCoordinates.fromCartesian(Vector3D.of(-1, -1, -1)), sqrt3, 1.25 * Geometry.PI, Math.acos(-1 / sqrt3));
}
@Test
@@ -159,22 +179,21 @@ public class SphericalCoordinatesTest {
public void testToCartesian_static() {
// arrange
double sqrt3 = Math.sqrt(3);
- DoubleFunction3N<Vector3D> factory = Vector3D::of;
// act/assert
- checkPoint(SphericalCoordinates.toCartesian(0, 0, 0, factory), 0, 0, 0);
+ checkVector(SphericalCoordinates.toCartesian(0, 0, 0), 0, 0, 0);
- checkPoint(SphericalCoordinates.toCartesian(1, 0, Geometry.HALF_PI, factory), 1, 0, 0);
- checkPoint(SphericalCoordinates.toCartesian(1, Geometry.PI, Geometry.HALF_PI, factory), -1, 0, 0);
+ checkVector(SphericalCoordinates.toCartesian(1, 0, Geometry.HALF_PI), 1, 0, 0);
+ checkVector(SphericalCoordinates.toCartesian(1, Geometry.PI, Geometry.HALF_PI), -1, 0, 0);
- checkPoint(SphericalCoordinates.toCartesian(2, Geometry.HALF_PI, Geometry.HALF_PI, factory), 0, 2, 0);
- checkPoint(SphericalCoordinates.toCartesian(2, Geometry.MINUS_HALF_PI, Geometry.HALF_PI, factory), 0, -2, 0);
+ checkVector(SphericalCoordinates.toCartesian(2, Geometry.HALF_PI, Geometry.HALF_PI), 0, 2, 0);
+ checkVector(SphericalCoordinates.toCartesian(2, Geometry.MINUS_HALF_PI, Geometry.HALF_PI), 0, -2, 0);
- checkPoint(SphericalCoordinates.toCartesian(3, 0, 0, factory), 0, 0, 3);
- checkPoint(SphericalCoordinates.toCartesian(3, 0, Geometry.PI, factory), 0, 0, -3);
+ checkVector(SphericalCoordinates.toCartesian(3, 0, 0), 0, 0, 3);
+ checkVector(SphericalCoordinates.toCartesian(3, 0, Geometry.PI), 0, 0, -3);
- checkPoint(SphericalCoordinates.toCartesian(Math.sqrt(3), QUARTER_PI, Math.acos(1 / sqrt3), factory), 1, 1, 1);
- checkPoint(SphericalCoordinates.toCartesian(Math.sqrt(3), MINUS_THREE_QUARTER_PI, Math.acos(-1 / sqrt3), factory), -1, -1, -1);
+ checkVector(SphericalCoordinates.toCartesian(Math.sqrt(3), QUARTER_PI, Math.acos(1 / sqrt3)), 1, 1, 1);
+ checkVector(SphericalCoordinates.toCartesian(Math.sqrt(3), MINUS_THREE_QUARTER_PI, Math.acos(-1 / sqrt3)), -1, -1, -1);
}
@Test
@@ -364,12 +383,6 @@ public class SphericalCoordinatesTest {
Assert.assertEquals(polar, c.getPolar(), EPS);
}
- private void checkPoint(Vector3D p, double x, double y, double z) {
- Assert.assertEquals(x, p.getX(), EPS);
- Assert.assertEquals(y, p.getY(), EPS);
- Assert.assertEquals(z, p.getZ(), EPS);
- }
-
private void checkVector(Vector3D v, double x, double y, double z) {
Assert.assertEquals(x, v.getX(), EPS);
Assert.assertEquals(y, v.getY(), EPS);
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 368d295..770659e 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
@@ -100,28 +100,6 @@ public class Vector3DTest {
Assert.assertEquals(3.0, arr[2], EPS);
}
-
- @Test
- public void testToSpherical() {
- // arrange
- double sqrt3 = Math.sqrt(3);
-
- // act/assert
- checkSpherical(Vector3D.of(0, 0, 0).toSpherical(), 0, 0, 0);
-
- checkSpherical(Vector3D.of(0.1, 0, 0).toSpherical(), 0.1, 0, Geometry.HALF_PI);
- checkSpherical(Vector3D.of(-0.1, 0, 0).toSpherical(), 0.1, Geometry.PI, Geometry.HALF_PI);
-
- checkSpherical(Vector3D.of(0, 0.1, 0).toSpherical(), 0.1, Geometry.HALF_PI, Geometry.HALF_PI);
- checkSpherical(Vector3D.of(0, -0.1, 0).toSpherical(), 0.1, Geometry.PI + Geometry.HALF_PI, Geometry.HALF_PI);
-
- checkSpherical(Vector3D.of(0, 0, 0.1).toSpherical(), 0.1, 0, 0);
- checkSpherical(Vector3D.of(0, 0, -0.1).toSpherical(), 0.1, 0, Geometry.PI);
-
- checkSpherical(Vector3D.of(1, 1, 1).toSpherical(), sqrt3, 0.25 * Geometry.PI, Math.acos(1 / sqrt3));
- checkSpherical(Vector3D.of(-1, -1, -1).toSpherical(), sqrt3, 1.25 * Geometry.PI, Math.acos(-1 / sqrt3));
- }
-
@Test
public void testDimension() {
// arrange
@@ -830,7 +808,7 @@ public class Vector3DTest {
private void checkProjectAndRejectFullSphere(Vector3D vec, double baseMag, double eps) {
for (double polar = 0.0; polar <= Geometry.PI; polar += 0.5) {
for (double azimuth = 0.0; azimuth <= Geometry.TWO_PI; azimuth += 0.5) {
- Vector3D base = Vector3D.ofSpherical(baseMag, azimuth, polar);
+ Vector3D base = SphericalCoordinates.toCartesian(baseMag, azimuth, polar);
Vector3D proj = vec.project(base);
Vector3D rej = vec.reject(base);
@@ -1034,41 +1012,20 @@ public class Vector3DTest {
}
@Test
- public void testOfArray() {
+ public void testOf_arrayArg() {
// act/assert
- checkVector(Vector3D.ofArray(new double[] { 1, 2, 3 }), 1, 2, 3);
- checkVector(Vector3D.ofArray(new double[] { -1, -2, -3 }), -1, -2, -3);
- checkVector(Vector3D.ofArray(new double[] { Math.PI, Double.NaN, Double.POSITIVE_INFINITY }),
+ checkVector(Vector3D.of(new double[] { 1, 2, 3 }), 1, 2, 3);
+ checkVector(Vector3D.of(new double[] { -1, -2, -3 }), -1, -2, -3);
+ checkVector(Vector3D.of(new double[] { Math.PI, Double.NaN, Double.POSITIVE_INFINITY }),
Math.PI, Double.NaN, Double.POSITIVE_INFINITY);
- checkVector(Vector3D.ofArray(new double[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Math.E}),
+ checkVector(Vector3D.of(new double[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Math.E}),
Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Math.E);
}
@Test(expected = IllegalArgumentException.class)
- public void testOfArray_invalidDimensions() {
+ public void testOf_arrayArg_invalidDimensions() {
// act/assert
- Vector3D.ofArray(new double[] { 0.0, 0.0 });
- }
-
- @Test
- public void testOfSpherical() {
- // arrange
- double sqrt3 = Math.sqrt(3);
-
- // act/assert
- checkVector(Vector3D.ofSpherical(0, 0, 0), 0, 0, 0);
-
- checkVector(Vector3D.ofSpherical(1, 0, Geometry.HALF_PI), 1, 0, 0);
- checkVector(Vector3D.ofSpherical(1, Geometry.PI, Geometry.HALF_PI), -1, 0, 0);
-
- checkVector(Vector3D.ofSpherical(2, Geometry.HALF_PI, Geometry.HALF_PI), 0, 2, 0);
- checkVector(Vector3D.ofSpherical(2, Geometry.MINUS_HALF_PI, Geometry.HALF_PI), 0, -2, 0);
-
- checkVector(Vector3D.ofSpherical(3, 0, 0), 0, 0, 3);
- checkVector(Vector3D.ofSpherical(3, 0, Geometry.PI), 0, 0, -3);
-
- checkVector(Vector3D.ofSpherical(sqrt3, 0.25 * Geometry.PI, Math.acos(1 / sqrt3)), 1, 1, 1);
- checkVector(Vector3D.ofSpherical(sqrt3, -0.75 * Geometry.PI, Math.acos(-1 / sqrt3)), -1, -1, -1);
+ Vector3D.of(new double[] { 0.0, 0.0 });
}
@Test
@@ -1149,10 +1106,4 @@ public class Vector3DTest {
Assert.assertEquals(y, v.getY(), EPS);
Assert.assertEquals(z, v.getZ(), EPS);
}
-
- private void checkSpherical(SphericalCoordinates c, double radius, double azimuth, double polar) {
- Assert.assertEquals(radius, c.getRadius(), EPS);
- Assert.assertEquals(azimuth, c.getAzimuth(), EPS);
- Assert.assertEquals(polar, c.getPolar(), EPS);
- }
}
diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinatesTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinatesTest.java
index 325e8d7..81e94dc 100644
--- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinatesTest.java
+++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/twod/PolarCoordinatesTest.java
@@ -19,7 +19,6 @@ package org.apache.commons.geometry.euclidean.twod;
import java.util.regex.Pattern;
import org.apache.commons.geometry.core.Geometry;
-import org.apache.commons.geometry.core.internal.DoubleFunction2N;
import org.junit.Assert;
import org.junit.Test;
@@ -96,23 +95,43 @@ public class PolarCoordinatesTest {
}
@Test
- public void testOfCartesian() {
+ public void testFromCartesian_coordinates() {
// arrange
double sqrt2 = Math.sqrt(2);
// act/assert
- checkPolar(PolarCoordinates.ofCartesian(0, 0), 0, 0);
+ checkPolar(PolarCoordinates.fromCartesian(0, 0), 0, 0);
- checkPolar(PolarCoordinates.ofCartesian(1, 0), 1, 0);
- checkPolar(PolarCoordinates.ofCartesian(1, 1), sqrt2, 0.25 * Geometry.PI);
- checkPolar(PolarCoordinates.ofCartesian(0, 1), 1, Geometry.HALF_PI);
+ checkPolar(PolarCoordinates.fromCartesian(1, 0), 1, 0);
+ checkPolar(PolarCoordinates.fromCartesian(1, 1), sqrt2, 0.25 * Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(0, 1), 1, Geometry.HALF_PI);
- checkPolar(PolarCoordinates.ofCartesian(-1, 1), sqrt2, 0.75 * Geometry.PI);
- checkPolar(PolarCoordinates.ofCartesian(-1, 0), 1, Geometry.PI);
- checkPolar(PolarCoordinates.ofCartesian(-1, -1), sqrt2, 1.25 * Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(-1, 1), sqrt2, 0.75 * Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(-1, 0), 1, Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(-1, -1), sqrt2, 1.25 * Geometry.PI);
- checkPolar(PolarCoordinates.ofCartesian(0, -1), 1, 1.5 * Geometry.PI);
- checkPolar(PolarCoordinates.ofCartesian(1, -1), sqrt2, 1.75 * Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(0, -1), 1, 1.5 * Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(1, -1), sqrt2, 1.75 * Geometry.PI);
+ }
+
+ @Test
+ public void testFromCartesian_vector() {
+ // arrange
+ double sqrt2 = Math.sqrt(2);
+
+ // act/assert
+ checkPolar(PolarCoordinates.fromCartesian(Vector2D.of(0, 0)), 0, 0);
+
+ checkPolar(PolarCoordinates.fromCartesian(Vector2D.of(1, 0)), 1, 0);
+ checkPolar(PolarCoordinates.fromCartesian(Vector2D.of(1, 1)), sqrt2, 0.25 * Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(Vector2D.of(0, 1)), 1, Geometry.HALF_PI);
+
+ checkPolar(PolarCoordinates.fromCartesian(Vector2D.of(-1, 1)), sqrt2, 0.75 * Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(Vector2D.of(-1, 0)), 1, Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(Vector2D.of(-1, -1)), sqrt2, 1.25 * Geometry.PI);
+
+ checkPolar(PolarCoordinates.fromCartesian(Vector2D.of(0, -1)), 1, 1.5 * Geometry.PI);
+ checkPolar(PolarCoordinates.fromCartesian(Vector2D.of(1, -1)), sqrt2, 1.75 * Geometry.PI);
}
@Test
@@ -215,62 +234,58 @@ public class PolarCoordinatesTest {
}
@Test
- public void testToVector() {
+ public void testToCartesian() {
// arrange
double sqrt2 = Math.sqrt(2);
// act/assert
- checkVector(PolarCoordinates.of(0, 0).toVector(), 0, 0);
+ checkVector(PolarCoordinates.of(0, 0).toCartesian(), 0, 0);
- checkVector(PolarCoordinates.of(1, 0).toVector(), 1, 0);
- checkVector(PolarCoordinates.of(sqrt2, 0.25 * Geometry.PI).toVector(), 1, 1);
- checkVector(PolarCoordinates.of(1, Geometry.HALF_PI).toVector(), 0, 1);
+ checkVector(PolarCoordinates.of(1, 0).toCartesian(), 1, 0);
+ checkVector(PolarCoordinates.of(sqrt2, 0.25 * Geometry.PI).toCartesian(), 1, 1);
+ checkVector(PolarCoordinates.of(1, Geometry.HALF_PI).toCartesian(), 0, 1);
- checkVector(PolarCoordinates.of(sqrt2, 0.75 * Geometry.PI).toVector(), -1, 1);
- checkVector(PolarCoordinates.of(1, Geometry.PI).toVector(), -1, 0);
- checkVector(PolarCoordinates.of(sqrt2, -0.75 * Geometry.PI).toVector(), -1, -1);
+ checkVector(PolarCoordinates.of(sqrt2, 0.75 * Geometry.PI).toCartesian(), -1, 1);
+ checkVector(PolarCoordinates.of(1, Geometry.PI).toCartesian(), -1, 0);
+ checkVector(PolarCoordinates.of(sqrt2, -0.75 * Geometry.PI).toCartesian(), -1, -1);
- checkVector(PolarCoordinates.of(1, Geometry.MINUS_HALF_PI).toVector(), 0, -1);
- checkVector(PolarCoordinates.of(sqrt2, -0.25 * Geometry.PI).toVector(), 1, -1);
+ checkVector(PolarCoordinates.of(1, Geometry.MINUS_HALF_PI).toCartesian(), 0, -1);
+ checkVector(PolarCoordinates.of(sqrt2, -0.25 * Geometry.PI).toCartesian(), 1, -1);
}
@Test
public void testToCartesian_static() {
// arrange
- DoubleFunction2N<Vector2D> factory = Vector2D::of;
double sqrt2 = Math.sqrt(2);
// act/assert
- checkPoint(PolarCoordinates.toCartesian(0, 0, factory), 0, 0);
+ checkVector(PolarCoordinates.toCartesian(0, 0), 0, 0);
- checkPoint(PolarCoordinates.toCartesian(1, 0, factory), 1, 0);
- checkPoint(PolarCoordinates.toCartesian(sqrt2, 0.25 * Geometry.PI, factory), 1, 1);
- checkPoint(PolarCoordinates.toCartesian(1, Geometry.HALF_PI, factory), 0, 1);
+ checkPoint(PolarCoordinates.toCartesian(1, 0), 1, 0);
+ checkPoint(PolarCoordinates.toCartesian(sqrt2, 0.25 * Geometry.PI), 1, 1);
+ checkPoint(PolarCoordinates.toCartesian(1, Geometry.HALF_PI), 0, 1);
- checkPoint(PolarCoordinates.toCartesian(sqrt2, 0.75 * Geometry.PI, factory), -1, 1);
- checkPoint(PolarCoordinates.toCartesian(1, Geometry.PI, factory), -1, 0);
- checkPoint(PolarCoordinates.toCartesian(sqrt2, -0.75 * Geometry.PI, factory), -1, -1);
+ checkPoint(PolarCoordinates.toCartesian(sqrt2, 0.75 * Geometry.PI), -1, 1);
+ checkPoint(PolarCoordinates.toCartesian(1, Geometry.PI), -1, 0);
+ checkPoint(PolarCoordinates.toCartesian(sqrt2, -0.75 * Geometry.PI), -1, -1);
- checkPoint(PolarCoordinates.toCartesian(1, Geometry.MINUS_HALF_PI, factory), 0, -1);
- checkPoint(PolarCoordinates.toCartesian(sqrt2, -0.25 * Geometry.PI, factory), 1, -1);
+ checkPoint(PolarCoordinates.toCartesian(1, Geometry.MINUS_HALF_PI), 0, -1);
+ checkPoint(PolarCoordinates.toCartesian(sqrt2, -0.25 * Geometry.PI), 1, -1);
}
@Test
public void testToCartesian_static_NaNAndInfinite() {
- // arrange
- DoubleFunction2N<Vector2D> factory = Vector2D::of;
-
// act/assert
- Assert.assertTrue(PolarCoordinates.toCartesian(Double.NaN, 0, factory).isNaN());
- Assert.assertTrue(PolarCoordinates.toCartesian(0, Double.NaN, factory).isNaN());
+ Assert.assertTrue(PolarCoordinates.toCartesian(Double.NaN, 0).isNaN());
+ Assert.assertTrue(PolarCoordinates.toCartesian(0, Double.NaN).isNaN());
- Assert.assertTrue(PolarCoordinates.toCartesian(Double.POSITIVE_INFINITY, 0, factory).isNaN());
- Assert.assertTrue(PolarCoordinates.toCartesian(0, Double.POSITIVE_INFINITY, factory).isNaN());
- Assert.assertTrue(PolarCoordinates.toCartesian(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, factory).isNaN());
+ Assert.assertTrue(PolarCoordinates.toCartesian(Double.POSITIVE_INFINITY, 0).isNaN());
+ Assert.assertTrue(PolarCoordinates.toCartesian(0, Double.POSITIVE_INFINITY).isNaN());
+ Assert.assertTrue(PolarCoordinates.toCartesian(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY).isNaN());
- Assert.assertTrue(PolarCoordinates.toCartesian(Double.NEGATIVE_INFINITY, 0, factory).isNaN());
- Assert.assertTrue(PolarCoordinates.toCartesian(0, Double.NEGATIVE_INFINITY, factory).isNaN());
- Assert.assertTrue(PolarCoordinates.toCartesian(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, factory).isNaN());
+ Assert.assertTrue(PolarCoordinates.toCartesian(Double.NEGATIVE_INFINITY, 0).isNaN());
+ Assert.assertTrue(PolarCoordinates.toCartesian(0, Double.NEGATIVE_INFINITY).isNaN());
+ Assert.assertTrue(PolarCoordinates.toCartesian(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY).isNaN());
}
@Test
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 5d01a1f..327547a 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
@@ -123,34 +123,6 @@ public class Vector2DTest {
}
@Test
- public void testToPolar() {
- // arrange
- double sqrt2 = Math.sqrt(2.0);
-
- // act/assert
- checkPolar(Vector2D.of(0, 0).toPolar(), 0, 0);
-
- checkPolar(Vector2D.of(1, 0).toPolar(), 1, 0);
- checkPolar(Vector2D.of(-1, 0).toPolar(), 1, Geometry.PI);
-
- checkPolar(Vector2D.of(0, 2).toPolar(), 2, Geometry.HALF_PI);
- checkPolar(Vector2D.of(0, -2).toPolar(), 2, Geometry.THREE_HALVES_PI);
-
- checkPolar(Vector2D.of(sqrt2, sqrt2).toPolar(), 2, 0.25 * Geometry.PI);
- checkPolar(Vector2D.of(-sqrt2, sqrt2).toPolar(), 2, 0.75 * Geometry.PI);
- checkPolar(Vector2D.of(sqrt2, -sqrt2).toPolar(), 2, 1.75 * Geometry.PI);
- checkPolar(Vector2D.of(-sqrt2, -sqrt2).toPolar(), 2, 1.25 * Geometry.PI);
- }
-
- @Test
- public void testToPolar_NaNAndInfinite() {
- // act/assert
- Assert.assertTrue(Vector2D.of(Double.NaN, Double.NaN).toPolar().isNaN());
- Assert.assertTrue(Vector2D.of(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY).toPolar().isInfinite());
- }
-
-
- @Test
public void testGetZero() {
// act/assert
checkVector(Vector2D.of(1.0, 1.0).getZero(), 0, 0);
@@ -427,7 +399,7 @@ public class Vector2DTest {
public void testOrthogonal_fullCircle() {
for (double az = 0.0; az<=Geometry.TWO_PI; az += 0.25) {
// arrange
- Vector2D v = Vector2D.ofPolar(Math.PI, az);
+ Vector2D v = PolarCoordinates.toCartesian(Math.PI, az);
//act
Vector2D ortho = v.orthogonal();
@@ -647,7 +619,7 @@ public class Vector2DTest {
private void checkProjectAndRejectFullCircle(Vector2D vec, double baseMag, double eps) {
for (double theta = 0.0; theta <= Geometry.TWO_PI; theta += 0.5) {
- Vector2D base = Vector2D.ofPolar(baseMag, theta);
+ Vector2D base = PolarCoordinates.toCartesian(baseMag, theta);
Vector2D proj = vec.project(base);
Vector2D rej = vec.reject(base);
@@ -841,38 +813,16 @@ public class Vector2DTest {
@Test
public void testOf_arrayArg() {
// act/assert
- checkVector(Vector2D.ofArray(new double[] { 0, 1 }), 0, 1);
- checkVector(Vector2D.ofArray(new double[] { -1, -2 }), -1, -2);
- checkVector(Vector2D.ofArray(new double[] { Math.PI, Double.NaN }), Math.PI, Double.NaN);
- checkVector(Vector2D.ofArray(new double[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }), Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
+ checkVector(Vector2D.of(new double[] { 0, 1 }), 0, 1);
+ checkVector(Vector2D.of(new double[] { -1, -2 }), -1, -2);
+ checkVector(Vector2D.of(new double[] { Math.PI, Double.NaN }), Math.PI, Double.NaN);
+ checkVector(Vector2D.of(new double[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }), Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY);
}
@Test(expected = IllegalArgumentException.class)
public void testOf_arrayArg_invalidDimensions() {
// act/assert
- Vector2D.ofArray(new double[] {0.0 });
- }
-
- @Test
- public void testOfPolar() {
- // arrange
- double eps = 1e-15;
- double sqrt2 = Math.sqrt(2.0);
-
- // act/assert
- checkVector(Vector2D.ofPolar(0, 0), 0, 0, eps);
- checkVector(Vector2D.ofPolar(1, 0), 1, 0, eps);
-
- checkVector(Vector2D.ofPolar(2, Geometry.PI), -2, 0, eps);
- checkVector(Vector2D.ofPolar(-2, Geometry.PI), 2, 0, eps);
-
- checkVector(Vector2D.ofPolar(2, Geometry.HALF_PI), 0, 2, eps);
- checkVector(Vector2D.ofPolar(-2, Geometry.HALF_PI), 0, -2, eps);
-
- checkVector(Vector2D.ofPolar(2, 0.25 * Geometry.PI), sqrt2, sqrt2, eps);
- checkVector(Vector2D.ofPolar(2, 0.75 * Geometry.PI), -sqrt2, sqrt2, eps);
- checkVector(Vector2D.ofPolar(2, -0.25 * Geometry.PI), sqrt2, - sqrt2, eps);
- checkVector(Vector2D.ofPolar(2, -0.75 * Geometry.PI), -sqrt2, - sqrt2, eps);
+ Vector2D.of(new double[] {0.0 });
}
@Test
@@ -956,9 +906,4 @@ public class Vector2DTest {
Assert.assertEquals(x, v.getX(), eps);
Assert.assertEquals(y, v.getY(), eps);
}
-
- private void checkPolar(PolarCoordinates polar, double radius, double azimuth) {
- Assert.assertEquals(radius, polar.getRadius(), EPS);
- Assert.assertEquals(azimuth, polar.getAzimuth(), EPS);
- }
}
diff --git a/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/oned/S1Point.java b/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/oned/S1Point.java
index d12dc9b..c13083d 100644
--- a/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/oned/S1Point.java
+++ b/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/oned/S1Point.java
@@ -47,7 +47,7 @@ public final class S1Point implements Point<S1Point>, Serializable {
*/
private S1Point(final double azimuth) {
this.azimuth = PolarCoordinates.normalizeAzimuth(azimuth);
- this.vector = Double.isFinite(azimuth) ? Vector2D.ofPolar(1.0, azimuth) : Vector2D.NaN;
+ this.vector = Double.isFinite(azimuth) ? PolarCoordinates.toCartesian(1.0, azimuth) : Vector2D.NaN;
}
/** Get the azimuthal angle in radians.
diff --git a/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/twod/S2Point.java b/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/twod/S2Point.java
index ddac14d..4579015 100644
--- a/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/twod/S2Point.java
+++ b/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/twod/S2Point.java
@@ -71,7 +71,7 @@ public final class S2Point implements Point<S2Point>, Serializable {
private S2Point(final double azimuth, final double polar, final Vector3D vector) {
this.azimuth = SphericalCoordinates.normalizeAzimuth(azimuth);
this.polar = SphericalCoordinates.normalizePolar(polar);
- this.vector = (vector != null) ? vector : Vector3D.ofSpherical(1.0, azimuth, polar);
+ this.vector = (vector != null) ? vector : SphericalCoordinates.toCartesian(1.0, azimuth, polar);
}
/** Get the azimuthal angle in the x-y plane in radians.
@@ -211,7 +211,7 @@ public final class S2Point implements Point<S2Point>, Serializable {
* @exception IllegalStateException if vector norm is zero
*/
public static S2Point ofVector(final Vector3D vector) {
- SphericalCoordinates coords = vector.toSpherical();
+ SphericalCoordinates coords = SphericalCoordinates.fromCartesian(vector);
return new S2Point(coords.getAzimuth(), coords.getPolar(), vector.normalize());
}
diff --git a/commons-geometry-spherical/src/test/java/org/apache/commons/geometry/spherical/twod/CircleTest.java b/commons-geometry-spherical/src/test/java/org/apache/commons/geometry/spherical/twod/CircleTest.java
index f281988..5cb1c77 100644
--- a/commons-geometry-spherical/src/test/java/org/apache/commons/geometry/spherical/twod/CircleTest.java
+++ b/commons-geometry-spherical/src/test/java/org/apache/commons/geometry/spherical/twod/CircleTest.java
@@ -132,8 +132,8 @@ public class CircleTest {
UnitSphereSampler sphRandom = new UnitSphereSampler(3, RandomSource.create(RandomSource.WELL_1024_A,
0xbfd34e92231bbcfel));
for (int i = 0; i < 100; ++i) {
- Circle c1 = new Circle(Vector3D.ofArray(sphRandom.nextVector()), 1.0e-10);
- Circle c2 = new Circle(Vector3D.ofArray(sphRandom.nextVector()), 1.0e-10);
+ Circle c1 = new Circle(Vector3D.of(sphRandom.nextVector()), 1.0e-10);
+ Circle c2 = new Circle(Vector3D.of(sphRandom.nextVector()), 1.0e-10);
checkArcIsInside(c1, c2);
checkArcIsInside(c2, c1);
}
@@ -157,16 +157,16 @@ public class CircleTest {
UnitSphereSampler sphRandom = new UnitSphereSampler(3, random);
for (int i = 0; i < 100; ++i) {
- Rotation r = new Rotation(Vector3D.ofArray(sphRandom.nextVector()),
+ Rotation r = new Rotation(Vector3D.of(sphRandom.nextVector()),
Math.PI * random.nextDouble(),
RotationConvention.VECTOR_OPERATOR);
Transform<S2Point, S1Point> t = Circle.getTransform(r);
- S2Point p = S2Point.ofVector(Vector3D.ofArray(sphRandom.nextVector()));
+ S2Point p = S2Point.ofVector(Vector3D.of(sphRandom.nextVector()));
S2Point tp = t.apply(p);
Assert.assertEquals(0.0, r.applyTo(p.getVector()).distance(tp.getVector()), 1.0e-10);
- Circle c = new Circle(Vector3D.ofArray(sphRandom.nextVector()), 1.0e-10);
+ Circle c = new Circle(Vector3D.of(sphRandom.nextVector()), 1.0e-10);
Circle tc = (Circle) t.apply(c);
Assert.assertEquals(0.0, r.applyTo(c.getPole()).distance(tc.getPole()), 1.0e-10);
Assert.assertEquals(0.0, r.applyTo(c.getXAxis()).distance(tc.getXAxis()), 1.0e-10);
diff --git a/commons-geometry-spherical/src/test/java/org/apache/commons/geometry/spherical/twod/SphericalPolygonsSetTest.java b/commons-geometry-spherical/src/test/java/org/apache/commons/geometry/spherical/twod/SphericalPolygonsSetTest.java
index 9c9f891..cad3250 100644
--- a/commons-geometry-spherical/src/test/java/org/apache/commons/geometry/spherical/twod/SphericalPolygonsSetTest.java
+++ b/commons-geometry-spherical/src/test/java/org/apache/commons/geometry/spherical/twod/SphericalPolygonsSetTest.java
@@ -42,7 +42,7 @@ public class SphericalPolygonsSetTest {
new UnitSphereSampler(3, RandomSource.create(RandomSource.WELL_1024_A,
0x852fd2a0ed8d2f6dl));
for (int i = 0; i < 1000; ++i) {
- Vector3D v = Vector3D.ofArray(random.nextVector());
+ Vector3D v = Vector3D.of(random.nextVector());
Assert.assertEquals(Location.INSIDE, full.checkPoint(S2Point.ofVector(v)));
}
Assert.assertEquals(4 * Math.PI, new SphericalPolygonsSet(0.01, new S2Point[0]).getSize(), 1.0e-10);
@@ -60,7 +60,7 @@ public class SphericalPolygonsSetTest {
new UnitSphereSampler(3, RandomSource.create(RandomSource.WELL_1024_A,
0x76d9205d6167b6ddl));
for (int i = 0; i < 1000; ++i) {
- Vector3D v = Vector3D.ofArray(random.nextVector());
+ Vector3D v = Vector3D.of(random.nextVector());
Assert.assertEquals(Location.OUTSIDE, empty.checkPoint(S2Point.ofVector(v)));
}
Assert.assertEquals(0, empty.getSize(), 1.0e-10);
@@ -79,7 +79,7 @@ public class SphericalPolygonsSetTest {
new UnitSphereSampler(3, RandomSource.create(RandomSource.WELL_1024_A,
0x6b9d4a6ad90d7b0bl));
for (int i = 0; i < 1000; ++i) {
- Vector3D v = Vector3D.ofArray(random.nextVector());
+ Vector3D v = Vector3D.of(random.nextVector());
if (v.getZ() < -sinTol) {
Assert.assertEquals(Location.INSIDE, south.checkPoint(S2Point.ofVector(v)));
} else if (v.getZ() > sinTol) {
@@ -115,7 +115,7 @@ public class SphericalPolygonsSetTest {
new UnitSphereSampler(3, RandomSource.create(RandomSource.WELL_1024_A,
0x9c9802fde3cbcf25l));
for (int i = 0; i < 1000; ++i) {
- Vector3D v = Vector3D.ofArray(random.nextVector());
+ Vector3D v = Vector3D.of(random.nextVector());
if ((v.getX() > sinTol) && (v.getY() > sinTol) && (v.getZ() > sinTol)) {
Assert.assertEquals(Location.INSIDE, octant.checkPoint(S2Point.ofVector(v)));
} else if ((v.getX() < -sinTol) || (v.getY() < -sinTol) || (v.getZ() < -sinTol)) {
@@ -180,7 +180,7 @@ public class SphericalPolygonsSetTest {
new UnitSphereSampler(3, RandomSource.create(RandomSource.WELL_1024_A,
0xb8fc5acc91044308l));
for (int i = 0; i < 1000; ++i) {
- Vector3D v = Vector3D.ofArray(random.nextVector());
+ Vector3D v = Vector3D.of(random.nextVector());
if ((v.getX() > sinTol) && (v.getY() > sinTol) && (v.getZ() > sinTol)) {
Assert.assertEquals(Location.INSIDE, octant.checkPoint(S2Point.ofVector(v)));
} else if ((v.getX() < -sinTol) || (v.getY() < -sinTol) || (v.getZ() < -sinTol)) {
@@ -206,7 +206,7 @@ public class SphericalPolygonsSetTest {
new UnitSphereSampler(3, RandomSource.create(RandomSource.WELL_1024_A,
0x9c9802fde3cbcf25l));
for (int i = 0; i < 1000; ++i) {
- Vector3D v = Vector3D.ofArray(random.nextVector());
+ Vector3D v = Vector3D.of(random.nextVector());
if (((v.getX() < -sinTol) || (v.getY() < -sinTol)) && (v.getZ() > sinTol)) {
Assert.assertEquals(Location.INSIDE, threeOctants.checkPoint(S2Point.ofVector(v)));
} else if (((v.getX() > sinTol) && (v.getY() > sinTol)) || (v.getZ() < -sinTol)) {
@@ -340,7 +340,7 @@ public class SphericalPolygonsSetTest {
new UnitSphereSampler(3, RandomSource.create(RandomSource.WELL_1024_A,
0xcc5ce49949e0d3ecl));
for (int i = 0; i < 1000; ++i) {
- Vector3D v = Vector3D.ofArray(random.nextVector());
+ Vector3D v = Vector3D.of(random.nextVector());
if ((v.getX() < -sinTol) && (v.getY() < -sinTol) && (v.getZ() < -sinTol)) {
Assert.assertEquals(Location.INSIDE, polygon.checkPoint(S2Point.ofVector(v)));
} else if ((v.getX() < sinTol) && (v.getY() < sinTol) && (v.getZ() < sinTol)) {