Boost.Geometry介绍
最在項目中要用到計算幾何的東西,計算三維空間中面片與六面體的相交判斷,通過各種搜索發(fā)現(xiàn)boost庫中的Geometry模塊還不錯,可以比較容易地實現(xiàn)。這里記錄一下這個庫的基本情況。
?
1、常見幾何對象
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/multi_point.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/multi_linestring.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/variant.hpp>
?
Boost.Geometry的model有point_xy,? point, multi_point, ,segment,linestring,multi_linestring, box,ring,polygon,multi_polygon, variant.
model::point
model::d2::point_xy
model::linestring
model::polygon
model::multi_point
model::multi_linestring
model::multi_polygon
model::box,
model::ring
model::segment
model::referring_segment
?
model::point
Basic point class, having coordinates defined in a neutral way.
Description
Defines a neutral point class, fulfilling the Point Concept. Library users can use this point class, or use their own point classes. This point class is used in most of the samples and tests of Boost.Geometry This point class is used occasionally within the library, where a temporary point class is necessary.
model::d2::point_xy
2D point in Cartesian coordinate system
?
model::linestring
A linestring (named so by OGC) is a collection (default a vector) of points.
?
model::polygon
The polygon contains an outer ring and zero or more inner rings.
?
2、常見算法
提供的算法有:面積、長度、周長、質(zhì)心、凸殼、交集(剪裁)、內(nèi)(多邊形中的點)、距離、包絡(luò)線(邊界框)、簡化、變換等。
area
assign
append
buffer
centroid
clear
convert
convex_hull
correct
covered_by
crosses
densify
difference
discrete_frechet_distance
discrete_hausdorff_distance
disjoint
distance
envelope
equals
expand
for_each
intersection
intersects
is_empty
is_simple
is_valid
length
make
num_geometries
num_interior_rings
num_points
num_segments
overlaps
perimeter
relate
relation
reverse
simplify
sym_difference
touches
transform
union_
unique
within
常見的有以下幾種:
計算面積
Boost::Geometry::area(obj1)
計算距離
Boost::Geometry::distance(obj1, obj2)
判斷是否相交
Boost::Geometry::intersects(obj1, obj2)
計算交點
Boost::Geometry::intersection(obj1, obj2, result)
判斷是否在box內(nèi)
Boost::Geometry::within(obj1, obj2)
?
?
3、boost.Geometry可以與VS的GraphicalDebugging插件可以配合使用,查看幾何圖形。
在調(diào)試過程中插入斷點,通過在GraphicalDebugging界面輸入對象的名字就可以查看幾何對象的形態(tài)。在視圖中的其他窗口可以找到剛剛安裝的插件的窗口,Geometry Watch,Graphical Watch,Plot Watch。
?
4、幾何圖形的輸入輸出
DSV (Delimiter-Separated Values)
WKT (Well-Known Text)
SVG (Scalable Vector Graphics)
前兩種是文本格式數(shù)據(jù)流,第三種是圖形化輸入和輸出。
?
5、幾何圖形的算術(shù)運算
add_point
add_value
assign_point
assign_value
cross_product
cross_product
divide_point
divide_value
dot_product
multiply_point
multiply_value
subtract_point
subtract_value
?
6、官方示例
https://www.boost.org/doc/libs/1_69_0/libs/geometry/doc/html/geometry/spatial_indexes/rtree_examples/index_of_polygons_stored_in_vector.html
?
#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()
{
??? typedef bg::model::point<float, 2, bg::cs::cartesian> point;? //define point 2 dimension
??? 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 ( unsigned 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(bgi::intersects(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));
?
??? // note: in Boost.Geometry the WKT representation of a box is polygon
?
??? // note: the values store the bounding boxes of polygons
??? // the polygons aren't used for querying but are printed
?
??? // 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;
}
總結(jié)
以上是生活随笔為你收集整理的Boost.Geometry介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode_Map、Set
- 下一篇: kiel 5中添加固件库的方式