Boost Geometry:段交集尚未实施?

问题描述

我正在尝试一个简单的测试:计算 2 个段与 Boost Geometry 的交集。它不编译。我还尝试了一些变化(整数点而不是浮点数,2D 而不是 3D)但没有任何改进。

boost 真的有可能不实现段交叉吗?或者我做错了什么?缺少一些 hpp 吗? 混淆算法“相交”和“相交”?

代码非常基础:

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

但是我在 Visual 编译时遇到了以下错误

- Error C2039   'apply' n'est pas membre de 'boost::geometry::dispatch::disjoint<Geometry1,Geometry2,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  

解决方法

确实有两个问题:

  1. 您正在与 3D 几何图形相交。那没有实施 相反,您可以对投影执行相同的操作。

  2. 您正在向 intersects 传递一个“输出”几何(它确实只返回您选择的名称 intersectionExists 建议的真/假值)。在存在第三个参数的情况下,它将用作策略 - output 显然不满足的概念。

    注意 intersection 始终返回 true:What does boost::geometry::intersection return - 尽管这不是文档化界面的一部分

由于您的几何图形被微不足道地投影到二维平面 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;
}

印刷品

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