boostintersectionboost-geometry

Boost Geometry: segments intersection not yet implemented?


I am trying a simple test: compute the intersection of 2 segments with Boost Geometry. It does not compile. I also tried with some variations (int points instead of float points, 2D instead of 3D) with no improvement.

Is it really possible that boost doesn't implement segment intersection ? Or what did I do wrong ? Missing some hpp ? Confusion between algorithms "intersects" & "intersection" ?

The code is very basic:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/segment.hpp>
#include <boost/geometry/algorithms/intersection.hpp>

    typedef boost::geometry::model::point<float, 3, boost::geometry::cs::cartesian> testPoint;
    typedef boost::geometry::model::segment<testPoint> testSegment;

    testSegment s1(
        testPoint(-1.f, 0.f, 0.f),
        testPoint(1.f, 0.f, 0.f)
    );
    testSegment s2(
        testPoint(0.f, -1.f, 0.f),
        testPoint(0.f, 1.f, 0.f)
    );
    std::vector<testPoint> output;
    bool intersectionExists = boost::geometry::intersects(s1, s2, output);

But I got the following errors at compile time by Visual:

- Error C2039   'apply' n'est pas membre de 'boost::geometry::dispatch::disjoint<Geometry1,Geometry2,3,boost::geometry::segment_tag,boost::geometry::segment_tag,false>'    CDCadwork   C:\Program Files\Boost\boost_1_75_0\boost\geometry\algorithms\detail\disjoint\interface.hpp 54  
- Error C2338   This operation is not or not yet implemented.   CDCadwork   C:\Program Files\Boost\boost_1_75_0\boost\geometry\algorithms\not_implemented.hpp   47  

Solution

  • There are indeed two problems:

    1. you're intersecting 3D geometries. That's not implemented Instead you can do the same operation on a projection.

    2. you're passing an "output" geometry to intersects (which indeed only returns the true/false value as your chosen name intersectionExists suggested). In the presence of a third parameter, it would be used as a Strategy - a concept for which output obviously doesn't satisfy.

      Note intersection always returns true: What does boost::geometry::intersection return - although that's not part of the documented interface

    Since your geometries are trivially projected onto 2d plane Z=0:

    Live On Coliru

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point.hpp>
    #include <boost/geometry/geometries/segment.hpp>
    #include <iostream>
    
    namespace bg = boost::geometry;
    namespace bgm = bg::model;
    
    using Point   = bgm::point<float, 2, bg::cs::cartesian>;
    using Segment = bgm::segment<Point>;
    
    int main() {
        Segment s1{{-1, 0}, {1, 0}};
        Segment s2{{0, -1}, {0, 1}};
    
        bool exists = bg::intersects(s1, s2);
    
        std::vector<Point> output;
        /*bool alwaysTrue = */ bg::intersection(s1, s2, output);
    
        std::cout << bg::wkt(s1) << "\n";
        std::cout << bg::wkt(s2) << "\n";
        for (auto& p : output) {
            std::cout << bg::wkt(p) << "\n";
        }
    
        return exists? 0:1;
    }
    

    Prints

    LINESTRING(-1 0,1 0)
    LINESTRING(0 -1,0 1)
    POINT(0 0)