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: A LinearTransform between two PassThroughTransform instances should not block us from merging those two instances.
Date Thu, 14 Feb 2019 12:01:26 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 9318a74  A LinearTransform between two PassThroughTransform instances should not
block us from merging those two instances.
9318a74 is described below

commit 9318a74c7e81427ccd94dbb042af98f274ba0e63
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Thu Feb 14 12:59:58 2019 +0100

    A LinearTransform between two PassThroughTransform instances should not block us from
merging those two instances.
---
 .../operation/transform/MathTransforms.java        |  7 +---
 .../operation/transform/PassThroughTransform.java  | 47 ++++++++++++++++++----
 .../transform/PassThroughTransformTest.java        |  2 +-
 3 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
index 6dbed36..7544a93 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/MathTransforms.java
@@ -331,12 +331,7 @@ public final class MathTransforms extends Static {
                 return IdentityTransform.create(firstAffectedCoordinate + dimension + numTrailingCoordinates);
             }
         }
-        final Matrix matrix = getMatrix(subTransform);
-        if (matrix != null) {
-            return linear(PassThroughTransform.asMatrix(firstAffectedCoordinate, matrix,
numTrailingCoordinates));
-        } else {
-            return PassThroughTransform.newInstance(firstAffectedCoordinate, subTransform,
numTrailingCoordinates);
-        }
+        return PassThroughTransform.create0(firstAffectedCoordinate, subTransform, numTrailingCoordinates);
     }
 
     /**
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
index eedfe57..b989543 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/PassThroughTransform.java
@@ -160,13 +160,44 @@ public class PassThroughTransform extends AbstractMathTransform implements
Seria
         return MathTransforms.passThrough(firstAffectedCoordinate, subTransform, numTrailingCoordinates);
     }
 
+    /** TODO: rename create(…) and recycle above javadoc. */
+    static MathTransform create0(final int firstAffectedCoordinate, final MathTransform subTransform,
final int numTrailingCoordinates) {
+        Matrix matrix = MathTransforms.getMatrix(subTransform);
+        if (matrix != null) {
+            return newInstance(firstAffectedCoordinate, matrix, numTrailingCoordinates);
+        }
+        /*
+         * Above checks tried to avoid the creation of PassThroughTransform instance. At
this point we can not
+         * avoid it anymore. But maybe we can merge two PassThroughTransforms into a single
one. It may happen
+         * if 'subTransform' is a concatenation of a linear transform + pass through transform
(in any order).
+         * In such case, moving the linear transform outside 'subTransform' enable above-cited
merge.
+         */
+        if (subTransform instanceof ConcatenatedTransform) {
+            MathTransform transform1 = ((ConcatenatedTransform) subTransform).transform1;
+            MathTransform transform2 = ((ConcatenatedTransform) subTransform).transform2;
+            matrix = MathTransforms.getMatrix(transform1);
+            if (matrix != null && transform2 instanceof PassThroughTransform) {
+                transform1 = newInstance(firstAffectedCoordinate, matrix,     numTrailingCoordinates);
+                transform2 = newInstance(firstAffectedCoordinate, transform2, numTrailingCoordinates);
+                return MathTransforms.concatenate(transform1, transform2);
+            }
+            matrix = MathTransforms.getMatrix(transform2);
+            if (matrix != null && transform1 instanceof PassThroughTransform) {
+                transform1 = newInstance(firstAffectedCoordinate, transform1, numTrailingCoordinates);
+                transform2 = newInstance(firstAffectedCoordinate, matrix,     numTrailingCoordinates);
+                return MathTransforms.concatenate(transform1, transform2);
+            }
+        }
+        return newInstance(firstAffectedCoordinate, subTransform, numTrailingCoordinates);
+    }
+
     /**
      * Special case for transformation backed by a matrix. Is is possible to use a new matrix
for such transform,
      * instead of wrapping the sub-transform into a {@code PassThroughTransform} object.
It is faster and easier
      * to concatenate.
      */
-    static Matrix asMatrix(final int firstAffectedCoordinate, final Matrix subTransform,
final int numTrailingCoordinates) {
-        return expand(MatrixSIS.castOrCopy(subTransform), firstAffectedCoordinate, numTrailingCoordinates,
1);
+    private static LinearTransform newInstance(int firstAffectedCoordinate, Matrix subTransform,
int numTrailingCoordinates) {
+        return MathTransforms.linear(expand(MatrixSIS.castOrCopy(subTransform), firstAffectedCoordinate,
numTrailingCoordinates, 1));
     }
 
     /**
@@ -174,9 +205,9 @@ public class PassThroughTransform extends AbstractMathTransform implements
Seria
      * the constructor for the case where the sub-transform is already a {@code PassThroughTransform}.
      * It is caller's responsibility to ensure that the argument values are valid.
      */
-    static PassThroughTransform newInstance(final int firstAffectedCoordinate,
-                                            final MathTransform subTransform,
-                                            final int numTrailingCoordinates)
+    private static PassThroughTransform newInstance(final int firstAffectedCoordinate,
+                                                    final MathTransform subTransform,
+                                                    final int numTrailingCoordinates)
     {
         int dim = subTransform.getSourceDimensions();
         if (subTransform.getTargetDimensions() == dim) {
@@ -493,11 +524,11 @@ public class PassThroughTransform extends AbstractMathTransform implements
Seria
      * sub-transform can be expressed as a matrix. It is also invoked for computing the
      * matrix returned by {@link #derivative}.
      *
-     * @param subMatrix              the sub-transform as a matrix.
+     * @param subMatrix                the sub-transform as a matrix.
      * @param firstAffectedCoordinate  index of the first affected coordinate.
      * @param numTrailingCoordinates   number of trailing coordinates to pass through.
-     * @param affine                 0 if the matrix do not contains translation terms, or
1 if
-     *                               the matrix is an affine transform with translation terms.
+     * @param affine                   0 if the matrix do not contains translation terms,
or 1 if
+     *                                 the matrix is an affine transform with translation
terms.
      */
     private static Matrix expand(final MatrixSIS subMatrix,
                                  final int firstAffectedCoordinate,
diff --git a/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PassThroughTransformTest.java
b/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PassThroughTransformTest.java
index e1605da..1f34339 100644
--- a/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PassThroughTransformTest.java
+++ b/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PassThroughTransformTest.java
@@ -264,7 +264,7 @@ public final strictfp class PassThroughTransformTest extends MathTransformTestCa
      */
     @Test
     public void testTryConcatenate() throws FactoryException {
-        PassThroughTransform ps = PassThroughTransform.newInstance(2, new PseudoTransform(2,
3), 2);
+        PassThroughTransform ps = new PassThroughTransform(2, new PseudoTransform(2, 3),
2);
         MathTransform c = ps.tryConcatenate(false, MathTransforms.linear(Matrices.create(4,
8, new double[] {
                 0, 1, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 1, 0, 0, 0,


Mime
View raw message