从Boost Geometry多边形中获取点的坐标
Getting the coordinates of points from a Boost Geometry polygon
我有一个简单的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;
}
}
}
当前编译错误如下:
- 错误C2039: 'type':不是'boost::mpl::eval_if_c'迭代器的成员。hpp 63
- 错误C3203: 'type':非特化类模板不能用作模板形参'Iterator'的模板实参,期望一个真正的类型difference_type.hpp 25
- 错误C2955: 'boost::type':使用类模板需要模板参数列表difference_type.hpp 25
- 错误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;
}
}
}
我发现了一些需要修复的东西:
- 我看到的一个问题是在你的模板。一定要有空格!
- boost range适用于包含开始和结束对的容器或范围
- 迭代器表示类似于指向对象的指针的东西。获取迭代器的大小不会达到你想要的效果。你需要使用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;
}
}
}
- 如何在C++中获取坐标?
- 使用 GTK3 C++获取指针坐标
- 从 CGAL 3D 网格生成中获取顶点坐标的 -6.27744e+66:mesh_implicit_sphere示例
- 如何从 3d 对象文件中获取 3d 坐标
- 从 3 个视点在 3 张图像上有 3 个点如何在 3D 空间中获取其坐标?
- 如何从 DirectXTK (ToolKit) DirectX::Model 类获取所有顶点坐标以用于冲突检测
- 获取具有X,Y和距离的OPENCV中的3D坐标
- 获取QImage像素坐标
- OpenCV从2D像素获取3D坐标
- 如何通过调用 gluUnproject 两次(不使用 gluUnProject)从场景坐标中获取深度值
- 获取数独块的坐标
- 在调整大小窗口时,如何使用鼠标单击来获取图像的坐标
- 将圆圈分为n相等的部分,以获取每个分界点的坐标
- 从卫星的ECI坐标获取地球上的纬度和经度点
- Android NDK-如何从TAP中获取坐标并将其显示在文本视图上
- OpenGL获取离当前鼠标位置最近的世界三维点的三维坐标
- 当用户按 Ctrl 并单击时获取鼠标坐标
- 获取RGBDemo的鼠标位置坐标
- D3D:如何从 x,y 坐标获取颜色
- 在c++中通过坐标获取png