sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1605055 - in /sis/branches/JDK8/core/sis-referencing/src: main/java/org/apache/sis/referencing/operation/transform/ test/java/org/apache/sis/referencing/operation/transform/ test/java/org/apache/sis/test/suite/
Date Tue, 24 Jun 2014 11:10:50 GMT
Author: desruisseaux
Date: Tue Jun 24 11:10:50 2014
New Revision: 1605055

URL: http://svn.apache.org/r1605055
Log:
Completed and added tests for TransferFunction.

Added:
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/TransferFunctionTest.java
  (with props)
Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LogarithmicTransform1D.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransferFunction.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/package-info.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PassThroughTransformTest.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LogarithmicTransform1D.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LogarithmicTransform1D.java?rev=1605055&r1=1605054&r2=1605055&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LogarithmicTransform1D.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/LogarithmicTransform1D.java
[UTF-8] Tue Jun 24 11:10:50 2014
@@ -112,7 +112,7 @@ class LogarithmicTransform1D extends Abs
      */
     public static MathTransform1D create(final double base, final double offset) {
         if (base == 10) {
-            return new Base10(offset);
+            return (offset == 0) ? Base10.INSTANCE : new Base10(offset);
         }
         if (base == 0 || base == Double.POSITIVE_INFINITY) {
             /*
@@ -236,6 +236,9 @@ class LogarithmicTransform1D extends Abs
         /** For cross-version compatibility. */
         private static final long serialVersionUID = -5435804027536647558L;
 
+        /** Commonly used instance. */
+        static LogarithmicTransform1D INSTANCE = new Base10(0);
+
         /** Constructs the inverse of the supplied exponential transform. */
         Base10(final ExponentialTransform1D inverse) {
             super(inverse);

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransferFunction.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransferFunction.java?rev=1605055&r1=1605054&r2=1605055&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransferFunction.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/TransferFunction.java
[UTF-8] Tue Jun 24 11:10:50 2014
@@ -19,16 +19,21 @@ package org.apache.sis.referencing.opera
 import java.io.Serializable;
 import org.opengis.metadata.content.TransferFunctionType;
 import org.opengis.referencing.operation.MathTransform1D;
+import org.opengis.referencing.operation.Matrix;
 import org.apache.sis.util.resources.Errors;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.StringBuilders;
+import org.apache.sis.util.Characters;
+import org.apache.sis.util.Debug;
 
 
 /**
- * The function converting <cite>sample values</cite> in a raster to <cite>geophysics
values</cite>.
+ * The function converting raster <cite>sample values</cite> to <cite>geophysics
values</cite>.
  * The function is usually linear, but can sometime be logarithmic or exponential. The later
occur
  * most often when measuring concentration of something.
  *
  * <table class="sis">
- *   <caption>Supported transfer functions</caption>
+ *   <caption>Supported transfer function types</caption>
  *   <tr><th>Type</th><th>Equation</th></tr>
  *   <tr><td>{@link TransferFunctionType#LINEAR LINEAR}</td>
  *       <td><var>y</var> = scale⋅<var>x</var> + offset</td></tr>
@@ -56,6 +61,11 @@ import org.apache.sis.util.resources.Err
  */
 public class TransferFunction implements Cloneable, Serializable {
     /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = 185931909755748004L;
+
+    /**
      * Whether the function is linear, logarithmic or exponential.
      */
     private TransferFunctionType type;
@@ -109,6 +119,7 @@ public class TransferFunction implements
      * @param type The transfer function type.
      */
     public void setType(final TransferFunctionType type) {
+        ArgumentChecks.ensureNonNull("type", type);
         this.type = type;
         transform = null;
     }
@@ -116,7 +127,7 @@ public class TransferFunction implements
     /**
      * Returns the logarithm or exponent base in the transfer function.
      * This value is always 1 for {@link TransferFunctionType#LINEAR},
-     * and usually (but not necessarily) 10 for the other types.
+     * and usually (but not necessarily) 10 for the logarithmic and exponential types.
      *
      * @return The logarithmic or exponent base.
      */
@@ -132,6 +143,7 @@ public class TransferFunction implements
      * @param base The new logarithm or exponent base.
      */
     public void setBase(final double base) {
+        ArgumentChecks.ensureStrictlyPositive("base", base);
         this.base = base;
         transform = null;
     }
@@ -211,4 +223,103 @@ public class TransferFunction implements
         }
         return transform;
     }
+
+    /**
+     * Sets the transform from sample values to geophysics values.
+     * This method infers the {@linkplain #getBase() base}, {@linkplain #getScale() scale}
and
+     * {@linkplain #getOffset() offset} values from the given transform.
+     *
+     * @param  function The transform to set.
+     * @throws IllegalArgumentException if this method does not recognize the given transform.
+     */
+    public void setTransform(final MathTransform1D function) throws IllegalArgumentException
{
+        ArgumentChecks.ensureNonNull("function", function);
+        if (function instanceof LinearTransform) {
+            setLinearTerms((LinearTransform) function);
+            type = TransferFunctionType.LINEAR;
+        } else if (function instanceof ExponentialTransform1D) {
+            final ExponentialTransform1D f = (ExponentialTransform1D) function;
+            type   = TransferFunctionType.EXPONENTIAL;
+            base   = f.base;
+            scale  = f.scale;
+            offset = 0;
+        } else if (function instanceof LogarithmicTransform1D) {
+            final LogarithmicTransform1D f = (LogarithmicTransform1D) function;
+            type   = TransferFunctionType.LOGARITHMIC;
+            base   = f.base;
+            offset = f.offset;
+            scale  = 1;
+        } else {
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.UnknownType_1, function.getClass()));
+        }
+        transform = function;
+    }
+
+    /**
+     * Sets the {@link #scale} and {@link #offset} terms from the given function.
+     *
+     * @param  function The transform to set.
+     * @throws IllegalArgumentException if this method does not recognize the given transform.
+     */
+    private void setLinearTerms(final LinearTransform function) throws IllegalArgumentException
{
+        final Matrix m = function.getMatrix();
+        final int numRow = m.getNumRow();
+        final int numCol = m.getNumCol();
+        if (numRow != 2 || numCol != 2) {
+            final Integer two = 2;
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.MismatchedMatrixSize_4,
two, two, numRow, numCol));
+        }
+        scale  = m.getElement(0, 0);
+        offset = m.getElement(0, 1);
+    }
+
+    /**
+     * Returns a string representation of this transfer function for debugging purpose.
+     * The string returned by this method may change in any future SIS version.
+     *
+     * @return A string representation of this transfer function.
+     */
+    @Debug
+    @Override
+    public String toString() {
+        final StringBuilder b = new StringBuilder("y = ");
+        if (scale != 1) {
+            if (scale == -1) {
+                b.append('−');
+            } else {
+                StringBuilders.trimFractionalPart(b.append(scale).append('∙'));
+            }
+        }
+        if (TransferFunctionType.LINEAR.equals(type)) {
+            b.append('x');
+        } else if (TransferFunctionType.EXPONENTIAL.equals(type)) {
+            if (base == Math.E) {
+                b.append('e');
+            } else {
+                StringBuilders.trimFractionalPart(b.append(base));
+            }
+            b.append('ˣ');
+        } else if (TransferFunctionType.LOGARITHMIC.equals(type)) {
+            if (base == Math.E) {
+                b.append("ln");
+            } else {
+                b.append('㏒');
+                if (base != 10) {
+                    final int c = (int) base;
+                    if (c == base && c >= 0 && c <= 9) {
+                        b.append(Characters.toSubScript((char) (c - '0')));
+                    } else {
+                        StringBuilders.trimFractionalPart(b.append(base));
+                    }
+                }
+            }
+            b.append('⒳');
+        } else {
+            b.append('?');
+        }
+        if (offset != 0) {
+            StringBuilders.trimFractionalPart(b.append(' ').append(offset < 0 ? '−'
: '+').append(' ').append(Math.abs(offset)));
+        }
+        return b.toString();
+    }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/package-info.java?rev=1605055&r1=1605054&r2=1605055&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/package-info.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/package-info.java
[UTF-8] Tue Jun 24 11:10:50 2014
@@ -34,15 +34,18 @@
  * In the 2D case, Apache SIS provides instances of the standard {@link java.awt.geom.AffineTransform}
  * class when possible.
  *
