forked from vvasilo/semnav
-
Notifications
You must be signed in to change notification settings - Fork 2
/
polygeom_lib.py
executable file
·1366 lines (1145 loc) · 49.5 KB
/
polygeom_lib.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
MIT License (modified)
Copyright (c) 2020 The Trustees of the University of Pennsylvania
Authors:
Omur Arslan <omur@seas.upenn.edu>
Vasileios Vasilopoulos <vvasilo@seas.upenn.edu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this **file** (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from __future__ import division
import numpy as np
import matplotlib.path as mpath
import shapely as sp
import tripy, time, math
from shapely.geometry import Polygon
from shapely.ops import cascaded_union
from operator import itemgetter
from itertools import groupby
def polysignarea(xy):
"""
polysignarea(xy) determines the signed area of a non-self-intersecting
polygon with vertices xy
Input:
xy : Vertex coordinated of a non-self-intersecting polygon
(Nx2 numpy.array)
Output:
area : Signed area of the polygon
Usage:
import numpy as np
from cvxpolygeom import polysignarea
xy = np.array([[0,0],[0,1],[1,0]])
area = polysignarea(xy)
"""
xy = xy.reshape(-1,2) # Convert the input data into a 2D array
numVertex = xy.shape[0] # Number of vertices
area = 0.0
for ck in range(0,numVertex):
cn = (ck + 1) % numVertex
area = area + np.cross(xy[ck],xy[cn])
area = 0.5*area
return area
def polyarea(xy):
"""
polyarea(xy) determines the area of a non-self-intersecting polygon
with vertices xy
Input:
xy : Vertex coordinated of a non-self-intersecting polygon
(Nx2 numpy.array)
Output:
area : Area of the polygon
Usage:
import numpy as np
from cvxpolygeom import polyarea
xy = np.array([[0,0],[0,1],[1,0]])
area = polyarea(xy)
"""
return abs(polysignarea(xy))
def ispolycw(xy):
"""
ispolycw(xy) determines if the vertices, xy, of a non-self-intersecting polygon
are in clockwise order. Its computation is based on the signed are of the polygon.
Input:
xy : Vertex coordinated of a non-self-intersecting polygon
(Nx2 numpy.array)
Output:
cw : a boolean variable which is True if the input polygon is in clockwise order
(Boolean [True/False])
Usage:
import numpy as np
from cvxpolygeom import ispolycw
xy = np.array([[0,0],[0,1],[1,0]])
cw = ispolycw(xy)
"""
return (polysignarea(xy) <= 0)
def inpolygon(xy, p):
"""
inpolygon(xy, p) determines if a given set of points, p, are contained in
a polygon, with vertex set xy.
Input:
xy : Vertex coordinates of a polygon
(Nx2 numpy.array)
p : Coordinates of a set of points
(Mx2 numpy.array)
Output:
I : a boolean array indicating which points are contained in the polygon
Usage:
import numpy as np
from cvxpolygeom import inpolygon
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
n = 10
v = 8
p = 2 * np.random.rand(n,2) - 1
th = np.linspace(0, 2*np.pi, v)
xy = np.array([np.cos(th), np.sin(th)]).T
I = inpolygon(xy, p)
pIn = p[I]
pOut = p[~I]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.add_patch(mpatches.Polygon(xy, closed=True, alpha=0.5))
plt.plot(pIn[:,0], pIn[:,1], 'g*', pOut[:,0], pOut[:,1], 'ro')
ax.axis('equal')
fig.show()
"""
# Convert input data into 2D arrays
xy = xy.reshape(-1,2)
p = p.reshape(-1,2)
# Create a path decribing the polygon and check if each point is contained in the polygon
polypath = mpath.Path(xy)
I = polypath.contains_points(p)
return I
def polydist(xy, p):
"""
polydist(xy, p) computes the distance between a set of points, p, and
a polygon, xy, and return the closest points on the polygon boundary.
Here, distance is defined as the minimum distance between an input
point and any point on the polygon boundary.
Input:
xy : Vertex coordinates of a polygon
(Nx2 numpy.array)
p : Coordinates of a set of points
(Mx2 numpy.array)
Output:
D : Distance between points and the polygon
C : Coordinates of the closest points on the polygon to the input points
Usage:
import numpy as np
from cvxpolygeom import polydist
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
n = 2
v = 7
p = 2 * np.random.rand(n,2) - 1
th = np.linspace(0, 2*np.pi, v)
xy = np.array([np.cos(th), np.sin(th)]).T
D, C = polydist(xy, p)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.add_patch(mpatches.Polygon(xy, closed=True, alpha=0.5))
plt.plot(p[:,0],p[:,1],'ro')
plt.plot(C[:,0],C[:,1], 'r*')
LX = np.array([p[:,0],C[:,0]])
LY = np.array([p[:,1],C[:,1]])
plt.plot(LX, LY, 'r-')
ax.axis('equal')
fig.show()
"""
# Convert input data into 2D arrays
xy = xy.reshape(-1,2)
p = p.reshape(-1,2)
# Distance to empty set is infinity
if (xy.shape[0] == 0):
D = np.zeros(p.shape[0])
D.fill(np.inf)
C = np.zeros(p.shape)
C.fill(np.inf)
return D,C
orientsign = 1 - 2 * ispolycw(xy) # orientation of the polygon
numPoint = p.shape[0] # number of points
# Relative coordinates of polygon rims
xyPre = np.roll(xy,1, axis=0)
dxy = xyPre - xy
dxyNorm = np.power(np.linalg.norm(dxy,axis=1)[:,np.newaxis],2)
dxyNorm[(dxyNorm==0)] = 1
# Compute distances and closest points on the polygon boundary
D = np.zeros(numPoint)
C = np.zeros([numPoint,2])
for k in range(numPoint):
w = np.sum((p[k] - xy)*dxy,axis=1)[:,np.newaxis]/dxyNorm
w = np.fmax(np.fmin(w,1),0)
ctemp = (1-w)*xy + w*xyPre
dtemp = np.linalg.norm(p[k] - ctemp, axis=1)
iMin = dtemp.argmin()
D[k] = dtemp[iMin]
C[k] = ctemp[iMin]
return D,C
def cvxpolyxhplane(xy, m, n):
"""
cvxpolyxhplane(xy, m, n) computes the intersection of a polygon,
with vertex coordinates xy, and a halfplane, defined by a boundary
point m and the inward normal n.
Input:
xy : Vertex coordinates of a polygon
(Nx2 numpy.array)
m, n : A boundary point and the inward normal of a halfplane
(1x2 numpy.array)
Output:
xyNew : Vertex coordinates of the intersection
Usage:
import numpy as np
from cvxpolygeom import cvxpolyxhplane
xy = np.array([[0,0],[0,1],[1,1],[1,0]])
m = np.array([0.25,0.25])
n = np.array([1,1])
xyNew = cvxpolyxhplane(xy,m,n)
"""
# Check if the input polygon is empty
if (xy.size == 0):
return xy
# Compute distance of polygon vertices to the halfspace boundary
n = n/np.linalg.norm(n) # Normalize this vector once again
dist2hplane = np.dot(xy - m, n)
xyNew = []
numVertex =xy.shape[0]
for ck in range(0,numVertex):
cn = (ck + 1) % numVertex
if ((dist2hplane[ck]*dist2hplane[cn]) < 0):
# Compute the point on the boundary and include it into the new vertex list
w = np.dot(m - xy[cn],n)/np.dot(xy[ck] - xy[cn],n)
b = w*xy[ck] + (1 - w)*xy[cn]
xyNew.append(b.tolist())
if (dist2hplane[cn] >= 0):
#Include the next vertex since it is included in the halfspace
xyNew.append(xy[cn].tolist())
xyNew = np.array(xyNew)
xyNew = xyNew.reshape(-1,2)
return xyNew
def polyxline(xy, m, n):
"""
polyxline(xy,m,n) computes the intersection of the boundary of a polygon,
with vertex coordinates xy, and a line, defined by a point m on the line
and its normal n.
Input:
xy : Vertex coordinates of a polygon
m, n : Line parameters, m is a point on the line and n is its normal
Output:
xyNew : Vertex coordinates of the intersecting points of the polygon
boundary and line
Usage:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from cvxpolygeom import polyxline
xy = np.array([[0,0],[0,1],[1,1],[1,0]])
m = np.array([0.5, 0.5])
n = np.array([np.cos(np.pi/6), np.sin(np.pi/6)])
xyNew = polyxline(xy, m, n)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.add_patch(mpatches.Polygon(xy, closed=True, facecolor='#FFFF00', edgecolor='b'))
plt.plot(xyNew[:,0], xyNew[:,1], 'ro')
ax.axis('equal')
fig.show()
"""
if (np.linalg.norm(n) == 0):
xyNew = []
else:
xy = xy.reshape(-1,2) # Convert the input polygon into 2D array
n = n/np.linalg.norm(n) # Normalize the line normal vector
dist2line = np.dot(xy-m,n) # Signed perpendicular distance to the line
numVertex = xy.shape[0] # Number of vertices
xyNew = []
for ck in range(numVertex):
if (dist2line[ck] == 0):
xyNew.append(xy[ck])
else:
cn = (ck+1) % numVertex # Next vertex index
if ((dist2line[ck]*dist2line[cn]) < 0):
a = -dist2line[cn]/(dist2line[ck] - dist2line[cn])
xyNew.append(a*xy[ck]+(1-a)*xy[cn])
xyNew = np.array(xyNew)
return xyNew
def cvxpolyerode(xy, r):
"""
Erosion (Contraction) of a convex polygon, with vertices xy, by a closed
circle of radius r
Input:
xy : Vertex Coordinates of a convex polygon
(Nx2 numpy.array)
r : Radius of the erosion (contraction) disk
Output:
xyNew : Coordinates of contracted convex polygon
Usage:
import numpy as np
from cvxpolygeom import cvxpolyerode
xy = np.array([[0,0],[0,1],[1,1],[1,0]])
r = 0.25
xyNew = cvxpolyerode(xy,r)
"""
numVertex = xy.shape[0] # Number of vertices
if (numVertex < 3):
# The input polygon is trivial
if (r > 0):
xyNew = np.array([[]])
else:
xyNew = xy
else:
# For nontrivial convex polygons:
# Determine if the input polygon is in clockwise or counter-clockwise order
orientsign = 1 - 2 * ispolycw(xy)
# Compute the contraction of input polygon
xyNew = xy
for ck in range(0,numVertex):
cp = (ck - 1) % numVertex # Previous vertex index
ek = xy[ck] - xy[cp] # Edge vector
if (np.linalg.norm(ek) > 0):
# Erode any nontrivial edge
nk = np.array([-ek[1], ek[0]])
nk = orientsign * nk/np.linalg.norm(nk) # inward pointing edge normal
mk = xy[ck] + r * nk # A point on the edge
xyNew = cvxpolyxhplane(xyNew, mk, nk)
return xyNew
def cvxpolyintersect(xy1,xy2):
"""
Compute the intersection of two convex polygons, with vertices xy1 and xy2
Input:
xy1 : Vertex Coordinates of a convex polygon
(Nx2 numpy.array)
xy2 : Vertex Coordinates of a convex polygon
(Nx2 numpy.array)
Output:
xyNew : Coordinates of polygon intersection (polygon, line, point or empty)
Usage:
import numpy as np
from shapely.geometry import Polygon
from cvxpolygeom import cvxpolyintersect
xy1 = np.array([[0,0],[0,1],[1,1],[1,0]])
xy2 = np.array([[0.5,0],[0,1],[1,1],[1,0]])
xyNew = cvxpolyintersect(xy1,xy2)
"""
numVertex1 = xy1.shape[0] # Number of vertices
numVertex2 = xy2.shape[0] # Number of vertices
polygon1 = Polygon(xy1)
polygon2 = Polygon(xy2)
res = polygon1.intersection(polygon2)
if isinstance(res,sp.geometry.polygon.Polygon) == True:
vertlist = list(res.exterior.coords)
numVertex = len(vertlist)-1
xyNew = np.zeros((numVertex,2))
for ctr in range(0,numVertex):
xyNew[ctr,0] = vertlist[ctr][0]
xyNew[ctr,1] = vertlist[ctr][1]
elif isinstance(res,sp.geometry.linestring.LineString) == True:
vertlist = list(res.coords)
xyNew = np.zeros((2,2))
xyNew[0,0] = vertlist[0][0]
xyNew[0,1] = vertlist[0][1]
xyNew[1,0] = vertlist[1][0]
xyNew[1,1] = vertlist[1][1]
elif isinstance(res,sp.geometry.point.Point) == True:
vertlist = list(res.coords)
xyNew = np.zeros((1,2))
xyNew[0,0] = vertlist[0][0]
xyNew[0,1] = vertlist[0][1]
else:
xyNew = np.array([[]])
return xyNew
def polyxray(x,b,v):
"""
Compute the intersection of the boundary of a polygon,
with vertex coordinates x and y, and a ray, defined by
a base point b on the line and a direction vector v.
Input:
x : Vertex Coordinates of a convex polygon
(Nx2 numpy.array)
b, v : Ray parameters starting at the base point b and in direction v
(1x2 numpy.array)
Output:
c : Vertex coordinates of the intersecting points of the polygon boundary and ray
Usage:
import numpy as np
from shapely.geometry import Polygon
from cvxpolygeom import polyxray, polyxline
x = np.array([[0,0],[0,1],[1,1],[1,0]])
b = np.array([0.5, 0.5])
v = np.array([np.cos(np.pi/6), np.sin(np.pi/6)])
c = polyxray(x,b,v)
"""
if (np.linalg.norm(v) == 0):
c = []
else:
x = x.reshape(-1,2) # Convert the input polygon into 2D array
v = v/np.linalg.norm(v) # Normalize the input vector
vn = np.array([-v[1],v[0]])
c = polyxline(x,b,vn)
a = np.array([(c[0][0]-b[0])*v[0]+(c[0][1]-b[1])*v[1],(c[1][0]-b[0])*v[0]+(c[1][1]-b[1])*v[1]])
if a[0]>0 and a[1]<=0: # Choose only the point in the positive direction
c = np.array([c[0][0],c[0][1]])
elif a[1]>0 and a[0]<=0:
c = np.array([c[1][0],c[1][1]])
else:
c = np.array([c[0][0],c[0][1]])
return c
def polydilate(xy,offset):
"""
Compute the dilation of a polygon by a fixed offset.
Input:
xy : Vertex Coordinates of a polygon
(Nx2 numpy.array)
offset : Offset of dilation
Output:
xyNew : Coordinates of dilated polygon
(Nx2 numpy.array)
"""
# Construct a polygon based on the input coordinate vertices
polygon_input = Polygon(xy)
# Find the output after the offset
polygon_output = polygon_input.buffer(offset, join_style=2)
polygon_output = sp.geometry.polygon.orient(polygon_output, 1.0) # orient polygon to be CCW
# Compute the coordinates
xyNew = np.array(polygon_output.exterior.coords.xy).transpose()
return xyNew
def polytriangulation(xy,workspace,touching_boundary):
"""
Compute the triangulation of the input polygon and its dual (adjacency) graph.
Input:
xy : Vertex Coordinates of input polygon - start and end vertices must be the same
(Nx2 numpy.array)
workspace : Convex boundary of the workspace - start and end vertices must be the same
(Nx2 numpy.array)
touching_boundary : Flag that is True if the polygon is touching the boundary of the workspace and False otherwise
Output:
tree : Array of dictionaries with triangles and generated adjacency graph
Each dictionary contains:
1) 'vertices': vertices of each triangle (arranged CCW - start and end vertices are NOT the same) - for each of the children, the vertices of the adjacency edge are the first two vertices
2) 'predecessor': the index of the triangle predecessor in the adjacency tree (-1 for the root)
3) 'depth': the depth of the (triangle) node in the adjacency tree (0 for the root)
4) 'index': the index of the triangle in the tree (its serial number)
5) 'adj_edge': the edge the triangle shares with its predecessor (CCW oriented with respect to the triangle)
"""
# Construct a polygon based on the input coordinate vertices
polygon_in = Polygon(np.array(xy))
# Find polygon vertices (except the last one because of tripy)
polygon_vertices = np.vstack((polygon_in.exterior.coords.xy[0][0:-1], polygon_in.exterior.coords.xy[1][0:-1])).transpose()
# Find triangulation
triangles = np.array(tripy.earclip(polygon_vertices))
# Sorting argument for triangles - Area if not touching boundary, Min distance to boundary if touching boundary
sorting = np.zeros(triangles.shape[0])
if not touching_boundary:
for i in range(triangles.shape[0]):
sorting[i] = -polyarea(triangles[i])
elif touching_boundary:
for i in range(triangles.shape[0]):
D, C = polydist(workspace[0:-1],triangles[i])
sorting[i] = np.min(D)
# Sort the triangles
inds = (sorting).argsort()
triangles = triangles[inds]
# Construct the first node of the tree that will act as the root
input_triangles = triangles
tree = [dict() for x in range(input_triangles.shape[0])]
tree[0]['vertices'] = triangles[0]
tree[0]['predecessor'] = -1
tree[0]['depth'] = 0
tree[0]['index'] = 0
tree[0]['adj_edge'] = np.array([])
tree_index = 0
# Initialize search
input_triangles = np.delete(input_triangles,0,axis=0)
stack = [tree[0]]
# Build the tree by expanding nodes until the stack is empty
# (The stack will be empty when the leaf nodes consist of only one edge)
while len(stack) is not 0:
# Pop the first element of the stack and delete it from the stack
expanded_node = stack[0]
del(stack[0])
i = 0
while i<input_triangles.shape[0]:
# Construct two edge arrays: one for the parent and one for the candidate child
# Orient the parent CCW as desired and the child CW to check for collisions
polygon1_edges = np.array([np.vstack((expanded_node['vertices'][0], expanded_node['vertices'][1])), np.vstack((expanded_node['vertices'][1], expanded_node['vertices'][2])), np.vstack((expanded_node['vertices'][2], expanded_node['vertices'][0]))])
polygon2_edges = np.array([np.vstack((input_triangles[i][0], input_triangles[i][2])), np.vstack((input_triangles[i][2], input_triangles[i][1])), np.vstack((input_triangles[i][1], input_triangles[i][0]))])
triangles_touch = False
for polygon1_edge_index in range(polygon1_edges.shape[0]):
for polygon2_edge_index in range(polygon2_edges.shape[0]):
if (np.abs(polygon1_edges[polygon1_edge_index]-polygon2_edges[polygon2_edge_index])<1e-5).all():
triangles_touch = True
adj_edge_index = polygon2_edge_index
# Check if the triangles touch, otherwise continue
if not triangles_touch:
i = i+1
continue
else:
# Add the child to the tree
tree_index = tree_index+1
tree[tree_index]['predecessor'] = expanded_node['index']
tree[tree_index]['depth'] = tree[tree[tree_index]['predecessor']]['depth']+1
tree[tree_index]['index'] = tree_index
tree[tree_index]['adj_edge'] = polygon2_edges[adj_edge_index]
# Find the 3rd point of the child triangle (that does not belong to the shared edge) and arrange the vertices so that this is the 3rd point
nrows, ncols = input_triangles[i].shape
dtype = {'names':['f{}'.format(j) for j in range(ncols)], 'formats':ncols * [input_triangles[i].dtype]}
set_diff = np.setdiff1d(input_triangles[i].view(dtype), np.array(tree[tree_index]['adj_edge'].transpose()).view(dtype))
third_vertex = set_diff.view(input_triangles[i].dtype).reshape(-1, ncols)
tree[tree_index]['vertices'] = np.array([tree[tree_index]['adj_edge'][1], tree[tree_index]['adj_edge'][0], third_vertex[0]]) # change the direction of adj_edge to make the child CCW again
# Delete the child from the input
input_triangles = np.delete(input_triangles,i,axis=0)
# Add the child to the stack to be expanded
stack.append(tree[tree_index])
# As a final step, sort the tree as a stack, in order of descending depth
tree = sorted(tree, key=itemgetter('depth'), reverse=True)
# Make sure to change the node and predecessor indices since the indices have now changed
indices_new = np.zeros(len(tree))
indices_old = np.zeros(len(tree))
for i in range(0,len(tree)):
indices_new[i] = i
indices_old[i] = tree[i]['index']
for i in range(0,len(tree)-1):
tree[i]['predecessor'] = int(indices_new[indices_old==tree[i]['predecessor']][0])
tree[i]['index'] = i
tree[len(tree)-1]['index'] = len(tree)-1
return tree
def polyconvexdecomposition(xy,workspace,touching_boundary):
"""
Compute the convex decomposition of the input polygon and its dual (adjacency) graph.
Input:
xy : Vertex Coordinates of input polygon - start and end vertices must be the same
(Nx2 numpy.array)
workspace : Convex boundary of the workspace - start and end vertices must be the same
(Nx2 numpy.array)
touching_boundary : Flag that is True if the polygon is touching the boundary of the workspace and False otherwise
Output:
tree : Array of dictionaries with polygons and generated adjacency graph
Each dictionary contains:
1) 'vertices': vertices of each polygon (arranged CCW - start and end vertices are NOT the same) - for each of the children, the vertices of the adjacency edge are the first two vertices
2) 'predecessor': the index of the polygon predecessor in the adjacency tree (-1 for the root)
3) 'depth': the depth of the (polygon) node in the adjacency tree (0 for the root)
4) 'index': the index of the polygon in the tree (its serial number)
5) 'adj_edge': the edge the polygon shares with its predecessor (CCW oriented with respect to the polygon)
"""
# Construct a polygon based on the input coordinate vertices
polygon_in = Polygon(np.array(xy))
# Find polygon vertices
polygon_vertices = np.vstack((polygon_in.exterior.coords.xy[0][0:-1], polygon_in.exterior.coords.xy[1][0:-1])).transpose()
# Find convex decomposition
polygons = np.array([np.array(xi) for xi in polycvxdecomp(polygon_vertices.tolist())])
# Sorting argument for polygons - Area if not touching boundary, Min distance to boundary if touching boundary
sorting = np.zeros(polygons.shape[0])
if not touching_boundary:
for i in range(polygons.shape[0]):
sorting[i] = -polyarea(polygons[i])
elif touching_boundary:
for i in range(polygons.shape[0]):
if (Polygon(workspace).exterior).intersection(Polygon(polygons[i])).geom_type == 'LineString':
sorting[i] = 0.0
else:
sorting[i] = 1.0
# Sort the polygons
inds = (sorting).argsort()
polygons = polygons[inds]
# Construct the first node of the tree that will act as the root
input_polygons = polygons
tree = [dict() for x in range(input_polygons.shape[0])]
tree[0]['vertices'] = polygons[0]
tree[0]['predecessor'] = -1
tree[0]['depth'] = 0
tree[0]['index'] = 0
tree[0]['adj_edge'] = np.array([])
tree_index = 0
# Initialize search
input_polygons = np.delete(input_polygons,0,axis=0)
stack = [tree[0]]
# Build the tree by expanding nodes until the stack is empty
while len(stack) is not 0:
# Pop the first element of the stack and delete it from the stack
expanded_node = stack[0]
del(stack[0])
# Find edges of expanded node - CW
polygon1_edges = []
for j in range(0,expanded_node['vertices'].shape[0]):
polygon1_edges.append(np.array([expanded_node['vertices'][(j+1)%expanded_node['vertices'].shape[0]], expanded_node['vertices'][j%expanded_node['vertices'].shape[0]]]))
polygon1_edges = np.array(polygon1_edges)
i = 0
while i<input_polygons.shape[0]:
# Find edges of candidate child - CCW
polygon2_edges = []
for j in range(0,input_polygons[i].shape[0]):
polygon2_edges.append(np.array([input_polygons[i][j%input_polygons[i].shape[0]], input_polygons[i][(j+1)%input_polygons[i].shape[0]]]))
polygon2_edges = np.array(polygon2_edges)
polygons_touch = False
for polygon1_edge_index in range(polygon1_edges.shape[0]):
for polygon2_edge_index in range(polygon2_edges.shape[0]):
if (np.abs(polygon1_edges[polygon1_edge_index]-polygon2_edges[polygon2_edge_index])<1e-5).all():
polygons_touch = True
adj_edge_index = polygon2_edge_index
# Check if the polygons touch, otherwise continue
if not polygons_touch:
i = i+1
continue
else:
# Add the child to the tree with the adjacency edge being first
tree_index = tree_index+1
tree[tree_index]['predecessor'] = expanded_node['index']
tree[tree_index]['depth'] = tree[tree[tree_index]['predecessor']]['depth']+1
tree[tree_index]['index'] = tree_index
tree[tree_index]['adj_edge'] = polygon2_edges[adj_edge_index]
tree[tree_index]['vertices'] = np.roll(input_polygons[i],-adj_edge_index,axis=0)
# As a final preprocessing step, check whether the edges before and after the adj_edge are parallel with adj_edge
# If that's the case, cut the triangle corresponding to that edge as an extra polygon
tangent_before = np.array(tree[tree_index]['vertices'][0]-tree[tree_index]['vertices'][-1])/np.linalg.norm(tree[tree_index]['vertices'][0]-tree[tree_index]['vertices'][-1])
tangent_after = np.array(tree[tree_index]['vertices'][2]-tree[tree_index]['vertices'][1])/np.linalg.norm(tree[tree_index]['vertices'][2]-tree[tree_index]['vertices'][1])
tangent_adj_edge = np.array(tree[tree_index]['adj_edge'][1]-tree[tree_index]['adj_edge'][0])/np.linalg.norm(tree[tree_index]['adj_edge'][1]-tree[tree_index]['adj_edge'][0])
normal_adj_edge = np.array([-tangent_adj_edge[1],tangent_adj_edge[0]])
if np.abs(np.dot(tangent_before,normal_adj_edge)) < 0.001:
# Add triangle
tree_before = dict()
tree_before['predecessor'] = tree_index
tree_before['depth'] = tree[tree_index]['depth']+1
tree_before['index'] = len(tree)
tree_before['adj_edge'] = np.vstack((tree[tree_index]['vertices'][0],tree[tree_index]['vertices'][-2]))
tree_before['vertices'] = np.vstack((tree[tree_index]['vertices'][0],tree[tree_index]['vertices'][-2],tree[tree_index]['vertices'][-1]))
# Delete the last vertex from the original polygon
tree[tree_index]['vertices'] = np.delete(tree[tree_index]['vertices'], -1, axis=0)
# Add the new triangle to the tree and stack
tree.append(tree_before)
stack.append(tree_before)
if np.abs(np.dot(tangent_after,normal_adj_edge)) < 0.001:
# Add triangle
tree_after = dict()
tree_after['predecessor'] = tree_index
tree_after['depth'] = tree[tree_index]['depth']+1
tree_after['index'] = len(tree)
tree_after['adj_edge'] = np.vstack((tree[tree_index]['vertices'][3],tree[tree_index]['vertices'][1]))
tree_after['vertices'] = np.vstack((tree[tree_index]['vertices'][3],tree[tree_index]['vertices'][1],tree[tree_index]['vertices'][2]))
# Delete the third vertex from the original polygon
tree[tree_index]['vertices'] = np.delete(tree[tree_index]['vertices'], 2, axis=0)
# Add the new triangle to the tree and stack
tree.append(tree_after)
stack.append(tree_after)
# Delete the child from the input
input_polygons = np.delete(input_polygons,i,axis=0)
# Add the child to the stack to be expanded
stack.append(tree[tree_index])
# As a final step, sort the tree as a stack, in order of descending depth
tree = sorted(tree, key=itemgetter('depth'), reverse=True)
# Make sure to change the node and predecessor indices since the indices have now changed
indices_new = np.zeros(len(tree))
indices_old = np.zeros(len(tree))
for i in range(0,len(tree)):
indices_new[i] = i
indices_old[i] = tree[i]['index']
for i in range(0,len(tree)-1):
tree[i]['predecessor'] = int(indices_new[indices_old==tree[i]['predecessor']][0])
tree[i]['index'] = i
tree[len(tree)-1]['index'] = len(tree)-1
return tree
def polyintersect(xy1,xy2):
"""
Checks if polygon xy1 intersects polygon xy2
Input:
xy1 : Vertex Coordinates of a polygon
(Nx2 numpy.array)
xy2 : Vertex Coordinates of a polygon
(Nx2 numpy.array)
Output:
outcome : True if xy1 intersects xy2, False otherwise
"""
# Construct polygon objects based on the input vertices
polygon1 = Polygon(xy1)
polygon2 = Polygon(xy2)
outcome = polygon1.intersects(polygon2)
return outcome
def polyunion(xy1,xy2):
"""
Computes the union of two polygons xy1 and xy2
Input:
xy1 : Vertex Coordinates of a polygon
(Nx2 numpy.array)
xy2 : Vertex Coordinates of a polygon
(Nx2 numpy.array)
Output:
xy : Coordinates of the output polygon
(Nx2 numpy.array)
"""
# Construct polygon objects based on the input vertices
polygon1 = Polygon(xy1)
polygon2 = Polygon(xy2)
polygons = [polygon1,polygon2]
# Find the union and orient appropriately
output = cascaded_union(polygons)
output = sp.geometry.polygon.orient(output, 1.0) # orient polygon to be CCW
# Find the actual vertices
xy = np.array(output.exterior.coords.xy).transpose()
return xy
def lineint(l1, l2, precision=0):
"""Compute the intersection between two lines.
Input:
l1 : first line
l2 : second line
precision : precision to check if lines are parallel (default 0)
Output:
The intersection point
"""
i = [0, 0] # point
a1 = l1[1][1] - l1[0][1]
b1 = l1[0][0] - l1[1][0]
c1 = a1 * l1[0][0] + b1 * l1[0][1]
a2 = l2[1][1] - l2[0][1]
b2 = l2[0][0] - l2[1][0]
c2 = a2 * l2[0][0] + b2 * l2[0][1]
det = a1 * b2 - a2 * b1
if not scalar_eq(det, 0, precision): # lines are not parallel
i[0] = (b2 * c1 - b1 * c2) / det
i[1] = (a1 * c2 - a2 * c1) / det
return i
def linesegmentsintersect(p1, p2, q1, q2):
"""Checks if two line segments intersect.
Input:
p1 : The start vertex of the first line segment.
p2 : The end vertex of the first line segment.
q1 : The start vertex of the second line segment.
q2 : The end vertex of the second line segment.
Output:
True if the two line segments intersect
"""
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
da = q2[0] - q1[0]
db = q2[1] - q1[1]
# segments are parallel
if (da*dy - db*dx) == 0:
return False
s = (dx * (q1[1] - p1[1]) + dy * (p1[0] - q1[0])) / (da * dy - db * dx)
t = (da * (p1[1] - q1[1]) + db * (q1[0] - p1[0])) / (db * dx - da * dy)
return s >= 0 and s <= 1 and t >= 0 and t <= 1
def trianglearea(a, b, c):
"""Calculates the area of a triangle spanned by three points.
Note that the area will be negative if the points are not given in counter-clockwise order.
Input:
a : First point
b : Second point
c : Third point
Output:
Area of triangle
"""
return ((b[0] - a[0])*(c[1] - a[1]))-((c[0] - a[0])*(b[1] - a[1]))
def isleft(a, b, c):
return trianglearea(a, b, c) > 0
def islefton(a, b, c):
return trianglearea(a, b, c) >= 0
def isright(a, b, c):
return trianglearea(a, b, c) < 0
def isrighton(a, b, c):
return trianglearea(a, b, c) <= 0
def collinear(a, b, c, thresholdAngle=0):
"""Checks if three points are collinear.
Input:
a : First point
b : Second point
c : Third point
thresholdAngle : threshold to consider if points are collinear, in radians (default 0)
Output:
True if points are collinear
"""
if thresholdAngle == 0:
return trianglearea(a, b, c) == 0
else:
ab = [None] * 2
bc = [None] * 2
ab[0] = b[0]-a[0]
ab[1] = b[1]-a[1]
bc[0] = c[0]-b[0]
bc[1] = c[1]-b[1]
dot = ab[0]*bc[0] + ab[1]*bc[1]
magA = math.sqrt(ab[0]*ab[0] + ab[1]*ab[1])
magB = math.sqrt(bc[0]*bc[0] + bc[1]*bc[1])
angle = math.acos(dot/(magA*magB))
return angle < thresholdAngle
def sqdist(a, b):
dx = b[0] - a[0]
dy = b[1] - a[1]
return dx * dx + dy * dy
def polyat(polygon, i):
"""Gets a vertex at position i on the polygon.
It does not matter if i is out of bounds.
Input:
polygon : The polygon
i : Position desired on the polygon
Output:
Vertex at position i
"""
s = len(polygon)
return polygon[i % s]
def polyclear(polygon):
"""Clears the polygon data
Input:
polygon : The polygon
"""
del polygon[:]
def polyappend(polygon, poly, start, end):
"""Grabs points at indicies `start` to `end` from `poly`
and appends them to `polygon`
Input:
polygon : The destination polygon
poly : The source polygon
start : Starting source index
end : Ending source index (not included in the slice)
"""
for i in range(start, end):
polygon.append(poly[i])
def polymakeccw(polygon):
"""Makes sure that the polygon vertices are ordered counter-clockwise.
Input:
polygon : The polygon
"""
br = 0
v = polygon
# find bottom right point
for i in range(1, len(polygon)):
if v[i][1] < v[br][1] or (v[i][1] == v[br][1] and v[i][0] > v[br][0]):
br = i
# reverse poly if clockwise
if not isleft(polyat(polygon, br - 1), polyat(polygon, br), polyat(polygon, br + 1)):
polyreverse(polygon)
def polyreverse(polygon):
"""Reverses the vertices in the polygon.
Input:
polygon : The polygon
"""
polygon.reverse()
def polyisreflex(polygon, i):
"""Checks if a point in the polygon is a reflex point.
Input:
polygon : The polygon
i : index of point to check
Output:
True is point is a reflex point
"""
return isright(polyat(polygon, i - 1), polyat(polygon, i), polyat(polygon, i + 1))
def polycansee(polygon, a, b):
"""Checks if two vertices in the polygon can see each other.
Input:
polygon : The polygon
a : Vertex 1
b : Vertex 2
Output:
True if vertices can see each other
"""
l1 = [None]*2
l2 = [None]*2
if islefton(polyat(polygon, a + 1), polyat(polygon, a), polyat(polygon, b)) and isrighton(polyat(polygon, a - 1), polyat(polygon, a), polyat(polygon, b)):
return False