从Boost Geometry多边形中获取点的坐标

Getting the coordinates of points from a Boost Geometry polygon

本文关键字:坐标 获取 Boost Geometry 多边形      更新时间:2023-10-16

我有一个简单的DLL做一些计算与Boost几何多边形。(主要是交集和差异。)由于DLL很可能从c#代码和Delphi中调用,谁知道从其他地方调用,我应该将结果转换为一切都可以处理的数组。

更新:我简化并修正了代码。新代码看起来完全不同,使用了完全不同的方法(for_each_point),不知何故仍然无法编译。

我的新代码:
#include <vector>
#include <boost/range.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
using namespace boost::geometry;
typedef boost::geometry::model::point
    <
        double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree>
    > spherical_point;
class PointAggregator {
private :
    double *x, *y;
    int count;
public :
    PointAggregator(int size) {
        x = (double*) malloc(sizeof(double) * size);
        y = (double*) malloc(sizeof(double) * size);
        count = 0;
    }
    ~PointAggregator() {
        free(x);
        free(y);
    }
    inline void operator()(spherical_point& p) {
        x[count] = get<0>(p);
        y[count] = get<1>(p);
        count++;
    }
    void GetResult(double *resultX, double *resultY) {
        resultX = x;
        resultY = y;
    }
};
void VectorToArray(std::vector<model::polygon<spherical_point>> resultVector, double x[], double y[], int *count) {
    int i = 0;      
    for (std::vector<model::polygon<spherical_point>>::iterator it = resultVector.begin(); it != resultVector.end(); ++it) {
        if (boost::size(*it) >= 2) {
            *count = boost::size(*it);
            PointAggregator* pa = new PointAggregator(*count);
            boost::geometry::for_each_point(*it, *pa);
            pa->GetResult(x, y);
            delete(pa);
            break;
        }       
    }
}

当前编译错误如下:

  1. 错误C2039: 'type':不是'boost::mpl::eval_if_c'迭代器的成员。hpp 63
  2. 错误C3203: 'type':非特化类模板不能用作模板形参'Iterator'的模板实参,期望一个真正的类型difference_type.hpp 25
  3. 错误C2955: 'boost::type':使用类模板需要模板参数列表difference_type.hpp 25
  4. 错误C2955: 'boost::iterator_difference':使用类模板需要模板参数列表difference_type.hpp 26

哪一个看起来不像他们有任何关系与这部分代码(我的文件名是Geometry .cpp),但其他一切使用Boost Geometry被注释掉,我仍然得到这些错误,所以…

这是我以前(由sehe编辑)的错误代码

(我是c++和Boost的新手,所以我可能错过了一些基本的概念,而把互联网上的代码放在一起。)我假设我不能那么容易地遍历一个多边形我错过了重要的部分,或者多边形不能用作环,或者迭代不是我想象的那样,或者我不知道还有什么错了。我做错了什么?

好的,我想我已经找到了你要找的东西。我仍然不太明白为什么你要寻找这个范围,我假设是大于或等于2的点,但我弄清楚了如何在使用boost::size()时编译它。

首先,认识到函数的第一个参数

void VectorToArray(std::vector<model::polygon<spherical_point> > resultVector, double x[], double y[], int *count)
{
...
}

是一个std::vector,包含model::polygon类型的实例。

这意味着当你解引用定义为

的迭代器时
std::vector<model::polygon<spherical_point> >::iterator it

右值是一个model::多边形。

boost::model::polygon本身不是boost。兼容的范围。Boost::model::polygon是一个包含5个成员函数的类型....

inline ring_type const& outer() const { return m_outer; }
inline inner_container_type const& inners() const { return m_inners; }
inline ring_type& outer() { return m_outer; }
inline inner_container_type & inners() { return m_inners; }
inline void clear()
{
    m_outer.clear();
    m_inners.clear();
} 

这意味着你的*it(即,一个模型::多边形)被限制为只调用这些函数。

看起来你想要做的是抓住向量中每个多边形的外环或一个内环(不确定是内环还是外环),并查看该环中的范围是否大于或等于2。

