首页 > 解决方案 > Boost Geometry:段相交尚未实现?

问题描述

我正在尝试一个简单的测试:使用 Boost Geometry 计算 2 个段的交集。它不编译。我还尝试了一些变化(int 点而不是浮点数,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, 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);

但我在 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  

标签: boostintersectionboost-geometry

解决方案


确实有两个问题:

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

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

    注意intersection总是返回 true:什么 boost::geometry::intersection 返回- 尽管这不是文档化接口的一部分

由于您的几何图形被简单地投影到 2d 平面 Z=0 上:

住在科利鲁

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

推荐阅读