From e1a4660d47477e76952aad4bd56d20b178a2cd4e Mon Sep 17 00:00:00 2001 From: Adam Wulkiewicz Date: Sat, 23 Feb 2013 23:47:19 +0000 Subject: [PATCH] Added examples of indexing polygons. Fixed c++11 compilation error caused by use of deleted Allocators copy ctor in rstar insert visitor. [SVN r83110] --- doc/html/geometry_index/r_tree.html | 4 + .../geometry_index/r_tree/rtree_examples.html | 192 +++++++++++++++++- .../r_tree/rtree_quickstart.html | 12 +- doc/html/index.html | 2 +- doc/imports.qbk | 2 + doc/rtree/examples.qbk | 8 + doc/src/examples/rtree/Jamfile.v2 | 6 +- doc/src/examples/rtree/interprocess.cpp | 10 +- .../examples/rtree/polygons_shared_ptr.cpp | 88 ++++++++ doc/src/examples/rtree/polygons_vector.cpp | 96 +++++++++ doc/src/examples/rtree/quick_start.cpp | 12 +- .../index/detail/rtree/rstar/insert.hpp | 2 +- 12 files changed, 419 insertions(+), 15 deletions(-) create mode 100644 doc/src/examples/rtree/polygons_shared_ptr.cpp create mode 100644 doc/src/examples/rtree/polygons_vector.cpp diff --git a/doc/html/geometry_index/r_tree.html b/doc/html/geometry_index/r_tree.html index 91109edb9..e4e8c3cec 100644 --- a/doc/html/geometry_index/r_tree.html +++ b/doc/html/geometry_index/r_tree.html @@ -68,6 +68,10 @@
Quick start
+
Index + of polygons stored in vector
+
Index + of shared pointers to polygons
Interprocess
Reference
diff --git a/doc/html/geometry_index/r_tree/rtree_examples.html b/doc/html/geometry_index/r_tree/rtree_examples.html index 7642a31b2..056fb38a4 100644 --- a/doc/html/geometry_index/r_tree/rtree_examples.html +++ b/doc/html/geometry_index/r_tree/rtree_examples.html @@ -29,6 +29,10 @@
Quick start
+
Index + of polygons stored in vector
+
Index + of shared pointers to polygons
Interprocess
@@ -39,7 +43,7 @@

#include <boost/geometry.hpp>
-#include <boost/geometry/geometries/point_xy.hpp>
+#include <boost/geometry/geometries/point.hpp>
 #include <boost/geometry/geometries/box.hpp>
 
 #include <boost/geometry/index/rtree.hpp>
@@ -61,7 +65,7 @@
     typedef std::pair<box, unsigned> value;
 
     // create the rtree using default constructor
-    bgi::rtree< value, bgi::quadratic<32, 8> > rtree;
+    bgi::rtree< value, bgi::quadratic<16, 4> > rtree;
 
     // create some values
     for ( unsigned i = 0 ; i < 10 ; ++i )
@@ -81,9 +85,15 @@
     std::vector<value> result_n;
     rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n));
 
+    // display results
+    std::cout << "spatial query box:" << std::endl;
+    std::cout << bg::wkt<box>(query_box) << std::endl;
     std::cout << "spatial query result:" << std::endl;
     BOOST_FOREACH(value const& v, result_s)
         std::cout << bg::wkt<box>(v.first) << " - " << v.second << std::endl;
+
+    std::cout << "knn query point:" << std::endl;
+    std::cout << bg::wkt<point>(point(0, 0)) << std::endl;
     std::cout << "knn query result:" << std::endl;
     BOOST_FOREACH(value const& v, result_n)
         std::cout << bg::wkt<box>(v.first) << " - " << v.second << std::endl;
@@ -96,6 +106,184 @@
 
+

+

