BOOST库的使用

前言

参加工作的时候需要用到boost库来处理图形,因此想写一下某些函数的使用方法,于人于己都方便。但是由于个人能力有限,所以有一些地方理解的较为片面还清多多谅解。如果能对你有所帮助,那就再好不过了。(注:英文目录后没带中文解释的都是还未探索的)

boost库的使用方法

Access Functions(访问方法)

get(获得点的坐标)

下面为我经常使用的点坐标的获得方法
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

namespace bg = boost::geometry;

int main()
{
    bg::model::d2::point_xy<double> point(1, 2);//初始化pointd的坐标为(1.0,2.0)

    double x = bg::get<0>(point);
    double y = bg::get<1>(point);

    std::cout << "x=" << x << " y=" << y << std::endl;

    return 0;
}
输出
	x=1 y=2
	
#include <iostream>
#include <boost/geometry.hpp>

namespace bg = boost::geometry;

int main()
{
    bg::model::point<double, 2, bg::cs::cartesian> point1;//构造一个笛卡尔坐标系下的二维点
    bg::model::point<double, 3, bg::cs::cartesian> point2(1.0, 2.0, 3.0);
    //构造一个笛卡尔坐标系下的三维点,并进行初始化

    bg::set<0>(point1, 1.0);//设置point1的横坐标
    point1.set<1>(2.0);//设置point1的纵坐标

    double x = bg::get<0>(point1);//获得point1的横坐标
    double y = point1.get<1>();//获得point1的纵坐标

    std::cout << x << ", " << y << std::endl;
    return 0;
}
输出
	1, 2

set(设置坐标)

设置点的坐标

下面为我经常使用的点坐标的设置方法
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

namespace bg = boost::geometry;

int main()
{
    bg::model::d2::point_xy<double> point;

    bg::set<0>(point, 1);
    bg::set<1>(point, 2);

    std::cout << "Location: " << bg::dsv(point) << std::endl;

    return 0;
}
输出
	Location: (1, 2)
#include <iostream>
#include <boost/geometry.hpp>

namespace bg = boost::geometry;

int main()
{
    bg::model::point<double, 2, bg::cs::cartesian> point1;//构造一个笛卡尔坐标系下的二维点
    bg::model::point<double, 3, bg::cs::cartesian> point2(1.0, 2.0, 3.0);
    //构造一个笛卡尔坐标系下的三维点,并进行初始化

    bg::set<0>(point1, 1.0);//设置point1的横坐标
    point1.set<1>(2.0);//设置point1的纵坐标

    double x = bg::get<0>(point1);//获得point1的横坐标
    double y = point1.get<1>();//获得point1的纵坐标

    std::cout << x << ", " << y << std::endl;
    return 0;
}
输出
	1, 2

设置线段

#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>

namespace bg = boost::geometry;

int main()
{
    typedef bg::model::point<double, 2, bg::cs::cartesian> point_t;
    typedef bg::model::segment<point_t> segment_t;

    segment_t seg1;
    segment_t seg2(point_t(0.0, 0.0), point_t(5.0, 5.0));

#ifndef BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX

    segment_t seg3{{0.0, 0.0}, {5.0, 5.0}};

#endif

    bg::set<0, 0>(seg1, 1.0);
    bg::set<0, 1>(seg1, 2.0);
    bg::set<1, 0>(seg1, 3.0);
    bg::set<1, 1>(seg1, 4.0);

    double x0 = bg::get<0, 0>(seg1);
    double y0 = bg::get<0, 1>(seg1);
    double x1 = bg::get<1, 0>(seg1);
    double y1 = bg::get<1, 1>(seg1);

    std::cout << x0 << ", " << y0 << ", " << x1 << ", " << y1 << std::endl;

    return 0;
}
输出
	1, 2, 3, 4

设置矩形

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

namespace bg = boost::geometry;

int main()
{
    bg::model::segment<bg::model::d2::point_xy<double> > segment;
    bg::set<bg::>(segment);
    bg::model::box<bg::model::d2::point_xy<double> > box;

    bg::set<bg::min_corner, 0>(box, 0);
    bg::set<bg::min_corner, 1>(box, 2);
    bg::set<bg::max_corner, 0>(box, 4);
    bg::set<bg::max_corner, 1>(box, 5);

    std::cout << "Extent: " << bg::dsv(box) << std::endl;

    return 0;
}
输出
	Extent: ((0, 2), (4, 5))

exterior_ring

interior_rings

Adapted models

C array

C++11 Array Container

Boost.Array

Boost.Fusion

Boost.Tuple

Boost.Polygon

