1 #include <cmath>
2 #include "vgl/vgl_polygon.h"
3 #include "vgl/vgl_clip.h"
4 #ifdef _MSC_VER
5 # include "vcl_msvc_warnings.h"
6 #endif
7
8 #include "testlib/testlib_test.h"
9
10 template <class T>
11 inline static bool
is_vertex(vgl_polygon<T> const & poly,T x,T y)12 is_vertex(vgl_polygon<T> const & poly, T x, T y)
13 {
14 for (unsigned int i = 0; i < poly.num_sheets(); ++i)
15 for (unsigned int p = 0; p < poly[i].size(); ++p)
16 if (std::abs(x - poly[i][p].x()) < 1e-6 && std::abs(y - poly[i][p].y()) < 1e-6)
17 return true;
18
19 return false;
20 }
21
22
23 static void
test_union()24 test_union()
25 {
26 double cont1[] = { 0, 0, 5, 0, 5, 5, 0, 5 };
27 vgl_polygon<double> poly1(cont1, 4);
28
29 {
30 vgl_polygon<double> poly2;
31
32 vgl_polygon<double> result = vgl_clip(poly1, poly2, vgl_clip_type_union);
33 TEST("union with null polygon 1", result.num_sheets() == 1 && result.num_vertices() == 4, true);
34
35 vgl_polygon<double> result2 = vgl_clip(poly2, poly1, vgl_clip_type_union);
36 TEST("union with null polygon 2", result2.num_sheets() == 1 && result2.num_vertices() == 4, true);
37 }
38
39 {
40 double cont2[] = { 6, 0, 8, 1, 6, 2 };
41 vgl_polygon<double> poly2(cont2, 3);
42
43 vgl_polygon<double> result = vgl_clip(poly1, poly2, vgl_clip_type_union);
44 TEST("disjoint union", result.num_sheets() == 2 && result.num_vertices() == 7, true);
45 }
46
47 #ifdef BUILD_NONCOMMERCIAL
48 {
49 double cont2[] = { 4, 1, 8, 1, 8, 6, 4, 6 };
50 vgl_polygon<double> poly2(cont2, 4);
51
52 vgl_polygon<double> result = vgl_clip(poly1, poly2, vgl_clip_type_union);
53 TEST("overlapping union",
54 result.num_sheets() == 1 && result.num_vertices() == 8 && is_vertex(result, 5.0, 1.0) &&
55 is_vertex(result, 4.0, 6.0),
56 true);
57 }
58 #endif // BUILD_NONCOMMERCIAL
59 }
60
61
62 static void
test_intersection()63 test_intersection()
64 {
65 float cont1[] = { 0, 0, 5, 0, 5, 5, 0, 5 };
66 vgl_polygon<float> poly1(cont1, 4);
67
68 {
69 vgl_polygon<float> poly2;
70
71 vgl_polygon<float> result = vgl_clip(poly1, poly2, vgl_clip_type_intersect);
72 TEST("intersection with null polygon 1", result.num_sheets() == 0 && result.num_vertices() == 0, true);
73
74 #if 0 // This test is identical to the previous. Why?
75 vgl_polygon<float> result2 = vgl_clip( poly1, poly2, vgl_clip_type_intersect );
76 TEST("intersection with null polygon 2",
77 result2.num_sheets() == 0 && result2.num_vertices() == 0, true);
78 #endif
79 }
80 {
81 float cont2[] = { 6, 0, 8, 1, 6, 2 };
82 vgl_polygon<float> poly2(cont2, 3);
83
84 vgl_polygon<float> result = vgl_clip(poly1, poly2, vgl_clip_type_intersect);
85 TEST("disjoint simple intersection", result.num_sheets() == 0 && result.num_vertices() == 0, true);
86 }
87
88 #ifdef BUILD_NONCOMMERCIAL
89 {
90 float cont2[] = { 4, 1, 8, 1, 8, 6, 4, 6 };
91 vgl_polygon<float> poly2(cont2, 4);
92
93 vgl_polygon<float> result = vgl_clip(poly1, poly2, vgl_clip_type_intersect);
94 TEST("overlapping simple intersection",
95 result.num_sheets() == 1 && result.num_vertices() == 4 && is_vertex(result, 4.f, 1.f),
96 true);
97 }
98 #endif // BUILD_NONCOMMERCIAL
99
100 {
101 float cont2[] = { -3, -3, 8, -3, 8, 8, -3, 8 };
102 float cont3[] = { -1, -1, 6, -1, 6, 6, -1, 6 };
103 vgl_polygon<float> poly2(cont2, 4);
104 poly2.add_contour(cont3, 4);
105
106 vgl_polygon<float> result = vgl_clip(poly1, poly2, vgl_clip_type_intersect);
107 TEST("disjoint holey intersection", result.num_sheets() == 0 && result.num_vertices() == 0, true);
108 }
109 }
110
111 static void
test_clip()112 test_clip()
113 {
114 test_union();
115 test_intersection();
116 }
117
118 TESTMAIN(test_clip);
119