c++ - BOOST_GEOMETRY_REGISTER_RING 的使用

标签 c++ boost-geometry

我无法理解如何将我自己的类型注册为 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定义的戒指类型.

所以我有两个问题:

  1. 如何指定正确的戒指方向?
  2. 为什么我的环类型不能与上面声明的多边形一起使用?

最佳答案

Trouble is, how can I define the "correct" orientation of a ring?

您可以尝试专攻boost::geometry::point_order :

live demo

#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/

相关文章:

c++ - 来自 void 指针的虚方法表

c++ - 是否有关于更换 MS CRT 的最新引用资料?

c++ - 推送到成员 vector 时出现段错误

ios - Boost::Geometry (1.53) 与 iOS

c++ - boost::geometry multi_point 不能像多边形那样构造

C++ 数组列表

C++ 函数运行超过预期

c++ - 使用 Boost.Geometry 时出现 "invalid operands to binary expression"?

c++ - 将几何多边形内部表示作为 STL 列表?

c++ - 使用 Boost::Geometry Polygon boolean/intersections 与线段属性