/libs/geometry/test/policies/compare.cpp

https://github.com/delalond/boost_1_54_0-bgq · C++ · 245 lines · 167 code · 51 blank · 27 comment · 2 complexity · 7656c04034be6d6fc1abc5dbc9db3d04 MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <geometry_test_common.hpp>
  8. #include <algorithm>
  9. #include <boost/geometry/algorithms/make.hpp>
  10. #include <boost/geometry/io/dsv/write.hpp>
  11. #include <boost/geometry/policies/compare.hpp>
  12. #include <boost/geometry/strategies/strategies.hpp>
  13. #include <boost/geometry/geometries/point.hpp>
  14. #include <boost/geometry/geometries/adapted/c_array.hpp>
  15. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  16. #include <test_common/test_point.hpp>
  17. BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
  18. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  19. template <typename Container>
  20. inline std::string coordinates(Container const& points)
  21. {
  22. std::ostringstream out;
  23. typedef typename boost::range_value<Container>::type point_type;
  24. for (typename boost::range_const_iterator<Container>::type it = boost::begin(points);
  25. it != boost::end(points);
  26. ++it)
  27. {
  28. out << bg::dsv(*it);
  29. }
  30. return out.str();
  31. }
  32. template <typename P>
  33. void test_2d_compare()
  34. {
  35. P p1 = bg::make<P>(3, 1);
  36. P p2 = bg::make<P>(3, 1);
  37. P p3 = bg::make<P>(1, 3);
  38. P p4 = bg::make<P>(5, 2);
  39. P p5 = bg::make<P>(3, 2);
  40. // Test in all dimensions
  41. {
  42. bg::equal_to<P> et;
  43. bg::less<P> lt;
  44. bg::greater<P> gt;
  45. BOOST_CHECK_EQUAL(et(p1, p2), true);
  46. BOOST_CHECK_EQUAL(et(p1, p3), false);
  47. BOOST_CHECK_EQUAL(et(p1, p4), false);
  48. BOOST_CHECK_EQUAL(et(p1, p5), false);
  49. BOOST_CHECK_EQUAL(et(p3, p4), false);
  50. BOOST_CHECK_EQUAL(lt(p1, p2), false);
  51. BOOST_CHECK_EQUAL(lt(p1, p3), false);
  52. BOOST_CHECK_EQUAL(lt(p1, p4), true);
  53. BOOST_CHECK_EQUAL(lt(p1, p5), true);
  54. BOOST_CHECK_EQUAL(lt(p3, p4), true);
  55. BOOST_CHECK_EQUAL(gt(p1, p2), false);
  56. BOOST_CHECK_EQUAL(gt(p1, p3), true);
  57. BOOST_CHECK_EQUAL(gt(p1, p4), false);
  58. BOOST_CHECK_EQUAL(gt(p1, p5), false);
  59. BOOST_CHECK_EQUAL(gt(p3, p4), false);
  60. }
  61. // Test in dimension 0, X
  62. {
  63. bg::equal_to<P, 0> et;
  64. bg::less<P, 0> lt;
  65. bg::greater<P, 0> gt;
  66. BOOST_CHECK_EQUAL(et(p1, p2), true);
  67. BOOST_CHECK_EQUAL(et(p1, p3), false);
  68. BOOST_CHECK_EQUAL(et(p1, p4), false);
  69. BOOST_CHECK_EQUAL(et(p1, p5), true);
  70. BOOST_CHECK_EQUAL(et(p3, p4), false);
  71. BOOST_CHECK_EQUAL(lt(p1, p2), false);
  72. BOOST_CHECK_EQUAL(lt(p1, p3), false);
  73. BOOST_CHECK_EQUAL(lt(p1, p4), true);
  74. BOOST_CHECK_EQUAL(lt(p1, p5), false);
  75. BOOST_CHECK_EQUAL(lt(p3, p4), true);
  76. BOOST_CHECK_EQUAL(gt(p1, p2), false);
  77. BOOST_CHECK_EQUAL(gt(p1, p3), true);
  78. BOOST_CHECK_EQUAL(gt(p1, p4), false);
  79. BOOST_CHECK_EQUAL(gt(p1, p5), false);
  80. BOOST_CHECK_EQUAL(gt(p3, p4), false);
  81. }
  82. // Test in dimension 1, Y
  83. {
  84. bg::equal_to<P, 1> et;
  85. bg::less<P, 1> lt;
  86. bg::greater<P, 1> gt;
  87. BOOST_CHECK_EQUAL(et(p1, p2), true);
  88. BOOST_CHECK_EQUAL(et(p1, p3), false);
  89. BOOST_CHECK_EQUAL(et(p1, p4), false);
  90. BOOST_CHECK_EQUAL(et(p1, p5), false);
  91. BOOST_CHECK_EQUAL(et(p3, p4), false);
  92. BOOST_CHECK_EQUAL(lt(p1, p2), false);
  93. BOOST_CHECK_EQUAL(lt(p1, p3), true);
  94. BOOST_CHECK_EQUAL(lt(p1, p4), true);
  95. BOOST_CHECK_EQUAL(lt(p1, p5), true);
  96. BOOST_CHECK_EQUAL(lt(p3, p4), false);
  97. BOOST_CHECK_EQUAL(gt(p1, p2), false);
  98. BOOST_CHECK_EQUAL(gt(p1, p3), false);
  99. BOOST_CHECK_EQUAL(gt(p1, p4), false);
  100. BOOST_CHECK_EQUAL(gt(p1, p5), false);
  101. BOOST_CHECK_EQUAL(gt(p3, p4), true);
  102. }
  103. }
  104. template <typename P>
  105. void test_2d_sort()
  106. {
  107. typedef typename bg::coordinate_type<P>::type ct;
  108. std::vector<P> v;
  109. v.push_back(bg::make<P>(3, 1));
  110. v.push_back(bg::make<P>(2, 3));
  111. v.push_back(bg::make<P>(2, 2));
  112. v.push_back(bg::make<P>(1, 3));
  113. // Sort on coordinates in order x,y,z
  114. std::sort(v.begin(), v.end(), bg::less<P>());
  115. std::string s = coordinates(v);
  116. BOOST_CHECK_EQUAL(s, "(1, 3)(2, 2)(2, 3)(3, 1)");
  117. // Reverse sort
  118. std::sort(v.begin(), v.end(), bg::greater<P>());
  119. s = coordinates(v);
  120. BOOST_CHECK_EQUAL(s, "(3, 1)(2, 3)(2, 2)(1, 3)");
  121. // Sort backwards on coordinates in order x,y,z
  122. //std::sort(v.begin(), v.end(), bg::greater<P>());
  123. //std::string s = coordinates(v);
  124. //BOOST_CHECK_EQUAL(s, "(1, 3)(2, 2)(2, 3)(3, 1)");
  125. // Refill to remove duplicate coordinates
  126. v.clear();
  127. v.push_back(bg::make<P>(4, 1));
  128. v.push_back(bg::make<P>(3, 2));
  129. v.push_back(bg::make<P>(2, 3));
  130. v.push_back(bg::make<P>(1, 4));
  131. // Sort ascending on only x-coordinate
  132. std::sort(v.begin(), v.end(), bg::less<P, 0>());
  133. s = coordinates(v);
  134. BOOST_CHECK_EQUAL(s, "(1, 4)(2, 3)(3, 2)(4, 1)");
  135. // Sort ascending on only y-coordinate
  136. std::sort(v.begin(), v.end(), bg::less<P, 1>());
  137. s = coordinates(v);
  138. BOOST_CHECK_EQUAL(s, "(4, 1)(3, 2)(2, 3)(1, 4)");
  139. // Sort descending on only x-coordinate
  140. std::sort(v.begin(), v.end(), bg::greater<P, 0>());
  141. s = coordinates(v);
  142. //BOOST_CHECK_EQUAL(s, "(4, 1)(3, 2)(2, 3)(1, 4)");
  143. // Sort descending on only y-coordinate
  144. std::sort(v.begin(), v.end(), bg::greater<P, 1>());
  145. s = coordinates(v);
  146. BOOST_CHECK_EQUAL(s, "(1, 4)(2, 3)(3, 2)(4, 1)");
  147. // Make non-unique vector
  148. v.push_back(bg::make<P>(4, 1));
  149. v.push_back(bg::make<P>(3, 2));
  150. v.push_back(bg::make<P>(2, 3));
  151. v.push_back(bg::make<P>(1, 4));
  152. v.push_back(bg::make<P>(1, 5));
  153. std::sort(v.begin(), v.end(), bg::less<P>());
  154. s = coordinates(v);
  155. BOOST_CHECK_EQUAL(s, "(1, 4)(1, 4)(1, 5)(2, 3)(2, 3)(3, 2)(3, 2)(4, 1)(4, 1)");
  156. std::vector<P> v2;
  157. std::unique_copy(v.begin(), v.end(), std::back_inserter(v2), bg::equal_to<P>());
  158. s = coordinates(v2);
  159. BOOST_CHECK_EQUAL(s, "(1, 4)(1, 5)(2, 3)(3, 2)(4, 1)");
  160. }
  161. template <typename P>
  162. void test_spherical()
  163. {
  164. typedef typename bg::coordinate_type<P>::type ct;
  165. std::vector<P> v;
  166. v.push_back(bg::make<P>( 179.73, 71.56)); // east
  167. v.push_back(bg::make<P>( 177.47, 71.23)); // less east
  168. v.push_back(bg::make<P>(-178.78, 70.78)); // further east, = west, this is the most right point
  169. // Sort on coordinates in order x,y,z
  170. std::sort(v.begin(), v.end(), bg::less<P>());
  171. std::string s = coordinates(v);
  172. BOOST_CHECK_EQUAL(s, "(177.47, 71.23)(179.73, 71.56)(-178.78, 70.78)");
  173. // Sort ascending on only x-coordinate
  174. std::sort(v.begin(), v.end(), bg::less<P, 0>());
  175. s = coordinates(v);
  176. BOOST_CHECK_EQUAL(s, "(177.47, 71.23)(179.73, 71.56)(-178.78, 70.78)");
  177. // Sort ascending on only x-coordinate, but override with std-comparison,
  178. // (so this is the normal sorting behaviour that would have been used
  179. // if it would not have been spherical)
  180. std::sort(v.begin(), v.end(), bg::less<P, 0, std::less<ct> >());
  181. s = coordinates(v);
  182. BOOST_CHECK_EQUAL(s, "(-178.78, 70.78)(177.47, 71.23)(179.73, 71.56)");
  183. }
  184. int test_main(int, char* [])
  185. {
  186. test_2d_compare<bg::model::point<int, 2, bg::cs::cartesian> >();
  187. test_2d_compare<bg::model::point<double, 2, bg::cs::cartesian> >();
  188. test_2d_sort<bg::model::point<int, 2, bg::cs::cartesian> >();
  189. test_2d_sort<bg::model::point<float, 2, bg::cs::cartesian> >();
  190. test_2d_sort<boost::tuple<double, double> >();
  191. test_2d_sort<bg::model::point<double, 2, bg::cs::cartesian> >();
  192. test_spherical<bg::model::point<double, 2, bg::cs::spherical<bg::degree> > >();
  193. return 0;
  194. }