Skip to content

Commit

Permalink
Broken logic but some progres son sparse fill zig-zag
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Kennard committed Sep 10, 2011
1 parent 5de93f3 commit 6035069
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 33 deletions.
3 changes: 3 additions & 0 deletions e3d-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ poly_sub (polygon_t * a, polygon_t * b)
void
poly_order (polygon_t * p, poly_dim_t * xp, poly_dim_t * yp)
{ // reorder contours in a polygon - first polygon stays same, but rest ordered to follow on from first if possible.
// Note that we consider c->dir==0 to be special and mean an unclosed path
if (!p)
return;
poly_contour_t *c = p->contours, *n = NULL, **np = &n, **cp;
Expand Down Expand Up @@ -105,6 +106,8 @@ poly_order (polygon_t * p, poly_dim_t * xp, poly_dim_t * yp)
bestvp = vp;
}
vp = v;
if (!c->dir)
break; // special case of no closed path - only consider first point
}
if (best == c)
bestve = vp;
Expand Down
122 changes: 100 additions & 22 deletions e3d-fill.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,35 +210,113 @@ fill (int e, stl_t * s, slice_t * a, polygon_t * p, int dir, poly_dim_t width, d
return;
polygon_t *q = poly_inset (p, width / 2);
poly_dim_t w = s->max.x - s->min.x, y, d = width * sqrtl (2.0), dy = d * 2.0, iy = dy - d;
int flag = 0;
int flag = 0, passes = 1, pass;
if (density < 1)
{ // sparse fill
dy = d * (2.0 * fillflow / density);
//iy = dy - d;
iy = dy / 2;
flag = 1;
passes = 2;
}
for (y = s->min.y - w; y < s->max.y + dy; y += dy)
{
polygon_t *n = poly_new ();
poly_dim_t oy = y + (d * dir / 4 + ((dir / 2 % 2) * dy / 2)) % dy;
if (dir & 1)
{
poly_add (n, s->min.x, oy, flag);
poly_add (n, s->min.x, oy + iy, flag);
poly_add (n, s->max.x, oy + w + iy, flag);
poly_add (n, s->max.x, oy + w, flag);
}
else
{
poly_add (n, s->max.x, oy + iy, flag);
poly_add (n, s->max.x, oy, flag);
poly_add (n, s->min.x, oy + w, flag);
poly_add (n, s->min.x, oy + w + iy, flag);
}
prefix_extrude (&a->extrude[e], poly_clip (POLY_INTERSECT, 2, n, q));
poly_free (n);
}
for (pass = 0; pass < passes; pass++)
for (y = s->min.y - w; y < s->max.y + dy; y += dy)
{
polygon_t *n = poly_new ();
poly_dim_t oy = y + (d * dir / 4 + (((dir / 2 + pass) % 2) * dy / 2)) % dy;
if (dir & 1)
{
poly_add (n, s->min.x, oy, flag);
poly_add (n, s->min.x, oy + iy, flag * 2);
poly_add (n, s->max.x, oy + w + iy, flag);
poly_add (n, s->max.x, oy + w, flag);
}
else
{
poly_add (n, s->max.x, oy, flag);
poly_add (n, s->min.x, oy + w, flag);
poly_add (n, s->min.x, oy + w + iy, flag * 2);
poly_add (n, s->max.x, oy + iy, flag);
}
polygon_t *p = poly_clip (POLY_INTERSECT, 2, n, q);
if (density < 1)
{ // clipping out parts to we make more of a zig-zag for sparse fills
poly_contour_t *c, **cp = &p->contours;
while ((c = *cp))
{
poly_vertex_t *v, *top = NULL;
for (v = c->vertices; v; v = v->next)
if (v->flag == 2)
top = v;
if (!top)
{ // This has no top line...
if (pass)
{
*cp = c->next;
poly_free_contour (c);
}
else
cp = &c->next;
continue;
}
for (v = (top->next ? : c->vertices); v != top && !v->flag; v = (v->next ? : c->vertices));
if (v != top && v->flag == 1)
{ // zap or keep this section and make an open path
poly_vertex_t *end = v;
poly_contour_t *nc = malloc (sizeof (*nc));
memset (nc, 0, sizeof (*nc));
if (pass)
{ // keep just this part of the contour, reversed...
v = (top->next ? : c->vertices);
while (1)
{
poly_vertex_t *nv = malloc (sizeof (*nv));
memset (nv, 0, sizeof (*nv));
nv->x = v->x;
nv->y = v->y;
nv->next = nc->vertices;
nc->vertices = nv;
if (v == end)
break;
v = (v->next ? : c->vertices);
}
}
else
{ // zap this part of the contour and keep the rest
poly_vertex_t **vp = &nc->vertices;
v = end;
top = (top->next ? : c->vertices);
while (1)
{
poly_vertex_t *nv = malloc (sizeof (*nv));
memset (nv, 0, sizeof (*nv));
nv->x = v->x;
nv->y = v->y;
nv->flag = v->flag;
*vp = nv;
vp = &nv->next;
if (v == top)
break;
v = (v->next ? : c->vertices);
}
}
nc->next = c->next;
*cp = nc;
poly_free_contour (c);
c = nc;
}
else if (pass)
{ // zap whole contour
*cp = c->next;
poly_free_contour (c);
continue;
}
cp = &c->next;
}
}
prefix_extrude (&a->extrude[e], p);
poly_free (n);
}
poly_free (q);
}

