-
Notifications
You must be signed in to change notification settings - Fork 78
/
LayerTinfour.java
355 lines (319 loc) · 13.6 KB
/
LayerTinfour.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package org.noise_planet.noisemodelling.pathfinder;
import org.locationtech.jts.algorithm.Orientation;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.index.quadtree.Quadtree;
import org.locationtech.jts.io.WKBWriter;
import org.locationtech.jts.io.WKTWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tinfour.common.*;
import org.tinfour.standard.IncrementalTin;
import org.tinfour.utils.TriangleCollector;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.function.Consumer;
public class LayerTinfour implements LayerDelaunay {
private double epsilon = 0.001; // merge of Vertex instances below this distance
private static final Logger LOGGER = LoggerFactory.getLogger(LayerTinfour.class);
public String dumpFolder = "";
//private Map<Vertex, Integer> pts = new HashMap<Vertex, Integer>();
//private List<Integer> segments = new ArrayList<Integer>();
List<IConstraint> constraints = new ArrayList<>();
List<Integer> constraintIndex = new ArrayList<>();
Quadtree ptsIndex = new Quadtree();
private boolean computeNeighbors = false;
private double maxArea = 0;
// Output data
private List<Coordinate> vertices = new ArrayList<Coordinate>();
private List<Triangle> triangles = new ArrayList<Triangle>();
private List<Triangle> neighbors = new ArrayList<Triangle>(); // The first neighbor triangle is opposite the first corner of triangle i
private Vertex addCoordinate(Coordinate coordinate, int index) {
final Envelope env = new Envelope(coordinate);
env.expandBy(epsilon);
List result = ptsIndex.query(env);
Vertex found = null;
for(Object vertex : result) {
if(vertex instanceof Vertex) {
if(((Vertex) vertex).getDistance(coordinate.x, coordinate.y) < epsilon) {
found = (Vertex) vertex;
break;
}
}
}
if(found == null) {
found = new Vertex(coordinate.x, coordinate.y, Double.isNaN(coordinate.z) ? 0 : coordinate.z, index);
ptsIndex.insert(new Envelope(coordinate), found);
}
return found;
}
private List<SimpleTriangle> computeTriangles(IncrementalTin incrementalTin) {
ArrayList<SimpleTriangle> triangles = new ArrayList<>(incrementalTin.countTriangles().getCount());
TriangleBuilder triangleBuilder = new TriangleBuilder(triangles);
TriangleCollector.visitSimpleTriangles(incrementalTin, triangleBuilder);
return triangles;
}
/**
* @return When an exception occur, this folder with receiver the input data
*/
public String getDumpFolder() {
return dumpFolder;
}
/**
* @param dumpFolder When an exception occur, this folder with receiver the input data
*/
public void setDumpFolder(String dumpFolder) {
this.dumpFolder = dumpFolder;
}
/**
* @return Merge vertices closer than specified epsilon
*/
public double getEpsilon() {
return epsilon;
}
/**
* @param epsilon Merge vertices closer than specified epsilon
*/
public void setEpsilon(double epsilon) {
this.epsilon = epsilon;
}
private static class TriangleBuilder implements Consumer<SimpleTriangle> {
ArrayList<SimpleTriangle> triangles;
public TriangleBuilder(ArrayList<SimpleTriangle> triangles) {
this.triangles = triangles;
}
@Override
public void accept(SimpleTriangle triangle) {
triangles.add(triangle);
}
}
private static Coordinate getCentroid(SimpleTriangle triangle) {
Vertex va = triangle.getVertexA();
Vertex vb = triangle.getVertexB();
Vertex vc = triangle.getVertexC();
double cx = ( va.getX() + vb.getX() + vc.getX() ) / 3d;
double cy = ( va.getY() + vb.getY() + vc.getY() ) / 3d;
double cz = ( va.getZ() + vb.getZ() + vc.getZ() ) / 3d;
return new Coordinate( cx, cy, cz);
}
public void dumpData() {
GeometryFactory factory = new GeometryFactory();
WKTWriter wktWriter = new WKTWriter(3);
try {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File(dumpFolder, "tinfour_dump.csv")))) {
for(Object vObj : ptsIndex.queryAll()) {
if(vObj instanceof Vertex) {
final Vertex v = (Vertex)vObj;
Point p = factory.createPoint(toCoordinate(v));
writer.write(wktWriter.write(p));
writer.write("\n");
}
}
for (IConstraint constraint : constraints) {
if (constraint instanceof LinearConstraint) {
List<Vertex> vertices = constraint.getVertices();
Coordinate[] coordinates = new Coordinate[vertices.size()];
for (int i = 0; i < vertices.size(); i++) {
final Vertex v = vertices.get(i);
coordinates[i] = new Coordinate(v.getX(), v.getY(), v.getZ());
}
LineString l = factory.createLineString(coordinates);
writer.write(wktWriter.write(l));
writer.write("\n");
} else if (constraint instanceof PolygonConstraint) {
List<Vertex> vertices = constraint.getVertices();
if(vertices != null && vertices.size() >= 3) {
Coordinate[] coordinates = new Coordinate[vertices.size() + 1];
for (int i = 0; i < vertices.size() ; i++) {
final Vertex v = vertices.get(i);
coordinates[i] = new Coordinate(v.getX(), v.getY(), v.getZ());
}
coordinates[coordinates.length - 1] = coordinates[0];
Polygon l = factory.createPolygon(coordinates);
writer.write(wktWriter.write(l));
writer.write("\n");
} else {
LOGGER.info("Weird null polygon " + constraint);
}
}
}
}
} catch (IOException ioEx) {
// ignore
}
}
@Override
public void processDelaunay() throws LayerDelaunayError {
triangles.clear();
vertices.clear();
List<Vertex> meshPoints = ptsIndex.queryAll();
IncrementalTin tin;
boolean refine;
List<SimpleTriangle> simpleTriangles = new ArrayList<>();
do {
// Triangulate
tin = new IncrementalTin();
// Add points
tin.add(meshPoints, null);
// Add constraints
try {
tin.addConstraints(constraints, maxArea > 0);
}catch (IllegalStateException ex) {
// Got error
// Dump input data
if(!dumpFolder.isEmpty()) {
dumpData();
}
throw new LayerDelaunayError(ex);
}
refine = false;
simpleTriangles = computeTriangles(tin);
// Will triangulate multiple time if refinement is necessary
if(maxArea > 0) {
for (SimpleTriangle triangle : simpleTriangles) {
if(triangle.getArea() > maxArea) {
// Insert steiner point in circumcircle
Coordinate centroid = getCentroid(triangle);
meshPoints.add(new Vertex(centroid.x, centroid.y, centroid.z));
refine = true;
}
}
}
} while (refine);
List<Vertex> verts = tin.getVertices();
vertices = new ArrayList<>(verts.size());
Map<Vertex, Integer> vertIndex = new HashMap<>();
for(Vertex v : verts) {
vertIndex.put(v, vertices.size());
vertices.add(toCoordinate(v));
}
Map<Integer, Integer> edgeIndexToTriangleIndex = new HashMap<>();
for(SimpleTriangle t : simpleTriangles) {
int triangleAttribute = 0;
if(t.getContainingRegion() != null) {
if(t.getContainingRegion().getConstraintIndex() < constraintIndex.size()) {
triangleAttribute = constraintIndex.get(t.getContainingRegion().getConstraintIndex());
}
}
triangles.add(new Triangle(vertIndex.get(t.getVertexA()), vertIndex.get(t.getVertexB()),vertIndex.get(t.getVertexC()), triangleAttribute));
edgeIndexToTriangleIndex.put(t.getEdgeA().getIndex(), triangles.size() - 1);
edgeIndexToTriangleIndex.put(t.getEdgeB().getIndex(), triangles.size() - 1);
edgeIndexToTriangleIndex.put(t.getEdgeC().getIndex(), triangles.size() - 1);
}
if(computeNeighbors) {
for(SimpleTriangle t : simpleTriangles) {
Integer neighA = edgeIndexToTriangleIndex.get(t.getEdgeA().getDual().getIndex());
Integer neighB = edgeIndexToTriangleIndex.get(t.getEdgeB().getDual().getIndex());
Integer neighC = edgeIndexToTriangleIndex.get(t.getEdgeC().getDual().getIndex());
neighbors.add(new Triangle(neighA != null ? neighA : -1,
neighB != null ? neighB : -1,
neighC != null ? neighC : -1));
}
}
}
/**
* Add height of building
*
* @return
*/
@Override
public void addPolygon(Polygon newPoly, int buildingId) throws LayerDelaunayError {
final Coordinate[] coordinates = newPoly.getExteriorRing().getCoordinates();
// Exterior ring must be CCW
if(!Orientation.isCCW(coordinates)) {
CoordinateArrays.reverse(coordinates);
}
if (coordinates.length >= 4) {
List<Vertex> vertexList = new ArrayList<>();
for(int vId = 0; vId < coordinates.length - 1 ; vId++) {
vertexList.add(addCoordinate(coordinates[vId], buildingId));
}
PolygonConstraint polygonConstraint = new PolygonConstraint(vertexList);
polygonConstraint.complete();
if(polygonConstraint.isValid()) {
constraints.add(polygonConstraint);
constraintIndex.add(buildingId);
}
}
// Append holes
final int holeCount = newPoly.getNumInteriorRing();
for (int holeIndex = 0; holeIndex < holeCount; holeIndex++) {
LineString holeLine = newPoly.getInteriorRingN(holeIndex);
final Coordinate[] hCoordinates = holeLine.getCoordinates();
// Holes must be CW
if(Orientation.isCCW(hCoordinates)) {
CoordinateArrays.reverse(hCoordinates);
}
// Should be clock wise
List<Vertex> vertexList = new ArrayList<>();
for(int vId = 0; vId < hCoordinates.length - 1 ; vId++) {
vertexList.add(addCoordinate(hCoordinates[vId], buildingId));
}
PolygonConstraint polygonConstraint = new PolygonConstraint(vertexList);
polygonConstraint.complete();
if(polygonConstraint.isValid()) {
constraints.add(polygonConstraint);
constraintIndex.add(buildingId);
}
}
}
@Override
public void setMinAngle(Double minAngle) {
// TODO Auto-generated method stub
}
@Override
public void hintInit(Envelope bBox, long polygonCount, long verticesCount) throws LayerDelaunayError {
}
@Override
public List<Coordinate> getVertices() throws LayerDelaunayError {
return this.vertices;
}
@Override
public List<Triangle> getTriangles() throws LayerDelaunayError {
return triangles;
}
private static Coordinate toCoordinate(Vertex v) {
return new Coordinate(v.getX(), v.getY(), v.getZ());
}
@Override
public void addVertex(Coordinate vertexCoordinate) throws LayerDelaunayError {
addCoordinate(vertexCoordinate, 0);
}
@Override
public void setMaxArea(Double maxArea) throws LayerDelaunayError {
this.maxArea = Math.max(0, maxArea);
}
//add buildingID to edge property and to points property
public void addLineString(LineString lineToProcess, int buildingID) throws LayerDelaunayError {
Coordinate[] coordinates = lineToProcess.getCoordinates();
List<Vertex> vertexList = new ArrayList<>();
for(Coordinate coordinate : coordinates) {
vertexList.add(addCoordinate(coordinate, buildingID));
}
LinearConstraint linearConstraint = new LinearConstraint(vertexList);
linearConstraint.complete();
if(linearConstraint.isValid()) {
constraints.add(linearConstraint);
constraintIndex.add(buildingID);
}
}
//add buildingID to edge property and to points property
@Override
public void reset() {
// TODO Auto-generated method stub
}
@Override
public List<Triangle> getNeighbors() throws LayerDelaunayError {
if (computeNeighbors) {
return neighbors;
} else {
throw new LayerDelaunayError("You must call setRetrieveNeighbors(True) before process delaunay triangulation");
}
}
@Override
public void setRetrieveNeighbors(boolean retrieve) {
this.computeNeighbors = retrieve;
}
}