2011-10-11 80 views
8

我有一個簡單的DLL,使用Boost Geometry多邊形進行一些計算。 (主要是交集和差異)。由於DLL很可能會從C#代碼和Delphi中調用,並且誰知道從哪裏得到,我應該將結果轉換爲一切都可以處理的數組。獲取Boost Geometry多邊形中點的座標

更新: 我已簡化並稍微糾正了我的代碼。新代碼看起來完全不同,使用完全不同的方法(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: '類型':不是 '提振:: MPL :: eval_if_c' 迭代中的一員。 hpp 63
  2. 錯誤C3203:'type':未專用的類模板不能用作模板參數'Iterator'的模板參數,期望實際類型difference_type.hpp 25
  3. 錯誤C2955: '的boost ::類型':使用類模板需要模板參數列表difference_type.hpp 25
  4. 錯誤C2955: '提高:: iterator_difference':使用類模板需要模板參數列表difference_type.hpp 26

哪些看起來不像他們與這部分代碼有什麼關係(我的文件名是geometry.cpp),但其他所有使用Boost Geometry的東西都被註釋掉了,我仍然會得到這些錯誤,所以...

Here is my bad code that I had previously (edited by sehe)

(我是C++和Boost的新手,所以我可能錯過了我的基本概念,同時把來自互聯​​網的代碼放在一起。) 我假設我不能迭代通過一個很容易的多邊形,我錯過了非平凡的部分,或者多邊形不能用作環,或者迭代是隻是不是我認爲的那樣,或者我不知道還有什麼可能是錯的。我做錯了什麼?

+0

您是否收到編譯錯誤,或者您在搜索邏輯錯誤?什麼不適合你? – sbrett

+0

我更新了我的代碼。由於我無法編譯,我不知道我是否在語義上做了正確的事情,所以我正在尋找我犯的任何錯誤。 – ytg

+1

我在我的系統上的升級版本是1.35,所以我更新了它在SVN上的版本(很快我會認爲是1.48)。 我能夠讓過去的問題就沒有的boost ::幾何,看看你得到什麼樣的錯誤,這是混亂的,但有沒有那麼多。從我可以看出來的角度來看,boost :: size方法被定義爲我認爲是boost_range的一部分,似乎並不知道如何處理你的model :: polygon 。看了升壓範圍參考後,我注意到它提到了所需的功能,如range_begin。 – sbrett

回答

4

我發現被固定在需要的幾件事情:

  1. 一個問題我看到的是在你的模板。一定要把空格!
  2. 升壓範圍適用於容器或保持開始,結束對的容器或範圍
  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; 
     }  
    } 
} 
+1

該死的,沒有看到你的答案,然後我張貼我的。道歉。 – sbrett

4

好吧,我想我有你在找什麼在這裏。 我仍然不明白爲什麼你在尋找這個我認爲是大於或等於2的點的範圍,但我想出瞭如何在至少使用boost :: size()時編譯它。

首先,實現該功能

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

的第一個參數是包含類型模型的實例::多邊形一個std ::向量。

這意味着,當你提領你的迭代器...定義爲

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

右值是一個模型::多邊形。

的boost ::型號::多邊形是其本身不是Boost.Range兼容和。 升壓::模型::多邊形是含有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(); 
} 

這意味着您*它(即,模型::多邊形)被限制爲僅調用這些函數。

它看起來像你想要做的是抓住向量中的每個多邊形的外環或內環之一(不知道是哪個,內部還是外部),並查看該環中任何內容的範圍大於或等於2

要做到這一點,我們必須做一些更多的MPL和類型定義。

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提升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; 
     } 
    } 
} 
+0

即使是在閱讀我的想法並找出外環的方面,這也是令人印象深刻的工作。 – ytg

相關問題