Expand Down
10 changes: 7 additions & 3 deletions e3d-gcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,19 @@ gcode_out (const char *filename, stl_t * stl, double flowrate, poly_dim_t layer,
move (px, py, z + hop, back);
move (v->x, v->y, z + hop, back);
}
move (v->x, v->y, z, 0);
if (d)
move (v->x, v->y, z, 0);
double flow = (c->vertices->flag ? fillflow : 1);
for (v = c->vertices->next; v; v = v->next)
{
extrude (v->x, v->y, z, speed, flowrate * flow);
flow = (v->flag ? fillflow : 1);
}
v = c->vertices;
extrude (v->x, v->y, z, speed, flowrate * flow);
if (c->dir)
{
v = c->vertices;
extrude (v->x, v->y, z, speed, flowrate * flow);
}
}
}
// layers
Expand Down
10 changes: 5 additions & 5 deletions e3d-svg.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ svg_out (const char *filename, stl_t * stl, poly_dim_t width)
{
if (!p)
return;
fprintf (f, "<path style=\"%s\" d=\"", style);
poly_contour_t *c;
for (c = p->contours; c; c = c->next)
if (c->vertices && (!dir || c->dir == dir))
{
fprintf (f, "<path style=\"%s\" d=\"", style);
poly_vertex_t *v;
char t = 'M';
for (v = c->vertices; v; v = v->next)
Expand All @@ -61,9 +61,10 @@ svg_out (const char *filename, stl_t * stl, poly_dim_t width)
fprintf (f, " %s", dimout (v->x));
fprintf (f, " %s", dimout (stl->max.y - v->y));
}
fprintf (f, " Z");
if (c->dir)
fprintf (f, " Z"); // close
fprintf (f, "\"/>\n");
}
fprintf (f, "\"/>\n");
}
char temp[1000];
outpath (s->outline, "fill:#ff8;stroke:none;fill-opacity:0.5", 0);
Expand All @@ -77,8 +78,7 @@ svg_out (const char *filename, stl_t * stl, poly_dim_t width)
{
snprintf (temp, sizeof (temp), "fill:none;stroke:#%X8f;stroke-width:%s;stroke-linecap:round;stroke-linejoin:round;stroke-opacity:0.5", e * 4,
dimout (width * 9 / 10));
outpath (s->extrude[e], temp, 1);
outpath (s->extrude[e], temp, -1);
outpath (s->extrude[e], temp, 0);
}
if (s == stl->slices)
{
Expand Down
16 changes: 13 additions & 3 deletions poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ poly_new (void)
return MALLOC (sizeof (polygon_t));
}

