sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 02/03: Slightly more efficient way to get wraparound step in a chain of transforms.
Date Fri, 02 Oct 2020 19:09:27 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

commit 7ce4d0b619c0703bd39db818762d13ba139158d8
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Oct 2 13:01:47 2020 +0200

    Slightly more efficient way to get wraparound step in a chain of transforms.
---
 .../internal/referencing/WraparoundInEnvelope.java | 22 +---------
 .../operation/transform/WraparoundTransform.java   | 47 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 21 deletions(-)

diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/WraparoundInEnvelope.java
b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/WraparoundInEnvelope.java
index a3da922..f45e038 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/WraparoundInEnvelope.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/WraparoundInEnvelope.java
@@ -19,12 +19,10 @@ package org.apache.sis.internal.referencing;
 import org.opengis.geometry.Envelope;
 import org.opengis.referencing.operation.MathTransform;
 import org.opengis.referencing.operation.TransformException;
-import org.apache.sis.referencing.operation.transform.MathTransforms;
 import org.apache.sis.referencing.operation.transform.WraparoundTransform;
 import org.apache.sis.geometry.Envelopes;
 import org.apache.sis.geometry.GeneralEnvelope;
 import org.apache.sis.internal.util.Numerics;
-import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.ComparisonMode;
 
 
@@ -203,24 +201,6 @@ public final class WraparoundInEnvelope extends WraparoundTransform {
     }
 
     /**
-     * Returns all instances of {@link WraparoundInEnvelope}, or {@code null} if none.
-     * If non-null, the returned array usually contains only one instance.
-     */
-    private static WraparoundInEnvelope[] wraparounds(final MathTransform transform) {
-        WraparoundInEnvelope[] wraparounds = null;
-        for (final MathTransform t : MathTransforms.getSteps(transform)) {
-            if (t instanceof WraparoundInEnvelope) {
-                if (wraparounds == null) {
-                    wraparounds = new WraparoundInEnvelope[] {(WraparoundInEnvelope) t};
-                } else {
-                    wraparounds = ArraysExt.append(wraparounds, (WraparoundInEnvelope) t);
-                }
-            }
-        }
-        return wraparounds;
-    }
-
-    /**
      * Transforms an envelope using the given math transform with special checks for wraparounds.
      * The transformation is only approximated: the returned envelope may be bigger than
necessary.
      *
@@ -235,7 +215,7 @@ public final class WraparoundInEnvelope extends WraparoundTransform {
      * @throws TransformException if a transform failed.
      */
     public static GeneralEnvelope transform(final MathTransform transform, final Envelope
envelope) throws TransformException {
-        final WraparoundInEnvelope[] wraparounds = wraparounds(transform);
+        final WraparoundInEnvelope[] wraparounds = getSteps(WraparoundInEnvelope.class, transform);
         if (wraparounds == null) {
             return Envelopes.transform(transform, envelope);
         }
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/WraparoundTransform.java
b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/WraparoundTransform.java
index 2ebafc7..15e2f5a 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/WraparoundTransform.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/WraparoundTransform.java
@@ -18,6 +18,7 @@ package org.apache.sis.referencing.operation.transform;
 
 import java.util.List;
 import java.io.Serializable;
+import java.lang.reflect.Array;
 import org.opengis.util.FactoryException;
 import org.opengis.geometry.DirectPosition;
 import org.opengis.parameter.ParameterValueGroup;
@@ -33,6 +34,7 @@ import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.measure.Longitude;
 import org.apache.sis.parameter.Parameters;
+import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.ComparisonMode;
 import org.apache.sis.util.logging.Logging;
@@ -503,6 +505,51 @@ public class WraparoundTransform extends AbstractMathTransform implements
Serial
     }
 
     /**
+     * Returns all {@link WraparoundTransform} instances of the given type, or {@code null}
if none.
+     * Invoking this method is equivalent to invoking {@link MathTransforms#getSteps(MathTransform)}
+     * and filter the list for retaining only instances of the given {@code type}.
+     *
+     * <div class="note"><b>Rational:</b>
+     * this specialized method is provided for efficiency because the returned array, if
non-null,
+     * usually contains only one instance. Callers may be interested in a specific subclass
of
+     * {@link WraparoundTransform} for finer control of wraparound operations,
+     * for example if using a specialized subclass for envelope transformation.</div>
+     *
+     * @param  <T>        compile-time value of {@code type} argument.
+     * @param  type       {@link WraparoundTransform} subclass to include in the returned
array.
+     * @param  transform  the transform for which to get {@link WraparoundTransform} steps.
+     * @return {@link WraparoundTransform} steps in a non-empty array, or {@code null} if
none.
+     *
+     * @see MathTransforms#getSteps(MathTransform)
+     */
+    public static <T extends WraparoundTransform> T[] getSteps(final Class<T>
type, final MathTransform transform) {
+        ArgumentChecks.ensureNonNull("type", type);
+        return getSteps(type, transform, null);
+    }
+
+    /**
+     * Implementation of {@link #getSteps(Class, MathTransform)} to be invoked recursively.
+     */
+    @SuppressWarnings("unchecked")
+    private static <T extends WraparoundTransform> T[] getSteps(
+            final Class<T> type, final MathTransform transform, T[] wraparounds)
+    {
+        if (type.isInstance(transform)) {
+            if (wraparounds == null) {
+                wraparounds = (T[]) Array.newInstance(type, 1);
+                wraparounds[0] = (T) transform;
+            } else {
+                wraparounds = ArraysExt.append(wraparounds, (T) transform);
+            }
+        } else if (transform instanceof ConcatenatedTransform) {
+            final ConcatenatedTransform ct = (ConcatenatedTransform) transform;
+            wraparounds = getSteps(type, ct.transform1, wraparounds);
+            wraparounds = getSteps(type, ct.transform2, wraparounds);
+        }
+        return wraparounds;
+    }
+
+    /**
      * Returns the parameter descriptors for this math transform.
      *
      * @return the parameter descriptors for this math transform.


Mime
View raw message