Boost.Range

Macro’s for adaption

Algorithms(算法部分)

area(面积计算)

计算一个几何图形的面积

支持的几何图形:
	点、线段、矩形、直线、圆环、多边形、多点、多直线、多多边形、
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry;

int main()
{
    //计算多边形面积
    bg::model::polygon<bg::model::d2::point_xy<double> > poly;
    bg::read_wkt("POLYGON((0 0,0 7,4 2,2 0,0 0))", poly);
    double area = bg::area(poly);
    std::cout << "Area: " << area << std::endl;

    //计算球面多边形面积
    bg::model::polygon<bg::model::point<float, 2, bg::cs::spherical_equatorial<bg::degree> > > sph_poly;
    bg::read_wkt("POLYGON((0 0,0 45,45 0,0 0))", sph_poly);
    area = bg::area(sph_poly);
    std::cout << "Area: " << area << std::endl;

    return 0;
}
输出
	Area: 16
	Area: 0.339837

assign

append

azimuth(方位角计算)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;

    point_type p1(0, 0);
    point_type p2(1, 1);

    auto azimuth = boost::geometry::azimuth(p1, p2);

    std::cout << "azimuth: " << azimuth << std::endl;

    return 0;
}
输出
	azimuth: 0.785398

buffer

centroid(质心计算)

#include <iostream>
#include <list>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>


int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    typedef boost::geometry::model::polygon<point_type> polygon_type;

    polygon_type poly;
    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", poly);

    point_type p;
    boost::geometry::centroid(poly, p);

    std::cout << "centroid: " << boost::geometry::dsv(p) << std::endl;

    return 0;
}
输出
	centroid: (4.04663, 1.6349)

红色为图形,绿色为质心

clear(清除)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

#include <boost/assign.hpp>

int main()
{
    using boost::assign::tuple_list_of;

    typedef boost::tuple<float, float> point;
    typedef boost::geometry::model::polygon<point> polygon;
    typedef boost::geometry::model::ring<point> ring;

    polygon poly;

    //创建一个多边形
    poly.outer() = tuple_list_of(0, 0)(0, 9)(10, 10)(0, 0);
    poly.inners().push_back(tuple_list_of(1, 2)(4, 6)(2, 8)(1, 2));

    std::cout << boost::geometry::dsv(poly) << std::endl;
    boost::geometry::clear(poly);
    std::cout << boost::geometry::dsv(poly) << std::endl;

    //创建一个圆环
    ring r = tuple_list_of(0, 0)(0, 9)(8, 8)(0, 0);

    std::cout << boost::geometry::dsv(r) << std::endl;
    boost::geometry::clear(r);
    std::cout << boost::geometry::dsv(r) << std::endl;

    return 0;
}
输出
	(((0, 0), (0, 10), (11, 11), (0, 0)), ((0, 0), (0, 10), (11, 11), (0, 0)))
	(())
	((0, 0), (0, 9), (8, 8), (0, 0))
	()

closest_points

convert

convex_hull(最小凸多边形)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)


int main()
{
    namespace bg=boost::geometry;
    typedef bg::model::point<double, 2, bg::cs::cartesian> point;
    typedef bg::model::polygon<point> polygon;

    polygon poly;
    boost::geometry::read_wkt("polygon((2.0 1.3, 2.4 1.7, 2.8 1.8, 3.4 1.2, 3.7 1.6,3.4 2.0, 4.1 3.0"
        ", 5.3 2.6, 5.4 1.2, 4.9 0.8, 2.9 0.7,2.0 1.3))", poly);

    polygon hull;
    boost::geometry::convex_hull(poly, hull);

    using boost::geometry::dsv;
    std::cout
        << "polygon: " << dsv(poly) << std::endl
        << "hull: " << dsv(hull) << std::endl;


    return 0;
}
输出
	polygon: (((2, 1.3), (2.4, 1.7), (2.8, 1.8), (3.4, 1.2), (3.7, 1.6), (3.4, 2), (4.1, 3), (5.3, 2.6), (5.4, 1.2), (4.9, 0.8), (2.9, 0.7), (2, 1.3)))
	hull: (((2, 1.3), (2.4, 1.7), (4.1, 3), (5.3, 2.6), (5.4, 1.2), (4.9, 0.8), (2.9, 0.7), (2, 1.3)))

多边形和最小凸多边形

correct(纠正图形)

将图形修正成,外部顺时针,内部逆时针,对于凹多边形也适用。

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

using namespace std;
namespace bg = boost::geometry;

