boost I
来源:互联网 发布:russell tovey知乎 编辑:程序博客网 时间:2024/05/18 19:19
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/index/rtree.hpp>
// to store queries results
#include <vector>
// just for output
#include <iostream>
#include <boost/foreach.hpp>
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
Typically you'll store e.g. std::pair<Box, MyGeometryId> in the R-tree. MyGeometryId will be some identifier of a complex Geometry stored in other container, e.g. index type of a Polygon stored in the vector or an iterator of list of Rings. To keep it simple to define Valuewe will use predefined Box and unsigned int.
通常状况下,将一个类似于std::pair<box, MyGeometryId>储存在R-tree中.MyGeometryId是一种复杂的几何标识符储存在其他容器中.例如存储在vector里的几何索引或者list的迭代器.为了使值简单我们用box并且类型为unsigned int
typedef bg::model::box<point> box;
typedef std::pair<box, unsigned> value;
R-tree may be created using various algorithm and parameters. You should choose the algorithm you'll find the best for your purpose. In this example we will use quadratic algorithm. Parameters are passed as template parameters. Maximum number of elements in nodes is set to 16.
R-tree可以通过各种算法和参数去建立.你必须去选择一个最好的算法去满足你的需求.这个例子我们将用二次的算法。参数作为模版参数.节点的最大参数值设置为16.
// create the rtree using default constructor
bgi::rtree< value, bgi::quadratic<16> > rtree;
Typically Values will be generated in a loop from e.g. Polygons stored in some other container. In this case Box objects will probably be created with geometry::envelope() function. But to keep it simple lets just generate some boxes manually and insert them into the R-tree by using insert() method.
典型的,例如用循环来生成将多边形的值存储在容器里。在这种情况下 box对象会用geometry::envelope() 来建立。但是为了简便,手动生成一些boxs并且用insert()方法将它们插入到r-tree中
// create some values
for ( unsigned i = 0 ; i < 10 ; ++i )
{
// create a box
box b(point(i, i), point(i + 0.5f, i + 0.5f));
// insert new value
rtree.insert(std::make_pair(b, i));
}
There are various types of spatial queries that may be performed, they can be even combined together in one call. For simplicity, we use the default one. The following query return values intersecting a box. The sequence of Values in the result is not specified.
有各种各样的可执行查询空间的方法,它们甚至可以联合起来去执行一次查询.简单起见,我们可以用它们其中一个默认方法.下面的查询会返回一个交叉的box的值.未指定值的顺序
// 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(bgi::intersects(query_box), std::back_inserter(result_s));
Other type of query is k-nearest neighbor search. It returns some number of values nearest to some point in space. The default knn query may be performed as follows. The sequence of Values in the result is not specified.
其他类型的查询是k-最近邻搜索。它返回在空间内一定数量内离它最近的某一点。默认knn查询可以如下进行。结果未指定顺序
// 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));
At the end we'll print results.
在最后我们把结果打印出来
// 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;
- boost I
- boost::iterator_adaptor (I)
- boost学习笔录-I
- 编译boost库 I
- boost::thread I
- Boost application performance using asynchronous I/O
- Boost application performance using asynchronous I/O-ref
- 客户端应用中的同步I/O boost asio
- 服务端应用中的同步I/O boost asio
- C/C++——++i、i+=1、i++之间的区别(附在学习boost时遇到的例子)
- Boost
- boost
- boost
- Boost
- boost
- boost
- Boost
- Boost
- CocoaPods安装和使用教程
- 计算机五大部件相关知识
- 林语堂:《醒觉·对人生的态度》
- Android14_SQLiteDatabase
- leetcode 题解 || Longest Common Prefix 问题
- boost I
- IP头结构和其它重要的结构
- FrameLayout里的TextView单击事件无反应
- 关于Java中对象的存储
- 区别存储过程中的硬解析和软解析
- Android 2D,3D处理
- session与cookie的区别与联系
- 【2015更新】修改Host文件,让你的Google跑起来 2015 Google hosts
- 16.3Sum Closest