static void
void
poly_free_contour (poly_contour_t * c)
{
poly_vertex_t *v = c->vertices;
Expand Down Expand Up @@ -479,6 +479,7 @@ poly_clip (int operation, int count, polygon_t * poly, ...)
struct point_s
{
point_t *next;
int flag;
int dir;
int use;
poly_dim_t ax, ay, bx, by;
Expand Down Expand Up @@ -537,6 +538,7 @@ poly_clip (int operation, int count, polygon_t * poly, ...)
poly_vertex_t *v = MALLOC (sizeof (*v));
v->x = p->bx;
v->y = p->by;
A->b->flag = p->flag;
A->b->next = v;
A->b = v;
}
Expand All @@ -545,6 +547,7 @@ poly_clip (int operation, int count, polygon_t * poly, ...)
poly_vertex_t *v = MALLOC (sizeof (*v));
v->x = p->ax;
v->y = p->ay;
v->flag = p->flag;
v->next = B->a;
B->a = v;
}
Expand All @@ -556,6 +559,7 @@ poly_clip (int operation, int count, polygon_t * poly, ...)
paths = A;
v->x = p->ax;
v->y = p->ay;
v->flag = p->flag;
A->a = v;
v = MALLOC (sizeof (*v));
v->x = p->bx;
Expand Down Expand Up @@ -638,6 +642,7 @@ poly_clip (int operation, int count, polygon_t * poly, ...)
p->bx = s->bx;
p->by = s->by;
p->dir = s->dir;
p->flag = s->flag;
p->use = use;
yp = &p->next;
free (s);
Expand Down Expand Up @@ -685,7 +690,7 @@ poly_test (void)
printf ("%c%c", prefix, c->dir ? c->dir < 0 ? '-' : '+' : ':');
poly_vertex_t *v;
for (v = c->vertices; v; v = v->next)
printf (" %3d,%-3d", (int) v->x, (int) v->y);
printf (" %3d%c%-3d", (int) v->x, v->flag ? 'x' : ',', (int) v->y);
printf ("\n");
}
}
Expand All @@ -704,7 +709,7 @@ poly_test (void)
poly_free (o);
poly_free (i);
}
#define test(x) i=poly_new();{printf("Test %s\n",#x);int a;for(a=0;a<sizeof(x)/sizeof(*x);a++){poly_start(i);int*p=x[a];int b=*p++;while(b--){poly_add(i,p[0],p[1],0);p+=2;}};show(#x);}
#define test(x) i=poly_new();{printf("Test %s\n",#x);int a;for(a=0;a<sizeof(x)/sizeof(*x);a++){poly_start(i);int*p=x[a];int f=((*p<0)?1:0);int b=abs(*p++);while(b--){poly_add(i,p[0],p[1],f);p+=2;}};show(#x);}
int *overlap2[] = {
(int[]) {
6, 0, 50, 100, 100, 200, 100, 250, 50, 200, 0, 100, 0}, (int[]) {
Expand Down Expand Up @@ -779,6 +784,10 @@ poly_test (void)
(int[]) {
12, 0, 0, 0, 100, 10, 100, 10, 55, 90, 55, 90, 100, 100, 100, 100, 0, 90, 0, 90, 45, 10, 45, 10, 0}
};
int *x[] = {
(int[]) {-4, 0, 10, 0, 90, 100, 90, 100, 10},
(int[]) {4, 10, 0, 10, 100, 90, 100, 90, 0},
};
test (boxc);
test (boxa);
test (box2);
Expand All @@ -794,4 +803,5 @@ poly_test (void)
test (figure8);
test (cshape2);
test (h);
test (x);
}
1 change: 1 addition & 0 deletions poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct poly_vertex_s
// General functions
polygon_t *poly_new (void); // New empty malloced polygon
void poly_free (polygon_t *); // Free malloced polygon, contours and vertices
void poly_free_contour (poly_contour_t * c); // free a contour
void poly_start (polygon_t *); // Start of new contour
void poly_add (polygon_t *, poly_dim_t x, poly_dim_t y, int flag); // Add point to end of new contour (adds new contour at start of contours if needed)

Expand Down

0 comments on commit 6035069

Please sign in to comment.