typedef bg::model::d2::point_xy<double> point_t;
typedef bg::model::segment<point_t> segment_t;
typedef bg::model::linestring<point_t> line_t;
typedef bg::model::polygon<point_t> polygon_t;
typedef bg::model::multi_point<point_t> mpoint_t;
typedef bg::model::multi_linestring<line_t> mline_t;
typedef bg::model::multi_polygon<polygon_t> mpolygon_t;
typedef bg::strategy::transform::rotate_transformer<bg::radian,double,2,2> rotate_t;

int main()
{

    polygon_t cwcp={{{0,0},{10,10},{0,9}},{{1,2},{4,6},{2,8},{1,2}}};

    double area_before = boost::geometry::area(cwcp);

    boost::geometry::correct(cwcp);

    double area_after = boost::geometry::area(cwcp);

    std::cout << boost::geometry::dsv(cwcp) << std::endl;
    std::cout << area_before << " -> " << area_after << std::endl;

    return 0;
}
输出
	(((0, 0), (0, 9), (10, 10), (0, 0)), ((1, 2), (4, 6), (2, 8), (1, 2)))
	-7 -> 38

纠正前
纠正后

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

using namespace std;
namespace bg = boost::geometry;

typedef bg::model::d2::point_xy<double> point_t;
typedef bg::model::segment<point_t> segment_t;
typedef bg::model::linestring<point_t> line_t;
typedef bg::model::polygon<point_t> polygon_t;
typedef bg::model::multi_point<point_t> mpoint_t;
typedef bg::model::multi_linestring<line_t> mline_t;
typedef bg::model::multi_polygon<polygon_t> mpolygon_t;
typedef bg::strategy::transform::rotate_transformer<bg::radian,double,2,2> rotate_t;

int main()
{

    polygon_t cwcp={{{0,0},{0,2},{-1,2},{-1,1},{-2,1},{-2,2},{-3,2},{-3,0}},{{-1.2,0.2},{-1.8,0.2},{-1.8,0.8},{-1.2,0.8}}};

    double area_before = boost::geometry::area(cwcp);

    // Correct it!
    boost::geometry::correct(cwcp);

    // Check its new area
    double area_after = boost::geometry::area(cwcp);

    // And output it
    std::cout << boost::geometry::dsv(cwcp) << std::endl;
    std::cout << area_before << " -> " << area_after << std::endl;

    return 0;
}
输出
	(((0, 0), (-3, 0), (-3, 2), (-2, 2), (-2, 1), (-1, 1), (-1, 2), (0, 2), (0, 0)), ((-1.2, 0.2), (-1.2, 0.8), (-1.8, 0.8), (-1.8, 0.2), (-1.2, 0.2)))
	-3.92 -> 4.64

在这里插入图片描述

在这里插入图片描述

covered_by(覆盖检测)

几何体线段矩形直线圆环多边形多点多直线多多边形
xxxxxxxx
线段xxxxxxxx
矩形xxxxxxx
直线xxxxxx
圆环xxx
多边形xxx
多点xxxxxxxx
多直线xxxxxx
多多边形xxx
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry;

int main()
{
    //检查poly1是否在poly2的内部或边界上
    bg::model::polygon<bg::model::d2::point_xy<double> > poly1;
    bg::read_wkt("POLYGON((0 2,0 3,2 4,1 2,0 2))", poly1);
    bg::model::polygon<bg::model::d2::point_xy<double> > poly2;
    bg::read_wkt("POLYGON((0 4,3 4,2 2,0 1,0 4))", poly2);
    bool check_covered = bg::covered_by(poly1, poly2);
    if (check_covered) {
         std::cout << "Covered: Yes" << std::endl;
    } else {
        std::cout << "Covered: No" << std::endl;
    }

    bg::model::polygon<bg::model::d2::point_xy<double> > poly3;
    bg::read_wkt("POLYGON((-1 -1,-3 -4,-7 -7,-4 -3,-1 -1))", poly3);
    check_covered = bg::covered_by(poly1, poly3);
    if (check_covered) {
         std::cout << "Covered: Yes" << std::endl;
    } else {
        std::cout << "Covered: No" << std::endl;
    }
    
    //两个图形重合也算在内部
    check_covered = bg::covered_by(poly1, poly1);
    if (check_covered) {
         std::cout << "Covered: Yes" << std::endl;
    } else {
        std::cout << "Covered: No" << std::endl;
    }

    return 0;
}
输出
	Covered: Yes
	Covered: No
	Covered: Yes

poly1为红色,poly2为绿色,poly3为蓝色

crosses(交叉检测)

