You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何设置Boost RTree坐标系?线段相交查询报错求助

问题

为实现RTree的query功能以查询两段线段的相交情况,编写了如下代码:

using Point2d =
    boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian>;
using Segment2d = boost::geometry::model::segment<Point2d>;
using SegmentRtree =
    boost::geometry::index::rtree<Segment2d, boost::geometry::index::rstar<16>>;
for (int point_index = 0;
     point_index + 1 < segment.line_segment().point_size();
     point_index++) {
    const Segment2d segment_in_range = {
        {segment.line_segment().point()[point_index].x(),
         segment.line_segment().point()[point_index].y()},
        {segment.line_segment().point()[point_index + 1].x(),
         segment.line_segment().point()[point_index + 1].y()}};
    uncrossable_segments_in_range.insert(segment_in_range);
}

编译时出现如下错误:

/home/xx/xx/xx/xx/xx/xx/xx/xx.cc:220:66:   required from here
/home/xx/xx/xx/build_x86/_deps/boost-src/include/boost/geometry/strategies/index/services.hpp:31:5: error: static assertion failed: Not implemented for this coordinate system.
   31 |     BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

请问该如何设置Boost RTree的坐标系以解决此问题?

解决方法
  • 问题本质:Boost.Geometry的RTree核心基于几何体的**包围盒(Bounding Box)**构建索引,直接将Segment2d作为存储类型时,默认索引策略无法自动适配线段的包围盒计算逻辑,导致坐标系相关的断言错误。

  • 最简修正方案:用std::pair包装线段与唯一标识(如ID),让RTree自动提取线段的包围盒作为索引键:

    // 修改RTree的存储类型为线段+标识的组合
    using SegmentRtree = boost::geometry::index::rtree<
        std::pair<Segment2d, int>, 
        boost::geometry::index::rstar<16>
    >;
    

    插入数据时同步调整:

    // 以point_index作为线段标识示例
    uncrossable_segments_in_range.insert(
        std::make_pair(segment_in_range, point_index)
    );
    
  • 自定义索引策略方案:若不想用std::pair,可手动实现indexable函数,显式计算线段的包围盒:

    namespace bg = boost::geometry;
    namespace bgi = boost::geometry::index;
    
    struct SegmentIndexable {
        using result_type = bg::model::box<Point2d>;
        result_type operator()(const Segment2d& s) const {
            result_type box;
            bg::envelope(s, box); // 计算线段的包围盒
            return box;
        }
    };
    
    // 在RTree定义中指定自定义索引策略
    using SegmentRtree = bgi::rtree<
        Segment2d,
        bgi::rstar<16>,
        SegmentIndexable
    >;
    

内容的提问来源于stack exchange,提问作者wong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 14:37:37