Added array constructor and getter for Vector2D and Vector3D.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1200546 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2011-11-10 21:03:55 +00:00
parent 0fd55c7ac3
commit ea13fac876
4 changed files with 62 additions and 0 deletions

View File

@ -20,6 +20,7 @@ package org.apache.commons.math.geometry.euclidean.threed;
import java.io.Serializable; import java.io.Serializable;
import java.text.NumberFormat; import java.text.NumberFormat;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MathArithmeticException; import org.apache.commons.math.exception.MathArithmeticException;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.geometry.Vector; import org.apache.commons.math.geometry.Vector;
@ -97,6 +98,21 @@ public class Vector3D implements Serializable, Vector<Euclidean3D> {
this.z = z; 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. /** Simple constructor.
* Build a vector from its azimuthal coordinates * Build a vector from its azimuthal coordinates
* @param alpha azimuth (&alpha;) around Z * @param alpha azimuth (&alpha;) around Z
@ -198,6 +214,14 @@ public class Vector3D implements Serializable, Vector<Euclidean3D> {
return z; 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} */ /** {@inheritDoc} */
public Space getSpace() { public Space getSpace() {
return Euclidean3D.getInstance(); return Euclidean3D.getInstance();

View File

@ -18,6 +18,7 @@ package org.apache.commons.math.geometry.euclidean.twod;
import java.text.NumberFormat; import java.text.NumberFormat;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MathArithmeticException; import org.apache.commons.math.exception.MathArithmeticException;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.geometry.Space; import org.apache.commons.math.geometry.Space;
@ -69,6 +70,20 @@ public class Vector2D implements Vector<Euclidean2D> {
this.y = y; 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 /** Multiplicative constructor
* Build a vector from another one and a scale factor. * Build a vector from another one and a scale factor.
* The vector built will be a * u * The vector built will be a * u
@ -143,6 +158,14 @@ public class Vector2D implements Vector<Euclidean2D> {
return y; 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} */ /** {@inheritDoc} */
public Space getSpace() { public Space getSpace() {
return Euclidean2D.getInstance(); return Euclidean2D.getInstance();

View File

@ -52,6 +52,9 @@ The <action> type attribute can be add,update,fix,remove.
If the output is not quite correct, check for invisible trailing spaces! If the output is not quite correct, check for invisible trailing spaces!
--> -->
<release version="3.0" date="TBD" description="TBD"> <release version="3.0" date="TBD" description="TBD">
<action dev="luc" type="add" due-to="Jan Kotek" >
Added array constructor and getter for Vector2D and Vector3D.
</action>
<action dev="luc" type="add" due-to="Jan Kotek" > <action dev="luc" type="add" due-to="Jan Kotek" >
Added applyTo and applyInverseTo methods in the Rotation class that Added applyTo and applyInverseTo methods in the Rotation class that
handle directly arrays instead of Vector3D instances. handle directly arrays instead of Vector3D instances.

View File

@ -17,6 +17,7 @@
package org.apache.commons.math.geometry.euclidean.threed; 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.exception.MathArithmeticException;
import org.apache.commons.math.random.Well1024a; import org.apache.commons.math.random.Well1024a;
import org.apache.commons.math.util.FastMath; import org.apache.commons.math.util.FastMath;
@ -41,6 +42,13 @@ public class Vector3DTest {
5, Vector3D.MINUS_J, 5, Vector3D.MINUS_J,
-3, Vector3D.MINUS_K), -3, Vector3D.MINUS_K),
2, 0, 3); 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 @Test
@ -49,6 +57,10 @@ public class Vector3DTest {
Assert.assertTrue(FastMath.abs(v.getX() - 1) < 1.0e-12); 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.getY() - 2) < 1.0e-12);
Assert.assertTrue(FastMath.abs(v.getZ() - 3) < 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 @Test