- * <p>{@code MathTransform} usually performs conversions or transformations from points
given in a
+ * <p>This package does not include map projections, which are a special kind of transforms
defined
+ * in their own {@linkplain org.apache.sis.referencing.operation.projection projection} package.</p>
+ *
+ * {@section Non-spatial coordinates}
+ * {@code MathTransform} usually performs conversions or transformations from points given
in a
  * {@linkplain org.apache.sis.referencing.operation.DefaultCoordinateOperation#getSourceCRS()
  * source coordinate reference system} to coordinate values for the same points in the
  * {@linkplain org.apache.sis.referencing.operation.DefaultCoordinateOperation#getTargetCRS()
  * target coordinate reference system}. However the conversions are not necessarily between
CRS;
- * a {@code MathTransform} can also be used for converting the sample values in a raster
for example.</p>
- *
- * <p>This package does not include map projections, which are a special kind of transforms
defined
- * in their own {@linkplain org.apache.sis.referencing.operation.projection projection} package.</p>
+ * a {@code MathTransform} can also be used for converting the sample values in a raster
for example.
+ * Such kind of transforms are named {@linkplain org.apache.sis.referencing.operation.transform.TransferFunction
+ * transfer functions}.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @since   0.5 (derived from geotk-1.2)

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PassThroughTransformTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PassThroughTransformTest.java?rev=1605055&r1=1605054&r2=1605055&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PassThroughTransformTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/PassThroughTransformTest.java
[UTF-8] Tue Jun 24 11:10:50 2014
@@ -111,11 +111,10 @@ public final strictfp class PassThroughT
      *
      * @throws TransformException Should never happen.
      */
