从 2D C 列表创建 boost.geometry.model.polygon
Creating boost.geometry.model.polygon from 2D C List
假设我有以下数据集
double * data = (double *) malloc(sizeof(double) * 100 * 2);
for (ii = 0; ii < 100; ii++) {
data[2*ii] = ii;
data[2*ii + 1] = ii;
}
如何根据此数据创建提升多边形?
谢谢
一个完整的例子
#include <iostream>
#include <boost/polygon/polygon.hpp>
#include <vector>
// Some typedefs
namespace bpl = boost::polygon;
typedef bpl::polygon_data<double> Polygon;
typedef bpl::polygon_traits<Polygon>::point_type Point;
int main() {
// Your C-style data (assumed (x,y) pairs)
double * data = (double *) malloc(sizeof(double) * 100 * 2);
for (int ii = 0; ii < 100; ii++) {
data[2*ii] = ii;
data[2*ii + 1] = ii;
}
// Convert to points
std::vector<Point> points;
for (int i=0;i<100;++i)
points.push_back(Point(data[2*i],data[2*i+1]));
// Create a polygon
Polygon polygon;
polygon.set(points.begin(),points.end());
// Do something with the polygon
std::cout << "Perimeter : " << bpl::perimeter(polygon) << std::endl;
std::cout << "Area : " << bpl::area(polygon) << std::endl;
return 0;
}
只是为了说明您实际拥有的灵活性:通过一些额外的 typedef 工作,可以定义您自己的双精度对点类型,该点类型可以混叠到您的数据上,从而避免了中间副本......
#include <iostream>
#include <boost/polygon/polygon.hpp>
#include <vector>
// Define a point type which can be aliased to your 'C' points
struct Pt {
double x;
double y;
};
// Some typedefs
namespace bpl = boost::polygon;
typedef bpl::polygon_data<double> Polygon;
// Add the necessary to use Pt
namespace boost {
namespace polygon {
template <> struct geometry_concept<Pt> {typedef point_concept type;};
template <> struct point_traits<Pt> {
typedef double coordinate_type;
static inline coordinate_type get(const Pt& pt,orientation_2d orient) {
return (orient == HORIZONTAL ? pt.x : pt.y);
}
};
template <> struct point_mutable_traits<Pt> {
static inline void set(Pt& pt, orientation_2d orient, int value) {
if(orient == HORIZONTAL)
pt.x = value;
else
pt.y = value;
}
static inline Pt construct(double x,double y) {
Pt r;
r.x=x;
r.y=y;
return r;
}
};
}
}
int main() {
// Your C-style data (assumed (x,y) pairs)
double * data = (double *) malloc(sizeof(double) * 100 * 2);
for (int ii = 0; ii < 100; ii++) {
data[2*ii] = ii;
data[2*ii + 1] = ii;
}
// Reinterpret your data as an array of Pt
const Pt*const pts=reinterpret_cast<const Pt*>(data);
// Create a polygon
Polygon polygon;
polygon.set(pts,pts+100);
// Do something with the polygon
std::cout << "Perimeter : " << bpl::perimeter(polygon) << std::endl;
std::cout << "Area : " << bpl::area(polygon) << std::endl;
return 0;
}
这种趋势可以延续到自定义多边形类。
相关文章:
- 像union_这样的 Boost.Geometry 操作如何处理浮点类型的基本不精确性?
- boost::geometry::within() 点和序列的行为
- QScreen geometry()api为分辨率2736 x 1824和2560 x 1600提供了错误的值
- 如何在 osg::Geometry 上设置光泽度,而无需在 OpenSceneGraph 中设置材质
- 如何从 DirectXTK (ToolKit) DirectX::Model 类获取所有顶点坐标以用于冲突检测
- gradle see output for model.components.binaries.all
- 使用boost::geometry::intersects时出现运行时错误
- 使用 boost::geometry::追加时,自定义点类中的 ID 字段间歇性丢失
- 为什么 malloc(0) 在"Inside the C++ Object Model?"中有用
- Qt,QListView Model
- Cythonic 方式包装 boost::geometry::P oint accessors
- 如何使用'Build Model'函数从 matlab simulink 块生成 c/c++ 代码?
- 为什么 boost::geometry::d istance 使用 model::d 2::p oint_xy<float> 返回双精度而不是浮点数?
- 从 2D C 列表创建 boost.geometry.model.polygon
- 将matlab中的libsvm模型结构保存到可以用C++读取的.model文件中
- Lib-svm,如何将MyModel.mat转换为MyModel.model
- boost::geometry::model::模板点类型的分段构造函数
- 如何从boost::geometry::model::point继承
- 如何从svg读取boost::geometry::model::polygon
- boost::geometry::model::linestring与boost:的交集:geometry::model