001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2002, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.referencing.operation.transform;
018:
019: // J2SE dependencies
020: import java.util.Arrays;
021: import java.awt.geom.AffineTransform;
022:
023: // JUnit dependencies
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: // OpenGIS dependencies
028: import org.opengis.parameter.ParameterValueGroup;
029: import org.opengis.referencing.FactoryException;
030: import org.opengis.referencing.operation.MathTransform;
031: import org.opengis.referencing.operation.TransformException;
032: import org.opengis.referencing.operation.OperationNotFoundException;
033:
034: // Geotools dependencies
035: import org.geotools.referencing.operation.TestTransform;
036: import org.geotools.referencing.operation.matrix.GeneralMatrix;
037:
038: /**
039: * Test the following transforms:
040: *
041: * <ul>
042: * <li>{@link MathTransformFactory#createPassthroughTransform}</li>
043: * <li>{@link MathTransformFactory#createSubTransform}</li>
044: * <li>{@link MathTransformFactory#createFilterTransform}</li>
045: * </ul>
046: *
047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/referencing/operation/transform/PassthroughTransformTest.java $
048: * @version $Id: PassthroughTransformTest.java 24576 2007-02-24 00:07:40Z desruisseaux $
049: * @author Martin Desruisseaux
050: */
051: public final class PassthroughTransformTest extends TestTransform {
052: /**
053: * Runs the tests with the textual test runner.
054: */
055: public static void main(String args[]) {
056: junit.textui.TestRunner.run(suite());
057: }
058:
059: /**
060: * Returns the test suite.
061: */
062: public static Test suite() {
063: return new TestSuite(PassthroughTransformTest.class);
064: }
065:
066: /**
067: * Constructs a test case with the given name.
068: */
069: public PassthroughTransformTest(final String name) {
070: super (name);
071: }
072:
073: /**
074: * Test the pass through transform using an affine transform. The "passthrough" of
075: * such transform are optimized in a special way.
076: */
077: public void testLinear() throws FactoryException,
078: TransformException {
079: runTest(mtFactory.createAffineTransform(new GeneralMatrix(
080: AffineTransform.getScaleInstance(4, 2))));
081: }
082:
083: /**
084: * Test the general passthrough transform.
085: */
086: public void testPassthrough() throws FactoryException,
087: TransformException {
088: final ParameterValueGroup param = mtFactory
089: .getDefaultParameters("Exponential");
090: runTest(mtFactory.createParameterizedTransform(param));
091: }
092:
093: /**
094: * Test the pass through transform.
095: */
096: private void runTest(final MathTransform sub)
097: throws FactoryException, TransformException {
098: compare(sub, sub, 0);
099: try {
100: mtFactory.createPassThroughTransform(-1, sub, 0);
101: fail("An illegal argument should have been detected");
102: } catch (FactoryException e) {
103: // This is the expected exception.
104: }
105: try {
106: mtFactory.createPassThroughTransform(0, sub, -1);
107: fail("An illegal argument should have been detected");
108: } catch (FactoryException e) {
109: // This is the expected exception.
110: }
111: assertSame(
112: "Failed to recognize that no passthrough transform was needed",
113: sub, mtFactory.createPassThroughTransform(0, sub, 0));
114:
115: final int subLower = 2;
116: final int subUpper = subLower + sub.getSourceDimensions();
117: final MathTransform passthrough = mtFactory
118: .createPassThroughTransform(subLower, sub, 1);
119: assertEquals("Wrong number of source dimensions", sub
120: .getSourceDimensions()
121: + subLower + 1, passthrough.getSourceDimensions());
122: assertEquals("Wrong number of target dimensions", sub
123: .getTargetDimensions()
124: + subLower + 1, passthrough.getTargetDimensions());
125: compare(passthrough, sub, 2);
126: /*
127: * Try to split the pass through transform and get back the original one.
128: */
129: final DimensionFilter filter = new DimensionFilter(mtFactory);
130: filter.addSourceDimensionRange(0, subLower);
131: assertTrue("Expected an identity transform", filter.separate(
132: passthrough).isIdentity());
133:
134: filter.clear();
135: filter.addSourceDimensionRange(subUpper, passthrough
136: .getSourceDimensions());
137: assertTrue("Expected an identity transform", filter.separate(
138: passthrough).isIdentity());
139:
140: filter.clear();
141: filter.addSourceDimensionRange(subLower, subUpper);
142: assertEquals("Expected the sub-transform", sub, filter
143: .separate(passthrough));
144: final int[] expectedDimensions = new int[sub
145: .getTargetDimensions()];
146: for (int i = 0; i < expectedDimensions.length; i++) {
147: expectedDimensions[i] = subLower + i;
148: }
149: assertTrue("Unexpected output dimensions", Arrays.equals(
150: expectedDimensions, filter.getTargetDimensions()));
151: }
152:
153: /**
154: * Test the specified transform.
155: *
156: * @param mt The transform to test.
157: * @param submt The sub transform.
158: * @param subOffset Index of the first input/output dimension which correspond to
159: * <code>submt</code>.
160: */
161: private void compare(final MathTransform mt,
162: final MathTransform submt, final int subOffset)
163: throws TransformException {
164: final int pointCount = 200;
165: final int mtDimension = mt.getSourceDimensions();
166: final int atDimension = submt.getSourceDimensions();
167: final double[] atData = new double[pointCount * atDimension];
168: final double[] mtData = new double[pointCount * mtDimension];
169: for (int j = 0; j < pointCount; j++) {
170: for (int i = 0; i < mtDimension; i++) {
171: mtData[j * mtDimension + i] = 100 * random.nextDouble() - 50;
172: }
173: for (int i = 0; i < atDimension; i++) {
174: atData[j * atDimension + i] = mtData[j * mtDimension
175: + subOffset + i];
176: }
177: }
178: if (atDimension == mtDimension) {
179: assertTrue("Test arrays are not correctly build.", Arrays
180: .equals(atData, mtData));
181: }
182: final double[] reference = (double[]) mtData.clone();
183: submt.transform(atData, 0, atData, 0, pointCount);
184: mt.transform(mtData, 0, mtData, 0, pointCount);
185: assertTrue("'subOffset' argument too high", subOffset
186: + atDimension <= mtDimension);
187: for (int j = 0; j < pointCount; j++) {
188: for (int i = 0; i < mtDimension; i++) {
189: final double expected;
190: if (i < subOffset || i >= subOffset + atDimension) {
191: expected = reference[j * mtDimension + i];
192: } else {
193: expected = atData[j * atDimension + i - subOffset];
194: }
195: assertEquals("A transformed value is wrong", expected,
196: mtData[j * mtDimension + i], 1E-6);
197: }
198: }
199: }
200: }
|