+
#include <boost/geometry.hpp>
+#include <boost/geometry/geometries/point.hpp>
+#include <boost/geometry/geometries/box.hpp>
+#include <boost/geometry/geometries/polygon.hpp>
+
+#include <boost/geometry/index/rtree.hpp>
+
+#include <cmath>
+#include <vector>
+#include <iostream>
+#include <boost/foreach.hpp>
+
+namespace bg = boost::geometry;
+namespace bgi = boost::geometry::index;
+
+int main(void)
+{
+    typedef bg::model::point<float, 2, bg::cs::cartesian> point;
+    typedef bg::model::box<point> box;
+    typedef bg::model::polygon<point, false, false> polygon; // ccw, open polygon
+    typedef std::pair<box, unsigned> value;
+
+    // polygons
+    std::vector<polygon> polygons;
+
+    // create some polygons
+    for ( unsigned i = 0 ; i < 10 ; ++i )
+    {
+        // create a polygon
+        polygon p;
+        for ( float a = 0 ; a < 6.28316f ; a += 1.04720f )
+        {
+            float x = i + int(10*::cos(a))*0.1f;
+            float y = i + int(10*::sin(a))*0.1f;
+            p.outer().push_back(point(x, y));
+        }
+
+        // add polygon
+        polygons.push_back(p);
+    }
+
+    // display polygons
+    std::cout << "generated polygons:" << std::endl;
+    BOOST_FOREACH(polygon const& p, polygons)
+        std::cout << bg::wkt<polygon>(p) << std::endl;
+
+    // create the rtree using default constructor
+    bgi::rtree< value, bgi::rstar<16, 4> > rtree;
+
+    // fill the spatial index
+    for ( size_t i = 0 ; i < polygons.size() ; ++i )
+    {
+        // calculate polygon bounding box
+        box b = bg::return_envelope<box>(polygons[i]);
+        // insert new value
+        rtree.insert(std::make_pair(b, i));
+    }
+
+    // find values intersecting some area defined by a box
+    box query_box(point(0, 0), point(5, 5));
+    std::vector<value> result_s;
+    rtree.query(query_box, std::back_inserter(result_s));
+
+    // find 5 nearest values to a point
+    std::vector<value> result_n;
+    rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n));
+
+    // display results
+    std::cout << "spatial query box:" << std::endl;
+    std::cout << bg::wkt<box>(query_box) << std::endl;
+    std::cout << "spatial query result:" << std::endl;
+    BOOST_FOREACH(value const& v, result_s)
+        std::cout << bg::wkt<polygon>(polygons[v.second]) << std::endl;
+
+    std::cout << "knn query point:" << std::endl;
+    std::cout << bg::wkt<point>(point(0, 0)) << std::endl;
+    std::cout << "knn query result:" << std::endl;
+    BOOST_FOREACH(value const& v, result_n)
+        std::cout << bg::wkt<polygon>(polygons[v.second]) << std::endl;
+
+    return 0;
+}
+
+

+

+
+
+ +

+

+
#include <boost/geometry.hpp>
+#include <boost/geometry/geometries/point.hpp>
+#include <boost/geometry/geometries/box.hpp>
+#include <boost/geometry/geometries/polygon.hpp>
+
+#include <boost/geometry/index/rtree.hpp>
+
+#include <cmath>
+#include <vector>
+#include <iostream>
+#include <boost/foreach.hpp>
+#include <boost/shared_ptr.hpp>
+
+namespace bg = boost::geometry;
+namespace bgi = boost::geometry::index;
+
+int main(void)
+{
+    typedef bg::model::point<float, 2, bg::cs::cartesian> point;
+    typedef bg::model::box<point> box;
+    typedef bg::model::polygon<point, false, false> polygon; // ccw, open polygon
+    typedef boost::shared_ptr<polygon> shp;
+    typedef std::pair<box, shp> value;
+
+    // create the rtree using default constructor
+    bgi::rtree< value, bgi::linear<16, 4> > rtree;
+
+    std::cout << "filling index with polygons shared pointers:" << std::endl;
+
+    // create some polygons and fill the spatial index
+    for ( unsigned i = 0 ; i < 10 ; ++i )
+    {
+        // create a polygon
+        shp p(new polygon());
+        for ( float a = 0 ; a < 6.28316f ; a += 1.04720f )
+        {
+            float x = i + int(10*::cos(a))*0.1f;
+            float y = i + int(10*::sin(a))*0.1f;
+            p->outer().push_back(point(x, y));
+        }
+
+        // display new polygon
+        std::cout << bg::wkt<polygon>(*p) << std::endl;
+
+        // calculate polygon bounding box
+        box b = bg::return_envelope<box>(*p);
+        // insert new value
+        rtree.insert(std::make_pair(b, p));
+    }
+
+    // find values intersecting some area defined by a box
+    box query_box(point(0, 0), point(5, 5));
+    std::vector<value> result_s;
+    rtree.query(query_box, std::back_inserter(result_s));
+
+    // find 5 nearest values to a point
+    std::vector<value> result_n;
+    rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n));
+
+    // display results
+    std::cout << "spatial query box:" << std::endl;
+    std::cout << bg::wkt<box>(query_box) << std::endl;
+    std::cout << "spatial query result:" << std::endl;
+    BOOST_FOREACH(value const& v, result_s)
+        std::cout << bg::wkt<polygon>(*v.second) << std::endl;
+
+    std::cout << "knn query point:" << std::endl;
+    std::cout << bg::wkt<point>(point(0, 0)) << std::endl;
+    std::cout << "knn query result:" << std::endl;
+    BOOST_FOREACH(value const& v, result_n)
+        std::cout << bg::wkt<polygon>(*v.second) << std::endl;
+
+    return 0;
+}
+
+