要做到这一点,我们必须做更多的mpl和typedef。

typedef boost::geometry::model::point<double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree> > spherical_point; // your definition of a spherical_point
typedef boost::geometry::model::polygon<spherical_point> polygon; //consolidation of template args for a polygon
typedef boost::geometry::ring_type<polygon>::type ring_type; // define a ring_type that can handle your spherical_point by way of the polygon typedef.
typedef boost::geometry::interior_type<polygon>::type int_type; //define a interior_type  that can handle your spherical_point 

为了完成这个,只是让它"工作",我决定假设你想要"外"环为你的范围限制条件。

这是,对我来说,编译代码,在gcc 4.1.1与boost 1.48。我把逻辑是否正确留给别人。

using namespace boost::geometry;
typedef boost::geometry::model::point<double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree> > spherical_point;
typedef boost::geometry::model::polygon<spherical_point> polygon;
typedef boost::geometry::ring_type<polygon>::type ring_type;
typedef boost::geometry::interior_type<polygon>::type int_type;
class PointAggregator 
{
private :
    double *x, *y;
    int count;
public :
    PointAggregator(int size) 
    {
        x = (double*) malloc(sizeof(double) * size);
        y = (double*) malloc(sizeof(double) * size);
        count = 0;
    }
    ~PointAggregator() 
    {
        free(x);
        free(y);
    }
    inline void operator()(spherical_point& p) 
    {
        x[count] = get<0>(p);
        y[count] = get<1>(p);
        count++;
    }
    void GetResult(double *resultX, double *resultY) 
    {
        resultX = x;
        resultY = y;
    }
};
void VectorToArray(std::vector<model::polygon<spherical_point> > resultVector, double x[], double y[], int *count) 
{
    for (std::vector<model::polygon<spherical_point> >::iterator it = resultVector.begin(); it != resultVector.end(); ++it) 
    {
      model::polygon<spherical_point> tmpPoly;
      tmpPoly = (*it);
      boost::geometry::ring_type<polygon>::type somering = tmpPoly.outer(); //typed it all out again instead of using ring_type since the complier was complaining and i didn't wanna get into it.
      int ringsize = boost::size(somering);
      if(ringsize >= 2)
      {
          *count = ringsize;
          PointAggregator* pa = new PointAggregator(*count);
          boost::geometry::for_each_point(*it, *pa);
          pa->GetResult(x, y);
          delete(pa);
          break;
      }
    }
}

我发现了一些需要修复的东西:

    我看到的一个问题是在你的模板。一定要有空格!
  1. boost range适用于包含开始和结束对的容器或范围
  2. 迭代器表示类似于指向对象的指针的东西。获取迭代器的大小不会达到你想要的效果。你需要使用boost::size整个容器,或者std::distance(begin_iterator,end_iterator)。

下面是编译的版本:

#include <vector>
#include <boost/range.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
using namespace boost::geometry;
typedef boost::geometry::model::point
    <
        double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree>
    > spherical_point;
class PointAggregator {
private :
    double *x, *y;
    int count;
public :
    PointAggregator(int size) {
        x = (double*) malloc(sizeof(double) * size);
        y = (double*) malloc(sizeof(double) * size);
        count = 0;
    }
    ~PointAggregator() {
        free(x);
        free(y);
    }
    inline void operator()(spherical_point& p) {
        x[count] = get<0>(p);
        y[count] = get<1>(p);
        count++;
    }
    void GetResult(double *resultX, double *resultY) {
        resultX = x;
        resultY = y;
    }
};
// added spaces to the close brackets >> becomes > >
void VectorToArray(std::vector<model::polygon<spherical_point> > resultVector, double x[], double y[], int *count) {
    for (std::vector<model::polygon<spherical_point> >::iterator it = resultVector.begin(); it != resultVector.end(); ++it) {
        if (boost::size(resultVector) >= 2) {
            // getting the size of the whole container
            *count = boost::size(resultVector);
            PointAggregator* pa = new PointAggregator(*count);
            boost::geometry::for_each_point(*it, *pa);
            pa->GetResult(x, y);
            delete(pa);
            break;
        }       
    }
}