ptrude/: changed extrusion from xy path to xz path; variable number of faces

This commit is contained in:
Werner Almesberger 2011-07-31 09:57:51 -03:00
parent 7ef626988c
commit ee634458dc
3 changed files with 83 additions and 74 deletions

View File

@ -20,104 +20,85 @@
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "path.h"
#include "extrude.h"
#define alloc_tn(t, n) ((t *) malloc(sizeof(t)*(n)))
static void slice(double x, double y, double nx, double ny,
const struct path *shape, struct point *p)
static void cvt_3d(struct point *p, const struct vertex *v, double z)
{
const struct vertex *v;
for (v = shape->vertices; v; v = v->next) {
p->x = x+nx*v->x;
p->y = y+ny*v->x;
p->z = v->y;
p++;
}
p->x = v->x;
p->y = v->y;
p->z = z;
}
static void mesh(const struct point *a, const struct point *b, int n,
static void mesh(const struct path *pa, double za,
const struct path *pb, double zb,
void (*face)(void *data, struct point a, struct point b, struct point c),
void *data)
{
int i;
const struct vertex *a = pa->vertices;
const struct vertex *b = pb->vertices;
double sa = 0, sb = 0; /* total distance traveled on path */
double da, db;
struct point a3, b3, n;
for (i = 0; i != n-1; i++) {
face(data, a[i], b[i], a[i+1]);
face(data, a[i+1], b[i], b[i+1]);
cvt_3d(&a3, a, za);
cvt_3d(&b3, b, zb);
while (a->next || b->next) {
da = fabs(sa+a->len-sb);
db = fabs(sb+b->len-sa);
if ((a->next && da < db) || !b->next) {
cvt_3d(&n, a->next, za);
face(data, a3, n, b3);
a3 = n;
sa += a->len;
a = a->next;
} else {
cvt_3d(&n, b->next, zb);
face(data, b3, a3, n);
b3 = n;
sb += b->len;
b = b->next;
}
}
}
void extrude(const struct path *path, const struct path *shape,
double r, double d,
void (*face)(void *data, struct point a, struct point b, struct point c),
void *data)
{
const struct vertex *v; /* current vertex (for iteration) */
const struct vertex *a, *b, *c; /* previous, current, next vertex */
struct point *prev, *this; /* previous and current slice */
double nx, ny; /* 2D normals */
double f; /* factor for normalization */
double tx, ty; /* temporary 2D normals */
int n = 0; /* number of vertices in shape */
const struct vertex *v, *next; /* current and next vertex */
struct path *prev, *curr; /* previous and current path */
struct path *tmp;
if (!path->vertices || !path->vertices->next || !shape->vertices)
if (!path->vertices || !path->vertices->next)
return;
for (v = shape->vertices; v; v = v->next)
n++;
v = shape->vertices;
if (!v || !v->next)
return;
prev = alloc_tn(struct point, n);
this = alloc_tn(struct point, n);
tmp = stretch_path(path, v->x);
prev = round_path(tmp, r, d);
free_path(tmp);
a = path->vertices;
b = a->next;
nx = b->y-a->y;
ny = a->x-b->x;
f = hypot(nx, ny);
slice(a->x, a->y, nx/f, ny/f, shape, prev);
while (v->next) {
next = v->next;
tmp = stretch_path(path, next->x);
curr = round_path(tmp, r, d);
free_path(tmp);
for (v = path->vertices->next; v->next; v = v->next) {
b = v;
c = v->next;
mesh(prev, v->y, curr, next->y, face, data);
tx = b->y-a->y;
ty = a->x-b->x;
f = hypot(tx, ty);
nx = tx/f;
ny = ty/f;
tx = c->y-b->y;
ty = b->x-c->x;
f = hypot(tx, ty);
nx += tx/f;
ny += ty/f;
slice(b->x, b->y, nx/2, ny/2, shape, this);
mesh(prev, this, n, face, data);
memcpy(prev, this, sizeof(*prev)*n);
a = v;
free_path(prev);
prev = curr;
v = next;
}
nx = v->y-a->y;
ny = a->x-v->x;
f = hypot(nx, ny);
slice(v->x, v->y, nx/f, ny/f, shape, this);
mesh(prev, this, n, face, data);
/* @@@ To do: close shape and close top and bottom */
free(prev);
free(this);
free_path(prev);
}

26
ptrude/extrude.h Normal file
View File

@ -0,0 +1,26 @@
/*
* extrude.h - Perform the extrusion
*
* Written 2011 by Werner Almesberger
* Copyright 2011 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef EXTRUDE_H
#define EXTRUDE_H
struct point {
double x, y, z;
};
void extrude(const struct path *path, const struct path *shape,
double r, double d,
void (*face)(void *data, struct point a, struct point b, struct point c),
void *data);
#endif /* !EXTRUDE_H */

View File

@ -64,12 +64,13 @@ static void do_face(void *data, struct point a, struct point b, struct point c)
}
static void do_extrude(const struct path *path, const struct path *shape)
static void do_extrude(const struct path *path, const struct path *shape,
double r, double d)
{
FILE *file = stdout;
fprintf(file, "solid ptrude\n");
extrude(path, shape, do_face, file);
extrude(path, shape, r, d, do_face, file);
fprintf(file, "endsolid ptrude\n");
}
@ -87,7 +88,8 @@ static void usage(const char *name)
int main(int argc, char **argv)
{
FILE *file;
const struct path *path = NULL, *shape;
struct path *path = NULL;
const struct path *shape;
double r, d;
char *end;
int c;
@ -128,9 +130,9 @@ int main(int argc, char **argv)
/*
* We split the error budget evenly between path and shape.
*/
path = round_path(path, r, sqrt(d));
path_set_length(path);
shape = round_path(shape, r, sqrt(d));
do_extrude(path, shape);
do_extrude(path, shape, r, sqrt(d));
} else {
shape = round_path(shape, r, d);
save_path(stdout, shape);