+

+
+
+

diff --git a/doc/html/geometry_index/r_tree/rtree_quickstart.html b/doc/html/geometry_index/r_tree/rtree_quickstart.html index e0430d0a4..84f002c80 100644 --- a/doc/html/geometry_index/r_tree/rtree_quickstart.html +++ b/doc/html/geometry_index/r_tree/rtree_quickstart.html @@ -36,7 +36,7 @@

#include <boost/geometry.hpp>
-#include <boost/geometry/geometries/point_xy.hpp>
+#include <boost/geometry/geometries/point.hpp>
 #include <boost/geometry/geometries/box.hpp>
 
 #include <boost/geometry/index/rtree.hpp>
@@ -79,7 +79,7 @@
 

// create the rtree using default constructor
-bgi::rtree< value, bgi::quadratic<32, 8> > rtree;
+bgi::rtree< value, bgi::quadratic<16, 4> > rtree;
 

@@ -139,9 +139,15 @@

-
std::cout << "spatial query result:" << std::endl;
+
// display results
+std::cout << "spatial query box:" << std::endl;
+std::cout << bg::wkt<box>(query_box) << std::endl;
+std::cout << "spatial query result:" << std::endl;
 BOOST_FOREACH(value const& v, result_s)
     std::cout << bg::wkt<box>(v.first) << " - " << v.second << std::endl;
+
+std::cout << "knn query point:" << std::endl;
+std::cout << bg::wkt<point>(point(0, 0)) << std::endl;
 std::cout << "knn query result:" << std::endl;
 BOOST_FOREACH(value const& v, result_n)
     std::cout << bg::wkt<box>(v.first) << " - " << v.second << std::endl;
diff --git a/doc/html/index.html b/doc/html/index.html
index 30f0631c9..e1c19784f 100644
--- a/doc/html/index.html
+++ b/doc/html/index.html
@@ -51,7 +51,7 @@
 
- +

Last revised: February 23, 2013 at 04:28:38 GMT

Last revised: February 23, 2013 at 23:45:13 GMT


