diff options
author | Alessandro Ranellucci <aar@cpan.org> | 2014-01-02 21:50:54 +0400 |
---|---|---|
committer | Alessandro Ranellucci <aar@cpan.org> | 2014-01-02 21:52:27 +0400 |
commit | 44e7e542c2a944d5195c1919ad26975abd927591 (patch) | |
tree | e0aaa539d8c7b6699a5cdfff16b4f3e0e140dffe /xs/src/admesh | |
parent | 380a083a3efc0d40cda61870662ae2699d4c9515 (diff) |
Bugfix: overflow causing full object facets reversal when STL file has large coordinates. #1666
Diffstat (limited to 'xs/src/admesh')
-rw-r--r-- | xs/src/admesh/util.c | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/xs/src/admesh/util.c b/xs/src/admesh/util.c index 84e9c1191..22aad0432 100644 --- a/xs/src/admesh/util.c +++ b/xs/src/admesh/util.c @@ -376,19 +376,22 @@ void stl_calculate_volume(stl_file *stl) static float get_area(stl_facet *facet) { - float cross[3][3]; + double cross[3][3]; float sum[3]; float n[3]; float area; int i; + // cast to double before calculating cross product because large coordinates + // can result in overflowing product + // (bad area is responsible for bad volume and bad facets reversal) for(i = 0; i < 3; i++){ - cross[i][0]=((facet->vertex[i].y * facet->vertex[(i + 1) % 3].z) - - (facet->vertex[i].z * facet->vertex[(i + 1) % 3].y)); - cross[i][1]=((facet->vertex[i].z * facet->vertex[(i + 1) % 3].x) - - (facet->vertex[i].x * facet->vertex[(i + 1) % 3].z)); - cross[i][2]=((facet->vertex[i].x * facet->vertex[(i + 1) % 3].y) - - (facet->vertex[i].y * facet->vertex[(i + 1) % 3].x)); + cross[i][0]=(((double)facet->vertex[i].y * (double)facet->vertex[(i + 1) % 3].z) - + ((double)facet->vertex[i].z * (double)facet->vertex[(i + 1) % 3].y)); + cross[i][1]=(((double)facet->vertex[i].z * (double)facet->vertex[(i + 1) % 3].x) - + ((double)facet->vertex[i].x * (double)facet->vertex[(i + 1) % 3].z)); + cross[i][2]=(((double)facet->vertex[i].x * (double)facet->vertex[(i + 1) % 3].y) - + ((double)facet->vertex[i].y * (double)facet->vertex[(i + 1) % 3].x)); } sum[0] = cross[0][0] + cross[1][0] + cross[2][0]; |