compare.cpp 8.4 KB

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