几何体线段矩形直线圆环多边形多点多直线多多边形
xxxx
线段xxxxxxxxx
矩形xxxxxxxxx
直线xxxxxx
圆环xxxxxx
多边形xxxxxx
多点xxxxxxxxx
多直线xxxxxx
多多边形xxxxxx
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry;

int main()
{
    //检查两个几何图形是否相交
    bg::model::polygon<bg::model::d2::point_xy<double> > poly;
    bg::read_wkt("POLYGON((0 0,0 3,3 3,3 0,0 0))", poly);
    bg::model::linestring<bg::model::d2::point_xy<double> > line1;
    bg::read_wkt("LINESTRING(1 1,2 2,4 4)", line1);
    bool check_crosses = bg::crosses(poly, line1);
    if (check_crosses) {
         std::cout << "Crosses: Yes" << std::endl;
    } else {
        std::cout << "Crosses: No" << std::endl;
    }

    //;两个图形只接触不算交叉
    bg::model::linestring<bg::model::d2::point_xy<double> > line2;
    bg::read_wkt("LINESTRING(1 1,1 2,1 3)", line2);
    check_crosses = bg::crosses(poly, line2);
    if (check_crosses) {
         std::cout << "Crosses: Yes" << std::endl;
    } else {
        std::cout << "Crosses: No" << std::endl;
    }

    return 0;
}
输出
	Crosses: Yes
	Crosses: No

红色为poly,绿色为line1,蓝色为line2

densify(增加点的密度)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    typedef boost::geometry::model::polygon<point_type> polygon_type;

    polygon_type poly;
    boost::geometry::read_wkt(
        "POLYGON((0 0,0 10,10 10,10 0,0 0),(1 1,4 1,4 4,1 4,1 1))", poly);

    polygon_type res;

    boost::geometry::densify(poly, res, 1.5);

    std::cout << "densified: " << boost::geometry::wkt(res) << std::endl;

    return 0;
}
输出
	densified: POLYGON((0 0,0 1.42857,0 2.85714,0 4.28571,0 5.71429,0 7.14286,0 8.57143,0 10,1.42857 10,2.85714 10,4.28571 10,5.71429 10,7.14286 10,8.57143 10,10 10,10 8.57143,10 7.14286,10 5.71429,10 4.28571,10 2.85714,10 1.42857,10 0,8.57143 0,7.14286 0,5.71429 0,4.28571 0,2.85714 0,1.42857 0,0 0),(1 1,2 1,3 1,4 1,4 2,4 3,4 4,3 4,2 4,1 4,1 3,1 2,1 1))

增加密度前增加密度后

difference(计算两个几何图形的差)

#include <iostream>
#include <list>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

#include <boost/foreach.hpp>


int main()
{
    typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;

    polygon green, blue;

    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", green);

    boost::geometry::read_wkt(
        "POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 , 6.0 1.5 , 4.5 1.0 , 4.0 -0.5))", blue);

    std::list<polygon> output;
    boost::geometry::difference(green, blue, output);

    int i = 0;
    std::cout << "green - blue:" << std::endl;
    BOOST_FOREACH(polygon const& p, output)
    {
        std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
    }


    output.clear();
    boost::geometry::difference(blue, green, output);

    i = 0;
    std::cout << "blue - green:" << std::endl;
    BOOST_FOREACH(polygon const& p, output)
    {
        std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
    }


    return 0;
}
输出
	green - blue:
	0: 0.02375
	1: 0.542951
	2: 0.0149697
	3: 0.226855
	4: 0.839424

	blue - green:
	0: 0.525154
	1: 0.015
	2: 0.181136
	3: 0.128798
	4: 0.340083
	5: 0.307778

两个图形
绿色减去蓝色
蓝色减去绿色

discrete_frechet_distance

discrete_hausdorff_distance

disjoint(检查两个几何图形是否不相交)

几何体线段矩形直线圆环多边形多点多直线多多边形
线段
矩形
直线
圆环x
多边形x
多点xxx
多直线
多多边形x
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry;

int main()
{
    //检查两个几何是否不相交
    bg::model::polygon<bg::model::d2::point_xy<double> > poly1;
    bg::read_wkt("POLYGON((0 2,-2 0,-4 2,-2 4,0 2))", poly1);
    bg::model::polygon<bg::model::d2::point_xy<double> > poly2;
    bg::read_wkt("POLYGON((2 2,4 4,6 2,4 0,2 2))", poly2);
    bool check_disjoint = bg::disjoint(poly1, poly2);
    if (check_disjoint) {
         std::cout << "Disjoint: Yes" << std::endl;
    } else {
        std::cout << "Disjoint: No" << std::endl;
    }

    bg::model::polygon<bg::model::d2::point_xy<double> > poly3;
    bg::read_wkt("POLYGON((0 2,2 4,4 2,2 0,0 2))", poly3);
    check_disjoint = bg::disjoint(poly1, poly3);
    if (check_disjoint) {
         std::cout << "Disjoint: Yes" << std::endl;
    } else {
        std::cout << "Disjoint: No" << std::endl;
    }

    return 0;
}
输出
	Disjoint: Yes
	Disjoint: No

