sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1599833 - in /sis/branches/JDK8/core/sis-referencing/src: main/java/org/apache/sis/internal/referencing/ test/java/org/apache/sis/referencing/operation/transform/ test/java/org/apache/sis/test/suite/
Date Tue, 03 Jun 2014 23:06:17 GMT
Author: desruisseaux
Date: Tue Jun  3 23:06:16 2014
New Revision: 1599833

URL: http://svn.apache.org/r1599833
Log:
Ported some tests.

Added:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/DirectPositionView.java
  (with props)
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/IterationStrategyTest.java
  (with props)
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PseudoTransform.java
  (with props)
Modified:
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java

Added: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/DirectPositionView.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/DirectPositionView.java?rev=1599833&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/DirectPositionView.java
(added)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/DirectPositionView.java
[UTF-8] Tue Jun  3 23:06:16 2014
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.referencing;
+
+import java.util.Arrays;
+import org.opengis.geometry.DirectPosition;
+import org.opengis.geometry.UnmodifiableGeometryException;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+
+
+/**
+ * A read-only direct position wrapping an array without performing any copy.
+ * This class shall be used for temporary objects only (it is not serializable for this reason).
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.5 (derived from geotk-3.20)
+ * @version 0.5
+ * @module
+ */
+public final class DirectPositionView implements DirectPosition {
+    /**
+     * The ordinate values. This is a direct reference to the array given to the constructor.
+     * The length of this array may be greater then the number of dimensions.
+     */
+    private final double[] ordinates;
+
+    /**
+     * The index of the first value in the {@linkplain #ordinates} array.
+     * This field is non-final in order to allow the caller to move the view over an array
of coordinates.
+     */
+    public int offset;
+
+    /**
+     * The number of valid ordinate values.
+     */
+    private final int dimension;
+
+    /**
+     * Creates a new direct position wrapping the given array.
+     *
+     * @param ordinates The ordinate values.
+     * @param offset    The first value index in the ordinates array.
+     * @param dimension The number of valid ordinate values.
+     */
+    public DirectPositionView(final double[] ordinates, final int offset, final int dimension)
{
+        this.ordinates = ordinates;
+        this.offset    = offset;
+        this.dimension = dimension;
+    }
+
+    /**
+     * Returns {@code null} since there is no CRS associated with this position.
+     */
+    @Override
+    public CoordinateReferenceSystem getCoordinateReferenceSystem() {
+        return null;
+    }
+
+    /**
+     * Returns the dimension given at construction time.
+     */
+    @Override
+    public int getDimension() {
+        return dimension;
+    }
+
+    /**
+     * Returns all ordinate values.
+     */
+    @Override
+    public double[] getCoordinate() {
+        return Arrays.copyOfRange(ordinates, offset, offset + dimension);
+    }
+
+    /**
+     * Returns the ordinate at the given index.
+     * <strong>This implementation does not check index validity</strong>, unless
assertions are enabled.
+     *
+     * @param dim The dimension of the ordinate to get fetch.
+     */
+    @Override
+    public double getOrdinate(final int dim) {
+        assert dim >= 0 && dim < dimension : dim;
+        return ordinates[offset + dim];
+    }
+
+    /**
+     * Do not allow any change.
+     */
+    @Override
+    public void setOrdinate(final int dimension, final double value) {
+        throw new UnmodifiableGeometryException();
+    }
+
+    /**
+     * Returns the direct position, which is {@code this}.
+     */
+    @Override
+    public DirectPosition getDirectPosition() {
+        return this;
+    }
+}

