diff --git a/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java b/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java index e69d6485d..26cc4cf02 100644 --- a/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java +++ b/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java @@ -20,6 +20,7 @@ package org.apache.commons.math.geometry.euclidean.threed; import java.io.Serializable; import java.text.NumberFormat; +import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.MathArithmeticException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.geometry.Vector; @@ -97,6 +98,21 @@ public class Vector3D implements Serializable, Vector { this.z = z; } + /** Simple constructor. + * Build a vector from its coordinates + * @param v coordinates array + * @exception DimensionMismatchException if array does not have 3 elements + * @see #toArray() + */ + public Vector3D(double[] v) throws DimensionMismatchException { + if (v.length != 3) { + throw new DimensionMismatchException(v.length, 3); + } + this.x = v[0]; + this.y = v[1]; + this.z = v[2]; + } + /** Simple constructor. * Build a vector from its azimuthal coordinates * @param alpha azimuth (α) around Z @@ -198,6 +214,14 @@ public class Vector3D implements Serializable, Vector { return z; } + /** Get the vector coordinates as a dimension 3 array. + * @return vector coordinates + * @see #Vector3D(double[]) + */ + public double[] toArray() { + return new double[] { x, y, z }; + } + /** {@inheritDoc} */ public Space getSpace() { return Euclidean3D.getInstance(); diff --git a/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java b/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java index 1ce325d2d..7669e809e 100644 --- a/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java +++ b/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java @@ -18,6 +18,7 @@ package org.apache.commons.math.geometry.euclidean.twod; import java.text.NumberFormat; +import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.MathArithmeticException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.geometry.Space; @@ -69,6 +70,20 @@ public class Vector2D implements Vector { this.y = y; } + /** Simple constructor. + * Build a vector from its coordinates + * @param v coordinates array + * @exception DimensionMismatchException if array does not have 2 elements + * @see #toArray() + */ + public Vector2D(double[] v) throws DimensionMismatchException { + if (v.length != 2) { + throw new DimensionMismatchException(v.length, 2); + } + this.x = v[0]; + this.y = v[1]; + } + /** Multiplicative constructor * Build a vector from another one and a scale factor. * The vector built will be a * u @@ -143,6 +158,14 @@ public class Vector2D implements Vector { return y; } + /** Get the vector coordinates as a dimension 2 array. + * @return vector coordinates + * @see #Vector2D(double[]) + */ + public double[] toArray() { + return new double[] { x, y }; + } + /** {@inheritDoc} */ public Space getSpace() { return Euclidean2D.getInstance(); diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index f1ce30a23..b54cdaffa 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -52,6 +52,9 @@ The type attribute can be add,update,fix,remove. If the output is not quite correct, check for invisible trailing spaces! --> + + Added array constructor and getter for Vector2D and Vector3D. + Added applyTo and applyInverseTo methods in the Rotation class that handle directly arrays instead of Vector3D instances. diff --git a/src/test/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DTest.java b/src/test/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DTest.java index 61bad25e1..26e0e3f5e 100644 --- a/src/test/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DTest.java +++ b/src/test/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DTest.java @@ -17,6 +17,7 @@ package org.apache.commons.math.geometry.euclidean.threed; +import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.MathArithmeticException; import org.apache.commons.math.random.Well1024a; import org.apache.commons.math.util.FastMath; @@ -41,6 +42,13 @@ public class Vector3DTest { 5, Vector3D.MINUS_J, -3, Vector3D.MINUS_K), 2, 0, 3); + checkVector(new Vector3D(new double[] { 2, 5, -3 }), + 2, 5, -3); + } + + @Test(expected=DimensionMismatchException.class) + public void testWrongDimension() { + new Vector3D(new double[] { 2, 5 }); } @Test @@ -49,6 +57,10 @@ public class Vector3DTest { Assert.assertTrue(FastMath.abs(v.getX() - 1) < 1.0e-12); Assert.assertTrue(FastMath.abs(v.getY() - 2) < 1.0e-12); Assert.assertTrue(FastMath.abs(v.getZ() - 3) < 1.0e-12); + double[] coordinates = v.toArray(); + Assert.assertTrue(FastMath.abs(coordinates[0] - 1) < 1.0e-12); + Assert.assertTrue(FastMath.abs(coordinates[1] - 2) < 1.0e-12); + Assert.assertTrue(FastMath.abs(coordinates[2] - 3) < 1.0e-12); } @Test