rtree_values.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Boost.Geometry Index
  2. // Unit Test
  3. // Copyright (c) 2014-2015 Adam Wulkiewicz, Lodz, Poland.
  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 <rtree/test_rtree.hpp>
  8. #include <boost/core/addressof.hpp>
  9. #include <boost/geometry/geometries/register/point.hpp>
  10. #include <boost/geometry/geometries/polygon.hpp>
  11. struct point
  12. {
  13. point(double xx = 0, double yy = 0) : x(xx), y(yy) {}
  14. double x, y;
  15. };
  16. BOOST_GEOMETRY_REGISTER_POINT_2D(point, double, bg::cs::cartesian, x, y)
  17. template <typename Rtree, typename Convertible>
  18. void check_convertible_to_value(Rtree const& rt, Convertible const& conv)
  19. {
  20. static const bool
  21. is_conv_to_indexable
  22. = boost::is_convertible<Convertible, typename Rtree::indexable_type>::value;
  23. static const bool
  24. is_conv_to_value
  25. = boost::is_convertible<Convertible, typename Rtree::value_type>::value;
  26. static const bool
  27. is_same_as_indexable
  28. = boost::is_same<Convertible, typename Rtree::indexable_type>::value;
  29. static const bool
  30. is_same_as_value
  31. = boost::is_same<Convertible, typename Rtree::value_type>::value;
  32. BOOST_CHECK_EQUAL(is_same_as_indexable, false);
  33. BOOST_CHECK_EQUAL(is_same_as_value, false);
  34. BOOST_CHECK_EQUAL(is_conv_to_indexable, false);
  35. BOOST_CHECK_EQUAL(is_conv_to_value, true);
  36. typename Rtree::value_type const& val = conv;
  37. BOOST_CHECK(rt.value_eq()(val, conv));
  38. }
  39. template <typename Box, typename Params>
  40. void test_pair()
  41. {
  42. typedef std::pair<Box, std::size_t> Value;
  43. typename boost::remove_const<Box>::type box;
  44. bg::assign_zero(box);
  45. Value val(box, 0);
  46. // sanity check
  47. std::vector<Value> vec;
  48. vec.push_back(val);
  49. vec.push_back(std::make_pair(box, 0));
  50. vec.push_back(std::make_pair(box, (unsigned short)0));
  51. bgi::rtree<Value, Params> rt;
  52. rt.insert(val);
  53. rt.insert(std::make_pair(box, 0));
  54. rt.insert(std::make_pair(box, (unsigned short)0));
  55. BOOST_CHECK_EQUAL(rt.size(), 3u);
  56. check_convertible_to_value(rt, std::make_pair(box, 0));
  57. check_convertible_to_value(rt, std::make_pair(box, (unsigned short)0));
  58. BOOST_CHECK(bg::covered_by(rt.indexable_get()(std::make_pair(box, 0)), rt.bounds()));
  59. BOOST_CHECK(bg::covered_by(rt.indexable_get()(std::make_pair(box, (unsigned short)0)), rt.bounds()));
  60. BOOST_CHECK_EQUAL(rt.count(val), 3u);
  61. BOOST_CHECK_EQUAL(rt.count(std::make_pair(box, 0)), 3u);
  62. BOOST_CHECK_EQUAL(rt.count(std::make_pair(box, (unsigned short)0)), 3u);
  63. BOOST_CHECK_EQUAL(rt.count(box), 3u);
  64. BOOST_CHECK_EQUAL(rt.remove(val), 1u);
  65. BOOST_CHECK_EQUAL(rt.remove(std::make_pair(box, 0)), 1u);
  66. BOOST_CHECK_EQUAL(rt.remove(std::make_pair(box, (unsigned short)0)), 1u);
  67. BOOST_CHECK_EQUAL(rt.size(), 0u);
  68. }
  69. template <typename Box, typename Params>
  70. void test_pair_geom_ptr()
  71. {
  72. typedef typename bg::point_type<Box>::type point_t;
  73. typedef bg::model::polygon<point_t> polygon_t;
  74. typedef std::pair<Box, polygon_t*> Value;
  75. typename boost::remove_const<Box>::type box;
  76. bg::assign_zero(box);
  77. polygon_t poly;
  78. Value val(box, boost::addressof(poly));
  79. bgi::rtree<Value, Params> rt;
  80. rt.insert(val);
  81. rt.insert(std::make_pair(box, boost::addressof(poly)));
  82. BOOST_CHECK_EQUAL(rt.size(), 2u);
  83. BOOST_CHECK_EQUAL(rt.remove(val), 1u);
  84. BOOST_CHECK_EQUAL(rt.remove(std::make_pair(box, boost::addressof(poly))), 1u);
  85. BOOST_CHECK_EQUAL(rt.size(), 0u);
  86. }
  87. template <typename Params>
  88. void test_point()
  89. {
  90. bgi::rtree<point, Params> rt;
  91. rt.insert(0.0);
  92. BOOST_CHECK_EQUAL(rt.size(), 1u);
  93. BOOST_CHECK_EQUAL(rt.remove(0.0), 1u);
  94. }
  95. int test_main(int, char* [])
  96. {
  97. typedef bg::model::point<double, 2, bg::cs::cartesian> Pt;
  98. typedef bg::model::box<Pt> Box;
  99. test_pair< Box, bgi::linear<16> >();
  100. test_pair< Box, bgi::quadratic<4> >();
  101. test_pair< Box, bgi::rstar<4> >();
  102. //test_rtree< Box const, bgi::linear<16> >();
  103. //test_rtree< Box const, bgi::quadratic<4> >();
  104. //test_rtree< Box const, bgi::rstar<4> >();
  105. test_pair_geom_ptr< Box, bgi::linear<16> >();
  106. test_pair_geom_ptr< Box, bgi::quadratic<4> >();
  107. test_pair_geom_ptr< Box, bgi::rstar<4> >();
  108. test_point< bgi::linear<16> >();
  109. test_point< bgi::quadratic<4> >();
  110. test_point< bgi::rstar<4> >();
  111. return 0;
  112. }