简体   繁体   English

编译boost :: geometry多边形

[英]Compiling boost::geometry polygon

I'm trying to use boost::geometry polygon class to calculate intersections and their areas. 我正在尝试使用boost :: geometry多边形类来计算交叉点及其区域。 I have my own 3d piont class, with which I define a polygon. 我有自己的3d piont类,我用它定义了一个多边形。 However, when I try to use area() or intersection() I get many compilation errors. 但是,当我尝试使用area()或intersection()时,我会遇到很多编译错误。

I know I'm missing some definitions, or some extra REGISTER macros, but I don't know which. 我知道我缺少一些定义,或者一些额外的REGISTER宏,但我不知道哪个。

I'm using boost 1.54, VC++ 2010 express. 我正在使用boost 1.54,VC ++ 2010 express。

Below is a simple program which doesn't compile. 下面是一个不编译的简单程序。 How do I make it compile? 我如何编译?

#include <boost/geometry.hpp>
#include <boost/geometry/geometry.hpp> 
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/register/point.hpp> 
#include <boost/geometry/geometries/register/ring.hpp> 

struct MyPoint
{
    double x, y, z;
};

typedef boost::geometry::model::polygon<MyPoint> Polygon;

BOOST_GEOMETRY_REGISTER_POINT_3D(MyPoint, double, boost::geometry::cs::cartesian, x, y, z);
BOOST_GEOMETRY_REGISTER_RING(Polygon::ring_type);

inline void addPoint(Polygon& poly, double x, double y, double z)
{
    MyPoint p;
    p.x = x; p.y = y; p.z = z;
    boost::geometry::append(poly, p);
}

int main()
{
    Polygon poly1;
    addPoint(poly1, 2, 0, 0);
    addPoint(poly1, 2, 2, 0);
    addPoint(poly1, 0, 2, 0);
    addPoint(poly1, 0, 0, 0);
    addPoint(poly1, 2, 0, 0);

    Polygon poly2;
    addPoint(poly2, 3, 0, 0);
    addPoint(poly2, 3, 3, 0);
    addPoint(poly2, 0, 3, 0);
    addPoint(poly2, 0, 0, 0);
    addPoint(poly2, 3, 0, 0);

    Polygon intersectionPoly;
    boost::geometry::intersection(poly1, poly2, intersectionPoly);

    std::cout << "Intersection polygon area is " << boost::geometry::area(intersectionPoly) << '\n';
}

Compilation errors: 编译错误:

