commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1200546 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/geometry/euclidean/threed/ main/java/org/apache/commons/math/geometry/euclidean/twod/ site/xdoc/ test/java/org/apache/commons/math/geometry/euclidean/threed/
Date Thu, 10 Nov 2011 21:03:55 GMT
Author: luc
Date: Thu Nov 10 21:03:55 2011
New Revision: 1200546

URL: http://svn.apache.org/viewvc?rev=1200546&view=rev
Log:
Added array constructor and getter for Vector2D and Vector3D.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java?rev=1200546&r1=1200545&r2=1200546&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/threed/Vector3D.java
Thu Nov 10 21:03:55 2011
@@ -20,6 +20,7 @@ package org.apache.commons.math.geometry
 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;
@@ -98,6 +99,21 @@ public class Vector3D implements Seriali
     }
 
     /** 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
      *              (0 is +X, π/2 is +Y, π is -X and 3π/2 is -Y)
@@ -198,6 +214,14 @@ public class Vector3D implements Seriali
         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();

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java?rev=1200546&r1=1200545&r2=1200546&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/geometry/euclidean/twod/Vector2D.java
Thu Nov 10 21:03:55 2011
@@ -18,6 +18,7 @@ package org.apache.commons.math.geometry
 
 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();

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1200546&r1=1200545&r2=1200546&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Thu Nov 10 21:03:55 2011
@@ -53,6 +53,9 @@ The <action> type attribute can be add,u
      -->
     <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" >
         Added applyTo and applyInverseTo methods in the Rotation class that
         handle directly arrays instead of Vector3D instances.
       </action>

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DTest.java?rev=1200546&r1=1200545&r2=1200546&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/geometry/euclidean/threed/Vector3DTest.java
Thu Nov 10 21:03:55 2011
@@ -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



Mime
View raw message