红色为poly1,绿色为poly2,蓝色为poly3

distance(计算两个几何图形之间的距离)

几何体线段矩形直线圆环多边形多点多直线多多边形
线段
矩形
直线
圆环
多边形
多点
多直线
多多边形
#include <iostream>
#include <list>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/multi_point.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>

#include <boost/foreach.hpp>

int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    typedef boost::geometry::model::polygon<point_type> polygon_type;
    typedef boost::geometry::model::linestring<point_type> linestring_type;
    typedef boost::geometry::model::multi_point<point_type> multi_point_type;

    point_type p(1,2);
    polygon_type poly;
    linestring_type line;
    multi_point_type mp;

    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", poly);
    line.push_back(point_type(0,0));
    line.push_back(point_type(0,3));
    mp.push_back(point_type(0,0));
    mp.push_back(point_type(3,3));

    std::cout
        << "Point-Poly: " << boost::geometry::distance(p, poly) << std::endl
        << "Point-Line: " << boost::geometry::distance(p, line) << std::endl
        << "Point-MultiPoint: " << boost::geometry::distance(p, mp) << std::endl;

    return 0;
}
输出
	Point-Poly: 1.22066
	Point-Line: 1
	Point-MultiPoint: 2.23607

envelope

equals(相等判断)

检查两个几何图形是否在空间上相等

几何体线段矩形直线圆环多边形多点多直线多多边形
xxxxxxxx
线段xxxxxxxxx
矩形xxxxxx
直线xxxxxxx
圆环xxxxxx
多边形xxxxx
多点xxxxxxxxx
多直线xxxxxxx
多多边形xxxxxxx
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

#include <boost/assign.hpp>

int main()
{
    using boost::assign::tuple_list_of;

    typedef boost::tuple<int, int> point;

    boost::geometry::model::polygon<point> poly1, poly2;
    boost::geometry::exterior_ring(poly1) = tuple_list_of(0, 0)(0, 5)(5, 5)(5, 0)(0, 0);
    boost::geometry::exterior_ring(poly2) = tuple_list_of(5, 0)(0, 0)(0, 5)(5, 5)(5, 0);

    std::cout
        << "polygons are spatially "
        << (boost::geometry::equals(poly1, poly2) ? "equal" : "not equal")
        << std::endl;

    boost::geometry::model::box<point> box;
    boost::geometry::assign_values(box, 0, 0, 5, 5);

    std::cout
        << "polygon and box are spatially "
        << (boost::geometry::equals(box, poly2) ? "equal" : "not equal")
        << std::endl;


    return 0;
}
输出
	polygons are spatially equal
	polygon and box are spatially equal

expand

for_each

intersection(计算两个几何图形的交点)

#include <iostream>
#include <deque>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

#include <boost/foreach.hpp>


int main()
{
    typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;

    polygon green, blue;

    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", green);

    boost::geometry::read_wkt(
        "POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 , 6.0 1.5 , 4.5 1.0 , 4.0 -0.5))", blue);

    std::deque<polygon> output;
    boost::geometry::intersection(green, blue, output);

    int i = 0;
    std::cout << "green && blue:" << std::endl;
    BOOST_FOREACH(polygon const& p, output)
    {
        std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
    }


    return 0;
}
输出
	green && blue:
	0: 2.50205

交集前在这里插入图片描述

intersects

is_empty

is_simple

is_valid

length

line_interpolate(计算直线上固定距离的点)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

using namespace boost::geometry;

