wireframe coplanar fixed is almost perfect; test3 fails

This commit is contained in:
Trammell Hudson 2015-01-25 16:37:04 -05:00
parent 7dfd2d4c4e
commit d10a21ac3b

View File

@ -48,72 +48,8 @@ struct stl_vertex
}; };
typedef struct face face_t;
typedef struct poly poly_t;
struct face /* Returns 1 for ever edge in f1 that is shared with f2.
{
float sides[3];
face_t * next[3];
int next_edge[3];
int coplanar[3];
int used[3];
};
// once this triangle has been used, it will be placed
// in a polygon group and fixed in a position relative to that group
struct poly
{
int start_edge;
int printed;
// local coordinates of the triangle vertices
float a;
float x2;
float y2;
float rot;
// absolute coordintes of the triangle vertices
float p[3][2];
// todo: make this const and add backtracking
face_t * face;
poly_t * next[3];
poly_t * work_next;
};
/* Compare two edges in two triangles.
*
* note that if the windings are all the same, the edges will
* compare in the opposite order (for example, the edge from 0 to 1
* compares to the edge from 2 to 1 in the other triangle).
*/
static int
edge_eq2(
const stl_face_t * const t0,
const stl_face_t * const t1,
int e0,
int e1
)
{
const v3_t * const v00 = &t0->p[e0];
const v3_t * const v01 = &t0->p[(e0+1) % 3];
const v3_t * const v10 = &t1->p[e1];
const v3_t * const v11 = &t1->p[(e1+1) % 3];
if (v3_eq(v00, v11) && v3_eq(v01, v10))
return 1;
return 0;
}
/* Returns the 0 for coplanar, negative for mountain, positive for valley.
* (approximates the angle between two triangles that share one edge).
*/ */
int int
coplanar_check( coplanar_check(
@ -121,6 +57,28 @@ coplanar_check(
const stl_face_t * const f2 const stl_face_t * const f2
) )
{ {
// Verify that there are three matching points
int match[3] = {0,0,0};
for (int i = 0 ; i < 3 ; i++)
{
for (int j = 0 ; j < 3 ; j++)
if (v3_eq(&f1->p[i], &f2->p[j]))
match[i] = 1;
}
uint8_t mask = 0;
if (match[0] && match[1])
mask = 1;
if (match[1] && match[2])
mask = 2;
if (match[2] && match[0])
mask = 4;
// otherwise they do not share enough points
if (mask == 0)
return 0;
// find the four distinct points // find the four distinct points
v3_t x1 = f1->p[0]; v3_t x1 = f1->p[0];
v3_t x2 = f1->p[1]; v3_t x2 = f1->p[1];
@ -147,87 +105,17 @@ coplanar_check(
float dot = v3_dot(dx31, cross); float dot = v3_dot(dx31, cross);
int check = -EPS < dot && dot < +EPS; int check = -EPS < dot && dot < +EPS;
if (debug) fprintf( stderr, "%p %p %s: %f\n", f1, f2, check ? "yes" : "no", dot);
return (int) dot; // if the dot product is not close enough to zero, they
// are not coplanar.
if (!check)
return 0;
// coplanar! return the shared edge mask
return mask;
} }
/** Translate a list of STL triangles into a connected graph of faces.
*
* If there are any triangles that do not have three connected edges,
* the first error will be reported and NULL will be returned.
*/
face_t *
stl2faces(
const stl_face_t * const stl_faces,
const int num_triangles
)
{
face_t * const faces = calloc(num_triangles, sizeof(*faces));
// convert the stl triangles into faces
for (int i = 0 ; i < num_triangles ; i++)
{
const stl_face_t * const stl = &stl_faces[i];
face_t * const f = &faces[i];
f->sides[0] = v3_len(&stl->p[0], &stl->p[1]);
f->sides[1] = v3_len(&stl->p[1], &stl->p[2]);
f->sides[2] = v3_len(&stl->p[2], &stl->p[0]);
if (debug) fprintf(stderr, "%p %f %f %f\n",
f, f->sides[0], f->sides[1], f->sides[2]);
}
// look to see if there is a matching point
// in the faces that we've already built
for (int i = 0 ; i < num_triangles ; i++)
{
const stl_face_t * const stl = &stl_faces[i];
face_t * const f = &faces[i];
for (int j = 0 ; j < num_triangles ; j++)
{
if (i == j)
continue;
const stl_face_t * const stl2 = &stl_faces[j];
face_t * const f2 = &faces[j];
for (int edge = 0 ; edge < 3 ; edge++)
{
if (f->next[edge])
continue;
for (int edge2 = 0 ; edge2 < 3 ; edge2++)
{
if (f2->next[edge2])
continue;
if (!edge_eq2(stl, stl2, edge, edge2))
continue;
f->next[edge] = f2;
f->next_edge[edge] = edge2;
f2->next[edge2] = f;
f2->next_edge[edge2] = edge;
f->coplanar[edge] =
f2->coplanar[edge2] = coplanar_check(stl, stl2);
}
}
}
// all three edges should be matched
if (f->next[0] && f->next[1] && f->next[2])
continue;
fprintf(stderr, "%d missing edges?\n", i);
free(faces);
return NULL;
}
return faces;
}
static inline float static inline float
sign( sign(
const float x const float x
@ -241,6 +129,33 @@ sign(
} }
/**
* Add a vector to the list of edges if it is not already present
* and if it is not coplanar with other ones.
* Note that if it is coplanar, but "outside" the other edges then it
* will replace the inside one.
*/
void
stl_edge_insert(
stl_vertex_t * const v1,
stl_vertex_t * const v2
)
{
for (int i = 0 ; i < v1->num_edges ; i++)
{
const v3_t * const p0 = &v1->p;
// if v2 already exists in the edges, discard it
if (v1->edges[i] == v2)
return;
}
// if we reach this point, we need to insert the edge
v1->edges[v1->num_edges++] = v2;
}
stl_vertex_t * stl_vertex_t *
stl_vertex_find( stl_vertex_find(
stl_vertex_t ** const vertices, stl_vertex_t ** const vertices,
@ -272,6 +187,7 @@ stl_vertex_find(
} }
int main(void) int main(void)
{ {
const size_t max_len = 1 << 20; const size_t max_len = 1 << 20;
@ -306,13 +222,35 @@ int main(void)
vp[j] = stl_vertex_find(vertices, &num_vertex, p); vp[j] = stl_vertex_find(vertices, &num_vertex, p);
} }
// walk all of other triangles to figure out if
// any of the triangles are coplanar and have shared
// edges.
uint8_t coplanar_mask = 0;
for (int j = 0 ; j < num_triangles ; j++)
{
if (j == i)
continue;
coplanar_mask |= coplanar_check(
&stl_faces[i], &stl_faces[j]);
}
// all three vertices are mapped; generate the // all three vertices are mapped; generate the
// connections // connections
for (int j = 0 ; j < 3 ; j++) for (int j = 0 ; j < 3 ; j++)
{ {
stl_vertex_t * const v = vp[j]; stl_vertex_t * const v = vp[j];
stl_vertex_find(v->edges, &v->num_edges, &vp[(j+1) % 3]->p);
stl_vertex_find(v->edges, &v->num_edges, &vp[(j+2) % 3]->p); // if the edge from j to j+1 is not coplanar,
// add it to the list
if ((coplanar_mask & (1 << j)) == 0)
stl_edge_insert(v, vp[(j+1) % 3]);
// if the edge from j+2 to j is not coplanar
const uint8_t j2 = (j + 2) % 3;
if ((coplanar_mask & (1 << j2)) == 0)
stl_edge_insert(v, vp[j2]);
} }
} }
@ -340,7 +278,7 @@ int main(void)
printf("%%rotate([0,%f,%f]) cylinder(r=1, h=%f); // %p\n", printf("%%rotate([0,%f,%f]) cylinder(r=1, h=%f); // %p\n",
b, b,
c, c,
len/3, len*.45,
v2 v2
); );
} }
@ -348,71 +286,5 @@ int main(void)
printf("}\n"); printf("}\n");
} }
#if 0
face_t * const faces = stl2faces(stl_faces, num_triangles);
for (int i = 0 ; i < num_triangles ; i++)
{
//if (i > 20) break;
const stl_face_t * const raw = &stl_faces[i];
face_t * const f = &faces[i];
for (int j = 0 ; j < 3 ; j++)
{
// if this edge is coplanar with the other
// triangle, do not output it.
if (f->coplanar[j] == 0)
continue;
// if we have already transited this edge
if (f->used[j])
continue;
// flag that we have used this vertex on the adjacent
f->used[j] = 1;
const v3_t * const p1 = &raw->p[(j+0) % 3];
const v3_t * const p2 = &raw->p[(j+1) % 3];
const float len = v3_len(p1,p2);
const v3_t d = v3_sub(*p2, *p1);
if (len == 0)
continue;
printf("translate([%f,%f,%f]) sphere(r=%f);\n",
p1->p[0], p1->p[1], p1->p[2],
4*thick
);
const float b = acos(d.p[2] / len) * 180/M_PI;
const float c = d.p[0] == 0 ? sign(d.p[1]) * 90 : atan2(d.p[1], d.p[0]) * 180/M_PI;
//
// generate a cube that goes from
// p1 to p2.
printf("%%translate([%f,%f,%f]) rotate([%f,%f,%f]) ",
p1->p[0], p1->p[1], p1->p[2],
0.0, b, c
);
if (do_square)
{
printf("translate([0,0,%f]) cube([%f,%f,%f], center=true);\n",
len/2,
thick,
thick,
len
);
} else {
printf("cylinder(r=%f, h=%f);\n",
thick,
len
);
}
}
}
#endif
return 0; return 0;
} }