MattiaMontanari / openGJK

Fast and reliable implementation of the Gilbert-Johnson-Keerthi (GJK) algorithm for C, C#, Go, Matlab and Python
https://www.mattiamontanari.com/opengjk/
GNU General Public License v3.0
135 stars 37 forks source link

Two rather simple non-colliding objects, but pygjk() returns "nan" #41

Closed manuel-koch closed 9 months ago

manuel-koch commented 1 year ago

The following code returns distance nan for two distinct not colliding rather simple objects. When running equivalent python code within a pytest suite, I can see the following output on captured console too ( but this is not printed in the snippet posted here ) : MAXIMUM ITERATION NUMBER REACHED!

import numpy
import openGJK_cython as opengjk

if __name__ == "__main__":
    vert_sphere_like = numpy.array(
        [
            [-0.58600003, 0.0, 1.0],
            [0.0, -0.58600003, 1.0],
            [0.58600003, 0.0, 1.0],
            [0.0, 0.58600003, 1.0],
            [1.0, 0.0, -0.70700002],
            [1.0, 0.414, -0.414],
            [1.0, 0.414, 0.414],
            [1.0, 0.0, 0.70700002],
            [1.0, -0.414, 0.414],
            [1.0, -0.414, -0.414],
            [0.0, 1.0, -0.70700002],
            [-0.414, 1.0, -0.414],
            [-0.414, 1.0, 0.414],
            [0.0, 1.0, 0.70700002],
            [0.414, 1.0, 0.414],
            [0.414, 1.0, -0.414],
            [-1.0, 0.0, -0.70700002],
            [-1.0, -0.414, -0.414],
            [-1.0, -0.414, 0.414],
            [-1.0, 0.0, 0.70700002],
            [-1.0, 0.414, 0.414],
            [-1.0, 0.414, -0.414],
            [0.0, -1.0, -0.70700002],
            [0.414, -1.0, -0.414],
            [0.414, -1.0, 0.414],
            [0.0, -1.0, 0.70700002],
            [-0.414, -1.0, 0.414],
            [-0.414, -1.0, -0.414],
            [0.0, 0.58600003, -1.0],
            [0.58600003, 0.0, -1.0],
            [0.0, -0.58600003, -1.0],
            [-0.58600003, 0.0, -1.0],
        ],
        dtype=numpy.float64,
    )
    vert_extruded_cube = numpy.array(
        [
            [-2.5, 0.0, 0.5],
            [-3.5, 1.0, 0.5],
            [-2.5, 0.0, -0.5],
            [-3.5, 1.0, -0.5],
            [0.0, 2.5, 0.5],
            [0.0, 3.5, 0.5],
            [-1.0, 3.5, 0.5],
            [0.0, 2.5, -0.5],
            [0.0, 3.5, -0.5],
            [-1.0, 3.5, -0.5],
        ],
        dtype=numpy.float64,
    )
    distance = opengjk.pygjk(vert_sphere_like, vert_extruded_cube)
    print(distance)

The scene looks like the following screenshots ( two perspectives of same scene ):

two-objects-from-left two-objects-from-right

My environment is MacOS Ventura 13.3.1 (22E261) arm64, Python 3.10.6, cpython build of openGJK ( master, commit 47df2d7 )

MattiaMontanari commented 9 months ago

@manuel-koch this is interesting! I can't put my finger on it. Looks like two faces are parallel, but that shouldn't be a big problem at that distance. I will need to take a closer look on the C side. Have you tried to pass these inputs to the C demo?

manuel-koch commented 9 months ago

@MattiaMontanari No, I've not tested the data with the C implementation.

MattiaMontanari commented 9 months ago

@manuel-koch the C implementation shows Distance between bodies 0.767918.

I'd suggest to:

  1. git pull to the latest version
  2. make sure you build with default precision (without SINGLE_PRECISION flag)

If the above don't fix the error, please add a test in examples/cython/test.py with your coords and expected results to be 0.767918. I will take it from there to find the error - which may be a conversion error from double to whatever cython uses

manuel-koch commented 9 months ago

@MattiaMontanari Thank you for the feedback. I already found some other python package to find collisions between meshes that fits my use case: distance3d