From 930e6752d9f60b2c90e1a729e31bf5ff876a7001 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Fri, 3 Mar 2017 12:54:00 +0100 Subject: [PATCH] Reverted unification of positive and negative zeros when loaded from an STL file. --- xs/src/admesh/stlinit.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/xs/src/admesh/stlinit.c b/xs/src/admesh/stlinit.c index f797004fb..2e55462df 100644 --- a/xs/src/admesh/stlinit.c +++ b/xs/src/admesh/stlinit.c @@ -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); } #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. */ memcpy(stl->facet_start+i, &facet, SIZEOF_STL_FACET); stl_facet_stats(stl, facet, first);