2026-03-22 17:31:41 +08:00
|
|
|
|
#include "DetectPresenter.h"
|
|
|
|
|
|
#include "rodAndBarDetection_Export.h"
|
2026-03-25 09:35:56 +08:00
|
|
|
|
#include "AlgoParamConverter.h"
|
2026-03-29 10:48:35 +08:00
|
|
|
|
#include "IHandEyeCalib.h"
|
2026-03-22 17:31:41 +08:00
|
|
|
|
#include <fstream>
|
|
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
|
|
#include <cmath>
|
2026-03-29 10:48:35 +08:00
|
|
|
|
#include <memory>
|
2026-03-22 17:31:41 +08:00
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
#include <QPen>
|
|
|
|
|
|
#include <QColor>
|
|
|
|
|
|
|
2026-03-29 10:48:35 +08:00
|
|
|
|
namespace
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
HECEulerOrder ToHandEyeEulerOrder(int eulerOrder)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (eulerOrder) {
|
|
|
|
|
|
case 10: return HECEulerOrder::XYZ;
|
|
|
|
|
|
case 11: return HECEulerOrder::ZYX;
|
|
|
|
|
|
case 12: return HECEulerOrder::ZXY;
|
|
|
|
|
|
case 13: return HECEulerOrder::YXZ;
|
|
|
|
|
|
case 14: return HECEulerOrder::YZX;
|
|
|
|
|
|
case 15: return HECEulerOrder::XZY;
|
|
|
|
|
|
default:
|
|
|
|
|
|
LOG_WARNING("Unsupported euler order %d, fallback to 11(sZYX)\n", eulerOrder);
|
|
|
|
|
|
return HECEulerOrder::ZYX;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
2026-03-22 17:31:41 +08:00
|
|
|
|
DetectPresenter::DetectPresenter(/* args */)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOG_DEBUG("DetectPresenter Init algo ver: %s\n", wd_rodAndBarDetectionVersion());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DetectPresenter::~DetectPresenter()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-25 09:35:56 +08:00
|
|
|
|
QString DetectPresenter::GetAlgoVersion()
|
|
|
|
|
|
{
|
|
|
|
|
|
return QString(wd_rodAndBarDetectionVersion());
|
|
|
|
|
|
}
|
2026-03-22 17:31:41 +08:00
|
|
|
|
|
|
|
|
|
|
int DetectPresenter::DetectRod(
|
|
|
|
|
|
int cameraIndex,
|
|
|
|
|
|
std::vector<std::pair<EVzResultDataType, SVzLaserLineData>>& laserLines,
|
|
|
|
|
|
const VrAlgorithmParams& algorithmParams,
|
|
|
|
|
|
const VrDebugParam& debugParam,
|
|
|
|
|
|
LaserDataLoader& dataLoader,
|
|
|
|
|
|
const double clibMatrix[16],
|
2026-03-29 10:48:35 +08:00
|
|
|
|
int eulerOrder,
|
|
|
|
|
|
int dirVectorInvert,
|
2026-03-29 20:37:36 +08:00
|
|
|
|
int longAxisDir,
|
2026-03-22 17:31:41 +08:00
|
|
|
|
DetectionResult& detectionResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (laserLines.empty()) {
|
|
|
|
|
|
LOG_WARNING("No laser lines data available\n");
|
|
|
|
|
|
return ERR_CODE(DEV_DATA_INVALID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前相机的校准参数
|
|
|
|
|
|
VrCameraPlaneCalibParam cameraCalibParamValue;
|
|
|
|
|
|
const VrCameraPlaneCalibParam* cameraCalibParam = nullptr;
|
|
|
|
|
|
if (algorithmParams.planeCalibParam.GetCameraCalibParam(cameraIndex, cameraCalibParamValue)) {
|
|
|
|
|
|
cameraCalibParam = &cameraCalibParamValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存debug数据
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
// 转换为算法需要的XYZ格式
|
|
|
|
|
|
std::vector<std::vector<SVzNL3DPosition>> xyzData;
|
|
|
|
|
|
int convertResult = dataLoader.ConvertToSVzNL3DPosition(laserLines, xyzData);
|
|
|
|
|
|
if (convertResult != SUCCESS || xyzData.empty()) {
|
|
|
|
|
|
LOG_WARNING("Failed to convert data to XYZ format or no XYZ data available\n");
|
|
|
|
|
|
return ERR_CODE(DEV_DATA_INVALID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-25 09:35:56 +08:00
|
|
|
|
// 使用 AlgoParamConverter 进行参数转换
|
|
|
|
|
|
SSX_rodParam rodParam = AlgoParamConverter::ToAlgoParam(algorithmParams.rodParam);
|
|
|
|
|
|
SSG_cornerParam cornerParam = AlgoParamConverter::ToAlgoParam(algorithmParams.cornerParam);
|
|
|
|
|
|
SSG_treeGrowParam growParam = AlgoParamConverter::ToAlgoParam(algorithmParams.growParam);
|
|
|
|
|
|
SSG_outlierFilterParam filterParam = AlgoParamConverter::ToAlgoParam(algorithmParams.filterParam);
|
2026-03-22 17:31:41 +08:00
|
|
|
|
|
|
|
|
|
|
// 构建平面标定参数
|
2026-03-25 09:35:56 +08:00
|
|
|
|
SSG_planeCalibPara poseCalibPara = AlgoParamConverter::ToAlgoPlaneCalibParam(cameraCalibParam);
|
2026-03-22 17:31:41 +08:00
|
|
|
|
|
|
|
|
|
|
if(debugParam.enableDebug && debugParam.printDetailLog)
|
|
|
|
|
|
{
|
2026-03-25 09:35:56 +08:00
|
|
|
|
AlgoParamConverter::LogAlgoParams("[Algo Thread]", rodParam, cornerParam, filterParam, growParam, clibMatrix);
|
2026-03-22 17:31:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int errCode = 0;
|
|
|
|
|
|
CVrTimeUtils oTimeUtils;
|
|
|
|
|
|
|
|
|
|
|
|
LOG_DEBUG("before sx_rodPositioning \n");
|
|
|
|
|
|
|
|
|
|
|
|
// 调用棒材定位算法
|
|
|
|
|
|
std::vector<SSX_rodPositionInfo> rodInfo;
|
|
|
|
|
|
sx_rodPositioning(
|
|
|
|
|
|
xyzData,
|
|
|
|
|
|
poseCalibPara,
|
|
|
|
|
|
cornerParam,
|
|
|
|
|
|
filterParam,
|
|
|
|
|
|
growParam,
|
|
|
|
|
|
rodParam,
|
|
|
|
|
|
rodInfo,
|
|
|
|
|
|
&errCode);
|
|
|
|
|
|
|
|
|
|
|
|
LOG_DEBUG("after sx_rodPositioning \n");
|
|
|
|
|
|
|
2026-03-29 10:48:35 +08:00
|
|
|
|
LOG_INFO("sx_rodPositioning: detected %zu rods, err=%d runtime=%.3fms\n", rodInfo.size(), errCode, oTimeUtils.GetElapsedTimeInMilliSec());
|
2026-03-22 17:31:41 +08:00
|
|
|
|
ERR_CODE_RETURN(errCode);
|
2026-03-29 10:48:35 +08:00
|
|
|
|
std::unique_ptr<IHandEyeCalib, decltype(&DestroyHandEyeCalibInstance)> handEyeCalib(
|
|
|
|
|
|
CreateHandEyeCalibInstance(),
|
|
|
|
|
|
DestroyHandEyeCalibInstance);
|
|
|
|
|
|
if (!handEyeCalib) {
|
|
|
|
|
|
LOG_ERROR("Failed to create HandEyeCalib instance\n");
|
|
|
|
|
|
return ERR_CODE(DEV_NOT_FIND);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-12 00:32:30 +08:00
|
|
|
|
const HECCalibResult calibResult = HECCalibResult::fromHomogeneousArray(clibMatrix);
|
2026-03-29 10:48:35 +08:00
|
|
|
|
const HECEulerOrder hecEulerOrder = ToHandEyeEulerOrder(eulerOrder);
|
2026-03-22 17:31:41 +08:00
|
|
|
|
|
|
|
|
|
|
// 构建检测结果:生成点云图像
|
|
|
|
|
|
// 1. 获取所有棒材的中心点用于可视化
|
|
|
|
|
|
std::vector<std::vector<SVzNL3DPoint>> objOps;
|
|
|
|
|
|
std::vector<SVzNL3DPoint> rodCenters;
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-02 00:38:50 +08:00
|
|
|
|
// 从点云数据生成投影图像(10cm边界),同时获取点云原始范围
|
|
|
|
|
|
double margin = 100.0; // 10cm = 100mm
|
|
|
|
|
|
double xMin, xMax, yMin, yMax;
|
|
|
|
|
|
detectionResult.image = PointCloudImageUtils::GeneratePointCloudRetPointImage(xyzData, objOps, margin, &xMin, &xMax, &yMin, &yMax);
|
2026-03-22 17:31:41 +08:00
|
|
|
|
|
|
|
|
|
|
// 在图像上绘制棒材的轴向方向线
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制起点到终点线段(绿色)
|
|
|
|
|
|
QPen segPen(QColor(0, 255, 0), 2);
|
|
|
|
|
|
painter.setPen(segPen);
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 转换检测结果为UI显示格式(使用机械臂坐标系数据)
|
|
|
|
|
|
for (size_t i = 0; i < rodInfo.size(); i++) {
|
|
|
|
|
|
const auto& rod = rodInfo[i];
|
2026-03-29 20:37:36 +08:00
|
|
|
|
|
|
|
|
|
|
LOG_INFO("[Algo Thread] Rod %zu Eye Center: X=%.2f, Y=%.2f, Z=%.2f\n", i, rod.center.x, rod.center.y, rod.center.z);
|
|
|
|
|
|
LOG_INFO("[Algo Thread] Rod %zu Input X seed: [%.6f, %.6f, %.6f]\n", i, rod.axialDir.x, rod.axialDir.y, rod.axialDir.z);
|
|
|
|
|
|
LOG_INFO("[Algo Thread] Rod %zu Input Z seed: [%.6f, %.6f, %.6f]\n", i, rod.normalDir.x, rod.normalDir.y, rod.normalDir.z);
|
|
|
|
|
|
|
|
|
|
|
|
HECPoseResult poseResult;
|
|
|
|
|
|
bool validPose = handEyeCalib->TransformPose(
|
|
|
|
|
|
calibResult,
|
|
|
|
|
|
HECPoint3D(rod.center.x, rod.center.y, rod.center.z),
|
|
|
|
|
|
HECPoint3D(rod.axialDir.x, rod.axialDir.y, rod.axialDir.z),
|
|
|
|
|
|
HECPoint3D(rod.normalDir.x, rod.normalDir.y, rod.normalDir.z),
|
|
|
|
|
|
dirVectorInvert,
|
|
|
|
|
|
hecEulerOrder,
|
|
|
|
|
|
longAxisDir == 1 ? HECLongAxisDir::AxisY : HECLongAxisDir::AxisX,
|
|
|
|
|
|
poseResult);
|
2026-03-29 10:48:35 +08:00
|
|
|
|
if (!validPose) {
|
|
|
|
|
|
LOG_WARNING("[Algo Thread] Rod %zu has invalid axial/normal direction, use zero pose\n", i);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-29 20:37:36 +08:00
|
|
|
|
double rollDeg = 0.0, pitchDeg = 0.0, yawDeg = 0.0;
|
|
|
|
|
|
poseResult.angles.toDegrees(rollDeg, pitchDeg, yawDeg);
|
2026-03-22 17:31:41 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建位置数据(使用转换后的机械臂坐标)
|
|
|
|
|
|
RodPosition pos;
|
2026-03-29 10:48:35 +08:00
|
|
|
|
pos.roll = rollDeg;
|
|
|
|
|
|
pos.pitch = pitchDeg;
|
|
|
|
|
|
pos.yaw = yawDeg;
|
2026-03-29 20:37:36 +08:00
|
|
|
|
pos.x = poseResult.position.x;
|
|
|
|
|
|
pos.y = poseResult.position.y;
|
|
|
|
|
|
pos.z = poseResult.position.z;
|
2026-03-22 17:31:41 +08:00
|
|
|
|
|
|
|
|
|
|
detectionResult.positions.push_back(pos);
|
|
|
|
|
|
|
|
|
|
|
|
// 保存棒材信息
|
|
|
|
|
|
RodInfo info;
|
2026-03-29 20:37:36 +08:00
|
|
|
|
info.centerX = poseResult.position.x;
|
|
|
|
|
|
info.centerY = poseResult.position.y;
|
|
|
|
|
|
info.centerZ = poseResult.position.z;
|
2026-03-22 17:31:41 +08:00
|
|
|
|
info.axialDirX = rod.axialDir.x;
|
|
|
|
|
|
info.axialDirY = rod.axialDir.y;
|
|
|
|
|
|
info.axialDirZ = rod.axialDir.z;
|
|
|
|
|
|
info.normalDirX = rod.normalDir.x;
|
|
|
|
|
|
info.normalDirY = rod.normalDir.y;
|
|
|
|
|
|
info.normalDirZ = rod.normalDir.z;
|
|
|
|
|
|
info.startPtX = rod.startPt.x;
|
|
|
|
|
|
info.startPtY = rod.startPt.y;
|
|
|
|
|
|
info.startPtZ = rod.startPt.z;
|
|
|
|
|
|
info.endPtX = rod.endPt.x;
|
|
|
|
|
|
info.endPtY = rod.endPt.y;
|
|
|
|
|
|
info.endPtZ = rod.endPt.z;
|
|
|
|
|
|
detectionResult.rodInfoList.push_back(info);
|
|
|
|
|
|
|
2026-03-29 20:37:36 +08:00
|
|
|
|
// Print key values for coordinate transform debugging
|
|
|
|
|
|
LOG_INFO("[Algo Thread] Rod %zu Robot Pose: X=%.2f, Y=%.2f, Z=%.2f, Roll=%.6f, Pitch=%.6f, Yaw=%.6f\n", i, pos.x, pos.y, pos.z, pos.roll, pos.pitch, pos.yaw);
|
2026-03-22 17:31:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(debugParam.enableDebug && debugParam.saveDebugImage){
|
|
|
|
|
|
// 获取当前时间戳,格式为YYYYMMDDHHMMSS
|
|
|
|
|
|
std::string fileName = debugParam.debugOutputPath + "/Image_" + std::to_string(cameraIndex) + "_" + timeStamp + ".png";
|
|
|
|
|
|
LOG_INFO("[Algo Thread] Debug image saved image : %s\n", fileName.c_str());
|
|
|
|
|
|
|
|
|
|
|
|
// 保存检测结果图片
|
|
|
|
|
|
if (!detectionResult.image.isNull()) {
|
|
|
|
|
|
QString qFileName = QString::fromStdString(fileName);
|
|
|
|
|
|
detectionResult.image.save(qFileName);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
LOG_WARNING("[Algo Thread] No valid image to save for debug\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nRet;
|
|
|
|
|
|
}
|