New automatic built-in STL repair

This commit is contained in:
Alessandro Ranellucci 2013-06-23 21:11:46 +02:00
parent f62896a77a
commit 2a2633da0a
7 changed files with 131 additions and 9 deletions

View file

@ -241,3 +241,26 @@ stl_write_vrml(stl_file *stl, char *file)
fprintf(fp, "}\n");
fclose(fp);
}
void stl_write_obj (stl_file *stl, char *file) {
int i;
/* Open the file */
FILE* fp = fopen(file, "w");
if (fp == NULL) {
char* error_msg = (char*)malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */
sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", file);
perror(error_msg);
free(error_msg);
exit(1);
}
for (i = 0; i < stl->stats.shared_vertices; i++) {
fprintf(fp, "v %f %f %f\n", stl->v_shared[i].x, stl->v_shared[i].y, stl->v_shared[i].z);
}
for (i = 0; i < stl->stats.number_of_facets; i++) {
fprintf(fp, "f %d %d %d\n", stl->v_indices[i].vertex[0]+1, stl->v_indices[i].vertex[1]+1, stl->v_indices[i].vertex[2]+1);
}
fclose(fp);
}

View file

@ -164,6 +164,7 @@ extern void stl_mirror_yz(stl_file *stl);
extern void stl_mirror_xz(stl_file *stl);
extern void stl_open_merge(stl_file *stl, char *file);
extern void stl_generate_shared_vertices(stl_file *stl);
extern void stl_write_obj(stl_file *stl, char *file);
extern void stl_write_off(stl_file *stl, char *file);
extern void stl_write_dxf(stl_file *stl, char *file, char *label);
extern void stl_write_vrml(stl_file *stl, char *file);

View file

@ -13,25 +13,72 @@ hello_world()
OUTPUT:
RETVAL
float
stl_volume(filename)
char* filename;
void
stl_repair(input_file, output_file)
char* input_file;
char* output_file;
CODE:
stl_file stl_in;
stl_open(&stl_in, filename);
int i;
stl_file stl_in;
stl_open(&stl_in, input_file);
// checking exact
stl_check_facets_exact(&stl_in);
stl_in.stats.facets_w_1_bad_edge = (stl_in.stats.connected_facets_2_edge - stl_in.stats.connected_facets_3_edge);
stl_in.stats.facets_w_2_bad_edge = (stl_in.stats.connected_facets_1_edge - stl_in.stats.connected_facets_2_edge);
stl_in.stats.facets_w_3_bad_edge = (stl_in.stats.number_of_facets - stl_in.stats.connected_facets_1_edge);
// checking nearby
int last_edges_fixed = 0;
float tolerance = stl_in.stats.shortest_edge;
float increment = stl_in.stats.bounding_diameter / 10000.0;
int iterations = 2;
if (stl_in.stats.connected_facets_3_edge < stl_in.stats.number_of_facets) {
for (i = 0; i < iterations; i++) {
if (stl_in.stats.connected_facets_3_edge < stl_in.stats.number_of_facets) {
printf("Checking nearby. Tolerance= %f Iteration=%d of %d...", tolerance, i + 1, iterations);
stl_check_facets_nearby(&stl_in, tolerance);
printf(" Fixed %d edges.\n", stl_in.stats.edges_fixed - last_edges_fixed);
last_edges_fixed = stl_in.stats.edges_fixed;
tolerance += increment;
} else {
printf("All facets connected. No further nearby check necessary.\n");
break;
}
}
} else {
printf("All facets connected. No nearby check necessary.\n");
}
// remove_unconnected
if (stl_in.stats.connected_facets_3_edge < stl_in.stats.number_of_facets) {
printf("Removing unconnected facets...\n");
stl_remove_unconnected_facets(&stl_in);
} else
printf("No unconnected need to be removed.\n");
// fill_holes
if (stl_in.stats.connected_facets_3_edge < stl_in.stats.number_of_facets) {
printf("Filling holes...\n");
stl_fill_holes(&stl_in);
} else
printf("No holes need to be filled.\n");
// normal_directions
printf("Checking normal directions...\n");
stl_fix_normal_directions(&stl_in);
// normal_values
printf("Checking normal values...\n");
stl_fix_normal_values(&stl_in);
stl_calculate_volume(&stl_in);
RETVAL = stl_in.stats.volume;
// write STL
//stl_write_ascii(&stl_in, output_file, (char*)"Repaired by Slic3r + ADMesh");
stl_generate_shared_vertices(&stl_in);
stl_write_obj(&stl_in, output_file);
stl_close(&stl_in);
OUTPUT:
RETVAL
%}