-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharch_models.py
More file actions
170 lines (135 loc) · 5.34 KB
/
arch_models.py
File metadata and controls
170 lines (135 loc) · 5.34 KB
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
from math import sqrt
from shapely.geometry import Polygon
from shapely import Point, LineString
class Rectangle:
def __init__(self, limits_x, limits_y):
self.polygon = Polygon(
[
(limits_x[0], limits_y[0]),
(limits_x[1], limits_y[0]),
(limits_x[1], limits_y[1]),
(limits_x[0], limits_y[1]),
]
)
self.limits_x = limits_x
self.limits_y = limits_y
def intersects(self, x, y):
return self.polygon.intersects(Point(x, y))
def limited(self, x, y, dx, dy, canti_dist):
if dx == 0:
return (
abs(y - self.limits_y[0]) < canti_dist
or abs(y - self.limits_y[1]) < canti_dist
)
elif dy == 0:
return (
abs(x - self.limits_x[0]) < canti_dist
or abs(x - self.limits_x[1]) < canti_dist
)
return False
def nearest_bdist(self, x, y):
"""Get the distance to the nearest boundary."""
boundary = self.polygon.boundary
nearest_point = boundary.interpolate(boundary.project(Point(x, y)))
dist_x, dist_y = nearest_point.x - x, nearest_point.y - y
if abs(dist_x) < abs(dist_y):
return dist_x
return dist_y
class L_shape(Rectangle):
def __init__(self, rectangle1, rectangle2):
self.rectangle1 = rectangle1
self.rectangle2 = rectangle2
def intersects(self, x, y):
return self.rectangle1.intersects(x, y) or self.rectangle2.intersects(x, y)
def limited(self, x, y, dx, dy, canti_dist):
return self.rectangle1.limited(
x, y, dx, dy, canti_dist
) or self.rectangle2.limited(x, y, dx, dy, canti_dist)
def nearest_bdist(self, x, y):
d1, d2 = self.rectangle1.nearest_bdist(x, y), self.rectangle2.nearest_bdist(
x, y
)
if abs(d1) < abs(d2):
return d1
return d2
class T_shape(L_shape):
pass
class Circle:
def __init__(self, cx, cy, r):
self.cx = cx
self.cy = cy
self.r = r
def intersects(self, x, y):
return (x - self.cx) ** 2 + (y - self.cy) ** 2 <= self.r**2
def limited(self, x, y, dx, dy, canti_dist):
return (x - self.cx) ** 2 + (y - self.cy) ** 2 <= (self.r + canti_dist) ** 2
def nearest_bdist(self, x, y):
return abs(sqrt((x - self.cx) ** 2 + (y - self.cy) ** 2) - self.r)
class Torus:
def __init__(self, r1, r2):
center = r1, r1
self.circle1 = Circle(center[0], center[1], r1)
self.circle2 = Circle(center[0], center[1], r2)
def intersects(self, x, y):
return self.circle1.intersects(x, y) and not self.circle2.intersects(x, y)
def limited(self, x, y, dx, dy, canti_dist):
return self.circle1.limited(x, y, dx, dy, canti_dist) or self.circle2.limited(
x, y, dx, dy, canti_dist
)
def nearest_bdist(self, x, y):
return min(self.circle1.nearest_bdist(x, y), self.circle2.nearest_bdist(x, y))
class Triangle:
def __init__(self, p1, p2, p3):
self.polygon = Polygon([p1, p2, p3])
def intersects(self, x, y):
return self.polygon.intersects(Point(x, y))
def limited(self, x, y, dx, dy, canti_dist):
"""Check if point is near the edge of the triangle by canti_dist"""
return self.polygon.boundary.distance(Point(x, y)) < canti_dist
def nearest_bdist(self, x, y):
return self.polygon.boundary.distance(Point(x, y))
class Villa:
def __init__(self, rectangle1, rectangle2, rectangle3, rectangle4, triangle):
self.rectangle1 = rectangle1
self.rectangle2 = rectangle2
self.rectangle3 = rectangle3
self.rectangle4 = rectangle4
self.triangle = triangle
def intersects(self, x, y):
return (
self.rectangle1.intersects(x, y)
or self.rectangle2.intersects(x, y)
or self.rectangle3.intersects(x, y)
or self.rectangle4.intersects(x, y)
or self.triangle.intersects(x, y)
)
def limited(self, x, y, dx, dy, canti_dist):
return (
self.rectangle1.limited(x, y, dx, dy, canti_dist)
or self.rectangle2.limited(x, y, dx, dy, canti_dist)
or self.rectangle3.limited(x, y, dx, dy, canti_dist)
or self.rectangle4.limited(x, y, dx, dy, canti_dist)
or self.triangle.limited(x, y, dx, dy, canti_dist)
)
def nearest_bdist(self, x, y):
return min(
self.rectangle1.nearest_bdist(x, y),
self.rectangle2.nearest_bdist(x, y),
self.rectangle3.nearest_bdist(x, y),
self.rectangle4.nearest_bdist(x, y),
self.triangle.nearest_bdist(x, y),
)
class Obstacle(Rectangle):
def __init__(self, x, y):
"""x -- x of lowermost left point, y -- y of lowermost left point"""
super().__init__((x, x + 0.3), (y, y + 0.45))
def intersects_frame(self, x1, x2, y):
"""Check if the horizontal line segment from (x1, y) to (x2, y) intersects the rectangle."""
return (
self.polygon.intersects(Point(x1, y))
or self.polygon.intersects(Point(x2, y))
or x1 < self.limits_x[0]
and x2 > self.limits_x[1]
and y > self.limits_y[0]
and y < self.limits_y[1]
)