This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh_to_stl.py
188 lines (156 loc) · 5.21 KB
/
mesh_to_stl.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
import numpy as np
from stl import mesh
from matplotlib import pyplot as plt
import matplotlib.animation
from dataclasses import dataclass
GRAVITY = np.array([0, 0, -0.9])
FRICTION = 0.8
NUM_RESOLVE_STEPS = 8
N_COLS = 15
N_ROWS = 15
SIZE = 100
@dataclass
class Point:
pos: np.ndarray
old_pos: np.ndarray
pinned: bool
@dataclass
class Stick:
p_a: Point
p_b: Point
len: float
points = [
Point(pos=np.array([x, y, 0]), old_pos=np.array([x, y, 0]), pinned=False)
for x in np.linspace(0, SIZE, N_COLS) for y in np.linspace(0, SIZE, N_ROWS) ]
# pin some points
for i, p in enumerate(points):
if p.pos[0] == 0 or p.pos[0] == SIZE or p.pos[1] == 0 or p.pos[1] == SIZE:
p.pinned = True
# make the sticks between adjacent points
def distance(p1: Point, p2: Point):
return np.linalg.norm(p1.pos - p2.pos)
def make_stick_from_indicies(c1, r1, c2, r2):
p1 = points[c1*N_ROWS + r1]
p2 = points[c2*N_ROWS + r2]
return Stick(p_a=p1, p_b=p2, len=distance(p1, p2))
sticks = [ make_stick_from_indicies(c, r, c+1, r) for c in range(N_COLS-1) for r in range(N_ROWS) ] \
+ [ make_stick_from_indicies(c, r, c, r+1) for c in range(N_COLS) for r in range(N_ROWS-1) ]
def motion_kinematics(points):
for i, p in enumerate(points):
if p.pinned: continue
vel = p.pos - p.old_pos
p.old_pos = p.pos.copy()
p.pos += vel * FRICTION
p.pos += GRAVITY
def motion_rigidsticks(points):
for stick in sticks:
if stick.p_a.pinned and stick.p_b.pinned: continue
# just straight up moves points towards/away from each other
# as per https://www.youtube.com/watch?v=pBMivz4rIJY
point_offset = stick.p_b.pos - stick.p_a.pos
point_distance = np.linalg.norm(point_offset)
offset = point_offset * (stick.len - point_distance)/point_distance
if stick.p_a.pinned:
stick.p_b.pos += offset
elif stick.p_b.pinned:
stick.p_a.pos -= offset
else:
stick.p_a.pos -= offset/2
stick.p_b.pos += offset/2
fig = plt.figure()
ax = fig.add_subplot(121, projection='3d')
sc = ax.scatter([], [], [])
line_ax = fig.add_subplot(122)
max_vel_data = []
avg_vel_data = []
max_vel_line, = line_ax.plot([], [], label="maximum velocity")
avg_vel_line, = line_ax.plot([], [], label="average velocity")
line_ax.set_xlabel('iterations')
line_ax.legend()
def animate(i):
motion_kinematics(points)
# resolution handlers
for _ in range(NUM_RESOLVE_STEPS):
motion_rigidsticks(points)
# update scatter plot
plot_x = [p.pos[0] for p in points]
plot_y = [p.pos[1] for p in points]
plot_z = [p.pos[2] for p in points]
# sc.set_offsets(np.c_[plot_x, plot_y, plot_z]) # for 2d scatter
sc._offsets3d = (plot_x, plot_y, plot_z) # https://stackoverflow.com/a/41609238
# update tracking line chart
vels = [np.linalg.norm(p.pos - p.old_pos) for p in points]
max_vel_data.append(max(vels))
avg_vel_data.append(sum(vels)/len(vels))
max_vel_line.set_ydata(max_vel_data)
max_vel_line.set_xdata(range(len(max_vel_data)))
avg_vel_line.set_ydata(avg_vel_data)
avg_vel_line.set_xdata(range(len(avg_vel_data)))
line_ax.set_xlim(0, len(avg_vel_data))
y_scale = max(max_vel_data)
line_ax.set_ylim(-0.2*y_scale, 1.2*y_scale)
if __name__ == '__main__':
ax.set_xlim3d([-SIZE*0.2, SIZE*1.2])
ax.set_ylim3d([-SIZE*0.2, SIZE*1.2])
ax.set_zlim3d([-30, 5])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ani = matplotlib.animation.FuncAnimation(fig, animate,
frames=60, interval=100, blit=False)
plt.show()
print("shape confirmed. exporting to stl...")
vertices = np.array([[p.pos[0], p.pos[2], p.pos[1]] for p in points])
# so how are we gonna trianglify the grid?
# well, each point just has to worry about the two triangles it's the right angle of
# 2
# |\
# | \
# | \
# | \
# 1----0----1
# \ |
# \ |
# \ |
# \|
# 2
# so we can loop through each point twice, and have it do it's upper right / lower left triangle if it's far enough away from the edge
faces = np.array(
[ [c*N_ROWS+r, (c+1)*N_ROWS+r, c*N_ROWS+r+1] # top right
for c in range(N_COLS-1) for r in range(N_ROWS-1) ] +
[ [c*N_ROWS+r, (c-1)*N_ROWS+r, c*N_ROWS+r-1] # bottom left
for c in range(1, N_COLS) for r in range(1, N_ROWS) ]
)
print(faces)
# # Define the 8 vertices of the cube
# vertices = np.array([\
# [-1, -1, -1],
# [+1, -1, -1],
# [+1, +1, -1],
# [-1, +1, -1],
# [-1, -1, +1],
# [+1, -1, +1],
# [+1, +1, +1],
# [-1, +1, +1]])
# # Define the 12 triangles composing the cube
# faces = np.array([\
# [0,3,1],
# [1,3,2],
# [0,4,7],
# [0,7,3],
# [4,5,6],
# [4,6,7],
# [5,1,2],
# [5,2,6],
# [2,3,6],
# [3,7,6],
# [0,1,5],
# [0,5,4]])
#
# Create the mesh
cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
cube.vectors[i][j] = vertices[f[j],:]
# Write the mesh to file "cube.stl"
cube.save('lakebed.stl')