使用boost::geometry::intersects时出现运行时错误

Run-time error when using boost::geometry::intersects

本文关键字:运行时错误 intersects boost geometry 使用      更新时间:2023-10-16

当使用boost::geometry::intersects来判断两个几何体是否相交时,我得到一个运行时错误,我得到这个错误:

test:/bigdisk/geo.agorithms/maso_packages/headers/boost/1.65.1/include/boost/geometry/policies/robusty/segment_ratio.hpp:54:static bool boost::geometry::detail::segment_radio::less::apply(const ratio&,const ratio&)[其中ratio=boost:几何体::segment_ratio;Type=double]:断言`lhs.denominator()!="0"失败。第1行等于第2行:中止

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

我已经将boost几何结构调整为我自己的几何模型,boost的相交函数在这种情况下刚刚中止:

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-string是一样的,但这不是问题所在,因为在其他情况下它运行得很好。

更奇怪的是,在这种情况下,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;

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

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

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

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

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

试验台

你可以调整这个测试台来获得一些诊断:

在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