sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: Correction in the way to compute the `gridToCRS` of target grid geometry.
Date Mon, 30 Mar 2020 10:35:59 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 4fe9e7e  Correction in the way to compute the `gridToCRS` of target grid geometry.
4fe9e7e is described below

commit 4fe9e7e5b0b4ddde6167bffd8c839952e7808842
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Mar 30 12:35:03 2020 +0200

    Correction in the way to compute the `gridToCRS` of target grid geometry.
---
 .../sis/coverage/grid/ResampledGridCoverage.java   | 13 +++++----
 .../sis/referencing/operation/matrix/Matrix1.java  |  9 ++++---
 .../sis/referencing/operation/matrix/Matrix2.java  | 12 ---------
 .../referencing/operation/matrix/MatrixSIS.java    | 28 ++++++++++---------
 .../operation/matrix/MatrixTestCase.java           | 31 +++++++++++++++-------
 5 files changed, 50 insertions(+), 43 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ResampledGridCoverage.java
b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ResampledGridCoverage.java
index 160bce5..3b2a1b1 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ResampledGridCoverage.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/coverage/grid/ResampledGridCoverage.java
@@ -36,6 +36,7 @@ import org.apache.sis.geometry.GeneralEnvelope;
 import org.apache.sis.internal.util.DoubleDouble;
 import org.apache.sis.internal.coverage.j2d.ImageUtilities;
 import org.apache.sis.internal.referencing.DirectPositionView;
+import org.apache.sis.internal.referencing.ExtendedPrecisionMatrix;
 import org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox;
 import org.apache.sis.referencing.operation.transform.MathTransforms;
 import org.apache.sis.referencing.operation.matrix.MatrixSIS;
