1 package org.opentrafficsim.core.geometry;
2
3 import java.awt.geom.Line2D;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.opentrafficsim.base.logger.Cat;
8 import org.opentrafficsim.core.network.NetworkException;
9
10 import com.vividsolutions.jts.geom.Coordinate;
11 import com.vividsolutions.jts.geom.Geometry;
12 import com.vividsolutions.jts.linearref.LengthIndexedLine;
13 import com.vividsolutions.jts.operation.buffer.BufferParameters;
14
15 import nl.tudelft.simulation.dsol.logger.SimLogger;
16
17
18
19
20
21
22
23
24
25
26
27 public final class OTSBufferingJTS
28 {
29
30 private static final int QUADRANTSEGMENTS = 16;
31
32
33
34
35 private OTSBufferingJTS()
36 {
37
38 }
39
40
41
42
43
44
45 private static double norm(final double angle)
46 {
47 double normalized = angle % (2 * Math.PI);
48 if (normalized < 0.0)
49 {
50 normalized += 2 * Math.PI;
51 }
52 return normalized;
53 }
54
55
56
57
58
59
60 private static double angle(final Coordinate c1, final Coordinate c2)
61 {
62 return norm(Math.atan2(c2.y - c1.y, c2.x - c1.x));
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76 public static double distanceLineSegmentToPoint(final OTSPoint3D lineP1, final OTSPoint3D lineP2, final OTSPoint3D point)
77 {
78 return closestPointOnSegmentToPoint(lineP1, lineP2, point).distanceSI(point);
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92 public static OTSPoint3D closestPointOnSegmentToPoint(final OTSPoint3D lineP1, final OTSPoint3D lineP2,
93 final OTSPoint3D point)
94 {
95 double dX = lineP2.x - lineP1.x;
96 double dY = lineP2.y - lineP1.y;
97 if ((0 == dX) && (0 == dY))
98 {
99 return lineP1;
100 }
101 final double u = ((point.x - lineP1.x) * dX + (point.y - lineP1.y) * dY) / (dX * dX + dY * dY);
102 if (u < 0)
103 {
104 return lineP1;
105 }
106 else if (u > 1)
107 {
108 return lineP2;
109 }
110 else
111 {
112 return new OTSPoint3D(lineP1.x + u * dX, lineP1.y + u * dY);
113 }
114 }
115
116
117
118
119
120
121
122 public static OTSLine3D offsetLine(final OTSLine3D referenceLine, final double offset)
123 {
124 try
125 {
126 double bufferOffset = Math.abs(offset);
127 final double precision = 0.00001;
128 if (bufferOffset < precision)
129 {
130 return referenceLine;
131 }
132 final double circlePrecision = 0.001;
133 List<OTSPoint3D> points = new ArrayList<>();
134
135 OTSPoint3D prevPoint = referenceLine.get(0);
136 Double prevAngle = null;
137 for (int index = 0; index < referenceLine.size() - 1; index++)
138 {
139 OTSPoint3D nextPoint = referenceLine.get(index + 1);
140 double angle = Math.atan2(nextPoint.y - prevPoint.y, nextPoint.x - prevPoint.x);
141 OTSPoint3D segmentFrom =
142 new OTSPoint3D(prevPoint.x - Math.sin(angle) * offset, prevPoint.y + Math.cos(angle) * offset);
143 OTSPoint3D segmentTo =
144 new OTSPoint3D(nextPoint.x - Math.sin(angle) * offset, nextPoint.y + Math.cos(angle) * offset);
145 if (index > 0)
146 {
147 double deltaAngle = angle - prevAngle;
148 if (Math.abs(deltaAngle) > Math.PI)
149 {
150 deltaAngle -= Math.signum(deltaAngle) * 2 * Math.PI;
151 }
152 if (deltaAngle * offset > 0)
153 {
154
155
156 OTSPoint3D pPoint = null;
157 for (int i = 0; i < points.size(); i++)
158 {
159 OTSPoint3D p = points.get(i);
160 if (Double.isNaN(p.z))
161 {
162 continue;
163 }
164 if (null != pPoint)
165 {
166 double pAngle = Math.atan2(p.y - pPoint.y, p.x - pPoint.x);
167 double totalAngle = angle - pAngle;
168 if (Math.abs(totalAngle) > Math.PI)
169 {
170 totalAngle += Math.signum(totalAngle) * 2 * Math.PI;
171 }
172 if (Math.abs(totalAngle) > 0.01)
173 {
174
175
176 OTSPoint3D intermediatePoint =
177 intersectionOfLineSegments(pPoint, p, segmentFrom, segmentTo);
178 if (null != intermediatePoint)
179 {
180
181 intermediatePoint =
182 new OTSPoint3D(intermediatePoint.x, intermediatePoint.y, Double.NaN);
183
184
185 points.add(intermediatePoint);
186 }
187 }
188 }
189 pPoint = p;
190 }
191 }
192 else
193 {
194
195
196
197 int numSegments = 1;
198 if (Math.abs(deltaAngle) > Math.PI / 2)
199 {
200 numSegments = 2;
201 }
202 for (; numSegments < 1000; numSegments *= 2)
203 {
204 double maxError = bufferOffset * (1 - Math.abs(Math.cos(deltaAngle / numSegments / 2)));
205 if (maxError < circlePrecision)
206 {
207 break;
208 }
209 }
210
211 for (int additionalPoint = 1; additionalPoint < numSegments; additionalPoint++)
212 {
213 double intermediateAngle =
214 (additionalPoint * angle + (numSegments - additionalPoint) * prevAngle) / numSegments;
215 if (prevAngle * angle < 0 && Math.abs(prevAngle) > Math.PI / 2 && Math.abs(angle) > Math.PI / 2)
216 {
217 intermediateAngle += Math.PI;
218 }
219 OTSPoint3D intermediatePoint = new OTSPoint3D(prevPoint.x - Math.sin(intermediateAngle) * offset,
220 prevPoint.y + Math.cos(intermediateAngle) * offset);
221
222
223 points.add(intermediatePoint);
224 }
225 }
226 }
227 points.add(segmentFrom);
228 points.add(segmentTo);
229 prevPoint = nextPoint;
230 prevAngle = angle;
231 }
232
233
234
235 for (int index = 1; index < points.size() - 1; index++)
236 {
237 OTSPoint3D checkPoint = points.get(index);
238 prevPoint = null;
239 boolean tooClose = false;
240 boolean somewhereAtCorrectDistance = false;
241 for (int i = 0; i < referenceLine.size(); i++)
242 {
243 OTSPoint3D p = referenceLine.get(i);
244 if (null != prevPoint)
245 {
246 OTSPoint3D closestPoint = closestPointOnSegmentToPoint(prevPoint, p, checkPoint);
247 if (closestPoint != referenceLine.get(0) && closestPoint != referenceLine.get(referenceLine.size() - 1))
248 {
249 double distance = closestPoint.horizontalDistanceSI(checkPoint);
250 if (distance < bufferOffset - circlePrecision)
251 {
252
253
254 tooClose = true;
255 break;
256 }
257 else if (distance < bufferOffset + precision)
258 {
259 somewhereAtCorrectDistance = true;
260 }
261 }
262 }
263 prevPoint = p;
264 }
265 if (tooClose || !somewhereAtCorrectDistance)
266 {
267
268 points.remove(index);
269 index--;
270 }
271 }
272
273 for (int index = 0; index < points.size(); index++)
274 {
275 OTSPoint3D p = points.get(index);
276 if (Double.isNaN(p.z))
277 {
278 points.set(index, new OTSPoint3D(p.x, p.y, 0));
279 }
280 }
281 return OTSLine3D.createAndCleanOTSLine3D(points);
282 }
283 catch (OTSGeometryException exception)
284 {
285 SimLogger.always().error(exception, "Exception in offsetLine - should never happen");
286 return null;
287 }
288 }
289
290
291
292
293
294
295
296
297
298 private static OTSPoint3D intersectionOfLineSegments(final OTSPoint3D line1P1, final OTSPoint3D line1P2,
299 final OTSPoint3D line2P1, final OTSPoint3D line2P2)
300 {
301 double denominator =
302 (line2P2.y - line2P1.y) * (line1P2.x - line1P1.x) - (line2P2.x - line2P1.x) * (line1P2.y - line1P1.y);
303 if (denominator == 0f)
304 {
305 return null;
306 }
307 double uA = ((line2P2.x - line2P1.x) * (line1P1.y - line2P1.y) - (line2P2.y - line2P1.y) * (line1P1.x - line2P1.x))
308 / denominator;
309 if ((uA < 0f) || (uA > 1f))
310 {
311 return null;
312 }
313 double uB = ((line1P2.x - line1P1.x) * (line1P1.y - line2P1.y) - (line1P2.y - line1P1.y) * (line1P1.x - line2P1.x))
314 / denominator;
315 if (uB < 0 || uB > 1)
316 {
317 return null;
318 }
319 return new OTSPoint3D(line1P1.x + uA * (line1P2.x - line1P1.x), line1P1.y + uA * (line1P2.y - line1P1.y), 0);
320 }
321
322
323
324
325
326
327
328
329 @SuppressWarnings("checkstyle:methodlength")
330 public static OTSLine3D offsetGeometryOLD(final OTSLine3D referenceLine, final double offset) throws OTSGeometryException
331 {
332 Coordinate[] referenceCoordinates = referenceLine.getCoordinates();
333
334 double bufferOffset = Math.abs(offset);
335 final double precision = 0.000001;
336 if (bufferOffset < precision)
337 {
338
339 return new OTSLine3D(referenceCoordinates);
340 }
341 Geometry geometryLine = referenceLine.getLineString();
342 Coordinate[] bufferCoordinates =
343 geometryLine.buffer(bufferOffset, QUADRANTSEGMENTS, BufferParameters.CAP_FLAT).getCoordinates();
344
345
346
347
348
349 Coordinate sC0 = referenceCoordinates[0];
350 Coordinate sC1 = referenceCoordinates[1];
351 Coordinate eCm1 = referenceCoordinates[referenceCoordinates.length - 1];
352 Coordinate eCm2 = referenceCoordinates[referenceCoordinates.length - 2];
353
354 double expectedStartAngle = norm(angle(sC0, sC1) + Math.signum(offset) * Math.PI / 2.0);
355 double expectedEndAngle = norm(angle(eCm2, eCm1) + Math.signum(offset) * Math.PI / 2.0);
356 Coordinate sExpected = new Coordinate(sC0.x + bufferOffset * Math.cos(expectedStartAngle),
357 sC0.y + bufferOffset * Math.sin(expectedStartAngle));
358 Coordinate eExpected = new Coordinate(eCm1.x + bufferOffset * Math.cos(expectedEndAngle),
359 eCm1.y + bufferOffset * Math.sin(expectedEndAngle));
360
361
362 double dS = Double.MAX_VALUE;
363 double dE = Double.MAX_VALUE;
364 int sIndex = -1;
365 int eIndex = -1;
366 for (int i = 0; i < bufferCoordinates.length; i++)
367 {
368 Coordinate c = bufferCoordinates[i];
369 double dsc = c.distance(sExpected);
370 double dec = c.distance(eExpected);
371 if (dsc < dS)
372 {
373 dS = dsc;
374 sIndex = i;
375 }
376 if (dec < dE)
377 {
378 dE = dec;
379 eIndex = i;
380 }
381 }
382
383 if (sIndex == -1)
384 {
385 throw new OTSGeometryException("offsetGeometry: startIndex not found for line " + referenceLine);
386 }
387 if (eIndex == -1)
388 {
389 throw new OTSGeometryException("offsetGeometry: endIndex not found for line " + referenceLine);
390 }
391 if (dS > 0.01)
392 {
393 SimLogger.filter(Cat.CORE).trace(referenceLine.toExcel() + "\n\n\n\n" + new OTSLine3D(bufferCoordinates).toExcel()
394 + "\n\n\n\n" + sExpected + "\n" + eExpected);
395 throw new OTSGeometryException("offsetGeometry: startDistance too big (" + dS + ") for line " + referenceLine);
396 }
397 if (dE > 0.01)
398 {
399 throw new OTSGeometryException("offsetGeometry: endDistance too big (" + dE + ") for line " + referenceLine);
400 }
401
402
403 boolean ok = true;
404 int i = sIndex;
405 Coordinate lastC = null;
406 List<OTSPoint3D> result = new ArrayList<>();
407 while (ok)
408 {
409 Coordinate c = bufferCoordinates[i];
410 if (lastC != null && close(c, lastC, sC0, eCm1))
411 {
412 ok = false;
413 break;
414 }
415 result.add(new OTSPoint3D(c));
416 if (i == eIndex)
417 {
418 return OTSLine3D.createAndCleanOTSLine3D(result);
419 }
420 i = (i == bufferCoordinates.length - 1) ? 0 : i + 1;
421 lastC = c;
422 }
423
424
425 ok = true;
426 i = sIndex;
427 lastC = null;
428 result = new ArrayList<>();
429 while (ok)
430 {
431 Coordinate c = bufferCoordinates[i];
432 if (lastC != null && close(c, lastC, sC0, eCm1))
433 {
434 ok = false;
435 break;
436 }
437 result.add(new OTSPoint3D(c));
438 if (i == eIndex)
439 {
440 return OTSLine3D.createAndCleanOTSLine3D(result);
441 }
442 i = (i == 0) ? bufferCoordinates.length - 1 : i - 1;
443 lastC = c;
444 }
445
446
447
448 throw new OTSGeometryException("offsetGeometry: could not find offset in either direction for line " + referenceLine);
449 }
450
451
452
453
454
455
456
457
458 private static boolean close(final Coordinate lineC1, final Coordinate lineC2, final Coordinate... check)
459 {
460 Line2D.Double line = new Line2D.Double(lineC1.x, lineC1.y, lineC2.x, lineC2.y);
461 for (Coordinate c : check)
462 {
463 if (line.ptSegDist(c.x, c.y) < 0.01)
464 {
465 return true;
466 }
467 }
468 return false;
469 }
470
471
472
473
474
475
476
477
478
479
480 public static OTSLine3D offsetLine(final OTSLine3D referenceLine, final double offsetAtStart, final double offsetAtEnd)
481 throws OTSGeometryException
482 {
483
484
485
486 OTSLine3D offsetLineAtStart = offsetLine(referenceLine, offsetAtStart);
487 if (offsetAtStart == offsetAtEnd)
488 {
489 return offsetLineAtStart;
490 }
491
492
493 OTSLine3D offsetLineAtEnd = offsetLine(referenceLine, offsetAtEnd);
494
495
496 Geometry startGeometry = offsetLineAtStart.getLineString();
497 Geometry endGeometry = offsetLineAtEnd.getLineString();
498 LengthIndexedLine first = new LengthIndexedLine(startGeometry);
499 double firstLength = startGeometry.getLength();
500 LengthIndexedLine second = new LengthIndexedLine(endGeometry);
501 double secondLength = endGeometry.getLength();
502 ArrayList<Coordinate> out = new ArrayList<Coordinate>();
503 Coordinate[] firstCoordinates = startGeometry.getCoordinates();
504 Coordinate[] secondCoordinates = endGeometry.getCoordinates();
505 int firstIndex = 0;
506 int secondIndex = 0;
507 Coordinate prevCoordinate = null;
508 final double tooClose = 0.05;
509 while (firstIndex < firstCoordinates.length && secondIndex < secondCoordinates.length)
510 {
511 double firstRatio = firstIndex < firstCoordinates.length ? first.indexOf(firstCoordinates[firstIndex]) / firstLength
512 : Double.MAX_VALUE;
513 double secondRatio = secondIndex < secondCoordinates.length
514 ? second.indexOf(secondCoordinates[secondIndex]) / secondLength : Double.MAX_VALUE;
515 double ratio;
516 if (firstRatio < secondRatio)
517 {
518 ratio = firstRatio;
519 firstIndex++;
520 }
521 else
522 {
523 ratio = secondRatio;
524 secondIndex++;
525 }
526 Coordinate firstCoordinate = first.extractPoint(ratio * firstLength);
527 Coordinate secondCoordinate = second.extractPoint(ratio * secondLength);
528 Coordinate resultCoordinate = new Coordinate((1 - ratio) * firstCoordinate.x + ratio * secondCoordinate.x,
529 (1 - ratio) * firstCoordinate.y + ratio * secondCoordinate.y);
530 if (null == prevCoordinate || resultCoordinate.distance(prevCoordinate) > tooClose)
531 {
532 out.add(resultCoordinate);
533 prevCoordinate = resultCoordinate;
534 }
535 }
536 Coordinate[] resultCoordinates = new Coordinate[out.size()];
537 for (int index = 0; index < out.size(); index++)
538 {
539 resultCoordinates[index] = out.get(index);
540 }
541 return new OTSLine3D(resultCoordinates);
542 }
543
544
545
546
547
548
549 public static void main(final String[] args) throws NetworkException, OTSGeometryException
550 {
551
552
553
554
555
556 OTSLine3D line = new OTSLine3D(new OTSPoint3D[] { new OTSPoint3D(-579.253, 60.157, 4.710),
557 new OTSPoint3D(-579.253, 60.144, 4.712), new OTSPoint3D(-579.253, 60.144, 0.000),
558 new OTSPoint3D(-579.251, 60.044, 0.000), new OTSPoint3D(-579.246, 59.944, 0.000),
559 new OTSPoint3D(-579.236, 59.845, 0.000), new OTSPoint3D(-579.223, 59.746, 0.000),
560 new OTSPoint3D(-579.206, 59.647, 0.000), new OTSPoint3D(-579.185, 59.549, 0.000),
561 new OTSPoint3D(-579.161, 59.452, 0.000), new OTSPoint3D(-579.133, 59.356, 0.000),
562 new OTSPoint3D(-579.101, 59.261, 0.000), new OTSPoint3D(-579.066, 59.168, 0.000),
563 new OTSPoint3D(-579.028, 59.075, 0.000), new OTSPoint3D(-578.986, 58.985, 0.000),
564 new OTSPoint3D(-578.940, 58.896, 0.000), new OTSPoint3D(-578.891, 58.809, 0.000),
565 new OTSPoint3D(-578.839, 58.723, 0.000), new OTSPoint3D(-578.784, 58.640, 0.000),
566 new OTSPoint3D(-578.725, 58.559, 0.000), new OTSPoint3D(-578.664, 58.480, 0.000),
567 new OTSPoint3D(-578.599, 58.403, 0.000), new OTSPoint3D(-578.532, 58.329, 0.000),
568 new OTSPoint3D(-578.462, 58.258, 0.000), new OTSPoint3D(-578.390, 58.189, 0.000),
569 new OTSPoint3D(-578.314, 58.123, 0.000), new OTSPoint3D(-578.237, 58.060, 0.000),
570 new OTSPoint3D(-578.157, 58.000, 0.000), new OTSPoint3D(-578.075, 57.943, 0.000),
571 new OTSPoint3D(-577.990, 57.889, 0.000), new OTSPoint3D(-577.904, 57.839, 0.000),
572 new OTSPoint3D(-577.816, 57.791, 0.000), new OTSPoint3D(-577.726, 57.747, 0.000),
573 new OTSPoint3D(-577.635, 57.707, 0.000), new OTSPoint3D(-577.542, 57.670, 0.000),
574 new OTSPoint3D(-577.448, 57.636, 0.000), new OTSPoint3D(-577.352, 57.606, 0.000),
575 new OTSPoint3D(-577.256, 57.580, 0.000), new OTSPoint3D(-577.159, 57.557, 0.000),
576 new OTSPoint3D(-577.060, 57.538, 0.000), new OTSPoint3D(-576.962, 57.523, 0.000),
577 new OTSPoint3D(-576.862, 57.512, 0.000), new OTSPoint3D(-576.763, 57.504, 0.000),
578 new OTSPoint3D(-576.663, 57.500, 0.000), new OTSPoint3D(-576.623, 57.500, 6.278),
579 new OTSPoint3D(-576.610, 57.500, 6.280), new OTSPoint3D(-567.499, 57.473, 6.280) });
580 System.out.println(line.toExcel());
581 System.out.println(OTSBufferingJTS.offsetGeometryOLD(line, -1.831));
582 }
583 }