You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2015/10/28 19:23:58 UTC

svn commit: r1711093 - in /sis/branches/JDK8/core/sis-referencing/src: main/java/org/apache/sis/referencing/operation/matrix/ main/java/org/apache/sis/referencing/operation/transform/ test/java/org/apache/sis/referencing/operation/transform/

Author: desruisseaux
Date: Wed Oct 28 18:23:57 2015
New Revision: 1711093

URL: http://svn.apache.org/viewvc?rev=1711093&view=rev
Log:
Added test for the derivative of Geographic/Geocentric conversion, with bug fix in the two-dimensional geographic case.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransform.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransformTest.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java?rev=1711093&r1=1711092&r2=1711093&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java [UTF-8] Wed Oct 28 18:23:57 2015
@@ -650,6 +650,67 @@ public final class Matrices extends Stat
     }
 
     /**
+     * Returns a new matrix with the same elements than the given matrix except for a row which have been omitted.
+     * This method is useful for removing a <em>target</em> dimension in an affine transform.
+     *
+     * @param  matrix The matrix where to remove a row, or {@code null}.
+     * @param  row Index of the row to remove.
+     * @return A copy of the given matrix with the specified row removed, or {@code null} if the given matrix was null.
+     *
+     * @since 0.7
+     */
+    public static Matrix removeRow(final Matrix matrix, final int row) {
+        if (matrix == null) {
+            return null;
+        }
+        final int numRow = matrix.getNumRow();
+        final int numCol = matrix.getNumCol();
+        ArgumentChecks.ensureBetween("row", 0, numRow-1, row);
+        final Matrix reduced = createZero(numRow - 1, numCol);
+        int dest = 0;
+        for (int j=0; j<numRow; j++) {
+            if (j != row) {
+                for (int i=0; i<numCol; i++) {
+                    reduced.setElement(dest, i, matrix.getElement(j, i));
+                }
+                dest++;
+            }
+        }
+        return reduced;
+    }
+
+    /**
+     * Returns a new matrix with the same elements than the given matrix except for a column which have been omitted.
+     * This method is useful for removing a <em>source</em> dimension in an affine transform. Coordinate values of
+     * the removed dimension will be presumed zero.
+     *
+     * @param  matrix The matrix where to remove a column, or {@code null}.
+     * @param  col Index of the column to remove.
+     * @return A copy of the given matrix with the specified column removed, or {@code null} if the given matrix was null.
+     *
+     * @since 0.7
+     */
+    public static Matrix removeColumn(final Matrix matrix, final int col) {
+        if (matrix == null) {
+            return null;
+        }
+        final int numRow = matrix.getNumRow();
+        final int numCol = matrix.getNumCol();
+        ArgumentChecks.ensureBetween("col", 0, numCol-1, col);
+        final Matrix reduced = createZero(numRow, numCol - 1);
+        int dest = 0;
+        for (int i=0; i<numCol; i++) {
+            if (i != col) {
+                for (int j=0; j<numRow; j++) {
+                    reduced.setElement(j, dest, matrix.getElement(j, i));
+                }
+                dest++;
+            }
+        }
+        return reduced;
+    }
+
+    /**
      * Creates a new matrix which is a copy of the given matrix.
      *
      * <div class="note"><b>Implementation note:</b>

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransform.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransform.java?rev=1711093&r1=1711092&r2=1711093&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransform.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransform.java [UTF-8] Wed Oct 28 18:23:57 2015
@@ -21,6 +21,7 @@ import java.io.Serializable;
 import javax.measure.unit.Unit;
 import javax.measure.quantity.Length;
 import org.opengis.util.FactoryException;
+import org.opengis.geometry.DirectPosition;
 import org.opengis.parameter.ParameterValueGroup;
 import org.opengis.parameter.ParameterDescriptorGroup;
 import org.opengis.referencing.datum.Ellipsoid;
@@ -367,6 +368,17 @@ public class EllipsoidalToCartesianTrans
                             final double[] dstPts, final int dstOff,
                             final boolean derivate)
     {
+        return transform(srcPts, srcOff, dstPts, dstOff, derivate, withHeight);
+    }
+
+    /**
+     * Implementation of {@link #transform(double[], int, double[], int, boolean)}
+     * with possibility to override the {@link #withHeight} value.
+     */
+    final Matrix transform(final double[] srcPts, final int srcOff,
+                           final double[] dstPts, final int dstOff,
+                           final boolean derivate, final boolean withHeight)
+    {
         final double λ    = srcPts[srcOff  ];                            // Longitude (radians)
         final double φ    = srcPts[srcOff+1];                            // Latitude (radians)
         final double h    = withHeight ? srcPts[srcOff+2] : 0;           // Height above the ellipsoid
@@ -622,6 +634,19 @@ next:   while (--numPts >= 0) {
         }
 
         /**
+         * Compute the derivative at the given location. We need to override this method because
+         * we will inverse 3×2 matrices in a special way, with the knowledge that <var>h</var>
+         * can be set to 0.
+         */
+        @Override
+        public Matrix derivative(final DirectPosition point) throws TransformException {
+            ArgumentChecks.ensureNonNull("point", point);
+            final double[] coordinate = point.getCoordinate();
+            ArgumentChecks.ensureDimensionMatches("point", 3, coordinate);
+            return this.transform(coordinate, 0, coordinate, 0, true);
+        }
+
+        /**
          * Inverse transforms a single coordinate in a list of ordinal values,
          * and optionally returns the derivative at that location.
          *
@@ -650,7 +675,11 @@ next:   while (--numPts >= 0) {
                 dstPts[dstOff  ] = point[0];
                 dstPts[dstOff+1] = point[1];
             }
-            return Matrices.inverse(EllipsoidalToCartesianTransform.this.transform(point, offset, null, 0, true));
+            Matrix matrix = Matrices.inverse(EllipsoidalToCartesianTransform.this.transform(point, offset, null, 0, true, true));
+            if (!withHeight) {
+                matrix = Matrices.removeRow(matrix, 2);     // Drop height
+            }
+            return matrix;
         }
 
         /**

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransformTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransformTest.java?rev=1711093&r1=1711092&r2=1711093&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransformTest.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/EllipsoidalToCartesianTransformTest.java [UTF-8] Wed Oct 28 18:23:57 2015
@@ -18,11 +18,15 @@ package org.apache.sis.referencing.opera
 
 import javax.measure.unit.SI;
 import org.opengis.util.FactoryException;
+import org.opengis.geometry.DirectPosition;
+import org.opengis.referencing.datum.Ellipsoid;
 import org.opengis.referencing.operation.MathTransformFactory;
 import org.opengis.referencing.operation.TransformException;
 import org.apache.sis.internal.system.DefaultFactories;
 import org.apache.sis.internal.referencing.Formulas;
 import org.apache.sis.referencing.CommonCRS;
+import org.apache.sis.geometry.DirectPosition2D;
+import org.apache.sis.geometry.GeneralDirectPosition;
 
 import static java.lang.StrictMath.toRadians;
 
@@ -125,4 +129,54 @@ public final strictfp class EllipsoidalT
         toleranceModifier = ToleranceModifier.PROJECTION;
         verifyInverse(40, 30, 10000);
     }
+
+    /**
+     * Executes the derivative test using the given ellipsoid.
+     *
+     * @param  ellipsoid The ellipsoid to use for the test.
+     * @param  hasHeight {@code true} if geographic coordinates include an ellipsoidal height (i.e. are 3-D),
+     *         or {@code false} if they are only 2-D.
+     * @throws TransformException Should never happen.
+     */
+    private void testDerivative(final Ellipsoid ellipsoid, final boolean hasHeight) throws TransformException {
+        transform = EllipsoidalToCartesianTransform.createGeodeticConversion(ellipsoid, hasHeight);
+        DirectPosition point = hasHeight ? new GeneralDirectPosition(-10, 40, 200) : new DirectPosition2D(-10, 40);
+        /*
+         * Derivative of the direct transform.
+         */
+        tolerance = 1E-2;
+        derivativeDeltas = new double[] {toRadians(1.0 / 60) / 1852}; // Approximatively one metre.
+        verifyDerivative(point.getCoordinate());
+        /*
+         * Derivative of the inverse transform.
+         */
+        point = transform.transform(point, null);
+        transform = transform.inverse();
+        tolerance = 1E-8;
+        derivativeDeltas = new double[] {1}; // Approximatively one metre.
+        verifyDerivative(point.getCoordinate());
+    }
+
+    /**
+     * Tests the {@link EllipsoidalToCartesianTransform#derivative(DirectPosition)} method on a sphere.
+     *
+     * @throws TransformException Should never happen.
+     */
+    @Test
+    public void testDerivativeOnSphere() throws TransformException {
+        testDerivative(CommonCRS.SPHERE.ellipsoid(), true);
+        testDerivative(CommonCRS.SPHERE.ellipsoid(), false);
+    }
+
+    /**
+     * Tests the {@link EllipsoidalToCartesianTransform#derivative(DirectPosition)} method on an ellipsoid.
+     *
+     * @throws TransformException Should never happen.
+     */
+    @Test
+    @DependsOnMethod("testDerivativeOnSphere")
+    public void testDerivative() throws TransformException {
+        testDerivative(CommonCRS.WGS84.ellipsoid(), true);
+        testDerivative(CommonCRS.WGS84.ellipsoid(), false);
+    }
 }