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.BasicDirectedLineGraphGenerator;
022: import org.geotools.graph.structure.DirectedEdge;
023: import org.geotools.graph.structure.DirectedNode;
024: import org.geotools.graph.structure.Edge;
025: import org.geotools.graph.structure.Graph;
026: import org.geotools.graph.structure.GraphVisitor;
027: import org.geotools.graph.structure.Graphable;
028:
029: import com.vividsolutions.jts.geom.Coordinate;
030: import com.vividsolutions.jts.geom.LineSegment;
031:
032: public class DirectedLineGraphGeneratorTest extends TestCase {
033:
034: private BasicDirectedLineGraphGenerator m_gen;
035:
036: public DirectedLineGraphGeneratorTest(String name) {
037: super (name);
038: }
039:
040: protected void setUp() throws Exception {
041: super .setUp();
042:
043: m_gen = createGenerator();
044: }
045:
046: /**
047: * Build a linear graph by adding a number of line segments that join
048: * at endpoints.
049: * <BR>
050: * Expected: 1. Number of edges = number of lines added.
051: * 2. Number of nodes = number of lines + 1
052: *
053: */
054: public void test_0() {
055: final Coordinate base = new Coordinate(0d, 0d);
056: final int n = 100;
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: e.setID(i - 1);
063: e.getNodeA().setID(i - 1);
064: e.getNodeB().setID(i);
065: }
066:
067: Graph built = generator().getGraph();
068:
069: //ensure correct graph structure
070: assertTrue(built.getEdges().size() == n);
071: assertTrue(built.getNodes().size() == n + 1);
072:
073: GraphVisitor visitor = new GraphVisitor() {
074: public int visit(Graphable component) {
075: DirectedNode node = (DirectedNode) component;
076: Coordinate c = (Coordinate) node.getObject();
077:
078: if (node.getDegree() == 1) {
079: assertTrue((node.getID() == 0
080: && node.getInDegree() == 0 && node
081: .getOutDegree() == 1)
082: || (node.getID() == n
083: && node.getInDegree() == 1 && node
084: .getOutDegree() == 0));
085: } else {
086: assertTrue(node.getInDegree() == 1
087: && node.getOutDegree() == 1);
088: }
089:
090: assertTrue(c.x == base.x + node.getID()
091: && c.y == base.y + node.getID());
092: return (0);
093: }
094: };
095: built.visitNodes(visitor);
096:
097: //ensure correct edge direction
098: visitor = new GraphVisitor() {
099: public int visit(Graphable component) {
100: DirectedEdge e = (DirectedEdge) component;
101: Coordinate c0 = (Coordinate) e.getInNode().getObject();
102: Coordinate c1 = (Coordinate) e.getOutNode().getObject();
103: LineSegment ls = (LineSegment) e.getObject();
104:
105: assertTrue(ls.p0.equals(c0) && ls.p1.equals(c1));
106:
107: return (0);
108: }
109: };
110: }
111:
112: /**
113: * Build a circular graph of line segments that join at endpoints. <BR>
114: * <BR>
115: * Expected: 1. Number of edges = number of nodes = number of lines.
116: */
117: public void test_1() {
118: final Coordinate base = new Coordinate(0d, 0d);
119: final int n = 100;
120: for (int i = 1; i <= n; i++) {
121: Edge e = (Edge) generator().add(
122: new LineSegment(new Coordinate(base.x + (i - 1),
123: base.y + (i - 1)), new Coordinate(base.x
124: + i, base.y + i)));
125: e.setID(i - 1);
126: e.getNodeA().setID(i - 1);
127: e.getNodeB().setID(i);
128: }
129:
130: //complete the circle
131: generator().add(
132: new LineSegment(new Coordinate(base.x + n, base.y + n),
133: base));
134:
135: Graph built = generator().getGraph();
136:
137: assertTrue(built.getEdges().size() == n + 1);
138: assertTrue(built.getNodes().size() == n + 1);
139:
140: GraphVisitor visitor = new GraphVisitor() {
141: public int visit(Graphable component) {
142: DirectedNode node = (DirectedNode) component;
143: assertTrue(node.getInDegree() == 1
144: && node.getOutDegree() == 1);
145: return 0;
146: }
147: };
148: }
149:
150: protected BasicDirectedLineGraphGenerator createGenerator() {
151: return (new BasicDirectedLineGraphGenerator());
152: }
153:
154: protected BasicDirectedLineGraphGenerator generator() {
155: return (m_gen);
156: }
157: }
|