From 2c416c81d80a1b2a1340b10d11889439a76b1247 Mon Sep 17 00:00:00 2001 From: Trammell Hudson Date: Sun, 15 Feb 2015 14:35:42 -0500 Subject: [PATCH] move face tracing into stl_3d.c --- faces.c | 68 +++++++------------------------------------------------- stl_3d.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ stl_3d.h | 20 +++++++++++++++++ 3 files changed, 88 insertions(+), 60 deletions(-) diff --git a/faces.c b/faces.c index 7d66922..f44f7eb 100644 --- a/faces.c +++ b/faces.c @@ -14,7 +14,7 @@ #include "stl_3d.h" static const char * stroke_string - = "stroke-width=\"1px\" fill=\"none\""; + = "stroke-width=\"0.1px\" fill=\"none\""; typedef struct { @@ -87,63 +87,6 @@ svg_circle( } -/** Starting at a point, trace the coplanar polygon and return a - * list of vertices. - */ -int -trace_face( - const stl_3d_t * const stl, - const stl_face_t * const f_start, - const stl_vertex_t ** vertex_list, - int * const face_used -) -{ - const stl_face_t * f = f_start; - int i = 0; - int vertex_count = 0; - - do { - const stl_vertex_t * const v1 = f->vertex[(i+0) % 3]; - const stl_vertex_t * const v2 = f->vertex[(i+1) % 3]; - const stl_face_t * const f_next = f->face[i]; - - fprintf(stderr, "%p %d: %f,%f,%f\n", f, i, v1->p.p[0], v1->p.p[1], v1->p.p[2]); - face_used[f - stl->face] = 1; - - if (!f_next || f->angle[i] != 0) - { - // not coplanar or no connection. - // add the NEXT vertex on this face and continue - vertex_list[vertex_count++] = v2; - i = (i+1) % 3; - continue; - } - - // coplanar; figure out which vertex on the next - // face we start at - int i_next = -1; - for (int j = 0 ; j < 3 ; j++) - { - if (f_next->vertex[j] != v1) - continue; - i_next = j; - break; - } - - if (i_next == -1) - abort(); - - // move to the new face - f = f_next; - i = i_next; - - // keep going until we reach our starting face - // at vertex 0. - } while (f != f_start || i != 0); - - return vertex_count; -} - // Determines the intersection point of the line defined by points A and B with the // line defined by points C and D. // @@ -286,7 +229,7 @@ main(void) if (!stl) return EXIT_FAILURE; const double inset_distance = 8; - const double hole_radius = 3; + const double hole_radius = 1.5; int * const face_used = calloc(sizeof(*face_used), stl->num_face); @@ -303,7 +246,12 @@ main(void) continue; const stl_face_t * const f = &stl->face[i]; - const int vertex_count = trace_face(stl, f, vertex_list, face_used); + const int vertex_count = stl_trace_face( + stl, + f, + vertex_list, + face_used + ); fprintf(stderr, "%d: %d vertices\n", i, vertex_count); diff --git a/stl_3d.c b/stl_3d.c index 0e05cf6..4eefb3a 100644 --- a/stl_3d.c +++ b/stl_3d.c @@ -233,3 +233,63 @@ stl_3d_parse( return stl; } + + +/** Starting at a point, trace the coplanar polygon and return a + * list of vertices. + */ +int +stl_trace_face( + const stl_3d_t * const stl, + const stl_face_t * const f_start, + const stl_vertex_t ** vertex_list, + int * const face_used +) +{ + const stl_face_t * f = f_start; + int i = 0; + int vertex_count = 0; + + do { + const stl_vertex_t * const v1 = f->vertex[(i+0) % 3]; + const stl_vertex_t * const v2 = f->vertex[(i+1) % 3]; + const stl_face_t * const f_next = f->face[i]; + + fprintf(stderr, "%p %d: %f,%f,%f\n", f, i, v1->p.p[0], v1->p.p[1], v1->p.p[2]); + if (face_used) + face_used[f - stl->face] = 1; + + if (!f_next || f->angle[i] != 0) + { + // not coplanar or no connection. + // add the NEXT vertex on this face and continue + vertex_list[vertex_count++] = v2; + i = (i+1) % 3; + continue; + } + + // coplanar; figure out which vertex on the next + // face we start at + int i_next = -1; + for (int j = 0 ; j < 3 ; j++) + { + if (f_next->vertex[j] != v1) + continue; + i_next = j; + break; + } + + if (i_next == -1) + abort(); + + // move to the new face + f = f_next; + i = i_next; + + // keep going until we reach our starting face + // at vertex 0. + } while (f != f_start || i != 0); + + return vertex_count; +} + diff --git a/stl_3d.h b/stl_3d.h index a6c86aa..2355cf7 100644 --- a/stl_3d.h +++ b/stl_3d.h @@ -43,4 +43,24 @@ stl_3d_parse( int fd ); + +/** Generate the list of vertices that are coplanar given a starting + * vertex in the stl file. + * + * vertex_list should have enough space to contain at least as many + * vertices as in the stl file. + * + * if face_used is not null it will be populated with which faces + * have been traversed during the search. it should have enough size + * to contain all of the faces in the file. + */ +int +stl_trace_face( + const stl_3d_t * const stl, + const stl_face_t * const f_start, + const stl_vertex_t ** vertex_list, + int * const face_used +); + + #endif