Skip to content

Commit c7b5c82

Browse files
committed
initial support for 3d mesh
1 parent 325d1cc commit c7b5c82

2 files changed

Lines changed: 72 additions & 14 deletions

File tree

simloader/importer.py

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,48 @@
77
from mathutils import Matrix
88
import additional_file_formats
99

10-
supported_mesh_format = ['triangle', 'quad']
10+
11+
def extract_faces(cell: meshio.CellBlock):
12+
if cell.type == "triangle":
13+
return cell.data.astype(np.uint64)
14+
elif cell.type == "triangle6":
15+
pass
16+
elif cell.type == "triangle7":
17+
pass
18+
elif cell.type == "quad":
19+
return cell.data.astype(np.uint64)
20+
elif cell.type == "quad8":
21+
pass
22+
elif cell.type == "quad9":
23+
pass
24+
elif cell.type == "tetra":
25+
data = cell.data.astype(np.uint64)
26+
faces = data[:, :3]
27+
faces = np.append(faces, data[:, [0, 2, 3]], axis=0)
28+
faces = np.append(faces, data[:, [0, 1, 3]], axis=0)
29+
faces = np.append(faces, data[:, [1, 2, 3]], axis=0)
30+
faces.sort(axis=1)
31+
_, indxs, count = np.unique(faces, axis=0, return_index=True, return_counts=True)
32+
faces = faces[indxs[count == 1]]
33+
return faces
34+
elif cell.type == "hexahedron":
35+
data = cell.data.astype(np.uint64)
36+
faces = data[:, :4]
37+
faces = np.append(faces, data[:, [0, 1, 5, 4]], axis=0)
38+
faces = np.append(faces, data[:, [1, 2, 6, 5]], axis=0)
39+
faces = np.append(faces, data[:, [2, 3, 7, 6]], axis=0)
40+
faces = np.append(faces, data[:, [5, 6, 7, 4]], axis=0)
41+
faces = np.append(faces, data[:, [0, 3, 7, 4]], axis=0)
42+
# to sort or not to sort?
43+
# Or convert it to triangles?
44+
# faces.sort(axis=1)
45+
_, indxs, count = np.unique(faces, axis=0, return_index=True, return_counts=True)
46+
faces = faces[indxs[count == 1]]
47+
return faces
48+
elif cell.type == "vertex":
49+
return np.array([])
50+
show_message_box(cell.type + " is unsupported mesh format yet")
51+
return np.array([])
1152

1253

1354
def update_mesh(meshio_mesh, mesh):
@@ -18,33 +59,28 @@ def update_mesh(meshio_mesh, mesh):
1859
n_loop = 0
1960
n_verts = len(mesh_vertices)
2061

21-
faces_loop_start = np.array([], dtype=np.int32)
22-
faces_loop_total = np.array([], dtype=np.int32)
23-
loops_vert_idx = np.array([], dtype=np.int32)
62+
faces_loop_start = np.array([], dtype=np.uint64)
63+
faces_loop_total = np.array([], dtype=np.uint64)
64+
loops_vert_idx = np.array([], dtype=np.uint64)
2465
shade_scheme = False
2566
if mesh.polygons:
2667
shade_scheme = mesh.polygons[0].use_smooth
2768
for cell in meshio_mesh.cells:
28-
if cell.type not in supported_mesh_format:
29-
# vertex is not mesh type, but supported
30-
if cell.type != "vertex":
31-
show_message_box(cell.type + " is unsupported mesh format yet")
69+
data = extract_faces(cell)
70+
# np array can't be simply written as `if not data:`,
71+
if not data.any():
3272
continue
33-
data = cell.data
3473
n_poly += len(data)
3574
n_loop += data.shape[0] * data.shape[1]
3675
loops_vert_idx = np.append(loops_vert_idx, data.ravel())
37-
faces_loop_total = np.append(faces_loop_total, np.ones((len(data)), dtype=np.int32) * data.shape[1])
76+
faces_loop_total = np.append(faces_loop_total, np.ones((len(data)), dtype=np.uint64) * data.shape[1])
3877
if faces_loop_total.size > 0:
3978
faces_loop_start = np.cumsum(faces_loop_total)
4079
# Add a zero as first entry
4180
faces_loop_start = np.roll(faces_loop_start, 1)
4281
faces_loop_start[0] = 0
4382

44-
45-
if len(mesh.vertices) == n_verts and \
46-
len(mesh.polygons) == n_poly and \
47-
len(mesh.loops) == n_loop:
83+
if len(mesh.vertices) == n_verts and len(mesh.polygons) == n_poly and len(mesh.loops) == n_loop:
4884
pass
4985
else:
5086
mesh.clear_geometry()

template/dim4.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fileseq
2+
import meshio
3+
import numpy as np
4+
5+
def preprocess(fileseq: fileseq.FileSequence, frame_number: int) -> meshio.Mesh:
6+
# this renders all the faces(both surface and inside)
7+
# by default, the addon only renders the surface faces
8+
frame_number = frame_number % len(fileseq)
9+
mesh = meshio.read(fileseq[frame_number])
10+
new_cells = []
11+
for cell in mesh.cells:
12+
if cell.type=="tetra":
13+
faces = []
14+
for d in cell.data:
15+
faces.append([d[0],d[1],d[2]])
16+
faces.append([d[0],d[1],d[3]])
17+
faces.append([d[0],d[2],d[3]])
18+
faces.append([d[1],d[2],d[3]])
19+
new_cells.append(('triangle',np.array(faces, dtype=np.int32)))
20+
else:
21+
new_cells.append((cell.type,cell.data))
22+
return meshio.Mesh(mesh.points,new_cells,mesh.point_data)

0 commit comments

Comments
 (0)