-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_cubes.py
More file actions
222 lines (191 loc) · 5.47 KB
/
Copy pathpy_cubes.py
File metadata and controls
222 lines (191 loc) · 5.47 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
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
"""Demonstrates custom object and numpy support
This example is really slow, and a good case for finding more ways to express
geometry as the sum of native parts.
"""
import numpy as np
import svg
from pyraydeon import (
AABB3,
Camera,
Geometry,
HitData,
LineSegment3D,
Scene,
CollisionGeometry,
Plane,
)
class RectPrism(Geometry):
def __init__(
self,
origin: np.ndarray,
right: np.ndarray,
width: float,
up: np.ndarray,
height: float,
depth: float,
):
up = up / np.linalg.norm(up)
right = right / np.linalg.norm(right)
fwd = np.cross(up, right)
fwd = fwd / np.linalg.norm(fwd)
self.origin = origin
self.right = right
self.up = up
self.fwd = fwd
self.width = width
self.height = height
self.depth = depth
self.vertices = np.array(
[
origin,
origin + right * width,
origin + right * width + fwd * depth,
origin + fwd * depth,
origin + up * height,
origin + right * width + up * height,
origin + right * width + fwd * depth + up * height,
origin + fwd * depth + up * height,
]
)
# Make edges stand out slightly so as to not be intersected by their own faces
origin = origin - (right * 0.0015) - (up * 0.0015) - (fwd * 0.0015)
width = width + 0.003
height = height + 0.003
depth = depth + 0.003
self.path_vertices = np.array(
[
origin,
origin + right * width,
origin + right * width + fwd * depth,
origin + fwd * depth,
origin + up * height,
origin + right * width + up * height,
origin + right * width + fwd * depth + up * height,
origin + fwd * depth + up * height,
]
)
self.faces = [
[0, 1, 2, 3],
[4, 5, 6, 7],
[0, 1, 5, 4],
[1, 2, 6, 5],
[2, 3, 7, 6],
[3, 0, 4, 7],
]
def __repr__(self):
return f"RectPrism(basis='[{self.right}, {self.up}, {self.fwd}]', dims='[{self.width}, {self.height}, {self.depth}]')"
def collision_geometry(self):
return [PyQuad(self.vertices[face]) for face in self.faces]
def paths(self, cam):
edges = set(
[
tuple(sorted((face[i], face[(i + 1) % len(face)])))
for face in self.faces
for i in range(len(face))
]
)
paths = [
LineSegment3D(self.path_vertices[edge[0]], self.path_vertices[edge[1]])
for edge in edges
]
return paths
class PyQuad(CollisionGeometry):
def __init__(self, vertices):
self.vertices = vertices
self.plane = self.compute_plane(vertices)
def compute_plane(self, points):
p1, p2, p3 = points[:3]
normal = np.cross(p2 - p1, p3 - p1)
normal /= np.linalg.norm(normal)
return Plane(p1, normal)
def is_point_in_face(self, point):
edge1 = self.vertices[1] - self.vertices[0]
edge2 = self.vertices[3] - self.vertices[0]
v = point - self.vertices[0]
u1 = np.dot(v, edge1) / np.dot(edge1, edge1)
u2 = np.dot(v, edge2) / np.dot(edge2, edge2)
return 0 <= u1 <= 1 and 0 <= u2 <= 1
def hit_by(self, ray) -> HitData | None:
intersection = self.plane.hit_by(ray)
if intersection is not None and self.is_point_in_face(intersection.hit_point):
return intersection
def bounding_box(self):
my_min = np.minimum.reduce(self.vertices)
my_max = np.maximum.reduce(self.vertices)
return AABB3(my_min, my_max)
up = np.array([-1.0, 1.0, 0.0])
up = up / np.linalg.norm(up)
right = np.array([1.0, 1.0, 0.0])
right = right / np.linalg.norm(right)
r = RectPrism(
origin=np.array([0.0, 0.0, 0.0]),
right=right,
width=1.0,
up=up,
height=1.0,
depth=1.0,
)
scene = Scene(
[
RectPrism(
origin=np.array([0.0, 0.0, 0.0]),
right=right,
width=1.0,
up=up,
height=1.0,
depth=1.0,
),
RectPrism(
origin=np.array([0.0, 0.0, 1.25]),
right=right,
width=1.0,
up=up,
height=1.0,
depth=1.0,
),
RectPrism(
origin=right * 1.1,
right=right,
width=1.0,
up=up,
height=1.0,
depth=1.0,
),
]
)
eye = np.array([0.25, 3, 6])
focus = np.array([0, 0, 0])
up = np.array([0, 1, 0])
fovy = 60.0
width = 1024
height = 1024
znear = 0.1
zfar = 10.0
cam = Camera().look_at(eye, focus, up).perspective(fovy, width, height, znear, zfar)
paths = scene.render(cam)
canvas = svg.SVG(
width="8in",
height="8in",
viewBox="0 0 1024 1024",
)
backing_rect = svg.Rect(
x=0,
y=0,
width="100%",
height="100%",
fill="white",
)
svg_lines = [
svg.Line(
x1=f"{path.p1[0]}",
y1=f"{path.p1[1]}",
x2=f"{path.p2[0]}",
y2=f"{path.p2[1]}",
stroke_width="0.7mm",
stroke="black",
)
for path in paths
]
line_group = svg.G(transform=f"translate(0, {height}) scale(1, -1)", elements=svg_lines)
canvas.elements = [backing_rect, line_group]
print(canvas)