diff --git a/SG_Algorithm.sln b/SG_Algorithm.sln index c32da6c..ebdea36 100644 --- a/SG_Algorithm.sln +++ b/SG_Algorithm.sln @@ -157,6 +157,17 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BQ_assemblyPosition", "BQ_a EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BQ_assemblyPosition_test", "BQ_assemblyPosition_test\BQ_assemblyPosition_test.vcxproj", "{BC38D1E5-10CB-438B-AC72-6012303CE139}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wheelArchHeigthMeaure", "wheelArchHeigthMeaure\wheelArchHeigthMeaure.vcxproj", "{CFE11556-106A-4216-BF62-FDA980528F7A}" + ProjectSection(ProjectDependencies) = postProject + {95DC3F1A-902A-490E-BD3B-B10463CF0EBD} = {95DC3F1A-902A-490E-BD3B-B10463CF0EBD} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wheelArchHeigthMeasure_test", "wheelArchHeigthMeasure_test\wheelArchHeigthMeasure_test.vcxproj", "{BBF5341E-8447-45E9-ADA3-3E8A9B52F5EF}" + ProjectSection(ProjectDependencies) = postProject + {95DC3F1A-902A-490E-BD3B-B10463CF0EBD} = {95DC3F1A-902A-490E-BD3B-B10463CF0EBD} + {CFE11556-106A-4216-BF62-FDA980528F7A} = {CFE11556-106A-4216-BF62-FDA980528F7A} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -413,6 +424,22 @@ Global {BC38D1E5-10CB-438B-AC72-6012303CE139}.Release|x64.Build.0 = Release|x64 {BC38D1E5-10CB-438B-AC72-6012303CE139}.Release|x86.ActiveCfg = Release|Win32 {BC38D1E5-10CB-438B-AC72-6012303CE139}.Release|x86.Build.0 = Release|Win32 + {CFE11556-106A-4216-BF62-FDA980528F7A}.Debug|x64.ActiveCfg = Debug|x64 + {CFE11556-106A-4216-BF62-FDA980528F7A}.Debug|x64.Build.0 = Debug|x64 + {CFE11556-106A-4216-BF62-FDA980528F7A}.Debug|x86.ActiveCfg = Debug|Win32 + {CFE11556-106A-4216-BF62-FDA980528F7A}.Debug|x86.Build.0 = Debug|Win32 + {CFE11556-106A-4216-BF62-FDA980528F7A}.Release|x64.ActiveCfg = Release|x64 + {CFE11556-106A-4216-BF62-FDA980528F7A}.Release|x64.Build.0 = Release|x64 + {CFE11556-106A-4216-BF62-FDA980528F7A}.Release|x86.ActiveCfg = Release|Win32 + {CFE11556-106A-4216-BF62-FDA980528F7A}.Release|x86.Build.0 = Release|Win32 + {BBF5341E-8447-45E9-ADA3-3E8A9B52F5EF}.Debug|x64.ActiveCfg = Debug|x64 + {BBF5341E-8447-45E9-ADA3-3E8A9B52F5EF}.Debug|x64.Build.0 = Debug|x64 + {BBF5341E-8447-45E9-ADA3-3E8A9B52F5EF}.Debug|x86.ActiveCfg = Debug|Win32 + {BBF5341E-8447-45E9-ADA3-3E8A9B52F5EF}.Debug|x86.Build.0 = Debug|Win32 + {BBF5341E-8447-45E9-ADA3-3E8A9B52F5EF}.Release|x64.ActiveCfg = Release|x64 + {BBF5341E-8447-45E9-ADA3-3E8A9B52F5EF}.Release|x64.Build.0 = Release|x64 + {BBF5341E-8447-45E9-ADA3-3E8A9B52F5EF}.Release|x86.ActiveCfg = Release|Win32 + {BBF5341E-8447-45E9-ADA3-3E8A9B52F5EF}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/sourceCode/SG_baseAlgo_Export.h b/sourceCode/SG_baseAlgo_Export.h index 109ed8b..29129d2 100644 --- a/sourceCode/SG_baseAlgo_Export.h +++ b/sourceCode/SG_baseAlgo_Export.h @@ -59,6 +59,14 @@ SG_APISHARED_EXPORT void sg_getLineCornerFeature( const SSG_cornerParam cornerPara, //scale通常取bagH的1/4 SSG_lineFeature* line_features); +// 对激光线上由Mask(nPointIdx转义定义)指定的点提取激光线上的拐点特征 +SG_APISHARED_EXPORT void sg_maskData_getLineCornerFeature( + std::vector< SVzNL3DPosition>& lineData, + int lineIdx, + int maskID, + const SSG_cornerParam cornerPara, //scale通常取bagH的1/4 + std::vector& features); + SG_APISHARED_EXPORT void sg_getLineCornerFeature_BQ( SVzNL3DPosition* lineData, int dataSize, @@ -67,6 +75,20 @@ SG_APISHARED_EXPORT void sg_getLineCornerFeature_BQ( const SSG_cornerParam cornerPara, std::vector& line_features); +SG_APISHARED_EXPORT void wd_getLineDataIntervals( + std::vector& lineData, + const SSG_lineSegParam lineSegPara, + std::vector& segs); + +// 最小点集数量(小于此数无法拟合直线) +const int MIN_POINT_COUNT = 3; +//使用端点直线,检查点到直线的距离,大于门限的分割 +SG_APISHARED_EXPORT void split( + SSG_RUN a_run, + std::vector< SVzNL3DPosition>& lineData, + const double maxError, + std::vector< SSG_RUN>& lineSegs); + /// /// 提取激光线上的特征:跳变、低于z阈值、V及L型,用于粒径检测(PSM) /// seg端点:z距离大于门限 @@ -360,6 +382,12 @@ SG_APISHARED_EXPORT double fitCircleByLeastSquare( SVzNL3DPoint& center, double& radius); +//抛物线最小二乘拟合 y=ax^2 + bx + c +SG_APISHARED_EXPORT bool leastSquareParabolaFitEigen( + const std::vector& points, + double& a, double& b, double& c, + double& mse, double& max_err); + //计算Z均值 SG_APISHARED_EXPORT double computeMeanZ(std::vector< SVzNL3DPoint>& pts); @@ -446,6 +474,9 @@ SG_APISHARED_EXPORT SSG_planeCalibPara sg_getPlaneCalibPara( SVzNL3DLaserLine* laser3DPoints, int lineNum); +SG_APISHARED_EXPORT SSG_planeCalibPara sg_HCameraVScan_getGroundCalibPara( + std::vector< std::vector>& scanLines); + //将一个平面调整为水平平面(z为固定值), SG_APISHARED_EXPORT SSG_planeCalibPara adjustPlaneToXYPlane( double plane_A, double plane_B, double plane_C //平面法向量 @@ -487,6 +518,10 @@ SG_APISHARED_EXPORT void lineDataRT_vector( std::vector< SVzNL3DPosition>& a_line, const double* camPoseR, double groundH); +SG_APISHARED_EXPORT void HCamera_lineDataRT_vector( + std::vector< SVzNL3DPosition>& a_line, + const double* camPoseR, + double groundH); SG_APISHARED_EXPORT void lineDataRT_RGBD( SVzNLXYZRGBDLaserLine* a_line, const double* camPoseR, diff --git a/sourceCode/SG_baseDataType.h b/sourceCode/SG_baseDataType.h index b7dec1f..3102cf8 100644 --- a/sourceCode/SG_baseDataType.h +++ b/sourceCode/SG_baseDataType.h @@ -525,3 +525,10 @@ typedef struct double cornerAngle; int cornerDir; }SWD_polarPeakInfo; + +typedef struct +{ + int clusterIdx; + int ptSize; + SVzNL3DRangeD roi3D; +}SWD_clustersInfo; diff --git a/sourceCode/SG_baseFunc.cpp b/sourceCode/SG_baseFunc.cpp index 8cfbee9..485ed9c 100644 --- a/sourceCode/SG_baseFunc.cpp +++ b/sourceCode/SG_baseFunc.cpp @@ -346,6 +346,148 @@ double fitCircleByLeastSquare( return err; } +#if 0 +bool leastSquareParabolaFit(const std::vector& points, + double& a, double& b, double& c, + double& mse, double& max_err) +{ + // 校验点集数量(至少3个点才能拟合抛物线) + int n = points.size(); + if (n < 3) { + return false; + } + + // 初始化各项求和参数 + double sum_x = 0.0, sum_x2 = 0.0, sum_x3 = 0.0, sum_x4 = 0.0; + double sum_y = 0.0, sum_xy = 0.0, sum_x2y = 0.0; + + // 计算各项求和 + for (const auto& p : points) { + double x = p.x; + double y = p.y; + double x2 = x * x; + double x3 = x2 * x; + double x4 = x3 * x; + + sum_x += x; + sum_x2 += x2; + sum_x3 += x3; + sum_x4 += x4; + sum_y += y; + sum_xy += x * y; + sum_x2y += x2 * y; + } + + // 构建线性方程组 M * [a,b,c]^T = N + // M矩阵:3x3 + double M[3][3] = { + {sum_x4, sum_x3, sum_x2}, + {sum_x3, sum_x2, sum_x}, + {sum_x2, sum_x, (double)n} + }; + + // N向量:3x1 + double N[3] = { sum_x2y, sum_xy, sum_y }; + + // 高斯消元法求解线性方程组(3元一次方程组) + // 步骤1:将M转化为上三角矩阵 + for (int i = 0; i < 3; i++) { + // 选主元(避免除数为0) + int pivot = i; + for (int j = i; j < 3; j++) { + if (fabs(M[j][i]) > fabs(M[pivot][i])) { + pivot = j; + } + } + // 交换行 + std::swap(M[i], M[pivot]); + std::swap(N[i], N[pivot]); + + // 归一化主元行 + double div = M[i][i]; + if (fabs(div) < 1e-10) { + return false; + } + for (int j = i; j < 3; j++) { + M[i][j] /= div; + } + N[i] /= div; + + // 消去下方行 + for (int j = i + 1; j < 3; j++) { + double factor = M[j][i]; + for (int k = i; k < 3; k++) { + M[j][k] -= factor * M[i][k]; + } + N[j] -= factor * N[i]; + } + } + + // 步骤2:回代求解 + c = N[2]; + b = N[1] - M[1][2] * c; + a = N[0] - M[0][1] * b - M[0][2] * c; + + // 计算拟合误差 + mse = 0.0; + max_err = 0.0; + for (const auto& p : points) { + double y_fit = a * p.x * p.x + b * p.x + c; + double err = y_fit - p.y; + double err_abs = fabs(err); + mse += err * err; + if (err_abs > max_err) { + max_err = err_abs; + } + } + mse /= n; // 均方误差 + + return true; +} +#endif +bool leastSquareParabolaFitEigen( + const std::vector& points, + double& a, double& b, double& c, + double& mse, double& max_err) +{ + int n = points.size(); + if (n < 3) { + return false; + } + + // 构建系数矩阵A和目标向量B + Eigen::MatrixXd A(n, 3); + Eigen::VectorXd B(n); + for (int i = 0; i < n; i++) { + double x = points[i].x; + A(i, 0) = x * x; + A(i, 1) = x; + A(i, 2) = 1.0; + B(i) = points[i].y; + } + + // 最小二乘求解:Ax = B,直接调用Eigen的求解器 + Eigen::Vector3d coeffs = A.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(B); + a = coeffs(0); + b = coeffs(1); + c = coeffs(2); + + // 计算误差 + mse = 0.0; + max_err = 0.0; + for (const auto& p : points) { + double y_fit = a * p.x * p.x + b * p.x + c; + double err = y_fit - p.y; + double err_abs = fabs(err); + mse += err * err; + if (err_abs > max_err) { + max_err = err_abs; + } + } + mse /= n; + return true; +} + //计算Z均值 double computeMeanZ(std::vector< SVzNL3DPoint>& pts) { @@ -1915,6 +2057,162 @@ SSG_planeCalibPara sg_getPlaneCalibPara2( return planePara; } +//水平安装相机垂直扫描模式地面调平 +SSG_planeCalibPara sg_HCameraVScan_getGroundCalibPara( + std::vector< std::vector>& scanLines) +{ + //设置初始结果 + double initCalib[9] = { + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0 }; + SSG_planeCalibPara planePara; + for (int i = 0; i < 9; i++) + planePara.planeCalib[i] = initCalib[i]; + planePara.planeHeight = -1.0; + + //提取地面直线段 + SSG_lineSegParam lineSegPara; + lineSegPara.maxDist = 2.0; + lineSegPara.segGapTh_y = 5.0; //y方向间隔大于5mm认为是分段 + lineSegPara.segGapTh_z = 10.0; //z方向间隔大于10mm认为是分段 + + std::vector groundPts; + int lineNum = (int)scanLines.size(); + for (int line = 0; line < lineNum; line++) + { + std::vector& lineData = scanLines[line]; + int dataSize = (int)lineData.size(); + //去除零点 + std::vector segs; + wd_getLineDataIntervals(lineData, lineSegPara, segs); + if (segs.size() == 0) + continue; + + //对最后一段进行处理 + SSG_RUN lastSeg = segs.back(); + //直线分割 + std::vector< SSG_RUN> segmentationLines; + split(lastSeg, lineData, lineSegPara.maxDist, segmentationLines); + //检查最后一段的直线段的斜率 + SSG_RUN lastLine = segmentationLines.back(); + //计算斜率 + int startIdx = lastLine.start; + int endIdx = lastLine.start + lastLine.len - 1; + double dy = abs(lineData[endIdx].pt3D.y - lineData[startIdx].pt3D.y) + 1e-8; //加扰,防止dy为0 + double dz = lineData[startIdx].pt3D.z - lineData[endIdx].pt3D.z; + if (dz > 0) + { + double tan_k = dz / dy; + if (tan_k > tan(PI / 3)) //大于60度,合格 + { + for (int i = startIdx; i <= endIdx; i++) + { + if (lineData[i].pt3D.z > 1e-4) + { + lineData[i].nPointIdx = 1; + cv::Point3f a_pt = cv::Point3f(lineData[i].pt3D.x, lineData[i].pt3D.y, lineData[i].pt3D.z); + groundPts.push_back(a_pt); + } + } + } + } + } + + //平面拟合 + std::vector planceFunc; + vzCaculateLaserPlane(groundPts, planceFunc); + +#if 1 //两个向量的旋转旋转,使用四元数法, + Vector3 a = Vector3(planceFunc[0], planceFunc[1], planceFunc[2]); + Vector3 b = Vector3(0, 1.0, 0); + Quaternion quanPara = rotationBetweenVectors(a, b); + + RotationMatrix rMatrix; + quaternionToMatrix(quanPara, rMatrix.data); + //计算反向旋转矩阵 + Quaternion invQuanPara = rotationBetweenVectors(b, a); + RotationMatrix invMatrix; + quaternionToMatrix(invQuanPara, invMatrix.data); +#else //根据平面的法向量计算欧拉角,进而计算旋转矩阵 + //参数计算 + SSG_EulerAngles eulerPra = planeNormalToEuler(planceFunc[0], planceFunc[1], planceFunc[2]); + //反射进行校正 + eulerPra.roll = eulerPra.roll; + eulerPra.pitch = eulerPra.pitch; + eulerPra.yaw = eulerPra.yaw; + RotationMatrix rMatrix = eulerToRotationMatrix(eulerPra.yaw, eulerPra.pitch, eulerPra.roll); +#endif + + planePara.planeCalib[0] = rMatrix.data[0][0]; + planePara.planeCalib[1] = rMatrix.data[0][1]; + planePara.planeCalib[2] = rMatrix.data[0][2]; + planePara.planeCalib[3] = rMatrix.data[1][0]; + planePara.planeCalib[4] = rMatrix.data[1][1]; + planePara.planeCalib[5] = rMatrix.data[1][2]; + planePara.planeCalib[6] = rMatrix.data[2][0]; + planePara.planeCalib[7] = rMatrix.data[2][1]; + planePara.planeCalib[8] = rMatrix.data[2][2]; + + planePara.invRMatrix[0] = invMatrix.data[0][0]; + planePara.invRMatrix[1] = invMatrix.data[0][1]; + planePara.invRMatrix[2] = invMatrix.data[0][2]; + planePara.invRMatrix[3] = invMatrix.data[1][0]; + planePara.invRMatrix[4] = invMatrix.data[1][1]; + planePara.invRMatrix[5] = invMatrix.data[1][2]; + planePara.invRMatrix[6] = invMatrix.data[2][0]; + planePara.invRMatrix[7] = invMatrix.data[2][1]; + planePara.invRMatrix[8] = invMatrix.data[2][2]; + +#if 0 //test: 两个矩阵的乘积必须是单位阵 + double testMatrix[3][3]; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + testMatrix[i][j] = 0; + for (int m = 0; m < 3; m++) + testMatrix[i][j] += invMatrix.data[i][m] * rMatrix.data[m][j]; + } + } +#endif + //数据进行转换 + SVzNLRangeD calibYRange = { 0, -1 }; + SVzNLRangeD topYRange = { 0, -1 }; + double sumMeanY = 0; + int sumSize = 0; + for (int i = 0, i_max = (int)groundPts.size(); i < i_max; i++) + { + + cv::Point3f a_calibPt; + a_calibPt.x = (float)(groundPts[i].x * planePara.planeCalib[0] + groundPts[i].y * planePara.planeCalib[1] + groundPts[i].z * planePara.planeCalib[2]); + a_calibPt.y = (float)(groundPts[i].x * planePara.planeCalib[3] + groundPts[i].y * planePara.planeCalib[4] + groundPts[i].z * planePara.planeCalib[5]); + a_calibPt.z = (float)(groundPts[i].x * planePara.planeCalib[6] + groundPts[i].y * planePara.planeCalib[7] + groundPts[i].z * planePara.planeCalib[8]); + //z + if (calibYRange.max < calibYRange.min) + { + calibYRange.min = a_calibPt.y; + calibYRange.max = a_calibPt.y; + sumMeanY += a_calibPt.y; + sumSize++; + } + else + { + if (calibYRange.min > a_calibPt.y) + calibYRange.min = a_calibPt.y; + if (calibYRange.max < a_calibPt.y) + calibYRange.max = a_calibPt.y; + sumMeanY += a_calibPt.y; + sumSize++; + } + } + if (sumSize > 0) + sumMeanY = sumMeanY / (double)sumSize; + planePara.planeHeight = sumMeanY; // calibZRange.min; + + return planePara; +} + SSG_planeCalibPara sg_getPlaneCalibPara2_ROI( std::vector< std::vector>& scanLines, SVzNL3DRangeD roi) @@ -2311,6 +2609,28 @@ void lineDataRT_vector(std::vector< SVzNL3DPosition>& a_line, const double* camP } return; } + +void HCamera_lineDataRT_vector(std::vector< SVzNL3DPosition>& a_line, const double* camPoseR, double groundH) +{ + for (int i = 0; i < a_line.size(); i++) + { + SVzNL3DPoint a_pt = a_line[i].pt3D; + if (a_pt.z < 1e-4) + continue; + double x = a_pt.x * camPoseR[0] + a_pt.y * camPoseR[1] + a_pt.z * camPoseR[2]; + double y = a_pt.x * camPoseR[3] + a_pt.y * camPoseR[4] + a_pt.z * camPoseR[5]; + double z = a_pt.x * camPoseR[6] + a_pt.y * camPoseR[7] + a_pt.z * camPoseR[8]; + if ((groundH > 0) && (y >= groundH)) //去除地面 + z = 0; + a_pt.x = x; + a_pt.y = y; + a_pt.z = z; + a_line[i].pt3D = a_pt; + } + return; +} + + void lineDataRT_RGBD(SVzNLXYZRGBDLaserLine* a_line, const double* camPoseR, double groundH) { for (int i = 0; i < a_line->nPointCnt; i++) diff --git a/sourceCode/SG_errCode.h b/sourceCode/SG_errCode.h index 1266374..8c58124 100644 --- a/sourceCode/SG_errCode.h +++ b/sourceCode/SG_errCode.h @@ -25,3 +25,6 @@ //拆包 #define SX_BAG_TRAY_EMPTY -2201 +//汽车轮眉高度测量 +#define SX_ERR_INVALID_ARC -2301 + diff --git a/sourceCode/SG_lineFeature.cpp b/sourceCode/SG_lineFeature.cpp index c63b5d1..a58b704 100644 --- a/sourceCode/SG_lineFeature.cpp +++ b/sourceCode/SG_lineFeature.cpp @@ -205,6 +205,55 @@ if (i == 370) return; } +void wd_getLineDataIntervals( + std::vector& lineData, + const SSG_lineSegParam lineSegPara, + std::vector& segs) +{ + int runIdx = 1; + SSG_RUN a_run = { 0, -1, 0 }; //startIdx, len, lastIdx + double pre_z = 0; + double pre_y = 0; + int dataSize = (int)lineData.size(); + for (int i = 0; i < dataSize; i++) + { + lineData[i].nPointIdx = 0; //转义使用 + if (lineData[i].pt3D.z > 1e-4) + { + if (a_run.len < 0) + { + a_run.start = i; + a_run.len = 1; + a_run.value = i; + } + else + { + double z_diff = abs(lineData[i].pt3D.z - pre_z); + double y_diff = abs(lineData[i].pt3D.y - pre_y); + if ((z_diff < lineSegPara.segGapTh_z) && (y_diff < lineSegPara.segGapTh_y)) + { + a_run.len = i - a_run.start + 1; + a_run.value = i; + } + else + { + a_run.value = runIdx; + segs.push_back(a_run); + runIdx++; + + a_run.start = i; + a_run.len = 1; + a_run.value = i; + } + } + pre_z = lineData[i].pt3D.z; + pre_y = lineData[i].pt3D.y; + } + } + if (a_run.len > 0) + segs.push_back(a_run); +} + //滤除离群点:z跳变门限方法 void sg_lineDataRemoveOutlier_changeOriginData( SVzNL3DPosition* lineData, @@ -1500,6 +1549,233 @@ void sg_getLineCornerFeature( return; } +/// +/// 对激光线上由Mask(nPointIdx转义定义)指定的点提取激光线上的拐点特征 +/// 算法流程: +/// (1)逐点计算前向角和后向角 +/// (2)逐点计算拐角,顺时针为负,逆时针为正 +/// (3)搜索正拐角的极大值。 +/// (4)判断拐角是否为跳变 +/// +void sg_maskData_getLineCornerFeature( + std::vector< SVzNL3DPosition>& lineData, + int lineIdx, + int maskID, + const SSG_cornerParam cornerPara, //scale通常取bagH的1/4 + std::vector& features) +{ + //去除零点 + int dataSize = (int)lineData.size(); + std::vector< SVzNL3DPosition> vldPts; + for (int i = 0; i < dataSize; i++) + { + SVzNL3DPosition a_pt = lineData[i]; + a_pt.nPointIdx = i; + if ( (lineData[i].pt3D.z > 1e-4) &&(lineData[i].nPointIdx == maskID)) + vldPts.push_back(a_pt); + } + //计算前向角和后向角 + std::vector< SSG_pntDirAngle> corners; + corners.resize(vldPts.size()); + for (int i = 0, i_max = (int)vldPts.size(); i < i_max; i++) + { + if (vldPts[i].nPointIdx == 521) + int kkk = 1; + //前向寻找 + int pre_i = -1; + for (int j = i - 1; j >= 0; j--) + { + double dist = sqrt(pow(vldPts[i].pt3D.y - vldPts[j].pt3D.y, 2) + + pow(vldPts[i].pt3D.z - vldPts[j].pt3D.z, 2)); + if (dist >= cornerPara.scale * 4) + { + pre_i = -1; + break; + } + else if (dist >= cornerPara.scale) + { + pre_i = j; + break; + } + } + //后向寻找 + int post_i = -1; + for (int j = i + 1; j < i_max; j++) + { + double dist = sqrt(pow(vldPts[i].pt3D.y - vldPts[j].pt3D.y, 2) + + pow(vldPts[i].pt3D.z - vldPts[j].pt3D.z, 2)); + if (dist >= cornerPara.scale * 4) + { + post_i = -1; + break; + } + else if (dist >= cornerPara.scale) + { + post_i = j; + break; + } + } + //计算拐角 + if ((pre_i < 0) || (post_i < 0)) + { + corners[i].pntIdx = -1; + corners[i].forwardAngle = 0; + corners[i].backwardAngle = 0; + corners[i].corner = 0; + corners[i].forwardDiffZ = 0; + corners[i].backwardDiffZ = 0; + } + else + { + double tanValue_pre = (vldPts[i].pt3D.z - vldPts[pre_i].pt3D.z) / abs(vldPts[i].pt3D.y - vldPts[pre_i].pt3D.y); + double tanValue_post = (vldPts[post_i].pt3D.z - vldPts[i].pt3D.z) / abs(vldPts[post_i].pt3D.y - vldPts[i].pt3D.y); + double forwardAngle = atan(tanValue_post) * 180.0 / PI; + double backwardAngle = atan(tanValue_pre) * 180.0 / PI; + corners[i].pntIdx = i; + corners[i].forwardAngle = forwardAngle; + corners[i].backwardAngle = backwardAngle; + corners[i].corner = -(forwardAngle - backwardAngle); //图像坐标系与正常坐标系y方向相反,所以有“-”号 + corners[i].forwardDiffZ = vldPts[post_i].pt3D.z - vldPts[i].pt3D.z; + corners[i].backwardDiffZ = vldPts[i].pt3D.z - vldPts[pre_i].pt3D.z; + } + } + + //搜索拐角极值 + int _state = 0; + int pre_i = -1; + int sEdgePtIdx = -1; + int eEdgePtIdx = -1; + SSG_pntDirAngle* pre_data = NULL; + std::vector< SSG_pntDirAngle> cornerPeakP; + std::vector< SSG_pntDirAngle> cornerPeakM; + for (int i = 0, i_max = (int)vldPts.size(); i < i_max; i++) + { + if (i == 275) + int kkk = 1; + SSG_pntDirAngle* curr_data = &corners[i]; + if (curr_data->pntIdx < 0) + { + if (i == i_max - 1) //最后一个 + { + if (1 == _state) //上升 + { + cornerPeakP.push_back(corners[eEdgePtIdx]); + } + else if (2 == _state) //下降 + { + cornerPeakM.push_back(corners[eEdgePtIdx]); + } + } + continue; + } + + if (NULL == pre_data) + { + sEdgePtIdx = i; + eEdgePtIdx = i; + pre_data = curr_data; + pre_i = i; + continue; + } + + eEdgePtIdx = i; + double cornerDiff = curr_data->corner - pre_data->corner; + switch (_state) + { + case 0: //初态 + if (cornerDiff < 0) //下降 + { + _state = 2; + } + else if (cornerDiff > 0) //上升 + { + _state = 1; + } + break; + case 1: //上升 + if (cornerDiff < 0) //下降 + { + cornerPeakP.push_back(*pre_data); + _state = 2; + } + break; + case 2: //下降 + if (cornerDiff > 0) // 上升 + { + cornerPeakM.push_back(*pre_data); + _state = 1; + } + break; + default: + _state = 0; + break; + } + pre_data = curr_data; + pre_i = i; + } + //注意:最后一个不处理,为基座位置 + + //极小值点(峰顶) + //极值比较,在尺度窗口下寻找局部极值点 + double square_distTh = 4 * cornerPara.scale * cornerPara.scale; //2倍的cornerScale。 + for (int i = 0, i_max = (int)cornerPeakP.size(); i < i_max; i++) + { + if (cornerPeakP[i].corner < cornerPara.cornerTh) + continue; + + bool isPeak = true; + //向前搜索 + int cornerPtIdx = cornerPeakP[i].pntIdx; + for (int j = i - 1; j >= 0; j--) + { + int prePtIdx = cornerPeakP[j].pntIdx; + double dist = pow(vldPts[cornerPtIdx].pt3D.y - vldPts[prePtIdx].pt3D.y, 2); // + pow(pkTop[i].pt3D.x - pkTop[j].pt3D.x, 2) ; + if (dist > square_distTh) //超出尺度窗口 + break; + + if (cornerPeakP[i].corner < cornerPeakP[j].corner) + { + isPeak = false; + break; + } + } + //向后搜索 + if (true == isPeak) + { + cornerPtIdx = cornerPeakP[i].pntIdx; + for (int j = i + 1; j < i_max; j++) + { + int postPtIdx = cornerPeakP[j].pntIdx; + double dist = pow(vldPts[cornerPtIdx].pt3D.y - vldPts[postPtIdx].pt3D.y, 2); // +pow(pkTop[i].pt3D.x - pkTop[j].pt3D.x, 2); + if (dist > square_distTh) //超出尺度窗口 + break; + + if (cornerPeakP[i].corner < cornerPeakP[j].corner) + { + isPeak = false; + break; + } + } + } + if (true == isPeak) + { + SSG_basicFeature1D a_feature; + if ((cornerPeakP[i].backwardAngle > cornerPara.jumpCornerTh_1) && (cornerPeakP[i].forwardAngle > -cornerPara.jumpCornerTh_2)) + a_feature.featureType = LINE_FEATURE_L_JUMP_H2L; + else if ((cornerPeakP[i].forwardAngle < -cornerPara.jumpCornerTh_1) && (cornerPeakP[i].backwardAngle < cornerPara.jumpCornerTh_2)) + a_feature.featureType = LINE_FEATURE_L_JUMP_L2H; + else + a_feature.featureType = LINE_FEATURE_CORNER_V; + a_feature.featureValue = cornerPeakP[i].corner; + + a_feature.jumpPos = vldPts[cornerPtIdx].pt3D; + a_feature.jumpPos2D = { lineIdx, vldPts[cornerPtIdx].nPointIdx }; + features.push_back(a_feature); + } + } + return; +} + /// /// 提取激光线上的拐点特征,水平端点视为Corner /// seg端点:z距离或y距离大于门限 @@ -2558,8 +2834,6 @@ void wd_getRingArcFeature( return; } -// 最小点集数量(小于此数无法拟合直线) -const int MIN_POINT_COUNT = 3; //使用端点直线,检查点到直线的距离,大于门限的分割 void split( SSG_RUN a_run, @@ -2628,46 +2902,7 @@ void wd_surfaceLineSegment( int dataSize = (int)lineData.size(); //去除零点 std::vector segs; - int runIdx = 1; - SSG_RUN a_run = { 0, -1, 0 }; //startIdx, len, lastIdx - double pre_z = 0; - double pre_y = 0; - for (int i = 0; i < dataSize; i++) - { - if (lineData[i].pt3D.z > 1e-4) - { - if (a_run.len < 0) - { - a_run.start = i; - a_run.len = 1; - a_run.value = i; - } - else - { - double z_diff = abs(lineData[i].pt3D.z - pre_z); - double y_diff = abs(lineData[i].pt3D.y - pre_y); - if ((z_diff < lineSegPara.segGapTh_z) && (y_diff < lineSegPara.segGapTh_y)) - { - a_run.len = i - a_run.start + 1; - a_run.value = i; - } - else - { - a_run.value = runIdx; - segs.push_back(a_run); - runIdx++; - - a_run.start = i; - a_run.len = 1; - a_run.value = i; - } - } - pre_z = lineData[i].pt3D.z; - pre_y = lineData[i].pt3D.y; - } - } - if (a_run.len > 0) - segs.push_back(a_run); + wd_getLineDataIntervals(lineData, lineSegPara, segs); //逐段处理 int segSize = (int)segs.size(); diff --git a/sourceCode/wheelArchHeigthMeasure.cpp b/sourceCode/wheelArchHeigthMeasure.cpp new file mode 100644 index 0000000..f563f84 --- /dev/null +++ b/sourceCode/wheelArchHeigthMeasure.cpp @@ -0,0 +1,492 @@ +#include +#include "SG_baseDataType.h" +#include "SG_baseAlgo_Export.h" +#include "wheelArchHeigthMeasure_Export.h" +#include +#include + +//version 1.0.0 : base version release to customer +std::string m_strVersion = "1.0.0"; +const char* wd_wheelArchHeigthMeasureVersion(void) +{ + return m_strVersion.c_str(); +} + +//相机水平安装计算地面调平参数。 +//相机Z轴基本平行地面时,需要以地面为参照,将相机调水平 +//旋转矩阵为调平参数,即将平面法向调整为垂直向量的参数 +SSG_planeCalibPara wd_horizonCamera_getGroundCalibPara( + std::vector< std::vector>& scanLines) +{ + return sg_HCameraVScan_getGroundCalibPara(scanLines); +} + +//相机水平时姿态调平,并去除地面 +void wd_horizonCamera_lineDataR( + std::vector< SVzNL3DPosition>& a_line, + const double* camPoseR, + double groundH) +{ + HCamera_lineDataRT_vector(a_line, camPoseR, groundH); +} + +SVzNL3DPoint _ptRotate(SVzNL3DPoint pt3D, const double matrix3d[9]) +{ + SVzNL3DPoint _r_pt; + _r_pt.x = pt3D.x * matrix3d[0] + pt3D.y * matrix3d[1] + pt3D.z * matrix3d[2]; + _r_pt.y = pt3D.x * matrix3d[3] + pt3D.y * matrix3d[4] + pt3D.z * matrix3d[5]; + _r_pt.z = pt3D.x * matrix3d[6] + pt3D.y * matrix3d[7] + pt3D.z * matrix3d[8]; + return _r_pt; +} + +bool compareByPtSize(const SWD_clustersInfo& a, const SWD_clustersInfo& b) { + return a.ptSize > b.ptSize; +} + +//提取轮眉区域的下端点 +void _getArcEndings(std::vector< std::vector>& scanLines, const int cluster_arc_id, std::vector& contourPts) +{ + int lineNum = (int)scanLines.size(); + + for (int i = 0; i < lineNum; i++) + { + int ptNum = (int)scanLines[i].size(); + int lastIdx = -1; + for (int j = 0; j < ptNum; j++) + { + if ((i == 380) && (j > 288)) + int kkk = 1; + if ( (scanLines[i][j].nPointIdx == cluster_arc_id) && (scanLines[i][j].pt3D.z >1e-4)) + lastIdx = j; + } + if (lastIdx >= 0) + { + SVzNL2DPoint a_pt = { i, lastIdx}; + contourPts.push_back(a_pt); + } + } +} + +//提取轮眉种子点,定义为y最小的端点 +int _getArcEndingsCenterPos(std::vector< std::vector>& scanLines, std::vector& contourPts) +{ + double minY = DBL_MAX; + int seedPosIdx = 0; + for (int i = 0, i_max = (int)contourPts.size(); i < i_max; i++) + { + SVzNL2DPoint& a_pos = contourPts[i]; + double y = scanLines[a_pos.x][a_pos.y].pt3D.y; + if ((minY > y) && (scanLines[a_pos.x][a_pos.y].pt3D.z > 1e-4)) + { + seedPosIdx = i; + minY = y; + } + } + return seedPosIdx; +} + +//使用端点直线,检查点到直线的距离,大于门限的分割 +int computeMaxDistPos( + int ptStartIdx, int ptEndIdx, + std::vector< SVzNL3DPosition>& lineData) +{ + SVzNL3DPoint pt1 = lineData[ptStartIdx].pt3D; + SVzNL3DPoint pt2 = lineData[ptEndIdx].pt3D; + if ((pt1.z < 1e-4) || (pt2.z < 1e-4)) + return -1; + double _a, _b, _c; + compute2ptLine_2( + pt1.y, pt1.z, + pt2.y, pt2.z, + &_a, &_b, &_c); + //compute2ptLine(pt1, pt2, &_a, &_b, &_c); + double denominator = sqrt(_a * _a + _b * _b); + //归一化 + _a = _a / denominator; + _b = _b / denominator; + _c = _c / denominator; + + double maxDist = 0; + int maxPos = 0; + for (int i = ptStartIdx; i <= ptEndIdx; i++) + { + SVzNL3DPoint a_pt = lineData[i].pt3D; + if (a_pt.z > 1e-4) + { + double dist = abs(a_pt.y * _a + a_pt.z * _b + _c); + if (maxDist < dist) + { + maxDist = dist; + maxPos = i; + } + } + } + return maxPos; +} + +void _extractArcFittingPts( + std::vector< std::vector>& scanLines, + std::vector& contourPts, + double chkWin, + int seedPosIdx, + std::vector< SVzNL2DPoint>& arcFittingPos) +{ + int size = (int)contourPts.size(); + arcFittingPos.push_back(contourPts[seedPosIdx]); + SVzNL3DPoint seedPt = scanLines[contourPts[seedPosIdx].x][contourPts[seedPosIdx].y].pt3D; + for (int i = seedPosIdx - 1; i >= 0; i--) + { + SVzNL2DPoint chkPos = contourPts[i]; + SVzNL3DPoint chkPt = scanLines[chkPos.x][chkPos.y].pt3D; + double len = sqrt(pow(chkPt.x - seedPt.x, 2) + pow(chkPt.y - seedPt.y, 2)); + if (len > chkWin) + break; + arcFittingPos.insert(arcFittingPos.begin(), chkPos); + } + for (int i = seedPosIdx + 1; i < size; i++) + { + SVzNL2DPoint chkPos = contourPts[i]; + SVzNL3DPoint chkPt = scanLines[chkPos.x][chkPos.y].pt3D; + double len = sqrt(pow(chkPt.x - seedPt.x, 2) + pow(chkPt.y - seedPt.y, 2)); + if (len > chkWin) + break; + arcFittingPos.push_back(chkPos); + } + + //检查正确的端点,检查20mm + double chkLen = 20; + for (int i = 0, i_max = (int)arcFittingPos.size(); i < i_max; i++) + { + SVzNL2DPoint chkPos = arcFittingPos[i]; + SVzNL3DPoint chkPt = scanLines[chkPos.x][chkPos.y].pt3D; + int endIdx = chkPos.y; + int startIdx = endIdx; + for (int m = endIdx - 1; m >= 0; m--) + { + SVzNL3DPoint a_pt = scanLines[chkPos.x][m].pt3D; + if (a_pt.z > 1e-4) + { + double len = sqrt(pow(a_pt.y - chkPt.y, 2) + pow(a_pt.z - chkPt.z, 2)); + if (len > chkLen) + break; + startIdx = m; + } + } + if (startIdx != endIdx) + { + int maxPos = computeMaxDistPos(startIdx, endIdx, scanLines[chkPos.x]); + SVzNL3DPoint max_pt = scanLines[chkPos.x][maxPos].pt3D; + double len1 = sqrt(pow(max_pt.y - chkPt.y, 2) + pow(max_pt.z - chkPt.z, 2)); + //构建等腰三角形,重新寻找点到底边的距离最高点 + for (int m = maxPos - 1; m >= 0; m--) + { + SVzNL3DPoint a_pt = scanLines[chkPos.x][m].pt3D; + if (a_pt.z > 1e-4) + { + double len = sqrt(pow(a_pt.y - max_pt.y, 2) + pow(a_pt.z - max_pt.z, 2)); + if (len > len1) + break; + startIdx = m; + } + } + maxPos = computeMaxDistPos(startIdx, endIdx, scanLines[chkPos.x]); + arcFittingPos[i].y = maxPos; + } + } + +} + +SVzNL3DPoint _getWheelArcFittingPoint( + std::vector< std::vector>& scanLines, + const int maskID, + std::vector< SVzNL2DPoint>& fittingPos, + SVzNL2DPoint& fittingPosition) +{ + //upWheel, 在XY平面生成拟合点 + std::vector fittingPoints; + for (int i = 0, i_max = (int)fittingPos.size(); i < i_max; i++) + { + int lineIdx = fittingPos[i].x; + int ptIdx = fittingPos[i].y; + cv::Point2d a_pt = { scanLines[lineIdx][ptIdx].pt3D.x,scanLines[lineIdx][ptIdx].pt3D.y }; + scanLines[lineIdx][ptIdx].nPointIdx = maskID; + fittingPoints.push_back(a_pt); + } + + double a, b, c, mse, max_err; + bool fitResult = leastSquareParabolaFitEigen( + fittingPoints, a, b, c, mse, max_err); + //计算轮毂上顶点 + SVzNL3DPoint fitPt; + fitPt.x = -b / (2 * a); + fitPt.y = (4 * a * c - b * b) / (4 * a); + //在轮廓点中寻找最近的点 + SVzNL2DPoint subOptiPtPos = { 0,0 }; + double minLen = DBL_MAX; + for (int i = 0, i_max = (int)fittingPos.size(); i < i_max; i++) + { + int lineIdx = fittingPos[i].x; + int ptIdx = fittingPos[i].y; + cv::Point2d a_pt = { scanLines[lineIdx][ptIdx].pt3D.x,scanLines[lineIdx][ptIdx].pt3D.y }; + double len = sqrt(pow(fitPt.x - a_pt.x, 2) + pow(fitPt.y - a_pt.y, 2)); + if (minLen > len) + { + subOptiPtPos = fittingPos[i]; + minLen = len; + } + } + fittingPosition = subOptiPtPos; + fitPt.z = scanLines[subOptiPtPos.x][subOptiPtPos.y].pt3D.z; + return fitPt; +} + +//轮眉高度测量 +WD_wheelArchInfo wd_wheelArchHeigthMeasure( + std::vector< std::vector>& scanLines, + const SSG_cornerParam cornerPara, + const SSG_lineSegParam lineSegPara, + const SSG_outlierFilterParam filterParam, + const SSG_treeGrowParam growParam, + const SSG_planeCalibPara groundCalibPara, + int* errCode) +{ + *errCode = 0; + WD_wheelArchInfo result; + memset(&result, 0, sizeof(WD_wheelArchInfo)); + + int lineNum = (int)scanLines.size(); + int linePtNum = (int)scanLines[0].size(); + bool isGridData = true; + for (int line = 0; line < lineNum; line++) + { + std::vector& lineData = scanLines[line]; + if (linePtNum != (int)lineData.size()) + isGridData = false; + //滤波,滤除异常点 + sg_lineDataRemoveOutlier_changeOriginData(&lineData[0], linePtNum, filterParam); + } + + //生成水平扫描 + std::vector> hLines_raw; + hLines_raw.resize(linePtNum); + for (int i = 0; i < linePtNum; i++) + hLines_raw[i].resize(lineNum); + for (int line = 0; line < lineNum; line++) + { + for (int j = 0; j < linePtNum; j++) + { + scanLines[line][j].nPointIdx = 0; //将原始数据的序列清0(会转义使用) + hLines_raw[j][line] = scanLines[line][j]; + hLines_raw[j][line].pt3D.x = scanLines[line][j].pt3D.y; + hLines_raw[j][line].pt3D.y = scanLines[line][j].pt3D.x; + } + } + //水平arc特征提取 + for (int line = 0; line < linePtNum; line++) + { + if (line == 974) + int kkk = 1; + std::vector& lineData = hLines_raw[line]; + //滤波,滤除异常点 + int ptNum = (int)lineData.size(); + sg_lineDataRemoveOutlier_changeOriginData(&lineData[0], ptNum, filterParam); + } + + //聚类,聚类出前盖板和轮胎 + std::vector> featureInfoMask; + std::vector> feature3DInfo; + featureInfoMask.resize(lineNum); + feature3DInfo.resize(lineNum); + for (int i = 0; i < lineNum; i++) + { + featureInfoMask[i].resize(linePtNum); + feature3DInfo[i].resize(linePtNum); + } + //生成Mask + for (int line = 0; line < lineNum; line++) + { + std::vector& lineData = scanLines[line]; + for (int ptIdx = 0; ptIdx < linePtNum; ptIdx++) + { + if (scanLines[line][ptIdx].pt3D.z > 1e-4) + { + SSG_featureClusteringInfo a_mask; + memset(&a_mask, 0, sizeof(SSG_featureClusteringInfo)); + a_mask.featurType = 1; + a_mask.lineIdx = line; + a_mask.ptIdx = ptIdx; + featureInfoMask[line][ptIdx] = a_mask; + feature3DInfo[line][ptIdx] = scanLines[line][ptIdx].pt3D; + } + } + } + //聚类 + //采用迭代思想,回归思路进行高效聚类 + std::vector> clusters; //只记录位置 + std::vector clustersInfo; + int clusterID = 1; + int clusterCheckWin = 5; + for (int y = 0; y < linePtNum; y++) + { + for (int x = 0; x < lineNum; x++) + { + SSG_featureClusteringInfo& a_featureInfo = featureInfoMask[x][y]; + if ((0 == a_featureInfo.featurType) || (a_featureInfo.clusterID > 0)) //非特征或已经处理 + continue; + + SVzNL3DPoint& a_feature3DValue = feature3DInfo[x][y]; + SVzNL3DRangeD a_clusterRoi; + a_clusterRoi.xRange.min = a_feature3DValue.x; + a_clusterRoi.xRange.max = a_feature3DValue.x; + a_clusterRoi.yRange.min = a_feature3DValue.y; + a_clusterRoi.yRange.max = a_feature3DValue.y; + a_clusterRoi.zRange.min = a_feature3DValue.z; + a_clusterRoi.zRange.max = a_feature3DValue.z; + + SVzNL2DPoint a_seedPos = { x, y }; + std::vector< SVzNL2DPoint> a_cluster; + a_cluster.push_back(a_seedPos); + wd_pointClustering2D( + featureInfoMask,//int,记录特征标记和clusterID,附加一个flag + feature3DInfo,//double,记录坐标信息 + clusterCheckWin, //搜索窗口 + growParam,//聚类条件 + clusterID, //当前Cluster的ID + a_cluster, //result + a_clusterRoi + ); + clusters.push_back(a_cluster); + SWD_clustersInfo a_info; + a_info.clusterIdx = clusterID; + a_info.ptSize = (int)a_cluster.size(); + a_info.roi3D = a_clusterRoi; + clustersInfo.push_back(a_info); + clusterID++; + } + } + //聚类结果分析 + //取最大的两个聚类。上面的聚类是前盖板,下面的是车轮 + std::sort(clustersInfo.begin(), clustersInfo.end(), compareByPtSize); + int cluseter_wheel_id, cluster_arc_id; + if (clustersInfo[0].roi3D.yRange.max > clustersInfo[1].roi3D.yRange.max) + { + cluseter_wheel_id = clustersInfo[0].clusterIdx; + cluster_arc_id = clustersInfo[1].clusterIdx; + } + else + { + cluseter_wheel_id = clustersInfo[1].clusterIdx; + cluster_arc_id = clustersInfo[0].clusterIdx; + } + std::vector< SVzNL2DPoint>& cluster_wheel = clusters[cluseter_wheel_id - 1]; + std::vector< SVzNL2DPoint>& cluster_arc = clusters[cluster_arc_id - 1]; + for (int i = 0, i_max = (int)cluster_wheel.size(); i < i_max; i++) + { + int lineIdx = cluster_wheel[i].x; + int ptIdx = cluster_wheel[i].y; + scanLines[lineIdx][ptIdx].nPointIdx = 1; + } + for (int i = 0, i_max = (int)cluster_arc.size(); i < i_max; i++) + { + int lineIdx = cluster_arc[i].x; + int ptIdx = cluster_arc[i].y; + scanLines[lineIdx][ptIdx].nPointIdx = 2; + } + + //寻找轮眉点 + //(1)快速寻找下端点(2)取中间区域,精确确定端点(3)抛物线拟合(4)计算轮眉点(最高点) + std::vector arcEndings; + _getArcEndings(scanLines, 2, arcEndings); //轮眉的ID是2 + if (arcEndings.size() == 0) + { + *errCode = SX_ERR_INVALID_ARC; + return result; + } + int arcMidPos = _getArcEndingsCenterPos(scanLines, arcEndings); + //取两侧固定长度(100mm内的点,进行精确边界提取,拟合) + double chkWin = 100.0; + std::vector< SVzNL2DPoint> arcFittingPos; + _extractArcFittingPts( + scanLines, + arcEndings, + chkWin, + arcMidPos, + arcFittingPos); + //在XY平面生成拟合点 + double outLineLen = 200; + SVzNL2DPoint arcPtPos; + SVzNL3DPoint arcPt = _getWheelArcFittingPoint(scanLines, 3, arcFittingPos, arcPtPos); + result.wheelArchPos = arcPt; + result.arcLine[0] = { arcPt.x - outLineLen, arcPt.y, arcPt.z }; + result.arcLine[1] = { arcPt.x + outLineLen, arcPt.y, arcPt.z }; + + //提取轮毂特征:V型槽。连长6mm,角度最大的V型槽 + std::vector< SVzNL2DPoint> upWheelPos; + std::vector< SVzNL2DPoint> downWheelPos; + int startLine = arcFittingPos[0].x; + int endLine = arcFittingPos.back().x; + SSG_cornerParam wheelCornerPara; + memset(&wheelCornerPara, 0, sizeof(SSG_cornerParam)); + wheelCornerPara.scale = 6.0; + wheelCornerPara.cornerTh = 75.0; + for (int line = startLine; line <= endLine; line++) + { + std::vector cornerFeatures; + sg_maskData_getLineCornerFeature( + scanLines[line], + line, + 1, //车轮的ID为1 + wheelCornerPara, //scale通常取bagH的1/4 + cornerFeatures); + //取第一个为up + if (cornerFeatures.size() >= 2) + { + upWheelPos.push_back(cornerFeatures[0].jumpPos2D); + downWheelPos.push_back(cornerFeatures.back().jumpPos2D); + } + } + SVzNL2DPoint upPos; + SVzNL3DPoint upWheelPt = _getWheelArcFittingPoint(scanLines, 4, upWheelPos, upPos); + result.wheelUpPos = upWheelPt; + result.upLine[0] = { upWheelPt.x - outLineLen, upWheelPt.y, upWheelPt.z }; + result.upLine[1] = { upWheelPt.x + outLineLen, upWheelPt.y, upWheelPt.z }; + SVzNL2DPoint downPos; + SVzNL3DPoint downWheelPt = _getWheelArcFittingPoint(scanLines, 5, downWheelPos, downPos); + result.wheelDownPos = downWheelPt; + result.downLine[0] = { downWheelPt.x - outLineLen, downWheelPt.y, downWheelPt.z }; + result.downLine[1] = { downWheelPt.x + outLineLen, downWheelPt.y, downWheelPt.z }; + double centerY = (upWheelPt.y + downWheelPt.y) / 2; + int searchLine = (upPos.x + downPos.x) / 2; + double minDist = DBL_MAX; + int minPtIdx = 0; + for (int i = 0; i < (int)scanLines[searchLine].size(); i++) + { + if (scanLines[searchLine][i].pt3D.z > 1e-4) + { + double dist = abs(scanLines[searchLine][i].pt3D.y - centerY); + if (minDist > dist) + { + minDist = dist; + minPtIdx = i; + } + } + } + result.centerLine[0] = { downWheelPt.x - outLineLen, centerY, scanLines[searchLine][minPtIdx].pt3D.z }; + result.centerLine[1] = { downWheelPt.x + outLineLen, centerY, scanLines[searchLine][minPtIdx].pt3D.z }; + result.archToCenterHeigth = centerY - arcPt.y; + //将数据重新投射回原来的坐标系,以保持手眼标定结果正确 + for (int i = 0; i < lineNum; i++) + lineDataRT_vector(scanLines[i], groundCalibPara.invRMatrix, -1); + //将检测结果重新投射回原来的坐标系 + result.wheelArchPos = _ptRotate(result.wheelArchPos, groundCalibPara.invRMatrix); + result.wheelUpPos = _ptRotate(result.wheelUpPos, groundCalibPara.invRMatrix); + result.wheelDownPos = _ptRotate(result.wheelDownPos, groundCalibPara.invRMatrix); + for (int i = 0; i < 2; i++) + { + result.arcLine[i] = _ptRotate(result.arcLine[i], groundCalibPara.invRMatrix); + result.centerLine[i] = _ptRotate(result.centerLine[i], groundCalibPara.invRMatrix); + result.downLine[i] = _ptRotate(result.downLine[i], groundCalibPara.invRMatrix); + result.upLine[i] = _ptRotate(result.upLine[i], groundCalibPara.invRMatrix); + } + return result; +} \ No newline at end of file diff --git a/sourceCode/wheelArchHeigthMeasure_Export.h b/sourceCode/wheelArchHeigthMeasure_Export.h new file mode 100644 index 0000000..4bcf7df --- /dev/null +++ b/sourceCode/wheelArchHeigthMeasure_Export.h @@ -0,0 +1,43 @@ +#pragma once + +#include "SG_algo_Export.h" +#include + +#define _OUTPUT_DEBUG_DATA 1 + +typedef struct +{ + SVzNL3DPoint wheelArchPos; + SVzNL3DPoint wheelUpPos; + SVzNL3DPoint wheelDownPos; + SVzNL3DPoint arcLine[2]; + SVzNL3DPoint upLine[2]; + SVzNL3DPoint downLine[2]; + SVzNL3DPoint centerLine[2]; + double archToCenterHeigth; +}WD_wheelArchInfo; + +//读版本号 +SG_APISHARED_EXPORT const char* wd_wheelArchHeigthMeasureVersion(void); + +//相机水平安装计算地面调平参数。。 +//相机Z轴基本平行地面时,需要以地面为参照,将相机调水平 +//旋转矩阵为调平参数,即将平面法向调整为垂直向量的参数 +SG_APISHARED_EXPORT SSG_planeCalibPara wd_horizonCamera_getGroundCalibPara( + std::vector< std::vector>& scanLines); + +//相机水平时姿态调平,并去除地面 +SG_APISHARED_EXPORT void wd_horizonCamera_lineDataR( + std::vector< SVzNL3DPosition>& a_line, + const double* camPoseR, + double groundH); + +//提取工件角点及定位长度信息 +SG_APISHARED_EXPORT WD_wheelArchInfo wd_wheelArchHeigthMeasure( + std::vector< std::vector>& scanLines, + const SSG_cornerParam cornerPara, + const SSG_lineSegParam lineSegPara, + const SSG_outlierFilterParam filterParam, + const SSG_treeGrowParam growParam, + const SSG_planeCalibPara groundCalibPara, + int* errCode); diff --git a/wheelArchHeigthMeasure_test/wheelArchHeigthMeasure_test.cpp b/wheelArchHeigthMeasure_test/wheelArchHeigthMeasure_test.cpp new file mode 100644 index 0000000..69a2212 --- /dev/null +++ b/wheelArchHeigthMeasure_test/wheelArchHeigthMeasure_test.cpp @@ -0,0 +1,2839 @@ +锘// wheelArchHeigthMeasure_test.cpp : 姝ゆ枃浠跺寘鍚 "main" 鍑芥暟銆傜▼搴忔墽琛屽皢鍦ㄦ澶勫紑濮嬪苟缁撴潫銆 +// +#include +#include +#include +#include +#include +#include +#include "wheelArchHeigthMeasure_Export.h" +#include +#ifdef _WIN32 +#include +#include "direct.h" +#endif + +typedef struct +{ + int nPointIdx; + double x; + double y; + double z; + float r; + float g; + float b; +} SPointXYZRGB; + +SVzNL3DPoint _ptRotate(SVzNL3DPoint pt3D, double matrix3d[9]) +{ + SVzNL3DPoint _r_pt; + _r_pt.x = pt3D.x * matrix3d[0] + pt3D.y * matrix3d[1] + pt3D.z * matrix3d[2]; + _r_pt.y = pt3D.x * matrix3d[3] + pt3D.y * matrix3d[4] + pt3D.z * matrix3d[5]; + _r_pt.z = pt3D.x * matrix3d[6] + pt3D.y * matrix3d[7] + pt3D.z * matrix3d[8]; + return _r_pt; +} + +SVzNLPointXYZRGBA _ptRotate_RGBD(SVzNLPointXYZRGBA pt3D, double matrix3d[9]) +{ + SVzNLPointXYZRGBA _r_pt; + _r_pt.x = pt3D.x * matrix3d[0] + pt3D.y * matrix3d[1] + pt3D.z * matrix3d[2]; + _r_pt.y = pt3D.x * matrix3d[3] + pt3D.y * matrix3d[4] + pt3D.z * matrix3d[5]; + _r_pt.z = pt3D.x * matrix3d[6] + pt3D.y * matrix3d[7] + pt3D.z * matrix3d[8]; + return _r_pt; +} + +#define DATA_VER_OLD 0 +#define DATA_VER_NEW 1 +#define DATA_VER_FROM_CUSTOM 2 +#define VZ_LASER_LINE_PT_MAX_NUM 4096 +SVzNLXYZRGBDLaserLine* vzReadLaserScanPointFromFile_XYZRGB(const char* fileName, int* scanLineNum, float* scanV, + int* dataCalib, int* scanMaxStamp, int* canClockUnit, bool removeNullLines) +{ + std::ifstream inputFile(fileName); + std::string linedata; + + if (inputFile.is_open() == false) + return NULL; + + SVzNLXYZRGBDLaserLine* _scanLines = NULL; + + int lines = 0; + int dataElements = 4; + int firstIndex = -1; + + int dataFileVer = DATA_VER_OLD; + std::getline(inputFile, linedata); //绗竴琛 + int lineNum = 0; + if (0 == strncmp("LineNum:", linedata.c_str(), 8)) + { + sscanf_s(linedata.c_str(), "LineNum:%d", &lines); + if (lines == 0) + return NULL; + lineNum = lines; + _scanLines = (SVzNLXYZRGBDLaserLine*)malloc(sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); + memset(_scanLines, 0, sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); + } + if (_scanLines == NULL) + return NULL; + + int lineIdx = 0; + int ptIdx = 0; + int ptNum = 0; + int pre_ptNum = -1; + std::vector< SVzNLPointXYZRGBA> a_line; + int vldLineIdx = 0; + int vldPtNum = 0; + unsigned int timeStamp = 0; + while (getline(inputFile, linedata)) + { + if (0 == strncmp("ScanSpeed:", linedata.c_str(), 10)) + { + double lineV = 0; + sscanf_s(linedata.c_str(), "ScanSpeed:%lf", &lineV); + if (scanV) + *scanV = (float)lineV; + } + else if (0 == strncmp("PointAdjust:", linedata.c_str(), 12)) + { + int ptAdjusted = 0; + sscanf_s(linedata.c_str(), "PointAdjust:%d", &ptAdjusted); + if (dataCalib) + *dataCalib = ptAdjusted; + } + else if (0 == strncmp("MaxTimeStamp:", linedata.c_str(), 13)) + { + unsigned int maxTimeStamp = 0; + unsigned int timePerStamp = 0; + sscanf_s(linedata.c_str(), "MaxTimeStamp:%u_%u", &maxTimeStamp, &timePerStamp); + if (scanMaxStamp) + *scanMaxStamp = maxTimeStamp; + if (canClockUnit) + *canClockUnit = timePerStamp; + } + else if (0 == strncmp("Line_", linedata.c_str(), 5)) + { + int lineIndex; + unsigned int curr_timeStamp; + sscanf_s(linedata.c_str(), "Line_%d_%u_%d", &lineIndex, &curr_timeStamp, &ptNum); + if (firstIndex < 0) + firstIndex = lineIndex; + + lineIndex = lineIndex - firstIndex; + if ((lineIndex < 0) || (lineIndex >= lines)) + break; + + int recvPtNum = (int)a_line.size(); + if ((recvPtNum == pre_ptNum) && ((vldPtNum > 0) || (false == removeNullLines))) + { + SVzNLPointXYZRGBA* p3DPoint; + if (pre_ptNum > 0) + p3DPoint = (SVzNLPointXYZRGBA*)malloc(sizeof(SVzNLPointXYZRGBA) * pre_ptNum); + else + p3DPoint = NULL; + _scanLines[vldLineIdx].nPointCnt = pre_ptNum; + _scanLines[vldLineIdx].nTimeStamp = timeStamp; + _scanLines[vldLineIdx].p3DPoint = p3DPoint; + for (int m = 0; m < pre_ptNum; m++) + p3DPoint[m] = a_line[m]; + vldLineIdx++; + } + //new Line + timeStamp = curr_timeStamp; + vldPtNum = 0; + a_line.clear(); + pre_ptNum = ptNum; + } + else if (0 == strncmp("{", linedata.c_str(), 1)) + { + float X, Y, Z; + int imageY = 0; + float leftX, leftY; + float rightX, rightY; + float r, g, b; + sscanf_s(linedata.c_str(), "{%f,%f,%f,%f,%f,%f }-{%f,%f}-{%f,%f}", &X, &Y, &Z, &r, &g, &b, &leftX, &leftY, &rightX, &rightY); + { + if (lineIdx == 537) + int kkk = 1; + + SVzNLPointXYZRGBA a_pt; + + a_pt.x = X; + a_pt.y = Y; + a_pt.z = Z; + int nr = (int)(r * 255); + int ng = (int)(g * 255); + int nb = (int)(b * 255); + nb <<= 8; + nb += ng; + nb <<= 8; + nb += nr; + a_pt.nRGB = nb; + if (a_pt.z > 1e-4) + vldPtNum++; + a_line.push_back(a_pt); + } + } + } + //last line + int recvPtNum = (int)a_line.size(); + if ((recvPtNum == pre_ptNum) && ((vldPtNum > 0) || (false == removeNullLines))) + { + SVzNLPointXYZRGBA* p3DPoint; + if (pre_ptNum > 0) + p3DPoint = (SVzNLPointXYZRGBA*)malloc(sizeof(SVzNLPointXYZRGBA) * pre_ptNum); + else + p3DPoint = NULL; + _scanLines[vldLineIdx].nPointCnt = pre_ptNum; + _scanLines[vldLineIdx].nTimeStamp = timeStamp; + _scanLines[vldLineIdx].p3DPoint = p3DPoint; + for (int m = 0; m < pre_ptNum; m++) + p3DPoint[m] = a_line[m]; + vldLineIdx++; + } + + if (scanLineNum) + *scanLineNum = vldLineIdx; + inputFile.close(); + return _scanLines; +} + +void vzReadLaserScanPointFromFile_XYZRGB_vector(const char* fileName, std::vector>&scanData) +{ + std::ifstream inputFile(fileName); + std::string linedata; + + if (inputFile.is_open() == false) + return; + + std::vector< SPointXYZRGB> a_line; + while (getline(inputFile, linedata)) + { + if (0 == strncmp("Line_", linedata.c_str(), 5)) //new line + { + if (a_line.size() > 0) + { + scanData.push_back(a_line); + a_line.clear(); + } + } + else if (0 == strncmp("{", linedata.c_str(), 1)) + { + float leftX, leftY; + float rightX, rightY; + SPointXYZRGB a_pt; + sscanf_s(linedata.c_str(), "{ %lf, %lf, %lf, %f, %f, %f }-{ %f, %f }-{ %f, %f }", &a_pt.x, &a_pt.y, &a_pt.z, &a_pt.r, &a_pt.g, &a_pt.b, &leftX, &leftY, &rightX, &rightY); + a_line.push_back(a_pt); + } + } + if (a_line.size() > 0) + scanData.push_back(a_line); + + inputFile.close(); + return; +} + +void vzReadPlyTxtPointFromFile_XYZRGB_vector(const char* fileName, std::vector>& scanData) +{ + std::ifstream inputFile(fileName); + std::string linedata; + + if (inputFile.is_open() == false) + return; + + const double lineYGap = 50; + std::vector< SVzNLPointXYZRGBA> a_line; + + double pre_y = FLT_MIN; + while (getline(inputFile, linedata)) + { + SVzNLPointXYZRGBA a_pt; + memset(&a_pt, 0, sizeof(SVzNLPointXYZRGBA)); + double _x, _y, _z; + sscanf_s(linedata.c_str(), "%lf %lf %lf", &_x, &_y, &_z); + a_pt.x = (float)_x; + a_pt.y = (float)_y; + a_pt.z = (float)_z; + if(a_pt.y < pre_y - lineYGap) //鏂扮殑鎵弿琛 + { + if (a_line.size() > 0) + { + scanData.push_back(a_line); + a_line.clear(); + } + } + pre_y = a_pt.y; + a_line.push_back(a_pt); + } + if (a_line.size() > 0) + scanData.push_back(a_line); + + inputFile.close(); + return; +} + +SVzNL3DLaserLine* vzReadLaserScanPointFromFile_XYZ(const char* fileName, int* scanLineNum, float* scanV, + int* dataCalib, int* scanMaxStamp, int* canClockUnit) +{ + std::ifstream inputFile(fileName); + std::string linedata; + + if (inputFile.is_open() == false) + return NULL; + + SVzNL3DLaserLine* _scanLines = NULL; + + int lines = 0; + int dataElements = 4; + int firstIndex = -1; + + int dataFileVer = DATA_VER_OLD; + std::getline(inputFile, linedata); //绗竴琛 + int lineNum = 0; + if (0 == strncmp("LineNum:", linedata.c_str(), 8)) + { + dataFileVer = DATA_VER_NEW; + sscanf_s(linedata.c_str(), "LineNum:%d", &lines); + if (lines == 0) + return NULL; + lineNum = lines; + _scanLines = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (lineNum + 1)); + memset(_scanLines, 0, sizeof(SVzNL3DLaserLine) * (lineNum + 1)); + if (scanLineNum) + *scanLineNum = lines; + } + else if (0 == strncmp("LineNum_", linedata.c_str(), 8)) + { + dataFileVer = DATA_VER_OLD; + sscanf_s(linedata.c_str(), "LineNum_%d", &lines); + if (lines == 0) + return NULL; + lineNum = lines; + _scanLines = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (lineNum + 1)); + memset(_scanLines, 0, sizeof(SVzNL3DLaserLine) * (lineNum + 1)); + if (scanLineNum) + *scanLineNum = lines; + } + if (_scanLines == NULL) + return NULL; + + int ptNum = 0; + int lineIdx = -1; + int ptIdx = 0; + SVzNL3DPosition* p3DPoint = NULL; + if (dataFileVer == DATA_VER_NEW) + { + while (getline(inputFile, linedata)) + { + if (0 == strncmp("ScanSpeed:", linedata.c_str(), 10)) + { + double lineV = 0; + sscanf_s(linedata.c_str(), "ScanSpeed:%lf", &lineV); + if (scanV) + *scanV = (float)lineV; + } + else if (0 == strncmp("PointAdjust:", linedata.c_str(), 12)) + { + int ptAdjusted = 0; + sscanf_s(linedata.c_str(), "PointAdjust:%d", &ptAdjusted); + if (dataCalib) + *dataCalib = ptAdjusted; + } + else if (0 == strncmp("MaxTimeStamp:", linedata.c_str(), 13)) + { + unsigned int maxTimeStamp = 0; + unsigned int timePerStamp = 0; + sscanf_s(linedata.c_str(), "MaxTimeStamp:%u_%u", &maxTimeStamp, &timePerStamp); + if (scanMaxStamp) + *scanMaxStamp = maxTimeStamp; + if (canClockUnit) + *canClockUnit = timePerStamp; + } + else if (0 == strncmp("Line_", linedata.c_str(), 5)) + { + int lineIndex; + unsigned int timeStamp; + sscanf_s(linedata.c_str(), "Line_%d_%u_%d", &lineIndex, &timeStamp, &ptNum); + if (firstIndex < 0) + firstIndex = lineIndex; + + lineIndex = lineIndex - firstIndex; + if ((lineIndex < 0) || (lineIndex >= lines)) + break; + + //new Line + lineIdx++; + if (ptNum > 0) + { + p3DPoint = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * ptNum); + memset(p3DPoint, 0, sizeof(SVzNL3DPosition) * ptNum); + } + else + p3DPoint = NULL; + _scanLines[lineIdx].nPositionCnt = 0; + _scanLines[lineIdx].nTimeStamp = timeStamp; + _scanLines[lineIdx].p3DPosition = p3DPoint; + + } + else if (0 == strncmp("{", linedata.c_str(), 1)) + { + float X, Y, Z; + int imageY = 0; + float leftX, leftY; + float rightX, rightY; + sscanf_s(linedata.c_str(), "{%f,%f,%f}-{%f,%f}-{%f,%f}", &X, &Y, &Z, &leftX, &leftY, &rightX, &rightY); + int id = _scanLines[lineIdx].nPositionCnt; + if (id < ptNum) + { + p3DPoint[id].pt3D.x = X; + p3DPoint[id].pt3D.y = Y; + p3DPoint[id].pt3D.z = Z; + _scanLines[lineIdx].nPositionCnt = id + 1; + } + } + } + + } + else if (dataFileVer == DATA_VER_OLD) + { + while (getline(inputFile, linedata)) + { + if (0 == strncmp("DataElements_", linedata.c_str(), 13)) + { + sscanf_s(linedata.c_str(), "DataElements_%d", &dataElements); + if ((dataElements != 3) && (dataElements != 4)) + break; + } + if (0 == strncmp("LineV_", linedata.c_str(), 6)) + { + double lineV = 0; + sscanf_s(linedata.c_str(), "LineV_%lf", &lineV); + } + else if (0 == strncmp("Line_", linedata.c_str(), 5)) + { + int lineIndex; + unsigned int timeStamp; + sscanf_s(linedata.c_str(), "Line_%d_%u", &lineIndex, &timeStamp); +#if 0 + if (scanLineListTail == NULL) + firstIndex = lineIndex; +#endif + lineIndex = lineIndex - firstIndex; + if ((lineIndex < 0) || (lineIndex >= lines)) + break; + //new Line + //new Line + lineIdx++; + p3DPoint = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * VZ_LASER_LINE_PT_MAX_NUM); + memset(p3DPoint, 0, sizeof(SVzNL3DPosition) * VZ_LASER_LINE_PT_MAX_NUM); + _scanLines[lineIdx].nPositionCnt = 0; + _scanLines[lineIdx].nTimeStamp = timeStamp; + _scanLines[lineIdx].p3DPosition = p3DPoint; + } + else if (0 == strncmp("(", linedata.c_str(), 1)) + { + float X, Y, Z; + int imageY = 0; + if (dataElements == 4) + sscanf_s(linedata.c_str(), "(%f,%f,%f,%d)", &X, &Y, &Z, &imageY); + else + sscanf_s(linedata.c_str(), "(%f,%f,%f)", &X, &Y, &Z); + int id = _scanLines[lineIdx].nPositionCnt; + if (id < VZ_LASER_LINE_PT_MAX_NUM) + { + p3DPoint[id].pt3D.x = X; + p3DPoint[id].pt3D.y = Y; + p3DPoint[id].pt3D.z = Z; + _scanLines[lineIdx].nPositionCnt = id + 1; + } + } + } + } + inputFile.close(); + return _scanLines; +} + +//浠嶳GBD鐐逛簯涓鍙朮YZ淇℃伅 +SVzNL3DLaserLine* vzReadXYZPointFromFile_XYZRGB(const char* fileName, int* scanLineNum, float* scanV, + int* dataCalib, int* scanMaxStamp, int* canClockUnit) +{ + std::ifstream inputFile(fileName); + std::string linedata; + + if (inputFile.is_open() == false) + return NULL; + + SVzNL3DLaserLine* _scanLines = NULL; + + int lines = 0; + int dataElements = 4; + int firstIndex = -1; + + int dataFileVer = DATA_VER_OLD; + std::getline(inputFile, linedata); //绗竴琛 + int lineNum = 0; + if (0 == strncmp("LineNum:", linedata.c_str(), 8)) + { + dataFileVer = DATA_VER_NEW; + sscanf_s(linedata.c_str(), "LineNum:%d", &lines); + if (lines == 0) + return NULL; + lineNum = lines; + _scanLines = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (lineNum + 1)); + memset(_scanLines, 0, sizeof(SVzNL3DLaserLine) * (lineNum + 1)); + if (scanLineNum) + *scanLineNum = lines; + } + else if (0 == strncmp("LineNum_", linedata.c_str(), 8)) + { + dataFileVer = DATA_VER_OLD; + sscanf_s(linedata.c_str(), "LineNum_%d", &lines); + if (lines == 0) + return NULL; + lineNum = lines; + _scanLines = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (lineNum + 1)); + memset(_scanLines, 0, sizeof(SVzNL3DLaserLine) * (lineNum + 1)); + if (scanLineNum) + *scanLineNum = lines; + } + if (_scanLines == NULL) + return NULL; + + int ptNum = 0; + int lineIdx = -1; + int ptIdx = 0; + SVzNL3DPosition* p3DPoint = NULL; + if (dataFileVer == DATA_VER_NEW) + { + while (getline(inputFile, linedata)) + { + if (0 == strncmp("ScanSpeed:", linedata.c_str(), 10)) + { + double lineV = 0; + sscanf_s(linedata.c_str(), "ScanSpeed:%lf", &lineV); + if (scanV) + *scanV = (float)lineV; + } + else if (0 == strncmp("PointAdjust:", linedata.c_str(), 12)) + { + int ptAdjusted = 0; + sscanf_s(linedata.c_str(), "PointAdjust:%d", &ptAdjusted); + if (dataCalib) + *dataCalib = ptAdjusted; + } + else if (0 == strncmp("MaxTimeStamp:", linedata.c_str(), 13)) + { + unsigned int maxTimeStamp = 0; + unsigned int timePerStamp = 0; + sscanf_s(linedata.c_str(), "MaxTimeStamp:%u_%u", &maxTimeStamp, &timePerStamp); + if (scanMaxStamp) + *scanMaxStamp = maxTimeStamp; + if (canClockUnit) + *canClockUnit = timePerStamp; + } + else if (0 == strncmp("Line_", linedata.c_str(), 5)) + { + int lineIndex; + unsigned int timeStamp; + sscanf_s(linedata.c_str(), "Line_%d_%u_%d", &lineIndex, &timeStamp, &ptNum); + if (firstIndex < 0) + firstIndex = lineIndex; + + lineIndex = lineIndex - firstIndex; + if ((lineIndex < 0) || (lineIndex >= lines)) + break; + + //new Line + lineIdx++; + if (ptNum > 0) + { + p3DPoint = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * ptNum); + memset(p3DPoint, 0, sizeof(SVzNL3DPosition) * ptNum); + } + else + p3DPoint = NULL; + _scanLines[lineIdx].nPositionCnt = 0; + _scanLines[lineIdx].nTimeStamp = timeStamp; + _scanLines[lineIdx].p3DPosition = p3DPoint; + + } + else if (0 == strncmp("{", linedata.c_str(), 1)) + { + float X, Y, Z; + float leftX, leftY; + float rightX, rightY; + float r, g, b; + sscanf_s(linedata.c_str(), "{%f,%f,%f,%f,%f,%f }-{%f,%f}-{%f,%f}", &X, &Y, &Z, &r, &g, &b, &leftX, &leftY, &rightX, &rightY); + int id = _scanLines[lineIdx].nPositionCnt; + if (id < ptNum) + { + p3DPoint[id].pt3D.x = X; + p3DPoint[id].pt3D.y = Y; + p3DPoint[id].pt3D.z = Z; + _scanLines[lineIdx].nPositionCnt = id + 1; + } + } + } + + } + inputFile.close(); + return _scanLines; +} + +void vzReadLaserScanPointFromFile_XYZ_vector(const char* fileName, std::vector>& scanData) +{ + std::ifstream inputFile(fileName); + std::string linedata; + + if (inputFile.is_open() == false) + return; + + std::vector< SVzNL3DPosition> a_line; + int ptIdx = 0; + while (getline(inputFile, linedata)) + { + if (0 == strncmp("Line_", linedata.c_str(), 5)) + { + int ptSize = (int)a_line.size(); + if (ptSize > 0) + { + scanData.push_back(a_line); + } + a_line.clear(); + ptIdx = 0; + } + else if (0 == strncmp("{", linedata.c_str(), 1)) + { + float X, Y, Z; + int imageY = 0; + float leftX, leftY; + float rightX, rightY; + sscanf_s(linedata.c_str(), "{%f,%f,%f}-{%f,%f}-{%f,%f}", &X, &Y, &Z, &leftX, &leftY, &rightX, &rightY); + SVzNL3DPosition a_pt; + a_pt.pt3D.x = X; + a_pt.pt3D.y = Y; + a_pt.pt3D.z = Z; + a_pt.nPointIdx = ptIdx; + ptIdx++; + a_line.push_back(a_pt); + } + } + //last line + int ptSize = (int)a_line.size(); + if (ptSize > 0) + { + scanData.push_back(a_line); + a_line.clear(); + } + + inputFile.close(); + return; +} + +void _convertToGridData_XYZRGB(std::vector>& scanData, double _F, std::vector>& gridData) +{ + int lineNum = (int)scanData.size(); + int min_y = 100000000; + int max_y = -10000000; + int validStartLine = -1; + int validEndLine = -1; + for (int line = 0; line < lineNum; line++) + { + if (scanData[line].size() > 0) + { + if (validStartLine < 0) + { + validStartLine = line; + validEndLine = line; + } + else + validEndLine = line; + } + + for (int i = 0; i < (int)scanData[line].size(); i++) + { + SVzNLPointXYZRGBA& a_pt = scanData[line][i]; + if (a_pt.z > 1e-4) + { + double v = _F * a_pt.y / a_pt.z + 2000; + a_pt.nRGB = (int)(v + 0.5); + max_y = max_y < (int)a_pt.nRGB ? (int)a_pt.nRGB : max_y; + min_y = min_y > (int)a_pt.nRGB ? (int)a_pt.nRGB : min_y; + } + } + } + if (min_y == 100000000) + return; + + int vldLineNum = validEndLine - validStartLine + 1; + gridData.resize(vldLineNum); + int pt_counter = max_y - min_y + 1; + for (int i = 0; i < vldLineNum; i++) + { + gridData[i].resize(pt_counter); + for (int j = 0; j < pt_counter; j++) + { + gridData[i][j].nPointIdx = j; + gridData[i][j].pt3D.x = 0; + gridData[i][j].pt3D.y = 0; + gridData[i][j].pt3D.z = 0; + } + } + for (int line = validStartLine; line <= validEndLine; line++) + { + int gridLine = line - validStartLine; + for (int i = 0; i < (int)scanData[line].size(); i++) + { + SVzNLPointXYZRGBA& a_pt = scanData[line][i]; + if (a_pt.z > 1e-4) + { + int pt_id = a_pt.nRGB - min_y; + gridData[gridLine][pt_id].pt3D.x = a_pt.x; + gridData[gridLine][pt_id].pt3D.y = a_pt.y; + gridData[gridLine][pt_id].pt3D.z = a_pt.z; + } + } + } + return; +} + +void _convertToGridData_XYZRGB_vector(std::vector>&scanData, double _F, std::vector>&scanData_grid) +{ + int min_y = 100000000; + int max_y = -10000000; + int lineNum = scanData.size(); + for (int line = 0; line < lineNum; line++) + { + std::vector< SPointXYZRGB>& a_line = scanData[line]; + int nPointCnt = a_line.size(); + for (int i = 0; i < nPointCnt; i++) + { + SPointXYZRGB* a_pt = &scanData[line][i]; + if (a_pt->z > 1e-4) + { + double v = _F * a_pt->y / a_pt->z + 2000; + a_pt->nPointIdx = (int)(v + 0.5); + max_y = max_y < (int)a_pt->nPointIdx ? (int)a_pt->nPointIdx : max_y; + min_y = min_y > (int)a_pt->nPointIdx ? (int)a_pt->nPointIdx : min_y; + } + } + } + if (min_y == 100000000) + return; + + int pt_counter = max_y - min_y + 1; + for (int line = 0; line < lineNum; line++) + { + std::vector< SPointXYZRGB> gridData; + gridData.resize(pt_counter); + for (int i = 0; i < pt_counter; i++) + gridData[i] = { 0, 0.0, 0.0, 0.0, 0.0f, 0.0f, 0.0f }; + + std::vector< SPointXYZRGB>& a_line = scanData[line]; + int nPointCnt = a_line.size(); + for (int i = 0; i < nPointCnt; i++) + { + SPointXYZRGB a_pt = a_line[i]; + if (a_pt.z > 1e-4) + { + int pt_id = a_pt.nPointIdx - min_y; + gridData[pt_id] = a_pt; + } + } + scanData_grid.push_back(gridData); + } + return; +} + +void _outputCalibPara(char* fileName, SSG_planeCalibPara calibPara) +{ + std::ofstream sw(fileName); + char dataStr[250]; + //璋冨钩鐭╅樀 + sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.planeCalib[0], calibPara.planeCalib[1], calibPara.planeCalib[2]); + sw << dataStr << std::endl; + sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.planeCalib[3], calibPara.planeCalib[4], calibPara.planeCalib[5]); + sw << dataStr << std::endl; + sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.planeCalib[6], calibPara.planeCalib[7], calibPara.planeCalib[8]); + sw << dataStr << std::endl; + //鍦伴潰楂樺害 + sprintf_s(dataStr, 250, "%g", calibPara.planeHeight); + sw << dataStr << std::endl; + //鍙嶅悜鏃嬭浆鐭╅樀 + sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.invRMatrix[0], calibPara.invRMatrix[1], calibPara.invRMatrix[2]); + sw << dataStr << std::endl; + sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.invRMatrix[3], calibPara.invRMatrix[4], calibPara.invRMatrix[5]); + sw << dataStr << std::endl; + sprintf_s(dataStr, 250, "%g, %g, %g", calibPara.invRMatrix[6], calibPara.invRMatrix[7], calibPara.invRMatrix[8]); + sw << dataStr << std::endl; + + sw.close(); +} + +SSG_planeCalibPara _readCalibPara(char* fileName) +{ + //璁剧疆鍒濆缁撴灉 + double initCalib[9] = { + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0 }; + SSG_planeCalibPara planePara; + for (int i = 0; i < 9; i++) + planePara.planeCalib[i] = initCalib[i]; + planePara.planeHeight = -1.0; + for (int i = 0; i < 9; i++) + planePara.invRMatrix[i] = initCalib[i]; + + std::ifstream inputFile(fileName); + std::string linedata; + + if (inputFile.is_open() == false) + return planePara; + + //璋冨钩鐭╅樀 + std::getline(inputFile, linedata); + sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.planeCalib[0], &planePara.planeCalib[1], &planePara.planeCalib[2]); + std::getline(inputFile, linedata); + sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.planeCalib[3], &planePara.planeCalib[4], &planePara.planeCalib[5]); + std::getline(inputFile, linedata); + sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.planeCalib[6], &planePara.planeCalib[7], &planePara.planeCalib[8]); + //鍦伴潰楂樺害 + std::getline(inputFile, linedata); + sscanf_s(linedata.c_str(), "%lf", &planePara.planeHeight); + //鍙嶅悜鏃嬭浆鐭╅樀 + std::getline(inputFile, linedata); + sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.invRMatrix[0], &planePara.invRMatrix[1], &planePara.invRMatrix[2]); + std::getline(inputFile, linedata); + sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.invRMatrix[3], &planePara.invRMatrix[4], &planePara.invRMatrix[5]); + std::getline(inputFile, linedata); + sscanf_s(linedata.c_str(), "%lf, %lf, %lf", &planePara.invRMatrix[6], &planePara.invRMatrix[7], &planePara.invRMatrix[8]); + + inputFile.close(); + return planePara; +} + +void _outputObjResult(char* fileName, std::vector&objOps) +{ + std::ofstream sw(fileName); + char dataStr[250]; + + int objSize = (int)objOps.size(); + for (int i = 0; i < objSize; i++) + { + sw << "obj_" << i << std::endl; + sprintf_s(dataStr, 250, " %g, %g, %g, %g", objOps[i].centerPos.x, objOps[i].centerPos.y, objOps[i].centerPos.z, objOps[i].centerPos.z_yaw); + sw << dataStr << std::endl; + } + sw.close(); +} + +void _outputScanDataFile_self(char* fileName, std::vector>& gridData, + float lineV, int maxTimeStamp, int clockPerSecond) +{ + int lineNum = (int)gridData.size(); + std::ofstream sw(fileName); + sw << "LineNum:" << lineNum << std::endl; + sw << "DataType: 0" << std::endl; + sw << "ScanSpeed:" << lineV << std::endl; + sw << "PointAdjust: 1" << std::endl; + sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; + + for (int line = 0; line < lineNum; line++) + { + int nPositionCnt = (int)gridData[line].size(); + sw << "Line_" << line << "_0_" << nPositionCnt << std::endl; + for (int i = 0; i < nPositionCnt; i++) + { + SVzNL3DPosition& pt3D = gridData[line][i]; + float x = (float)pt3D.pt3D.x; + float y = (float)pt3D.pt3D.y; + float z = (float)pt3D.pt3D.z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}" << std::endl; + } + } + sw.close(); +} + +void _outputScanDataFile_SPointXYZRGB_vector(char* fileName, std::vector>&scanData) +{ + std::ofstream sw(fileName); + int lineNum = scanData.size(); + sw << "LineNum:" << lineNum << std::endl; + sw << "DataType: 0" << std::endl; + sw << "ScanSpeed: 0" << std::endl; + sw << "PointAdjust: 1" << std::endl; + sw << "MaxTimeStamp: 0_0" << std::endl; + + for (int line = 0; line < lineNum; line++) + { + int nPositionCnt = scanData[line].size(); + sw << "Line_" << line << "_0_" << nPositionCnt << std::endl; + for (int i = 0; i < nPositionCnt; i++) + { + SVzNLPointXYZRGBA& pt3D = scanData[line][i]; + char str[250]; + sprintf_s(str, "{ %lf, %lf, %lf, 0, 0, 0 } - { 0, 0 } - { 0, 0 }", + pt3D.x, pt3D.y, pt3D.z); + + sw << str << std::endl; + } + } + sw.close(); +} + +typedef struct +{ + int r; + int g; + int b; +}SG_color; +void _outputScanDataFile_RGBD_obj(char* fileName, std::vector>& scanData, + float lineV, int maxTimeStamp, int clockPerSecond, WD_wheelArchInfo& wheelArcHeight) +{ + int lineNum = (int)scanData.size(); + std::ofstream sw(fileName); + int realLines = lineNum; + realLines++; + sw << "LineNum:" << realLines << std::endl; + sw << "DataType: 0" << std::endl; + sw << "ScanSpeed:" << lineV << std::endl; + sw << "PointAdjust: 1" << std::endl; + sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; + + int maxLineIndex = 0; + int max_stamp = 0; + + SG_color rgb = { 0, 0, 0 }; + + SG_color objColor[8] = { + {245,222,179},//娣¢粍鑹 + {210,105, 30},//宸у厠鍔涜壊 + {240,230,140},//榛勮鑹 + {135,206,235},//澶╄摑鑹 + {250,235,215},//鍙よ懀鐧 + {189,252,201},//钖勮嵎鑹 + {221,160,221},//姊呯孩鑹 + {188,143,143},//鐜懓绾㈣壊 + }; + int size = 1; + int nTimeStamp = 0; + double alpha = 0.8; + for (int line = 0; line < lineNum; line++) + { + int nPositionCnt = (int)scanData[line].size(); + sw << "Line_" << line << "_0_" << nPositionCnt << std::endl; + for (int i = 0; i < nPositionCnt; i++) + { + SVzNL3DPosition& pt3D = scanData[line][i]; + int type = pt3D.nPointIdx; + if (1 == type) + { + rgb = { 0,255,0 }; + rgb.r = (int)((double)rgb.r * alpha); + rgb.g = (int)((double)rgb.g * alpha); + rgb.b = (int)((double)rgb.b * alpha); + size = 2; + } + else if (2 == type) + { + rgb = { 0,0,255 }; + rgb.r = (int)((double)rgb.r * alpha); + rgb.g = (int)((double)rgb.g * alpha); + rgb.b = (int)((double)rgb.b * alpha); + size = 2; + } + else if (3 == type) //杞湁 + { + rgb = { 255, 0, 0 }; + size = 3; + } + else if (4 == type) // + { + rgb = { 255, 255, 0 }; + size = 3; + } + else if (5 == type) // + { + rgb = { 255, 255, 0 }; + size = 3; + } + else + { + rgb = { 100, 100, 100 }; + size = 1; + } + + float x = (float)pt3D.pt3D.x; + float y = (float)pt3D.pt3D.y; + float z = (float)pt3D.pt3D.z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + } + } + //if (objOps.size() > 0) + { + int ptNum = 3; + sw << "Line_" << lineNum << "_" << (nTimeStamp + 1000) << "_" << ptNum << std::endl; + + rgb = { 255, 0, 0 }; + + size = 10; + float x = (float)wheelArcHeight.wheelArchPos.x; + float y = (float)wheelArcHeight.wheelArchPos.y; + float z = (float)wheelArcHeight.wheelArchPos.z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + + x = (float)wheelArcHeight.wheelUpPos.x; + y = (float)wheelArcHeight.wheelUpPos.y; + z = (float)wheelArcHeight.wheelUpPos.z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + + x = (float)wheelArcHeight.wheelDownPos.x; + y = (float)wheelArcHeight.wheelDownPos.y; + z = (float)wheelArcHeight.wheelDownPos.z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + + x = (float)wheelArcHeight.wheelArchPos.x; + y = (float)wheelArcHeight.wheelArchPos.y; + z = (float)wheelArcHeight.wheelArchPos.z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + } + //鐢诲嚭鏂瑰悜绾 + rgb = { 255, 0, 0 }; + size = 3; + int lineIdx = 0; + sw << "Poly_" << lineIdx << "_2" << std::endl; + sw << "{" << (float)wheelArcHeight.arcLine[0].x << "," << (float)wheelArcHeight.arcLine[0].y << "," << (float)wheelArcHeight.arcLine[0].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + sw << "{" << (float)wheelArcHeight.arcLine[1].x << "," << (float)wheelArcHeight.arcLine[1].y << "," << (float)wheelArcHeight.arcLine[1].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + + lineIdx++; + sw << "Poly_" << lineIdx << "_2" << std::endl; + sw << "{" << (float)wheelArcHeight.upLine[0].x << "," << (float)wheelArcHeight.upLine[0].y << "," << (float)wheelArcHeight.upLine[0].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + sw << "{" << (float)wheelArcHeight.upLine[1].x << "," << (float)wheelArcHeight.upLine[1].y << "," << (float)wheelArcHeight.upLine[1].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + + lineIdx++; + sw << "Poly_" << lineIdx << "_2" << std::endl; + sw << "{" << (float)wheelArcHeight.downLine[0].x << "," << (float)wheelArcHeight.downLine[0].y << "," << (float)wheelArcHeight.downLine[0].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + sw << "{" << (float)wheelArcHeight.downLine[1].x << "," << (float)wheelArcHeight.downLine[1].y << "," << (float)wheelArcHeight.downLine[1].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + + lineIdx++; + sw << "Poly_" << lineIdx << "_2" << std::endl; + sw << "{" << (float)wheelArcHeight.centerLine[0].x << "," << (float)wheelArcHeight.centerLine[0].y << "," << (float)wheelArcHeight.centerLine[0].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + sw << "{" << (float)wheelArcHeight.centerLine[1].x << "," << (float)wheelArcHeight.centerLine[1].y << "," << (float)wheelArcHeight.centerLine[1].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + + lineIdx++; + sw << "Poly_" << lineIdx << "_2" << std::endl; + sw << "{" << (float)wheelArcHeight.arcLine[0].x << "," << (float)wheelArcHeight.arcLine[0].y << "," << (float)wheelArcHeight.arcLine[0].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + sw << "{" << (float)wheelArcHeight.arcLine[1].x << "," << (float)wheelArcHeight.arcLine[1].y << "," << (float)wheelArcHeight.arcLine[1].z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; + + sw.close(); +} + +void _outputScanDataFile_obj_vector(char* fileName, std::vector>scanData, + std::vector&objOps) +{ + int lineNum = scanData.size(); + std::ofstream sw(fileName); + int realLines = lineNum; + if (objOps.size() > 0) + realLines++; + sw << "LineNum:" << realLines << std::endl; + sw << "DataType: 0" << std::endl; + sw << "ScanSpeed: 0" << std::endl; + sw << "PointAdjust: 1" << std::endl; + sw << "MaxTimeStamp: 0_0" << std::endl; + + int maxLineIndex = 0; + int max_stamp = 0; + + SG_color rgb = { 0, 0, 0 }; + + SG_color objColor[8] = { + {245,222,179},//娣¢粍鑹 + {210,105, 30},//宸у厠鍔涜壊 + {240,230,140},//榛勮鑹 + {135,206,235},//澶╄摑鑹 + {250,235,215},//鍙よ懀鐧 + {189,252,201},//钖勮嵎鑹 + {221,160,221},//姊呯孩鑹 + {188,143,143},//鐜懓绾㈣壊 + }; + int size = 1; + int nTimeStamp = 0; + for (int line = 0; line < lineNum; line++) + { + int nPositionCnt = scanData[line].size(); + sw << "Line_" << line << "_0_" << nPositionCnt << std::endl; + for (int i = 0; i < nPositionCnt; i++) + { + SVzNL3DPosition* pt3D = &scanData[line][i]; + int vType = pt3D->nPointIdx & 0xff; + int hType = vType >> 4; + int objId = (pt3D->nPointIdx >> 16) & 0xffff; + vType = vType & 0x0f; + if (LINE_FEATURE_L_JUMP_H2L == vType) + { + rgb = { 255, 97, 0 }; + size = 3; + } + else if (LINE_FEATURE_L_JUMP_L2H == vType) + { + rgb = { 255, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_V_SLOPE == vType) + { + rgb = { 255, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_H2L == vType) + { + rgb = { 160, 82, 45 }; + size = 3; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) + { + rgb = { 255, 0, 0 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_L2H == vType) + { + rgb = { 233, 150, 122 }; + size = 3; + } + else if (LINE_FEATURE_L_JUMP_H2L == hType) + { + rgb = { 0, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_L_JUMP_L2H == hType) + { + rgb = { 0, 255, 255 }; + size = 3; + } + else if (LINE_FEATURE_V_SLOPE == hType) + { + rgb = { 0, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_H2L == hType) + { + rgb = { 85, 107, 47 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_L2H == hType) + { + rgb = { 0, 255, 154 }; + size = 3; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) + { + rgb = { 255, 0, 0 }; + size = 3; + } + else if (objId > 0) //鐩爣 + { + rgb = objColor[objId % 8]; + size = 5; + } + else + { + rgb = { 200, 200, 200 }; + size = 1; + } + + float x = (float)pt3D->pt3D.x; + float y = (float)pt3D->pt3D.y; + float z = (float)pt3D->pt3D.z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + } + } + if (objOps.size() > 0) + { + int ptNum = objOps.size(); + sw << "Line_" << lineNum << "_0_" << ptNum << std::endl; + for (int i = 0; i < objOps.size(); i++) + { + if (i == 0) + rgb = { 255, 0, 0 }; + else + rgb = { 255, 255, 0 }; + size = 25; + float x = (float)objOps[i].centerPos.x; + float y = (float)objOps[i].centerPos.y; + float z = (float)objOps[i].centerPos.z; + + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + if (i == 0) + { + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + } + } + } + //鐢诲嚭鏂瑰悜绾 + sw.close(); +} + +void _outputRGBDScanDataFile_RGBD_obj(char* fileName, SVzNLXYZRGBDLaserLine * scanData, int lineNum, + float lineV, int maxTimeStamp, int clockPerSecond, std::vector&objOps) +{ + std::ofstream sw(fileName); + int realLines = lineNum; + if (objOps.size() > 0) + realLines++; + sw << "LineNum:" << realLines << std::endl; + sw << "DataType: 0" << std::endl; + sw << "ScanSpeed:" << lineV << std::endl; + sw << "PointAdjust: 1" << std::endl; + sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; + + int maxLineIndex = 0; + int max_stamp = 0; + + SG_color rgb = { 0, 0, 0 }; + + SG_color objColor[8] = { + {245,222,179},//娣¢粍鑹 + {210,105, 30},//宸у厠鍔涜壊 + {240,230,140},//榛勮鑹 + {135,206,235},//澶╄摑鑹 + {250,235,215},//鍙よ懀鐧 + {189,252,201},//钖勮嵎鑹 + {221,160,221},//姊呯孩鑹 + {188,143,143},//鐜懓绾㈣壊 + }; + int size = 1; + int nTimeStamp = 0; + for (int line = 0; line < lineNum; line++) + { + sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << scanData[line].nPointCnt << std::endl; + nTimeStamp = scanData[line].nTimeStamp; + for (int i = 0; i < scanData[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; +#if 0 + int vType = pt3D->nRGB & 0xff; + int hType = vType >> 4; + int objId = (pt3D->nRGB >> 16) & 0xffff; + vType = vType & 0x0f; + if (LINE_FEATURE_L_JUMP_H2L == vType) + { + rgb = { 255, 97, 0 }; + size = 3; + } + else if (LINE_FEATURE_L_JUMP_L2H == vType) + { + rgb = { 255, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_V_SLOPE == vType) + { + rgb = { 255, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_H2L == vType) + { + rgb = { 160, 82, 45 }; + size = 3; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) + { + rgb = { 255, 0, 0 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_L2H == vType) + { + rgb = { 233, 150, 122 }; + size = 3; + } + else if (LINE_FEATURE_L_JUMP_H2L == hType) + { + rgb = { 0, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_L_JUMP_L2H == hType) + { + rgb = { 0, 255, 255 }; + size = 3; + } + else if (LINE_FEATURE_V_SLOPE == hType) + { + rgb = { 0, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_H2L == hType) + { + rgb = { 85, 107, 47 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_L2H == hType) + { + rgb = { 0, 255, 154 }; + size = 3; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) + { + rgb = { 255, 0, 0 }; + size = 3; + } + else if (objId > 0) //鐩爣 + { + rgb = objColor[objId % 8]; + size = 5; + } + else +#endif + { + rgb = { 200, 200, 200 }; + size = 1; + } + float x = (float)pt3D->x; + float y = (float)pt3D->y; + float z = (float)pt3D->z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + } + } + if (objOps.size() > 0) + { + int ptNum = objOps.size(); + sw << "Line_" << lineNum << "_" << (nTimeStamp + 1000) << "_" << ptNum << std::endl; + for (int i = 0; i < objOps.size(); i++) + { + if (i == 0) + rgb = { 255, 0, 0 }; + else + rgb = { 255, 255, 0 }; + size = 25; + float x = (float)objOps[i].centerPos.x; + float y = (float)objOps[i].centerPos.y; + float z = (float)objOps[i].centerPos.z; + + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + if (i == 0) + { + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + } + } + } + sw.close(); +} + +void _outputScanDataFile_RGBD_sideBagObj(char* fileName, SVzNL3DLaserLine * scanData, int lineNum, + float lineV, int maxTimeStamp, int clockPerSecond, std::vector&objOps) +{ + std::ofstream sw(fileName); + int realLines = lineNum; + if (objOps.size() > 0) + realLines++; + sw << "LineNum:" << realLines << std::endl; + sw << "DataType: 0" << std::endl; + sw << "ScanSpeed:" << lineV << std::endl; + sw << "PointAdjust: 1" << std::endl; + sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; + + int maxLineIndex = 0; + int max_stamp = 0; + + SG_color rgb = { 0, 0, 0 }; + + SG_color objColor[8] = { + {245,222,179},//娣¢粍鑹 + {210,105, 30},//宸у厠鍔涜壊 + {240,230,140},//榛勮鑹 + {135,206,235},//澶╄摑鑹 + {250,235,215},//鍙よ懀鐧 + {189,252,201},//钖勮嵎鑹 + {221,160,221},//姊呯孩鑹 + {188,143,143},//鐜懓绾㈣壊 + }; + int size = 1; + int nTimeStamp = 0; + for (int line = 0; line < lineNum; line++) + { + int nPntCount = 0; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; + if (pt3D->pt3D.z > 1e-4) + nPntCount++; + } + sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << nPntCount << std::endl; + nTimeStamp = scanData[line].nTimeStamp; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; + if (pt3D->pt3D.z < 1e-4) + continue; + + int vType = pt3D->nPointIdx & 0xff; + int hType = vType >> 4; + int objId = (pt3D->nPointIdx >> 16) & 0xffff; + vType = vType & 0x0f; + if (LINE_FEATURE_L_JUMP_H2L == vType) + { + rgb = { 255, 97, 0 }; + size = 3; + } + else if (LINE_FEATURE_L_JUMP_L2H == vType) + { + rgb = { 255, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_V_SLOPE == vType) + { + rgb = { 255, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_H2L == vType) + { + rgb = { 160, 82, 45 }; + size = 3; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) + { + rgb = { 255, 0, 0 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_L2H == vType) + { + rgb = { 233, 150, 122 }; + size = 3; + } + else if (LINE_FEATURE_L_JUMP_H2L == hType) + { + rgb = { 0, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_L_JUMP_L2H == hType) + { + rgb = { 0, 255, 255 }; + size = 3; + } + else if (LINE_FEATURE_V_SLOPE == hType) + { + rgb = { 0, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_H2L == hType) + { + rgb = { 85, 107, 47 }; + size = 3; + } + else if (LINE_FEATURE_L_SLOPE_L2H == hType) + { + rgb = { 0, 255, 154 }; + size = 3; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) + { + rgb = { 255, 0, 0 }; + size = 3; + } + else if (objId > 0) //鐩爣 + { + rgb = objColor[objId % 8]; + size = 5; + } + else + { + rgb = { 200, 200, 200 }; + size = 1; + } + + float x = (float)pt3D->pt3D.x; + float y = (float)pt3D->pt3D.y; + float z = (float)pt3D->pt3D.z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + } + } + if (objOps.size() > 0) + { + int ptNum = objOps.size(); + sw << "Line_" << lineNum << "_" << (nTimeStamp + 1000) << "_" << ptNum << std::endl; + for (int i = 0; i < objOps.size(); i++) + { + if (i == 0) + rgb = { 255, 0, 0 }; + else + rgb = { 255, 255, 0 }; + size = 25; + float x = (float)objOps[i].graspPos.x; + float y = (float)objOps[i].graspPos.y; + float z = (float)objOps[i].graspPos.z; + + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + if (i == 0) + { + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + } + } + } + sw.close(); +} + +void EulerRpyToRotation1(double rpy[3], double matrix3d[9]) { + double cos0 = cos(rpy[0] * PI / 180); + double sin0 = sin(rpy[0] * PI / 180); + double cos1 = cos(rpy[1] * PI / 180); + double sin1 = sin(rpy[1] * PI / 180); + double cos2 = cos(rpy[2] * PI / 180); + double sin2 = sin(rpy[2] * PI / 180); + matrix3d[0] = cos2 * cos1; + matrix3d[1] = cos2 * sin1 * sin0 - sin2 * cos0; + matrix3d[2] = cos2 * sin1 * cos0 + sin2 * sin0; + matrix3d[3] = sin2 * cos1; + matrix3d[4] = sin2 * sin1 * sin0 + cos2 * cos0; + matrix3d[5] = sin2 * sin1 * cos0 - cos2 * sin0; + matrix3d[6] = -sin1; + matrix3d[7] = cos1 * sin0; + matrix3d[8] = cos1 * cos0; + return; +} + +void _rotateCloudPts(SVzNL3DLaserLine * scanData, int lineNum, double matrix3d[9], std::vector>&rotateLines, SVzNLRangeD * rx_range, SVzNLRangeD * ry_range) +{ + rx_range->min = 0; + rx_range->max = -1; + ry_range->min = 0; + ry_range->max = -1; + for (int line = 0; line < lineNum; line++) + { + std::vector< SVzNL3DPosition> linePts; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; + if (pt3D->pt3D.z < 1e-4) + continue; + + SVzNL3DPosition r_pt; + r_pt.pt3D = _ptRotate(pt3D->pt3D, matrix3d); + r_pt.nPointIdx = pt3D->nPointIdx; + if (rx_range->max < rx_range->min) + { + rx_range->min = r_pt.pt3D.x; + rx_range->max = r_pt.pt3D.x; + } + else + { + if (rx_range->min > r_pt.pt3D.x) + rx_range->min = r_pt.pt3D.x; + if (rx_range->max < r_pt.pt3D.x) + rx_range->max = r_pt.pt3D.x; + } + if (ry_range->max < ry_range->min) + { + ry_range->min = r_pt.pt3D.y; + ry_range->max = r_pt.pt3D.y; + } + else + { + if (ry_range->min > r_pt.pt3D.y) + ry_range->min = r_pt.pt3D.y; + if (ry_range->max < r_pt.pt3D.y) + ry_range->max = r_pt.pt3D.y; + } + linePts.push_back(r_pt); + + } + rotateLines.push_back(linePts); + } +} + +void _rotateCloudPts_RGBD(SVzNLXYZRGBDLaserLine * scanData, int lineNum, double matrix3d[9], std::vector>&rotateLines, SVzNLRangeD * rx_range, SVzNLRangeD * ry_range) +{ + rx_range->min = 0; + rx_range->max = -1; + ry_range->min = 0; + ry_range->max = -1; + for (int line = 0; line < lineNum; line++) + { + std::vector< SVzNLPointXYZRGBA> linePts; + for (int i = 0; i < scanData[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; + if (pt3D->z < 1e-4) + continue; + + SVzNLPointXYZRGBA r_pt; + r_pt = _ptRotate_RGBD(*pt3D, matrix3d); + if (rx_range->max < rx_range->min) + { + rx_range->min = r_pt.x; + rx_range->max = r_pt.x; + } + else + { + if (rx_range->min > r_pt.x) + rx_range->min = r_pt.x; + if (rx_range->max < r_pt.x) + rx_range->max = r_pt.x; + } + if (ry_range->max < ry_range->min) + { + ry_range->min = r_pt.y; + ry_range->max = r_pt.y; + } + else + { + if (ry_range->min > r_pt.y) + ry_range->min = r_pt.y; + if (ry_range->max < r_pt.y) + ry_range->max = r_pt.y; + } + linePts.push_back(r_pt); + } + rotateLines.push_back(linePts); + } +} + +void _XOYprojection( + cv::Mat & img, + std::vector>&dataLines, + std::vector&objOps, + const double x_scale, + const double y_scale, + const SVzNLRangeD x_range, + const SVzNLRangeD y_range, + bool drawDirAngle, + int dirAngleLen) +{ + int x_skip = 16; + int y_skip = 16; + + cv::Vec3b rgb = cv::Vec3b(0, 0, 0); + cv::Vec3b objColor[8] = { + {245,222,179},//娣¢粍鑹 + {210,105, 30},//宸у厠鍔涜壊 + {240,230,140},//榛勮鑹 + {135,206,235},//澶╄摑鑹 + {250,235,215},//鍙よ懀鐧 + {189,252,201},//钖勮嵎鑹 + {221,160,221},//姊呯孩鑹 + {188,143,143},//鐜懓绾㈣壊 + }; + int size = 1; + for (int line = 0; line < dataLines.size(); line++) + { + std::vector< SVzNL3DPosition>& a_line = dataLines[line]; + for (int i = 0; i < a_line.size(); i++) + { + SVzNL3DPosition* pt3D = &a_line[i]; + if (pt3D->pt3D.z < 1e-4) + continue; + + int vType = pt3D->nPointIdx & 0xff; + int hType = vType >> 4; + int objId = (pt3D->nPointIdx >> 16) & 0xff; + vType = vType & 0x0f; + if (LINE_FEATURE_L_JUMP_H2L == vType) + { + rgb = { 255, 97, 0 }; + size = 2; + } + else if (LINE_FEATURE_L_JUMP_L2H == vType) + { + rgb = { 255, 255, 0 }; + size = 2; + } + else if (LINE_FEATURE_V_SLOPE == vType) + { + rgb = { 255, 0, 255 }; + size = 2; + } + else if (LINE_FEATURE_L_SLOPE_H2L == vType) + { + rgb = { 160, 82, 45 }; + size = 2; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) + { + rgb = { 255, 0, 0 }; + size = 2; + } + else if (LINE_FEATURE_L_SLOPE_L2H == vType) + { + rgb = { 233, 150, 122 }; + size = 2; + } + else if (LINE_FEATURE_L_JUMP_H2L == hType) + { + rgb = { 0, 0, 255 }; + size = 2; + } + else if (LINE_FEATURE_L_JUMP_L2H == hType) + { + rgb = { 0, 255, 255 }; + size = 2; + } + else if (LINE_FEATURE_V_SLOPE == hType) + { + rgb = { 0, 255, 0 }; + size = 2; + } + else if (LINE_FEATURE_L_SLOPE_H2L == hType) + { + rgb = { 85, 107, 47 }; + size = 2; + } + else if (LINE_FEATURE_L_SLOPE_L2H == hType) + { + rgb = { 0, 255, 154 }; + size = 2; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) + { + rgb = { 255, 0, 0 }; + size = 2; + } + else if (objId > 0) //鐩爣 + { + rgb = objColor[objId % 8]; + size = 1; + } + else + { + rgb = { 150, 150, 150 }; + size = 1; + } + + + double x = pt3D->pt3D.x; + double y = pt3D->pt3D.y; + + int px = (int)((x - x_range.min) / x_scale + x_skip); + int py = (int)((y - y_range.min) / y_scale + y_skip); + if (size == 1) + img.at(py, px) = cv::Vec3b(rgb[2], rgb[1], rgb[0]); + else + cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); + } + } + if (objOps.size() > 0) + { + for (int i = 0; i < objOps.size(); i++) + { + if (i == 0) + { + rgb = { 255, 0, 0 }; + size = 20; + } + else + { + rgb = { 255, 255, 0 }; + size = 10; + } + int px = (int)((objOps[i].centerPos.x - x_range.min) / x_scale + x_skip); + int py = (int)((objOps[i].centerPos.y - y_range.min) / y_scale + y_skip); + cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); + if (true == drawDirAngle) + { + //鐢荤嚎 + double R = (double)dirAngleLen / 2.0; + const double deg2rad = PI / 180.0; + const double yaw = objOps[i].centerPos.z_yaw * deg2rad; + double cy = cos(yaw); double sy = sin(yaw); + double x1 = objOps[i].centerPos.x + R * cy; double y1 = objOps[i].centerPos.y - R * sy; + double x2 = objOps[i].centerPos.x - R * cy; double y2 = objOps[i].centerPos.y + R * sy; + int px1 = (int)((x1 - x_range.min) / x_scale + x_skip); + int py1 = (int)((y1 - y_range.min) / y_scale + y_skip); + int px2 = (int)((x2 - x_range.min) / x_scale + x_skip); + int py2 = (int)((y2 - y_range.min) / y_scale + y_skip); + cv::line(img, cv::Point(px1, py1), cv::Point(px2, py2), cv::Scalar(rgb[2], rgb[1], rgb[0]), 2); + } + } + } + +} + +void _XOYprojection_RGBD( + cv::Mat & img, + std::vector>&dataLines, + std::vector&objOps, + const double x_scale, + const double y_scale, + const SVzNLRangeD x_range, + const SVzNLRangeD y_range, + bool drawDirAngle, + int dirAngleLen) +{ + int x_skip = 16; + int y_skip = 16; + + cv::Vec3b rgb = cv::Vec3b(0, 0, 0); + cv::Vec3b objColor[8] = { + {245,222,179},//娣¢粍鑹 + {210,105, 30},//宸у厠鍔涜壊 + {240,230,140},//榛勮鑹 + {135,206,235},//澶╄摑鑹 + {250,235,215},//鍙よ懀鐧 + {189,252,201},//钖勮嵎鑹 + {221,160,221},//姊呯孩鑹 + {188,143,143},//鐜懓绾㈣壊 + }; + int size = 1; + for (int line = 0; line < dataLines.size(); line++) + { + std::vector< SVzNLPointXYZRGBA>& a_line = dataLines[line]; + for (int i = 0; i < a_line.size(); i++) + { + SVzNLPointXYZRGBA* pt3D = &a_line[i]; + if (pt3D->z < 1e-4) + continue; + + int nRGB = pt3D->nRGB; + int r = nRGB & 0xff; + nRGB >>= 8; + int g = nRGB & 0xff; + nRGB >>= 8; + int b = nRGB & 0xff; + + rgb[0] = r; + rgb[1] = g; + rgb[2] = b; + size = 1; + double x = pt3D->x; + double y = pt3D->y; + int px = (int)((x - x_range.min) / x_scale + x_skip); + int py = (int)((y - y_range.min) / y_scale + y_skip); + if ((px == 666) && (py == 828)) + int kkk = 1; + if (size == 1) + img.at(py, px) = cv::Vec3b(rgb[2], rgb[1], rgb[0]); + else + cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); + } + } + if (objOps.size() > 0) + { + for (int i = 0; i < objOps.size(); i++) + { + if (i == 0) + { + rgb = { 255, 0, 0 }; + size = 20; + } + else + { + rgb = { 255, 255, 0 }; + size = 10; + } + int px = (int)((objOps[i].centerPos.x - x_range.min) / x_scale + x_skip); + int py = (int)((objOps[i].centerPos.y - y_range.min) / y_scale + y_skip); + cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); + if (true == drawDirAngle) + { + //鐢荤嚎 + double R = (double)dirAngleLen / 2.0; + const double deg2rad = PI / 180.0; + const double yaw = objOps[i].centerPos.z_yaw * deg2rad; + double cy = cos(yaw); double sy = sin(yaw); + double arrowLen = R / 3; + double arrowAngle = 30; + double ca = cos(arrowAngle * deg2rad); + double sa = sin(arrowAngle * deg2rad); + SVzNL2DPointD endingPt[4]; + endingPt[0] = { R, 0 }; + endingPt[1] = { -R, 0 }; + endingPt[2] = { R - arrowLen * ca, -arrowLen * sa }; + endingPt[3] = { R - arrowLen * ca, arrowLen * sa }; + for (int m = 0; m < 4; m++) + { + double tmp_x = endingPt[m].x * cy - endingPt[m].y * sy; + double tmp_y = -endingPt[m].x * sy - endingPt[m].y * cy; + endingPt[m].x = tmp_x + objOps[i].centerPos.x; + endingPt[m].y = tmp_y + objOps[i].centerPos.y; + } + int px1 = (int)((endingPt[0].x - x_range.min) / x_scale + x_skip); + int py1 = (int)((endingPt[0].y - y_range.min) / y_scale + y_skip); + int px2 = (int)((endingPt[1].x - x_range.min) / x_scale + x_skip); + int py2 = (int)((endingPt[1].y - y_range.min) / y_scale + y_skip); + cv::line(img, cv::Point(px1, py1), cv::Point(px2, py2), cv::Scalar(rgb[2], rgb[1], rgb[0]), 2); + int px3 = (int)((endingPt[2].x - x_range.min) / x_scale + x_skip); + int py3 = (int)((endingPt[2].y - y_range.min) / y_scale + y_skip); + int px4 = (int)((endingPt[3].x - x_range.min) / x_scale + x_skip); + int py4 = (int)((endingPt[3].y - y_range.min) / y_scale + y_skip); + if (objOps[i].orienFlag == 1) + { + cv::line(img, cv::Point(px1, py1), cv::Point(px3, py3), cv::Scalar(0, 255, 0), 2); + cv::line(img, cv::Point(px1, py1), cv::Point(px4, py4), cv::Scalar(0, 255, 0), 2); + //cv::circle(img, cv::Point(px1, py1), 5, cv::Scalar(0, 255, 0), -1); + } + else if (objOps[i].orienFlag == 2) + { + cv::line(img, cv::Point(px1, py1), cv::Point(px3, py3), cv::Scalar(0, 0, 255), 2); + cv::line(img, cv::Point(px1, py1), cv::Point(px4, py4), cv::Scalar(0, 0, 255), 2); + //cv::circle(img, cv::Point(px1, py1), 5, cv::Scalar(0, 0, 255), -1); + } + } + } + } + +} + +void _XOYprojection_sideBagInfo(cv::Mat & img, std::vector>&dataLines, std::vector&objOps, + const double x_scale, const double y_scale, const SVzNLRangeD x_range, const SVzNLRangeD y_range) +{ + int x_skip = 16; + int y_skip = 16; + + cv::Vec3b rgb = cv::Vec3b(0, 0, 0); + cv::Vec3b objColor[8] = { + {245,222,179},//娣¢粍鑹 + {210,105, 30},//宸у厠鍔涜壊 + {240,230,140},//榛勮鑹 + {135,206,235},//澶╄摑鑹 + {250,235,215},//鍙よ懀鐧 + {189,252,201},//钖勮嵎鑹 + {221,160,221},//姊呯孩鑹 + {188,143,143},//鐜懓绾㈣壊 + }; + int size = 1; + for (int line = 0; line < dataLines.size(); line++) + { + std::vector< SVzNL3DPosition>& a_line = dataLines[line]; + for (int i = 0; i < a_line.size(); i++) + { + SVzNL3DPosition* pt3D = &a_line[i]; + if (pt3D->pt3D.z < 1e-4) + continue; + + int vType = pt3D->nPointIdx & 0xff; + int hType = vType >> 4; + int objId = (pt3D->nPointIdx >> 16) & 0xffff; + vType = vType & 0x0f; + if (LINE_FEATURE_L_JUMP_H2L == vType) + { + rgb = { 255, 97, 0 }; + size = 2; + } + else if (LINE_FEATURE_L_JUMP_L2H == vType) + { + rgb = { 255, 255, 0 }; + size = 2; + } + else if (LINE_FEATURE_V_SLOPE == vType) + { + rgb = { 255, 0, 255 }; + size = 2; + } + else if (LINE_FEATURE_L_SLOPE_H2L == vType) + { + rgb = { 160, 82, 45 }; + size = 2; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == vType) || (LINE_FEATURE_LINE_ENDING_1 == vType)) + { + rgb = { 255, 0, 0 }; + size = 2; + } + else if (LINE_FEATURE_L_SLOPE_L2H == vType) + { + rgb = { 233, 150, 122 }; + size = 2; + } + else if (LINE_FEATURE_L_JUMP_H2L == hType) + { + rgb = { 0, 0, 255 }; + size = 2; + } + else if (LINE_FEATURE_L_JUMP_L2H == hType) + { + rgb = { 0, 255, 255 }; + size = 2; + } + else if (LINE_FEATURE_V_SLOPE == hType) + { + rgb = { 0, 255, 0 }; + size = 2; + } + else if (LINE_FEATURE_L_SLOPE_H2L == hType) + { + rgb = { 85, 107, 47 }; + size = 2; + } + else if (LINE_FEATURE_L_SLOPE_L2H == hType) + { + rgb = { 0, 255, 154 }; + size = 2; + } + else if ((LINE_FEATURE_LINE_ENDING_0 == hType) || (LINE_FEATURE_LINE_ENDING_1 == hType)) + { + rgb = { 255, 0, 0 }; + size = 2; + } + else if ((objId > 0) && (objId < 1000)) //鐩爣 + { + rgb = objColor[objId % 8]; + size = 3; + } + else + { + rgb = { 150, 150, 150 }; + size = 1; + } + + + double x = pt3D->pt3D.x; + double y = pt3D->pt3D.y; + + int px = (int)((x - x_range.min) / x_scale + x_skip); + int py = (int)((y - y_range.min) / y_scale + y_skip); + if (size == 1) + img.at(py, px) = cv::Vec3b(rgb[2], rgb[1], rgb[0]); + else + cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); + } + } + if (objOps.size() > 0) + { + for (int i = 0; i < objOps.size(); i++) + { + if (i == 0) + { + rgb = { 255, 0, 0 }; + size = 20; + } + else + { + rgb = { 255, 255, 0 }; + size = 10; + } + int px = (int)((objOps[i].graspPos.x - x_range.min) / x_scale + x_skip); + int py = (int)((objOps[i].graspPos.y - y_range.min) / y_scale + y_skip); + cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); + + //鐢籖OI + size = 3; + cv::Point2d vec2d[4]; + vec2d[0].x = objOps[i].objROI.left; vec2d[0].y = objOps[i].objROI.top; + vec2d[1].x = objOps[i].objROI.right; vec2d[1].y = objOps[i].objROI.top; + vec2d[2].x = objOps[i].objROI.right; vec2d[2].y = objOps[i].objROI.bottom; + vec2d[3].x = objOps[i].objROI.left; vec2d[3].y = objOps[i].objROI.bottom; + cv::Point vec[4]; + for (int j = 0; j < 4; j++) + { + vec[j].x = (int)((vec2d[j].x - x_range.min) / x_scale + x_skip); + vec[j].y = (int)((vec2d[j].y - y_range.min) / y_scale + y_skip); + } + for (int j = 0; j < 4; j++) + { + int nxtIdx = (j + 1) % 4; + cv::line(img, vec[j], vec[nxtIdx], cv::Scalar(rgb[2], rgb[1], rgb[0]), size); + } + + //鐢诲捐 + double r = 50; + double angle = objOps[i].graspPos.z_yaw; + angle = -angle * PI / 180; + cv::Point2d line_pt[2]; + line_pt[0].x = (int)(r * cos(angle) + px); + line_pt[0].y = (int)(-r * sin(angle) + py); + line_pt[1].x = (int)(-r * cos(angle) + px); + line_pt[1].y = (int)(r * sin(angle) + py); + cv::line(img, line_pt[0], line_pt[1], cv::Scalar(rgb[2], rgb[1], rgb[0]), size); + } + + + } + +} + +void _genXOYProjectionImage(cv::String & fileName, SVzNL3DLaserLine * scanData, int lineNum, std::vector&objOps, double rpy[3], double dirLen) +{ + //缁熻X鍜孻鐨勮寖鍥 + std::vector> scan_lines; + SVzNLRangeD x_range = { 0, -1 }; + SVzNLRangeD y_range = { 0, -1 }; + for (int line = 0; line < lineNum; line++) + { + std::vector< SVzNL3DPosition> a_line; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; + if (pt3D->pt3D.z < 1e-4) + continue; + + a_line.push_back(*pt3D); + if (x_range.max < x_range.min) + { + x_range.min = pt3D->pt3D.x; + x_range.max = pt3D->pt3D.x; + } + else + { + if (x_range.min > pt3D->pt3D.x) + x_range.min = pt3D->pt3D.x; + if (x_range.max < pt3D->pt3D.x) + x_range.max = pt3D->pt3D.x; + } + if (y_range.max < y_range.min) + { + y_range.min = pt3D->pt3D.y; + y_range.max = pt3D->pt3D.y; + } + else + { + if (y_range.min > pt3D->pt3D.y) + y_range.min = pt3D->pt3D.y; + if (y_range.max < pt3D->pt3D.y) + y_range.max = pt3D->pt3D.y; + } + } + scan_lines.push_back(a_line); + } + + int imgRows = 992; + int imgCols = 1056; + double y_rows = 960.0; + double x_cols = 1024.0; + cv::Mat img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); + //璁$畻鎶曞奖姣斾緥 + double x_scale = (x_range.max - x_range.min) / x_cols; + double y_scale = (y_range.max - y_range.min) / y_rows; + if (x_scale < y_scale) + x_scale = y_scale; + else + y_scale = x_scale; + + int angleDrawLen = dirLen;// / x_scale; + _XOYprojection(img, scan_lines, objOps, x_scale, y_scale, x_range, y_range, true, angleDrawLen); + + //鏃嬭浆瑙嗚鏄剧ず + double matrix3d[9]; + EulerRpyToRotation1(rpy, matrix3d); + std::vector r_objOps; + r_objOps.insert(r_objOps.end(), objOps.begin(), objOps.end()); + for (int i = 0; i < objOps.size(); i++) + { + SVzNL3DPoint c_pt = { objOps[i].centerPos.x, objOps[i].centerPos.y, objOps[i].centerPos.z }; + SVzNL3DPoint r_c_pt = _ptRotate(c_pt, matrix3d); + r_objOps[i].centerPos.x = r_c_pt.x; + r_objOps[i].centerPos.y = r_c_pt.y; + r_objOps[i].centerPos.z = r_c_pt.z; + } + std::vector> rotateLines; + SVzNLRangeD rx_range, ry_range; + _rotateCloudPts(scanData, lineNum, matrix3d, rotateLines, &rx_range, &ry_range); + cv::Mat r_img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); + //璁$畻鎶曞奖姣斾緥 + double rx_scale = (rx_range.max - rx_range.min) / x_cols; + double ry_scale = (ry_range.max - ry_range.min) / y_rows; + _XOYprojection(r_img, rotateLines, r_objOps, rx_scale, ry_scale, rx_range, ry_range, false, angleDrawLen); + + cv::Mat dis_img; + cv::hconcat(img, r_img, dis_img); + cv::imwrite(fileName, dis_img); + return; +} + +void _RGBDto2DImage(char* fileName, SVzNLXYZRGBDLaserLine * scanData, int lineNum) +{ + cv::String imgName(fileName); + //缁熻X鍜孻鐨勮寖鍥 + SVzNLRangeD x_range = { 0, -1 }; + SVzNLRangeD y_range = { 0, -1 }; + for (int line = 0; line < lineNum; line++) + { + for (int i = 0; i < scanData[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; + if (pt3D->z < 1e-4) + continue; + + if (x_range.max < x_range.min) + { + x_range.min = pt3D->x; + x_range.max = pt3D->x; + } + else + { + if (x_range.min > pt3D->x) + x_range.min = pt3D->x; + if (x_range.max < pt3D->x) + x_range.max = pt3D->x; + } + if (y_range.max < y_range.min) + { + y_range.min = pt3D->y; + y_range.max = pt3D->y; + } + else + { + if (y_range.min > pt3D->y) + y_range.min = pt3D->y; + if (y_range.max < pt3D->y) + y_range.max = pt3D->y; + } + } + } + + int x_skip = 16; + int y_skip = 16; + int imgRows = 992; + int imgCols = 1056; + double y_rows = (double)(imgRows - y_skip * 2); + double x_cols = (double)(imgCols - x_skip * 2); + cv::Mat img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); + //璁$畻鎶曞奖姣斾緥 + double x_scale = (x_range.max - x_range.min) / x_cols; + double y_scale = (y_range.max - y_range.min) / y_rows; + if (x_scale < y_scale) + x_scale = y_scale; + else + y_scale = x_scale; + + int size = 1; + cv::Vec3b rgb = cv::Vec3b(0, 0, 0); + for (int line = 0; line < lineNum; line++) + { + for (int i = 0; i < scanData[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; + if (pt3D->z < 1e-4) + continue; + + int nRGB = pt3D->nRGB; + int r = nRGB & 0xff; + nRGB >>= 8; + int g = nRGB & 0xff; + nRGB >>= 8; + int b = nRGB & 0xff; + + rgb[0] = r; + rgb[1] = g; + rgb[2] = b; + size = 1; + double x = pt3D->x; + double y = pt3D->y; + int px = (int)((x - x_range.min) / x_scale + x_skip); + int py = (int)((y - y_range.min) / y_scale + y_skip); + if ((px == 666) && (py == 828)) + int kkk = 1; + if (size == 1) + img.at(py, px) = cv::Vec3b(rgb[2], rgb[1], rgb[0]); + else + cv::circle(img, cv::Point(px, py), size, cv::Scalar(rgb[2], rgb[1], rgb[0]), -1); + } + } + cv::imwrite(imgName, img); + return; +} + +void _genXOYProjectionImage_RGBD(cv::String & fileName, SVzNLXYZRGBDLaserLine * scanData, int lineNum, std::vector&objOps, double rpy[3], double dirLen) +{ + //缁熻X鍜孻鐨勮寖鍥 + std::vector> scan_lines; + SVzNLRangeD x_range = { 0, -1 }; + SVzNLRangeD y_range = { 0, -1 }; + for (int line = 0; line < lineNum; line++) + { + std::vector< SVzNLPointXYZRGBA> a_line; + for (int i = 0; i < scanData[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA* pt3D = &scanData[line].p3DPoint[i]; + if (pt3D->z < 1e-4) + continue; + + a_line.push_back(*pt3D); + if (x_range.max < x_range.min) + { + x_range.min = pt3D->x; + x_range.max = pt3D->x; + } + else + { + if (x_range.min > pt3D->x) + x_range.min = pt3D->x; + if (x_range.max < pt3D->x) + x_range.max = pt3D->x; + } + if (y_range.max < y_range.min) + { + y_range.min = pt3D->y; + y_range.max = pt3D->y; + } + else + { + if (y_range.min > pt3D->y) + y_range.min = pt3D->y; + if (y_range.max < pt3D->y) + y_range.max = pt3D->y; + } + } + scan_lines.push_back(a_line); + } + + int imgRows = 992; + int imgCols = 1056; + double y_rows = 960.0; + double x_cols = 1024.0; + cv::Mat img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); + //璁$畻鎶曞奖姣斾緥 + double x_scale = (x_range.max - x_range.min) / x_cols; + double y_scale = (y_range.max - y_range.min) / y_rows; + if (x_scale < y_scale) + x_scale = y_scale; + else + y_scale = x_scale; + + int angleDrawLen = dirLen / x_scale; + _XOYprojection_RGBD(img, scan_lines, objOps, x_scale, y_scale, x_range, y_range, true, angleDrawLen); + + //鏃嬭浆瑙嗚鏄剧ず + double matrix3d[9]; + EulerRpyToRotation1(rpy, matrix3d); + std::vector r_objOps; + r_objOps.insert(r_objOps.end(), objOps.begin(), objOps.end()); + for (int i = 0; i < objOps.size(); i++) + { + SVzNL3DPoint c_pt = { objOps[i].centerPos.x, objOps[i].centerPos.y, objOps[i].centerPos.z }; + SVzNL3DPoint r_c_pt = _ptRotate(c_pt, matrix3d); + r_objOps[i].centerPos.x = r_c_pt.x; + r_objOps[i].centerPos.y = r_c_pt.y; + r_objOps[i].centerPos.z = r_c_pt.z; + } + std::vector> rotateLines; + SVzNLRangeD rx_range, ry_range; + _rotateCloudPts_RGBD(scanData, lineNum, matrix3d, rotateLines, &rx_range, &ry_range); + cv::Mat r_img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); + //璁$畻鎶曞奖姣斾緥 + double rx_scale = (rx_range.max - rx_range.min) / x_cols; + double ry_scale = (ry_range.max - ry_range.min) / y_rows; + _XOYprojection_RGBD(r_img, rotateLines, r_objOps, rx_scale, ry_scale, rx_range, ry_range, false, angleDrawLen); + + cv::Mat dis_img; + cv::hconcat(img, r_img, dis_img); + cv::imwrite(fileName, dis_img); + return; +} + +void _genXOYProjectionImage_sideBagInfo(cv::String & fileName, SVzNL3DLaserLine * scanData, int lineNum, std::vector&objOps, double rpy[3]) +{ + //缁熻X鍜孻鐨勮寖鍥 + std::vector> scan_lines; + SVzNLRangeD x_range = { 0, -1 }; + SVzNLRangeD y_range = { 0, -1 }; + for (int line = 0; line < lineNum; line++) + { + std::vector< SVzNL3DPosition> a_line; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; + if (pt3D->pt3D.z < 1e-4) + continue; + + a_line.push_back(*pt3D); + if (x_range.max < x_range.min) + { + x_range.min = pt3D->pt3D.x; + x_range.max = pt3D->pt3D.x; + } + else + { + if (x_range.min > pt3D->pt3D.x) + x_range.min = pt3D->pt3D.x; + if (x_range.max < pt3D->pt3D.x) + x_range.max = pt3D->pt3D.x; + } + if (y_range.max < y_range.min) + { + y_range.min = pt3D->pt3D.y; + y_range.max = pt3D->pt3D.y; + } + else + { + if (y_range.min > pt3D->pt3D.y) + y_range.min = pt3D->pt3D.y; + if (y_range.max < pt3D->pt3D.y) + y_range.max = pt3D->pt3D.y; + } + } + scan_lines.push_back(a_line); + } + + double x_scale = 1.0; + double y_scale = 1.0; + int x_rows = int((x_range.max - x_range.min) / x_scale); + int y_rows = int((y_range.max - y_range.min) / y_scale); + if (x_rows % 2 > 0) + x_rows += 1; + if (y_rows % 2 > 0) + y_rows += 1; + int imgRows = y_rows + 32; + int imgCols = x_rows + 32; + + cv::Mat img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); + //璁$畻鎶曞奖姣斾緥 + + _XOYprojection_sideBagInfo(img, scan_lines, objOps, x_scale, y_scale, x_range, y_range); + +#if 0 + //鏃嬭浆瑙嗚鏄剧ず + double matrix3d[9]; + EulerRpyToRotation1(rpy, matrix3d); + std::vector r_objOps; + r_objOps.insert(r_objOps.end(), objOps.begin(), objOps.end()); + for (int i = 0; i < objOps.size(); i++) + { + SVzNL3DPoint c_pt = { objOps[i].graspPos.x, objOps[i].graspPos.y, objOps[i].graspPos.z }; + SVzNL3DPoint r_c_pt = _ptRotate(c_pt, matrix3d); + r_objOps[i].graspPos.x = r_c_pt.x; + r_objOps[i].graspPos.y = r_c_pt.y; + r_objOps[i].graspPos.z = r_c_pt.z; + } + std::vector> rotateLines; + SVzNLRangeD rx_range, ry_range; + _rotateCloudPts(scanData, lineNum, matrix3d, rotateLines, &rx_range, &ry_range); + cv::Mat r_img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); + //璁$畻鎶曞奖姣斾緥 + double rx_scale = (rx_range.max - rx_range.min) / x_cols; + double ry_scale = (ry_range.max - ry_range.min) / y_rows; + _XOYprojection_sideBagInfo(r_img, rotateLines, r_objOps, rx_scale, ry_scale, rx_range, ry_range); + + cv::Mat dis_img; + cv::hconcat(img, r_img, dis_img); + cv::imwrite(fileName, dis_img); +#else + cv::Mat rot; + cv::rotate(img, rot, cv::ROTATE_90_CLOCKWISE); // 椤烘椂閽90掳鏃嬭浆 + cv::flip(rot, img, 1); // 宸﹀彸缈昏浆 + cv::imwrite(fileName, img); +#endif + return; +} + + +//閲忓寲鎴640*640宸﹀彸澶у皬鐨勫浘鍍忋 +void project2DWithInterpolate(cv::Mat & img, SVzNL3DLaserLine * scanData, int lineNum, double fixed_xy_scale, double z_scale) +{ + double namedSize = 640.0;//鐩爣鍥惧儚鏈澶у昂搴︿负640鍍忕礌 + + SVzNLRangeD x_range = { 0, -1 }; + SVzNLRangeD y_range = { 0, -1 }; + SVzNLRangeD z_range = { 0, -1 }; + for (int line = 0; line < lineNum; line++) + { + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; + if (pt3D->pt3D.z < 1e-4) + continue; + + if (x_range.max < x_range.min) + { + x_range.min = pt3D->pt3D.x; + x_range.max = pt3D->pt3D.x; + } + else + { + if (x_range.min > pt3D->pt3D.x) + x_range.min = pt3D->pt3D.x; + if (x_range.max < pt3D->pt3D.x) + x_range.max = pt3D->pt3D.x; + } + if (y_range.max < y_range.min) + { + y_range.min = pt3D->pt3D.y; + y_range.max = pt3D->pt3D.y; + } + else + { + if (y_range.min > pt3D->pt3D.y) + y_range.min = pt3D->pt3D.y; + if (y_range.max < pt3D->pt3D.y) + y_range.max = pt3D->pt3D.y; + } + if (z_range.max < z_range.min) + { + z_range.min = pt3D->pt3D.z; + z_range.max = pt3D->pt3D.z; + } + else + { + if (z_range.min > pt3D->pt3D.z) + z_range.min = pt3D->pt3D.z; + if (z_range.max < pt3D->pt3D.z) + z_range.max = pt3D->pt3D.z; + } + } + } + + double xy_scale = fixed_xy_scale; + if (fixed_xy_scale < 1e-4) + { + double x_scale = (x_range.max - x_range.min) / namedSize; + double y_scale = (y_range.max - y_range.min) / namedSize; + double xy_scale; + if (x_scale < y_scale) + xy_scale = y_scale; + else + xy_scale = x_scale; + } + int img_rows = (int)((y_range.max - y_range.min) / xy_scale) + 1; + if (img_rows % 2 > 0) + img_rows += 1; + int img_cols = (int)((x_range.max - x_range.min) / xy_scale) + 1; + if (img_cols % 2 > 0) + img_cols += 1; + img = cv::Mat::zeros(img_rows, img_cols, CV_8UC1); + + int polateWin = 5; + for (int line = 0; line < lineNum; line++) + { + //鍚屾椂杩涜鍨傜洿鎻掑 + int pre_py = -1; + SVzNL3DPosition* pre_pt3D = NULL; + int pre_i = -1; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; + if (pt3D->pt3D.z < 1e-4) + continue; + double x = pt3D->pt3D.x; + double y = pt3D->pt3D.y; + int px = (int)((x - x_range.min) / xy_scale); + int py = (int)((y - y_range.min) / xy_scale); + int value = (int)((pt3D->pt3D.z - z_range.min) / z_scale); + if (value > 254) + value = 254; + value = 255 - value; + img.at(py, px) = (uchar)value; + //妫鏌ユ槸鍚﹂渶瑕佹彃鍊 + if ((py > pre_py + 1) && (py <= pre_py + polateWin + 1) && (pre_py >= 0) && (pre_i >= 0) && (i == pre_i + 1)) //3D鐐硅繛缁紝閲忓寲鐐逛笉杩炵画锛屾彃鍊 + { + double dist = double(py - pre_py); + for (int iy = pre_py + 1; iy < py; iy++) + { + double k1 = (double)(iy - pre_py) / dist; + double k0 = 1 - k1; + double inter_x = pre_pt3D->pt3D.x * k0 + pt3D->pt3D.x * k1; + double inter_y = pre_pt3D->pt3D.y * k0 + pt3D->pt3D.y * k1; + double inter_z = pre_pt3D->pt3D.z * k0 + pt3D->pt3D.z * k1; + int polate_px = (int)((inter_x - x_range.min) / xy_scale); + int polate_py = (int)((inter_y - y_range.min) / xy_scale); + int polate_value = (int)((inter_z - z_range.min) / z_scale); + if (polate_value > 254) + polate_value = 254; + polate_value = 255 - polate_value; + img.at(polate_py, polate_px) = (uchar)polate_value; + } + } + pre_i = i; + pre_py = py; + pre_pt3D = pt3D; + } + } + //姘村钩鎻掑 + for (int y = 0; y < img.rows; y++) + { + int pre_x = -1; + uchar pre_value = 0; + for (int x = 0; x < img.cols; x++) + { + uchar value = img.at(y, x); + if (value > 0) + { + if ((x > pre_x + 1) && (x <= pre_x + polateWin + 1) && (pre_x >= 0)) //姘村钩涓嶈繛缁紝鎻掑 + { + double dist = double(x - pre_x); + for (int ix = pre_x + 1; ix < x; ix++) + { + double k1 = (double)(ix - pre_x) / dist; + double k0 = 1 - k1; + double inter_data = (double)pre_value * k0 + (double)value * k1; + if (inter_data > 255) + inter_data = 255; + uchar polate_value = (uchar)inter_data; + img.at(y, ix) = polate_value; + } + } + pre_x = x; + pre_value = value; + } + } + } +} +void _outputScanDataFile_removeZeros(char* fileName, SVzNL3DLaserLine * scanData, int lineNum, + float lineV, int maxTimeStamp, int clockPerSecond) +{ + std::ofstream sw(fileName); + sw << "LineNum:" << lineNum << std::endl; + sw << "DataType: 0" << std::endl; + sw << "ScanSpeed:" << lineV << std::endl; + sw << "PointAdjust: 1" << std::endl; + sw << "MaxTimeStamp:" << maxTimeStamp << "_" << clockPerSecond << std::endl; + + for (int line = 0; line < lineNum; line++) + { + int realNum = 0; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + if (scanData[line].p3DPosition[i].pt3D.z > 1e-4) + realNum++; + } + sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << realNum << std::endl; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + if (scanData[line].p3DPosition[i].pt3D.z > 1e-4) + { + SVzNL3DPosition* pt3D = &scanData[line].p3DPosition[i]; + float x = (float)pt3D->pt3D.x; + float y = (float)pt3D->pt3D.y; + float z = (float)pt3D->pt3D.z; + sw << "{ " << x << "," << y << "," << z << " }-"; + sw << "{0,0}-{0,0}" << std::endl; + } + } + } + sw.close(); +} + +#define TEST_CONVERT_TO_GRID 0 +#define TEST_COMPUTE_WHEEL_ARCH 1 +#define TEST_COMPUTE_CALIB_PARA 0 + +#define TEST_GROUP 1 +int main() +{ +#if TEST_CONVERT_TO_GRID + //灏嗘暟鎹浆鎹㈡垚鏍呮牸鏍煎紡鏍煎紡 + char _scan_dir[256]; + int lineNum = 0; + float lineV = 0.0f; + int dataCalib = 0; + int maxTimeStamp = 0; + int clockPerSecond = 0; + sprintf_s(_scan_dir, "F:/ShangGu/椤圭洰/鍐犻挦_杞湁楂樺害娴嬮噺/娴嬭瘯鏁版嵁/"); + char _scan_src_file[256]; + double _F = 1177.2; //f + for (int i = 1; i <= 1; i++) + { + //read ply txt + sprintf_s(_scan_src_file, "%sLaserLine%d.txt", _scan_dir, i); + std::vector> scanData; + vzReadPlyTxtPointFromFile_XYZRGB_vector(_scan_src_file, scanData); + sprintf_s(_scan_src_file, "%sscanData%d.txt", _scan_dir, i); + _outputScanDataFile_SPointXYZRGB_vector(_scan_src_file, scanData); + +#if 0 + double rpy[3] = { 0,-18, 0 }; + double camPoseR[9]; + EulerRpyToRotation1(rpy, camPoseR); +#else + double camPoseR[9] = { + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0 }; +#endif + std::vector> gridData; + _convertToGridData_XYZRGB(scanData, _F, gridData); + char _out_file[256]; + sprintf_s(_out_file, "%sLaserLine%d_grid.txt", _scan_dir, i); + _outputScanDataFile_self(_out_file, gridData, 0, 0, 0); + printf("%s: convert done!\n", _scan_src_file); + } +#endif + +#if TEST_COMPUTE_CALIB_PARA + char _calib_datafile[256]; + sprintf_s(_calib_datafile, "F:/ShangGu/椤圭洰/鍐犻挦_杞湁楂樺害娴嬮噺/娴嬭瘯鏁版嵁/LaserLine1_grid.txt"); + + std::vector< std::vector> scanLines; + vzReadLaserScanPointFromFile_XYZ_vector(_calib_datafile, scanLines); + if(scanLines.size() > 0) + { + SSG_planeCalibPara calibPara = wd_horizonCamera_getGroundCalibPara(scanLines); +#if 1 + //缁撴灉杩涜楠岃瘉 + int lineNum = (int)scanLines.size(); + for (int i = 0; i < lineNum; i++) + { + if (i == 14) + int kkk = 1; + //琛屽鐞 + //璋冨钩锛屽幓闄ゅ湴闈 + wd_horizonCamera_lineDataR(scanLines[i], calibPara.planeCalib, -1);// calibPara.planeHeight); + } +#endif + // + char calibFile[250]; + sprintf_s(calibFile, "F:/ShangGu/椤圭洰/鍐犻挦_杞湁楂樺害娴嬮噺/娴嬭瘯鏁版嵁/ground_calib_para.txt"); + _outputCalibPara(calibFile, calibPara); + char _out_file[256]; + sprintf_s(_out_file, "F:/ShangGu/椤圭洰/鍐犻挦_杞湁楂樺害娴嬮噺/娴嬭瘯鏁版嵁/LaserLine1_grid_calib.txt"); + _outputScanDataFile_self(_out_file, scanLines, 0, 0, 0); + printf("%s: calib done!\n", _calib_datafile); + } + +#endif + +#if TEST_COMPUTE_WHEEL_ARCH + const char* dataPath[TEST_GROUP] = { + "F:/ShangGu/椤圭洰/鍐犻挦_杞湁楂樺害娴嬮噺/娴嬭瘯鏁版嵁/", //0 + }; + + SVzNLRange fileIdx[TEST_GROUP] = { + {1,1}, + }; + + SSG_planeCalibPara poseCalibPara; + //鍒濆鍖栨垚鍗曚綅闃 + poseCalibPara.planeCalib[0] = 1.0; + poseCalibPara.planeCalib[1] = 0.0; + poseCalibPara.planeCalib[2] = 0.0; + poseCalibPara.planeCalib[3] = 0.0; + poseCalibPara.planeCalib[4] = 1.0; + poseCalibPara.planeCalib[5] = 0.0; + poseCalibPara.planeCalib[6] = 0.0; + poseCalibPara.planeCalib[7] = 0.0; + poseCalibPara.planeCalib[8] = 1.0; + poseCalibPara.planeHeight = -1.0; + for (int i = 0; i < 9; i++) + poseCalibPara.invRMatrix[i] = poseCalibPara.planeCalib[i]; + + const char* ver = wd_wheelArchHeigthMeasureVersion(); + printf("ver:%s\n", ver); + + char _scan_file[256]; + + int endGroup = TEST_GROUP - 1; + for (int grp = 0; grp <= endGroup; grp++) + { + char calibFile[250]; + sprintf_s(calibFile, "%sground_calib_para.txt", dataPath[grp]); + poseCalibPara = _readCalibPara(calibFile); + for (int fidx = fileIdx[grp].nMin; fidx <= fileIdx[grp].nMax; fidx++) + { + //fidx = 193; + sprintf_s(_scan_file, "%sLaserLine1_grid.txt", dataPath[grp], fidx); + std::vector> scanData; + vzReadLaserScanPointFromFile_XYZ_vector(_scan_file, scanData); + if (scanData.size() == 0) + continue; + + //algoParam.filterParam.continuityTh = 20.0; //鍣0婊ら櫎銆傚綋鐩搁偦鐐圭殑z璺冲彉澶т簬姝ら棬闄愭椂锛屾鏌ユ槸鍚︿负鍣0銆傝嫢闀垮害灏忎簬outlierLen锛 瑙嗕负鍣0 + //algoParam.filterParam.outlierTh = 5; + + long t1 = GetTickCount64(); + int lineNum = (int)scanData.size(); + for (int i = 0; i < lineNum; i++) + { + if (i == 14) + int kkk = 1; + //琛屽鐞 + //璋冨钩锛屽幓闄ゅ湴闈 + wd_horizonCamera_lineDataR(scanData[i], poseCalibPara.planeCalib, poseCalibPara.planeHeight); + } +#if 0 //鏁版嵁杞瓨 + sprintf_s(_scan_file, "%sLaserLine%d_grid_RTadjust.txt", dataPath[grp], fidx); + _outputScanDataFile_self(_scan_file, scanData, 0, 0, 0); +#endif + SSG_cornerParam cornerParam; + cornerParam.cornerTh = 60; //45搴﹁ + cornerParam.scale = 50; // algoParam.bagParam.bagH / 8; // 15; // algoParam.bagParam.bagH / 8; + cornerParam.minEndingGap = 20; // algoParam.bagParam.bagW / 4; + cornerParam.minEndingGap_z = 20; + cornerParam.jumpCornerTh_1 = 10; //姘村钩瑙掑害锛屽皬浜庢瑙掑害瑙嗕负姘村钩 + cornerParam.jumpCornerTh_2 = 60; + + SSG_lineSegParam lineSegPara; + lineSegPara.maxDist = 1.0; + lineSegPara.segGapTh_y = 5.0; //y鏂瑰悜闂撮殧澶т簬5mm璁や负鏄垎娈 + lineSegPara.segGapTh_z = 10.0; //z鏂瑰悜闂撮殧澶т簬10mm璁や负鏄垎娈 + + SSG_outlierFilterParam filterParam; + filterParam.continuityTh = 20.0; //鍣0婊ら櫎銆傚綋鐩搁偦鐐圭殑z璺冲彉澶т簬姝ら棬闄愭椂锛屾鏌ユ槸鍚︿负鍣0銆傝嫢闀垮害灏忎簬outlierLen锛 瑙嗕负鍣0 + filterParam.outlierTh = 5; + + SSG_treeGrowParam growParam; + growParam.maxLineSkipNum = 10; + growParam.yDeviation_max = 5.0; + growParam.maxSkipDistance = 5.0; + growParam.zDeviation_max = 2.0;// + growParam.minLTypeTreeLen = 100; //mm + growParam.minVTypeTreeLen = 100; //mm + + int errCode = 0; + WD_wheelArchInfo wheelArcHeight = wd_wheelArchHeigthMeasure( + scanData, + cornerParam, + lineSegPara, + filterParam, + growParam, + poseCalibPara, + &errCode); +#endif + long t2 = GetTickCount64(); + + char _dbg_file[256]; +#if 1 + + sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result.txt", dataPath[grp], fidx); + _outputScanDataFile_RGBD_obj(_dbg_file, scanData, 0, 0, 0, wheelArcHeight); +#endif + printf("%s: height=%f, %d(ms)!\n", _scan_file, wheelArcHeight.archToCenterHeigth, (int)(t2 - t1)); + + + } + } + printf("all done!\n"); +} + + +// 杩愯绋嬪簭: Ctrl + F5 鎴栬皟璇 >鈥滃紑濮嬫墽琛(涓嶈皟璇)鈥濊彍鍗 +// 璋冭瘯绋嬪簭: F5 鎴栬皟璇 >鈥滃紑濮嬭皟璇曗濊彍鍗 + +// 鍏ラ棬浣跨敤鎶宸: +// 1. 浣跨敤瑙e喅鏂规璧勬簮绠$悊鍣ㄧ獥鍙f坊鍔/绠$悊鏂囦欢 +// 2. 浣跨敤鍥㈤槦璧勬簮绠$悊鍣ㄧ獥鍙h繛鎺ュ埌婧愪唬鐮佺鐞 +// 3. 浣跨敤杈撳嚭绐楀彛鏌ョ湅鐢熸垚杈撳嚭鍜屽叾浠栨秷鎭 +// 4. 浣跨敤閿欒鍒楄〃绐楀彛鏌ョ湅閿欒 +// 5. 杞埌鈥滈」鐩>鈥滄坊鍔犳柊椤光濅互鍒涘缓鏂扮殑浠g爜鏂囦欢锛屾垨杞埌鈥滈」鐩>鈥滄坊鍔犵幇鏈夐」鈥濅互灏嗙幇鏈変唬鐮佹枃浠舵坊鍔犲埌椤圭洰 +// 6. 灏嗘潵锛岃嫢瑕佸啀娆℃墦寮姝ら」鐩紝璇疯浆鍒扳滄枃浠垛>鈥滄墦寮鈥>鈥滈」鐩濆苟閫夋嫨 .sln 鏂囦欢 diff --git a/wheelArchHeigthMeasure_test/wheelArchHeigthMeasure_test.vcxproj b/wheelArchHeigthMeasure_test/wheelArchHeigthMeasure_test.vcxproj new file mode 100644 index 0000000..fcd689d --- /dev/null +++ b/wheelArchHeigthMeasure_test/wheelArchHeigthMeasure_test.vcxproj @@ -0,0 +1,157 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {bbf5341e-8447-45e9-ada3-3e8a9b52f5ef} + wheelArchHeigthMeasuretest + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + $(SolutionDir)build\$(Platform)\$(Configuration)\ + ..\..\thirdParty\VzNLSDK\Inc;..\sourceCode;..\sourceCode\inc;$(IncludePath) + + + false + $(SolutionDir)build\$(Platform)\$(Configuration)\ + ..\..\thirdParty\VzNLSDK\Inc;..\sourceCode;..\sourceCode\inc;$(IncludePath) + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\thirdParty\opencv320\build\include; + + + Console + true + ..\..\thirdParty\opencv320\build\x64\vc14\lib;..\build\x64\Debug;%(AdditionalLibraryDirectories) + opencv_world320d.lib;wheelArchHeigthMeasure.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + _CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\thirdParty\opencv320\build\include; + + + Console + true + true + true + ..\..\thirdParty\opencv320\build\x64\vc14\lib;..\build\x64\Release;%(AdditionalLibraryDirectories) + opencv_world320.lib;wheelArchHeigthMeasure.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/wheelArchHeigthMeaure/wheelArchHeigthMeaure.vcxproj b/wheelArchHeigthMeaure/wheelArchHeigthMeaure.vcxproj new file mode 100644 index 0000000..77a77f5 --- /dev/null +++ b/wheelArchHeigthMeaure/wheelArchHeigthMeaure.vcxproj @@ -0,0 +1,174 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 16.0 + Win32Proj + {cfe11556-106a-4216-bf62-fda980528f7a} + wheelArchHeigthMeaure + 10.0 + wheelArchHeigthMeasure + + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + $(SolutionDir)build\$(Platform)\$(Configuration)\ + ..\..\thirdParty\VzNLSDK\Inc;..\..\thirdParty\opencv320\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + + + false + $(SolutionDir)build\$(Platform)\$(Configuration)\ + ..\..\thirdParty\VzNLSDK\Inc;..\..\thirdParty\opencv320\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + + + + Level3 + true + WIN32;_DEBUG;WHEELARCHHEIGTHMEAURE_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;WHEELARCHHEIGTHMEAURE_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + _CRT_SECURE_NO_WARNINGS;_DEBUG;WHEELARCHHEIGTHMEAURE_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\thirdParty\opencv320\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + false + ..\..\thirdParty\opencv320\build\x64\vc14\lib;..\build\x64\Debug;%(AdditionalLibraryDirectories) + true + opencv_world320d.lib;baseAlgorithm.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + _CRT_SECURE_NO_WARNINGS;NDEBUG;WHEELARCHHEIGTHMEAURE_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\thirdParty\opencv320\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + false + ..\..\thirdParty\opencv320\build\x64\vc14\lib;..\build\x64\Release;%(AdditionalLibraryDirectories) + opencv_world320.lib;baseAlgorithm.lib;%(AdditionalDependencies) + + + + + + \ No newline at end of file