-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledhelper.py
409 lines (341 loc) · 13.8 KB
/
ledhelper.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
import sys
class Vertex():
def __init__(self, x: float, y: float, z: float):
self.x = x
self.y = y
self.z = z
class Quad():
def __init__(self, vix1: float, vix2: float, vix3: float, vix4: float, name: str = None, reverse: bool = False):
self.vix1 = vix1
self.vix2 = vix2
self.vix3 = vix3
self.vix4 = vix4
self.name = name
self.reverse = reverse
class IslandPoint():
def __init__(self, position, roofindex: int, rooforder: int) -> None:
self.position = position
self.roofindex = roofindex
self.rooforder = rooforder
class Island():
def __init__(self, name: str, elevation: float, height: float):
self.name = name
self.elevation = elevation
self.height = height
self.points = []
self.roofs = [] # 1-based
def add_roof(self, indexes):
self.roofs.append(indexes)
def _generate_walls(self, obj3d) -> None:
for ix, p in enumerate(self.points[:-1]):
p1 = p.position
p2 = self.points[ix + 1].position
top1 = (p1[0], p1[1], self.height + self.elevation)
top2 = (p2[0], p2[1], self.height + self.elevation)
bottom3 = (p2[0], p2[1], self.elevation)
bottom4 = (p1[0], p1[1], self.elevation)
obj3d.add(top1, top2, bottom3, bottom4, f"Wall {ix}")
def _generate_floor(self, obj3d, z: float) -> None:
for roof in self.roofs:
ps = [(self.points[ix - 1].position[0], self.points[ix - 1].position[1], z) for ix in roof]
obj3d.add_polygon(ps)
#vecs = [(p[0], p[1], z) for p in self.points[:-1]]
#obj3d.add_polygon(vecs)
def generate(self, obj3d) -> None:
self._generate_walls(obj3d)
self._generate_floor(obj3d, self.elevation)
self._generate_floor(obj3d, self.elevation + self.height)
class Box():
def __init__(self, width: float, depth: float, height: float, name: str = None):
self.width = width
self.depth = depth
self.height = height
self.name = name
self.x = 0
self.y = 0
self.z = 0
def __repr__(self) -> str:
return f"Box({self.width}, {self.depth}, {self.height}, \"{self.name}\") {self.x},{self.y},{self.z}"
def translate(self, x: float, y: float, z: float):
self.x += x
self.y += y
self.z += z
def _generate(self, obj3d):
# bottom
obj3d.add(
(self.x, self.y, self.z),
(self.x + self.width, self.y, self.z),
(self.x + self.width, self.y + self.depth, self.z),
(self.x, self.y + self.depth, self.z),
f"Bottom [{self.name}]")
# top
obj3d.add(
(self.x, self.y, self.z + self.height),
(self.x + self.width, self.y, self.z + self.height),
(self.x + self.width, self.y + self.depth, self.z + self.height),
(self.x, self.y + self.depth, self.z + self.height),
f"top [{self.name}]")
# front
obj3d.add(
(self.x, self.y, self.z),
(self.x + self.width, self.y, self.z),
(self.x + self.width, self.y, self.z + self.height),
(self.x, self.y, self.z + self.height),
f"Front [{self.name}]")
# back
obj3d.add(
(self.x, self.y + self.depth, self.z),
(self.x + self.width, self.y + self.depth, self.z),
(self.x + self.width, self.y + self.depth, self.z + self.height),
(self.x, self.y + self.depth, self.z + self.height),
f"Back [{self.name}]")
# right
obj3d.add(
(self.x + self.width, self.y, self.z),
(self.x + self.width, self.y, self.z + self.height),
(self.x + self.width, self.y + self.depth, self.z + self.height),
(self.x + self.width, self.y + self.depth, self.z),
f"Right [{self.name}]")
# left
obj3d.add(
(self.x, self.y, self.z),
(self.x, self.y, self.z + self.height),
(self.x, self.y + self.depth, self.z + self.height),
(self.x, self.y + self.depth, self.z),
f"Left [{self.name}]")
class Object3D():
def __init__(self):
self.quads = []
self.vertices = []
self.boxes = []
self.polygons = []
def save(self, filename: str):
for box in self.boxes:
print(box)
box._generate(self)
with open(filename, "w") as fo:
fo.write(f"# {filename}\n")
fo.write("#\n")
fo.write("\n")
fo.write("g box\n")
fo.write("\n")
fo.write("# vertices\n")
fo.write("\n")
for ix, vertex in enumerate(self.vertices):
fo.write(f"v {vertex.x} {vertex.z} {vertex.y} # {ix + 1}\n")
fo.write("\n")
fo.write("# vertice normals\n")
fo.write("\n")
fo.write("\n")
fo.write("# faces\n")
fo.write("\n")
for quad in self.quads:
if quad.name is not None:
fo.write(f"\n# Quad '{quad.name}'\n")
if quad.reverse:
fo.write(f"f {quad.vix4 + 1} {quad.vix3 + 1} {quad.vix2 + 1} {quad.vix1 + 1}\n")
else:
fo.write(f"f {quad.vix1 + 1} {quad.vix2 + 1} {quad.vix3 + 1} {quad.vix4 + 1}\n")
for poly in self.polygons:
fo.write(f"\n# polygon\n")
s = "f " + "".join([f"{v + 1} " for v in poly])
fo.write(s + "\n")
fo.write("\n")
def find_vertex(self, x: float, y: float, z: float) -> int:
for ix, v in enumerate(self.vertices):
if v.x == x and v.y == y and v.z == z:
return ix
return None
def add_polygon(self, points) -> None:
poly = []
print("add_polygon")
print(points)
for point in points:
vix = self.find_vertex(*point)
if vix is None:
vix = self.add_vertex(*point)
poly.append(vix)
self.polygons.append(poly)
def add_vertex(self, x: float, y: float, z: float) -> int:
vertex = Vertex(x, y, z)
self.vertices.append(vertex)
return len(self.vertices) - 1
def add_quad(self, vix1: float, vix2: float, vix3: float, vix4: float, name: str = None, reverse: bool = False) -> int:
quad = Quad(vix1, vix2, vix3, vix4, name, reverse)
self.quads.append(quad)
return len(self.quads) - 1
def add_box(self, width: float, depth: float, height: float, name: str = None) -> Box:
b = Box(width, depth, height, name)
self.boxes.append(b)
return b
def add(self, v1, v2, v3, v4, name: str = None, reverse: bool = False) -> int:
vix1 = self.find_vertex(*v1)
if vix1 is None:
vix1 = self.add_vertex(*v1)
vix2 = self.find_vertex(*v2)
if vix2 is None:
vix2 = self.add_vertex(*v2)
vix3 = self.find_vertex(*v3)
if vix3 is None:
vix3 = self.add_vertex(*v3)
vix4 = self.find_vertex(*v4)
if vix4 is None:
vix4 = self.add_vertex(*v4)
return self.add_quad(vix1, vix2, vix3, vix4, name, reverse)
def build_object() -> Object3D:
obj = Object3D()
# base
width = 45
depth = 23
base_height = 1
height = 1
b = obj.add_box(width, depth, base_height, "base")
# island 5
width = 1
depth = 7
x = 0
y = 0
b = obj.add_box(width, depth, height, "Island 5")
b.translate(x, y, base_height)
# island 6
width = 23
depth = 7
x = 11
y = 0
b = obj.add_box(width, depth, height, "Island 6")
b.translate(x, y, base_height)
# island 7
width = 1
depth = 7
x = 44
y = 0
b = obj.add_box(width, depth, height, "Island 7")
b.translate(x, y, base_height)
# island 1
i = Island("Island 1", elevation = base_height, height = height)
i.points = [
IslandPoint((1, 8), x, y),
IslandPoint((1, 8 + 9), x, y),
IslandPoint((1, 8 + 14), x, y),
IslandPoint((1 + 4, 8 + 14), x, y),
IslandPoint((1 + 20 + 20 + 3 - 4, 8 + 14), x, y),
IslandPoint((1 + 20 + 20 + 3, 8 + 14), x, y),
IslandPoint((1 + 20 + 20 + 3, 8 + 14 - 5), x, y),
IslandPoint((1 + 20 + 20 + 3, 8), x, y),
IslandPoint((1 + 20 + 20 + 3 - 1, 8), x, y),
IslandPoint((1 + 20 + 20 + 3 - 1, 8 + 9), x, y),
IslandPoint((1 + 20 + 20 + 3 - 1 - 3, 8 + 9 + 3), x, y),
IslandPoint((1 + 20 + 20 + 3 - 1 - 3 - (16 + 16 + 3), 8 + 9 + 3), x, y),
IslandPoint((1 + 20 + 20 + 3 - 1 - 3 - (16 + 16 + 3) - 3, 8 + 9), x, y),
IslandPoint((1 + 20 + 20 + 3 - 1 - 3 - (16 + 16 + 3) - 3, 8), x, y),
IslandPoint((1 + 20 + 20 + 3 - 1 - 3 - (16 + 16 + 3) - 3 - 1, 8), x, y)
]
i.add_roof([1, 2, 13, 14])
i.add_roof([13, 12, 4, 3, 2])
i.add_roof([4, 5, 11, 12])
i.add_roof([5, 6, 7, 10, 11])
i.generate(obj)
# island 2
i = Island("Island 2", elevation = base_height, height = height)
i.points = [
IslandPoint((4, 8), x, y),
IslandPoint((4, 14), x, y),
IslandPoint((4, 17), x, y),
IslandPoint((6, 8 + 6 + 2 + 2), x, y),
IslandPoint((4 + 2 + 2, 8 + 6 + 2 + 2), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3), 8 + 6 + 2 + 2), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2, 8 + 6 + 2 + 2), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2 + 2, 8 + 6 + 2 + 2 - 2), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2 + 2, 8 + 6 + 2 + 2 - 2 - 2), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2 + 2, 8 + 6 + 2 + 2 - 2 - 2 - 6), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2 + 2 - 1, 8 + 6 + 2 + 2 - 2 - 2 - 6), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2 + 2 - 1, 8 + 6 + 2 + 2 - 2 - 2 - 6 + 6), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2 + 2 - 1 - 3, 8 + 6 + 2 + 2 - 2 - 2 - 6 + 6 + 3), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2 + 2 - 1 - 3 - (13 * 2 + 3), 8 + 6 + 2 + 2 - 2 - 2 - 6 + 6 + 3), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2 + 2 - 1 - 3 - (13 * 2 + 3) - 3, 8 + 6 + 2 + 2 - 2 - 2 - 6 + 6 + 3 - 3), x, y),
IslandPoint((4 + 2 + 2 + (2 * 13 + 3) + 2 + 2 - 1 - 3 - (13 * 2 + 3) - 3, 8 + 6 + 2 + 2 - 2 - 2 - 6 + 6 + 3 - 3 - 6), x, y),
IslandPoint((4, 8), x, y),
]
i.add_roof([1, 2, 15, 16])
i.add_roof([2, 3, 4, 5, 14, 15])
i.add_roof([5, 6, 13, 14])
i.add_roof([6, 7, 8, 9, 12, 13])
i.add_roof([9, 10, 11, 12])
i.generate(obj)
# island 3
i = Island("Island 3", elevation = base_height, height = height)
i.points = [
IslandPoint((7, 8), x, y),
IslandPoint((7, 8 + 3), x, y),
IslandPoint((7, 8 + 3 + 2), x, y),
IslandPoint((7 + 2, 8 + 3 + 2 + 2), x, y),
IslandPoint((7 + 2 + 2, 8 + 3 + 2 + 2), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3), 8 + 3 + 2 + 2), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3) + 2, 8 + 3 + 2 + 2), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3) + 2 + 2, 8 + 3 + 2 + 2 - 2), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3) + 2 + 2, 8 + 3 + 2 + 2 - 2 - 5), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3) + 2 + 2 - 1, 8 + 3 + 2 + 2 - 2 - 5), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3) + 2 + 2 - 1, 8 + 3 + 2 + 2 - 2 - 5 + 3), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3) + 2 + 2 - 1 - 3, 8 + 3 + 2 + 2 - 2 - 5 + 3 + 3), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3) + 2 + 2 - 1 - 3 - (2 * 10 + 3), 8 + 3 + 2 + 2 - 2 - 5 + 3 + 3), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3) + 2 + 2 - 1 - 3 - (2 * 10 + 3) - 3, 8 + 3 + 2 + 2 - 2 - 5 + 3 + 3 - 3), x, y),
IslandPoint((7 + 2 + 2 + (2 * 10 + 3) + 2 + 2 - 1 - 3 - (2 * 10 + 3) - 3, 8 + 3 + 2 + 2 - 2 - 5 + 3 + 3 - 3 - 3), x, y),
IslandPoint((7, 8), x, y),
]
i.add_roof([1, 2, 15, 16])
i.add_roof([2, 3, 4, 5, 14, 15])
i.add_roof([5, 6, 13, 14])
i.add_roof([6, 7, 8, 9, 12, 13])
i.add_roof([9, 10, 11, 12])
i.generate(obj)
# island 4
i = Island("Island 4", elevation = base_height, height = height)
i.points = [
IslandPoint((10, 8), x, y),
IslandPoint((10, 8 + 2), x, y),
IslandPoint((10 + 2, 8 + 2 + 2), x, y),
IslandPoint((10 + 2 + (3 + 2 * 9), 8 + 2 + 2), x, y),
IslandPoint((10 + 2 + (3 + 2 * 9) + 2, 8 + 2 + 2 - 2), x, y),
IslandPoint((10 + 2 + (3 + 2 * 9) + 2, 8 + 2 + 2 - 2 - 2), x, y),
IslandPoint((10, 8), x, y),
]
i.add_roof([1, 2, 3, 4, 5, 6])
i.generate(obj)
return obj
def build_plastic_cover(h) -> Object3D:
obj = Object3D()
width = 29.3333
depth = 29.3333
height = h
corner = 2
x = 0
y = 0
i = Island("Plastic Cover", elevation = 0, height = height)
i.points = [
# top left
IslandPoint((0, corner), x, y),
IslandPoint((corner, 0), x, y),
# top right
IslandPoint((width - corner, 0), x, y),
IslandPoint((width, corner), x, y),
# bottom right
IslandPoint((width, depth - corner), x, y),
IslandPoint((width - corner, depth), x, y),
# bottom left
IslandPoint((0 + corner, depth), x, y),
IslandPoint((0, depth - corner), x, y),
# same as first point
IslandPoint((0, corner), x, y),
]
i.add_roof([1, 2, 3, 4, 5, 6, 7, 8])
i.generate(obj)
return obj
def main():
obj = build_object()
obj.save("ledhelper.obj")
obj = build_plastic_cover(1)
obj.save("plastic_cover_1.obj")
obj = build_plastic_cover(2)
obj.save("plastic_cover_2.obj")
if __name__ == "__main__":
main()