• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry Index
2 // Unit Test
3 
4 // Copyright (c) 2014-2015 Adam Wulkiewicz, Lodz, Poland.
5 
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 #include <rtree/test_rtree.hpp>
11 
12 #include <boost/core/addressof.hpp>
13 
14 #include <boost/geometry/geometries/register/point.hpp>
15 #include <boost/geometry/geometries/polygon.hpp>
16 
17 struct point
18 {
pointpoint19     point(double xx = 0, double yy = 0) : x(xx), y(yy) {}
20     double x, y;
21 };
22 
BOOST_GEOMETRY_REGISTER_POINT_2D(point,double,bg::cs::cartesian,x,y)23 BOOST_GEOMETRY_REGISTER_POINT_2D(point, double, bg::cs::cartesian, x, y)
24 
25 template <typename Rtree, typename Convertible>
26 void check_convertible_to_value(Rtree const& rt, Convertible const& conv)
27 {
28     static const bool
29         is_conv_to_indexable
30             = boost::is_convertible<Convertible, typename Rtree::indexable_type>::value;
31     static const bool
32         is_conv_to_value
33             = boost::is_convertible<Convertible, typename Rtree::value_type>::value;
34     static const bool
35         is_same_as_indexable
36             = boost::is_same<Convertible, typename Rtree::indexable_type>::value;
37     static const bool
38         is_same_as_value
39             = boost::is_same<Convertible, typename Rtree::value_type>::value;
40 
41     BOOST_CHECK_EQUAL(is_same_as_indexable, false);
42     BOOST_CHECK_EQUAL(is_same_as_value, false);
43     BOOST_CHECK_EQUAL(is_conv_to_indexable, false);
44     BOOST_CHECK_EQUAL(is_conv_to_value, true);
45 
46     typename Rtree::value_type const& val = conv;
47     BOOST_CHECK(rt.value_eq()(val, conv));
48 }
49 
50 template <typename Box, typename Params>
test_pair()51 void test_pair()
52 {
53     typedef std::pair<Box, std::size_t> Value;
54 
55     typename boost::remove_const<Box>::type box;
56     bg::assign_zero(box);
57 
58     Value val(box, 0);
59 
60     // sanity check
61     std::vector<Value> vec;
62     vec.push_back(val);
63     vec.push_back(std::make_pair(box, 0));
64     vec.push_back(std::make_pair(box, (unsigned short)0));
65 
66     bgi::rtree<Value, Params> rt;
67     rt.insert(val);
68     rt.insert(std::make_pair(box, 0));
69     rt.insert(std::make_pair(box, (unsigned short)0));
70     BOOST_CHECK_EQUAL(rt.size(), 3u);
71 
72     check_convertible_to_value(rt, std::make_pair(box, 0));
73     check_convertible_to_value(rt, std::make_pair(box, (unsigned short)0));
74     BOOST_CHECK(bg::covered_by(rt.indexable_get()(std::make_pair(box, 0)), rt.bounds()));
75     BOOST_CHECK(bg::covered_by(rt.indexable_get()(std::make_pair(box, (unsigned short)0)), rt.bounds()));
76 
77     BOOST_CHECK_EQUAL(rt.count(val), 3u);
78     BOOST_CHECK_EQUAL(rt.count(std::make_pair(box, 0)), 3u);
79     BOOST_CHECK_EQUAL(rt.count(std::make_pair(box, (unsigned short)0)), 3u);
80     BOOST_CHECK_EQUAL(rt.count(box), 3u);
81 
82     BOOST_CHECK_EQUAL(rt.remove(val), 1u);
83     BOOST_CHECK_EQUAL(rt.remove(std::make_pair(box, 0)), 1u);
84     BOOST_CHECK_EQUAL(rt.remove(std::make_pair(box, (unsigned short)0)), 1u);
85     BOOST_CHECK_EQUAL(rt.size(), 0u);
86 }
87 
88 template <typename Box, typename Params>
test_pair_geom_ptr()89 void test_pair_geom_ptr()
90 {
91     typedef typename bg::point_type<Box>::type point_t;
92     typedef bg::model::polygon<point_t> polygon_t;
93 
94     typedef std::pair<Box, polygon_t*> Value;
95 
96     typename boost::remove_const<Box>::type box;
97     bg::assign_zero(box);
98 
99     polygon_t poly;
100 
101     Value val(box, boost::addressof(poly));
102 
103     bgi::rtree<Value, Params> rt;
104     rt.insert(val);
105     rt.insert(std::make_pair(box, boost::addressof(poly)));
106 
107     BOOST_CHECK_EQUAL(rt.size(), 2u);
108 
109     BOOST_CHECK_EQUAL(rt.remove(val), 1u);
110     BOOST_CHECK_EQUAL(rt.remove(std::make_pair(box, boost::addressof(poly))), 1u);
111 
112     BOOST_CHECK_EQUAL(rt.size(), 0u);
113 }
114 
115 template <typename Params>
test_point()116 void test_point()
117 {
118     bgi::rtree<point, Params> rt;
119 
120     rt.insert(0.0);
121     BOOST_CHECK_EQUAL(rt.size(), 1u);
122     BOOST_CHECK_EQUAL(rt.remove(0.0), 1u);
123 }
124 
test_main(int,char * [])125 int test_main(int, char* [])
126 {
127     typedef bg::model::point<double, 2, bg::cs::cartesian> Pt;
128     typedef bg::model::box<Pt> Box;
129 
130     test_pair< Box, bgi::linear<16> >();
131     test_pair< Box, bgi::quadratic<4> >();
132     test_pair< Box, bgi::rstar<4> >();
133     //test_rtree< Box const, bgi::linear<16> >();
134     //test_rtree< Box const, bgi::quadratic<4> >();
135     //test_rtree< Box const, bgi::rstar<4> >();
136 
137     test_pair_geom_ptr< Box, bgi::linear<16> >();
138     test_pair_geom_ptr< Box, bgi::quadratic<4> >();
139     test_pair_geom_ptr< Box, bgi::rstar<4> >();
140 
141     test_point< bgi::linear<16> >();
142     test_point< bgi::quadratic<4> >();
143     test_point< bgi::rstar<4> >();
144 
145     return 0;
146 }
147