/
ContourBuilder.java
158 lines (143 loc) · 6.42 KB
/
ContourBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package com.graphhopper.isochrone.algorithm;
import com.vividsolutions.jts.algorithm.CGAlgorithms;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.triangulate.quadedge.QuadEdge;
import com.vividsolutions.jts.triangulate.quadedge.QuadEdgeSubdivision;
import java.util.*;
/**
*
* Adapted from org.opentripplanner.common.geometry.DelaunayIsolineBuilder,
* which is under LGPL.
*
* @author laurent
* @author michaz
*
*/
public class ContourBuilder {
private static final double EPSILON = 0.000001;
private QuadEdgeSubdivision triangulation;
private GeometryFactory geometryFactory = new GeometryFactory();
public ContourBuilder(QuadEdgeSubdivision triangulation) {
this.triangulation = triangulation;
}
public Geometry computeIsoline(double z0) {
Set<QuadEdge> processed = new HashSet<>();
List<LinearRing> rings = new ArrayList<>();
Queue<QuadEdge> processQ = new ArrayDeque<>(getPrimaryEdges());
while (!processQ.isEmpty()) {
QuadEdge e = processQ.remove();
if (processed.contains(e))
continue;
processed.add(e);
int cut = cut(e.orig().getZ(), e.dest().getZ(), z0);
if (cut == 0) {
continue; // While, next edge
}
List<Coordinate> polyPoints = new ArrayList<>();
boolean ccw = cut > 0;
while (true) {
// Add a point to polyline
Coordinate cC;
if (triangulation.isFrameVertex(e.orig())) {
cC = moveEpsilonTowards(e.dest().getCoordinate(), e.orig().getCoordinate());
} else if (triangulation.isFrameVertex(e.dest())) {
cC = moveEpsilonTowards(e.orig().getCoordinate(), e.dest().getCoordinate());
} else {
cC = e.orig().midPoint(e.dest()).getCoordinate();
}
polyPoints.add(cC);
processed.add(e);
QuadEdge E1 = ccw ? e.oNext().getPrimary() : e.oPrev().getPrimary();
QuadEdge E2 = ccw ? e.dPrev().getPrimary() : e.dNext().getPrimary();
int cut1 = E1 == null ? 0 : cut(E1.orig().getZ(), E1.dest().getZ(), z0);
int cut2 = E2 == null ? 0 : cut(E2.orig().getZ(), E2.dest().getZ(), z0);
boolean ok1 = cut1 != 0 && !processed.contains(E1);
boolean ok2 = cut2 != 0 && !processed.contains(E2);
if (ok1) {
e = E1;
ccw = cut1 > 0;
} else if (ok2) {
e = E2;
ccw = cut2 > 0;
} else {
// This must be the end of the polyline...
break;
}
}
// Close the polyline
polyPoints.add(polyPoints.get(0));
if (polyPoints.size() >= 4) {
LinearRing ring = geometryFactory.createLinearRing(polyPoints
.toArray(new Coordinate[polyPoints.size()]));
rings.add(ring);
}
}
List<Polygon> isolinePolygons = punchHoles(rings);
return geometryFactory.createMultiPolygon(isolinePolygons.toArray(new Polygon[isolinePolygons.size()]));
}
@SuppressWarnings("unchecked") // JTS is not generified
private Collection<QuadEdge> getPrimaryEdges() {
return (Collection<QuadEdge>) triangulation.getPrimaryEdges(true);
}
private Coordinate moveEpsilonTowards(Coordinate coordinate, Coordinate distantFrameCoordinate) {
return new Coordinate(coordinate.x + EPSILON * (distantFrameCoordinate.x - coordinate.x), coordinate.y + EPSILON * (distantFrameCoordinate.y - coordinate.y));
}
private int cut(double za, double zb, double z0) {
if (za < z0 && zb > z0) return 1;
if (za > z0 && zb < z0) return -1;
return 0;
}
@SuppressWarnings("unchecked")
private List<Polygon> punchHoles(List<LinearRing> rings) {
List<Polygon> shells = new ArrayList<>(rings.size());
List<LinearRing> holes = new ArrayList<>(rings.size() / 2);
// 1. Split the polygon list in two: shells and holes (CCW and CW)
for (LinearRing ring : rings) {
if (CGAlgorithms.signedArea(ring.getCoordinateSequence()) > 0.0)
holes.add(ring);
else
shells.add(geometryFactory.createPolygon(ring));
}
// 2. Sort the shells based on number of points to optimize step 3.
Collections.sort(shells, new Comparator<Polygon>() {
@Override
public int compare(Polygon o1, Polygon o2) {
return o2.getNumPoints() - o1.getNumPoints();
}
});
for (Polygon shell : shells) {
shell.setUserData(new ArrayList<LinearRing>());
}
// 3. For each hole, determine which shell it fits in.
for (LinearRing hole : holes) {
outer: {
// Probably most of the time, the first shell will be the one
for (Polygon shell : shells) {
if (shell.contains(hole)) {
((List<LinearRing>) shell.getUserData()).add(hole);
break outer;
}
}
throw new RuntimeException("Found a hole without a shell.");
}
}
// 4. Build the list of punched polygons
List<Polygon> punched = new ArrayList<>(shells.size());
for (Polygon shell : shells) {
List<LinearRing> shellHoles = ((List<LinearRing>) shell.getUserData());
punched.add(geometryFactory.createPolygon((LinearRing) (shell.getExteriorRing()),
shellHoles.toArray(new LinearRing[shellHoles.size()])));
}
return punched;
}
}