-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_rhombohedron.py
156 lines (132 loc) · 3.87 KB
/
py_rhombohedron.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
"""Demonstrates custom objects with native collision geometry"""
import numpy as np
import svg
from pyraydeon import (
Camera,
Geometry,
LineSegment3D,
Scene,
Quad,
)
class Rhombohedron(Geometry):
def __init__(self, origin, basis, dims):
basis = basis / np.linalg.norm(basis, axis=1, keepdims=True)
self.origin = origin
self.basis = basis
self.dims = dims
self.faces = [
[0, 1, 3, 2],
[4, 5, 7, 6],
[0, 1, 5, 4],
[2, 3, 7, 6],
[0, 2, 6, 4],
[1, 3, 7, 5],
]
combinations = np.array(np.meshgrid([0, 1], [0, 1], [0, 1])).T.reshape(-1, 3)
scaled_combinations = combinations * dims
# Transform the scaled combinations using the basis
transformed_vertices = np.dot(scaled_combinations, basis)
# Shift by the origin
self.vertices = transformed_vertices + origin
# Make edges stand out slightly so as to not be intersected by their own faces
centroid = np.mean(self.vertices, axis=0)
vert_move_dirs = self.vertices - centroid
unit_move_dirs = vert_move_dirs / np.linalg.norm(
vert_move_dirs, axis=0, keepdims=True
)
move_vectors = unit_move_dirs * 0.0015
self.path_vertices = self.vertices + move_vectors
self.quads = self.compute_quads()
def __repr__(self):
return f"Rhomboid(origin='{self.origin}', basis='{self.basis}', dims='{self.dims}')"
def compute_quads(self):
quads = []
for face in self.faces:
verts = self.vertices[face]
origin = verts[0]
basis = np.array(
[
verts[1] - origin,
verts[3] - origin,
]
)
dims = np.linalg.norm(basis, axis=1)
quads.append(Quad(origin, basis, dims))
return quads
def collision_geometry(self):
return [geom for quad in self.quads for geom in quad.collision_geometry()]
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
scene = Scene(
[
Rhombohedron(
origin=np.array([0.0, 0.0, 0.2]),
basis=np.array(
[
[0.45, 0.2, -0.3],
[-0.3, 1.0, 0.0],
[-0.5, 0.0, -0.2],
]
),
dims=np.array([2.0, 0.5, 1.0]),
),
Rhombohedron(
origin=np.array([1.0, 0.0, -2.0]),
basis=np.array(
[
[-0.9, 0.5, 0.0],
[0.3, 1.0, 0.5],
[1.5, 0.25, -0.7],
]
),
dims=np.array([1.0, 1.0, 0.8]),
),
]
)
eye = np.array([0, -0.5, 5])
focus = np.array([0, 0.4, 0])
up = np.array([0, 1, 0])
fovy = 60.0
width = 1024
height = 1024
znear = 0.1
zfar = 20.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)