淄博市建设工程质量协会网站,个人网站建设教学视频百度云盘,有需要做网站推广找我,阿里云账号密码发给网站开发CuraEngine之代码阅读#xff08;1#xff09;之路径优化函数 注#xff1a;整理一些突然学到的C知识#xff0c;随时mark一下 例如#xff1a;忘记的关键字用法#xff0c;新关键字#xff0c;新数据结构 C 的 STL CuraEngine之代码阅读#xff08;1#xff09;之路径…CuraEngine之代码阅读1之路径优化函数 注整理一些突然学到的C知识随时mark一下 例如忘记的关键字用法新关键字新数据结构 C 的 STL CuraEngine之代码阅读1之路径优化函数一、路径优化函数 提示本文为curaengine 中 路径优化函数代码 一、路径优化函数 CuraEngine的功能用于3D打印接受STL文件或其他格式的文件如AMF文件作为输入并输出G代码GCode。G代码类似于汇编代码可以直接在底层硬件上运行控制电机等运动单元动作。
void PathOrderOptimizer::optimize()
{// NOTE: Keep this vector fixed-size, it replaces an (non-standard, sized at runtime) array:std::vectorbool picked(polygons.size(), false);loc_to_line nullptr;for (unsigned poly_idx 0; poly_idx polygons.size(); poly_idx) /// find closest point to initial starting point within each polygon initialize picked{const ConstPolygonRef poly *polygons[poly_idx];switch (config.type){case EZSeamType::USER_SPECIFIED:polyStart.push_back(getClosestPointInPolygon(config.pos, poly_idx));break;case EZSeamType::RANDOM:polyStart.push_back(getRandomPointInPolygon(poly_idx));break;case EZSeamType::SHARPEST_CORNER:case EZSeamType::SHORTEST:default:polyStart.push_back(getClosestPointInPolygon(startPoint, poly_idx));break;}assert(poly.size() ! 2);}Point prev_point;switch (config.type){case EZSeamType::USER_SPECIFIED:prev_point config.pos;break;case EZSeamType::RANDOM: //TODO: Starting position of the first polygon isnt random.case EZSeamType::SHARPEST_CORNER:case EZSeamType::SHORTEST:default:prev_point startPoint;}for (unsigned int poly_order_idx 0; poly_order_idx polygons.size(); poly_order_idx) /// actual path order optimizer{int best_poly_idx -1;float bestDist2 std::numeric_limitsfloat::infinity();for (unsigned int poly_idx 0; poly_idx polygons.size(); poly_idx){if (picked[poly_idx] || polygons[poly_idx]-size() 1) /// skip single-point-polygons{continue;}assert (polygons[poly_idx]-size() ! 2);const Point p (*polygons[poly_idx])[polyStart[poly_idx]];float dist2 vSize2f(p - prev_point);if (dist2 bestDist2 combing_boundary){// using direct routing, this poly is the closest so far but as the combing boundary// is available see if the travel would cross the combing boundary and, if so, either get// the combed distance and use that instead or increase the distance to make it less attractiveif (PolygonUtils::polygonCollidesWithLineSegment(*combing_boundary, p, prev_point)){if ((polygons.size() - poly_order_idx) 100){// calculating the combing distance for lots of polygons is too time consuming so, instead,// just increase the distance to penalise travels that hit the combing boundarydist2 * 5;}else{if (!loc_to_line){// the combing boundary has been provided so do the initialisation// required to be able to calculate realistic travel distances to the start of new pathsconst int travel_avoid_distance 2000; // assume 2mm - not really critical for our purposesloc_to_line PolygonUtils::createLocToLineGrid(*combing_boundary, travel_avoid_distance);}CombPath comb_path;if (LinePolygonsCrossings::comb(*combing_boundary, *loc_to_line, p, prev_point, comb_path, -40, 0, false)){float dist 0;Point last_point p;for (const Point comb_point : comb_path){dist vSize(comb_point - last_point);last_point comb_point;}dist2 dist * dist;}}}}if (dist2 bestDist2){best_poly_idx poly_idx;bestDist2 dist2;}}if (best_poly_idx -1) /// should always be true; we should have been able to identify the best next polygon{assert(polygons[best_poly_idx]-size() ! 2);prev_point (*polygons[best_poly_idx])[polyStart[best_poly_idx]];picked[best_poly_idx] true;polyOrder.push_back(best_poly_idx);}else{logError(Failed to find next closest polygon.\n);}}if (loc_to_line ! nullptr){delete loc_to_line;}
}这段代码定义了一个名为 PathOrderOptimizer 的类的成员函数 optimize。这个函数的目的是优化多边形polygons的打印顺序通常用于3D打印或其他需要按顺序处理多边形的应用场景。以下是这段代码的详细解释
初始化: std::vector picked(polygons.size(), false);创建一个布尔向量其大小与多边形数组 polygons 相同并将所有元素初始化为 false。这个向量用于跟踪哪些多边形已经被选中或处理过。 loc_to_line nullptr;将 loc_to_line 设置为 nullptr。这可能是指向某个数据结构的指针但在这段代码中并没有进一步使用。 找到每个多边形的起始点: 遍历每个多边形并根据配置 config.type 的类型来找到每个多边形的起始点。 如果配置是 USER_SPECIFIED则使用用户指定的位置 config.pos 来找到最近点。 如果配置是 RANDOM则随机选择多边形内的一个点作为起始点。 如果配置是 SHARPEST_CORNER 或 SHORTEST或默认情况则使用 startPoint 来找到最近点。 polyStart 是一个容器用于存储每个多边形的起始点。 确定第一个多边形的起始点: 根据配置 config.type 的类型来确定整个路径的起始点 prev_point。 优化多边形打印顺序: 遍历每个多边形目的是确定它们应该被处理的顺序。 对于每个尚未处理的多边形计算其与当前 prev_point 的距离bestDist2并找到距离最小的多边形best_poly_idx。 如果多边形只有一个点或已被处理picked[poly_idx] 为 true则跳过该多边形。 断言确保多边形不只有两个点这可能是因为只有两个点的多边形对于此优化过程没有意义或可能导致错误。
到 assert (polygons[poly_idx]-size() ! 2);这里
后面的逻辑如如何处理 best_poly_idx 和 bestDist2以及如何更新 picked 向量和 prev_point还未读完。但基于这段代码我们可以推断 optimize 函数的目的是根据某种标准如距离来确定多边形的处理顺序可能是为了最小化打印头在打印不同多边形之间的移动距离。
需要注意的是这段代码依赖于一些外部定义和变量如 polygons、polyStart、config、startPoint 和可能的其他成员函数如 getClosestPointInPolygon 和 getRandomPointInPolygon