我无法理解如何将我自己的类型注册为 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
定义的戒指类型.
所以我有两个问题:
- 如何指定正确的戒指方向?
- 为什么我的环类型不能与上面声明的多边形一起使用?
最佳答案
Trouble is, how can I define the "correct" orientation of a ring?
您可以尝试专攻boost::geometry::point_order :
#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)
关于c++ - BOOST_GEOMETRY_REGISTER_RING 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15443179/