【问题标题】:Run-time error when using boost::geometry::intersects使用 boost::geometry::intersects 时的运行时错误
【发布时间】:2017-10-11 07:30:24
【问题描述】:

使用 boost::geometry::intersects 判断两个几何图形是否相互交叉时出现运行时错误,出现以下错误:

test:/bigdisk/geo.algorithms/mason_packages/headers/boost/1.65.1/include/boost/geometry/policies/robustness/segment_ratio.hpp:54: 静态bool boost::geometry::detail::segment_ratio ::less::apply(const Ratio&, const Ratio&) [with Ratio = boost::geometry::segment_ratio; Type = double]:断言 `lhs.denominator() != 0' 失败。 line1 等于 line2:已中止

在我天真的眼里,问题出在哪里并不明显。如果有人有任何建议,我将不胜感激。

我已将 boost 几何调整为我自己的几何模型,并且 boost 的 intersects 函数在这种情况下刚刚中止:

    point<double, GCJ02LL> pt11{118.8031, 32.10011};
    point<double, GCJ02LL> pt12{118.80297, 32.10016};
    point<double, GCJ02LL> pt13{118.80284, 32.10021};
    dataop::geometry::line_string<double, GCJ02LL> line1{pt11, pt12, pt13};
    dataop::geometry::line_string<double, GCJ02LL> line2{pt11, pt12, pt13};
    std::cout << "line1 intersects line2? : " << intersects(line1, line2) << std::endl;

你可以看到我的两个 line_strings 是一样的,但这不是问题,因为在其他情况下它运行良好。

更奇怪的是 boost::geometry::equal 在这种情况下也会像这样中止:

    point<double, GCJ02LL> pt11{118.8031, 32.10011};
    point<double, GCJ02LL> pt12{118.80297, 32.10016};
    point<double, GCJ02LL> pt13{118.80284, 32.10021};
    dataop::geometry::line_string<double, GCJ02LL> line1{pt11, pt12, pt13};
    std::cout << "line1 equal line1? " << equal(line1, line1) << std::endl;

【问题讨论】:

    标签: c++ boost boost-geometry


    【解决方案1】:

    断言触发。您的某些数据无效/不适合您尝试执行的操作。

    具体来说,内部计算步骤遇到了分母 (q) 为零的分数 (p/q)。这不会飞。

    现在,造成这种情况的原因是未满足先决条件。

    也许你的几何图形无效(你试过bg::is_valid()吗?你看过bg::correct()吗?)。

    如果所有先决条件都已满足,那么罪魁祸首可能在于适应您的自定义类型。如果改编调用了未定义的行为(例如错误地返回对临时对象的引用),那么所有的赌注都将被取消。

    试验台

    您可以调整此测试平台以获得一些诊断:

    Live On Coliru

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/linestring.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    
    #include <iostream>
    
    namespace bg = boost::geometry;
    namespace bgm = bg::model;
    
    int main() {
        using P = bgm::d2::point_xy<double>;
        P pt11{ 118.8031, 32.10011 };
        P pt12{ 118.80297, 32.10016 };
        P pt13{ 118.80284, 32.10021 };
        bgm::linestring<P> line1{ pt11, pt12, pt13 };
        bgm::linestring<P> line2{ pt11, pt12, pt13 };
        std::string reason;
        std::cout << std::boolalpha;
        std::cout << "line1 valid? " << bg::is_valid(line1, reason) << " (" << reason << ")\n";
        std::cout << "line2 valid? " << bg::is_valid(line2, reason) << " (" << reason << ")\n";
        std::cout << "line1 intersects line2? : " << bg::intersects(line1, line2) << std::endl;
    }
    

    不用说,上面成功了,输出很干净:

    line1 valid? true (Geometry is valid)
    line2 valid? true (Geometry is valid)
    line1 intersects line2? : true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      相关资源
      最近更新 更多