我很难理解如何将自己的类型注册为boost::geometry::model::ring
。我有自己的点课:
struct Point { double x, y; };
戒指是以std::vector<Point>
的形式存储的。因此,我是这样登记的:
BOOST_GEOMETRY_REGISTER_POINT_2D(Point , double, cs::cartesian, x, y);
BOOST_GEOMETRY_REGISTER_RING(std::vector<Point>);
现在,我想纠正环的方向,实际上,下面是编译的:
void foo(std::vector<Point>& ring) {
boost::geometry::correct(ring);
}
问题是,我如何定义一个戒指的“正确”方向?在使用boost::geometry::model::polygon
时,更明显的是,模板参数允许我指定预期的方向。然而,以下内容没有编译:
void foo(std::vector<Point>& ring) {
typedef boost::geometry::model::polygon<vector> clockwise_closed_polygon;
clockwise_closed_polygon cwcp;
boost::geometry::exterior_ring(cwcp) = ring; //<<< fails to compile
boost::geometry::correct(cwcp);
}
显然,我自己的戒指类型不能转换为clockwise_closed_polygon
定义的类型。
所以我有两个问题:
发布于 2013-03-15 23:53:38
问题是,我如何定义一个戒指的“正确”方向?
您可以尝试专门化订单
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/register/ring.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/core/point_order.hpp>
#include <boost/geometry.hpp>
#include <iostream>
#include <ostream>
#include <vector>
using namespace boost::geometry;
using namespace std;
struct Point { double x, y; };
BOOST_GEOMETRY_REGISTER_POINT_2D(Point , double, cs::cartesian, x, y)
BOOST_GEOMETRY_REGISTER_RING(vector<Point>)
namespace boost { namespace geometry
{
template<>
struct point_order<vector<Point>>
{
//static const order_selector value=counterclockwise;
static const order_selector value=clockwise;
};
}}
template<typename Ring>
void print(const Ring &r)
{
for(auto p : r)
cout << "(" << p.x << "," << p.y << ")";
cout << endl;
}
int main()
{
std::vector<Point> ring{{0.0,0.0},{1.0,0.0},{0.0,1.0},{0.0,0.0}};
print(ring);
correct(ring);
print(ring);
}
产出如下:
(0,0)(1,0)(0,1)(0,0)
(0,0)(0,1)(1,0)(0,0)
如果从顺时针改为逆时针,则输出为:
(0,0)(1,0)(0,1)(0,0)
(0,0)(1,0)(0,1)(0,0)
https://stackoverflow.com/questions/15443179
复制相似问题