1

我很难理解如何将我自己的类型注册为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. 如上所述,为什么我的环类型不能与多边形一起使用?
4

1 回答 1

3

麻烦的是,如何定义环的“正确”方向?

您可以尝试专门化 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)
于 2013-03-15T23:53:38.520 回答