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.OptDirectedLineGraphGenerator;
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 OptDirectedLineGraphGeneratorTest extends TestCase {
032:
033: private OptDirectedLineGraphGenerator m_gen;
034:
035: public OptDirectedLineGraphGeneratorTest(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 = 5;
056:
057: for (int i = 1; i <= n; i++) {
058: Edge e = (Edge) generator().add(
059: new LineSegment(new Coordinate(base.x + (i - 1),
060: base.y + (i - 1)), new Coordinate(base.x
061: + i, base.y + i)));
062: }
063:
064: generator().generate();
065: Graph built = generator().getGraph();
066:
067: //ensure correct graph structure
068: assertTrue(built.getEdges().size() == n);
069: assertTrue(built.getNodes().size() == n + 1);
070:
071: //ensure coordinates
072: GraphVisitor visitor = new GraphVisitor() {
073: public int visit(Graphable component) {
074: Edge e = (Edge) component;
075: XYNode a = (XYNode) e.getNodeA();
076: XYNode b = (XYNode) e.getNodeB();
077:
078: //coordinats should be a distance of sqrt(2)
079: //assertTrue(b.getX() == a.getX() + 1 && b.getY() == a.getY() + 1);
080: assertTrue(b.getCoordinate().equals(
081: new Coordinate(a.getCoordinate().x + 1, a
082: .getCoordinate().y + 1)));
083:
084: return (0);
085: }
086: };
087: built.visitEdges(visitor);
088: }
089:
090: /**
091: * Build a circular graph of line segments that join at endpoints. <BR>
092: * <BR>
093: * Expected: 1. Number of edges = number of nodes = number of lines.
094: */
095: public void test_1() {
096: final Coordinate base = new Coordinate(0d, 0d);
097: final int n = 100;
098: for (int i = 1; i <= n; i++) {
099: Edge e = (Edge) generator().add(
100: new LineSegment(new Coordinate(base.x + (i - 1),
101: base.y + (i - 1)), new Coordinate(base.x
102: + i, base.y + i)));
103: }
104:
105: //complete the circle
106: generator().add(
107: new LineSegment(new Coordinate(base.x + n, base.y + n),
108: base));
109:
110: generator().generate();
111:
112: Graph built = generator().getGraph();
113:
114: assertTrue(built.getEdges().size() == n + 1);
115: assertTrue(built.getNodes().size() == n + 1);
116:
117: assertTrue(built.getNodesOfDegree(2).size() == n + 1);
118:
119: //ensure coordinates
120: GraphVisitor visitor = new GraphVisitor() {
121: public int visit(Graphable component) {
122: Edge e = (Edge) component;
123: XYNode a = (XYNode) e.getNodeA();
124: XYNode b = (XYNode) e.getNodeB();
125:
126: //coordinats should be a distance of sqrt(2)
127: if (b.getCoordinate().equals(base))
128: assertTrue(a.getCoordinate().equals(
129: new Coordinate(n, n)));
130: else
131: assertTrue(b.getCoordinate().equals(
132: new Coordinate(a.getCoordinate().x + 1, a
133: .getCoordinate().y + 1)));
134:
135: // if (b.getX() == base.x && b.getY() == base.y)
136: // assertTrue(a.getX() == n && a.getY() == n);
137: // else assertTrue(b.getX() == a.getX() + 1 && b.getY() == a.getY() + 1);
138:
139: return (0);
140: }
141: };
142: built.visitEdges(visitor);
143: }
144:
145: protected OptDirectedLineGraphGenerator createGenerator() {
146: return (new OptDirectedLineGraphGenerator());
147: }
148:
149: protected OptDirectedLineGraphGenerator generator() {
150: return (m_gen);
151: }
152: }
|