Skip to content

Commit 589391f

Browse files
committed
write obj for mass spring example
1 parent 8f6acc1 commit 589391f

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
test.py
3-
*.pyc
3+
*.pyc
4+
output/

mass_spring/simulator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def screen_projection(x):
3737
return [offset[0] + scale * x[0], resolution[1] - (offset[1] + scale * x[1])]
3838

3939
time_step = 0
40+
square_mesh.write_to_file(time_step, x, n_seg)
4041
screen = pygame.display.set_mode(resolution)
4142
running = True
4243
while running:
@@ -60,5 +61,6 @@ def screen_projection(x):
6061
[x, v] = time_integrator.step_forward(x, e, v, m, l2, k, h, 1e-2)
6162
time_step += 1
6263
pygame.time.wait(int(h * 1000))
64+
square_mesh.write_to_file(time_step, x, n_seg)
6365

6466
pygame.quit()

mass_spring/square_mesh.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import os
23

34
def generate(side_length, n_seg):
45
# sample nodes uniformly on a square
@@ -24,4 +25,22 @@ def generate(side_length, n_seg):
2425
e.append([i * (n_seg + 1) + j, (i + 1) * (n_seg + 1) + j + 1])
2526
e.append([(i + 1) * (n_seg + 1) + j, i * (n_seg + 1) + j + 1])
2627

27-
return [x, e]
28+
return [x, e]
29+
30+
def write_to_file(frameNum, x, n_seg):
31+
# Check if 'output' directory exists; if not, create it
32+
if not os.path.exists('output'):
33+
os.makedirs('output')
34+
35+
# create obj file
36+
filename = f"output/{frameNum}.obj"
37+
with open(filename, 'w') as f:
38+
# write vertex coordinates
39+
for row in x:
40+
f.write(f"v {float(row[0]):.6f} {float(row[1]):.6f} 0.0\n")
41+
# write vertex indices for each triangle
42+
for i in range(0, n_seg):
43+
for j in range(0, n_seg):
44+
#NOTE: each cell is exported as 2 triangles for rendering
45+
f.write(f"f {i * (n_seg+1) + j + 1} {(i+1) * (n_seg+1) + j + 1} {(i+1) * (n_seg+1) + j+1 + 1}\n")
46+
f.write(f"f {i * (n_seg+1) + j + 1} {(i+1) * (n_seg+1) + j+1 + 1} {i * (n_seg+1) + j+1 + 1}\n")

0 commit comments

Comments
 (0)