Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convex hull produces invalid topology #311

Open
werner291 opened this issue Jan 31, 2025 · 3 comments
Open

Convex hull produces invalid topology #311

werner291 opened this issue Jan 31, 2025 · 3 comments

Comments

@werner291
Copy link

Hi there,

I put this in the Discord too, but just for good measure I'll open an issue too.

It appears that ConvexPolyhedron::from_convex_hull(&points) produces invalid topology:

use std::fs::File;
use parry3d::na::Point3;
use parry3d::shape::{ConvexPolyhedron, TriMeshFlags};

fn main() {
    let decoded: Vec<f32> = bincode::deserialize_from(File::open("points.bin").unwrap()).unwrap();

    let points : Vec<Point3<f32>> = decoded.chunks(3).map(|c| Point3::new(c[0], c[1], c[2])).collect();

    let convex = ConvexPolyhedron::from_convex_hull(&points)
        .expect("Failed to compute convex hull of mesh");

    let (vertices, mut indices) = convex.to_trimesh();

    let mut convex_mesh = parry3d::shape::TriMesh::new(vertices, indices)
        .expect("Failed to convert convex polyhedron to triangle mesh");

    // This panics.
    convex_mesh.set_flags(TriMeshFlags::HALF_EDGE_TOPOLOGY).unwrap()
}

Here's the input data (remove the .txt, GitHub refused to upload it otherwise)

points.bin.txt

@werner291
Copy link
Author

werner291 commented Jan 31, 2025

let (vertices, mut indices) = convex.to_trimesh();

    let mut edge_counter = HashMap::new();

    for triangle in indices.iter_mut() {
        for i in 0..3 {
            let edge = [triangle[i], triangle[(i + 1) % 3]];

            edge_counter.entry(edge).and_modify(|e| *e += 1).or_insert(1);

            if edge_counter[&edge] > 1 {
                println!("Edge {:?} is shared by {} triangles", edge, edge_counter[&edge]);
            }
        }
    }

Oh, this is weird:

Edge [31, 22] is shared by 2 triangles
Edge [22, 31] is shared by 2 triangles

If we see edges as undirected, that's one edge showing up 4 times there.

@werner291
Copy link
Author

werner291 commented Jan 31, 2025

Image

Here's a picture of the bad edge (highlighted in magenta); the renderer doesn't seem to quite know what to do with it either

@werner291
Copy link
Author

Here I'm highlighting all triangles where at least one edge is part of a duplicated set:

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant