Skip to content

Commit 0ad4be4

Browse files
committed
output obj for FEM
1 parent c138563 commit 0ad4be4

6 files changed

Lines changed: 60 additions & 3 deletions

File tree

6_inv_free/simulator.py

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

5252
time_step = 0
53+
square_mesh.write_to_file(time_step, x, e)
5354
screen = pygame.display.set_mode(resolution)
5455
running = True
5556
while running:
@@ -80,5 +81,6 @@ def screen_projection(x):
8081
[x, v] = time_integrator.step_forward(x, e, v, m, vol, IB, mu_lame, lam, ground_n, ground_o, contact_area, mu, is_DBC, DBC, DBC_v, DBC_limit, h, 1e-2)
8182
time_step += 1
8283
pygame.time.wait(int(h * 1000))
84+
square_mesh.write_to_file(time_step, x, e)
8385

8486
pygame.quit()

6_inv_free/square_mesh.py

Lines changed: 18 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
@@ -20,4 +21,20 @@ def generate(side_length, n_seg):
2021
e.append([i * (n_seg + 1) + j, (i + 1) * (n_seg + 1) + j, (i + 1) * (n_seg + 1) + j + 1])
2122
e.append([i * (n_seg + 1) + j, (i + 1) * (n_seg + 1) + j + 1, i * (n_seg + 1) + j + 1])
2223

23-
return [x, e]
24+
return [x, e]
25+
26+
def write_to_file(frameNum, x, e):
27+
# Check if 'output' directory exists; if not, create it
28+
if not os.path.exists('output'):
29+
os.makedirs('output')
30+
31+
# create obj file
32+
filename = f"output/{frameNum}.obj"
33+
with open(filename, 'w') as f:
34+
# write vertex coordinates
35+
for row in x:
36+
f.write(f"v {float(row[0]):.6f} {float(row[1]):.6f} 0.0\n")
37+
# write vertex indices for each triangle
38+
for row in e:
39+
#NOTE: vertex indices start from 1 in obj file format
40+
f.write(f"f {row[0] + 1} {row[1] + 1} {row[2] + 1}\n")

7_self_contact/simulator.py

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

5555
time_step = 0
56+
square_mesh.write_to_file(time_step, x, e)
5657
screen = pygame.display.set_mode(resolution)
5758
running = True
5859
while running:
@@ -83,5 +84,6 @@ def screen_projection(x):
8384
[x, v] = time_integrator.step_forward(x, e, v, m, vol, IB, mu_lame, lam, ground_n, ground_o, bp, be, contact_area, mu, is_DBC, DBC, DBC_v, DBC_limit, h, 1e-2)
8485
time_step += 1
8586
pygame.time.wait(int(h * 1000))
87+
square_mesh.write_to_file(time_step, x, e)
8688

8789
pygame.quit()

7_self_contact/square_mesh.py

Lines changed: 18 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
@@ -39,4 +40,20 @@ def find_boundary(e):
3940
be.append([eI[0], eI[1]])
4041
bp_set.add(eI[0])
4142
bp_set.add(eI[1])
42-
return [list(bp_set), be]
43+
return [list(bp_set), be]
44+
45+
def write_to_file(frameNum, x, e):
46+
# Check if 'output' directory exists; if not, create it
47+
if not os.path.exists('output'):
48+
os.makedirs('output')
49+
50+
# create obj file
51+
filename = f"output/{frameNum}.obj"
52+
with open(filename, 'w') as f:
53+
# write vertex coordinates
54+
for row in x:
55+
f.write(f"v {float(row[0]):.6f} {float(row[1]):.6f} 0.0\n")
56+
# write vertex indices for each triangle
57+
for row in e:
58+
#NOTE: vertex indices start from 1 in obj file format
59+
f.write(f"f {row[0] + 1} {row[1] + 1} {row[2] + 1}\n")

8_self_friction/simulator.py

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

5555
time_step = 0
56+
square_mesh.write_to_file(time_step, x, e)
5657
screen = pygame.display.set_mode(resolution)
5758
running = True
5859
while running:
@@ -83,5 +84,6 @@ def screen_projection(x):
8384
[x, v] = time_integrator.step_forward(x, e, v, m, vol, IB, mu_lame, lam, ground_n, ground_o, bp, be, contact_area, mu, is_DBC, DBC, DBC_v, DBC_limit, h, 1e-2)
8485
time_step += 1
8586
pygame.time.wait(int(h * 1000))
87+
square_mesh.write_to_file(time_step, x, e)
8688

8789
pygame.quit()

8_self_friction/square_mesh.py

Lines changed: 18 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
@@ -39,4 +40,20 @@ def find_boundary(e):
3940
be.append([eI[0], eI[1]])
4041
bp_set.add(eI[0])
4142
bp_set.add(eI[1])
42-
return [list(bp_set), be]
43+
return [list(bp_set), be]
44+
45+
def write_to_file(frameNum, x, e):
46+
# Check if 'output' directory exists; if not, create it
47+
if not os.path.exists('output'):
48+
os.makedirs('output')
49+
50+
# create obj file
51+
filename = f"output/{frameNum}.obj"
52+
with open(filename, 'w') as f:
53+
# write vertex coordinates
54+
for row in x:
55+
f.write(f"v {float(row[0]):.6f} {float(row[1]):.6f} 0.0\n")
56+
# write vertex indices for each triangle
57+
for row in e:
58+
#NOTE: vertex indices start from 1 in obj file format
59+
f.write(f"f {row[0] + 1} {row[1] + 1} {row[2] + 1}\n")

0 commit comments

Comments
 (0)