diff --git a/doc/imports.qbk b/doc/imports.qbk index 79c49edbd..68c57c29a 100644 --- a/doc/imports.qbk +++ b/doc/imports.qbk @@ -9,4 +9,6 @@ =============================================================================/] [import src/examples/rtree/quick_start.cpp] +[import src/examples/rtree/polygons_vector.cpp] +[import src/examples/rtree/polygons_shared_ptr.cpp] [import src/examples/rtree/interprocess.cpp] diff --git a/doc/rtree/examples.qbk b/doc/rtree/examples.qbk index 5691be531..917de425a 100644 --- a/doc/rtree/examples.qbk +++ b/doc/rtree/examples.qbk @@ -14,6 +14,14 @@ [rtree_quickstart] [endsect] +[section Index of polygons stored in vector] +[rtree_polygons_vector] +[endsect] + +[section Index of shared pointers to polygons] +[rtree_polygons_shared_ptr] +[endsect] + [section Interprocess] [rtree_interprocess] [endsect] diff --git a/doc/src/examples/rtree/Jamfile.v2 b/doc/src/examples/rtree/Jamfile.v2 index 57422ddc1..dcf90b575 100644 --- a/doc/src/examples/rtree/Jamfile.v2 +++ b/doc/src/examples/rtree/Jamfile.v2 @@ -1,6 +1,6 @@ # Boost.Geometry Index # -# Copyright (c) 2011-2012 Adam Wulkiewicz, Lodz, Poland. +# Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland. # # Use, modification and distribution is subject to the Boost Software License, # Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at @@ -8,6 +8,10 @@ exe quick_start : quick_start.cpp ; +exe polygons_vector : polygons_vector.cpp + ; +exe polygons_shared_ptr : polygons_shared_ptr.cpp + ; exe interprocess : interprocess.cpp /boost/thread//boost_thread : diff --git a/doc/src/examples/rtree/interprocess.cpp b/doc/src/examples/rtree/interprocess.cpp index e160d6ea6..1b59451b9 100644 --- a/doc/src/examples/rtree/interprocess.cpp +++ b/doc/src/examples/rtree/interprocess.cpp @@ -1,7 +1,9 @@ -// Boost.Geometry.Index Rtree interprocess example - -// Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland. - +// Boost.Geometry Index +// +// Quickbook Examples +// +// Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland. +// // Use, modification and distribution is subject to the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) diff --git a/doc/src/examples/rtree/polygons_shared_ptr.cpp b/doc/src/examples/rtree/polygons_shared_ptr.cpp new file mode 100644 index 000000000..210fb27ad --- /dev/null +++ b/doc/src/examples/rtree/polygons_shared_ptr.cpp @@ -0,0 +1,88 @@ +// Boost.Geometry Index +// +// Quickbook Examples +// +// Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland. +// +// Use, modification and distribution is subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +//[rtree_polygons_shared_ptr + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +namespace bg = boost::geometry; +namespace bgi = boost::geometry::index; + +int main(void) +{ + typedef bg::model::point point; + typedef bg::model::box box; + typedef bg::model::polygon polygon; // ccw, open polygon + typedef boost::shared_ptr shp; + typedef std::pair value; + + // create the rtree using default constructor + bgi::rtree< value, bgi::linear<16, 4> > rtree; + + std::cout << "filling index with polygons shared pointers:" << std::endl; + + // create some polygons and fill the spatial index + for ( unsigned i = 0 ; i < 10 ; ++i ) + { + // create a polygon + shp p(new polygon()); + for ( float a = 0 ; a < 6.28316f ; a += 1.04720f ) + { + float x = i + int(10*::cos(a))*0.1f; + float y = i + int(10*::sin(a))*0.1f; + p->outer().push_back(point(x, y)); + } + + // display new polygon + std::cout << bg::wkt(*p) << std::endl; + + // calculate polygon bounding box + box b = bg::return_envelope(*p); + // insert new value + rtree.insert(std::make_pair(b, p)); + } + + // find values intersecting some area defined by a box + box query_box(point(0, 0), point(5, 5)); + std::vector result_s; + rtree.query(query_box, std::back_inserter(result_s)); + + // find 5 nearest values to a point + std::vector result_n; + rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n)); + + // display results + std::cout << "spatial query box:" << std::endl; + std::cout << bg::wkt(query_box) << std::endl; + std::cout << "spatial query result:" << std::endl; + BOOST_FOREACH(value const& v, result_s) + std::cout << bg::wkt(*v.second) << std::endl; + + std::cout << "knn query point:" << std::endl; + std::cout << bg::wkt(point(0, 0)) << std::endl; + std::cout << "knn query result:" << std::endl; + BOOST_FOREACH(value const& v, result_n) + std::cout << bg::wkt(*v.second) << std::endl; + + return 0; +} + +//] \ No newline at end of file diff --git a/doc/src/examples/rtree/polygons_vector.cpp b/doc/src/examples/rtree/polygons_vector.cpp new file mode 100644 index 000000000..3fa6cb51f --- /dev/null +++ b/doc/src/examples/rtree/polygons_vector.cpp @@ -0,0 +1,96 @@ +// Boost.Geometry Index +// +// Quickbook Examples +// +// Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland. +// +// Use, modification and distribution is subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +//[rtree_polygons_vector + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +namespace bg = boost::geometry; +namespace bgi = boost::geometry::index; + +int main(void) +{ + typedef bg::model::point point; + typedef bg::model::box box; + typedef bg::model::polygon polygon; // ccw, open polygon + typedef std::pair value; + + // polygons + std::vector polygons; + + // create some polygons + for ( unsigned i = 0 ; i < 10 ; ++i ) + { + // create a polygon + polygon p; + for ( float a = 0 ; a < 6.28316f ; a += 1.04720f ) + { + float x = i + int(10*::cos(a))*0.1f; + float y = i + int(10*::sin(a))*0.1f; + p.outer().push_back(point(x, y)); + } + + // add polygon + polygons.push_back(p); + } + + // display polygons + std::cout << "generated polygons:" << std::endl; + BOOST_FOREACH(polygon const& p, polygons) + std::cout << bg::wkt(p) << std::endl; + + // create the rtree using default constructor + bgi::rtree< value, bgi::rstar<16, 4> > rtree; + + // fill the spatial index + for ( size_t i = 0 ; i < polygons.size() ; ++i ) + { + // calculate polygon bounding box + box b = bg::return_envelope(polygons[i]); + // insert new value + rtree.insert(std::make_pair(b, i)); + } + + // find values intersecting some area defined by a box + box query_box(point(0, 0), point(5, 5)); + std::vector result_s; + rtree.query(query_box, std::back_inserter(result_s)); + + // find 5 nearest values to a point + std::vector result_n; + rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n)); + + // display results + std::cout << "spatial query box:" << std::endl; + std::cout << bg::wkt(query_box) << std::endl; + std::cout << "spatial query result:" << std::endl; + BOOST_FOREACH(value const& v, result_s) + std::cout << bg::wkt(polygons[v.second]) << std::endl; + + std::cout << "knn query point:" << std::endl; + std::cout << bg::wkt(point(0, 0)) << std::endl; + std::cout << "knn query result:" << std::endl; + BOOST_FOREACH(value const& v, result_n) + std::cout << bg::wkt(polygons[v.second]) << std::endl; + + return 0; +} + +//] \ No newline at end of file diff --git a/doc/src/examples/rtree/quick_start.cpp b/doc/src/examples/rtree/quick_start.cpp index d64d38a29..f25b2383c 100644 --- a/doc/src/examples/rtree/quick_start.cpp +++ b/doc/src/examples/rtree/quick_start.cpp @@ -2,7 +2,7 @@ // // Quickbook Examples // -// Copyright (c) 2011-2012 Adam Wulkiewicz, Lodz, Poland. +// Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland. // // Use, modification and distribution is subject to the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at @@ -13,7 +13,7 @@ //[rtree_quickstart_include #include -#include +#include #include #include @@ -39,7 +39,7 @@ int main(void) //[rtree_quickstart_create // create the rtree using default constructor - bgi::rtree< value, bgi::quadratic<32, 8> > rtree; + bgi::rtree< value, bgi::quadratic<16, 4> > rtree; //] //[rtree_quickstart_insert @@ -67,9 +67,15 @@ int main(void) //] //[rtree_quickstart_output + // display results + std::cout << "spatial query box:" << std::endl; + std::cout << bg::wkt(query_box) << std::endl; std::cout << "spatial query result:" << std::endl; BOOST_FOREACH(value const& v, result_s) std::cout << bg::wkt(v.first) << " - " << v.second << std::endl; + + std::cout << "knn query point:" << std::endl; + std::cout << bg::wkt(point(0, 0)) << std::endl; std::cout << "knn query result:" << std::endl; BOOST_FOREACH(value const& v, result_n) std::cout << bg::wkt(v.first) << " - " << v.second << std::endl; diff --git a/include/boost/geometry/index/detail/rtree/rstar/insert.hpp b/include/boost/geometry/index/detail/rtree/rstar/insert.hpp index 58aba4038..896286be0 100644 --- a/include/boost/geometry/index/detail/rtree/rstar/insert.hpp +++ b/include/boost/geometry/index/detail/rtree/rstar/insert.hpp @@ -532,7 +532,7 @@ private: size_t m_relative_level; - Allocators m_allocators; + Allocators & m_allocators; }; }}} // namespace detail::rtree::visitors