move refframe into stl_3d

This commit is contained in:
Trammell Hudson 2015-02-15 14:55:43 -05:00
parent 2c416c81d8
commit 5b69edf5c4
4 changed files with 113 additions and 41 deletions

View File

@ -25,6 +25,8 @@ main(void)
// for each vertex, find the coplanar triangles
// \todo: do coplanar bits
const stl_vertex_t ** const vertex_list = calloc(sizeof(**vertex_list), stl->num_vertex);
for(int i = 0 ; i < stl->num_vertex ; i++)
{
const stl_vertex_t * const v = &stl->vertex[i];
@ -37,10 +39,43 @@ main(void)
"sphere(r=20);\n",
i, origin.p[0], origin.p[1], origin.p[2]);
int * const face_used = calloc(sizeof(*face_used), stl->num_face);
for (int j = 0 ; j < v->num_face; j++)
{
// generate the polygon face for this vertex
const stl_face_t * const f = v->face[j];
if (face_used[f - stl->face])
continue;
const int vertex_count = stl_trace_face(
stl,
f,
vertex_list,
face_used
);
refframe_t ref;
refframe_init(&ref,
f->vertex[(v->face_num[j]+0) % 3]->p,
f->vertex[(v->face_num[j]+1) % 3]->p,
f->vertex[(v->face_num[j]+2) % 3]->p
);
printf("linear_extrude(height=%f) polygon(points=[\n",
thickness
);
for(int k=0 ; k < vertex_count ; k++)
{
double x, y;
v3_project(&ref, vertex_list[k]->p, &x, &y);
printf("[%f,%f],", x, y);
}
printf("\n]);\n");
// generate a polyhedron that spans
// the width of this coplanar thingy
#if 0
v3_t v0 = v3_sub(f->vertex[0]->p, origin);
v3_t v1 = v3_sub(f->vertex[1]->p, origin);
v3_t v2 = v3_sub(f->vertex[2]->p, origin);
@ -97,10 +132,13 @@ main(void)
"faces"
#endif
);
#endif
//break; // only do one right now
}
free(face_used);
printf("}\n");
if (i == 1) break; // only do one right now
}

47
faces.c
View File

@ -16,36 +16,6 @@
static const char * stroke_string
= "stroke-width=\"0.1px\" fill=\"none\"";
typedef struct
{
v3_t origin;
v3_t x;
v3_t y;
v3_t z;
} refframe_t;
static void
v3_project(
const refframe_t * const ref,
const v3_t p_in,
double * const x_out,
double * const y_out
)
{
v3_t p = v3_sub(p_in, ref->origin);
double x = ref->x.p[0]*p.p[0] + ref->x.p[1]*p.p[1] + ref->x.p[2]*p.p[2];
double y = ref->y.p[0]*p.p[0] + ref->y.p[1]*p.p[1] + ref->y.p[2]*p.p[2];
double z = ref->z.p[0]*p.p[0] + ref->z.p[1]*p.p[1] + ref->z.p[2]*p.p[2];
fprintf(stderr, "%f,%f,%f\n", x, y, z);
*x_out = x;
*y_out = y;
}
static void
svg_line(
const refframe_t * const ref,
@ -256,17 +226,12 @@ main(void)
fprintf(stderr, "%d: %d vertices\n", i, vertex_count);
// generate a refernce frame based on this face
refframe_t ref = {
.origin = f->vertex[0]->p,
};
const v3_t dx = v3_norm(v3_sub(f->vertex[1]->p, ref.origin));
const v3_t dy = v3_norm(v3_sub(f->vertex[2]->p, ref.origin));
ref.x = dx;
ref.z = v3_norm(v3_cross(dx, dy));
ref.y = v3_norm(v3_cross(ref.x, ref.z));
refframe_t ref;
refframe_init(&ref,
f->vertex[0]->p,
f->vertex[1]->p,
f->vertex[2]->p
);
printf("<!-- face %d --><g>\n", i);
// generate the polygon outline (should be one path?)

View File

@ -293,3 +293,44 @@ stl_trace_face(
return vertex_count;
}
void
refframe_init(
refframe_t * ref,
const v3_t p0,
const v3_t p1,
const v3_t p2
)
{
ref->origin = p0;
const v3_t dx = v3_norm(v3_sub(p1, ref->origin));
const v3_t dy = v3_norm(v3_sub(p2, ref->origin));
ref->x = dx;
ref->z = v3_norm(v3_cross(dx, dy));
ref->y = v3_norm(v3_cross(ref->x, ref->z));
}
void
v3_project(
const refframe_t * const ref,
const v3_t p_in,
double * const x_out,
double * const y_out
)
{
v3_t p = v3_sub(p_in, ref->origin);
double x = ref->x.p[0]*p.p[0] + ref->x.p[1]*p.p[1] + ref->x.p[2]*p.p[2];
double y = ref->y.p[0]*p.p[0] + ref->y.p[1]*p.p[1] + ref->y.p[2]*p.p[2];
double z = ref->z.p[0]*p.p[0] + ref->z.p[1]*p.p[1] + ref->z.p[2]*p.p[2];
fprintf(stderr, "%f,%f,%f\n", x, y, z);
*x_out = x;
*y_out = y;
}

View File

@ -63,4 +63,32 @@ stl_trace_face(
);
typedef struct
{
v3_t origin;
v3_t x;
v3_t y;
v3_t z;
} refframe_t;
void
refframe_init(
refframe_t * ref,
const v3_t p0,
const v3_t p1,
const v3_t p2
);
/** Project a 3D point onto a 2D space */
void
v3_project(
const refframe_t * const ref,
const v3_t p_in,
double * const x_out,
double * const y_out
);
#endif