boost::geometry::intersection( 3.html )的文档表示该函数返回一个bool。但是,文档没有说明返回值指示的是什么。我猜如果找到了一个交集,它就会返回true。
错了!
这段代码
#include <iostream>
#include <deque>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
namespace bg = boost::geometry;
using namespace std;
class cxy
{
public:
double x;
double y;
cxy( double X, double Y )
: x( X )
, y( Y )
{
}
/// boost geometry insists on a default constructor
cxy()
: cxy(0,0)
{
}
};
BOOST_GEOMETRY_REGISTER_POINT_2D( cxy, double, bg::cs::cartesian, x, y )
typedef bg::model::segment<cxy> segment_t;
int main()
{
cxy a(1,0);
cxy b(1,1);
cxy c(0,0.5);
cxy d(0.5,0.5) ;
segment_t ab( a, b );
segment_t cd( c, d );
std::vector<cxy> out;
if( ! bg::intersection( ab, cd, out ) ) {
std::cout << "intersection returned false\n";
return 1;
}
if( ! out.size() ) {
std::cout << "no intersection point!\n";
return 2;
}
std::cout << "intersection at " << out[0].x <<" " << out[0].y << "\n";
return 0;
}输出
no intersection point!真实指示的返回是什么?
发布于 2020-07-27 16:14:25
返回值为true,表示没有错误。例如,在呼叫链的深处:
template <typename RobustPolicy, typename GeometryOut, typename Strategy>
static inline bool apply(Geometry1 const& geometry1,
Geometry2 const& geometry2,
RobustPolicy const& robust_policy,
GeometryOut& geometry_out,
Strategy const& strategy)
{
typedef typename geometry::detail::output_geometry_value
<
GeometryOut
>::type SingleOut;
intersection_insert
<
Geometry1, Geometry2, SingleOut,
overlay_intersection
>::apply(geometry1, geometry2, robust_policy,
geometry::detail::output_geometry_back_inserter(geometry_out),
strategy);
return true;
}那是在
#0 0x000055555555598c in boost::geometry::dispatch::intersection<boost::geometry::model::segment<cxy>, boost::geometry::model::segment<cxy>, boost::geometry::segment_tag, boost::geometry::segment_tag, false>::apply<boost::geometry::detail::no_rescale_policy, std::vector<cxy, std::allocator<cxy> >, boost::geometry::strategy::intersection::cartesian_segments<void> > (geometry1=..., geometry2=..., robust_policy=..., geometry_out=std::vector of length 0, capacity 0, strategy=...) at /home/sehe/custom/boost_1_73_0/boost/geometry/algorithms/detail/intersection/interface.hpp:63
#1 0x0000555555555842 in boost::geometry::resolve_strategy::intersection::apply<boost::geometry::model::segment<cxy>, boost::geometry::model::segment<cxy>, std::vector<cxy, std::allocator<cxy> > > (geometry1=..., geometry2=..., geometry_out=std::vector of length 0, capacity 0) at /home/sehe/custom/boost_1_73_0/boost/geometry/algorithms/detail/intersection/interface.hpp:175
#2 0x00005555555556ed in boost::geometry::resolve_variant::intersection<boost::geometry::model::segment<cxy>, boost::geometry::model::segment<cxy> >::apply<std::vector<cxy, std::allocator<cxy> >, boost::geometry::default_strategy> (geometry1=..., geometry2=..., geometry_out=std::vector of length 0, capacity 0, strategy=...) at /home/sehe/custom/boost_1_73_0/boost/geometry/algorithms/detail/intersection/interface.hpp:198
#3 0x00005555555554f3 in boost::geometry::intersection<boost::geometry::model::segment<cxy>, boost::geometry::model::segment<cxy>, std::vector<cxy, std::allocator<cxy> > > (geometry1=..., geometry2=..., geometry_out=std::vector of length 0, capacity 0) at /home/sehe/custom/boost_1_73_0/boost/geometry/algorithms/detail/intersection/interface.hpp:403
#4 0x0000555555554eab in main () at /home/sehe/Projects/stackoverflow/test.cpp:40我查看了Boost地质测量的OGC简单特性规范
图书馆遵循现有的公约:
它在概念上建模算法,没有返回值:

我检查了算法/细节/交叉(areal_areal.hpp、box_box.hpp、implementation.hpp、interface.hpp、multi.hpp)中的所有实现,没有任何返回为false。
TL;DR综述
返回值是明确无文档化的,换句话说:它是一个您可能不依赖的实现细节。
在库接口方面,文档化的接口不能在新版本中更改(没有警告)。许多通过标头“可发现”的东西都是没有文档的--最常见的是detail::名称空间和/或detail/头文件夹。
https://stackoverflow.com/questions/63118550
复制相似问题