1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-23 09:17:42 +02:00

poly2d/: add foreach_v2d* and foreach_edges looping constructs

This commit is contained in:
Werner Almesberger 2012-05-08 13:14:00 -03:00
parent 1c11f4d201
commit 3ee5b1aa31
4 changed files with 152 additions and 2 deletions

View File

@ -23,7 +23,7 @@ OBJS = v2d_intersect.o v2d_line_distance.o \
CFLAGS_WARN = -Wall -Wshadow -Wmissing-prototypes \ CFLAGS_WARN = -Wall -Wshadow -Wmissing-prototypes \
-Wmissing-declarations -Wno-format-zero-length -Wmissing-declarations -Wno-format-zero-length
CFLAGS = $(CFLAGS_WARN) -g CFLAGS = $(CFLAGS_WARN) -g -std=gnu99
CXXFLAGS = -Wall -frounding-math CXXFLAGS = -Wall -frounding-math
LDFLAGS = LDFLAGS =
LDLIBS = -lm LDLIBS = -lm

View File

@ -30,6 +30,34 @@ struct p2d {
}; };
/*
* forall_v2d* need -std=gnu99
*/
#define forall_v2d_once(var, first) \
for (const struct v2d *__fv2do_0 = (var = (first)); \
var; \
var = var->next == __fv2do_0 ? NULL : var->next)
#define forall_v2d(var, first) \
for (const struct v2d *__fv2d_0 = (var = (first)); \
var; \
({ \
var = __fv2d_0 ? var->next : NULL; \
if (var == __fv2d_0) \
__fv2d_0 = NULL; \
}))
#define forall_edges(a, b, first) \
for (const struct v2d *__fe_0 = \
(a = (first), b = a ? a->next : a, a); \
a && b; \
({ \
a = b; \
b = b == __fe_0 ? NULL : b->next; \
}))
/* /*
* Polygon creation * Polygon creation
*/ */

View File

@ -14,6 +14,7 @@
compile_and_run() compile_and_run()
{ {
CFLAGS="-Wall -Werror -g -std=gnu99 -I.."
LIBS="-lpoly2d -lCGAL -lCGAL_Core -lboost_thread" LIBS="-lpoly2d -lCGAL -lCGAL_Core -lboost_thread"
LIBS="$LIBS -lstdc++ -lmpfr -lgmp -lm" LIBS="$LIBS -lstdc++ -lmpfr -lgmp -lm"
@ -54,7 +55,7 @@ int main(void)
return 0; return 0;
} }
EOF EOF
gcc -Wall -Werror -g -I.. _.c -L.. $LIBS || return gcc $CFLAGS _.c -L.. $LIBS || return
$VALGRIND ./a.out $VALGRIND ./a.out
} }

121
poly2d/test/forall Executable file
View File

@ -0,0 +1,121 @@
#!/bin/sh
. ./Common
###############################################################################
tst "forall_v2d_once, open polygon" <<EOF
struct p2d *p = p2d_new();
const struct v2d *v;
p2d_append(p, v2d_new(0, 0));
p2d_append(p, v2d_new(1, 0));
p2d_append(p, v2d_new(2, 3));
forall_v2d_once(v, p->v)
printf("%g %g\n", v->x, v->y);
EOF
expect <<EOF
0 0
1 0
2 3
EOF
#------------------------------------------------------------------------------
tst "forall_v2d_once, closed polygon" <<EOF
struct p2d *p = p2d_new();
const struct v2d *v;
p2d_append(p, v2d_new(0, 0));
p2d_append(p, v2d_new(1, 0));
p2d_append(p, v2d_new(2, 3));
p2d_close(p);
forall_v2d_once(v, p->v)
printf("%g %g\n", v->x, v->y);
EOF
expect <<EOF
0 0
1 0
2 3
EOF
#------------------------------------------------------------------------------
tst "forall_v2d, open polygon" <<EOF
struct p2d *p = p2d_new();
const struct v2d *v;
p2d_append(p, v2d_new(0, 0));
p2d_append(p, v2d_new(1, 0));
p2d_append(p, v2d_new(2, 3));
forall_v2d(v, p->v)
printf("%g %g\n", v->x, v->y);
EOF
expect <<EOF
0 0
1 0
2 3
EOF
#------------------------------------------------------------------------------
tst "forall_v2d, closed polygon" <<EOF
struct p2d *p = p2d_new();
const struct v2d *v;
p2d_append(p, v2d_new(0, 0));
p2d_append(p, v2d_new(1, 0));
p2d_append(p, v2d_new(2, 3));
p2d_close(p);
forall_v2d(v, p->v)
printf("%g %g\n", v->x, v->y);
EOF
expect <<EOF
0 0
1 0
2 3
0 0
EOF
#------------------------------------------------------------------------------
tst "forall_edges, open polygon" <<EOF
struct p2d *p = p2d_new();
const struct v2d *a, *b;
p2d_append(p, v2d_new(0, 0));
p2d_append(p, v2d_new(1, 0));
p2d_append(p, v2d_new(2, 3));
forall_edges(a, b, p->v)
printf("%g %g %g %g\n", a->x, a->y, b->x, b->y);
EOF
expect <<EOF
0 0 1 0
1 0 2 3
EOF
#------------------------------------------------------------------------------
tst "forall_edges, closed polygon" <<EOF
struct p2d *p = p2d_new();
const struct v2d *a, *b;
p2d_append(p, v2d_new(0, 0));
p2d_append(p, v2d_new(1, 0));
p2d_append(p, v2d_new(2, 3));
p2d_close(p);
forall_edges(a, b, p->v)
printf("%g %g %g %g\n", a->x, a->y, b->x, b->y);
EOF
expect <<EOF
0 0 1 0
1 0 2 3
2 3 0 0
EOF
###############################################################################