mirror of
git://projects.qi-hardware.com/cae-tools.git
synced 2025-04-21 12:27:27 +03:00
poly2d/: Yet another 2D polygon library (WIP)
This commit is contained in:
99
poly2d/test/Common
Executable file
99
poly2d/test/Common
Executable file
@@ -0,0 +1,99 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# Common - Elements shared by all regression tests for poly2d
|
||||
#
|
||||
# Written 2010, 2011 by Werner Almesberger
|
||||
# Copyright 2010, 2011 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.
|
||||
#
|
||||
|
||||
|
||||
compile_and_run()
|
||||
{
|
||||
LIBS="-lpoly2d -lCGAL -lCGAL_Core -lboost_thread"
|
||||
LIBS="$LIBS -lstdc++ -lmpfr -lgmp -lm"
|
||||
|
||||
cat <<EOF >_.c
|
||||
#include <poly2d.h>
|
||||
#include "p2d_hsort.h"
|
||||
|
||||
|
||||
static void recurse_hier(const struct p2d_hier *h, int level)
|
||||
{
|
||||
const struct v2d *v;
|
||||
|
||||
while (h) {
|
||||
printf("%*s", level*2, "");
|
||||
v = h->p.v;
|
||||
while (v) {
|
||||
printf("%s%g %g", v == h->p.v ? "" : " ", v->x, v->y);
|
||||
v = v->next;
|
||||
if (v == h->p.v)
|
||||
break;
|
||||
}
|
||||
printf("\n");
|
||||
recurse_hier(h->holes, level+1);
|
||||
h = p2d_to_hier(h->p.next);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void __attribute__((unused)) print_hier(const struct p2d_hier *h)
|
||||
{
|
||||
recurse_hier(h, 0);
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
`cat _in`
|
||||
return 0;
|
||||
}
|
||||
EOF
|
||||
gcc -Wall -Werror -g -I.. _.c -L.. $LIBS || return
|
||||
$VALGRIND ./a.out
|
||||
}
|
||||
|
||||
|
||||
tst()
|
||||
{
|
||||
echo -n "$1: " 1>&2
|
||||
shift
|
||||
cat >_in
|
||||
compile_and_run "$@" >_out 2>&1 || {
|
||||
echo FAILED "($SCRIPT)" 1>&2
|
||||
cat _out
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
tst_fail()
|
||||
{
|
||||
echo -n "$1: " 1>&2
|
||||
shift
|
||||
cat >_in
|
||||
compile_and_run "$@" >_out 2>&1 && {
|
||||
echo FAILED "($SCRIPT)" 1>&2
|
||||
cat _out
|
||||
exit 1
|
||||
}
|
||||
rm -f _in _.c a.out
|
||||
}
|
||||
|
||||
|
||||
expect()
|
||||
{
|
||||
diff -u - "$@" _out >_diff || {
|
||||
echo FAILED "($SCRIPT)" 1>&2
|
||||
cat _diff 1>&2
|
||||
exit 1
|
||||
}
|
||||
echo PASSED 1>&2
|
||||
rm -f _in _out _diff _.c a.out
|
||||
passed=`expr ${passed:-0} + 1`
|
||||
}
|
||||
103
poly2d/test/area
Executable file
103
poly2d/test/area
Executable file
@@ -0,0 +1,103 @@
|
||||
#!/bin/sh
|
||||
. ./Common
|
||||
|
||||
###############################################################################
|
||||
|
||||
tst "area without holes, constant offset" <<EOF
|
||||
struct p2d *p = p2d_new();
|
||||
struct p2d *q;
|
||||
|
||||
p2d_append(p, v2d_new(0, 0));
|
||||
p2d_append(p, v2d_new(5.5, 0));
|
||||
p2d_append(p, v2d_new(5.5, 4.5));
|
||||
p2d_append(p, v2d_new(0, 4.5));
|
||||
p2d_close(p);
|
||||
q = p2d_area(p, 1, 0);
|
||||
p2d_write_gnuplot_all(stdout, q);
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
1 1
|
||||
4.5 1
|
||||
4.5 3.5
|
||||
1 3.5
|
||||
1 1
|
||||
|
||||
2 2
|
||||
3.5 2
|
||||
3.5 2.5
|
||||
2 2.5
|
||||
2 2
|
||||
|
||||
EOF
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
tst "area without holes, offset with overlap" <<EOF
|
||||
struct p2d *p = p2d_new();
|
||||
struct p2d *q;
|
||||
|
||||
p2d_append(p, v2d_new(0, 0));
|
||||
p2d_append(p, v2d_new(5.5, 0));
|
||||
p2d_append(p, v2d_new(5.5, 4.5));
|
||||
p2d_append(p, v2d_new(0, 4.5));
|
||||
p2d_close(p);
|
||||
q = p2d_area(p, 1, 0.3);
|
||||
p2d_write_gnuplot_all(stdout, q);
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
1 1
|
||||
4.5 1
|
||||
4.5 3.5
|
||||
1 3.5
|
||||
1 1
|
||||
|
||||
1.7 1.7
|
||||
3.8 1.7
|
||||
3.8 2.8
|
||||
1.7 2.8
|
||||
1.7 1.7
|
||||
|
||||
EOF
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
tst "area with one symmetric hole" <<EOF
|
||||
struct p2d *pl, *p;
|
||||
struct p2d *q;
|
||||
|
||||
pl = p = p2d_new();
|
||||
p2d_append(p, v2d_new(0, 0));
|
||||
p2d_append(p, v2d_new(20, 0));
|
||||
p2d_append(p, v2d_new(20, 10));
|
||||
p2d_append(p, v2d_new(0, 10));
|
||||
p2d_close(p);
|
||||
|
||||
p = p->next = p2d_new();
|
||||
p2d_append(p, v2d_new(2, 2));
|
||||
p2d_append(p, v2d_new(2, 8));
|
||||
p2d_append(p, v2d_new(18, 8));
|
||||
p2d_append(p, v2d_new(18, 2));
|
||||
p2d_close(p);
|
||||
|
||||
q = p2d_area(pl, 0.7, 0);
|
||||
p2d_write_gnuplot_all(stdout, q);
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
0.7 0.7
|
||||
19.3 0.7
|
||||
19.3 9.3
|
||||
0.7 9.3
|
||||
0.7 0.7
|
||||
|
||||
1.3 1.3
|
||||
1.3 8.7
|
||||
18.7 8.7
|
||||
18.7 1.3
|
||||
1.3 1.3
|
||||
|
||||
EOF
|
||||
|
||||
###############################################################################
|
||||
96
poly2d/test/hsort
Executable file
96
poly2d/test/hsort
Executable file
@@ -0,0 +1,96 @@
|
||||
#!/bin/sh
|
||||
. ./Common
|
||||
|
||||
###############################################################################
|
||||
|
||||
tst "hierarchical sort with one hole" <<EOF
|
||||
struct p2d *pl, *p;
|
||||
|
||||
pl = p = p2d_new();
|
||||
p2d_append(p, v2d_new(0, 0));
|
||||
p2d_append(p, v2d_new(20, 0));
|
||||
p2d_append(p, v2d_new(20, 10));
|
||||
p2d_append(p, v2d_new(0, 10));
|
||||
p2d_close(p);
|
||||
|
||||
p = p->next = p2d_new();
|
||||
p2d_append(p, v2d_new(2, 2));
|
||||
p2d_append(p, v2d_new(2, 8));
|
||||
p2d_append(p, v2d_new(18, 8));
|
||||
p2d_append(p, v2d_new(18, 2));
|
||||
p2d_close(p);
|
||||
|
||||
print_hier(p2d_hsort(pl));
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
0 0 20 0 20 10 0 10
|
||||
2 2 2 8 18 8 18 2
|
||||
EOF
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
tst "hierarchical sort with two holes" <<EOF
|
||||
struct p2d *pl, *p;
|
||||
|
||||
pl = p = p2d_new();
|
||||
p2d_append(p, v2d_new(0, 0));
|
||||
p2d_append(p, v2d_new(10, 0));
|
||||
p2d_append(p, v2d_new(10, 10));
|
||||
p2d_close(p);
|
||||
|
||||
p = p->next = p2d_new();
|
||||
p2d_append(p, v2d_new(2, 2));
|
||||
p2d_append(p, v2d_new(4, 2));
|
||||
p2d_append(p, v2d_new(4, 4));
|
||||
p2d_close(p);
|
||||
|
||||
p = p->next = p2d_new();
|
||||
p2d_append(p, v2d_new(6, 2));
|
||||
p2d_append(p, v2d_new(8, 2));
|
||||
p2d_append(p, v2d_new(8, 8));
|
||||
p2d_close(p);
|
||||
|
||||
print_hier(p2d_hsort(pl));
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
0 0 10 0 10 10
|
||||
2 2 4 2 4 4
|
||||
6 2 8 2 8 8
|
||||
EOF
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
tst "hierarchical sort with nested holes" <<EOF
|
||||
struct p2d *pl, *p;
|
||||
|
||||
pl = p = p2d_new();
|
||||
p2d_append(p, v2d_new(0, 0));
|
||||
p2d_append(p, v2d_new(10, 0));
|
||||
p2d_append(p, v2d_new(10, 10));
|
||||
p2d_close(p);
|
||||
|
||||
p = p->next = p2d_new();
|
||||
p2d_append(p, v2d_new(2, 2));
|
||||
p2d_append(p, v2d_new(8, 2));
|
||||
p2d_append(p, v2d_new(8, 8));
|
||||
p2d_close(p);
|
||||
|
||||
p = p->next = p2d_new();
|
||||
p2d_append(p, v2d_new(3, 3));
|
||||
p2d_append(p, v2d_new(7, 3));
|
||||
p2d_append(p, v2d_new(7, 7));
|
||||
p2d_close(p);
|
||||
|
||||
print_hier(p2d_hsort(pl));
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
0 0 10 0 10 10
|
||||
2 2 8 2 8 8
|
||||
3 3 7 3 7 7
|
||||
EOF
|
||||
|
||||
|
||||
###############################################################################
|
||||
61
poly2d/test/make
Executable file
61
poly2d/test/make
Executable file
@@ -0,0 +1,61 @@
|
||||
#!/bin/sh
|
||||
. ./Common
|
||||
|
||||
###############################################################################
|
||||
|
||||
tst "make an open polygon" <<EOF
|
||||
struct p2d *p = p2d_new();
|
||||
|
||||
p2d_append(p, v2d_new(0, 0));
|
||||
p2d_append(p, v2d_new(1, 2));
|
||||
p2d_write_gnuplot(stdout, p);
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
0 0
|
||||
1 2
|
||||
|
||||
EOF
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
tst "make a closed polygon" <<EOF
|
||||
struct p2d *p = p2d_new();
|
||||
|
||||
p2d_append(p, v2d_new(-1, 1));
|
||||
p2d_append(p, v2d_new(3, 7));
|
||||
p2d_close(p);
|
||||
p2d_write_gnuplot(stdout, p);
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
-1 1
|
||||
3 7
|
||||
-1 1
|
||||
|
||||
EOF
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
tst "make two open polygons" <<EOF
|
||||
struct p2d *p = p2d_new();
|
||||
struct p2d *q = p2d_new();
|
||||
|
||||
p2d_append(p, v2d_new(1, 4));
|
||||
p2d_append(p, v2d_new(2, 8));
|
||||
p->next = q;
|
||||
p2d_append(q, v2d_new(3, 15));
|
||||
p2d_append(q, v2d_new(4, 16));
|
||||
p2d_write_gnuplot_all(stdout, p);
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
1 4
|
||||
2 8
|
||||
|
||||
3 15
|
||||
4 16
|
||||
|
||||
EOF
|
||||
|
||||
###############################################################################
|
||||
52
poly2d/test/offset
Executable file
52
poly2d/test/offset
Executable file
@@ -0,0 +1,52 @@
|
||||
#!/bin/sh
|
||||
. ./Common
|
||||
|
||||
###############################################################################
|
||||
|
||||
tst "outer offset" <<EOF
|
||||
struct p2d *p = p2d_new();
|
||||
struct p2d *q;
|
||||
|
||||
p2d_append(p, v2d_new(0, 0));
|
||||
p2d_append(p, v2d_new(2, 0));
|
||||
p2d_append(p, v2d_new(2, 1));
|
||||
p2d_append(p, v2d_new(0, 1));
|
||||
p2d_close(p);
|
||||
q = p2d_offset(p, 0.5);
|
||||
p2d_write_gnuplot(stdout, p2d_reverse(q->next));
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
-0.5 -0.5
|
||||
2.5 -0.5
|
||||
2.5 1.5
|
||||
-0.5 1.5
|
||||
-0.5 -0.5
|
||||
|
||||
EOF
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
tst "inner offset" <<EOF
|
||||
struct p2d *p = p2d_new();
|
||||
struct p2d *q;
|
||||
|
||||
p2d_append(p, v2d_new(0, 0));
|
||||
p2d_append(p, v2d_new(2, 0));
|
||||
p2d_append(p, v2d_new(2, 1));
|
||||
p2d_append(p, v2d_new(0, 1));
|
||||
p2d_close(p);
|
||||
q = p2d_offset(p, -0.1);
|
||||
p2d_write_gnuplot(stdout, q);
|
||||
EOF
|
||||
|
||||
expect <<EOF
|
||||
0.1 0.1
|
||||
1.9 0.1
|
||||
1.9 0.9
|
||||
0.1 0.9
|
||||
0.1 0.1
|
||||
|
||||
EOF
|
||||
|
||||
###############################################################################
|
||||
Reference in New Issue
Block a user