polygon_point_test.cpp 4.9 KB

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