在boost几何中创建实心多边形

时间:2017-11-15 16:17:15

标签: c++ boost geometry boost-geometry

我是提升几何的新手,我用## EXPIRES CACHING ## <IfModule mod_expires.c> ExpiresActive On ExpiresByType image/jpg "access plus 1 year" ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/gif "access plus 1 year" ExpiresByType image/png "access plus 1 year" ExpiresByType text/css "access plus 1 month" ExpiresByType application/pdf "access plus 1 month" ExpiresByType text/x-javascript "access plus 1 month" ExpiresByType application/x-shockwave-flash "access plus 1 month" ExpiresByType image/x-icon "access plus 1 year" ExpiresDefault "access plus 2 days" </IfModule> ## EXPIRES CACHING ## 创建了多边形。但我只创建该多边形的外部和内部是空的。所以我尝试用两个多边形测试boost::geometry::assign_points() A,B和A在B里面,结果不重叠。

那么,我该怎么做才能创建实心多边形(只知道多边形的外点和多边形内部是否有效)?

1 个答案:

答案 0 :(得分:0)

多边形 按照定义 为实体,直到您减去内圈。来自标准¹6.1.11.1:

  

多边形是由1个外部边界和0个或更多内部边界定义的平面表面。每个内部   边界在多边形中定义了一个洞。三角形是一个多边形,有3个不同的非共线顶点,没有   内部边界。 ¹

重叠并不意味着你的意思。

来自§6.1.15.3(基于DE-9IM的命名空间关系谓词)

  • 十字架enter image description here
  • enter image description here
  • 重叠enter image description here

    定义为

    a.Overlaps(b) ⇔ ( dim(I(a)) = dim(I(b)) = dim(I(a) ∩ I(b)))
                     ∧ (a ∩ b ≠ a) ∧ (a ∩ b ≠ b)
    
  • 包含

    a.Contains(b) ⇔ b.Within(a)
    
  • 相交

    a.Intersects(b) ⇔ ! a.Disjoint(b) 
    

在您的情况下,您可能正在寻找!disjointwithincontainsintersection

<强> Live On Coliru

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

namespace bg = boost::geometry;

template <typename Geo> void debug(std::string name, Geo const& g) {
    std::string reason;
    std::cout << name << ": " << bg::dsv(g) << " " << bg::is_valid(g, reason) << ", '" << reason << "'\n"; 
}

template <typename Geo, typename F>
void both_ways(std::string name, Geo const& a, Geo const& b, F f) {
    std::cout << name << "(a, b) -> " << f(a,b) << "\n";
    std::cout << name << "(b, a) -> " << f(b,a) << "\n";
}

int main() {
    std::cout << std::boolalpha;

    using Pt = bg::model::d2::point_xy<int>;
    using Poly = bg::model::polygon<Pt>;
    using Multi = bg::model::multi_polygon<Poly>;

    Poly const a {{ { 0,0 }, { 0,3 }, { 3,3 }, { 3,0 }, { 0,0 }} };
    Poly const b {{ { 1,1 }, { 1,2 }, { 2,2 }, { 2,1 }, { 1,1 }} };

    debug("a", a);
    debug("b", b);

#define TEST(algo) both_ways(#algo, a, b, [](auto& a, auto& b) { return bg::algo(a, b); })
    TEST(overlaps);
    TEST(intersects);
    TEST(within);
    //TEST(contains); // contains(a,b) ⇔ within(b,a)
    //TEST(crosses); // not implemented for polygons
    TEST(disjoint);

    both_ways("intersection", a, b, [](auto& a, auto& b) {
        Multi c; 
        bg::intersection(a, b, c);
        return boost::lexical_cast<std::string>(bg::dsv(c));
    });
}

打印

a: (((0, 0), (0, 3), (3, 3), (3, 0), (0, 0))) true, 'Geometry is valid'
b: (((1, 1), (1, 2), (2, 2), (2, 1), (1, 1))) true, 'Geometry is valid'
overlaps(a, b) -> false
overlaps(b, a) -> false
intersects(a, b) -> true
intersects(b, a) -> true
within(a, b) -> false
within(b, a) -> true
disjoint(a, b) -> false
disjoint(b, a) -> false
intersection(a, b) -> ((((1, 1), (1, 2), (2, 2), (2, 1), (1, 1))))
intersection(b, a) -> ((((1, 1), (1, 2), (2, 2), (2, 1), (1, 1))))

¹OGC Simple Feature / Common架构