-//    TODO
-//    @Test
-//    public void testPassthrough() throws TransformException {
-//        runTest(ExponentialTransform1D.create(10, -2), PassThroughTransform.class);
-//    }
+    @Test
+    public void testPassthrough() throws TransformException {
+        runTest(ExponentialTransform1D.create(10, 2), PassThroughTransform.class);
+    }
 
     /**
      * Tests a pass-through transform built using the given sub-transform.
@@ -168,10 +167,10 @@ public final strictfp class PassThroughT
          *   - passthrough data, to be given to the transform to be tested.
          *   - sub-transform data, which we will use internally for verifying the pass-through
work.
          */
-        final int passthroughDim  = transform.getSourceDimensions();
-        final int subTransformDim = subTransform.getSourceDimensions();
-        final int numPts          = ORDINATE_COUNT / passthroughDim;
-        final double[] passthroughData  = CoordinateDomain.GEOGRAPHIC.generateRandomInput(random,
passthroughDim, numPts);
+        final int      passthroughDim   = transform.getSourceDimensions();
+        final int      subTransformDim  = subTransform.getSourceDimensions();
+        final int      numPts           = ORDINATE_COUNT / passthroughDim;
+        final double[] passthroughData  = CoordinateDomain.RANGE_10.generateRandomInput(random,
passthroughDim, numPts);
         final double[] subTransformData = new double[numPts * subTransformDim];
         Arrays.fill(subTransformData, Double.NaN);
         for (int i=0; i<numPts; i++) {

Added: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/TransferFunctionTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/TransferFunctionTest.java?rev=1605055&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/TransferFunctionTest.java
(added)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/TransferFunctionTest.java
[UTF-8] Tue Jun 24 11:10:50 2014
@@ -0,0 +1,122 @@
+/*
+ * 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.metadata.content.TransferFunctionType;
+import org.opengis.referencing.operation.MathTransform1D;
+import org.apache.sis.referencing.operation.matrix.Matrix2;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.opengis.test.Assert.*;
+
+
+/**
+ * Tests {@link TransferFunction}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.5
+ * @version 0.5
+ * @module
+ */
+@DependsOn(ExponentialTransform1DTest.class)
+public final class TransferFunctionTest extends TestCase {
+    /**
+     * For floating point comparisons.
+     */
+    private static final double STRICT = 0;
+
+    /**
+     * Tests the creation of a linear transfer function.
+     */
+    @Test
+    public void testLinear() {
+        final TransferFunction f = new TransferFunction();
+        assertEquals("type", TransferFunctionType.LINEAR, f.getType());
+        assertEquals("base",   1, f.getBase(),   STRICT);
+        assertEquals("scale",  1, f.getScale(),  STRICT);
+        assertEquals("offset", 0, f.getOffset(), STRICT);
+        assertEquals("toString", "y = x", f.toString());
+
+        f.setScale(0.15);
+        f.setOffset(-2);
+        assertEquals("toString", "y = 0.15∙x − 2", f.toString());
+        final MathTransform1D transform = f.getTransform();
+        assertInstanceOf("transform", LinearTransform.class, transform);
+        assertMatrixEquals("transform.matrix", new Matrix2(0.15, -2, 0, 1),
+                ((LinearTransform) transform).getMatrix(), STRICT);
+        /*
+         * Get back the coefficients.
+         */
+        final TransferFunction b = new TransferFunction();
+        b.setTransform(transform);
+        assertEquals("type", TransferFunctionType.LINEAR, b.getType());
+        assertEquals("base",    1,    b.getBase(),   STRICT);
+        assertEquals("scale",   0.15, b.getScale(),  STRICT);
+        assertEquals("offset", -2,    b.getOffset(), STRICT);
+    }
+
+    /**
+     * Tests the creation of a logarithmic transfer function.
+     */
+    @Test
+    @DependsOnMethod("testLinear")
+    public void testLogarithmic() {
+        final TransferFunction f = new TransferFunction();
+        f.setType(TransferFunctionType.LOGARITHMIC);
+        f.setOffset(-2);
+        assertEquals("base", 10, f.getBase(), STRICT);
+        assertEquals("toString", "y = ㏒⒳ − 2", f.toString());
+        final MathTransform1D transform = f.getTransform();
+        assertInstanceOf("transform", LogarithmicTransform1D.class, transform);
+        /*
+         * Get back the coefficients.
+         */
+        final TransferFunction b = new TransferFunction();
+        b.setTransform(transform);
+        assertEquals("type", TransferFunctionType.LOGARITHMIC, b.getType());
+        assertEquals("base",   10, b.getBase(),   STRICT);
+        assertEquals("scale",   1, b.getScale(),  STRICT);
+        assertEquals("offset", -2, b.getOffset(), STRICT);
+    }
+
+    /**
+     * Tests the creation of an exponential transfer function.
+     */
+    @Test
+    @DependsOnMethod("testLinear")
+    public void testExponential() {
+        final TransferFunction f = new TransferFunction();
+        f.setType(TransferFunctionType.EXPONENTIAL);
+        f.setScale(0.15);
+        assertEquals("base", 10, f.getBase(), STRICT);
+        assertEquals("toString", "y = 0.15∙10ˣ", f.toString());
+        final MathTransform1D transform = f.getTransform();
+        assertInstanceOf("transform", ExponentialTransform1D.class, transform);
+        /*
+         * Get back the coefficients.
+         */
+        final TransferFunction b = new TransferFunction();
+        b.setTransform(transform);
+        assertEquals("type", TransferFunctionType.EXPONENTIAL, b.getType());
+        assertEquals("base",   10,    b.getBase(),   STRICT);
+        assertEquals("scale",   0.15, b.getScale(),  STRICT);
+        assertEquals("offset",  0,    b.getOffset(), STRICT);
+    }
+}

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

Propchange: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/transform/TransferFunctionTest.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=1605055&r1=1605054&r2=1605055&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 24 11:10:50 2014
@@ -48,6 +48,7 @@ import org.junit.BeforeClass;
     org.apache.sis.referencing.operation.transform.ExponentialTransform1DTest.class,
     org.apache.sis.referencing.operation.transform.CopyTransformTest.class,
     org.apache.sis.referencing.operation.transform.PassThroughTransformTest.class,
+    org.apache.sis.referencing.operation.transform.TransferFunctionTest.class,
 
     org.apache.sis.internal.referencing.FormulasTest.class,
     org.apache.sis.internal.referencing.VerticalDatumTypesTest.class,



Mime
View raw message