001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2002, Refractions Reserach Inc.
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;
010: * version 2.1 of the License.
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.graph.linegraph;
018:
019: import junit.framework.TestCase;
020:
021: import org.geotools.graph.build.line.OptLineGraphGenerator;
022: import org.geotools.graph.structure.Edge;
023: import org.geotools.graph.structure.Graph;
024: import org.geotools.graph.structure.GraphVisitor;
025: import org.geotools.graph.structure.Graphable;
026: import org.geotools.graph.structure.line.XYNode;
027:
028: import com.vividsolutions.jts.geom.Coordinate;
029: import com.vividsolutions.jts.geom.LineSegment;
030:
031: public class OptLineGraphGeneratorTest extends TestCase {
032:
033: private OptLineGraphGenerator m_gen;
034:
035: public OptLineGraphGeneratorTest(String name) {
036: super (name);
037: }
038:
039: protected void setUp() throws Exception {
040: super .setUp();
041:
042: m_gen = createGenerator();
043: }
044:
045: /**
046: * Build a linear graph by adding a number of line segments that join
047: * at endpoints.
048: * <BR>
049: * Expected: 1. Number of edges = number of lines added.
050: * 2. Number of nodes = number of lines + 1
051: *
052: */
053: public void test_0() {
054: final Coordinate base = new Coordinate(0d, 0d);
055: final int n = 100;
056: for (int i = 1; i <= n; i++) {
057: Edge e = (Edge) generator().add(
058: new LineSegment(new Coordinate(base.x + (i - 1),
059: base.y + (i - 1)), new Coordinate(base.x
060: + i, base.y + i)));
061: }
062: generator().generate();
063: Graph built = generator().getGraph();
064:
065: //ensure correct graph structure
066: assertTrue(built.getEdges().size() == n);
067: assertTrue(built.getNodes().size() == n + 1);
068:
069: assertTrue(built.getNodesOfDegree(1).size() == 2);
070: assertTrue(built.getNodesOfDegree(2).size() == n - 1);
071:
072: //ensure coordinates
073: GraphVisitor visitor = new GraphVisitor() {
074: public int visit(Graphable component) {
075: Edge e = (Edge) component;
076: XYNode a = (XYNode) e.getNodeA();
077: XYNode b = (XYNode) e.getNodeB();
078:
079: //coordinats should be a distance of sqrt(2)
080: assertTrue(Math.abs(a.getCoordinate().x
081: - b.getCoordinate().x) == 1
082: && Math.abs(a.getCoordinate().y
083: - b.getCoordinate().y) == 1);
084:
085: // assertTrue(
086: // Math.abs(a.getX() - b.getX()) == 1
087: // && Math.abs(a.getY() - b.getY()) == 1
088: // );
089: return (0);
090: }
091: };
092: built.visitEdges(visitor);
093: }
094:
095: /**
096: * Build a circular graph of line segments that join at endpoints. <BR>
097: * <BR>
098: * Expected: 1. Number of edges = number of nodes = number of lines.
099: */
100: public void test_1() {
101:
102: final Coordinate base = new Coordinate(0d, 0d);
103: final int n = 100;
104: for (int i = 1; i <= n; i++) {
105: Edge e = (Edge) generator().add(
106: new LineSegment(new Coordinate(base.x + (i - 1),
107: base.y + (i - 1)), new Coordinate(base.x
108: + i, base.y + i)));
109: }
110:
111: //complete the circle
112: generator().add(
113: new LineSegment(new Coordinate(base.x + n, base.y + n),
114: base));
115:
116: generator().generate();
117: Graph built = generator().getGraph();
118:
119: assertTrue(built.getEdges().size() == n + 1);
120: assertTrue(built.getNodes().size() == n + 1);
121:
122: //all nodes should be of degree 2
123: assertTrue(built.getNodesOfDegree(2).size() == built.getNodes()
124: .size());
125:
126: //ensure coordinates
127: GraphVisitor visitor = new GraphVisitor() {
128: public int visit(Graphable component) {
129: Edge e = (Edge) component;
130: XYNode a = (XYNode) e.getNodeA();
131: XYNode b = (XYNode) e.getNodeB();
132:
133: if (b.getCoordinate().equals(base)) {
134: assertTrue(a.getCoordinate().equals(
135: new Coordinate(n, n)));
136: } else {
137: assertTrue(Math.abs(a.getCoordinate().x
138: - b.getCoordinate().x) == 1
139: && Math.abs(a.getCoordinate().y
140: - b.getCoordinate().y) == 1);
141: }
142:
143: return (0);
144: }
145: };
146: built.visitEdges(visitor);
147: }
148:
149: protected OptLineGraphGenerator createGenerator() {
150: return (new OptLineGraphGenerator());
151: }
152:
153: protected OptLineGraphGenerator generator() {
154: return (m_gen);
155: }
156: }
|