1 #include "vgl/vgl_polygon.h"
2 #include "vgl/vgl_area.h"
3
4 #include "testlib/testlib_test.h"
5
6 static void
test_simple1()7 test_simple1()
8 {
9 double cont1[] = { 0, 0, 5, 0, 5, 3, 0, 3 };
10 vgl_polygon<double> poly1(cont1, 4);
11 TEST_NEAR("rectangle (ccw) signed", vgl_area_signed(poly1), 15, 1e-6);
12 TEST_NEAR("rectangle (ccw) unsigned", vgl_area(poly1), 15, 1e-6);
13 vgl_point_2d<double> c = vgl_centroid(poly1);
14 TEST_NEAR("rectangle (ccw) centroid x", c.x(), 2.5, 1e-6);
15 TEST_NEAR("rectangle (ccw) centroid y", c.y(), 1.5, 1e-6);
16 }
17
18 static void
test_simple2()19 test_simple2()
20 {
21 float cont1[] = { 0, 0, 0, 5, 4, 5, 4, 0 };
22 vgl_polygon<float> poly1(cont1, 4);
23 TEST_NEAR("rectangle (cw) signed", vgl_area_signed(poly1), -20, 1e-6);
24 TEST_NEAR("rectangle (cw) unsigned", vgl_area(poly1), 20, 1e-6);
25 vgl_point_2d<float> c = vgl_centroid(poly1);
26 TEST_NEAR("rectangle (cw) centroid x", c.x(), 2.0, 1e-6);
27 TEST_NEAR("rectangle (cw) centroid y", c.y(), 2.5, 1e-6);
28 }
29
30 static void
test_simple3()31 test_simple3()
32 {
33 const double x[4] = { 1.0, 20.0, 20.0, 1.0 };
34 const double y[4] = { 1.0, 1.0, 20.0, 20.0 };
35 constexpr unsigned int size = 4;
36
37 vgl_polygon<double> poly;
38 poly.new_sheet();
39 poly[0].resize(size);
40 for (unsigned int ii = 0; ii < size; ++ii)
41 {
42 poly[0][ii].set(x[ii], y[ii]);
43 }
44
45 TEST("rectangle (ccw)", vgl_area(poly), 19 * 19);
46 TEST("rectangle (ccw) signed", vgl_area_signed(poly), 19 * 19);
47 }
48
49 static void
test_holey()50 test_holey()
51 {
52 double cont1[] = { 0, 0, 5, 0, 5, 5, 0, 5 };
53 double cont2[] = { 1, 1, 1, 2, 2, 2, 2, 1 };
54 vgl_polygon<double> poly(cont1, 4);
55 poly.add_contour(cont2, 4);
56 TEST_NEAR("rectangle with rectgular cutout signed", vgl_area_signed(poly), 24, 1e-6);
57 TEST_NEAR("rectangle with rectgular cutout unsigned", vgl_area(poly), 24, 1e-6);
58 vgl_point_2d<double> c = vgl_centroid(poly);
59 TEST_NEAR("rectangle with rectgular cutout centroid x", c.x(), 61.0 / 24.0, 1e-6);
60 TEST_NEAR("rectangle with rectgular cutout centroid y", c.y(), 61.0 / 24.0, 1e-6);
61 }
62
63
64 static void
test_area()65 test_area()
66 {
67 test_simple1();
68 test_simple2();
69 test_simple3();
70 test_holey();
71 }
72
73 TESTMAIN(test_area);
74