int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    using segment_type = model::segment<point_type>;
    using linestring_type = model::linestring<point_type>;
    using multipoint_type = model::multi_point<point_type>;

    segment_type const s { {0, 0}, {1, 1} };//线段s
    linestring_type const l { {0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2} };//直线l
    point_type p;
    multipoint_type mp;

    std::cout << "point interpolation" << std::endl;

    line_interpolate(s, std::sqrt(2)/4, p);//计算距离起点(0,0)长度为√2的点的坐标,记录在点p中
    std::cout << "on segment : " << wkt(p) << std::endl;

    line_interpolate(l, 1.4, p);//计算距离起点(0,0)长度为1.4的点的坐标,记录在点p中
    std::cout << "on linestring : " << wkt(p) << std::endl << std::endl;

    std::cout << "multipoint interpolation" << std::endl;

    line_interpolate(s, std::sqrt(2)/4, mp);//计算距离起点(0,0)到终点(1,1)间距为√2的点的坐标,记录在点集mp中
    std::cout << "on segment : " << wkt(mp) << std::endl;

    mp=multipoint_type();
    line_interpolate(l, 1.4, mp);//计算距离起点(0,0)到终点(0,2)间距为√2的点的坐标,记录在点集mp中
    std::cout << "on linestring : " << wkt(mp) << std::endl;

    return 0;
}
输出
	point interpolation
	on segment : POINT(0.25 0.25)
	on linestring : POINT(1 0.4)
	
	multipoint interpolation
	on segment : MULTIPOINT((0.25 0.25),(0.5 0.5),(0.75 0.75),(1 1))
	on linestring : MULTIPOINT((1 0.4),(0.2 1))

初始状态,红色为s,绿色为l
新增蓝色点p在s上
点pi在l上
新增点集mp在s上
新增点集mp在l上

make

num_geometries

num_interior_rings

num_points

num_segments

overlaps(检查两个几何图形是否重叠)

几何体线段矩形直线圆环多边形多点多直线多多边形
xxxxxxx
线段xxxxxxxxx
矩形xxxxxxxx
直线xxxxxxx
圆环xxxxxx
多边形xxxxxx
多点xxxxxxx
多直线xxxxxxx
多多边形xxxxxx
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry;

int main()
{
    // Checks if the two geometries overlaps or not. 
    bg::model::polygon<bg::model::d2::point_xy<double> > poly1;
    bg::read_wkt("POLYGON((0 0,0 4,4 4,4 0,0 0))", poly1);
    bg::model::polygon<bg::model::d2::point_xy<double> > poly2;
    bg::read_wkt("POLYGON((2 2,2 6,6 7,6 1,2 2))", poly2);
    bool check_overlap = bg::overlaps(poly1, poly2);
    if (check_overlap) {
         std::cout << "Overlaps: Yes" << std::endl;
    } else {
        std::cout << "Overlaps: No" << std::endl;
    }

    bg::model::polygon<bg::model::d2::point_xy<double> > poly3;
    bg::read_wkt("POLYGON((-1 -1,-3 -4,-7 -7,-4 -3,-1 -1))", poly3);
    check_overlap = bg::overlaps(poly1, poly3);
    if (check_overlap) {
         std::cout << "Overlaps: Yes" << std::endl;
    } else {
        std::cout << "Overlaps: No" << std::endl;
    }

    return 0;
}
输出
	Overlaps: Yes
	Overlaps: No

红色为poly1,绿色为poly2,蓝色为poly3

perimeter(求周长)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry;

int main()
{
    // Calculate the perimeter of a cartesian polygon
    bg::model::polygon<bg::model::d2::point_xy<double> > poly;
    bg::read_wkt("POLYGON((0 0,3 4,5 -5,-2 -4, 0 0))", poly);
    double perimeter = bg::perimeter(poly);
    std::cout << "Perimeter: " << perimeter << std::endl;

    return 0;
}
输出
	Perimeter: 25.7627

relate

relation

reverse(改变几何体的顺逆时针)

几何体结果
什么都没有发生,几何形状不变
线段不支持旋转操作
矩形什么都没有发生,几何形状不变
直线方向变为反向
圆环方向变为反向
多边形外环和所有内环方向均变为反向
多点什么都没有发生,几何形状不变
多直线单独反转所有的线
多多边形单独反转所有的多边形
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

#include <boost/assign.hpp>

int main()
{
    using boost::assign::tuple_list_of;

    typedef boost::tuple<int, int> point;
    typedef boost::geometry::model::polygon<point> polygon;
    typedef boost::geometry::model::ring<point> ring;


    polygon poly;
    boost::geometry::exterior_ring(poly) = tuple_list_of(0, 0)(0, 9)(10, 10)(0, 0);
    boost::geometry::interior_rings(poly).push_back(tuple_list_of(1, 2)(4, 6)(2, 8)(1, 2));

    double area_before = boost::geometry::area(poly);
    boost::geometry::reverse(poly);
    double area_after = boost::geometry::area(poly);
    std::cout << boost::geometry::dsv(poly) << std::endl;
    std::cout << area_before << " -> " << area_after << std::endl;

    ring r = tuple_list_of(0, 0)(0, 9)(8, 8)(0, 0);

    area_before = boost::geometry::area(r);
    boost::geometry::reverse(r);
    area_after = boost::geometry::area(r);
    std::cout << boost::geometry::dsv(r) << std::endl;
    std::cout << area_before << " -> " << area_after << std::endl;

    return 0;
}
输出
	(((0, 0), (10, 10), (0, 9), (0, 0)), ((1, 2), (2, 8), (4, 6), (1, 2)))
	38 -> -38
	((0, 0), (8, 8), (0, 9), (0, 0))
	36 -> -36

