1 // Boost.Polygon library polygon_point_test.cpp file
2
3 // Copyright Andrii Sydorchuk 2012.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 // See http://www.boost.org for updates, documentation, and revision history.
9
10 #include <boost/core/lightweight_test.hpp>
11 #include <boost/polygon/point_concept.hpp>
12 #include <boost/polygon/point_data.hpp>
13 #include <boost/polygon/point_traits.hpp>
14
15 using namespace boost::polygon;
16
point_data_test()17 void point_data_test()
18 {
19 typedef point_data<int> point_type;
20
21 point_type point1(1, 2);
22 point_type point2;
23 point2 = point1;
24 BOOST_TEST_EQ(point1.x(), 1);
25 BOOST_TEST_EQ(point1.y(), 2);
26 BOOST_TEST_EQ(point2.x(), 1);
27 BOOST_TEST_EQ(point2.y(), 2);
28 BOOST_TEST(point1 == point2);
29 BOOST_TEST(!(point1 != point2));
30 BOOST_TEST(!(point1 < point2));
31 BOOST_TEST(!(point1 > point2));
32 BOOST_TEST(point1 <= point2);
33 BOOST_TEST(point1 >= point2);
34
35 point2.x(2);
36 point2.y(1);
37 BOOST_TEST_EQ(point2.x(), 2);
38 BOOST_TEST_EQ(point2.y(), 1);
39 BOOST_TEST(!(point1 == point2));
40 BOOST_TEST(point1 != point2);
41 BOOST_TEST(point1 < point2);
42 BOOST_TEST(!(point1 > point2));
43 BOOST_TEST(point1 <= point2);
44 BOOST_TEST(!(point1 >= point2));
45
46 point2.set(HORIZONTAL, 1);
47 point2.set(VERTICAL, 2);
48 BOOST_TEST(point1 == point2);
49 }
50
point_traits_test()51 void point_traits_test()
52 {
53 typedef point_data<int> point_type;
54
55 point_type point = point_mutable_traits<point_type>::construct(1, 2);
56 BOOST_TEST_EQ(point_traits<point_type>::get(point, HORIZONTAL), 1);
57 BOOST_TEST_EQ(point_traits<point_type>::get(point, VERTICAL), 2);
58
59 point_mutable_traits<point_type>::set(point, HORIZONTAL, 3);
60 point_mutable_traits<point_type>::set(point, VERTICAL, 4);
61 BOOST_TEST_EQ(point_traits<point_type>::get(point, HORIZONTAL), 3);
62 BOOST_TEST_EQ(point_traits<point_type>::get(point, VERTICAL), 4);
63 }
64
65 template <typename T>
66 struct Point {
67 T x;
68 T y;
69 };
70
71 namespace boost {
72 namespace polygon {
73 template <typename T>
74 struct geometry_concept< Point<T> > {
75 typedef point_concept type;
76 };
77
78 template <typename T>
79 struct point_traits< Point<T> > {
80 typedef T coordinate_type;
81
getboost::polygon::point_traits82 static coordinate_type get(const Point<T>& point, orientation_2d orient) {
83 return (orient == HORIZONTAL) ? point.x : point.y;
84 }
85 };
86
87 template <typename T>
88 struct point_mutable_traits< Point<T> > {
89 typedef T coordinate_type;
90
setboost::polygon::point_mutable_traits91 static void set(Point<T>& point, orientation_2d orient, T value) {
92 (orient == HORIZONTAL) ? point.x = value : point.y = value;
93 }
94
constructboost::polygon::point_mutable_traits95 static Point<T> construct(coordinate_type x, coordinate_type y) {
96 Point<T> point;
97 point.x = x;
98 point.y = y;
99 return point;
100 }
101 };
102 } // polygon
103 } // boost
104
point_concept_test1()105 void point_concept_test1()
106 {
107 typedef Point<int> point_type;
108
109 point_type point1 = construct<point_type>(1, 2);
110 BOOST_TEST_EQ(point1.x, 1);
111 BOOST_TEST_EQ(point1.y, 2);
112
113 set(point1, HORIZONTAL, 3);
114 set(point1, VERTICAL, 4);
115 BOOST_TEST_EQ(get(point1, HORIZONTAL), 3);
116 BOOST_TEST_EQ(get(point1, VERTICAL), 4);
117
118 point_type point2;
119 assign(point2, point1);
120 BOOST_TEST(equivalence(point1, point2));
121
122 x(point2, 1);
123 y(point2, 2);
124 BOOST_TEST_EQ(x(point2), 1);
125 BOOST_TEST_EQ(y(point2), 2);
126 }
127
point_concept_test2()128 void point_concept_test2()
129 {
130 typedef Point<int> point_type;
131
132 point_type point1 = construct<point_type>(1, 2);
133 point_type point2 = construct<point_type>(5, 5);
134 BOOST_TEST_EQ(euclidean_distance(point1, point2, HORIZONTAL), 4);
135 BOOST_TEST_EQ(euclidean_distance(point1, point2, VERTICAL), 3);
136 BOOST_TEST_EQ(manhattan_distance(point1, point2), 7);
137 BOOST_TEST_EQ(euclidean_distance(point1, point2), 5.0);
138 }
139
point_concept_test3()140 void point_concept_test3()
141 {
142 typedef Point<int> point_type;
143
144 point_type point = construct<point_type>(1, 2);
145 point_type shift = construct<point_type>(4, 3);
146 convolve(point, shift);
147 BOOST_TEST_EQ(x(point), 5);
148 BOOST_TEST_EQ(y(point), 5);
149
150 deconvolve(point, shift);
151 BOOST_TEST_EQ(x(point), 1);
152 BOOST_TEST_EQ(y(point), 2);
153
154 scale_up(point, 5);
155 BOOST_TEST_EQ(x(point), 5);
156 BOOST_TEST_EQ(y(point), 10);
157
158 scale_down(point, 5);
159 BOOST_TEST_EQ(x(point), 1);
160 BOOST_TEST_EQ(y(point), 2);
161
162 move(point, HORIZONTAL, 2);
163 move(point, VERTICAL, 3);
164 BOOST_TEST_EQ(x(point), 3);
165 BOOST_TEST_EQ(y(point), 5);
166 }
167
168 template<typename T>
169 struct Transformer {
scaleTransformer170 void scale(T& x, T& y) const {
171 x *= 2;
172 y *= 2;
173 }
174
transformTransformer175 void transform(T& x, T& y) const {
176 T tmp = x;
177 x = y;
178 y = tmp;
179 }
180 };
181
point_concept_test4()182 void point_concept_test4()
183 {
184 typedef Point<int> point_type;
185
186 point_type point = construct<point_type>(1, 2);
187 scale(point, Transformer<int>());
188 BOOST_TEST_EQ(x(point), 2);
189 BOOST_TEST_EQ(y(point), 4);
190
191 transform(point, Transformer<int>());
192 BOOST_TEST_EQ(x(point), 4);
193 BOOST_TEST_EQ(y(point), 2);
194 }
195
main()196 int main()
197 {
198 point_data_test();
199 point_traits_test();
200 point_concept_test1();
201 point_concept_test2();
202 point_concept_test3();
203 point_concept_test4();
204 return boost::report_errors();
205 }
206