Bug in ConvexHull

i think i found a bug in the ConvexHull function found in b2Polygon.cpp (ConvexDecomposition).
one shape i was trying to find a ConvexHull just crashed because the condition winIndex != minYIndex was never satisfied in the while loop.
so i added another condition. now it looks like:
Code:
while ((winIndex != minYIndex) && (numEdges < nVert)) { ...


i have to admit that i didn't bother too much to understand the code but with adding my condition it worked.

by the way: the shape i was trying to find the shape of was the outline of the letter 'Q'.

best & thanks for the wonderful box2d & the contributed ConvexDecomposition
joerg
I'm sorry, but the code in the Contrib section is not so well trafficked. I cannot verify whether this is a good patch or not, and we cannot rely on users picking up bug for contributed code. By your own admission you're none too certain either. I suggest you contact the original author (ewjordan, it seems), and ask him to "bless" it.
I'll take a look through and see if this is the right solution - would it be possible for you to post the exact vertices or code that cause the problem? Usually these things happen because things are very close to co-linear or something like that, but it can be very dependent on the specific vertices. I tried to randomly stress test this code, but sometimes you just miss things.

Thanks for the report!
thanks for your help but you were right:
it was a problem with the vertices. i had checked for collinearity but not for double vertices. so i had double and triple vertices...

but maybe it would be good to add my condition and print out an error message in case it's about to crash?
wouldn't cost that much processor time but could be helpful to others who encounter the same problem.

anyway if you'd like to check here's a list of vertices that crashes ConvexHull (the values in parantheses are the vertex values scaled to the box2d scale):
vertex 0: 0.500000, 2.609375 (0.050000, 0.260938)
vertex 1: 0.500000, 2.609375 (0.050000, 0.260938)
vertex 2: 0.500000, 1.203125 (0.050000, 0.120312)
vertex 3: 0.500000, 1.203125 (0.050000, 0.120312)
vertex 4: 0.500000, 1.203125 (0.050000, 0.120312)
vertex 5: 0.015625, 1.203125 (0.001563, 0.120312)
vertex 6: -0.500000, 0.921875 (-0.050000, 0.092188)
vertex 7: -0.500000, 0.468750 (-0.050000, 0.046875)
vertex 8: -0.234375, 0.203125 (-0.023438, 0.020312)
vertex 9: 0.234375, 0.203125 (0.023438, 0.020312)
vertex 10: 0.500000, 0.390625 (0.050000, 0.039062)
vertex 11: 0.500000, 0.875000 (0.050000, 0.087500)

with the double & triple vertices removed it works fine.

best
joerg
Ah, glad you figured it out for me. 🙂

The utility really should handle multiple or colinear vertices properly, but it takes special handling (the gift wrap algorithm is failing because it's trying to compute the angle between line segments of zero length and it's not checking for that special case first). IIRC I went to some pains to make sure the convex decomposition code handled these things properly (basically by removing redundant vertices automatically beforehand). I never noticed the same issues in the convex hull routine because I never had a reason to personally use the code. 🙂 This code is not meant to be used in real-time in any case (buyer beware - I'll happily accept contributions that are faster, though, as long as they are equally robust!), so adding those checks shouldn't hurt too much. I'll put that in when I get a chance, in the meantime just check for multiple points yourself.

Thanks for the report!

Original thread: https://box2d.org/forum/viewtopic.php?t=2654