simplify

sym_difference

touches(检查图形是否存在接触点)

touches (检测是否自交)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry;

int main()
{
    // Checks if the geometry has self-tangency.
    bg::model::polygon<bg::model::d2::point_xy<double> > poly1;
    bg::read_wkt("POLYGON((0 0,0 3,2 3,2 2,1 2,1 1,2 1,2 2,3 2,3 0,0 0))", poly1);
    bool check_touches = bg::touches(poly1);
    if (check_touches) {
         std::cout << "Touches: Yes" << std::endl;
    } else {
        std::cout << "Touches: No" << std::endl;
    }

    bg::model::polygon<bg::model::d2::point_xy<double> > poly2;
    bg::read_wkt("POLYGON((0 0,0 4,4 4,4 0,2 3,0 0))", poly2);
    check_touches = bg::touches(poly2);
    if (check_touches) {
         std::cout << "Touches: Yes" << std::endl;
    } else {
        std::cout << "Touches: No" << std::endl;
    }

    return 0;
}
输出
	Touches: Yes
	Touches: No

红色为poly1绿色为poly2

touches (检查两个几何图形是否至少有一个接触点)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry;

int main()
{
    //检查两个几何图形是否接触
    bg::model::polygon<bg::model::d2::point_xy<double> > poly1;
    bg::read_wkt("POLYGON((0 0,0 4,4 4,4 0,0 0))", poly1);
    bg::model::polygon<bg::model::d2::point_xy<double> > poly2;
    bg::read_wkt("POLYGON((0 0,0 -4,-4 -4,-4 0,0 0))", poly2);
    bool check_touches = bg::touches(poly1, poly2);
    if (check_touches) {
         std::cout << "Touches: Yes" << std::endl;
    } else {
        std::cout << "Touches: No" << std::endl;
    }

    bg::model::polygon<bg::model::d2::point_xy<double> > poly3;
    bg::read_wkt("POLYGON((1 1,0 -4,-4 -4,-4 0,1 1))", poly3);
    check_touches = bg::touches(poly1, poly3);
    if (check_touches) {
         std::cout << "Touches: Yes" << std::endl;
    } else {
        std::cout << "Touches: No" << std::endl;
    }

    return 0;
}
输出
	Touches: Yes
	Touches: No

红色为poly1,绿色为poly2红色为poly1,蓝色为poly3

transform

union_(叠加两个几何图形)

#include <iostream>
#include <vector>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

#include <boost/foreach.hpp>


int main()
{
    typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;

    polygon green, blue;

    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", green);

    boost::geometry::read_wkt(
        "POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 , 6.0 1.5 , 4.5 1.0 , 4.0 -0.5))", blue);

    std::vector<polygon> output;
    boost::geometry::union_(green, blue, output);

    int i = 0;
    std::cout << "green || blue:" << std::endl;
    BOOST_FOREACH(polygon const& p, output)
    {
        std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
    }


    return 0;
}
输出
	green || blue:
	0: 5.64795

叠加前的图形叠加后的图形

unique(计算几何图形的最小集合)

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

int main()
{
    boost::geometry::model::polygon<boost::tuple<double, double> > poly;
    boost::geometry::read_wkt("POLYGON((0 0,0 0,0 5,5 5,5 5,5 5,5 0,5 0,0 0,0 0,0 0,0 0))", poly);
    boost::geometry::unique(poly);//去掉重复的点
    std::cout << boost::geometry::wkt(poly) << std::endl;

    return 0;
}
输出
	POLYGON((0 0,0 5,5 5,5 0,0 0))

within(检查第一个几何体是否完全在第二个几何体内部)

几何体线段矩形直线圆环多边形多点多直线多多边形
xxxxxxxx
线段xxxxxxxx
矩形xxxxxxx
直线xxxxxx
圆环xxx
多边形xxx
多点xxxxxxxx
多直线xxxxxx
多多边形xxx
#include <iostream>
#include <list>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>


