Skip to content

Commit 042d297

Browse files
committed
write mesh example 2 - 5
1 parent 44cf12c commit 042d297

8 files changed

Lines changed: 88 additions & 4 deletions

File tree

2_dirichlet/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, is_DBC, 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()

2_dirichlet/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")

3_contact/simulator.py

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

4242
time_step = 0
43+
square_mesh.write_to_file(time_step, x, n_seg)
4344
screen = pygame.display.set_mode(resolution)
4445
running = True
4546
while running:
@@ -64,5 +65,6 @@ def screen_projection(x):
6465
[x, v] = time_integrator.step_forward(x, e, v, m, l2, k, y_ground, contact_area, is_DBC, h, 1e-2)
6566
time_step += 1
6667
pygame.time.wait(int(h * 1000))
68+
square_mesh.write_to_file(time_step, x, n_seg)
6769

6870
pygame.quit()

3_contact/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")

4_friction/simulator.py

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

4545
time_step = 0
46+
square_mesh.write_to_file(time_step, x, n_seg)
4647
screen = pygame.display.set_mode(resolution)
4748
running = True
4849
while running:
@@ -68,5 +69,6 @@ def screen_projection(x):
6869
[x, v] = time_integrator.step_forward(x, e, v, m, l2, k, ground_n, ground_o, contact_area, mu, is_DBC, h, 1e-2)
6970
time_step += 1
7071
pygame.time.wait(int(h * 1000))
72+
square_mesh.write_to_file(time_step, x, n_seg)
7173

7274
pygame.quit()

4_friction/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")

5_mov_dirichlet/simulator.py

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

4848
time_step = 0
49+
square_mesh.write_to_file(time_step, x, n_seg)
4950
screen = pygame.display.set_mode(resolution)
5051
running = True
5152
while running:
@@ -74,5 +75,6 @@ def screen_projection(x):
7475
[x, v] = time_integrator.step_forward(x, e, v, m, l2, k, ground_n, ground_o, contact_area, mu, is_DBC, DBC, DBC_v, DBC_limit, h, 1e-2)
7576
time_step += 1
7677
pygame.time.wait(int(h * 1000))
78+
square_mesh.write_to_file(time_step, x, n_seg)
7779

7880
pygame.quit()

5_mov_dirichlet/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)