10

我有一个简单的 DLL,它使用 Boost Geometry 多边形进行一些计算。(主要是交叉点和差异。)由于 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' iterator.hpp 63 的成员
  2. 错误 C3203: 'type' : unspecialized class template can't be used as a template argument for template parameter 'Iterator', 应为真实类型differ_type.hpp 25
  3. 错误 C2955:'boost::type':使用类模板需要模板参数列表differ_type.hpp 25
  4. 错误 C2955:“boost::iterator_difference”:使用类模板需要模板参数列表differ_type.hpp 26

哪些看起来与这部分代码没有任何关系(我的文件名是geometry.cpp),但是使用Boost Geometry的所有其他内容都被注释掉了,我仍然得到这些错误,所以......

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

(我是 C++ 和 Boost 的新手,所以在将来自互联网的代码放在一起时,我可能错过了一些基本概念。)我假设我不能轻易地遍历多边形,我错过了非平凡的部分,或者多边形不能用作环,或者迭代不是我想象的那样,或者我不知道还有什么问题。我做错什么了?

4

2 回答 2

5

好的,我想我在这里找到了你要找的东西。我仍然不太明白为什么你要寻找这个我认为大于或等于 2 的点的范围,但我至少在使用 boost::size() 时想出了如何让它编译。

首先,实现函数的第一个参数

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

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

这意味着当您取消引用迭代器时...定义为

std::vector<model::polygon<spherical_point> >::iterator it

右值是一个模型::多边形。

boost::model::polygon 本身与 Boost.Range 不兼容。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(即 model::polygon)仅限于调用这些函数。

它看起来像你想要做的是抓住向量中每个多边形的外环或内环之一(不确定哪个,内部或外部),并查看该环中任何内容的范围是否大于或等于 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;
      }
    }
}
于 2011-10-13T00:24:17.717 回答
4

我发现了一些需要修复的地方:

  1. 我看到的一个问题是在您的模板中。一定要放空格!
  2. boost range 适用于包含开始、结束对的容器或范围
  3. 迭代器表示类似于指向对象的指针。获取迭代器的大小不会做你想要的。您需要使用整个容器的 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;
        }       
    }
}
于 2011-10-12T23:52:45.580 回答