-
Notifications
You must be signed in to change notification settings - Fork 0
/
direction_fix.py
106 lines (101 loc) · 2.93 KB
/
direction_fix.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
"""
Fix the direction of polygon contour to certain directions
by inserting key points between every 2 points
"""
__all__ = ["fix_direction_octagon", "fix_direction_square"]
def fix_direction_octagon(poly):
"""fix direction to octagon like"""
# input is a polygon contour
segments = len(poly)
v3s = []
for i in range(segments):
# get v1, v2 with v1 is at left (lower x)
if i != segments-1:
if poly[i][0] < poly[i + 1][0]:
v_1 = poly[i]
v_2 = poly[i + 1]
else:
v_1 = poly[i + 1]
v_2 = poly[i]
else:
if poly[i][0] < poly[0][0]:
v_1 = poly[i]
v_2 = poly[0]
else:
v_1 = poly[0]
v_2 = poly[i]
if v_1[1] > v_2[1]:
# y1 > y2
if v_2[0] - v_1[0] > v_1[1] - v_2[1]:
# case 1
# \___
v_3 = [v_1[0] + v_1[1] - v_2[1], v_2[1]]
else:
# case 2
# \
# |
v_3 = [v_2[0], v_1[0] - v_2[0] + v_1[1]]
else:
# y1 <= y2
if v_2[0] - v_1[0] > v_2[1] - v_1[1]:
# case 3
# ___
# /
v_3 = [v_1[0] + v_2[1] - v_1[1], v_2[1]]
else:
# case 4
# |
# /
v_3 = [v_2[0], v_1[1] + v_2[0] - v_1[0]]
v3s.append(v_3)
for i in range(segments):
poly.insert(2 * i + 1, v3s[i])
return poly
def fix_direction_square(poly):
"""fix direction to square like"""
# input is a polygon contour
segments = len(poly)
v_3_s = []
for i in range(segments):
# get v1, v2 with v1 is at left (lower x)
if i != segments-1:
if poly[i][0] < poly[i + 1][0]:
v_1 = poly[i]
v_2 = poly[i + 1]
else:
v_1 = poly[i + 1]
v_2 = poly[i]
else:
if poly[i][0] < poly[0][0]:
v_1 = poly[i]
v_2 = poly[0]
else:
v_1 = poly[0]
v_2 = poly[i]
if v_1[1] > v_2[1]:
# y1 > y2
if v_2[0] - v_1[0] > v_1[1] - v_2[1]:
# case 1
# \___
v_3 = [v_1[0], v_2[1]]
else:
# case 2
# \
# |
v_3 = [v_2[0], v_1[1]]
else:
# y1 <= y2
if v_2[0] - v_1[0] > v_2[1] - v_1[1]:
# case 3
# ___
# /
v_3 = [v_1[0], v_2[1]]
else:
# case 4
# |
# /
v_3 = [v_2[0], v_1[1]]
v_3_s.append(v_3)
for i in range(segments):
poly.insert(2 * i + 1, v_3_s[i])
return poly