Skip to content

Commit

Permalink
feat: add concatenate and consolidate to Mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
william-silversmith committed Oct 30, 2024
1 parent bc2427f commit f7434b3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions zmesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,43 @@ def triangles(self):

return tris

@classmethod
def concatenate(cls, *meshes, id=None):
vertex_ct = np.zeros(len(meshes) + 1, np.uint32)
vertex_ct[1:] = np.cumsum([ len(mesh) for mesh in meshes ])

vertices = np.concatenate([ mesh.vertices for mesh in meshes ])

faces = np.concatenate([
mesh.faces + vertex_ct[i] for i, mesh in enumerate(meshes)
])

# normals = np.concatenate([ mesh.normals for mesh in meshes ])

return Mesh(vertices, faces, None, id=id)

def consolidate(self):
"""Remove duplicate vertices and faces. Returns a new mesh object."""
if self.empty():
return Mesh([], [], normals=None)

vertices = self.vertices
faces = self.faces
normals = self.normals

eff_verts, uniq_idx, idx_representative = np.unique(
vertices, axis=0, return_index=True, return_inverse=True
)

face_vector_map = np.vectorize(lambda x: idx_representative[x])
eff_faces = face_vector_map(faces)
eff_faces = np.unique(eff_faces, axis=0)

# normal_vector_map = np.vectorize(lambda idx: normals[idx])
# eff_normals = normal_vector_map(uniq_idx)

return Mesh(eff_verts, eff_faces, None, id=self.id)

@classmethod
def from_precomputed(self, binary):
"""
Expand Down

0 comments on commit f7434b3

Please sign in to comment.