Propchange: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/DirectPositionView.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/DirectPositionView.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Added: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/IterationStrategyTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/IterationStrategyTest.java?rev=1599833&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/IterationStrategyTest.java
(added)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/IterationStrategyTest.java
[UTF-8] Tue Jun  3 23:06:16 2014
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.referencing.operation.transform;
+
+import java.util.Arrays;
+import java.util.Random;
+import org.opengis.referencing.operation.TransformException;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static java.lang.StrictMath.*;
+import static org.apache.sis.referencing.operation.transform.IterationStrategy.*;
+
+
+/**
+ * Tests the {@link IterationStrategy} class.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.5 (derived from geotk-3.00)
+ * @version 0.5
+ * @module
+ */
+public final strictfp class IterationStrategyTest extends TestCase {
+    /**
+     * Maximum number of dimension tested. The referencing module should be able to handle
high
+     * numbers, but we stick to low one in order to avoid making the test to long to execute.
+     */
+    private static final int MAX_DIMENSION = 6;
+
+    /**
+     * Maximum offset to test. The referencing module should be able to handle high numbers,
+     * but we stick to low one in order to avoid making the test to long to execute.
+     */
+    private static final int MAX_OFFSET = 20;
+
+    /**
+     * Tests {@link IterationStrategy#suggest(int, int, int, int, int)} with a few basic
cases,
+     * comparing the computed value with the expected value.
+     */
+    @Test
+    public void testSuggest() {
+        assertEquals("Target replace source.", ASCENDING,  suggest(0, 1, 0, 1, 240));
+        assertEquals("Target before source.",  ASCENDING,  suggest(1, 1, 0, 1, 239));
+        assertEquals("Target after source.",   DESCENDING, suggest(0, 1, 1, 1, 239));
+        assertEquals("Overlaps.",           BUFFER_TARGET, suggest(0, 2, 1, 1, 120));
+        assertEquals("Overlaps.",           BUFFER_SOURCE, suggest(1, 1, 0, 2, 120));
+    }
+
+    /**
+     * An empirical test making sure that the target subarray didn't overwrote the
+     * source subarray while a transformation was in progress.
+     *
+     * @throws TransformException Should never occur.
+     */
+    @Test
+    @DependsOnMethod("testSuggest")
+    public void empiricalTest() throws TransformException {
+        final int[] statistics = new int[4];
+        /*
+         * The length of the above array is hard-coded on purpose, as a reminder that if
we
+         * need to augment this value, then we need to augment the statistic checks at the
+         * end of this method as well.
+         */
+        final int length = (2*MAX_OFFSET) * MAX_DIMENSION;
+        final double[] sourcePts = new double[length];
+        final double[] targetPts = new double[length];
+        final double[] sharedPts = new double[length];
+        final Random random = new Random(650268926);
+        for (int i=0; i<length; i++) {
+            sourcePts[i] = random.nextDouble();
+        }
+        final int checksum = Arrays.hashCode(sourcePts);
+        for (int sourceDimension=1; sourceDimension<=MAX_DIMENSION; sourceDimension++)
{
+            for (int targetDimension=1; targetDimension<=MAX_DIMENSION; targetDimension++)
{
+                final PseudoTransform tr = new PseudoTransform(sourceDimension, targetDimension);
+                for (int srcOff=0; srcOff<=MAX_OFFSET; srcOff++) {
+                    for (int dstOff=0; dstOff<=MAX_OFFSET; dstOff++) {
+                        final int numPts = min((length-srcOff) / sourceDimension,
+                                               (length-dstOff) / targetDimension);
+                        final IterationStrategy strategy = IterationStrategy.suggest(
+                                srcOff, sourceDimension, dstOff, targetDimension, numPts);
+                        statistics[strategy.ordinal()]++;
+                        Arrays.fill(targetPts, Double.NaN);
+                        System.arraycopy(sourcePts, 0, sharedPts, 0, length);
+                        tr.transform(sharedPts, srcOff, sharedPts, dstOff, numPts);
+                        tr.transform(sourcePts, srcOff, targetPts, dstOff, numPts);
+                        assertEquals("Source points have been modified.", checksum, Arrays.hashCode(sourcePts));
+                        final int stop = dstOff + numPts * targetDimension;
+                        for (int i=dstOff; i<stop; i++) {
+                            final double expected = targetPts[i];
+                            final double actual   = sharedPts[i];
+                            if (actual != expected) {
+                                final int index = i - dstOff;
+                                fail("Transform" +
+                                     "(srcOff=" + srcOff + " srcDim=" + sourceDimension +
+                                     " dstOff=" + dstOff + " dstDim=" + targetDimension +
+                                     " numPts=" + numPts + ") using strategy " + strategy
+
+                                     ": for point " + (index / targetDimension) +
+                                     " at dimension " + (index % targetDimension) +
+                                     ", expected " + expected + " but got " + actual);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        int sum = 0;
+        for (int i=0; i<statistics.length; i++) {
+            sum += statistics[i];
+        }
+        assertEquals(MAX_DIMENSION * MAX_DIMENSION * (MAX_OFFSET+1) * (MAX_OFFSET+1), sum);
+        /*
+         * The following statistics were determined empirically at the time we wrote this
test,
+         * right after having debugged IterationStrategy. They depend on the value of MAX_OFFSET
+         * and MAX_DIMENSION constants, but do not depend on the random number generation.
The
+         * sum checked just before this comment verifies partially that assertion.
+         *
+         * If we assume that the calculation done by IterationStrategy was optimal at the
time
+         * we wrote it, then the statistics below should not change for current MAX_* setting.
+         * If those values change because of some algorithm change, then ASCENDING + DESCENDING
+         * count should increase while the BUFFER_SOURCE + BUFFER_TARGET count should decrease,
+         * otherwise it would not be an improvement.
+         */
+        assertEquals(4851, statistics[ASCENDING    .ordinal()]);
+        assertEquals(4410, statistics[DESCENDING   .ordinal()]);
+        assertEquals(3465, statistics[BUFFER_SOURCE.ordinal()]);
+        assertEquals(3150, statistics[BUFFER_TARGET.ordinal()]);
+    }
+}

Propchange: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/IterationStrategyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/IterationStrategyTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Added: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PseudoTransform.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PseudoTransform.java?rev=1599833&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PseudoTransform.java
(added)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PseudoTransform.java
[UTF-8] Tue Jun  3 23:06:16 2014
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.referencing.operation.transform;
+
+import org.opengis.referencing.operation.Matrix;
+import org.opengis.referencing.operation.TransformException;
+import org.apache.sis.internal.referencing.DirectPositionView;
+
+import static java.lang.StrictMath.*;
+
+
+/**
+ * A pseudo-transform for debugging purpose. The input points can be random numbers between
0 and 1.
+ * The transformed points are build as below (when formatted in base 10):
+ *
+ * {@preformat text
+ *     [1 digit for dimension] [3 first fraction digits] . [random digits from source]
+ * }
+ *
+ * For example if the first input coordinate is (0.2, 0.5, 0.3), then the transformed coordinate
will be:
+ *
+ * {@preformat text
+ *     1002.2
+ *     2005.5
+ *     3003.3
+ * }
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.5 (derived from geotk-3.00)
+ * @version 0.5
+ * @module
+ */
+strictfp class PseudoTransform extends AbstractMathTransform {
+    /**
+     * The source and target dimensions.
+     */
+    protected final int sourceDimension, targetDimension;
+
+    /**
+     * Temporary buffer for copying the ordinate of a single source points.
+     * Used in order to be compliant with {@link IterationStrategy} contract.
+     */
+    private final double[] buffer;
+
+    /**
+     * Creates a transform for the given dimensions.
+     *
+     * @param sourceDimension The source dimension.
+     * @param targetDimension The target dimension.
+     */
+    public PseudoTransform(final int sourceDimension, final int targetDimension) {
+        this.sourceDimension = sourceDimension;
+        this.targetDimension = targetDimension;
+        this.buffer = new double[sourceDimension];
+    }
+
+    /**
+     * Returns the source dimension.
+     */
+    @Override
+    public int getSourceDimensions() {
+        return sourceDimension;
+    }
+
+    /**
+     * Returns the target dimension.
+     */
+    @Override
+    public int getTargetDimensions() {
+        return targetDimension;
+    }
+
+    /**
+     * Pseudo-transform a point in the given array.
+     *
+     * @throws TransformException should never occurs in this class,
+     *         but can occur in method overridden in subclasses.
+     */
+    @Override
+    public Matrix transform(final double[] srcPts, final int srcOff,
+                            final double[] dstPts, final int dstOff,
+                            final boolean derivate) throws TransformException
+    {
+        final Matrix derivative = derivate ? derivative(
+                new DirectPositionView(srcPts, srcOff, getSourceDimensions())) : null;
+        System.arraycopy(srcPts, srcOff, buffer, 0, sourceDimension);
+        for (int i=0; i<targetDimension; i++) {
+            double v = buffer[i % sourceDimension];
+            v += (i+1)*1000 + round(v * 1000);
+            dstPts[dstOff + i] = v;
+        }
+        return derivative;
+    }
+}

Propchange: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PseudoTransform.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PseudoTransform.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java?rev=1599833&r1=1599832&r2=1599833&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] Tue Jun  3 23:06:16 2014
@@ -40,6 +40,7 @@ import org.junit.BeforeClass;
     org.apache.sis.referencing.operation.matrix.NonSquareMatrixTest.class, // Expected to
be last MatrixTestCase - see javadoc.
     org.apache.sis.referencing.operation.matrix.MatricesTest.class,
     org.apache.sis.referencing.operation.matrix.AffineTransforms2DTest.class,
+    org.apache.sis.referencing.operation.transform.IterationStrategyTest.class,
 
     org.apache.sis.internal.referencing.FormulasTest.class,
     org.apache.sis.internal.referencing.VerticalDatumTypesTest.class,
@@ -60,6 +61,7 @@ import org.junit.BeforeClass;
     org.apache.sis.parameter.ParameterFormatTest.class,
     org.apache.sis.parameter.TensorParametersTest.class,
     org.apache.sis.parameter.TensorValuesTest.class,
+
     org.apache.sis.referencing.datum.BursaWolfParametersTest.class,
     org.apache.sis.referencing.datum.TimeDependentBWPTest.class,
     org.apache.sis.referencing.datum.DefaultEllipsoidTest.class,



Mime
View raw message