我正在尝试计算点与线段之间的距离。但是以下代码在运行时突然终止(@comparable_distance)。
using namespace std;
namespace bg = boost::geometry;
class Vertex {
public:
Vertex(int px, int py):x(px), y(py){}
int x;
int y;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, cs::cartesian, x, y)
typedef Vertex* vertref;
typedef bg::model::segment<vertref> segment_type;
std::vector<segment_type> segments;
int main()
{
segment_type l(new Vertex(1,2), new Vertex(2,2));
Vertex p(4,5);
double comp_dist = 0;
comp_dist = bg::comparable_distance(p, l);
cout<<comp_dist<<endl;
return 0;
}
如果我将bg :: model :: segment替换为linestring;向其添加两个点,它可以正常工作,如下所示...
using namespace std;
namespace bg = boost::geometry;
class Vertex {
public:
Vertex(int px, int py):x(px), y(py){}
int x;
int y;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, cs::cartesian, x, y)
typedef Vertex* vertref;
typedef bg::model::linestring<vertref> segment_type;
std::vector<segment_type> segments;
int main()
{
segment_type l;
l.push_back(new Vertex(1,2));
l.push_back(new Vertex(2,2));
Vertex p(4,5);
double comp_dist = 0;
comp_dist = bg::comparable_distance(p, l);
cout<<comp_dist<<endl;
return 0;
}
有什么想法在使用段的第一个代码中我做错了吗?
谢谢!
调用堆栈...
#0 0x40486f boost::geometry::traits::access<Vertex, 0u, void>::set(p=..., value=@0x63fde8: 1) (C:\Projects\test\main.cpp:17)
#1 0x403e14 boost::geometry::core_dispatch::access<boost::geometry::point_tag, Vertex, double, 0u, boost::integral_constant<bool, true> >::set(p=0x77b12580, value=@0x63fde8: 1) (C:/boost_1_68_0/boost/geometry/core/access.hpp:187)
#2 0x404182 boost::geometry::set<0u, Vertex*>(geometry=@0x63fe34: 0x77b12580, value=@0x63fde8: 1, dummy=0x0) (C:/boost_1_68_0/boost/geometry/core/access.hpp:321)
#3 0x40469a boost::geometry::detail::assign::assign_point_from_index<boost::geometry::model::segment<Vertex*>, Vertex*, 0u, 0u, 2u>::apply(geometry=..., point=@0x63fe34: 0x77b12580) (C:/boost_1_68_0/boost/geometry/algorithms/detail/assign_values.hpp:195)
#4 0x4045de boost::geometry::detail::assign_point_from_index<0u, Vertex*, boost::geometry::model::segment<Vertex*> >(geometry=..., point=@0x63fe34: 0x77b12580) (C:/boost_1_68_0/boost/geometry/algorithms/detail/assign_indexed_point.hpp:80)
#5 0x4049fc boost::geometry::dispatch::distance<Vertex, boost::geometry::model::segment<Vertex*>, boost::geometry::strategy::distance::projected_point<void, boost::geometry::strategy::distance::comparable::pythagoras<void> >, boost::geometry::point_tag, boost::geometry::segment_tag, boost::geometry::strategy_tag_distance_point_segment, false>::apply(point=..., segment=..., strategy=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/distance/point_to_geometry.hpp:419)
#6 0x403f9b boost::geometry::resolve_strategy::comparable_distance::apply<Vertex, boost::geometry::model::segment<Vertex*> >(geometry1=..., geometry2=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/comparable_distance/interface.hpp:83)
#7 0x403f38 boost::geometry::resolve_variant::comparable_distance<Vertex, boost::geometry::model::segment<Vertex*> >::apply<boost::geometry::default_strategy>(geometry1=..., geometry2=..., strategy=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/comparable_distance/interface.hpp:106)
#8 0x403ff5 boost::geometry::comparable_distance<Vertex, boost::geometry::model::segment<Vertex*>, boost::geometry::default_strategy>(geometry1=..., geometry2=..., strategy=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/comparable_distance/interface.hpp:328)
#9 0x403fc9 boost::geometry::comparable_distance<Vertex, boost::geometry::model::segment<Vertex*> >(geometry1=..., geometry2=...) (C:/boost_1_68_0/boost/geometry/algorithms/detail/comparable_distance/interface.hpp:356)
#10 0x401518 main() (C:\Projects\test\main.cpp:30)
答案 0 :(得分:3)
Segment Concept由model::segment
建模,该Point采用模拟Live On Coliru的模板参数。
现在您使用了:
typedef Vertex *vertref;
typedef bg::model::segment<vertref> segment_type;
请注意,vertref
实际上不是为Point概念建模的对象。因此,相比之下,请考虑:
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <iostream>
namespace bg = boost::geometry;
struct Vertex {
double x;
double y;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, bg::cs::cartesian, x, y)
typedef bg::model::segment<Vertex> segment_type;
int main() {
segment_type l({1, 2}, {2, 2});
Vertex p {4, 5};
auto comp_dist = bg::comparable_distance(p, l);
std::cout << comp_dist << "\n";
}
打印:
13
实际上13是实际距离的平方:Live On Coliru
为回应评论,是的,甚至还存在针对参考点(来自其他几何)的线段的现成模型。 referring_segment<>
的使用方法如下:
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <iostream>
namespace bg = boost::geometry;
struct Vertex {
double x;
double y;
};
BOOST_GEOMETRY_REGISTER_POINT_2D(Vertex, double, bg::cs::cartesian, x, y)
typedef bg::model::referring_segment<Vertex> segment_type;
int main() {
Vertex A{1,2},
B{2,2};
segment_type l(A, B);
Vertex p {4, 5};
std::cout << bg::distance(p, l) << "\n";
bg::multiply_value(A, -1);
bg::multiply_value(B, -1);
std::cout << bg::distance(p, l) << "\n";
}
打印前后突变的距离:
3.60555
8.60233
这又符合您的期望:Why should C++ programmers minimize use of 'new'?
new
几乎总是一个错误。此错误最常见的是来自Java或C#({{3}})等垃圾收集语言的初学者x
,y
是int
个)