Skip to content

Commit 6402d75

Browse files
committed
add util for splitting meshes by views
1 parent d39d1dc commit 6402d75

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/meshes.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,30 @@ function merge_vertex_indices(
437437
end
438438

439439

440+
"""
441+
split_mesh(mesh::Mesh, views::Vector{UnitRange{Int}} = mesh.views)
442+
443+
Creates a new mesh containing `faces(mesh)[range]` for each range in `views`.
444+
This also removes unused vertices.
445+
"""
446+
function split_mesh(mesh::Mesh, views::Vector{UnitRange{Int}} = mesh.views)
447+
return map(views) do idxs
448+
new_fs, maps = merge_vertex_indices((view(faces(mesh), idxs),))
449+
new_va = Dict{Symbol, VertexAttributeType}()
450+
451+
for (k, v) in vertex_attributes(mesh)
452+
if v isa FaceView
453+
_fs, _maps = merge_vertex_indices((view(faces(v), idxs),))
454+
new_va[k] = FaceView(values(v)[_maps[1]], _fs)
455+
else
456+
new_va[k] = v[maps[1]]
457+
end
458+
end
459+
460+
return Mesh(new_va, new_fs)
461+
end
462+
end
463+
440464

441465
function map_coordinates(f, mesh::Mesh)
442466
result = copy(mesh)

0 commit comments

Comments
 (0)