001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.referencing.operation.builder;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021: import org.geotools.geometry.DirectPosition2D;
022: import org.opengis.geometry.DirectPosition;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Random;
027:
028: public class TriangulationFactoryTest extends TestCase {
029:
030: /**
031: * Run the suit from the command line.
032: */
033: public static void main(String[] args) {
034: junit.textui.TestRunner.run(suite());
035: }
036:
037: /**
038: * Returns the test suite.
039: */
040: public static Test suite() {
041: TestSuite suite = new TestSuite(TriangulationFactoryTest.class);
042:
043: return suite;
044: }
045:
046: /**
047: * Test (@link TringulationFactory).
048: */
049: public void testTringulationFactory() {
050: DirectPosition sp1 = new DirectPosition2D(10, 10);
051: DirectPosition tp1 = new DirectPosition2D(10, 10);
052:
053: DirectPosition sp2 = new DirectPosition2D(20, 10);
054: DirectPosition tp2 = new DirectPosition2D(20, 10);
055:
056: DirectPosition sp3 = new DirectPosition2D(20, 20);
057: DirectPosition tp3 = new DirectPosition2D(20, 20);
058:
059: DirectPosition sp4 = new DirectPosition2D(10, 20);
060: DirectPosition tp4 = new DirectPosition2D(10, 20);
061:
062: DirectPosition sp5 = new DirectPosition2D(14, 16);
063: DirectPosition tp5 = new DirectPosition2D(14, 16);
064:
065: ExtendedPosition mtp1 = new ExtendedPosition(sp1, tp1);
066: ExtendedPosition mtp2 = new ExtendedPosition(sp2, tp2);
067: ExtendedPosition mtp3 = new ExtendedPosition(sp3, tp3);
068: ExtendedPosition mtp4 = new ExtendedPosition(sp4, tp4);
069: ExtendedPosition mtp5 = new ExtendedPosition(sp5, tp5);
070: DirectPosition[] vertices = new DirectPosition[1];
071: vertices[0] = mtp5;
072:
073: Quadrilateral quad = new Quadrilateral(mtp1, mtp2, mtp3, mtp4);
074:
075: try {
076: new TriangulationFactory(quad, vertices);
077: } catch (TriangulationException e) {
078: System.out.println(e.getMessage());
079: }
080: }
081:
082: /**
083: * Test (@link TringulationFactory). Triangles are tested with delaunay test.
084: */
085: public void testDelaunay() throws TriangulationException {
086: // coordinates of quadrilateral for triangulation
087: DirectPosition2D leftDown = new DirectPosition2D(100, 100);
088:
089: DirectPosition2D rightDown = new DirectPosition2D(200, 100);
090:
091: DirectPosition2D rightTop = new DirectPosition2D(200, 250);
092:
093: DirectPosition2D leftTop = new DirectPosition2D(100, 250);
094:
095: // ArrayList vertices = new ArrayList();
096:
097: // generator for points within the quadrilateral:
098: Random randomCoord = new Random(872066443);
099:
100: // number of points
101: int number = 5;
102: DirectPosition[] vertices = new DirectPosition[number];
103:
104: for (int i = 0; i < number; i++) {
105: double x = leftDown.x
106: + (randomCoord.nextDouble() * (rightDown.x - leftDown.x));
107: double y = leftDown.y
108: + (randomCoord.nextDouble() * (leftTop.y - leftDown.y));
109: vertices[i] = new DirectPosition2D(x, y);
110: }
111:
112: Quadrilateral quad = new Quadrilateral(leftDown, rightDown,
113: rightTop, leftTop);
114:
115: List triangles = new ArrayList();
116:
117: try {
118: TriangulationFactory trigfac = new TriangulationFactory(
119: quad, vertices);
120: triangles = trigfac.getTriangulation();
121: } catch (TriangulationException e) {
122: System.out.println(e.getMessage());
123: }
124:
125: int j = 1;
126:
127: for (Iterator i = triangles.iterator(); i.hasNext();) {
128: TINTriangle triangle = (TINTriangle) i.next();
129:
130: for (j = 0; j < vertices.length; j++) {
131: // Delunay Test - there are no vetrices in the CircumCicle
132: assertFalse(triangle.getCircumCicle().contains(
133: vertices[j]));
134: }
135: }
136: }
137: }
|