@@ -204,11 +205,11 @@ final class ResampledGridCoverage extends GridCoverage {
              * dimensions than magnitude values (unusual, but not forbidden), some grid dimensions
will be ignored
              * provided that their size is 1 (otherwise a SubspaceNotSpecifiedException is
thrown).
              */
-            final double[]  magnitudes = vectors.normalizeColumns();          // Length is
dimension  of source grid.
+            final MatrixSIS magnitudes = vectors.normalizeColumns();          // Length is
dimension  of source grid.
             final int       crsDim     = vectors.getNumRow();                 // Number of
dimensions of target CRS.
             final int       gridDim    = target.getDimension();               // Number of
dimensions of target grid.
-            final int       mappedDim  = Math.min(magnitudes.length, Math.min(crsDim, gridDim));
-            final MatrixSIS crsToGrid  = Matrices.createZero(gridDim + 1, crsDim + 1);
+            final int       mappedDim  = Math.min(magnitudes.getNumCol(), Math.min(crsDim,
gridDim));
+            final MatrixSIS crsToGrid  = Matrices.create(gridDim + 1, crsDim + 1, ExtendedPrecisionMatrix.ZERO);
             final int[]     dimSelect  = (gridDim > crsDim && targetExtent !=
null) ?
                                          targetExtent.getSubspaceDimensions(crsDim) : null;
             /*
@@ -248,7 +249,9 @@ final class ResampledGridCoverage extends GridCoverage {
                 for (int j=0; j<crsDim; j++) {
                     vectors.setElement(j, tgDim, 0);    // For preventing this column to
be selected again.
                 }
-                crsToGrid.setElement(tgDim, tcDim, magnitudes[tgDim]);
+                final DoubleDouble m = DoubleDouble.castOrCopy(magnitudes.getNumber(0, tgDim));
+                m.inverseDivide(1);
+                crsToGrid.setNumber(tgDim, tcDim, m);
             }
             crsToGrid.setElement(gridDim, crsDim, 1);
             /*
@@ -261,7 +264,7 @@ final class ResampledGridCoverage extends GridCoverage {
                 // Create an extent of same size but with lower coordinates set to 0.
                 final long[] coordinates = new long[gridDim * 2];
                 for (int i=0; i<gridDim; i++) {
-                    coordinates[i + gridDim] = tentative.getSize(i);
+                    coordinates[i + gridDim] = tentative.getSize(i) - 1;
                 }
                 targetExtent = new GridExtent(tentative, coordinates);
             }
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
index 49bdf2b..33a6445 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix1.java
@@ -251,14 +251,15 @@ public class Matrix1 extends MatrixSIS {
 
     /**
      * Normalizes all columns in-place.
-     * For a 1×1 matrix, this method just sets unconditionally the {@link #m00} value to
1.
+     * For a 1×1 matrix with non-NaN value, this method sets the {@link #m00} value
+     * to +1, -1 or 0 with the same sign than the original value.
      *
      * @return the magnitude of the column, which is the absolute value of {@link #m00}.
      */
     @Override
-    public double[] normalizeColumns() {
-        final double[] magnitudes = new double[] {Math.abs(m00)};
-        m00 = 1;
+    public MatrixSIS normalizeColumns() {
+        final MatrixSIS magnitudes = new Matrix1(Math.abs(m00));
+        m00 = Math.signum(m00);
         return magnitudes;
     }
 
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix2.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix2.java
index a717530..3614806 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix2.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrix2.java
@@ -276,18 +276,6 @@ public class Matrix2 extends MatrixSIS {
      * {@inheritDoc}
      */
     @Override
-    public double[] normalizeColumns() {
-        final double[] magnitudes = new double[2];
-        double m;
-        magnitudes[0] = m = Math.hypot(m00, m10); m00 /= m; m10 /= m;
-        magnitudes[1] = m = Math.hypot(m01, m11); m01 /= m; m11 /= m;
-        return magnitudes;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
     public Matrix2 clone() {
         return (Matrix2) super.clone();
     }
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
index a34112a..26389e8 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/MatrixSIS.java
@@ -411,10 +411,10 @@ public abstract class MatrixSIS implements Matrix, LenientComparable,
Cloneable,
     public abstract void transpose();
 
     /**
-     * Normalizes all columns in-place. Each columns in this matrix is considered as a vector.
-     * For each column (vector), this method computes the magnitude (vector length) as the
square
-     * root of the sum of all square values. Then, all values in the column are divided by
that
-     * magnitude.
+     * Normalizes all columns in-place and returns their magnitudes as a row vector.
+     * Each columns in this matrix is considered as a vector. For each column (vector),
+     * this method computes the magnitude (vector length) as the square root of the sum of
all square values.
+     * Then, all values in the column are divided by that magnitude.
      *
      * <p>This method is useful when the matrix is a
      * {@linkplain org.opengis.referencing.operation.MathTransform#derivative transform derivative}.
@@ -422,13 +422,13 @@ public abstract class MatrixSIS implements Matrix, LenientComparable,
Cloneable,
      * coordinate in the source space is increased by one. Invoking this method turns those
vectors
      * into unitary vectors, which is useful for forming the basis of a new coordinate system.</p>
      *
-     * @return the magnitude for each column. The length of this array is the number of columns.
+     * @return the magnitude for each column in a matrix having only one row.
      * @throws UnsupportedOperationException if this matrix is unmodifiable.
      */
-    public double[] normalizeColumns() {
+    public MatrixSIS normalizeColumns() {
         final int numRow = getNumRow();
         final int numCol = getNumCol();
-        final double[] magnitudes = new double[numCol];
+        final MatrixSIS magnitudes = new NonSquareMatrix(1, numCol, false, 2);
         final DoubleDouble sum = new DoubleDouble();
         final DoubleDouble dot = new DoubleDouble();
         final DoubleDouble tmp = new DoubleDouble();
@@ -440,13 +440,15 @@ public abstract class MatrixSIS implements Matrix, LenientComparable,
Cloneable,
                 sum.add(dot);
             }
             sum.sqrt();
-            for (int j=0; j<numRow; j++) {
-                get(j, i, tmp);
-                dot.setFrom(sum);
-                dot.inverseDivide(tmp);
-                set(j, i, dot);
+            if (!sum.isZero()) {
+                for (int j=0; j<numRow; j++) {
+                    get(j, i, tmp);
+                    dot.setFrom(sum);
+                    dot.inverseDivide(tmp);
+                    set(j, i, dot);
+                }
+                magnitudes.setNumber(0, i, sum);
             }
-            magnitudes[i] = sum.doubleValue();
         }
         return magnitudes;
     }
diff --git a/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
b/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
index 81173bc..749ddb1 100644
--- a/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
+++ b/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
@@ -45,7 +45,7 @@ import static org.apache.sis.test.Assert.*;
  * <p>This class uses <a href="http://math.nist.gov/javanumerics/jama">JAMA</a>
as the reference implementation.</p>
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
  * @since   0.4
  * @module
  */
@@ -378,16 +378,29 @@ public abstract strictfp class MatrixTestCase extends TestCase {
         final double[] elements = createRandomPositiveValues(numRow * numCol);
         final MatrixSIS matrix = Matrices.create(numRow, numCol, elements);
         validate(matrix);
-        matrix.normalizeColumns();
+        final double[] expected = new double[numCol];
         for (int i=0; i<numCol; i++) {
-            double m = 0;
-            for (int j=0; j<numRow; j++) {
-                final double e = matrix.getElement(j, i);
-                m += e*e;
-            }
-            m = StrictMath.sqrt(m);
-            assertEquals(1, m, 1E-12);
+            expected[i] = magnitude(matrix, i, numRow);
+        }
+        assertArrayEquals(expected, matrix.normalizeColumns().getElements(), 1E-12);
+        /*
+         * Array elements after normalization shall be unit vectors.
+         */
+        for (int i=0; i<numCol; i++) {
+            assertEquals(1, magnitude(matrix, i, numRow), 1E-12);
+        }
+    }
+
+    /**
+     * Computes the magnitude of vector in column <var>i</var>.
+     */
+    private static double magnitude(final MatrixSIS matrix, final int i, final int numRow)
{
+        double sum = 0;
+        for (int j=0; j<numRow; j++) {
+            final double e = matrix.getElement(j, i);
+            sum += e*e;
         }
+        return StrictMath.sqrt(sum);
     }
 
     /**


Mime
View raw message