1>------ Build started: Project: TestPolygon, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\dev\externals\boost_1_54_0\boost\range\iterator.hpp(63): error C2039: 'type' : is not a member of 'boost::mpl::eval_if_c<C,F1,F2>'
1>          with
1>          [
1>              C=false,
1>              F1=boost::range_const_iterator<boost::geometry::model::polygon<MyPoint>>,
1>              F2=boost::range_mutable_iterator<Polygon>
1>          ]
1>          c:\dev\externals\boost_1_54_0\boost\range\value_type.hpp(30) : see reference to class template instantiation 'boost::range_iterator<C>' being compiled
1>          with
1>          [
1>              C=Polygon
1>          ]
1>          c:\dev\externals\boost_1_54_0\boost\geometry\algorithms\intersection.hpp(97) : see reference to class template instantiation 'boost::range_value<T>' being compiled
1>          with
1>          [
1>              T=Polygon
1>          ]
1>          c:\dev\externals\boost_1_54_0\boost\geometry\algorithms\intersection.hpp(201) : see reference to function template instantiation 'bool boost::geometry::dispatch::intersection<Geometry1,Geometry2>::apply<GeometryOut,boost::geometry::strategy_intersection<Tag,Geometry1,Geometry2,IntersectionPoint>>(const Geometry1 &,const Geometry2 &,GeometryOut &,const Strategy &)' being compiled
1>          with
1>          [
1>              Geometry1=Polygon,
1>              Geometry2=Polygon,
1>              GeometryOut=Polygon,
1>              Tag=boost::geometry::cartesian_tag,
1>              IntersectionPoint=MyPoint,
1>              Strategy=boost::geometry::strategy_intersection<boost::geometry::cartesian_tag,Polygon,Polygon,MyPoint>
1>          ]
1>          c:\test\main.cpp(45) : see reference to function template instantiation 'bool boost::geometry::intersection<Polygon,Polygon,Polygon>(const Geometry1 &,const Geometry2 &,GeometryOut &)' being compiled
1>          with
1>          [
1>              Geometry1=Polygon,
1>              Geometry2=Polygon,
1>              GeometryOut=Polygon
1>          ]
1>c:\dev\externals\boost_1_54_0\boost\range\value_type.hpp(30): error C3203: 'type' : unspecialized class template can't be used as a template argument for template parameter 'Iterator', expected a real type
1>c:\dev\externals\boost_1_54_0\boost\range\value_type.hpp(30): error C2955: 'boost::type' : use of class template requires template argument list
1>          c:\dev\externals\boost_1_54_0\boost\type.hpp(14) : see declaration of 'boost::type'
1>c:\dev\externals\boost_1_54_0\boost\range\value_type.hpp(31): error C2955: 'boost::iterator_value' : use of class template requires template argument list
1>          c:\dev\externals\boost_1_54_0\boost\iterator\iterator_traits.hpp(29) : see declaration of 'boost::iterator_value'
1>d:\program files\microsoft visual studio 10.0\vc\include\iterator(21): error C2039: 'const_reference' : is not a member of 'boost::geometry::model::polygon<Point>'
1>          with
1>          [
1>              Point=MyPoint
1>          ]
1>          c:\dev\externals\boost_1_54_0\boost\geometry\algorithms\intersection.hpp(103) : see reference to class template instantiation 'std::back_insert_iterator<_Container>' being compiled
1>          with
1>          [
1>              _Container=Polygon
1>          ]
1>d:\program files\microsoft visual studio 10.0\vc\include\iterator(21): error C2146: syntax error : missing ';' before identifier 'const_reference'
1>d:\program files\microsoft visual studio 10.0\vc\include\iterator(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\program files\microsoft visual studio 10.0\vc\include\iterator(21): error C2602: 'std::back_insert_iterator<_Container>::const_reference' is not a member of a base class of 'std::back_insert_iterator<_Container>'
1>          with
1>          [
1>              _Container=Polygon
1>          ]
1>          d:\program files\microsoft visual studio 10.0\vc\include\iterator(21) : see declaration of 'std::back_insert_iterator<_Container>::const_reference'
1>          with
1>          [
1>              _Container=Polygon
1>          ]
1>d:\program files\microsoft visual studio 10.0\vc\include\iterator(21): error C2868: 'std::back_insert_iterator<_Container>::const_reference' : illegal syntax for using-declaration; expected qualified-name
1>          with
1>          [
1>              _Container=Polygon
1>          ]
1>d:\program files\microsoft visual studio 10.0\vc\include\iterator(22): error C2039: 'value_type' : is not a member of 'boost::geometry::model::polygon<Point>'
1>          with
1>          [
1>              Point=MyPoint
1>          ]
1>d:\program files\microsoft visual studio 10.0\vc\include\iterator(29): error C2182: '_Val' : illegal use of type 'void'
1>d:\program files\microsoft visual studio 10.0\vc\include\iterator(35): error C2182: '_Val' : illegal use of type 'void'
1>c:\dev\externals\boost_1_54_0\boost\geometry\algorithms\intersection.hpp(103): error C2064: term does not evaluate to a function taking 4 arguments
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

There's a number of issues: 有很多问题:

  • Intersecting polygons results in a set of polygons ( MultiPolygon concept), not a single Polygon (this is why you got the compilation errors about missing range_value<Polygon> traits, among others) 相交多边形会产生一组多边形( MultiPolygon概念),而不是单个多边形(这就是为什么你得到关于缺少range_value<Polygon>特征等的编译错误的原因)
  • the area algorithm is only applicable to 2D coordinate systems. area算法仅适用于2D坐标系。 Therefore, it wouldn't compile with a assert_dimension<Ring, 2>() . 因此,它不会使用assert_dimension<Ring, 2>()编译。 I 'fixed' it by doing 我通过这样做来“修复”它

     struct MyPoint { double x, y/*, z*/; }; BOOST_GEOMETRY_REGISTER_POINT_2D(MyPoint, double, boost::geometry::cs::cartesian, x, y/*, z*/) 
  • finally, the polygons themselves weren't well-formed. 最后,多边形本身并不是很好。 You need to either make sure they are correct polygons as specified, or use the correct algorithm to fix that. 您需要确保它们是指定的正确多边形,或使用correct算法来修复它。

Here's a fixed/cleaned sample: Live on Coliru 这是一个固定/清洁的样本: 住在Coliru

#include <boost/geometry/geometry.hpp> 
#include <boost/geometry/geometries/register/point.hpp> 
#include <boost/geometry/geometries/register/ring.hpp> 

struct MyPoint
{
    double x, y/*, z*/;
};

BOOST_GEOMETRY_REGISTER_POINT_2D(MyPoint, double, boost::geometry::cs::cartesian, x, y/*, z*/)

typedef boost::geometry::model::polygon<MyPoint> Polygon;
BOOST_GEOMETRY_REGISTER_RING(Polygon::ring_type)

int main()
{
    using boost::geometry::append;
    using boost::geometry::correct;
    using boost::geometry::dsv;

    Polygon poly1;
    Polygon poly2;

    append(poly1, MyPoint {2, 0});
    append(poly1, MyPoint {2, 2});
    append(poly1, MyPoint {0, 2});
    append(poly1, MyPoint {0, 0});
    append(poly1, MyPoint {2, 0});

    append(poly2, MyPoint {3, 0});
    append(poly2, MyPoint {3, 3});
    append(poly2, MyPoint {0, 3});
    append(poly2, MyPoint {0, 0});
    append(poly2, MyPoint {3, 0});

    correct(poly1);
    correct(poly2);

    std::vector<Polygon> polys;
    if (boost::geometry::intersection(poly1, poly2, polys))
    {
        for (Polygon const& inter : polys)
            std::cout << "Intersection polygon area is " << boost::geometry::area(inter) << " with " << dsv(inter) << "\n";
    }
}

As a bonus, it prints the actual intersection polygon: 作为奖励,它打印实际的交叉点多边形:

Intersection polygon area is 4 with (((0, 0), (0, 2), (2, 2), (2, 0), (0, 0)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM