001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, Geotools Project Managment Committee (PMC)
005: * (C) 2006, Geomatys
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.matrix;
018:
019: // J2SE and JUnit dependencies
020: import java.awt.geom.AffineTransform;
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: /**
026: * Tests {@link XAffineTransform} static methods.
027: *
028: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/referencing/operation/matrix/XAffineTransformTest.java $
029: * @version $Id: XAffineTransformTest.java 24576 2007-02-24 00:07:40Z desruisseaux $
030: * @author Martin Desruisseaux
031: */
032: public final class XAffineTransformTest extends TestCase {
033: /**
034: * Run the suite from the command line.
035: */
036: public static void main(String[] args) {
037: junit.textui.TestRunner.run(suite());
038: }
039:
040: /**
041: * Returns the test suite.
042: */
043: public static Test suite() {
044: return new TestSuite(XAffineTransformTest.class);
045: }
046:
047: /**
048: * Tolerance value for comparaisons.
049: */
050: private static final double EPS = 1E-10;
051:
052: /**
053: * Constructs a test case.
054: */
055: public XAffineTransformTest(String testName) {
056: super (testName);
057: }
058:
059: /**
060: * Tests {@link XAffineTransform} in the unflipped case.
061: */
062: public void testUnflipped() {
063: runTest(+1);
064: }
065:
066: /**
067: * Tests {@link XAffineTransform} in the flipped case.
068: */
069: public void testFlipped() {
070: runTest(-1);
071: }
072:
073: /**
074: * Gets the flip state using standard {@code AffineTransform} API.
075: */
076: private static int getFlipFromType(final AffineTransform tr) {
077: return (tr.getType() & AffineTransform.TYPE_FLIP) != 0 ? -1
078: : +1;
079: }
080:
081: /**
082: * Run the test in the flipped or unflipped case.
083: *
084: * @param f -1 for the flipped case, or +1 for the unflipped case.
085: */
086: private static void runTest(final int f) {
087: // Test identity
088: final AffineTransform tr = new AffineTransform();
089: tr.setToScale(1, f);
090: assertEquals(1, XAffineTransform.getScaleX0(tr), EPS);
091: assertEquals(1, XAffineTransform.getScaleY0(tr), EPS);
092: assertEquals(0, XAffineTransform.getRotation(tr), EPS);
093: assertEquals(1, XAffineTransform.getSwapXY(tr));
094: assertEquals(f, XAffineTransform.getFlip(tr));
095: assertEquals(f, getFlipFromType(tr));
096:
097: // Tests rotation (< 45°)
098: double r = Math.toRadians(25);
099: tr.rotate(r);
100: assertEquals(1, XAffineTransform.getScaleX0(tr), EPS);
101: assertEquals(1, XAffineTransform.getScaleY0(tr), EPS);
102: assertEquals(r, XAffineTransform.getRotation(tr), EPS);
103: assertEquals(1, XAffineTransform.getSwapXY(tr));
104: assertEquals(f, XAffineTransform.getFlip(tr));
105: assertEquals(f, getFlipFromType(tr));
106:
107: // Tests more rotation (> 45°)
108: r = Math.toRadians(65);
109: tr.rotate(Math.toRadians(40));
110: assertEquals(1, XAffineTransform.getScaleX0(tr), EPS);
111: assertEquals(1, XAffineTransform.getScaleY0(tr), EPS);
112: assertEquals(r, XAffineTransform.getRotation(tr), EPS);
113: assertEquals(-1, XAffineTransform.getSwapXY(tr));
114: assertEquals(f, XAffineTransform.getFlip(tr));
115: assertEquals(f, getFlipFromType(tr));
116:
117: // Tests scale
118: tr.setToScale(2, 3 * f);
119: assertEquals(2, XAffineTransform.getScaleX0(tr), EPS);
120: assertEquals(3, XAffineTransform.getScaleY0(tr), EPS);
121: assertEquals(0, XAffineTransform.getRotation(tr), EPS);
122: assertEquals(1, XAffineTransform.getSwapXY(tr));
123: assertEquals(f, XAffineTransform.getFlip(tr));
124: assertEquals(f, getFlipFromType(tr));
125:
126: // Tests rotation + scale
127: tr.rotate(r);
128: assertEquals(2, XAffineTransform.getScaleX0(tr), EPS);
129: assertEquals(3, XAffineTransform.getScaleY0(tr), EPS);
130: assertEquals(r, XAffineTransform.getRotation(tr), EPS);
131: assertEquals(-1, XAffineTransform.getSwapXY(tr));
132: assertEquals(f, XAffineTransform.getFlip(tr));
133: assertEquals(1, getFlipFromType(tr)); // Always unflipped according Java 1.5.0_09...
134:
135: // Tests axis swapping
136: r = Math.toRadians(-90 * f);
137: tr.setTransform(0, 1, f, 0, 0, 0);
138: assertEquals(1, XAffineTransform.getScaleX0(tr), EPS);
139: assertEquals(1, XAffineTransform.getScaleY0(tr), EPS);
140: assertEquals(r, XAffineTransform.getRotation(tr), EPS);
141: assertEquals(-1, XAffineTransform.getSwapXY(tr));
142: assertEquals(-f, XAffineTransform.getFlip(tr));
143: assertEquals(-f, getFlipFromType(tr));
144:
145: // Tests axis swapping + scale
146: tr.scale(2, 3);
147: assertEquals(3, XAffineTransform.getScaleX0(tr), EPS);
148: assertEquals(2, XAffineTransform.getScaleY0(tr), EPS);
149: assertEquals(r, XAffineTransform.getRotation(tr), EPS);
150: assertEquals(-1, XAffineTransform.getSwapXY(tr));
151: assertEquals(-f, XAffineTransform.getFlip(tr));
152: assertEquals(-f, getFlipFromType(tr));
153: }
154: }
|