/
predicates_2.py
584 lines (475 loc) · 20.3 KB
/
predicates_2.py
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from compas.tolerance import TOL
from compas.geometry import distance_point_point_xy
from compas.geometry import distance_point_line_xy
from compas.geometry import closest_point_on_segment_xy
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# Fundamental
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
def is_ccw_xy(a, b, c, colinear=False):
"""Determine if c is on the left of ab when looking from a to b,
and assuming that all points lie in the XY plane.
Parameters
----------
a : [float, float, float] | :class:`compas.geometry.Point`
Base point defined by XY(Z) coordinates.
b : [float, float, float] | :class:`compas.geometry.Point`
First end point defined by XY(Z) coordinates.
c : [float, float, float] | :class:`compas.geometry.Point`
Second end point defined by XY(Z) coordinates.
colinear : bool, optional
If True, colinear points will return a positive result.
Returns
-------
bool
True if ccw.
False otherwise.
See Also
--------
is_colinear_xy
References
----------
For more info, see [1]_.
.. [1] Marsh, C. *Computational Geometry in Python: From Theory to Application*.
Available at: https://www.toptal.com/python/computational-geometry-in-python-from-theory-to-implementation
Examples
--------
>>> print(is_ccw_xy([0,0,0], [0,1,0], [-1, 0, 0]))
True
>>> print(is_ccw_xy([0,0,0], [0,1,0], [+1, 0, 0]))
False
>>> print(is_ccw_xy([0,0,0], [1,0,0], [2,0,0]))
False
>>> print(is_ccw_xy([0,0,0], [1,0,0], [2,0,0], True))
True
"""
ab_x = b[0] - a[0]
ab_y = b[1] - a[1]
ac_x = c[0] - a[0]
ac_y = c[1] - a[1]
if colinear:
return ab_x * ac_y - ab_y * ac_x >= 0
return ab_x * ac_y - ab_y * ac_x > 0
def is_colinear_xy(a, b, c):
"""Determine if three points are colinear on the XY-plane.
Parameters
----------
a : [float, float, float] | :class:`compas.geometry.Point`
Point 1 defined by XY(Z) coordinates.
b : [float, float, float] | :class:`compas.geometry.Point`
Point 2 defined by XY(Z) coordinates.
c : [float, float, float] | :class:`compas.geometry.Point`
Point 3 defined by XY(Z) coordinates.
Returns
-------
bool
True if the points are colinear.
False otherwise.
See Also
--------
is_ccw_xy
"""
ab_x = b[0] - a[0]
ab_y = b[1] - a[1]
ac_x = c[0] - a[0]
ac_y = c[1] - a[1]
return ab_x * ac_y == ab_y * ac_x
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# Parallel, Perpendicular
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# Convexity
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
def is_polygon_convex_xy(polygon, colinear=False):
"""Determine if the polygon is convex on the XY-plane.
Parameters
----------
polygon : sequence[point] | :class:`compas.geometry.Polygon`
The XY(Z) coordinates of the corners of a polygon.
The vertices are assumed to be in order.
The polygon is assumed to be closed: the first and last vertex in the sequence should not be the same.
colinear : bool, optional
Are points allowed to be colinear?
Returns
-------
bool
True if the polygon is convex.
False otherwise.
"""
a = polygon[-2]
b = polygon[-1]
c = polygon[0]
direction = is_ccw_xy(a, b, c, colinear)
for i in range(-1, len(polygon) - 2):
a = b
b = c
c = polygon[i + 2]
if direction != is_ccw_xy(a, b, c, colinear):
return False
return True
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# Containment (Curves)
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
def is_point_on_line_xy(point, line, tol=None):
"""Determine if a point lies on a line on the XY-plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of a point.
line : [point, point] | :class:`compas.geometry.Line`
XY(Z) coordinates of two points defining a line.
tol : float, optional
The tolerance for comparing the distance between point and line to zero.
Default is :attr:`TOL.absolute`.
Returns
-------
bool
True if the point is in on the line.
False otherwise.
See Also
--------
is_point_on_segment_xy
is_point_on_polyline_xy
"""
return TOL.is_zero(distance_point_line_xy(point, line), tol)
def is_point_on_segment_xy(point, segment, tol=None):
"""Determine if a point lies on a given line segment on the XY-plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of a point.
segment : [point, point] | :class:`compas.geometry.Line`
XY(Z) coordinates of two points defining a segment.
tol : float, optional
The tolerance for comparing the distance between point and segment to zero.
Default is :attr:`TOL.absolute`.
Returns
-------
bool
True if the point is on the line segment.
False otherwise.
See Also
--------
is_point_on_line_xy
is_point_on_polyline_xy
"""
a, b = segment
if not is_point_on_line_xy(point, segment, tol=tol):
return False
d_ab = distance_point_point_xy(a, b)
if d_ab == 0:
return False
d_pa = distance_point_point_xy(a, point)
d_pb = distance_point_point_xy(b, point)
if TOL.is_close(d_pa + d_pb, d_ab, atol=tol):
return True
return False
def is_point_on_polyline_xy(point, polyline, tol=None):
"""Determine if a point is on a polyline on the XY-plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates.
polyline : sequence[point] | :class:`compas.geometry.Polyline`
XY(Z) coordinates of the points of the polyline.
tol : float, optional
The tolerance for comparing the distance between point and polyline to zero.
Default is :attr:`TOL.absolute`.
Returns
-------
bool
True if the point is on the polyline.
False otherwise.
See Also
--------
is_point_on_line_xy
is_point_on_segment_xy
"""
for i in range(len(polyline) - 1):
a = polyline[i]
b = polyline[i + 1]
c = closest_point_on_segment_xy(point, (a, b))
if TOL.is_zero(distance_point_point_xy(point, c), tol):
return True
return False
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# Containment (Shapes)
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
def is_point_in_triangle_xy(point, triangle, colinear=False):
"""Determine if a point is in the interior of a triangle lying on the XY-plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of a point.
triangle : [point, point, point]
XY(Z) coordinates of the corners of the triangle.
colinear : bool, optional
Allow points to be colinear.
Returns
-------
bool
True if the point is in the convex polygon.
False otherwise.
See Also
--------
is_point_in_convex_polygon_xy
is_point_in_polygon_xy
is_point_in_circle_xy
"""
a, b, c = triangle
ccw = is_ccw_xy(c, a, point, colinear)
if ccw != is_ccw_xy(a, b, point, colinear):
return False
if ccw != is_ccw_xy(b, c, point, colinear):
return False
return True
def is_point_in_convex_polygon_xy(point, polygon):
"""Determine if a point is in the interior of a convex polygon lying on the XY-plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of a point (Z will be ignored).
polygon : sequence[point] | :class:`compas.geometry.Polygon`
A sequence of XY(Z) coordinates of points representing the locations of the corners of a polygon (Z will be ignored).
The vertices are assumed to be in order. The polygon is assumed to be closed:
the first and last vertex in the sequence should not be the same.
Returns
-------
bool
True if the point is in the convex polygon
False otherwise.
Warnings
--------
Does not work for concave polygons.
See Also
--------
is_point_in_triangle_xy
is_point_in_polygon_xy
is_point_in_circle_xy
"""
ccw = None
for i in range(-1, len(polygon) - 1):
a = polygon[i]
b = polygon[i + 1]
if ccw is None:
ccw = is_ccw_xy(a, b, point, True)
else:
if ccw != is_ccw_xy(a, b, point, True):
return False
return True
def is_point_in_polygon_xy(point, polygon):
"""Determine if a point is in the interior of a polygon lying on the XY-plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of a point (Z will be ignored).
polygon : sequence[point] | :class:`compas.geometry.Polygon`
A sequence of XY(Z) coordinates of points representing the locations of the corners of a polygon (Z will be ignored).
The vertices are assumed to be in order.
The polygon is assumed to be closed.
The first and last vertex in the sequence should not be the same.
Returns
-------
bool
True if the point is in the polygon.
False otherwise.
Warnings
--------
A boundary check is not yet implemented. This should include a tolerance value.
See Also
--------
is_point_in_triangle_xy
is_point_in_convex_polygon_xy
is_point_in_circle_xy
"""
x, y = point[0], point[1]
polygon = [(p[0], p[1]) for p in polygon] # make 2D
inside = False
for i in range(-1, len(polygon) - 1):
x1, y1 = polygon[i]
x2, y2 = polygon[i + 1]
if y1 != y2:
xinters = (y - y1) * (x2 - x1) / (y2 - y1) + x1
else:
xinters = None
if y > min(y1, y2):
if y <= max(y1, y2):
if x <= max(x1, x2):
if x1 == x2 or (xinters is not None and x <= xinters):
inside = not inside
return inside
def is_point_in_circle_xy(point, circle):
"""Determine if a point lies in a circle lying on the XY-plane.
Parameters
----------
point : [float, float, float] | :class:`compas.geometry.Point`
XY(Z) coordinates of a point (Z will be ignored).
circle : [[point, vector], float] | :class:`compas.geometry.Circle`
Center and radius of the circle on the XY plane.
Returns
-------
bool
True if the point lies in the circle.
False otherwise.
See Also
--------
is_point_in_triangle_xy
is_point_in_convex_polygon_xy
is_point_in_polygon_xy
"""
dis = distance_point_point_xy(point, circle[0][0])
if dis <= circle[1]:
return True
return False
def is_polygon_in_polygon_xy(polygon1, polygon2):
"""Determine if a polygon is in the interior of another polygon on the XY-plane.
Parameters
----------
polygon1 : sequence[point] | :class:`compas.geometry.Polygon`
List of XY(Z) coordinates of points representing the locations of the corners of the exterior polygon (Z will be ignored).
The vertices are assumed to be in order. The polygon is assumed to be closed:
the first and last vertex in the sequence should not be the same.
polygon2 : sequence[point] | :class:`compas.geometry.Polygon`
List of XY(Z) coordinates of points representing the locations of the corners of the interior polygon (Z will be ignored).
The vertices are assumed to be in order. The polygon is assumed to be closed:
the first and last vertex in the sequence should not be the same.
Returns
-------
bool
True if polygon2 is inside polygon1.
False otherwise.
"""
if is_polygon_convex_xy(polygon1) and is_polygon_convex_xy(polygon2):
for pt in polygon2:
if not is_point_in_convex_polygon_xy(pt, polygon1):
return False
return True
else:
for i in range(len(polygon1)):
line = [polygon1[-i], polygon1[-i - 1]]
for j in range(len(polygon2)):
line_ = [polygon2[-j], polygon2[-j - 1]]
if is_intersection_segment_segment_xy(line, line_):
return False
for pt in polygon2:
if is_point_in_polygon_xy(pt, polygon1):
return True
return False
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# Deprecated
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
# =============================================================================
def is_intersection_line_line_xy(l1, l2, tol=None):
"""Verifies if two lines intersect on the XY-plane.
Parameters
----------
l1 : [point, point] | :class:`compas.geometry.Line`
XY(Z) coordinates of two points defining a line.
l2 : [point, point] | :class:`compas.geometry.Line`
XY(Z) coordinates of two points defining a line.
tol : float, optional
A tolerance for intersection verification.
Returns
-------
bool
True if the lines intersect in one point
False if the lines are skew, parallel or lie on top of each other.
"""
raise NotImplementedError
def is_intersection_segment_segment_xy(ab, cd):
"""Determines if two segments, ab and cd, intersect.
Parameters
----------
ab : [point, point] | :class:`compas.geometry.Line`
Two points representing the start and end points of a segment.
Z coordinates will be ignored.
cd : [point, point] | :class:`compas.geometry.Line`
Two points representing the start and end points of a segment.
Z coordinates will be ignored.
Returns
-------
bool
True if the segments intersect.
False otherwise.
Notes
-----
The segments intersect if both of the following conditions are true:
* `c` is on the left of `ab`, and `d` is on the right, or vice versa.
* `d` is on the left of `ac`, and on the right of `bc`, or vice versa.
"""
a, b = ab
c, d = cd
return is_ccw_xy(a, c, d) != is_ccw_xy(b, c, d) and is_ccw_xy(a, b, c) != is_ccw_xy(a, b, d)