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.BasicLineGraphGenerator;
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.Node;
027:
028: import com.vividsolutions.jts.geom.Coordinate;
029: import com.vividsolutions.jts.geom.LineSegment;
030:
031: public class LineGraphGeneratorTest extends TestCase {
032:
033: private BasicLineGraphGenerator m_gen;
034:
035: public LineGraphGeneratorTest(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: e.setID(i - 1);
062: e.getNodeA().setID(i - 1);
063: e.getNodeB().setID(i);
064: }
065:
066: Graph built = generator().getGraph();
067:
068: //ensure correct graph structure
069: assertTrue(built.getEdges().size() == n);
070: assertTrue(built.getNodes().size() == n + 1);
071:
072: GraphVisitor visitor = new GraphVisitor() {
073: public int visit(Graphable component) {
074: Node node = (Node) component;
075: Coordinate c = (Coordinate) node.getObject();
076:
077: if (node.getDegree() == 1) {
078: assertTrue(node.getID() == 0 || node.getID() == n);
079: } else {
080: assertTrue(node.getDegree() == 2);
081: }
082:
083: assertTrue(c.x == base.x + node.getID()
084: && c.y == base.y + node.getID());
085: return (0);
086: }
087: };
088: built.visitNodes(visitor);
089:
090: visitor = new GraphVisitor() {
091: public int visit(Graphable component) {
092: Edge edge = (Edge) component;
093: LineSegment line = (LineSegment) edge.getObject();
094:
095: assertTrue(line.p1.x == line.p0.x + 1
096: && line.p1.y == line.p0.y + 1);
097:
098: return (0);
099: }
100: };
101: built.visitEdges(visitor);
102:
103: }
104:
105: /**
106: * Build a circular graph of line segments that join at endpoints. <BR>
107: * <BR>
108: * Expected: 1. Number of edges = number of nodes = number of lines.
109: */
110: public void test_1() {
111: final Coordinate base = new Coordinate(0d, 0d);
112: final int n = 100;
113: for (int i = 1; i <= n; i++) {
114: Edge e = (Edge) generator().add(
115: new LineSegment(new Coordinate(base.x + (i - 1),
116: base.y + (i - 1)), new Coordinate(base.x
117: + i, base.y + i)));
118: e.setID(i - 1);
119: e.getNodeA().setID(i - 1);
120: e.getNodeB().setID(i);
121: }
122:
123: //complete the circle
124: generator().add(
125: new LineSegment(new Coordinate(base.x + n, base.y + n),
126: base));
127:
128: Graph built = generator().getGraph();
129:
130: assertTrue(built.getEdges().size() == n + 1);
131: assertTrue(built.getNodes().size() == n + 1);
132:
133: //all nodes should be of degree 2
134: assertTrue(built.getNodesOfDegree(2).size() == built.getNodes()
135: .size());
136: }
137:
138: protected BasicLineGraphGenerator createGenerator() {
139: return (new BasicLineGraphGenerator());
140: }
141:
142: protected BasicLineGraphGenerator generator() {
143: return (m_gen);
144: }
145: }
|