dimforge / parry

2D and 3D collision-detection library for Rust.
https://parry.rs
Apache License 2.0
555 stars 96 forks source link

Convex decomposition panics on some inputs #65

Open hakolao opened 2 years ago

hakolao commented 2 years ago

The following input panics:

thread 'main' panicked at 'index out of bounds: the len is 0 but the index is 0', .../build/parry2d/../../src/shape/convex_polygon.rs:40:12
    let verts = vec![
     Point::new(0.04296875, -0.021484375),
        Point::new(0.041015625, -0.0234375),
        Point::new(0.0390625, -0.025390625),
        Point::new(0.037109375, -0.02734375),
        Point::new(0.03515625, -0.025390625),
        Point::new(0.033203125, -0.0234375),
        Point::new(0.029296875, -0.0234375),
        Point::new(0.02734375, -0.021484375),
        Point::new(0.029296875, -0.01953125),
        Point::new(0.033203125, -0.01953125),
        Point::new(0.037109375, -0.01953125),
        Point::new(0.041015625, -0.01953125),
        Point::new(0.04296875, -0.021484375),
    ];
    let indices = vec![
        [0, 1],
        [1, 2],
        [2, 3],
        [3, 4],
        [4, 5],
        [5, 6],
        [6, 7],
        [7, 8],
        [8, 9],
        [9, 10],
        [10, 11],
        [11, 12],
    ];
    let shape =
       SharedShape::convex_decomposition_with_params(&verts, &indices, &VHACDParameters {
            resolution: 64,
            ..VHACDParameters::default()
        });

This seems to depend on the resolution, if I use 32 or more than 64 on this specific input, it works. I'd like to use 32 or 64 for speed. It does end up in this panic however occasionally with some inputs.

Investigation: The decomp.compute_exact_convex_hulls(&vertices, &indices) after decompose outputs some empty vertex vectors and passes them to ConvexPolygon::from_convex_polyline. Which then panics when indexing first normal[0] when checking if first vertex should be removed.

Two fix ideas:

  1. Figure out why compute_exact_convex_hulls outputs empty sets of points, fix that or just filter empty parts out.
  2. Don't allow ConvexPolygon::from_convex_polyline to work on empty vectors... (return early if points.len() == 0.
hakolao commented 2 years ago

I suppose this belongs to rapier instead... Will close.