sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1654423 - in /sis/branches/JDK8/core: sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/ sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/ sis-referencing/src/test/java/org/apache/sis/re...
Date Fri, 23 Jan 2015 22:39:03 GMT
Author: desruisseaux
Date: Fri Jan 23 22:39:02 2015
New Revision: 1654423

URL: http://svn.apache.org/r1654423
Log:
Add support for the one-dimensional case in LinearTransformBuilder.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilder.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LinearTransform.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilderTest.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Line.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Plane.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilder.java?rev=1654423&r1=1654422&r2=1654423&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilder.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilder.java
[UTF-8] Fri Jan 23 22:39:02 2015
@@ -18,6 +18,7 @@ package org.apache.sis.referencing.opera
 
 import org.opengis.geometry.DirectPosition;
 import org.opengis.geometry.MismatchedDimensionException;
+import org.apache.sis.math.Line;
 import org.apache.sis.math.Plane;
 import org.apache.sis.referencing.operation.matrix.Matrices;
 import org.apache.sis.referencing.operation.matrix.MatrixSIS;
@@ -46,6 +47,10 @@ import org.apache.sis.util.Debug;
  * @since   0.5
  * @version 0.5
  * @module
+ *
+ * @see LinearTransform
+ * @see Line
+ * @see Plane
  */
 public class LinearTransformBuilder {
     /**
@@ -98,15 +103,19 @@ public class LinearTransformBuilder {
     /**
      * Sets the source points. The number of points shall be the same than the number of
target points.
      *
-     * <p><b>Limitation:</b> in current implementation, the source points
must be two-dimensional.
-     * But this restriction may be removed in a future SIS version.</p>
+     * <p><b>Limitation:</b> in current implementation, the source points
must be one or two-dimensional.
+     * This restriction may be removed in a future SIS version.</p>
      *
      * @param  points The source points, assumed precise.
      * @throws MismatchedDimensionException if at least one point does not have the expected
number of dimensions.
      */
     public void setSourcePoints(final DirectPosition... points) throws MismatchedDimensionException
{
         ArgumentChecks.ensureNonNull("points", points);
-        sources = toArrays(points, 2);
+        if (points.length != 0) {
+            sources = toArrays(points, points[0].getDimension() == 1 ? 1 : 2);
+        } else {
+            sources = null;
+        }
         correlation = null;
     }
 
@@ -148,16 +157,31 @@ public class LinearTransformBuilder {
         }
         final int sourceDim = sources.length;
         final int targetDim = targets.length;
-        final MatrixSIS matrix = Matrices.createZero(targetDim + 1, sourceDim + 1);
-        final Plane plan = new Plane();
         correlation = new double[targetDim];
-        for (int j=0; j<targets.length; j++) {
-            correlation[j] = plan.fit(sources[0], sources[1], targets[j]);
-            matrix.setElement(j, 0, plan.slopeX());
-            matrix.setElement(j, 1, plan.slopeY());
-            matrix.setElement(j, 2, plan.z0());
-        }
+        final MatrixSIS matrix = Matrices.createZero(targetDim + 1, sourceDim + 1);
         matrix.setElement(targetDim, sourceDim, 1);
+        switch (sourceDim) {
+            case 1: {
+                final Line line = new Line();
+                for (int j=0; j<targets.length; j++) {
+                    correlation[j] = line.fit(sources[0], targets[j]);
+                    matrix.setElement(j, 0, line.slope());
+                    matrix.setElement(j, 1, line.y0());
+                }
+                break;
+            }
+            case 2: {
+                final Plane plan = new Plane();
+                for (int j=0; j<targets.length; j++) {
+                    correlation[j] = plan.fit(sources[0], sources[1], targets[j]);
+                    matrix.setElement(j, 0, plan.slopeX());
+                    matrix.setElement(j, 1, plan.slopeY());
+                    matrix.setElement(j, 2, plan.z0());
+                }
+                break;
+            }
+            default: throw new AssertionError(sourceDim); // Should have been verified by
setSourcePoints(…) method.
+        }
         return MathTransforms.linear(matrix);
     }
 

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LinearTransform.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LinearTransform.java?rev=1654423&r1=1654422&r2=1654423&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LinearTransform.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LinearTransform.java
[UTF-8] Fri Jan 23 22:39:02 2015
@@ -64,6 +64,8 @@ import org.opengis.referencing.operation
  * @version 0.4
  * @module
  *
+ * @see org.apache.sis.referencing.operation.transform.MathTransforms#linear(Matrix)
+ * @see org.apache.sis.referencing.operation.builder.LinearTransformBuilder
  * @see java.awt.geom.AffineTransform
  * @see <a href="http://mathworld.wolfram.com/AffineTransformation.html">Affine transformation
on MathWorld</a>
  */

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java?rev=1654423&r1=1654422&r2=1654423&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
[UTF-8] Fri Jan 23 22:39:02 2015
@@ -108,6 +108,7 @@ public final class MathTransforms extend
      * @param  matrix The matrix used to define the linear transform.
      * @return The linear (usually affine) transform.
      *
+     * @see #getMatrix(MathTransform)
      * @see org.opengis.referencing.operation.MathTransformFactory#createAffineTransform(Matrix)
      */
     public static LinearTransform linear(final Matrix matrix) {
@@ -296,6 +297,9 @@ public final class MathTransforms extend
      *
      * @param  transform The transform for which to get the matrix, or {@code null}.
      * @return The matrix of the given transform, or {@code null} if none.
+     *
+     * @see #linear(Matrix)
+     * @see LinearTransform#getMatrix()
      */
     public static Matrix getMatrix(final MathTransform transform) {
         if (transform instanceof LinearTransform) {

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilderTest.java?rev=1654423&r1=1654422&r2=1654423&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilderTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/builder/LinearTransformBuilderTest.java
[UTF-8] Fri Jan 23 22:39:02 2015
@@ -19,6 +19,7 @@ package org.apache.sis.referencing.opera
 import java.util.Random;
 import java.awt.geom.AffineTransform;
 import org.opengis.referencing.operation.Matrix;
+import org.apache.sis.geometry.DirectPosition1D;
 import org.apache.sis.geometry.DirectPosition2D;
 import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.TestUtilities;
@@ -48,7 +49,25 @@ public final strictfp class LinearTransf
      * Tests a very simple case where an exact answer is expected.
      */
     @Test
-    public void testMinimalist() {
+    public void testMinimalist1D() {
+        final LinearTransformBuilder builder = new LinearTransformBuilder();
+        builder.setSourcePoints(
+                new DirectPosition1D(1),
+                new DirectPosition1D(2));
+        builder.setTargetPoints(
+                new DirectPosition1D(1),
+                new DirectPosition1D(3));
+        final Matrix m = builder.create().getMatrix();
+        assertEquals("m₀₀",  2, m.getElement(0, 0), STRICT);
+        assertEquals("m₀₁", -1, m.getElement(0, 1), STRICT);
+        assertArrayEquals("correlation", new double[] {1}, builder.correlation(), STRICT);
+    }
+
+    /**
+     * Tests a very simple case where an exact answer is expected.
+     */
+    @Test
+    public void testMinimalist2D() {
         final LinearTransformBuilder builder = new LinearTransformBuilder();
         builder.setSourcePoints(
                 new DirectPosition2D(1, 1),
@@ -75,17 +94,41 @@ public final strictfp class LinearTransf
 
     /**
      * Tests with a random number of points with an exact solution expected.
+     */
+    @Test
+    @DependsOnMethod("testMinimalist1D")
+    public void testExact1D() {
+        final Random rd = TestUtilities.createRandomNumberGenerator(-6080923837183751016L);
+        for (int i=0; i<10; i++) {
+            test1D(rd, rd.nextInt(900) + 100, false, 1E-14, 1E-12);
+        }
+    }
+
+    /**
+     * Tests with a random number of points with an exact solution expected.
      *
      * <p><b>Note:</b> this test can pass with a random seed most of the
time. But we fix the seed anyway
      * because there is always a small probability that truly random points are all colinear,
in which case
      * the test would fail. Even if the probability is low, we do not take the risk of random
build failures.</p>
      */
     @Test
-    @DependsOnMethod("testMinimalist")
-    public void testExact() {
+    @DependsOnMethod("testMinimalist2D")
+    public void testExact2D() {
         final Random rd = TestUtilities.createRandomNumberGenerator(41632405806929L);
         for (int i=0; i<10; i++) {
-            test(rd, rd.nextInt(900) + 100, false, 1E-14, 1E-12);
+            test2D(rd, rd.nextInt(900) + 100, false, 1E-14, 1E-12);
+        }
+    }
+
+    /**
+     * Tests with a random number of points and a random errors in target points.
+     */
+    @Test
+    @DependsOnMethod("testExact1D")
+    public void testNonExact1D() {
+        final Random rd = TestUtilities.createRandomNumberGenerator(8819436190826166876L);
+        for (int i=0; i<4; i++) {
+            test1D(rd, rd.nextInt(900) + 100, true, 0.02, 0.5);
         }
     }
 
@@ -99,23 +142,59 @@ public final strictfp class LinearTransf
      * failures.</p>
      */
     @Test
-    @DependsOnMethod("testExact")
-    public void testNonExact() {
+    @DependsOnMethod("testExact2D")
+    public void testNonExact2D() {
         final Random rd = TestUtilities.createRandomNumberGenerator(270575025643864L);
         for (int i=0; i<4; i++) {
-            test(rd, rd.nextInt(900) + 100, true, 0.02, 0.5);
+            test2D(rd, rd.nextInt(900) + 100, true, 0.02, 0.5);
         }
     }
 
     /**
-     * Implementation of {@link #testExact()} and {@link #testNonExact()}.
+     * Implementation of {@link #testExact1D()} and {@link #testNonExact1D()}.
+     *
+     * @param rd        The random number generator to use.
+     * @param numPts    The number of points to generate.
+     * @param addErrors {@code true} for adding a random error in the target points.
+     * @param scaleTolerance Tolerance threshold for floating point comparisons.
+     */
+    private static void test1D(final Random rd, final int numPts, final boolean addErrors,
+            final double scaleTolerance, final double translationTolerance)
+    {
+        final double scale  = rd.nextDouble() * 30 - 12;
+        final double offset = rd.nextDouble() * 10 - 4;
+        final DirectPosition1D[] sources = new DirectPosition1D[numPts];
+        final DirectPosition1D[] targets = new DirectPosition1D[numPts];
+        for (int i=0; i<numPts; i++) {
+            final DirectPosition1D src = new DirectPosition1D(rd.nextDouble() * 100 - 50);
+            final DirectPosition1D tgt = new DirectPosition1D(src.ordinate * scale + offset);
+            if (addErrors) {
+                tgt.ordinate += rd.nextDouble() * 10 - 5;
+            }
+            sources[i] = src;
+            targets[i] = tgt;
+        }
+        /*
+         * Create the fitted transform to test.
+         */
+        final LinearTransformBuilder builder = new LinearTransformBuilder();
+        builder.setSourcePoints(sources);
+        builder.setTargetPoints(targets);
+        final Matrix m = builder.create().getMatrix();
+        assertEquals("m₀₀", scale,  m.getElement(0, 0), scaleTolerance);
+        assertEquals("m₀₁", offset, m.getElement(0, 1), translationTolerance);
+        assertEquals("correlation", 1, Math.abs(builder.correlation()[0]), scaleTolerance);
+    }
+
+    /**
+     * Implementation of {@link #testExact2D()} and {@link #testNonExact2D()}.
      *
      * @param rd        The random number generator to use.
      * @param numPts    The number of points to generate.
      * @param addErrors {@code true} for adding a random error in the target points.
      * @param scaleTolerance Tolerance threshold for floating point comparisons.
      */
-    private static void test(final Random rd, final int numPts, final boolean addErrors,
+    private static void test2D(final Random rd, final int numPts, final boolean addErrors,
             final double scaleTolerance, final double translationTolerance)
     {
         /*
@@ -148,7 +227,6 @@ public final strictfp class LinearTransf
         /*
          * Compare the coefficients with the reference implementation.
          */
-        double r;
         assertEquals("m₀₀", ref.getScaleX(),     m.getElement(0, 0), scaleTolerance);
         assertEquals("m₀₁", ref.getShearX(),     m.getElement(0, 1), scaleTolerance);
         assertEquals("m₀₂", ref.getTranslateX(), m.getElement(0, 2), translationTolerance);

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Line.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Line.java?rev=1654423&r1=1654422&r2=1654423&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Line.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Line.java [UTF-8]
Fri Jan 23 22:39:02 2015
@@ -44,6 +44,9 @@ import static java.lang.Double.*;
  * @since   0.5 (derived from geotk-1.0)
  * @version 0.5
  * @module
+ *
+ * @see Plane
+ * @see org.apache.sis.referencing.operation.builder.LinearTransformBuilder
  */
 public class Line implements Cloneable, Serializable {
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Plane.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Plane.java?rev=1654423&r1=1654422&r2=1654423&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Plane.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/math/Plane.java [UTF-8]
Fri Jan 23 22:39:02 2015
@@ -46,6 +46,9 @@ import static java.lang.Math.ulp;
  * @since   0.5 (derived from geotk-1.0)
  * @version 0.5
  * @module
+ *
+ * @see Line
+ * @see org.apache.sis.referencing.operation.builder.LinearTransformBuilder
  */
 public class Plane implements Cloneable, Serializable {
     /**
@@ -140,7 +143,7 @@ public class Plane implements Cloneable,
 
     /**
      * Returns the <var>z</var> value at (<var>x</var>,<var>y</var>)
= (0,0). This coefficient appears in the
-     * plane equation <var>sx</var>⋅<var>x</var> + <var>sy</var>⋅<var>y</var>
+ <b><u><var>z</var>₀</u></b>.
+     * plane equation <var>sx</var>⋅<var>x</var> + <var>sy</var>⋅<var>y</var>
+ <b><var>z</var>₀</b>.
      *
      * @return The <var>z</var>₀ term.
      *



Mime
View raw message