Search code examples
c++boostgeometryboost-geometry

Create solid polygon in boost geometry


I'm newbie at boost geometry, I have created polygon with boost::geometry::assign_points(). But I only create outer and inner of that polygon is empty. So I try test boost::geometry::overlaps() with two polygons A, B and A is inside B, result is not overlaps.

So, What can I do to create solid polygon (only know outer point of polygon and inside of polygon is valid) ?


Solution

  • Polygons are by definition solid until you subtract inner rings. From §6.1.11.1 from the standard¹:

    A Polygon is a planar Surface defined by 1 exterior boundary and 0 or more interior boundaries. Each interior boundary defines a hole in the Polygon. A Triangle is a polygon with 3 distinct, non-collinear vertices and no interior boundary. ¹

    Overlapping doesn't mean what you think it means.

    From §6.1.15.3 (Named spatial relationship predicates based on the DE-9IM)

    • Crosses enter image description here
    • Within enter image description here
    • Overlaps enter image description here

      It is defined as

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

      a.Contains(b) ⇔ b.Within(a)
      
    • Intersects

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

    In your case you might be looking for !disjoint, within, contains or intersection:

    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));
        });
    }
    

    Which prints

    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))))
    

    ¹ The OGC Simple Feature / Common architecture