-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_utils.py
274 lines (208 loc) · 5.32 KB
/
common_utils.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
from dataclasses import dataclass
from functools import cached_property
import os
import sys
log_file = None
def log_to_file(path):
global log_file
log_file = open(path, 'wt')
sys.stdout = log_file
sys.stderr = log_file
def cache_as_property(property_name):
def decorator(fn):
def wrap(a1, *a, **kw):
if hasattr(a1, property_name):
return getattr(a1, property_name)
r = fn(a1, *a, **kw)
setattr(a1, property_name, r)
return r
return wrap
return decorator
def log(*a, **kw):
if 'file' in kw:
print(*a, **kw)
elif log_file:
print(*a, **kw, file=log_file)
log_file.flush()
else:
print(*a, **kw)
def index_where(xs, p):
return next(i for i, x in enumerate(xs) if p(x))
def yield_list(fn):
def wrap(*a, **kw):
return list(fn(*a, **kw))
wrap.__name__ = fn.__name__
return wrap
def pad_back(xs, n, pad):
npad = n - len(xs)
if npad > 0:
return xs + [pad] * npad
return xs
def pad_front(xs, n, pad):
npad = n - len(xs)
if npad > 0:
return [pad] * npad + xs
return xs
@dataclass
class Vec2:
x: float
y: float
def __getitem__(self, i):
return [self.x, self.y][i]
def __setitem__(self, i, v):
if i == 0: self.x = v
elif i == 1: self.y = v
else: raise IndexError(i)
def __add__(self, o):
return Vec2(self.x + o.x, self.y + o.y)
def __sub__(self, o):
return Vec2(self.x - o.x, self.y - o.y)
def __mul__(self, f):
return Vec2(self.x * f, self.y * f)
def __iter__(self):
return iter([self.x, self.y])
@dataclass
class Vec3:
x: float
y: float
z: float
def __getitem__(self, i):
return [self.x, self.y, self.z][i]
def __setitem__(self, i, v):
if i == 0: self.x = v
elif i == 1: self.y = v
elif i == 2: self.z = v
else: raise IndexError(i)
@dataclass
class Range:
a: float = float('inf')
b: float = float('-inf')
def __getitem__(self, i):
return [self.a, self.b][i]
def __bool__(self):
return not self.empty
def __str__(self):
return f'{self.a}:{self.b}'
@property
def min(self):
return self.a
@property
def max(self):
return self.b
@property
def size(self):
if self.empty:
return None
return self.b - self.a
def union(self, o):
return Range(
min(self.a, o.a),
max(self.b, o.b)
)
def intersection(self, o):
return Range(
max(self.a, o.a),
min(self.b, o.b)
)
def contains(self, x):
return self.a <= x and x < self.b
@property
def empty(self):
return self.b < self.a
@property
def center(self):
return self.a + (self.b - self.a) / 2
class Bounds:
axes: list[Range]
def __str__(self):
return f'{self.axes}'
def contains(self, point):
return all(
axis.contains(coord)
for axis, coord in zip(self.axes, point)
)
@property
def x(self):
return self.axes[0]
@property
def y(self):
return self.axes[1]
@property
def z(self):
return self.axes[2]
def __init__(self, axes=None):
if axes is None:
axes = [Range(), Range(), Range()]
self.axes = axes
def union(self, o):
return Bounds([
self.axes[i].union(o.axes[i])
for i in range(3)
])
def intersection(self, o):
return Bounds([
self.axes[i].intersection(o.axes[i])
for i in range(3)
])
@property
def empty(self):
return any(axis.empty for axis in self.axes)
@property
def center(self):
return Vec3(*[
a.center
for a in self.axes
])
def __bool__(self):
return not self.empty
def bounds_union(a, b): return a.union(b)
def bounds_intersection(a, b): return a.intersection(b)
@dataclass
class Rect:
origin: Vec2
size: Vec2
@classmethod
def centered(cls, c, size):
return Rect(
c - size * .5,
size
)
@property
def min(self):
return self.origin
@min.setter
def min(self, p):
self.origin = p
@property
def max(self):
return self.origin + self.size
@max.setter
def max(self, p):
self.size = p - self.origin
@classmethod
def bounding_points(cls, *points):
log('bp', points)
r = Rect(
points[0],
Vec2(0, 0)
)
for p in points[1:]:
r = r.bounds_union(Rect(p, Vec2(1, 1)))
return r
def bounds_union(self, o):
log(self, o, self.min, o.min)
x0 = min(self.min.x, o.min.x)
x1 = max(self.max.x, o.max.x)
y0 = min(self.min.y, o.min.y)
y1 = max(self.max.y, o.max.y)
return Rect(Vec2(x0, y0), Vec2(x1 - x0, y1 - y0))
def expand(self, v):
r = Rect(self.origin, self.size)
r.origin = r.origin - Vec2(v, v)
r.size = r.size + Vec2(2*v, 2*v)
return r
def __add__(self, v):
return Rect(
self.origin + v,
self.size
)