001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2003, Institut de Recherche pour le Développement
006: * (C) 1998, Pêches et Océans Canada
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: */
018: package org.geotools.math;
019:
020: // J2SE dependencies
021: import java.awt.geom.Line2D;
022: import java.awt.geom.Point2D;
023: import java.util.Random;
024:
025: import javax.vecmath.Point3d;
026:
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030:
031: /**
032: * Test the {@link Line} and {@link Plane} classes.
033: *
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/test/java/org/geotools/math/GeometryTest.java $
035: * @version $Id: GeometryTest.java 22443 2006-10-27 20:47:22Z desruisseaux $
036: * @author Martin Desruisseaux
037: */
038: public final class GeometryTest extends TestCase {
039: /**
040: * Tolerance factor for comparaisons.
041: */
042: private static final double EPS = 1E-8;
043:
044: /**
045: * Run the suit from the command line.
046: */
047: public static void main(final String[] args) {
048: junit.textui.TestRunner.run(suite());
049: }
050:
051: /**
052: * Returns the test suite.
053: */
054: public static Test suite() {
055: return new TestSuite(GeometryTest.class);
056: }
057:
058: /**
059: * Constructs a test case with the given name.
060: */
061: public GeometryTest(final String name) {
062: super (name);
063: }
064:
065: /**
066: * Test {@link Line#setLine}.
067: */
068: public void testLine() {
069: final Line line = new Line();
070: line.setLine(new Point2D.Double(-2, 2), new Point2D.Double(8,
071: 22));
072: assertEquals("slope", 2, line.getSlope(), EPS);
073: assertEquals("x0", -3, line.getX0(), EPS);
074: assertEquals("y0", 6, line.getY0(), EPS);
075:
076: // Horizontal line
077: line.setLine(new Point2D.Double(-2, 2),
078: new Point2D.Double(8, 2));
079: assertEquals("slope", 0, line.getSlope(), EPS);
080: assertTrue("x0", Double.isInfinite(line.getX0()));
081: assertEquals("y0", 2, line.getY0(), EPS);
082:
083: // Vertical line
084: line.setLine(new Point2D.Double(-2, 2), new Point2D.Double(-2,
085: 22));
086: assertTrue("slope", Double.isInfinite(line.getSlope()));
087: assertEquals("x0", -2, line.getX0(), EPS);
088: assertTrue("y0", Double.isInfinite(line.getY0()));
089:
090: // Horizontal line on the x axis
091: line.setLine(new Point2D.Double(-2, 0),
092: new Point2D.Double(8, 0));
093: assertEquals("slope", 0, line.getSlope(), EPS);
094: assertTrue("x0", Double.isInfinite(line.getX0()));
095: assertEquals("y0", 0, line.getY0(), EPS);
096:
097: // Vertical line on the y axis
098: line.setLine(new Point2D.Double(0, 2),
099: new Point2D.Double(0, 22));
100: assertTrue("slope", Double.isInfinite(line.getSlope()));
101: assertEquals("x0", 0, line.getX0(), EPS);
102: assertTrue("y0", Double.isInfinite(line.getY0()));
103: }
104:
105: /**
106: * Test {@link Line#isoscelesTriangleBase}.
107: */
108: public void testIsoscelesTriangleBase() {
109: final Line test = new Line();
110: test.setLine(new Point2D.Double(20, 30), new Point2D.Double(80,
111: 95));
112: assertEquals("slope", 1.083333333333333333333333, test
113: .getSlope(), EPS);
114: assertEquals("y0", 8.333333333333333333333333, test.getY0(),
115: EPS);
116:
117: final double distance = 40;
118: final Point2D summit = new Point2D.Double(27, -9); // An arbitrary point.
119: final Line2D base = test
120: .isoscelesTriangleBase(summit, distance);
121: assertEquals("distance P1", distance, base.getP1().distance(
122: summit), EPS);
123: assertEquals("distance P2", distance, base.getP2().distance(
124: summit), EPS);
125:
126: final double x = 10; // Can be any arbitrary point.
127: final double y = 8;
128: assertEquals("nearest colinear point", base.ptLineDist(x, y),
129: test.nearestColinearPoint(new Point2D.Double(x, y))
130: .distance(x, y), EPS);
131: }
132:
133: /**
134: * Test {@link Plane#setPlane} methods.
135: */
136: public void testPlaneFit() {
137: final Random rd = new Random(457821698762354L);
138: final Plane plan = new Plane();
139: final Point3d P1 = new Point3d(100 * rd.nextDouble() + 25,
140: 100 * rd.nextDouble() + 25, Math.rint(100 * rd
141: .nextDouble() + 40));
142: final Point3d P2 = new Point3d(100 * rd.nextDouble() + 25,
143: 100 * rd.nextDouble() + 25, Math.rint(100 * rd
144: .nextDouble() + 40));
145: final Point3d P3 = new Point3d(100 * rd.nextDouble() + 25,
146: 100 * rd.nextDouble() + 25, Math.rint(100 * rd
147: .nextDouble() + 40));
148: plan.setPlane(P1, P2, P3);
149: assertEquals("P1", P1.z, plan.z(P1.x, P1.y), EPS);
150: assertEquals("P2", P2.z, plan.z(P2.x, P2.y), EPS);
151: assertEquals("P3", P3.z, plan.z(P3.x, P3.y), EPS);
152:
153: final double[] x = new double[4000];
154: final double[] y = new double[4000];
155: final double[] z = new double[4000];
156: for (int i = 0; i < z.length; i++) {
157: x[i] = 40 + 100 * rd.nextDouble();
158: y[i] = 40 + 100 * rd.nextDouble();
159: z[i] = plan.z(x[i], y[i]) + 10 * rd.nextDouble() - 5;
160: }
161: final Plane copy = (Plane) plan.clone();
162: final double eps = 1E-2; // We do expect some difference, but not much more than that.
163: assertEquals("c", copy.c, plan.c, eps);
164: assertEquals("cx", copy.cx, plan.cx, eps);
165: assertEquals("cy", copy.cy, plan.cy, eps);
166: }
167: }
|