boost 几何运算函数初步

Introduction

Boost.Geometry (aka Generic Geometry Library, GGL), part of collection of the Boost C++ Libraries, defines concepts, primitives and algorithms for solving geometry problems.

Boost.Geometry contains a dimension-agnostic, coordinate-system-agnostic and scalable kernel, based on concepts, meta-functions and tag dispatching. On top of that kernel, algorithms are built: area, length, perimeter, centroid, convex hull, intersection (clipping), within (point in polygon), distance, envelope (bounding box), simplify, transform, and much more. The library supports high precision arithmetic numbers, such as ttmath.

Boost.Geometry contains instantiable geometry classes, but library users can also use their own. Using registration macros or traits classes their geometries can be adapted to fulfil Boost.Geometry concepts.

Boost.Geometry might be used in all domains where geometry plays a role: mapping and GIS, game development, computer graphics and widgets, robotics, astronomy and more. The core is designed to be as generic as possible and support those domains. For now, the development has been mostly GIS-oriented.

The library follows existing conventions:

  • conventions from boost
  • conventions from the std library
  • conventions and names from one of the OGC standards on geometry and, more specificly, from the OGC Simple Feature Specification
  • The library was first released with Boost 1.47.0 and from that point on it is officially part of the Boost C++ Libraries.

    Latest stable version of the source code is included in the Boost packaged releases. It can also be downloaded from the Boost GitHub repository (master branch).

    The library development upstream is available from the Boost.Geometry (develop branch).

    Note that the library extensions are not distributed in the official Boost releases, but only available in the Boost.Geometry (develop branch) and that they are subject to change.

    Boost.Geometry was accepted by Boost at November 28, 2009 (review report).

    There is a Boost.Geometry mailing list. The mailing list and its messages are also accessible from Nabble as Boost Geometry. Also on the Boost Developers list and on the Boost Users list Boost.Geometry is discussed.

  • 编程示例

  • 我编译的版本是boost_1_69_0,应当修复了许多以前的bug,编程工具采用Visual Studio 2015,从此次初步实现上来看,效果还是不错的,当初为了实现多边形相交,费尽心血还不能应对所有情况,看来思路要变。以下是示例代码和注释

  • #include "stdafx.h"
    #include "iostream"

    #include <boost/assign.hpp>
    #include <boost/geometry/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    #include <boost/geometry/geometries/linestring.hpp>
    #include <boost/geometry/geometries/box.hpp>
    #include <boost/geometry/geometries/ring.hpp>
    #include <boost/geometry/geometries/polygon.hpp>
    namespace bg = boost::geometry;
    typedef bg::model::d2::point_xy<double> DPoint; //点
    typedef bg::model::segment<DPoint> DSegment; //线段
    typedef bg::model::linestring<DPoint> DLineString; //曲线段
    typedef bg::model::box<DPoint> DBox; //矩形区域
    //这里的ring就是我们通常说的多边形闭合区域(内部不存在缕空),模板参数为true,表示顺时针存储点,为false,表示逆时针存储点,由于MM_TEXT坐标系与传统上的坐标系的Y轴方向是相反的,所以最后为false,将TopLeft、TopRight、BottomRight、BottomLeft、TopLeft以此存储到ring中,以便能正确计算
    typedef bg::model::ring<DPoint, false> DRing;
    //polygon模板参数false,也是由上面相同的原因得出来的
    typedef bg::model::polygon<DPoint, false> DPolygon;

    using namespace std;

    int main()
    {
        DPoint pt0(100, 100);
        DPoint pt1(200, 200);
        DSegment sg0(pt0, pt1);

        double dDistance = 0;

        //1、点到点的距离
        dDistance = bg::distance(pt0, pt1);
        //2、点到线段的距离,如果点到直线的垂足不在线段上,
        //所计算的距离为(点到直线的距离x、线段到垂足延长的距离y,distance=sqrt(x^2+y^2))
        dDistance = bg::distance(DPoint(200, 100), sg0);
        dDistance = bg::distance(DPoint(100, 0), sg0);

        //3、判断线段是否相交
        DSegment sg1(DPoint(0, 100), DPoint(100, 0));
        DSegment sg2(DPoint(100, 200), DPoint(200, 100));
        bool bIntersect = false;
        bIntersect = bg::intersects(sg0, sg1);
        bIntersect = bg::intersects(sg0, sg2);

        //4、求线段与线段的交点
        std::list<DPoint> lstPoints;
        bg::intersection(sg0, sg1, lstPoints);
        lstPoints.clear();
        bg::intersection(sg0, sg2, lstPoints);

        DBox rc2(DPoint(0, 0), DPoint(0, 0));

        //5、判断box是否相交
        DBox rc(DPoint(0, 0), DPoint(200, 200));
        DBox rc0(DPoint(250, 250), DPoint(450, 450));
        DBox rc1(DPoint(100, 100), DPoint(300, 300));

        bIntersect = bg::intersects(rc, rc0);
        bIntersect = bg::intersects(rc, rc1);

        //6、判断box是否与LineString相交
        DLineString line0;

        line0.push_back(DPoint(10, 250));
        line0.push_back(DPoint(100, 100));
        line0.push_back(DPoint(120, -10));
        line0.push_back(DPoint(210, 200));
        bIntersect = bg::intersects(rc, line0);
        bIntersect = bg::intersects(rc0, line0);

        //7、求box与linestring的交点
        std::list<DLineString> lstLines;
        bg::intersection(rc, line0, lstLines);

        //8、点是否在box内
        DBox rc7(DPoint(0, 0), DPoint(100, 100));
        bool bInside = false;
        bInside = bg::within(DPoint(50, 50), rc7);
        bInside = bg::within(DPoint(0, 0), rc7);

        //9、判断LineString与LineString是否相交
        DLineString line1, line2, line3;

        line1.push_back(DPoint(50, 50));
        line1.push_back(DPoint(150, 50));
        line1.push_back(DPoint(50, 200));
        line1.push_back(DPoint(150, 200));
        line2.push_back(DPoint(100, 0));
        line2.push_back(DPoint(70, 100));
        line2.push_back(DPoint(150, 210));
        line3.push_back(DPoint(200, 0));
        line3.push_back(DPoint(200, 200));

        bIntersect = bg::intersects(line1, line2);
        bIntersect = bg::intersects(line1, line3);

        //10、求LineString与LineString的交点
        lstPoints.clear();
        bg::intersection(line1, line2, lstPoints);
        lstPoints.clear();
        bg::intersection(line1, line3, lstPoints);


        //11、判断ring与ring是否相交
        DPoint arDPoint0[6] = { DPoint(0, 0), DPoint(100, 0), DPoint(200, 100), DPoint(100, 200), DPoint(0, 200), DPoint(0, 0) };
        DPoint arDPoint1[6] = { DPoint(100, 100), DPoint(200, 0), DPoint(300, 0), DPoint(300, 200), DPoint(200, 200), DPoint(100, 100) };
        DRing r0(arDPoint0, arDPoint0 + 6);
        DRing r1(arDPoint1, arDPoint1 + 6);
        bIntersect = bg::intersects(r0, r1);

        //12、求ring与ring的交点
        lstPoints.clear();
        bg::intersection(r0, r1, lstPoints);

        DPolygon poly1;
        DPolygon poly2;
        DPolygon poly3;

        auto lstOf = boost::assign::list_of(DPoint(0, 0))(DPoint(200, 0))(DPoint(200, 200))(DPoint(0, 200))(DPoint(0, 0));
        poly1.outer().assign(lstOf.begin(), lstOf.end());
        lstOf = boost::assign::list_of(DPoint(50, 50))(DPoint(150, 50))(DPoint(150, 150))(DPoint(50, 150))(DPoint(50, 50));
        poly1.inners().push_back(lstOf);
        lstOf = boost::assign::list_of(DPoint(100, 0))(DPoint(120, 0))(DPoint(120, 200))(DPoint(100, 200))(DPoint(100, 0));
        poly2.outer().assign(lstOf.begin(), lstOf.end());
        lstOf = boost::assign::list_of(DPoint(100, 60))(DPoint(120, 60))(DPoint(120, 140))(DPoint(100, 140))(DPoint(100, 60));
        poly3.outer().assign(lstOf.begin(), lstOf.end());

        //13、判断polygon与polygon是否相交
        bIntersect = bg::intersects(poly1, poly2);
        bIntersect = bg::intersects(poly1, poly3);

        //14、求polygon与polygon相交的区域
        std::list<DPolygon> lstPolygon;

        bg::intersection(poly1, poly2, lstPolygon);
        lstPolygon.clear();
        bg::intersection(poly1, poly3, lstPolygon);

        //15、判断点是否在polygon内
        bInside = bg::within(DPoint(100, 100), poly1);
        bInside = bg::within(DPoint(25, 25), poly1);

        return 0;
    }

  •  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值