我有一个简单的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;
}
}
}
当前的编译错误是:
哪些看起来与这部分代码(我的文件名是geometry.cpp)有什么关系,但是使用Boost Geometry的其他所有内容都被注释掉了,我仍然会收到这些错误,所以...
Here is my bad code that I had previously (edited by sehe)
(我是C ++和Boost的新手,所以我可能会错过一些基本概念,同时将代码从互联网上放到一起。) 我假设我不能轻易地遍历多边形并且我错过了非平凡的部分,或者多边形不能用作环,或者迭代不是我认为的那样,或者我没有想法还有什么可能是错的。我做错了什么?
答案 0 :(得分:5)
好的,我想我在这里找到了你想要的东西。 我仍然不太明白你为什么要寻找这个我认为大于或等于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
rvalue是一个模型:: polygon。
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();
}
这意味着你的*它(即一个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;
}
}
}
答案 1 :(得分:4)
我发现了一些需要修复的事情:
这是一个编译的版本:
#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;
}
}
}