-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathutilities_bbox.py
231 lines (190 loc) · 4.94 KB
/
utilities_bbox.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
import math
from mathutils import Vector, Matrix
class BBox:
@classmethod
def calc_bbox(cls, coords):
xmin = math.inf
xmax = -math.inf
ymin = math.inf
ymax = -math.inf
for x, y in coords:
if xmin > x:
xmin = x
if xmax < x:
xmax = x
if ymin > y:
ymin = y
if ymax < y:
ymax = y
return cls(xmin, xmax, ymin, ymax)
@classmethod
def calc_bbox_uv(cls, group, uv_layers, are_loops=False):
xmin = math.inf
xmax = -math.inf
ymin = math.inf
ymax = -math.inf
if not are_loops:
for face in group:
for loop in face.loops:
x, y = loop[uv_layers].uv
if xmin > x:
xmin = x
if xmax < x:
xmax = x
if ymin > y:
ymin = y
if ymax < y:
ymax = y
else:
for loop in group:
x, y = loop[uv_layers].uv
if xmin > x:
xmin = x
if xmax < x:
xmax = x
if ymin > y:
ymin = y
if ymax < y:
ymax = y
return cls(xmin, xmax, ymin, ymax)
@classmethod
def init_from_minmax(cls, min, max):
bbox = cls(min[0], max[0], min[1], max[1])
bbox.sanitize()
return bbox
def __init__(self, xmin=math.inf, xmax=-math.inf, ymin=math.inf, ymax=-math.inf):
self.xmin = xmin
self.xmax = xmax
self.ymin = ymin
self.ymax = ymax
def __str__(self):
return f"xmin={self.xmin:.6}, xmax={self.xmax:.6}, ymin={self.ymin:.6}, ymax={self.ymax:.6}, width={self.width:.6}, height={self.height:.6}"
@property
def is_valid(self) -> bool:
return (self.xmin < self.xmax) and (self.ymin < self.ymax)
@property
def max(self):
return Vector((self.xmax, self.ymax))
@property
def min(self):
return Vector((self.xmin, self.ymin))
@property
def left_upper(self):
return Vector((self.xmin, self.ymax))
@property
def left_bottom(self):
return Vector((self.xmin, self.ymin))
@property
def right_bottom(self):
return Vector((self.xmax, self.ymin))
@property
def right_upper(self):
return Vector((self.xmax, self.ymax))
@property
def upper(self):
return Vector(((self.xmin + self.xmax) * 0.5, self.ymax))
@property
def bottom(self):
return Vector(((self.xmin + self.xmax) * 0.5, self.ymin))
@property
def left(self):
return Vector((self.xmin, (self.ymin + self.ymax) * 0.5))
@property
def right(self):
return Vector((self.xmax, (self.ymin + self.ymax) * 0.5))
@property
def center(self):
return Vector(((self.xmin + self.xmax) * 0.5, (self.ymin + self.ymax) * 0.5))
@property
def width(self) -> float:
return self.xmax - self.xmin
@property
def height(self) -> float:
return self.ymax - self.ymin
@property
def max_lenght(self):
return max(self.width, self.height)
@property
def min_lenght(self):
return min(self.width, self.height)
@property
def half_width(self) -> float:
return (self.xmax - self.xmin) * 0.5
@property
def half_height(self) -> float:
return (self.ymax - self.ymin) * 0.5
@property
def area(self):
return self.width * self.height
@property
def is_empty(self) -> bool:
return (self.xmax <= self.xmin) or (self.ymax <= self.ymin)
def union(self, other):
if self.xmin > other.xmin:
self.xmin = other.xmin
if self.xmax < other.xmax:
self.xmax = other.xmax
if self.ymin > other.ymin:
self.ymin = other.ymin
if self.ymax < other.ymax:
self.ymax = other.ymax
return self
def sanitize(self):
if self.xmin > self.xmax:
self.xmin, self.xmax = self.xmax, self.xmin
if self.ymin > self.ymax:
self.ymin, self.ymax = self.ymax, self.ymin
# assert self.is_valid
return self
def do_minmax_v(self, xy):
if xy[0] < self.xmin:
self.xmin = xy[0]
if xy[0] > self.xmax:
self.xmax = xy[0]
if xy[1] < self.ymin:
self.ymin = xy[1]
if xy[1] > self.ymax:
self.ymax = xy[1]
def clamp(self, xmin=0, ymin=0, xmax=1, ymax=1):
if self.xmin < xmin:
self.xmin = xmin
if self.ymin < ymin:
self.ymin = ymin
if self.xmax > xmax:
self.xmax = xmax
if self.ymax > ymax:
self.ymax = ymax
def translate(self, delta):
self.xmin, self.ymin = self.min + delta
self.xmax, self.ymax = self.max + delta
return self
def rotate_expand(self, angle):
center = self.center
rot_matrix = Matrix.Rotation(-angle, 2)
corner = self.right_upper - center
corner_rot = corner @ rot_matrix
corner_max = Vector((abs(corner_rot[0]), abs(corner_rot[1])))
corner.y *= -1
corner_rot = corner @ rot_matrix
corner_max[0] = max(corner_max[0], abs(corner_rot[0]))
corner_max[1] = max(corner_max[1], abs(corner_rot[1]))
self.xmin = center[0] - corner_max[0]
self.xmax = center[0] + corner_max[0]
self.ymin = center[1] - corner_max[1]
self.ymax = center[1] + corner_max[1]
return self
def scale(self, scale):
center = self.center
self.xmin, self.ymin = (self.min - center) * scale + center
self.xmax, self.ymax = (self.max - center) * scale + center
return self.sanitize()
def update(self, coords):
for x, y in coords:
if x < self.xmin:
self.xmin = x
if x > self.xmax:
self.xmax = x
if y < self.ymin:
self.ymin = y
if y > self.ymax:
self.ymax = y