-
Notifications
You must be signed in to change notification settings - Fork 0
/
labyrinth_level.py
231 lines (190 loc) · 7.89 KB
/
labyrinth_level.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
from typing import Literal
import numpy as np
from solid import *
from solid.utils import *
class ConnectionDirection:
x_positive = 0
y_positive = 1
z_negative = 2
class LabyrinthLevel:
def __init__(
self, wallThickness, pathThickness, isConnected, isRoom, hasWindows=False
):
self.wallThickness = wallThickness
self.pathThickness = pathThickness
self.isConnected = isConnected
self.isRoom = isRoom
self.hasWindows = hasWindows
self.eps = 0.00001
@property
def gridSize(self) -> int:
return self.isRoom.shape[0]
@property
def roomSize(self) -> float:
return self.wallThickness + self.pathThickness
@property
def levelSizeXY(self) -> float:
# length of all rooms in a row and outer wall thickness
return self.gridSize * self.roomSize + self.wallThickness
@property
def floorThickness(self) -> float:
return self.wallThickness
@property
def levelSizeZ(self) -> float:
return self.floorThickness + self.pathThickness
@property
def connectionSizeX(self) -> np.ndarray:
return np.array([self.wallThickness + 2 * self.eps, self.pathThickness])
@property
def connectionSizeY(self) -> np.ndarray:
return np.array([self.pathThickness, self.wallThickness + 2 * self.eps])
def _getRoomCorner(self, i: int, j: int) -> np.ndarray:
return np.array([i * self.roomSize, j * self.roomSize])
def getRoomCorner(self, i: int, j: int) -> Optional[np.ndarray]:
if i < self.gridSize and j < self.gridSize:
return self._getRoomCorner(i, j)
else:
return None
def get3DRoomCorner(self, i: int, j: int) -> Optional[np.ndarray]:
if i < self.gridSize and j < self.gridSize:
flatRoomCorner = np.array([*self._getRoomCorner(i, j), 0])
spatialOffset = [
self.wallThickness,
self.wallThickness,
self.floorThickness,
]
return flatRoomCorner + spatialOffset
else:
return None
def get3dRoomCenter(self, i: int, j: int) -> Optional[np.ndarray]:
roomCorner = self.get3DRoomCorner(i, j)
if roomCorner is not None:
centerOffset = np.ones(3) * (self.pathThickness / 2)
return roomCorner + centerOffset
else:
return None
def createLevelBase(self) -> OpenSCADObject:
base = linear_extrude(self.levelSizeZ)(square(self.levelSizeXY))
return base
def createLevelRooms(self) -> OpenSCADObject:
rooms = []
for i in range(self.gridSize):
for j in range(self.gridSize):
rooms.append(
translate([i * self.roomSize, j * self.roomSize])(
square(np.ones(2) * self.pathThickness, center=False)
if self.isRoom[i, j]
else square([0, 0])
)
)
solidRooms = translate(
[self.wallThickness, self.wallThickness, self.floorThickness]
)(linear_extrude(self.pathThickness)(rooms))
return solidRooms
def getWindowSolid(
self, i: int, j: int, dir: Literal["xp, xn, yp, yn"]
) -> OpenSCADObject:
pathSize = self.pathThickness
center = self.get3dRoomCenter(i, j)
levelMaxXY = self.levelSizeXY
if dir == "xn":
outside = np.array([0, center[1], center[2]])
distToOutside = np.linalg.norm(outside - center)
size = [distToOutside, pathSize / 3, pathSize / 3]
rotation = np.array([45, 0, 0])
elif dir == "xp":
outside = np.array([levelMaxXY, center[1], center[2]])
distToOutside = np.linalg.norm(outside - center)
size = [distToOutside, pathSize / 3, pathSize / 3]
rotation = np.array([45, 0, 0])
elif dir == "yn":
outside = np.array([center[0], 0, center[2]])
distToOutside = np.linalg.norm(outside - center)
size = [pathSize / 3, distToOutside, pathSize / 3]
rotation = np.array([0, 45, 0])
elif dir == "yp":
outside = np.array([center[0], levelMaxXY, center[2]])
distToOutside = np.linalg.norm(outside - center)
size = [pathSize / 3, distToOutside, pathSize / 3]
rotation = np.array([0, 45, 0])
wcube = translate((center + outside) / 2)(
rotate(a=rotation)(cube(size, center=True))
)
return wcube
def getAllWindows(self) -> OpenSCADObject:
windows = []
for i in range(self.gridSize):
for j in range(self.gridSize):
if i == 0:
windows.append(self.getWindowSolid(i, j, "xn"))
if j == 0:
windows.append(self.getWindowSolid(i, j, "yn"))
if i == self.gridSize - 1:
windows.append(self.getWindowSolid(i, j, "xp"))
if j == self.gridSize - 1:
windows.append(self.getWindowSolid(i, j, "yp"))
return union()(windows)
def getXYConnection(self, i, j, type: Literal["X", "Y"]) -> OpenSCADObject:
if type == "X":
neighbor_room_corner = self.getRoomCorner(i + 1, j)
offset = [self.wallThickness + self.eps, 0]
size = self.connectionSizeX
elif type == "Y":
neighbor_room_corner = self.getRoomCorner(i, j + 1)
offset = [0, self.wallThickness + self.eps]
size = self.connectionSizeY
else:
Exception(f"Unknown type {type}")
if neighbor_room_corner is not None:
between = neighbor_room_corner - offset
return translate([0, 0, self.floorThickness])(
linear_extrude(self.pathThickness)(
translate(between)(square(size=size))
)
)
return OpenSCADObject("empty", {})
def createRoomConnections(self) -> OpenSCADObject:
u = union()
cd = ConnectionDirection
for i in range(self.gridSize):
for j in range(self.gridSize):
curr_room_center = self.getRoomCorner(i, j)
if self.isConnected[i, j, cd.x_positive]:
u.add(self.getXYConnection(i, j, "X"))
if self.isConnected[i, j, cd.y_positive]:
u.add(self.getXYConnection(i, j, "Y"))
if self.isConnected[i, j, cd.z_negative]:
u.add(
linear_extrude(self.floorThickness)(
translate(curr_room_center)(
square(size=[self.pathThickness, self.pathThickness])
)
)
)
solidConnections = translate([self.wallThickness, self.wallThickness])(u)
return solidConnections
def getSolidLevel(self) -> OpenSCADObject:
base = self.createLevelBase()
rooms = self.createLevelRooms()
connections = self.createRoomConnections()
windows = OpenSCADObject("empty", {})
if self.hasWindows:
windows = self.getAllWindows()
level = difference()([base, union()([rooms, connections, windows])])
return level
def createScadFile(self, name):
scad_render_to_file(self.getSolidLevel(), f"{name}.scad")
if __name__ == "__main__":
isRoom = np.ones((4, 4), dtype=bool)
roomConnection = np.zeros((4, 4, 3), dtype=bool)
isRoom[0, 0] = False
roomConnection[1, 1, 1] = True
roomConnection[2, 1, 0] = True
roomConnection[2, 2, 2] = True
roomConnection[0, 1, 1] = True
roomConnection[0, 2, 1] = True
roomConnection[1, 0, 0] = True
roomConnection[2, 0, 0] = True
levelData = LabyrinthLevel(2, 8, roomConnection, isRoom)
level = levelData.getSolidLevel()
scad_render_to_file(level, "auto3dlab.scad")