Skip to content

cumesh simplify bug? #28

@ydayda

Description

@ydayda

Hi, @JeffreyXiang
I test the cumesh simplify function, but it does not simplify to the target_num_faces, is it a bug or right?

the mesh from here using in TRELLIS2.0 o-voxel test.
It hanpens in TRELLIS2.0 too, same behavior.

import torch
import trimesh
import cumesh


def ensure_trimesh(loaded):
    if isinstance(loaded, trimesh.Trimesh):
        return loaded

    if isinstance(loaded, trimesh.Scene):
        # Apply node transforms and merge all geometries into one mesh.
        parts = loaded.dump(concatenate=False)
        meshes = [m for m in parts if isinstance(m, trimesh.Trimesh)]
        if not meshes:
            raise ValueError("Loaded scene has no mesh geometry")
        return trimesh.util.concatenate(meshes)

    raise TypeError(f"Unsupported mesh type: {type(loaded)}")


if __name__ == "__main__":
    loaded = trimesh.load("/home/yda/TRELLIS.2/o-voxel/examples/cache/helmet.glb")

    # Check if it's a Scene and concatenate geometries if so
    if isinstance(loaded, trimesh.Scene):
        print("Scene detected, merging geometries...")
        # dump() returns a list of Trimesh objects; concat combines them
        mesh = trimesh.util.concatenate(
            [geometry for geometry in loaded.geometry.values()]
        )
    else:
        mesh = loaded

    mesh = ensure_trimesh(loaded)

    mesh.export("original.ply")
    vertices = torch.from_numpy(mesh.vertices).float()
    faces = torch.from_numpy(mesh.faces).int()
    print(f"Original mesh: {vertices.shape[0]} vertices, {faces.shape[0]} faces")

    vertices = vertices.cuda()
    faces = faces.cuda()

    target_faces = 10000

    mesh = cumesh.CuMesh()

    mesh.init(vertices, faces)
    mesh.simplify(target_faces, verbose=True)
    new_vertices, new_faces = mesh.read()

    print(f"Simplified mesh: {new_vertices.shape[0]} vertices, {new_faces.shape[0]} faces")

    new_mesh = trimesh.Trimesh(vertices=new_vertices.cpu().numpy(), faces=new_faces.cpu().numpy(), process=False)
    new_mesh.export("simplified.ply")

the running log:

(trellis2) root@h20-2:/home/yda/TRELLIS.2# python cumesh_simplify_test.py 
Scene detected, merging geometries...
Original mesh: 14556 vertices, 15452 faces
Simplifying [thres=1.00e-05]: 100%|██████████████| 5452/5452 [00:00<00:00, 781522.40it/s]
Simplified mesh: 10654 vertices, 9607 faces


another test
(trellis2) root@h20-2:/home/yda/TRELLIS.2# python cumesh_simplify_test.py 
Scene detected, merging geometries...
Original mesh: 18608101 vertices, 25359076 faces
Simplifying [thres=1.00e-04]: 100%|█████| 25349076/25349076 [00:03<00:00, 8326688.68it/s]
Simplified mesh: 2671902 vertices, 8916 faces

cumesh version:

(trellis2) root@h20-2:/home/yda/TRELLIS.2# pip show cumesh
Name: cumesh
Version: 0.0.1
Summary: Cuda mesh utils
Home-page: 
Author: 
Author-email: Jianfeng Xiang <belljig@outlook.com>
License: MIT License

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions