-
Notifications
You must be signed in to change notification settings - Fork 23
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
Bug when using single precsion #7
Comments
I replaced CH_FLOAT rnd(CH_FLOAT x, CH_FLOAT y)
{
// Reference(s):
//
// - Improvements to the canonical one-liner GLSL rand() for OpenGL ES 2.0
// http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/
//
CH_FLOAT a = 12.9898;
CH_FLOAT b = 78.233;
CH_FLOAT c = 43758.5453;
CH_FLOAT dt = x*a + y*b;
#ifdef CONVHULL_3D_USE_SINGLE_PRECISION
float sn = fmodf(dt, 3.14);
float intpart;
return modff(sin(sn) * c, &intpart);
#else
double sn = fmod(dt, 3.14);
double intpart;
return modf(sin(sn) * c, &intpart);
#endif // CONVHULL_3D_USE_SINGLE_PRECISION
} |
Input that fails with ch_vec3 vert[] =
{
{ 2.531722, 6.652832, -0.009874 },
{ 3.408637, 5.542159, -1.423177 },
{ 0.189403, -0.000172, 3.765184 },
{ 1.066318, -1.110846, 2.351881 },
{ 0.832276, 6.652832, -1.064333 },
{ 1.709191, 5.542159, -2.477636 },
{ -1.510042, -0.000172, 2.710725 },
{ -0.633127, -1.110846, 1.297422 },
}; Input in these tests is just box that rotates. |
Hi! Thank you for letting me know, and for investigating it further. This looks like a problem of the numerical precision (or rather: in-precision) when trying to figure out the orientation of a face. This assert is basically to stop the loop from going on for infinity. Indeed, the introduction of a bit noise is inelegant, but I've not found a better way yet. So I am very open to suggestions. Note that the issue is significantly more rare when using double precision, which is why this is the default configuration. Double precision is also the default for MATLAB, so the algorithm in the original implementation (which convhull_3d is derived from) also used double precision: |
One thing I was thinking it to introduce another define. Right now define single or double precision affects input/output, and processing data. It would be ideal if input/output data could be single precission while processing happens as double precission. Another idea (I didn't actually check much code, so it might already happen) option is to scale inputs into homogenous space do all calculation, and then restore it back into model scale. |
@bkaradzic good suggestions! If And true, the interfacing with the function could be single precision while having the internals using double precision. I actually wrote this wrapper for another project, but it could be integrated into And do you mean to scale-up the vertices values prior to building the convexhull? If it helps, then I say go for it : ) |
Ok, I fixed random issue: #8 I tested moving vertices into origin into homogeneous -1, 1 space, but while it improved situation, it didn't fully solve the issue. I actually need to step thru to completely understand why this is happening. Wrapper function does solve the issue on simple models I've been using. Probably good enough, but I'm really curious to dig into it, to figure out what actually is causing it. I feel my simple box, with 8 points should be error free even with single-precision, floating point range is pretty low, and it has no overlapping points. |
I ran into issue where calling function
convhull_3d_build
multiple times with the same input causes different results. The bug exist only when used withCONVHULL_3D_USE_SINGLE_PRECISION
.Repro:
Program hits this assert:
convhull_3d/convhull_3d.h
Line 923 in 88eca3c
The text was updated successfully, but these errors were encountered: