Reverted unification of positive and negative zeros when loaded

from an STL file.
This commit is contained in:
bubnikv 2017-03-03 12:54:00 +01:00
parent 4d00aa1800
commit 930e6752d9

View File

@ -308,6 +308,29 @@ stl_read(stl_file *stl, int first_facet, int first) {
printf("stl_read: facet %d.z = %e\r\n", j, facet.vertex[j].z); printf("stl_read: facet %d.z = %e\r\n", j, facet.vertex[j].z);
} }
#endif #endif
#if 1
{
// Positive and negative zeros are possible in the floats, which are considered equal by the FP unit.
// When using a memcmp on raw floats, those numbers report to be different.
// Unify all +0 and -0 to +0 to make the floats equal under memcmp.
uint32_t *f = (uint32_t*)&facet;
for (int j = 0; j < 12; ++ j, ++ f) // 3x vertex + normal: 4x3 = 12 floats
if (*f == 0x80000000)
// Negative zero, switch to positive zero.
*f = 0;
}
#else
{
// Due to the nature of the floating point numbers, close to zero values may be represented with singificantly higher precision
// than the rest of the vertices. Round them to zero.
float *f = (float*)&facet;
for (int j = 0; j < 12; ++ j, ++ f) // 3x vertex + normal: 4x3 = 12 floats
if (*f > -1e-12f && *f < 1e-12f)
// Negative zero, switch to positive zero.
*f = 0;
}
#endif
/* Write the facet into memory. */ /* Write the facet into memory. */
memcpy(stl->facet_start+i, &facet, SIZEOF_STL_FACET); memcpy(stl->facet_start+i, &facet, SIZEOF_STL_FACET);
stl_facet_stats(stl, facet, first); stl_facet_stats(stl, facet, first);