From b0c165fa8a068dee36f438fff42ce9e97bf4896c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=B0=E4=BB=94?= Date: Sun, 12 Apr 2026 22:56:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(presenter):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E7=9A=84debug=E6=95=B0=E6=8D=AE=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E9=80=BB=E8=BE=91=E5=B9=B6=E4=BC=98=E5=8C=96=E7=82=B9?= =?UTF-8?q?=E4=BA=91=E5=9B=BE=E5=83=8F=E7=94=9F=E6=88=90=E3=80=82=E4=BD=BF?= =?UTF-8?q?=E7=94=A8PointCloudCanvas=E9=87=8D=E6=9E=84=E6=A3=92=E6=9D=90?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E7=BB=93=E6=9E=9C=E7=9A=84=E5=8F=AF=E8=A7=86?= =?UTF-8?q?=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Presenter/Src/DetectPresenter.cpp | 9 +- .../Presenter/Src/DetectPresenter.cpp | 113 +++--------------- .../Presenter/Src/DetectPresenter.cpp | 11 +- 3 files changed, 20 insertions(+), 113 deletions(-) diff --git a/App/HoleDetection/HoleDetectionApp/Presenter/Src/DetectPresenter.cpp b/App/HoleDetection/HoleDetectionApp/Presenter/Src/DetectPresenter.cpp index 5a98641..ed1040e 100644 --- a/App/HoleDetection/HoleDetectionApp/Presenter/Src/DetectPresenter.cpp +++ b/App/HoleDetection/HoleDetectionApp/Presenter/Src/DetectPresenter.cpp @@ -51,15 +51,8 @@ int DetectPresenter::DetectHoles( return ERR_CODE(DEV_DATA_INVALID); } - // 保存debug数据 - // 调试模式下先保存原始激光线数据,便于后续离线复现问题。 + // debug保存点云已由 BasePresenter::DetectTask() 统一处理,此处不再重复保存 std::string timeStamp = CVrDateUtils::GetNowTime(); - if (debugParam.enableDebug && debugParam.savePointCloud) { - LOG_INFO("[Algo Thread] Debug mode is enabled, saving point cloud data\n"); - - std::string fileName = debugParam.debugOutputPath + "/Laserline_" + std::to_string(cameraIndex) + "_" + timeStamp + ".txt"; - dataLoader.SaveLaserScanData(fileName, laserLines, laserLines.size(), 0.0, 0, 0); - } int nRet = SUCCESS; diff --git a/App/RodAndBarPosition/RodAndBarPositionApp/Presenter/Src/DetectPresenter.cpp b/App/RodAndBarPosition/RodAndBarPositionApp/Presenter/Src/DetectPresenter.cpp index ae12c80..8a4b1e0 100644 --- a/App/RodAndBarPosition/RodAndBarPositionApp/Presenter/Src/DetectPresenter.cpp +++ b/App/RodAndBarPosition/RodAndBarPositionApp/Presenter/Src/DetectPresenter.cpp @@ -3,11 +3,7 @@ #include "AlgoParamConverter.h" #include "IHandEyeCalib.h" #include -#define _USE_MATH_DEFINES -#include #include -#include -#include #include namespace @@ -68,17 +64,8 @@ int DetectPresenter::DetectRod( cameraCalibParam = &cameraCalibParamValue; } - // 保存debug数据 + // debug保存点云已由 BasePresenter::DetectTask() 统一处理,此处不再重复保存 std::string timeStamp = CVrDateUtils::GetNowTime(); - if(debugParam.enableDebug && debugParam.savePointCloud){ - LOG_INFO("[Algo Thread] Debug mode is enabled, saving point cloud data\n"); - - // 获取当前时间戳,格式为YYYYMMDDHHMMSS - std::string fileName = debugParam.debugOutputPath + "/Laserline_" + std::to_string(cameraIndex) + "_" + timeStamp + ".txt"; - - // 直接使用统一格式保存数据 - dataLoader.SaveLaserScanData(fileName, laserLines, laserLines.size(), 0.0, 0, 0); - } int nRet = SUCCESS; @@ -136,96 +123,32 @@ int DetectPresenter::DetectRod( const HECCalibResult calibResult = HECCalibResult::fromHomogeneousArray(clibMatrix); const HECEulerOrder hecEulerOrder = ToHandEyeEulerOrder(eulerOrder); - // 构建检测结果:生成点云图像 - // 1. 获取所有棒材的中心点用于可视化 - std::vector> objOps; - std::vector rodCenters; + // 使用 PointCloudCanvas 生成点云图像(灰色底图 + 棒材中心/方向线标记) + { + PointCloudCanvas canvas = PointCloudCanvas::Create(xyzData); - for (const auto& rod : rodInfo) { - SVzNL3DPoint pt; - pt.x = rod.center.x; - pt.y = rod.center.y; - pt.z = rod.center.z; - rodCenters.push_back(pt); - } - if (!rodCenters.empty()) { - objOps.push_back(rodCenters); - } + for (size_t i = 0; i < rodInfo.size(); i++) { + const auto& rod = rodInfo[i]; - // 从点云数据生成投影图像(10cm边界),同时获取点云原始范围 - double margin = 100.0; // 10cm = 100mm - double xMin, xMax, yMin, yMax; - detectionResult.image = PointCloudImageUtils::GeneratePointCloudRetPointImage(xyzData, objOps, margin, &xMin, &xMax, &yMin, &yMax); + // 绘制棒材中心点(红色) + canvas.drawPoint(rod.center.x, rod.center.y, QColor(255, 0, 0), 6); - // 在图像上绘制棒材的轴向方向线 - if (!detectionResult.image.isNull() && !rodInfo.empty()) { - QPainter painter(&detectionResult.image); - painter.setRenderHint(QPainter::Antialiasing); - - // 扩展边界(与GeneratePointCloudRetPointImage相同) - xMin -= margin; - xMax += margin; - yMin -= margin; - yMax += margin; - - // 使用与GeneratePointCloudRetPointImage相同的参数 - int imgRows = detectionResult.image.height(); - int imgCols = detectionResult.image.width(); - int x_skip = 50; - int y_skip = 50; - - // 计算投影比例(与PointCloudImageUtils相同的方式) - double y_rows = (double)(imgRows - y_skip * 2); - double x_cols = (double)(imgCols - x_skip * 2); - double x_scale = (xMax - xMin) / x_cols; - double y_scale = (yMax - yMin) / y_rows; - - // 使用统一的比例尺 - double scale = std::max(x_scale, y_scale); - x_scale = scale; - y_scale = scale; - - // 计算点云在图像中居中的偏移量(与PointCloudImageUtils一致) - double cloudWidth = (xMax - xMin) / scale; - double cloudHeight = (yMax - yMin) / scale; - int x_offset = x_skip + (int)((x_cols - cloudWidth) / 2); - int y_offset = y_skip + (int)((y_rows - cloudHeight) / 2); - - // 转换3D坐标到图像坐标的lambda函数(使用居中偏移) - auto toImageCoord = [&](const SVzNL3DPoint& pt) -> QPointF { - int px = (int)((pt.x - xMin) / x_scale + x_offset); - int py = (int)((pt.y - yMin) / y_scale + y_offset); - return QPointF(px, py); - }; - - // 绘制棒材的轴向方向线 - for (const auto& rod : rodInfo) { // 绘制轴向方向线(红色) double len = 60; - QPen axisPen(QColor(255, 0, 0), 2); - painter.setPen(axisPen); - - SVzNL3DPoint pt0 = { rod.center.x - len * rod.axialDir.x, - rod.center.y - len * rod.axialDir.y, - rod.center.z - len * rod.axialDir.z }; - SVzNL3DPoint pt1 = { rod.center.x + len * rod.axialDir.x, - rod.center.y + len * rod.axialDir.y, - rod.center.z + len * rod.axialDir.z }; - - QPointF imgPt0 = toImageCoord(pt0); - QPointF imgPt1 = toImageCoord(pt1); - painter.drawLine(imgPt0, imgPt1); + double ax0 = rod.center.x - len * rod.axialDir.x; + double ay0 = rod.center.y - len * rod.axialDir.y; + double ax1 = rod.center.x + len * rod.axialDir.x; + double ay1 = rod.center.y + len * rod.axialDir.y; + canvas.drawLine(ax0, ay0, ax1, ay1, QColor(255, 0, 0), 2); // 绘制起点到终点线段(绿色) - QPen segPen(QColor(0, 255, 0), 2); - painter.setPen(segPen); + canvas.drawLine(rod.startPt.x, rod.startPt.y, rod.endPt.x, rod.endPt.y, QColor(0, 255, 0), 2); - SVzNL3DPoint startPt = { rod.startPt.x, rod.startPt.y, rod.startPt.z }; - SVzNL3DPoint endPt = { rod.endPt.x, rod.endPt.y, rod.endPt.z }; - QPointF imgStart = toImageCoord(startPt); - QPointF imgEnd = toImageCoord(endPt); - painter.drawLine(imgStart, imgEnd); + // 中心点白色编号 + canvas.drawText(rod.center.x, rod.center.y, QString("%1").arg(i + 1), Qt::white, 14); } + + detectionResult.image = canvas.image(); } // 转换检测结果为UI显示格式(使用机械臂坐标系数据) diff --git a/App/WorkpieceHole/WorkpieceHoleApp/Presenter/Src/DetectPresenter.cpp b/App/WorkpieceHole/WorkpieceHoleApp/Presenter/Src/DetectPresenter.cpp index 5d19d58..1255bdf 100644 --- a/App/WorkpieceHole/WorkpieceHoleApp/Presenter/Src/DetectPresenter.cpp +++ b/App/WorkpieceHole/WorkpieceHoleApp/Presenter/Src/DetectPresenter.cpp @@ -45,17 +45,8 @@ int DetectPresenter::DetectWorkpieceHole( cameraCalibParam = &cameraCalibParamValue; } - // 保存debug数据 + // debug保存点云已由 BasePresenter::DetectTask() 统一处理,此处不再重复保存 std::string timeStamp = CVrDateUtils::GetNowTime(); - if(debugParam.enableDebug && debugParam.savePointCloud){ - LOG_INFO("[Algo Thread] Debug mode is enabled, saving point cloud data\n"); - - // 获取当前时间戳,格式为YYYYMMDDHHMMSS - std::string fileName = debugParam.debugOutputPath + "/Laserline_" + std::to_string(cameraIndex) + "_" + timeStamp + ".txt"; - - // 直接使用统一格式保存数据 - dataLoader.SaveLaserScanData(fileName, laserLines, laserLines.size(), 0.0, 0, 0); - } int nRet = SUCCESS;