Boost logo

Boost-Commit :

From: mariano.consoni_at_[hidden]
Date: 2008-06-05 11:00:04


Author: mconsoni
Date: 2008-06-05 11:00:04 EDT (Thu, 05 Jun 2008)
New Revision: 46168
URL: http://svn.boost.org/trac/boost/changeset/46168

Log:
- new example/test showing that it's possible to use any point as key of the index.

Added:
   sandbox/SOC/2008/spacial_indexing/libs/spatial_index/test/custom_point_test.cpp (contents, props changed)
Text files modified:
   sandbox/SOC/2008/spacial_indexing/libs/spatial_index/test/Jamfile | 1 +
   1 files changed, 1 insertions(+), 0 deletions(-)

Modified: sandbox/SOC/2008/spacial_indexing/libs/spatial_index/test/Jamfile
==============================================================================
--- sandbox/SOC/2008/spacial_indexing/libs/spatial_index/test/Jamfile (original)
+++ sandbox/SOC/2008/spacial_indexing/libs/spatial_index/test/Jamfile 2008-06-05 11:00:04 EDT (Thu, 05 Jun 2008)
@@ -13,5 +13,6 @@
     ;
 
 run simple_test.cpp ;
+run custom_point_test.cpp ;
 run random_test.cpp ;
 run performance_test.cpp ;

Added: sandbox/SOC/2008/spacial_indexing/libs/spatial_index/test/custom_point_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2008/spacial_indexing/libs/spatial_index/test/custom_point_test.cpp 2008-06-05 11:00:04 EDT (Thu, 05 Jun 2008)
@@ -0,0 +1,158 @@
+// Copyright 2008 Federico J. Fernandez.
+// Distributed under 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)
+
+
+#include <boost/spatial_index.hpp>
+
+#include <boost/test/included/test_exec_monitor.hpp>
+#include <boost/shared_ptr.hpp>
+
+#include <geometry/geometry.hpp>
+
+#include <vector>
+#include <string>
+
+
+#include <boost/tuple/tuple.hpp>
+#include <geometry/distance.hpp>
+#include <geometry/aswkt.hpp>
+
+
+// we define our own point
+struct my_2d_point: boost::tuple<float, float>
+{
+ typedef float coordinate_type;
+ enum { coordinate_count = 2 };
+
+ my_2d_point(void)
+ {
+ get<0>() = 0.0;
+ get<1>() = 0.0;
+
+ }
+ my_2d_point(float x, float y)
+ {
+ get<0>() = x;
+ get<1>() = y;
+ }
+
+ bool operator<(const my_2d_point &o) const { return get<0>() < o.get<0>(); }
+
+ // Because the geometry::point concept shares the "get" methods with boost::tuple,
+ // no more methods are needed.
+};
+
+
+namespace geometry {
+
+ template <>
+ struct strategy_traits<my_2d_point, my_2d_point>
+ {
+ typedef strategy::distance::pythagoras<my_2d_point, my_2d_point> point_distance;
+ typedef strategy::not_implemented point_segment_distance;
+ typedef strategy::not_implemented area;
+ typedef strategy::not_implemented within;
+ typedef strategy::not_implemented centroid;
+ typedef strategy::not_implemented envelope;
+ };
+
+ template <>
+ struct wkt_traits<my_2d_point>
+ {
+ typedef impl::wkt::stream_point<my_2d_point> stream_type;
+ };
+
+} // namespace geometry
+
+
+
+template <typename CH, typename TR>
+inline std::basic_ostream<CH,TR>& operator<<(std::basic_ostream<CH,TR> &os, const my_2d_point &p)
+{
+ os << geometry::as_wkt<my_2d_point>(p);
+ return os;
+}
+
+
+
+int test_main(int, char* [])
+{
+ std::vector<std::string> data;
+ std::vector< my_2d_point > points;
+
+ data.push_back("test0");
+ data.push_back("test1");
+ data.push_back("test2");
+ data.push_back("test3");
+ data.push_back("test4");
+ data.push_back("test5");
+
+ points.push_back(my_2d_point(1.0,1.0));
+ points.push_back(my_2d_point(2.0,1.0));
+ points.push_back(my_2d_point(5.0,5.0));
+ points.push_back(my_2d_point(1.0,6.0));
+ points.push_back(my_2d_point(9.0,9.0));
+ points.push_back(my_2d_point(9.0,8.0));
+
+ geometry::box<my_2d_point > b(my_2d_point(0.0, 0.0), my_2d_point(20.0, 20.0));
+
+ boost::shared_ptr< boost::spatial_index::spatial_index< my_2d_point , std::vector<std::string>::iterator > >
+ q(new boost::spatial_index::quadtree< my_2d_point , std::vector<std::string>::iterator >(b));
+
+// std::cerr << " --> bulk insert" << std::endl;
+// std::vector<std::string>::iterator b, e;
+// b = data.begin();
+// e = data.end();
+// q->bulk_insert(b,e, points);
+
+ std::vector<std::string>::iterator it = data.begin();
+
+ std::cerr << " --> insert" << std::endl;
+ q->insert(my_2d_point(1.0,1.0), it++);
+ std::cerr << " --> insert" << std::endl;
+ q->insert(my_2d_point(2.0,1.0), it++);
+ std::cerr << " --> insert" << std::endl;
+ q->insert(my_2d_point(5.0,5.0), it++);
+ std::cerr << " --> insert" << std::endl;
+ q->insert(my_2d_point(1.0,6.0), it++);
+ std::cerr << " --> insert" << std::endl;
+ q->insert(my_2d_point(9.0,9.0), it++);
+ std::cerr << " --> insert" << std::endl;
+ q->insert(my_2d_point(9.0,8.0), it++);
+
+
+ std::vector<std::string>::iterator it1;
+
+ std::cerr << " --> find" << std::endl;
+ it1 = q->find(my_2d_point(9.0,9.0));
+ BOOST_CHECK_EQUAL(*it1, "test4");
+ std::cout << " " << *it1 << std::endl;
+
+ std::cerr << " --> find" << std::endl;
+ it1 = q->find(my_2d_point(5.0,5.0));
+ BOOST_CHECK_EQUAL(*it1, "test2");
+ std::cout << " " << *it1 << std::endl;
+
+ // expected result
+ std::vector<std::string> res;
+ res.push_back("test0");
+ res.push_back("test1");
+ res.push_back("test2");
+
+ std::cerr << "Elements: " << q->elements() << std::endl;
+ BOOST_CHECK_EQUAL(q->elements(), 6u);
+
+ std::cerr << " --> find rectangle" << std::endl;
+ geometry::box<my_2d_point > query_box(my_2d_point(0.0, 0.0), my_2d_point(5.0, 5.0));
+ std::deque< std::vector<std::string>::iterator > d = q->find(query_box);
+ BOOST_CHECK_EQUAL(d.size(), 3u);
+ unsigned int i = 0;
+ for(std::deque< std::vector<std::string>::iterator >::const_iterator dit = d.begin(); dit != d.end(); ++dit) {
+ std::cerr << "Value: " << *(*dit) << std::endl;
+ BOOST_CHECK_EQUAL(*(*dit), res[i++]);
+ }
+
+ return 0;
+};


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk