c++ - 用浮点数提升多边形相交
问题描述
我正在尝试通过标准 Boost.Geometry Intersection 方法使用浮点数而不是双精度数。但是,如果我只是在第一个循环中将所有内容都更改为浮点数,那么这段代码就会崩溃。使用 double 它可以毫无问题地工作。这是错误和代码:
未处理的异常:System.AccessViolationException:试图读取或写入受保护的内存。这通常表明其他内存已损坏
PINVOKE int PolylineIntersectionFloat(
float* polyline0, size_t polyline0_XYCount,
float* polyline1, size_t polyline1_XYCount,
float*& intersection, int& intersection_XYCount) {
//////////////////////////////////////////////////////////////////////////////////////////
/// Convert double array to Boost polygons
//////////////////////////////////////////////////////////////////////////////////////////
bg::model::polygon<boost::geometry::model::d2::point_xy<float>> polygon0;
bg::model::polygon<boost::geometry::model::d2::point_xy<float>> polygon1;
for (int i = 0; i < polyline0_XYCount; i += 2) {
double a = polyline0[i];
double b = polyline0[i+1];
bg::append(polygon0.outer(), bg::model::d2::point_xy<float>(a,b));
}
for (int i = 0; i < polyline1_XYCount; i += 2) {
bg::append(polygon1.outer(), bg::model::d2::point_xy<float>(polyline1[i], polyline1[i + 1]));
}
//////////////////////////////////////////////////////////////////////////////////////////
/// Perform 2D Polygon Interserction
//////////////////////////////////////////////////////////////////////////////////////////
boost::geometry::correct(polygon0);
boost::geometry::correct(polygon1);
typedef bg::model::polygon<boost::geometry::model::d2::point_xy<float>> polygon;
std::deque<polygon> output;
bg::intersection(polygon0, polygon1, output);
//////////////////////////////////////////////////////////////////////////////////////////
/// Convert to Coordinate Array for Export
//////////////////////////////////////////////////////////////////////////////////////////
//std::ofstream myfile;
// myfile.open("C:\\Users\\petra\\AppData\\Roaming\\McNeel\\Rhinoceros\\packages\\7.0\\ngon\\3.1.0\\Test.txt");
// myfile << "Writing this to a file.\n";
// myfile << "Number of Polygons" << output.size();
// BOOST_FOREACH(polygon const& p, output)
// {
// myfile << "Count" << p.outer().size();
// myfile << "Count*2" << p.outer().size()*2;
// }
//myfile.close();
if (output.size() == 0)
return 0;
BOOST_FOREACH(polygon const& p, output)
{
intersection_XYCount = p.outer().size() * 2;
intersection = new float[intersection_XYCount];
//Iterate Polygon Points
int j = 0;
for (auto it = boost::begin(bg::exterior_ring(p)); it != boost::end(bg::exterior_ring(p)); ++it)
{
float x = bg::get<0>(*it);
float y = bg::get<1>(*it);
intersection[j] = x;
intersection[j + 1] = y;
j += 2;
}
return 1;
}
return 0;
}
解决方案
推荐阅读
- javascript - 如何使用循环创建 div。我在一本书中找到了这段代码,但它不起作用
- vue.js - 为什么 Vuex 的动作会返回一个承诺
? - javascript - Angular ngx-mat-datetime-picker 选择事件
- c# - Log4net 在记录几秒钟后将日志路径更改为安装文件夹。这是为什么?
- c++ - 将成员函数作为参数传递给不同标头中的父类
- android - 在自定义视图中处理变量 Drawable 的正确方法?
- python - 无法调用函数
- google-cloud-platform - GCP vpc相同的目的地,不同的路线
- python - 如何删除位置如此接近的两个节点之一
- android - 从服务发送 KeyEvent