int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    typedef boost::geometry::model::polygon<point_type> polygon_type;

    polygon_type poly;
    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", poly);

    point_type p(4, 1);

    std::cout << "within: " << (boost::geometry::within(p, poly) ? "yes" : "no") << std::endl;

    return 0;
}
输出
	within: yes

在这里插入图片描述

Arithmetic(计算部分)

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

Concepts(概念部分)

Point Concept(点的概念)

每一个点类型,必须包含下述部分
点的类型(是点类型,而不是圆环、多边形等)
点的坐标类型,如 :(1,2) (1.1,5.0)
点的坐标系,如:笛卡尔坐标系、球面坐标系等
点的维度
点坐标的设置函数
点坐标的获取函数

Linestring Concept

Polygon Concept

MultiPoint Concept

MultiLinestring Concept

MultiPolygon Concept

Box Concept

Ring Concept

Segment Concept

Constants

min_corner

max_corner

Coordinate Systems

cs::cartesian

cs::spherical

cs::spherical_equatorial

cs::geographic

Core Metafunctions(核心元函数)

closure

coordinate_system

coordinate_type

cs_tag

degree

dimension

interior_type

point_order

point_type

radian

ring_type

tag

tag_cast

DE-9IM

de9im::mask
de9im::matrix
de9im::static_mask

Enumerations

buffer_side_selector
closure_selector
join_selector
order_selector
piece_type
result_code
validity_failure_type

Exceptions

exception
centroid_exception

IO (input/output)

DSV (Delimiter-Separated Values)
WKT (Well-Known Text)
SVG (Scalable Vector Graphics)

Iterators

closing_iterator
ever_circling_iterator

Models

model::point
model::d2::point_xy
model::d3::point_xyz
model::linestring
model::polygon
model::multi_point
model::multi_linestring
model::multi_polygon
model::box
model::ring
model::segment
model::referring_segment

Spatial Indexes

boost::geometry::index::rtree
R-tree free functions (boost::geometry::index::)
R-tree parameters (boost::geometry::index::)
Observers (boost::geometry::index::)
Inserters (boost::geometry::index::)
Adaptors (boost::geometry::index::adaptors::)
Predicates (boost::geometry::index::)

SRS

srs::spheroid

Strategies

strategy::area::cartesian
strategy::area::spherical
strategy::area::geographic
strategy::buffer::join_round
strategy::buffer::join_miter
strategy::buffer::end_round
strategy::buffer::end_flat
strategy::buffer::distance_symmetric
strategy::buffer::distance_asymmetric
strategy::buffer::point_circle
strategy::buffer::point_square
strategy::buffer::geographic_point_circle
strategy::buffer::side_straight
strategy::centroid::average
strategy::centroid::bashein_detmer
strategy::densify::cartesian
strategy::densify::geographic
strategy::densify::spherical
strategy::distance::pythagoras
strategy::distance::pythagoras_box_box
strategy::distance::pythagoras_point_box
strategy::distance::haversine
strategy::distance::projected_point
strategy::distance::cross_track
strategy::distance::cross_track_point_box
strategy::line_interpolate::cartesian
strategy::line_interpolate::geographic
strategy::line_interpolate::spherical
strategy::side::side_by_triangle
strategy::side::side_by_cross_track
strategy::side::spherical_side_formula
strategy::side::geographic
strategy::simplify::douglas_peucker
strategy::transform::inverse_transformer
strategy::transform::map_transformer

strategy::transform::rotate_transformer(直角坐标系下的旋转)

红色l1,绿色l2,蓝色l3。
l3为l2绕原点旋转l1的角度

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

using namespace std;
namespace bg=boost::geometry;
typedef bg::model::d2::point_xy<double> point_t;
typedef bg::model::segment<point_t> segment_t;
typedef bg::model::linestring<point_t> line_t;
typedef bg::model::polygon<point_t> polygon_t;
typedef bg::model::multi_polygon<polygon_t> mpolygon_t;
typedef bg::strategy::transform::rotate_transformer<boost::geometry::radian, double, 2UL, 2UL> rotate_t;

int main()
{
    line_t l1={{0,0},{1,1}},l2={{2,0},{3,1}},l3;
    rotate_t rotate(0);
    rotate={std::atan2(l1[1].y()-l1[0].y(),l1[1].x()-l1[0].x())};
    bg::transform(l2,l3,rotate);
    return 0;
}

旋转结果

strategy::transform::scale_transformer
strategy::transform::translate_transformer
strategy::transform::matrix_transformer
strategy::within::winding
strategy::within::franklin
strategy::within::crossings_multiply

Views

box_view
segment_view
closeable_view
reversible_view
identity_view
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值