1 package org.opentrafficsim.core.geometry;
2
3 import java.awt.geom.Point2D;
4 import java.io.Serializable;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import javax.media.j3d.BoundingSphere;
9 import javax.media.j3d.Bounds;
10 import javax.vecmath.Point3d;
11
12 import org.djunits.unit.LengthUnit;
13 import org.djunits.value.vdouble.scalar.Length;
14
15 import com.vividsolutions.jts.geom.Coordinate;
16 import com.vividsolutions.jts.geom.Point;
17
18 import nl.tudelft.simulation.dsol.animation.Locatable;
19 import nl.tudelft.simulation.language.d3.CartesianPoint;
20 import nl.tudelft.simulation.language.d3.DirectedPoint;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class OTSPoint3D implements Locatable, Serializable
36 {
37
38 private static final long serialVersionUID = 20150722L;
39
40
41 @SuppressWarnings("checkstyle:visibilitymodifier")
42 public final double x;
43
44
45 @SuppressWarnings("checkstyle:visibilitymodifier")
46 public final double y;
47
48
49 @SuppressWarnings("checkstyle:visibilitymodifier")
50 public final double z;
51
52
53
54
55
56
57
58 public OTSPoint3D(final double x, final double y, final double z)
59 {
60 this.x = x;
61 this.y = y;
62 this.z = z;
63 }
64
65
66
67
68 public OTSPoint3D(final double[] xyz)
69 {
70 this(xyz[0], xyz[1], (xyz.length > 2) ? xyz[2] : 0.0);
71 }
72
73
74
75
76 public OTSPoint3D(final OTSPoint3D point)
77 {
78 this(point.x, point.y, point.z);
79 }
80
81
82
83
84
85 public OTSPoint3D(final Point3d point)
86 {
87 this(point.x, point.y, point.z);
88 }
89
90
91
92
93
94 public OTSPoint3D(final CartesianPoint point)
95 {
96 this(point.x, point.y, point.z);
97 }
98
99
100
101
102
103 public OTSPoint3D(final DirectedPoint point)
104 {
105 this(point.x, point.y, point.z);
106 }
107
108
109
110
111
112 public OTSPoint3D(final Point2D point2d)
113 {
114 this(point2d.getX(), point2d.getY(), 0.0);
115 }
116
117
118
119
120
121 public OTSPoint3D(final Coordinate coordinate)
122 {
123 this(coordinate.x, coordinate.y, Double.isNaN(coordinate.z) ? 0.0 : coordinate.z);
124 }
125
126
127
128
129
130 public OTSPoint3D(final Point point)
131 {
132 this(point.getX(), point.getY(), 0.0);
133 }
134
135
136
137
138
139
140 public OTSPoint3D(final double x, final double y)
141 {
142 this(x, y, 0.0);
143 }
144
145
146
147
148
149
150
151
152 public static OTSPoint3D interpolate(final double ratio, final OTSPoint3D zeroValue, final OTSPoint3D oneValue)
153 {
154 double complement = 1 - ratio;
155 return new OTSPoint3D(complement * zeroValue.x + ratio * oneValue.x, complement * zeroValue.y + ratio * oneValue.y,
156 complement * zeroValue.z + ratio * oneValue.z);
157 }
158
159
160
161
162
163
164
165
166
167
168
169 @Deprecated
170 public static OTSPoint3D intersectionOfLineSegmentsDumb(final OTSPoint3D line1P1, final OTSPoint3D line1P2,
171 final OTSPoint3D line2P1, final OTSPoint3D line2P2)
172 {
173 double denominator =
174 (line2P2.y - line2P1.y) * (line1P2.x - line1P1.x) - (line2P2.x - line2P1.x) * (line1P2.y - line1P1.y);
175 if (denominator == 0f)
176 {
177 return null;
178 }
179 double uA = ((line2P2.x - line2P1.x) * (line1P1.y - line2P1.y) - (line2P2.y - line2P1.y) * (line1P1.x - line2P1.x))
180 / denominator;
181 if ((uA < 0f) || (uA > 1f))
182 {
183 return null;
184 }
185 double uB = ((line1P2.x - line1P1.x) * (line1P1.y - line2P1.y) - (line1P2.y - line1P1.y) * (line1P1.x - line2P1.x))
186 / denominator;
187 if (uB < 0 || uB > 1)
188 {
189 return null;
190 }
191 return new OTSPoint3D(line1P1.x + uA * (line1P2.x - line1P1.x), line1P1.y + uA * (line1P2.y - line1P1.y), 0);
192 }
193
194
195
196
197
198
199
200
201
202
203 public static OTSPoint3D intersectionOfLineSegments(final OTSPoint3D line1P1, final OTSPoint3D line1P2,
204 final OTSPoint3D line2P1, final OTSPoint3D line2P2)
205 {
206 double l1p1x = line1P1.x;
207 double l1p1y = line1P1.y;
208 double l1p2x = line1P2.x - l1p1x;
209 double l1p2y = line1P2.y - l1p1y;
210 double l2p1x = line2P1.x - l1p1x;
211 double l2p1y = line2P1.y - l1p1y;
212 double l2p2x = line2P2.x - l1p1x;
213 double l2p2y = line2P2.y - l1p1y;
214 double denominator = (l2p2y - l2p1y) * l1p2x - (l2p2x - l2p1x) * l1p2y;
215 if (denominator == 0f)
216 {
217 return null;
218 }
219 double uA = ((l2p2x - l2p1x) * (-l2p1y) - (l2p2y - l2p1y) * (-l2p1x)) / denominator;
220
221 if ((uA < 0f) || (uA > 1f))
222 {
223 return null;
224 }
225 double uB = (l1p2y * l2p1x - l1p2x * l2p1y) / denominator;
226
227 if (uB < 0 || uB > 1)
228 {
229 return null;
230 }
231 return new OTSPoint3D(line1P1.x + uA * l1p2x, line1P1.y + uA * l1p2y, 0);
232 }
233
234
235
236
237
238
239
240
241
242
243 @Deprecated
244 public static OTSPoint3D intersectionOfLinesDumb(final OTSPoint3D line1P1, final OTSPoint3D line1P2,
245 final OTSPoint3D line2P1, final OTSPoint3D line2P2)
246 {
247 double determinant =
248 (line1P1.x - line1P2.x) * (line2P1.y - line2P2.y) - (line1P1.y - line1P2.y) * (line2P1.x - line2P2.x);
249 if (Math.abs(determinant) < 0.0000001)
250 {
251 return null;
252 }
253 return new OTSPoint3D(
254 ((line1P1.x * line1P2.y - line1P1.y * line1P2.x) * (line2P1.x - line2P2.x)
255 - (line1P1.x - line1P2.x) * (line2P1.x * line2P2.y - line2P1.y * line2P2.x)) / determinant,
256 ((line1P1.x * line1P2.y - line1P1.y * line1P2.x) * (line2P1.y - line2P2.y)
257 - (line1P1.y - line1P2.y) * (line2P1.x * line2P2.y - line2P1.y * line2P2.x)) / determinant);
258 }
259
260
261
262
263
264
265
266
267
268
269 public static OTSPoint3D intersectionOfLines(final OTSPoint3D line1P1, final OTSPoint3D line1P2, final OTSPoint3D line2P1,
270 final OTSPoint3D line2P2)
271 {
272 double l1p1x = line1P1.x;
273 double l1p1y = line1P1.y;
274 double l1p2x = line1P2.x - l1p1x;
275 double l1p2y = line1P2.y - l1p1y;
276 double l2p1x = line2P1.x - l1p1x;
277 double l2p1y = line2P1.y - l1p1y;
278 double l2p2x = line2P2.x - l1p1x;
279 double l2p2y = line2P2.y - l1p1y;
280 double determinant = (0 - l1p2x) * (l2p1y - l2p2y) - (0 - l1p2y) * (l2p1x - l2p2x);
281 if (Math.abs(determinant) < 0.0000001)
282 {
283 return null;
284 }
285 return new OTSPoint3D(l1p1x + (l1p2x * (l2p1x * l2p2y - l2p1y * l2p2x)) / determinant,
286 l1p1y + (l1p2y * (l2p1x * l2p2y - l2p1y * l2p2x)) / determinant);
287 }
288
289
290
291
292
293
294
295
296
297
298
299
300 public final OTSPoint3D closestPointOnSegment(final OTSPoint3D segmentPoint1, final OTSPoint3D segmentPoint2)
301 {
302 double dX = segmentPoint2.x - segmentPoint1.x;
303 double dY = segmentPoint2.y - segmentPoint1.y;
304 if ((0 == dX) && (0 == dY))
305 {
306 return segmentPoint1;
307 }
308 final double u = ((this.x - segmentPoint1.x) * dX + (this.y - segmentPoint1.y) * dY) / (dX * dX + dY * dY);
309 if (u < 0)
310 {
311 return segmentPoint1;
312 }
313 else if (u > 1)
314 {
315 return segmentPoint2;
316 }
317 else
318 {
319 return interpolate(u, segmentPoint1, segmentPoint2);
320 }
321 }
322
323
324
325
326
327
328
329
330 private OTSPoint3D internalClosestPointOnLine(final OTSLine3D line, final boolean useHorizontalDistance)
331 {
332 OTSPoint3D prevPoint = null;
333 double distance = Double.MAX_VALUE;
334 OTSPoint3D result = null;
335 for (OTSPoint3D nextPoint : line.getPoints())
336 {
337 if (null != prevPoint)
338 {
339 OTSPoint3D closest = closestPointOnSegment(prevPoint, nextPoint);
340 double thisDistance = useHorizontalDistance ? horizontalDistanceSI(closest) : distanceSI(closest);
341 if (thisDistance < distance)
342 {
343 result = closest;
344 distance = thisDistance;
345 }
346 }
347 prevPoint = nextPoint;
348 }
349 return result;
350 }
351
352
353
354
355
356
357 public final OTSPoint3D closestPointOnLine(final OTSLine3D line)
358 {
359 return internalClosestPointOnLine(line, false);
360 }
361
362
363
364
365
366
367
368 public final OTSPoint3D closestPointOnLine2D(final OTSLine3D line)
369 {
370 return internalClosestPointOnLine(line, true);
371 }
372
373
374
375
376
377 public final OTSPoint3D normalize()
378 {
379 double length = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
380 return this.translate(length);
381 }
382
383
384
385
386
387
388 public final OTSPoint3D translate(final double factor)
389 {
390 return new OTSPoint3D(this.x / factor, this.y / factor, this.z / factor);
391 }
392
393
394
395
396
397
398
399
400 public static final List<OTSPoint3D> circleCenter(final OTSPoint3D point1, final OTSPoint3D point2, final double radius)
401 {
402 List<OTSPoint3D> center = new ArrayList<>();
403 OTSPoint3D m = interpolate(0.5, point1, point2);
404 double h = point1.distanceSI(m);
405 if (radius < h)
406 {
407 return center;
408 }
409 if (radius == h)
410 {
411 center.add(m);
412 return center;
413 }
414 OTSPoint3D p = new OTSPoint3D(point2.y - point1.y, point1.x - point2.x).normalize();
415 double d = Math.sqrt(radius * radius - h * h);
416 center.add(new OTSPoint3D(m.x + d * p.x, m.y + d * p.y, m.z));
417 center.add(new OTSPoint3D(m.x - d * p.x, m.y - d * p.y, m.z));
418 return center;
419 }
420
421
422
423
424
425
426
427
428
429 public static final List<OTSPoint3D> circleIntersections(final OTSPoint3D center1, final double radius1,
430 final OTSPoint3D center2, final double radius2)
431 {
432 List<OTSPoint3D> center = new ArrayList<>();
433 OTSPoint3D m = interpolate(radius1 / (radius1 + radius2), center1, center2);
434 double h = center1.distanceSI(m);
435 if (radius1 < h)
436 {
437 return center;
438 }
439 if (radius1 == h)
440 {
441 center.add(m);
442 return center;
443 }
444 OTSPoint3D p = new OTSPoint3D(center2.y - center1.y, center1.x - center2.x).normalize();
445 double d = Math.sqrt(radius1 * radius1 - h * h);
446 center.add(new OTSPoint3D(m.x + d * p.x, m.y + d * p.y, m.z));
447 center.add(new OTSPoint3D(m.x - d * p.x, m.y - d * p.y, m.z));
448 return center;
449 }
450
451
452
453
454
455 public final double distanceSI(final OTSPoint3D point)
456 {
457 double dx = point.x - this.x;
458 double dy = point.y - this.y;
459 double dz = point.z - this.z;
460
461 return Math.sqrt(dx * dx + dy * dy + dz * dz);
462 }
463
464
465
466
467
468 public final double horizontalDistanceSI(final OTSPoint3D point)
469 {
470 double dx = point.x - this.x;
471 double dy = point.y - this.y;
472
473 return Math.sqrt(dx * dx + dy * dy);
474 }
475
476
477
478
479
480 public final Length horizontalDistance(final OTSPoint3D point)
481 {
482 return new Length(horizontalDistanceSI(point), LengthUnit.SI);
483 }
484
485
486
487
488
489 public final Length distance(final OTSPoint3D point)
490 {
491 return new Length(distanceSI(point), LengthUnit.SI);
492 }
493
494
495
496
497 public final Coordinate getCoordinate()
498 {
499 return new Coordinate(this.x, this.y, this.z);
500 }
501
502
503
504
505 public final DirectedPoint getDirectedPoint()
506 {
507 return new DirectedPoint(this.x, this.y, this.z);
508 }
509
510
511
512
513 public final Point2D getPoint2D()
514 {
515 return new Point2D.Double(this.x, this.y);
516 }
517
518
519 @Override
520 public final DirectedPoint getLocation()
521 {
522 return getDirectedPoint();
523 }
524
525
526
527
528 @Override
529 public final Bounds getBounds()
530 {
531 return new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 0.5);
532 }
533
534
535 @Override
536 @SuppressWarnings("checkstyle:designforextension")
537 public String toString()
538 {
539 return String.format("(%.3f,%.3f,%.3f)", this.x, this.y, this.z);
540 }
541
542
543 @Override
544 @SuppressWarnings("checkstyle:designforextension")
545 public int hashCode()
546 {
547 final int prime = 31;
548 int result = 1;
549 long temp;
550 temp = Double.doubleToLongBits(this.x);
551 result = prime * result + (int) (temp ^ (temp >>> 32));
552 temp = Double.doubleToLongBits(this.y);
553 result = prime * result + (int) (temp ^ (temp >>> 32));
554 temp = Double.doubleToLongBits(this.z);
555 result = prime * result + (int) (temp ^ (temp >>> 32));
556 return result;
557 }
558
559
560 @Override
561 @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces" })
562 public boolean equals(final Object obj)
563 {
564 if (this == obj)
565 return true;
566 if (obj == null)
567 return false;
568 if (getClass() != obj.getClass())
569 return false;
570 OTSPoint3D other = (OTSPoint3D) obj;
571 if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x))
572 return false;
573 if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y))
574 return false;
575 if (Double.doubleToLongBits(this.z) != Double.doubleToLongBits(other.z))
576 return false;
577 return true;
578 }
579
580 }