static int stl_check_normal_vector(stl_file *stl, int facet_num, int normal_fix_flag) { /* Returns 0 if the normal is within tolerance */ /* Returns 1 if the normal is not within tolerance, but direction is OK */ /* Returns 2 if the normal is not within tolerance and backwards */ /* Returns 4 if the status is unknown. */ stl_facet *facet; facet = &stl->facet_start[facet_num]; stl_normal normal; stl_calculate_normal(normal, facet); stl_normalize_vector(normal); stl_normal normal_dif = (normal - facet->normal).cwiseAbs(); const float eps = 0.001f; if (normal_dif(0) < eps && normal_dif(1) < eps && normal_dif(2) < eps) { /* It is not really necessary to change the values here */ /* but just for consistency, I will. */ facet->normal = normal; return 0; } stl_normal test_norm = facet->normal; stl_normalize_vector(test_norm); normal_dif = (normal - test_norm).cwiseAbs(); if (normal_dif(0) < eps && normal_dif(1) < eps && normal_dif(2) < eps) { if(normal_fix_flag) { facet->normal = normal; stl->stats.normals_fixed += 1; } return 1; } test_norm *= -1.f; normal_dif = (normal - test_norm).cwiseAbs(); if (normal_dif(0) < eps && normal_dif(1) < eps && normal_dif(2) < eps) { // Facet is backwards. if(normal_fix_flag) { facet->normal = normal; stl->stats.normals_fixed += 1; } return 2; } if(normal_fix_flag) { facet->normal = normal; stl->stats.normals_fixed += 1; } return 4; }
static float get_area(stl_facet *facet) { float cross[3][3]; float sum[3]; float n[3]; float area; int i; 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)); } sum[0] = cross[0][0] + cross[1][0] + cross[2][0]; sum[1] = cross[0][1] + cross[1][1] + cross[2][1]; sum[2] = cross[0][2] + cross[1][2] + cross[2][2]; /* This should already be done. But just in case, let's do it again */ stl_calculate_normal(n, facet); stl_normalize_vector(n); area = 0.5 * (n[0] * sum[0] + n[1] * sum[1] + n[2] * sum[2]); return area; }
void stl_reverse_all_facets(stl_file *stl) { if (stl->error) return; stl_normal normal; for(int i = 0; i < stl->stats.number_of_facets; i++) { stl_reverse_facet(stl, i); stl_calculate_normal(normal, &stl->facet_start[i]); stl_normalize_vector(normal); stl->facet_start[i].normal = normal; } }
static void calculate_normals(stl_file *stl) { long i; float normal[3]; for(i = 0; i < stl->stats.number_of_facets; i++){ stl_calculate_normal(normal, &stl->facet_start[i]); stl_normalize_vector(normal); stl->facet_start[i].normal.x = normal[0]; stl->facet_start[i].normal.y = normal[1]; stl->facet_start[i].normal.z = normal[2]; } }
void stl_reverse_all_facets(stl_file *stl) { int i; float normal[3]; for(i = 0; i < stl->stats.number_of_facets; i++) { stl_reverse_facet(stl, i); stl_calculate_normal(normal, &stl->facet_start[i]); stl_normalize_vector(normal); stl->facet_start[i].normal.x = normal[0]; stl->facet_start[i].normal.y = normal[1]; stl->facet_start[i].normal.z = normal[2]; } }
int stl_check_normal_vector(stl_file *stl, int facet_num, int normal_fix_flag) { /* Returns 0 if the normal is within tolerance */ /* Returns 1 if the normal is not within tolerance, but direction is OK */ /* Returns 2 if the normal is not within tolerance and backwards */ /* Returns 4 if the status is unknown. */ float normal[3]; float test_norm[3]; stl_facet *facet; facet = &stl->facet_start[facet_num]; stl_calculate_normal(normal, facet); stl_normalize_vector(normal); if( (ABS(normal[0] - facet->normal.x) < 0.001) && (ABS(normal[1] - facet->normal.y) < 0.001) && (ABS(normal[2] - facet->normal.z) < 0.001)) { /* It is not really necessary to change the values here */ /* but just for consistency, I will. */ facet->normal.x = normal[0]; facet->normal.y = normal[1]; facet->normal.z = normal[2]; return 0; } test_norm[0] = facet->normal.x; test_norm[1] = facet->normal.y; test_norm[2] = facet->normal.z; stl_normalize_vector(test_norm); if( (ABS(normal[0] - test_norm[0]) < 0.001) && (ABS(normal[1] - test_norm[1]) < 0.001) && (ABS(normal[2] - test_norm[2]) < 0.001)) { if(normal_fix_flag) { facet->normal.x = normal[0]; facet->normal.y = normal[1]; facet->normal.z = normal[2]; stl->stats.normals_fixed += 1; } return 1; } stl_reverse_vector(test_norm); if( (ABS(normal[0] - test_norm[0]) < 0.001) && (ABS(normal[1] - test_norm[1]) < 0.001) && (ABS(normal[2] - test_norm[2]) < 0.001)) { /* Facet is backwards. */ if(normal_fix_flag) { facet->normal.x = normal[0]; facet->normal.y = normal[1]; facet->normal.z = normal[2]; stl->stats.normals_fixed += 1; } return 2; } if(normal_fix_flag) { facet->normal.x = normal[0]; facet->normal.y = normal[1]; facet->normal.z = normal[2]; stl->stats.normals_fixed += 1; } return 4; }