getoptified

This commit is contained in:
Trammell Hudson 2015-05-04 21:10:38 -04:00
parent 70caae1411
commit 4237a942ce

151
faces.c
View File

@ -10,11 +10,19 @@
#include <math.h> #include <math.h>
#include <err.h> #include <err.h>
#include <assert.h> #include <assert.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "v3.h" #include "v3.h"
#include "stl_3d.h" #include "stl_3d.h"
static FILE * output;
static int verbose;
static const char * stroke_string static const char * stroke_string
= "stroke-width=\"0.1px\" fill=\"none\""; = "stroke-width=\"1px\" fill=\"none\"";
static void static void
svg_line( svg_line(
@ -30,7 +38,7 @@ svg_line(
v3_project(ref, p2, &x2, &y2); v3_project(ref, p2, &x2, &y2);
const char * color = "#FF0000"; const char * color = "#FF0000";
printf("<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke=\"%s\" %s/>\n", fprintf(output, "<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke=\"%s\" %s/>\n",
x1, y1, x1, y1,
x2, y2, x2, y2,
color, color,
@ -47,7 +55,7 @@ svg_circle(
const char * const color const char * const color
) )
{ {
printf("<circle cx=\"%f\" cy=\"%f\" r=\"%f\" stroke=\"%s\" %s/>\n", fprintf(output, "<circle cx=\"%f\" cy=\"%f\" r=\"%f\" stroke=\"%s\" %s/>\n",
x, x,
y, y,
rad, rad,
@ -57,23 +65,128 @@ svg_circle(
} }
int static struct option long_options[] =
main(void)
{ {
stl_3d_t * const stl = stl_3d_parse(STDIN_FILENO); { "verbose", no_argument, 0, 'v' },
if (!stl) { "inset", required_argument, 0, 'i' },
return EXIT_FAILURE; { "radius", required_argument, 0, 'r' },
const double inset_distance = 5; { "input", required_argument, 0, 'I' },
const double hole_radius = 1.15; { "output", required_argument, 0, 'O' },
{ 0, 0, 0, 0 },
};
int * const face_used = calloc(sizeof(*face_used), stl->num_face);
static void
usage(
FILE * const out
)
{
fprintf(out,
"Usage: faces [options] < stl-binary.stl > faces.svg\n"
"Options:\n"
" -v | --verbose Enable verbosity\n"
" -i | --inset N Inset mm\n"
" -r | --radius N Hole radius mm\n"
" -I | --input file Read binary STL from file\n"
" -O | --output file Write SVG to file\n"
"\n"
);
}
int
main(
int argc,
char ** argv
)
{
double inset_distance = 5;
double hole_radius = 1.15;
const char * input_file = NULL;
const char * output_file = NULL;
int option_index = 0;
while (1)
{
const int c = getopt_long(
argc,
argv,
"vI:r:i:O:",
long_options,
&option_index
);
if (c == -1)
break;
switch(c)
{
case 'v': verbose++; break;
case 'i': inset_distance = atof(optarg); break;
case 'r': hole_radius = atof(optarg); break;
case 'I': input_file = optarg; break;
case 'O': output_file = optarg; break;
case 'h': case '?':
usage(stdout);
return 0;
default:
usage(stderr);
return -1;
}
}
int input_fd;
if (!input_file)
{
input_fd= STDIN_FILENO;
input_file = "stdin";
} else {
input_fd = open(input_file, O_RDONLY);
if (input_fd < 0)
{
perror(input_file);
return -1;
}
}
if (!output_file)
{
output_file = "stdout";
output = stdout;
} else {
output = fopen(output_file, "w");
if (!output)
{
perror(output_file);
return -1;
}
}
stl_3d_t * const stl = stl_3d_parse(input_fd);
if (!stl)
{
fprintf(stderr, "%s: Unable to parse STL\n", input_file);
return EXIT_FAILURE;
}
close(input_fd);
if (verbose)
fprintf(stderr,
"%s: %d faces, %d vertex\n",
input_file,
stl->num_face,
stl->num_vertex
);
int * const face_used
= calloc(sizeof(*face_used), stl->num_face);
// for each vertex, find the coplanar triangles // for each vertex, find the coplanar triangles
// \todo: do coplanar bits // \todo: do coplanar bits
printf("<svg xmlns=\"http://www.w3.org/2000/svg\">\n"); fprintf(output, "<svg xmlns=\"http://www.w3.org/2000/svg\">\n");
printf("<g transform=\"scale(3.543307)\"><!-- scale to mm -->\n"); fprintf(output, "<g transform=\"scale(3.543307)\"><!-- scale to mm -->\n");
const stl_vertex_t ** const vertex_list = calloc(sizeof(*vertex_list), stl->num_vertex); const stl_vertex_t ** const vertex_list
= calloc(sizeof(*vertex_list), stl->num_vertex);
for(int i = 0 ; i < stl->num_face ; i++) for(int i = 0 ; i < stl->num_face ; i++)
{ {
@ -89,7 +202,7 @@ main(void)
0 0
); );
fprintf(stderr, "%d: %d vertices\n", i, vertex_count); fprintf(stderr, "%s: %d: %d vertices\n", input_file, i, vertex_count);
// generate a refernce frame based on this face // generate a refernce frame based on this face
refframe_t ref; refframe_t ref;
@ -98,7 +211,7 @@ main(void)
f->vertex[1]->p, f->vertex[1]->p,
f->vertex[2]->p f->vertex[2]->p
); );
printf("<!-- face %d --><g>\n", i); fprintf(output, "<!-- face %d --><g>\n", i);
// generate the polygon outline (should be one path?) // generate the polygon outline (should be one path?)
for (int j = 0 ; j < vertex_count ; j++) for (int j = 0 ; j < vertex_count ; j++)
@ -120,10 +233,12 @@ main(void)
svg_circle(x, y, hole_radius, "#00ff00"); svg_circle(x, y, hole_radius, "#00ff00");
} }
printf("</g>\n"); fprintf(output, "</g>\n");
} }
printf("</g></svg>\n"); fprintf(output, "</g></svg>\n");
fclose(output);
return 0; return 0;
} }