diff --git a/SG_Algorithm.sln b/SG_Algorithm.sln index f8405b9..cffc042 100644 --- a/SG_Algorithm.sln +++ b/SG_Algorithm.sln @@ -205,6 +205,17 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bagThreadPositioning_test", {F371FCBC-0AD6-4546-8785-1C05CD0C5B57} = {F371FCBC-0AD6-4546-8785-1C05CD0C5B57} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "workpieceHolePositioning_test", "workpieceHolePositioning_test\workpieceHolePositioning_test.vcxproj", "{CA8CBA7F-4D72-4630-AF6D-BF872306502C}" + ProjectSection(ProjectDependencies) = postProject + {95DC3F1A-902A-490E-BD3B-B10463CF0EBD} = {95DC3F1A-902A-490E-BD3B-B10463CF0EBD} + {C65F7F4B-B77A-4D65-9490-5304B760B607} = {C65F7F4B-B77A-4D65-9490-5304B760B607} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "workpieceHolePositioning", "workpieceHolePositioning\workpieceHolePositioning.vcxproj", "{C65F7F4B-B77A-4D65-9490-5304B760B607}" + ProjectSection(ProjectDependencies) = postProject + {95DC3F1A-902A-490E-BD3B-B10463CF0EBD} = {95DC3F1A-902A-490E-BD3B-B10463CF0EBD} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -525,6 +536,22 @@ Global {831F7635-98DB-45BB-9815-5334DA70BFE8}.Release|x64.Build.0 = Release|x64 {831F7635-98DB-45BB-9815-5334DA70BFE8}.Release|x86.ActiveCfg = Release|Win32 {831F7635-98DB-45BB-9815-5334DA70BFE8}.Release|x86.Build.0 = Release|Win32 + {CA8CBA7F-4D72-4630-AF6D-BF872306502C}.Debug|x64.ActiveCfg = Debug|x64 + {CA8CBA7F-4D72-4630-AF6D-BF872306502C}.Debug|x64.Build.0 = Debug|x64 + {CA8CBA7F-4D72-4630-AF6D-BF872306502C}.Debug|x86.ActiveCfg = Debug|Win32 + {CA8CBA7F-4D72-4630-AF6D-BF872306502C}.Debug|x86.Build.0 = Debug|Win32 + {CA8CBA7F-4D72-4630-AF6D-BF872306502C}.Release|x64.ActiveCfg = Release|x64 + {CA8CBA7F-4D72-4630-AF6D-BF872306502C}.Release|x64.Build.0 = Release|x64 + {CA8CBA7F-4D72-4630-AF6D-BF872306502C}.Release|x86.ActiveCfg = Release|Win32 + {CA8CBA7F-4D72-4630-AF6D-BF872306502C}.Release|x86.Build.0 = Release|Win32 + {C65F7F4B-B77A-4D65-9490-5304B760B607}.Debug|x64.ActiveCfg = Debug|x64 + {C65F7F4B-B77A-4D65-9490-5304B760B607}.Debug|x64.Build.0 = Debug|x64 + {C65F7F4B-B77A-4D65-9490-5304B760B607}.Debug|x86.ActiveCfg = Debug|Win32 + {C65F7F4B-B77A-4D65-9490-5304B760B607}.Debug|x86.Build.0 = Debug|Win32 + {C65F7F4B-B77A-4D65-9490-5304B760B607}.Release|x64.ActiveCfg = Release|x64 + {C65F7F4B-B77A-4D65-9490-5304B760B607}.Release|x64.Build.0 = Release|x64 + {C65F7F4B-B77A-4D65-9490-5304B760B607}.Release|x86.ActiveCfg = Release|Win32 + {C65F7F4B-B77A-4D65-9490-5304B760B607}.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 885db6b..c698ca8 100644 --- a/sourceCode/SG_baseAlgo_Export.h +++ b/sourceCode/SG_baseAlgo_Export.h @@ -201,6 +201,16 @@ SG_APISHARED_EXPORT void wd_surfaceLineSegment( std::vector& lineSegs, std::vector& invlaidLineSegs); +#if 0 +//对一个面的边缘特征进行提取。 +//此算法首先获取扫描线上各个段,然后对段的端点进行处理,得到边缘特征 +void wd_getSurfaceEdgeFeatures( + std::vector< SVzNL3DPosition>& lineData, + int lineIdx, + const SSG_lineSegParam lineSegPara, + std::vector& edgeFeatures); +#endif + /// /// 提取激光线上的Jumping特征 /// nPointIdx被重新定义成Feature类型 @@ -521,6 +531,9 @@ SG_APISHARED_EXPORT double getLineAngle(const double _a, const double _b, const //计算两点的2D距离 SG_APISHARED_EXPORT double compute2DLen(SVzNL3DPoint pt1, SVzNL3DPoint pt2); +//计算XY平面面的三角形顶角(p0的张角) +SG_APISHARED_EXPORT double computeXOYVertexAngle(SVzNL3DPoint p0, SVzNL3DPoint p1, SVzNL3DPoint p2); + //计算点到直线距离 SG_APISHARED_EXPORT double computePtDistToLine(double x0, double y0, double a, double b, double c); diff --git a/sourceCode/SG_baseDataType.h b/sourceCode/SG_baseDataType.h index 553f012..9f15391 100644 --- a/sourceCode/SG_baseDataType.h +++ b/sourceCode/SG_baseDataType.h @@ -25,6 +25,20 @@ typedef struct double yawAngle; //偏转角:绕Z轴的偏转, 弧度 }SSG_6AxisAttitude; +typedef struct +{ + double x; + double y; + double z; +}SWD3DPoint; + +typedef struct +{ + int lineIdx; + int ptIdx; + SWD3DPoint point; +}SWDIndexing3DPoint; + typedef struct { bool validFlag; //指示结果是否有效 @@ -67,6 +81,12 @@ typedef struct double bottom; }SSG_ROIRectD; +typedef struct +{ + SVzNL3DPoint center; + double radius; +}SWD_HoleInfo; + struct HSV { double h; // 色相 (0-360) double s; // 饱和度 (0-1) @@ -172,7 +192,7 @@ typedef struct { double segGapTh_y; //y方向连续段门限。大于此门限,为不连续 double segGapTh_z; //z方向连续段门限。大于此门限,为不连续 - double maxDist; //计算方向角的窗口比例尺 + double distScale; //计算方向角的窗口比例尺 }SSG_lineSegParam; typedef struct diff --git a/sourceCode/SG_baseFunc.cpp b/sourceCode/SG_baseFunc.cpp index 312af44..f9af922 100644 --- a/sourceCode/SG_baseFunc.cpp +++ b/sourceCode/SG_baseFunc.cpp @@ -639,6 +639,20 @@ double compute2DLen(SVzNL3DPoint pt1, SVzNL3DPoint pt2) return len; } +//计算XY平面面的三角形顶角(p0的张角) +double computeXOYVertexAngle(SVzNL3DPoint p0, SVzNL3DPoint p1, SVzNL3DPoint p2) +{ + double len_c = compute2DLen(p1, p2); + double len_a = compute2DLen(p0, p1); + double len_b = compute2DLen(p0, p2); + double cosAngle = (pow(len_a, 2) + pow(len_b, 2) - pow(len_c, 2)) / (2 * len_a * len_b); + double angle = acos(cosAngle); + angle = angle * 180 / M_PI; + if (angle < 0) + angle = angle + 180; + return angle; +} + double computePtDistToLine(double x0, double y0, double a, double b, double c) { double tmp = sqrt(pow(a, 2) + pow(b, 2)); @@ -2287,7 +2301,7 @@ SSG_planeCalibPara sg_HCameraVScan_getGroundCalibPara( //提取地面直线段 SSG_lineSegParam lineSegPara; - lineSegPara.maxDist = 2.0; + lineSegPara.distScale = 2.0; lineSegPara.segGapTh_y = 5.0; //y方向间隔大于5mm认为是分段 lineSegPara.segGapTh_z = 10.0; //z方向间隔大于10mm认为是分段 @@ -2307,7 +2321,7 @@ SSG_planeCalibPara sg_HCameraVScan_getGroundCalibPara( SSG_RUN lastSeg = segs.back(); //直线分割 std::vector< SSG_RUN> segmentationLines; - split(lastSeg, lineData, lineSegPara.maxDist, segmentationLines); + split(lastSeg, lineData, lineSegPara.distScale, segmentationLines); //检查最后一段的直线段的斜率 SSG_RUN lastLine = segmentationLines.back(); //计算斜率 diff --git a/sourceCode/SG_clustering.cpp b/sourceCode/SG_clustering.cpp index 78fd39c..8da9b91 100644 --- a/sourceCode/SG_clustering.cpp +++ b/sourceCode/SG_clustering.cpp @@ -80,6 +80,7 @@ void wd_gridPointClustering( int i = 0; int lineNum = (int)featureMask.size(); int linePtNum = (int)featureMask[0].size(); + featureMask[a_cluster[0].x][a_cluster[0].y].flag = 1; //防止第一个被重复添加 while (1) { if (i >= a_cluster.size()) diff --git a/sourceCode/SG_lineFeature.cpp b/sourceCode/SG_lineFeature.cpp index 8acbabe..12e4182 100644 --- a/sourceCode/SG_lineFeature.cpp +++ b/sourceCode/SG_lineFeature.cpp @@ -4270,6 +4270,238 @@ void wd_getLineCorerFeature_accelerate( return; } +SSG_pntDirAngle _computeCornerAngle(int ptIdx, int startIdx, int endIdx, double scale, std::vector< SVzNL3DPosition>& lineData) +{ + SSG_pntDirAngle a_corner; + memset(&a_corner, 0, sizeof(SSG_pntDirAngle)); + if (lineData[ptIdx].pt3D.z < 1e-4) + { + a_corner.pntIdx = -1; + a_corner.forwardAngle = 0; + a_corner.backwardAngle = 0; + a_corner.corner = 0; + a_corner.forwardDiffZ = 0; + a_corner.backwardDiffZ = 0; + return a_corner; + } + //前向寻找 + int pre_i = -1; + for (int j = ptIdx-1; j >= startIdx; j--) + { + if (lineData[j].pt3D.z > 1e-4) + { + double dist = sqrt(pow(lineData[ptIdx].pt3D.y - lineData[j].pt3D.y, 2) + + pow(lineData[ptIdx].pt3D.z - lineData[j].pt3D.z, 2)); + if (dist >= scale) + break; + else + pre_i = j; + } + } + //后向寻找 + int post_i = -1; + for (int j = ptIdx+1; j < endIdx; j++) + { + if (lineData[j].pt3D.z > 1e-4) + { + double dist = sqrt(pow(lineData[ptIdx].pt3D.y - lineData[j].pt3D.y, 2) + + pow(lineData[ptIdx].pt3D.z - lineData[j].pt3D.z, 2)); + if (dist >= scale) + break; + else + post_i = j; + } + } + //计算拐角 + if ((pre_i < 0) || (post_i < 0)) + { + a_corner.pntIdx = -1; + a_corner.forwardAngle = 0; + a_corner.backwardAngle = 0; + a_corner.corner = 0; + a_corner.forwardDiffZ = 0; + a_corner.backwardDiffZ = 0; + } + else + { + double tanValue_pre = (lineData[ptIdx].pt3D.z - lineData[pre_i].pt3D.z) / abs(lineData[ptIdx].pt3D.y - lineData[pre_i].pt3D.y); + double tanValue_post = (lineData[post_i].pt3D.z - lineData[ptIdx].pt3D.z) / abs(lineData[post_i].pt3D.y - lineData[ptIdx].pt3D.y); + double forwardAngle = atan(tanValue_post) * 180.0 / PI; + double backwardAngle = atan(tanValue_pre) * 180.0 / PI; + a_corner.pntIdx = ptIdx; + a_corner.forwardAngle = forwardAngle; + a_corner.backwardAngle = backwardAngle; + a_corner.corner = -(forwardAngle - backwardAngle); //图像坐标系与正常坐标系y方向相反,所以有“-”号 + a_corner.forwardDiffZ = lineData[post_i].pt3D.z - lineData[ptIdx].pt3D.z; + a_corner.backwardDiffZ = lineData[ptIdx].pt3D.z - lineData[pre_i].pt3D.z; + } + return a_corner; +} + +#if 0 +//对一个面的边缘特征进行提取。 +//此算法首先获取扫描线上各个段,然后对段的端点进行处理,得到边缘特征 +void wd_getSurfaceEdgeFeatures( + std::vector< SVzNL3DPosition>& lineData, + int lineIdx, + const SSG_lineSegParam lineSegPara, + const SSG_cornerParam cornerPara, + double edgeAngleTh, //边缘的判断角度。当一个端点的前向角或后向角大于此门限,将拐点最大点视为边缘,反之,端点是边缘点 + std::vector& edgeFeatures) +{ + int dataSize = (int)lineData.size(); + //去除零点 + std::vector segs; + wd_getLineDataIntervals(lineData, lineSegPara, segs); + + //逐段处理,对端点进行处理 + int segSize = (int)segs.size(); + for (int si = 0; si < segSize; si++) + { + int endingIdx_s = segs[si].start; + int endingIdx_e = segs[si].start + segs[si].len - 1; + //后向寻找 + int post_i = -1; + for (int j = endingIdx_s + 1; j <= endingIdx_e; j++) + { + if (lineData[j].pt3D.z > 1e-4) + { + double dist = sqrt(pow(lineData[endingIdx_s].pt3D.y - lineData[j].pt3D.y, 2) + + pow(lineData[endingIdx_s].pt3D.z - lineData[j].pt3D.z, 2)); + if (dist >= lineSegPara.distScale) + { + post_i = j; + break; + } + } + } + double tanValue_post = (lineData[post_i].pt3D.z - lineData[endingIdx_s].pt3D.z) / abs(lineData[post_i].pt3D.y - lineData[endingIdx_s].pt3D.y); + double forwardAngle = atan(tanValue_post) * 180.0 / PI; + if (forwardAngle > edgeAngleTh) + { + //需要寻找拐点,作为端点 + std::vector corners; + for (int j = endingIdx_s + 1; j < endingIdx_e; j++) + { + SSG_pntDirAngle a_corner = _computeCornerAngle(j, endingIdx_s, endingIdx_e, lineSegPara.distScale, lineData); + corners.push_back(a_corner); + double cornerMergeScale = cornerPara.scale * 2; + std::vector< SSG_pntDirAngle> cornerPeakP; + std::vector< SSG_pntDirAngle> cornerPeakM; + _searchCornerPeaks( + corners, + lineData, + cornerPara, + cornerMergeScale, + cornerPeakP, + cornerPeakM + ); + //取cornerPeakM的第一个极值点 + + + } + } + else + { + + } + + + + + std::vector< SSG_RUN> segmentationLines; + split(segs[si], lineData, lineSegPara.maxDist, segmentationLines); + + //对于每个分段,只能有一个合格的直线段 + std::vector< SSG_featureSemiCircle> candiLines; + for (int m = 0, m_max = (int)segmentationLines.size(); m < m_max; m++) + { + SSG_featureSemiCircle a_seg; + a_seg.lineIdx = lineIdx; + a_seg.startPtIdx = segmentationLines[m].start; + a_seg.endPtIdx = segmentationLines[m].start + segmentationLines[m].len - 1; + + SVzNL3DPoint ptStart = lineData[a_seg.startPtIdx].pt3D; + SVzNL3DPoint ptEnd = lineData[a_seg.endPtIdx].pt3D; + double len = sqrt(pow(ptStart.x - ptEnd.x, 2) + pow(ptStart.y - ptEnd.y, 2) + pow(ptStart.z - ptEnd.z, 2)); + if ((len >= lineLenRange.min) && (len <= lineLenRange.max)) + { + int midPtIdx = (a_seg.startPtIdx + a_seg.endPtIdx) / 2; + bool validMid = true; + if (lineData[midPtIdx].pt3D.z < 1e-4) + { + validMid = false; + int chkWin = 1; + while (1) + { + int idx = midPtIdx + chkWin; + if (idx >= a_seg.endPtIdx) + break; + if (lineData[idx].pt3D.z > 1e-4) + { + midPtIdx = idx; + validMid = true; + break; + } + idx = midPtIdx - chkWin; + if (idx <= a_seg.startPtIdx) + break; + if (lineData[idx].pt3D.z > 1e-4) + { + midPtIdx = idx; + validMid = true; + break; + } + chkWin++; + } + } + if (true == validMid) + { + a_seg.midPtIdx = midPtIdx; + a_seg.midPt = lineData[midPtIdx].pt3D; + candiLines.push_back(a_seg); + } + } + } + //转成SSG_featureSemiCircle格式 + if (candiLines.size() > 0) + { + //选择一个水平角度更小的 + double minTan = 0; + int bestIdx = -1; + for (int m = 0, m_max = (int)candiLines.size(); m < m_max; m++) + { + SSG_featureSemiCircle& a_seg = candiLines[m]; + SVzNL3DPoint ptStart = lineData[a_seg.startPtIdx].pt3D; + SVzNL3DPoint ptEnd = lineData[a_seg.endPtIdx].pt3D; + double tank = abs((ptEnd.z - ptStart.z) / (ptEnd.y - ptStart.y)); + if (bestIdx < 0) + { + minTan = tank; + bestIdx = m; + } + else + { + if (minTan > tank) + { + minTan = tank; + bestIdx = m; + } + } + } + lineSegs.push_back(candiLines[bestIdx]); + for (int m = 0, m_max = (int)candiLines.size(); m < m_max; m++) + { + if (m != bestIdx) + invlaidLineSegs.push_back(candiLines[m]); + } + } + else if (candiLines.size() == 1) + lineSegs.push_back(candiLines[0]); + } +} +#endif + //提取凸起段 void wd_getLineRaisedFeature( std::vector< SVzNL3DPosition>& lineData, @@ -4417,7 +4649,7 @@ void wd_surfaceLineSegment( for (int si = 0; si < segSize; si++) { std::vector< SSG_RUN> segmentationLines; - split(segs[si], lineData, lineSegPara.maxDist, segmentationLines); + split(segs[si], lineData, lineSegPara.distScale, segmentationLines); //对于每个分段,只能有一个合格的直线段 std::vector< SSG_featureSemiCircle> candiLines; diff --git a/sourceCode/workpieceHolePositioning.cpp b/sourceCode/workpieceHolePositioning.cpp new file mode 100644 index 0000000..50170a8 --- /dev/null +++ b/sourceCode/workpieceHolePositioning.cpp @@ -0,0 +1,445 @@ +#include +#include "SG_baseDataType.h" +#include "SG_baseAlgo_Export.h" +#include "workpieceHolePositioning_Export.h" +#include +#include + +//version 1.0.0 : base version release to customer +std::string m_strVersion = "1.0.0"; +const char* wd_workpieceHolePositioningVersion(void) +{ + return m_strVersion.c_str(); +} + +//相机水平安装计算地面调平参数。 +//相机Z轴基本平行地面时,需要以地面为参照,将相机调水平 +//旋转矩阵为调平参数,即将平面法向调整为垂直向量的参数 +SSG_planeCalibPara wd_getGroundCalibPara( + std::vector< std::vector>& scanLines) +{ + return sg_getPlaneCalibPara2(scanLines); +} + +//相机水平时姿态调平,并去除地面 +void wd_lineDataR( + std::vector< SVzNL3DPosition>& a_line, + const double* camPoseR, + double groundH) +{ + 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; +} + +//搜索最接近distance的目标 +int distanceSearchObject(SVzNL3DPoint seed, std::vector& holes, double distance, double distDeviation) +{ + int result = -1; + int holeSize = (int)holes.size(); + double minDistDiff = DBL_MAX; + int minDistIndex = -1; + for (int i = 0; i < holeSize; i++) + { + if (holes[i].radius < 0) + continue; + + double dist = sqrt(pow(seed.x - holes[i].center.x, 2) + pow(seed.y - holes[i].center.y, 2)); + double distDiff = abs(dist - distance); + if (minDistDiff > distDiff) + { + minDistDiff = distDiff; + minDistIndex = i; + } + } + if ((minDistIndex >= 0) && (minDistDiff < distDeviation)) + result = minDistIndex; + return result; +} + +//搜索最接近distance且角度为angle的目标, 以角度为优先 +int angleConditionDistanceSearch( + SVzNL3DPoint seed, SVzNL3DPoint angleSide, + std::vector& holes, + double distance, double distDeviation, + SVzNLRangeD angleRange) +{ + int result = -1; + int holeSize = (int)holes.size(); + std::vector< int> distValidHoleIndex; + for (int i = 0; i < holeSize; i++) + { + if (holes[i].radius < 0) + continue; + + double dist = sqrt(pow(seed.x - holes[i].center.x, 2) + pow(seed.y - holes[i].center.y, 2)); + double distDiff = abs(dist - distance); + if (distDiff < distDeviation) + { + distValidHoleIndex.push_back(i); + } + } + if (distValidHoleIndex.size() == 1) + { + int idx = distValidHoleIndex[0]; + double angle = computeXOYVertexAngle(seed, angleSide, holes[idx].center); + if( (angle >= angleRange.min) &&(angle <= angleRange.max)) + result = idx; + } + else if (distValidHoleIndex.size() > 1) + { + double bestAngle = (angleRange.min + angleRange.max) / 2; + double minAngleDeviateion = DBL_MAX; + int minAngleIdx = -1; + for (int i = 0, i_max = (int)distValidHoleIndex.size(); i < i_max; i++) + { + int idx = distValidHoleIndex[i]; + double angle = computeXOYVertexAngle(seed, angleSide, holes[idx].center); + if ((angle >= angleRange.min) && (angle <= angleRange.max)) + { + double angleDiff = abs(angle - bestAngle); + if (minAngleDeviateion > angleDiff) + { + minAngleDeviateion = angleDiff; + minAngleIdx = idx; + } + } + } + result = minAngleIdx; + } + return result; +} + +//工件孔定位 +void wd_workpieceHolePositioning( + std::vector< std::vector>& scanLinesInput, + const WD_workpieceHoleParam workpiecePara, + const SSG_lineSegParam lineSegPara, + const SSG_outlierFilterParam filterParam, + const SSG_treeGrowParam growParam, + const SSG_planeCalibPara groundCalibPara, + std::vector< WD_workpieceInfo>& workpiecePositioning, + int* errCode) +{ + *errCode = 0; + + int lineNum = (int)scanLinesInput.size(); + std::vector< std::vector> scanLines; + scanLines.resize(lineNum); + int linePtNum = (int)scanLinesInput[0].size(); + bool isGridData = true; + for (int i = 0; i < lineNum; i++) + { + if (linePtNum != (int)scanLinesInput[i].size()) + isGridData = false; + + scanLines[i].resize(scanLinesInput[i].size()); + std::copy(scanLinesInput[i].begin(), scanLinesInput[i].end(), scanLines[i].begin()); // 使用std::copy算法 + } + if (false == isGridData)//数据不是网格格式 + { + *errCode = SG_ERR_NOT_GRID_FORMAT; + return; + } + for (int i = 0; i < lineNum; i++) + { //行处理 + //调平,去除地面 + wd_lineDataR(scanLines[i], groundCalibPara.planeCalib, -1); + } + + //生成量化数据,以1mm为量化尺度,用于确定工件表面高度 + + + + std::vector> pointMask; + pointMask.resize(lineNum); + + std::vector endingPoints; + //提取线段端点特征 + for (int line = 0; line < lineNum; line++) + { + if (line == 1677) + int kkk = 1; + + std::vector& lineData = scanLines[line]; + pointMask[line].resize(lineData.size()); + std::fill(pointMask[line].begin(), pointMask[line].end(), 0);//初始化为0 + //滤波,滤除异常点 + sg_lineDataRemoveOutlier_changeOriginData(&lineData[0], linePtNum, filterParam); + + std::vector segs; + wd_getLineDataIntervals( + lineData, + lineSegPara, + segs); + //将seg端点作为边缘点。做了地面调平后,垂直孔的内侧在XY平面上均为边缘点。 + for (int i = 0, i_max = (int)segs.size(); i < i_max; i++) + { + int ptIdx = segs[i].start; + endingPoints.push_back(lineData[ptIdx].pt3D); + pointMask[line][ptIdx] = 1; //防止重复 + ptIdx = segs[i].start + segs[i].len - 1; + endingPoints.push_back(lineData[ptIdx].pt3D); + pointMask[line][ptIdx] = 1; + } + } + + //生成水平扫描 + 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特征提取 + int lineNum_h_raw = (int)hLines_raw.size(); + for (int line = 0; line < lineNum_h_raw; 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 segs; + wd_getLineDataIntervals( + lineData, + lineSegPara, + segs); + //将seg端点作为边缘点。做了地面调平后,垂直孔的内侧在XY平面上均为边缘点。 + for (int i = 0, i_max = (int)segs.size(); i < i_max; i++) + { + int ptIdx = segs[i].start; + if (pointMask[ptIdx][line] == 0) //防止点重复 + { + SVzNL3DPoint an_ending; + an_ending.x = lineData[ptIdx].pt3D.y; + an_ending.y = lineData[ptIdx].pt3D.x; + an_ending.z = lineData[ptIdx].pt3D.z; + endingPoints.push_back(an_ending); + pointMask[ptIdx][line] = 1; + } + ptIdx = segs[i].start + segs[i].len - 1; + if (pointMask[ptIdx][line] == 0) //防止点重复 + { + SVzNL3DPoint an_ending; + an_ending.x = lineData[ptIdx].pt3D.y; + an_ending.y = lineData[ptIdx].pt3D.x; + an_ending.z = lineData[ptIdx].pt3D.z; + endingPoints.push_back(an_ending); + pointMask[ptIdx][line] = 1; + } + } + } + + //标注 + std::vector> featureInfoMask; + std::vector> feature3DInfo; + featureInfoMask.resize(lineNum); + feature3DInfo.resize(lineNum); + for (int i = 0; i < lineNum; i++) + { + featureInfoMask[i].resize(lineNum_h_raw); + feature3DInfo[i].resize(lineNum_h_raw); + } + + //标注 + for (int line = 0; line < lineNum; line++) + { + std::vector& a_lineMask = pointMask[line]; + for (int m = 0; m < lineNum_h_raw; m++) + { + if (a_lineMask[m] > 0) + { + SSG_featureClusteringInfo& a_featureInfo = featureInfoMask[line][m]; + a_featureInfo.clusterID = 0; + a_featureInfo.featurType = 1; + a_featureInfo.featureIdx_v = 0; + a_featureInfo.featureIdx_h = 0; + a_featureInfo.lineIdx = line; + a_featureInfo.ptIdx = m; + a_featureInfo.flag = 0; + feature3DInfo[line][m] = scanLines[line][m].pt3D; + } + } + } + //聚类 + //采用迭代思想,回归思路进行高效聚类 + std::vector> clusters; //只记录位置 + std::vector clustersRoi3D; + int clusterID = 1; + int clusterCheckWin = 5; + for (int y = 0; y < lineNum_h_raw; 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_gridPointClustering( + featureInfoMask,//int,记录特征标记和clusterID,附加一个flag + feature3DInfo,//double,记录坐标信息 + clusterCheckWin, //搜索窗口 + growParam,//聚类条件 + clusterID, //当前Cluster的ID + a_cluster, //result + a_clusterRoi + ); + clusters.push_back(a_cluster); + clustersRoi3D.push_back(a_clusterRoi); + clusterID++; + } + } + //聚类结果分析 + std::vector validCluserIndexing; + int clusterSize = (int)clusters.size(); + for (int i = 0; i < clusterSize; i++) + { + SVzNL3DRangeD& a_roi = clustersRoi3D[i]; + double L = a_roi.xRange.max - a_roi.xRange.min; + double W = a_roi.yRange.max - a_roi.yRange.min; + if ((L > workpiecePara.holeDiameter * 0.5) && (L < workpiecePara.holeDiameter * 2) && + (W > workpiecePara.holeDiameter * 0.5) && (W < workpiecePara.holeDiameter * 2)) + validCluserIndexing.push_back(i); + } + //生成结果 + std::vector< SWD_HoleInfo> holes; + int objectSize = (int)validCluserIndexing.size(); + for (int objIdx = 0; objIdx < objectSize; objIdx++) + { + std::vector pointArray; + int clusterIdx = validCluserIndexing[objIdx]; + //取cluster上的点 + int clusterPtSize = (int)clusters[clusterIdx].size(); + double minZ = DBL_MAX; + for (int i = 0; i < clusterPtSize; i++) + { + SVzNL2DPoint a_pos = clusters[clusterIdx][i]; + SSG_featureClusteringInfo& a_featureInfo = featureInfoMask[a_pos.x][a_pos.y]; + int lineIdx = a_featureInfo.lineIdx; + int ptIdx = a_featureInfo.ptIdx; + SVzNL3DPoint a_pt3d = scanLines[lineIdx][ptIdx].pt3D; + if (minZ > a_pt3d.z) + minZ = a_pt3d.z; + pointArray.push_back(a_pt3d); + } + //圆拟合 + SVzNL3DPoint center; + double radius; + double err = fitCircleByLeastSquare(pointArray, center, radius); + center.z = minZ; + SWD_HoleInfo a_hole; + a_hole.center = { center.x, center.y, center.z }; + a_hole.radius = radius; + holes.push_back(a_hole); + } + //分割 + //方法:先搜索与W最接近的点,然后条件搜索(垂直)与L最接近的点 + double distDeviation = 5.0; //距离搜索的合格门限。小于此距离,认为搜索到的目标为有效 + for (int objIdx = 0; objIdx < objectSize; objIdx++) + { + if (holes[objIdx].radius < 0) + continue; + + holes[objIdx].radius = -1; + SWD_HoleInfo& p0 = holes[objIdx]; + int idx1 = distanceSearchObject(p0.center, holes, workpiecePara.holeDist_W, distDeviation); + if (idx1 < 0) + continue; + + SVzNLRangeD angleRange = { 85, 95 }; //垂直,5度范围 + SWD_HoleInfo& p1 = holes[idx1]; + //搜索最接近distance且角度为angle的目标, 以角度为优先 + int idx2 = angleConditionDistanceSearch( + p0.center, p1.center, + holes, + workpiecePara.holeDist_L, distDeviation, + angleRange); + if (idx2 < 0) + continue; + + SWD_HoleInfo& p2 = holes[idx2]; + //搜索最接近distance且角度为angle的目标, 以角度为优先 + int idx3 = angleConditionDistanceSearch( + p1.center, p0.center, + holes, + workpiecePara.holeDist_L, distDeviation, + angleRange); + if (idx3 < 0) + continue; + SWD_HoleInfo& p3 = holes[idx3]; + p1.radius = -1; + p2.radius = -1; + p3.radius = -1; + + //重新计算Z值。因为沉孔的原因,Z值会不准确。取四条边的中点处的Z值的均值作为整个的Z值 + + WD_workpieceInfo a_workpiece; + a_workpiece.workpieceType = workpiecePara.workpieceType; + a_workpiece.holes.push_back(p0.center); + a_workpiece.holes.push_back(p1.center); + a_workpiece.holes.push_back(p2.center); + a_workpiece.holes.push_back(p3.center); + a_workpiece.center = { (p0.center.x + p1.center.x + p2.center.x + p3.center.x) / 4, + (p0.center.y + p1.center.y + p2.center.y + p3.center.y) / 4, + (p0.center.z + p1.center.z + p2.center.z + p3.center.z) / 4 }; + + SVzNL3DPoint y_dir; + if (p0.center.y < p2.center.y) + y_dir = { p0.center.x - p2.center.x, p0.center.y - p2.center.y, a_workpiece.center.z }; + else + y_dir = { p2.center.x - p0.center.x, p2.center.y - p0.center.y, a_workpiece.center.z }; + a_workpiece.y_dir = { y_dir.x + a_workpiece.center.x, y_dir.y + a_workpiece.center.y, y_dir.z }; + a_workpiece.z_dir = { a_workpiece.center.x, a_workpiece.center.y, a_workpiece.center.z - 10 }; + workpiecePositioning.push_back(a_workpiece); + } + + int workpieceNum = (int)workpiecePositioning.size(); + //旋转回去 + for (int i = 0; i < workpieceNum; i++) + { + SVzNL3DPoint rpt; + rpt = _ptRotate(workpiecePositioning[i].center, groundCalibPara.invRMatrix); + workpiecePositioning[i].center = rpt; + rpt = _ptRotate(workpiecePositioning[i].y_dir, groundCalibPara.invRMatrix); + workpiecePositioning[i].y_dir = rpt; + rpt = _ptRotate(workpiecePositioning[i].z_dir, groundCalibPara.invRMatrix); + workpiecePositioning[i].z_dir = rpt; + for (int j = 0, j_max = (int)workpiecePositioning[i].holes.size(); j < j_max; j++) + { + rpt = _ptRotate(workpiecePositioning[i].holes[j], groundCalibPara.invRMatrix); + workpiecePositioning[i].holes[j] = rpt; + } + } + return; + +} \ No newline at end of file diff --git a/sourceCode/workpieceHolePositioning_Export.h b/sourceCode/workpieceHolePositioning_Export.h new file mode 100644 index 0000000..655da32 --- /dev/null +++ b/sourceCode/workpieceHolePositioning_Export.h @@ -0,0 +1,48 @@ +#pragma once + +#include "SG_algo_Export.h" +#include + +#define _OUTPUT_DEBUG_DATA 1 + +typedef struct +{ + int workpieceType; + double holeDiameter; //孔直径 + double holeDist_L; //孔间距_长 + double holeDist_W; //孔间距_宽 +}WD_workpieceHoleParam; + +typedef struct +{ + int workpieceType; + std::vector holes; + SVzNL3DPoint center; + SVzNL3DPoint z_dir; + SVzNL3DPoint y_dir; +}WD_workpieceInfo; + +//读版本号 +SG_APISHARED_EXPORT const char* wd_workpieceHolePositioningVersion(void); + +//相机水平安装计算地面调平参数。。 +//旋转矩阵为调平参数,即将平面法向调整为垂直向量的参数 +SG_APISHARED_EXPORT SSG_planeCalibPara wd_getGroundCalibPara( + std::vector< std::vector>& scanLines); + +//相机水平时姿态调平,并去除地面 +SG_APISHARED_EXPORT void wd_lineDataR( + std::vector< SVzNL3DPosition>& a_line, + const double* camPoseR, + double groundH); + +//工件孔定位 +SG_APISHARED_EXPORT void wd_workpieceHolePositioning( + std::vector< std::vector>& scanLinesInput, + const WD_workpieceHoleParam workpiecePara, + const SSG_lineSegParam lineSegPara, + const SSG_outlierFilterParam filterParam, + const SSG_treeGrowParam growParam, + const SSG_planeCalibPara groundCalibPara, + std::vector< WD_workpieceInfo>& workpiecePositioning, + int* errCode); diff --git a/workpieceHolePositioning/workpieceHolePositioning.vcxproj b/workpieceHolePositioning/workpieceHolePositioning.vcxproj new file mode 100644 index 0000000..8ec0ecf --- /dev/null +++ b/workpieceHolePositioning/workpieceHolePositioning.vcxproj @@ -0,0 +1,172 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 16.0 + Win32Proj + {c65f7f4b-b77a-4d65-9490-5304b760b607} + workpieceHolePositioning + 10.0 + + + + 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;WORKPIECEHOLEPOSITIONING_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;WORKPIECEHOLEPOSITIONING_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + _CRT_SECURE_NO_WARNINGS;_DEBUG;WORKPIECEHOLEPOSITIONING_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) + opencv_world320d.lib;baseAlgorithm.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + _CRT_SECURE_NO_WARNINGS;NDEBUG;WORKPIECEHOLEPOSITIONING_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 diff --git a/workpieceHolePositioning_test/workpieceHolePositioning_test.cpp b/workpieceHolePositioning_test/workpieceHolePositioning_test.cpp new file mode 100644 index 0000000..9fd67a5 --- /dev/null +++ b/workpieceHolePositioning_test/workpieceHolePositioning_test.cpp @@ -0,0 +1,531 @@ +锘// BQ_workpieceCornerExtract_test.cpp : 姝ゆ枃浠跺寘鍚 "main" 鍑芥暟銆傜▼搴忔墽琛屽皢鍦ㄦ澶勫紑濮嬪苟缁撴潫銆 +// + +#include +#include +#include +#include +#include +#include "direct.h" +#include +#include "workpieceHolePositioning_Export.h" +#include +#include +#include + +typedef struct +{ + int r; + int g; + int b; +}SG_color; + +typedef struct +{ + int nPointIdx; + double x; + double y; + double z; + float r; + float g; + float b; +} SPointXYZRGB; + +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 _outputScanDataFile_XYZ_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++) + { + SVzNL3DPosition* pt3D = &scanData[line][i]; + float x = (float)pt3D->pt3D.x; + float y = (float)pt3D->pt3D.y; + float z = (float)pt3D->pt3D.z; + char str[250]; + sprintf_s(str, "{ %f, %f, %f } - { 0, 0 } - { 0, 0 }", x, y, z); + + sw << str << std::endl; + } + } + sw.close(); +} + +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(); +} + +void _outputWorkpieceInfo(char* fileName, std::vector< WD_workpieceInfo>& workpiecePositioning) +{ + std::ofstream sw(fileName); + char dataStr[250]; + + int number = (int)workpiecePositioning.size(); + for (int i = 0; i < number; i++) + { + sprintf_s(dataStr, 250, "宸ヤ欢_%d", i + 1); + sw << dataStr << std::endl; + int holeNumber = (int)workpiecePositioning[i].holes.size(); + for (int j = 0; j < holeNumber; j++) + { + sprintf_s(dataStr, 250, " 瀛%d: (%g, %g, %g)", (j+1), workpiecePositioning[i].holes[j].x, workpiecePositioning[i].holes[j].y, workpiecePositioning[i].holes[j].z); + sw << dataStr << std::endl; + } + sprintf_s(dataStr, 50, " center: (%g, %g, %g)", workpiecePositioning[i].center.x, workpiecePositioning[i].center.y, workpiecePositioning[i].center.z); + sw << dataStr << std::endl; + } + sw.close(); +} + +void _outputScanDataFile_vector(char* fileName, std::vector>& scanLines, bool removeZeros, int* headNullLines) +{ + std::ofstream sw(fileName); + int lineNum = (int)scanLines.size(); + if (lineNum == 0) + return; + + 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; + + int lineIdx = 0; + int null_lines = 0; + bool counterNull = true; + for (int line = 0; line < lineNum; line++) + { + int linePtNum = (int)scanLines[line].size(); + if (linePtNum == 0) + continue; + + if (true == removeZeros) + { + int vldPtNum = 0; + for (int i = 0; i < linePtNum; i++) + { + if (scanLines[line][i].pt3D.z > 1e-4) + vldPtNum++; + } + linePtNum = vldPtNum; + } + sw << "Line_" << lineIdx << "_0_" << linePtNum << std::endl; + lineIdx++; + bool isNull = true; + for (int i = 0; i < linePtNum; i++) + { + SVzNL3DPoint* pt3D = &scanLines[line][i].pt3D; + if ((pt3D->z > 1e-4) && (isNull == true)) + isNull = false; + if ((true == removeZeros) && (pt3D->z < 1e-4)) + continue; + float x = (float)pt3D->x; + float y = (float)pt3D->y; + float z = (float)pt3D->z; + sw << "{ " << x << "," << y << "," << z << " }-"; + sw << "{0,0}-{0,0}" << std::endl; + } + if (true == counterNull) + { + if (true == isNull) + null_lines++; + else + counterNull = false; + } + } + *headNullLines = null_lines; + 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 _outputRGBDResult_RGBD( + char* fileName, + std::vector>& scanLines, + std::vector< WD_workpieceInfo>& workpiecePositioning) +{ + std::vector objects; + int objNumber = (int)workpiecePositioning.size(); + for (int i = 0; i < objNumber; i++) + { + SVzNL3DPosition a_objPt; + int holeNumber = (int)workpiecePositioning[i].holes.size(); + for (int j = 0; j < holeNumber; j++) + { + a_objPt.nPointIdx = i + 1; + a_objPt.pt3D = workpiecePositioning[i].holes[j]; + objects.push_back(a_objPt); + } + objects.push_back(a_objPt); + } + + int lineNum = (int)scanLines.size(); + std::ofstream sw(fileName); + int realLines = (objNumber == 0) ? lineNum : (lineNum + 1); + 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 lineIdx = 0; + for (int line = 0; line < lineNum; line++) + { + int linePtNum = (int)scanLines[line].size(); + if (linePtNum == 0) + continue; + + sw << "Line_" << lineIdx << "_0_" << linePtNum << std::endl; + lineIdx++; + for (int i = 0; i < linePtNum; i++) + { + SVzNL3DPosition* pt3D = &scanLines[line][i]; + if (pt3D->nPointIdx > 0) + int kkk = 1; + int featureType_v = pt3D->nPointIdx & 0xff; + int featureType_h = featureType_v >> 4; + featureType_v &= 0x0f; + + if (pt3D->nPointIdx == 1) + { + rgb = { 255, 97, 0 }; + 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; + } + } + + int linePtNum = (int)objects.size(); + sw << "Line_" << lineNum << "_0_" << linePtNum + 1 << std::endl; + lineNum++; + for (int i = 0; i < linePtNum; i++) + { + int colorIdx = objects[i].nPointIdx % 8; + rgb = objColor[colorIdx]; + size = 10; + float x = (float)objects[i].pt3D.x; + float y = (float)objects[i].pt3D.y; + float z = (float)objects[i].pt3D.z; + sw << "{" << x << "," << y << "," << z << "}-"; + sw << "{0,0}-{0,0}-"; + sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; + } + //杈撳嚭鏂瑰悜绾挎潯 + + sw.close(); +} + +SVzNL3DPoint _pointRT(SVzNL3DPoint& origin, const double* R, const double* T) +{ + SVzNL3DPoint result; + result.x = origin.x * R[0] + origin.y * R[1] + origin.z * R[2]; + result.y = origin.x * R[3] + origin.y * R[4] + origin.z * R[5]; + result.z = origin.x * R[6] + origin.y * R[7] + origin.z * R[8]; + result.x += T[0]; + result.y += T[1]; + result.z += T[2]; + return result; +} + +#define TEST_COMPUTE_CALIB_PARA 0 +#define TEST_COMPUTE_HOLE 1 +#define TEST_GROUP 1 +int main() +{ + const char* dataPath[TEST_GROUP] = { + + "F:/ShangGu/椤圭洰/鍐犻挦椤圭洰/鎷撴櫘鍙戝伐浠跺瓟瀹氫綅/鎷撴櫘鍙戠偣浜/", //0 + }; + + SVzNLRange fileIdx[TEST_GROUP] = { + {1,4}, + }; + + const char* ver = wd_workpieceHolePositioningVersion(); + printf("ver:%s\n", ver); + +#if TEST_COMPUTE_CALIB_PARA + char _calib_datafile[256]; + sprintf_s(_calib_datafile, "%sLaserData_ground.txt", dataPath[0]); + int lineNum = 0; + float lineV = 0.0f; + int dataCalib = 0; + int maxTimeStamp = 0; + int clockPerSecond = 0; + std::vector> scanData; + vzReadLaserScanPointFromFile_XYZ_vector(_calib_datafile, scanData); + + lineNum = (int)scanData.size(); + if (scanData.size() > 0) + { + SSG_planeCalibPara calibPara = wd_getGroundCalibPara(scanData); + //缁撴灉杩涜楠岃瘉 + for (int i = 0; i < lineNum; i++) + { + if (i == 14) + int kkk = 1; + //琛屽鐞 + //璋冨钩锛屽幓闄ゅ湴闈 + wd_lineDataR(scanData[i], calibPara.planeCalib, -1); + } + // + char calibFile[250]; + sprintf_s(calibFile, "%sground_calib_para.txt", dataPath[0]); + _outputCalibPara(calibFile, calibPara); + char _out_file[256]; + sprintf_s(_out_file, "%sscanData_ground_calib_verify.txt", dataPath[0]); + int headNullLines = 0; + _outputScanDataFile_vector(_out_file, scanData, false, &headNullLines); + + for (int fidx = fileIdx[0].nMin; fidx <= fileIdx[0].nMax; fidx++) + { + //fidx =4; + char _scan_file[256]; + sprintf_s(_scan_file, "%sLaserData_%d.txt", dataPath[0], fidx); + std::vector> scanLines; + vzReadLaserScanPointFromFile_XYZ_vector(_scan_file, scanLines); + if (scanLines.size() == 0) + continue; + lineNum = (int)scanLines.size(); + for (int i = 0; i < lineNum; i++) + { + //璋冨钩锛屽幓闄ゅ湴闈 + wd_lineDataR(scanLines[i], calibPara.planeCalib, -1); + } + sprintf_s(_scan_file, "%sLaserData_%d_calib_verify.txt", dataPath[0], fidx); + int headNullLines = 0; + _outputScanDataFile_vector(_scan_file, scanLines, false, &headNullLines); + } + printf("%s: calib done!\n", _calib_datafile); + } +#endif + +#if TEST_COMPUTE_HOLE + for (int grp = 0; grp <= 0; grp++) + { + SSG_planeCalibPara groundCalibPara; + //鍒濆鍖栨垚鍗曚綅闃 + groundCalibPara.planeCalib[0] = 1.0; + groundCalibPara.planeCalib[1] = 0.0; + groundCalibPara.planeCalib[2] = 0.0; + groundCalibPara.planeCalib[3] = 0.0; + groundCalibPara.planeCalib[4] = 1.0; + groundCalibPara.planeCalib[5] = 0.0; + groundCalibPara.planeCalib[6] = 0.0; + groundCalibPara.planeCalib[7] = 0.0; + groundCalibPara.planeCalib[8] = 1.0; + groundCalibPara.planeHeight = -1.0; + for (int i = 0; i < 9; i++) + groundCalibPara.invRMatrix[i] = groundCalibPara.planeCalib[i]; + char calibFile[250]; + sprintf_s(calibFile, "%sground_calib_para.txt", dataPath[grp]); + groundCalibPara = _readCalibPara(calibFile); + + for (int fidx = fileIdx[grp].nMin; fidx <= fileIdx[grp].nMax; fidx++) + { + //fidx =4; + char _scan_file[256]; + sprintf_s(_scan_file, "%sLaserData_%d.txt", dataPath[grp], fidx); + std::vector> scanLines; + vzReadLaserScanPointFromFile_XYZ_vector(_scan_file, scanLines); + if (scanLines.size() == 0) + continue; + + long t1 = (long)GetTickCount64();//缁熻鏃堕棿 + + SSG_lineSegParam lineSegPara; + lineSegPara.distScale = 3.0; + lineSegPara.segGapTh_y = 3.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 = 10.0; + growParam.maxSkipDistance = 10.0; + growParam.zDeviation_max = 10.0;// algoParam.bagParam.bagH / 2; //琚嬪瓙楂樺害1/2 + growParam.minLTypeTreeLen = 100; //mm + growParam.minVTypeTreeLen = 100; //mm + WD_workpieceHoleParam workpiecePara; + workpiecePara.workpieceType = 0; + workpiecePara.holeDiameter = 6.0; // + workpiecePara.holeDist_W = 32.0; + workpiecePara.holeDist_L = 40.0; + int errCode = 0; + std::vector< WD_workpieceInfo> workpiecePositioning; + wd_workpieceHolePositioning( + scanLines, + workpiecePara, + lineSegPara, + filterParam, + growParam, + groundCalibPara, + workpiecePositioning, + &errCode); + long t2 = (long)GetTickCount64(); + printf("%s: %d(ms)!\n", _scan_file, (int)(t2 - t1)); + //杈撳嚭娴嬭瘯缁撴灉 + sprintf_s(_scan_file, "%sresult\\LaserLine%d_result.txt", dataPath[grp], fidx); + _outputRGBDResult_RGBD(_scan_file, scanLines, workpiecePositioning); + sprintf_s(calibFile, "%sresult\\LaserLine%d_corner_info.txt", dataPath[grp], fidx); + _outputWorkpieceInfo(calibFile, workpiecePositioning); + } + } +#endif +} + +// 杩愯绋嬪簭: Ctrl + F5 鎴栬皟璇 >鈥滃紑濮嬫墽琛(涓嶈皟璇)鈥濊彍鍗 +// 璋冭瘯绋嬪簭: F5 鎴栬皟璇 >鈥滃紑濮嬭皟璇曗濊彍鍗 + +// 鍏ラ棬浣跨敤鎶宸: +// 1. 浣跨敤瑙e喅鏂规璧勬簮绠$悊鍣ㄧ獥鍙f坊鍔/绠$悊鏂囦欢 +// 2. 浣跨敤鍥㈤槦璧勬簮绠$悊鍣ㄧ獥鍙h繛鎺ュ埌婧愪唬鐮佺鐞 +// 3. 浣跨敤杈撳嚭绐楀彛鏌ョ湅鐢熸垚杈撳嚭鍜屽叾浠栨秷鎭 +// 4. 浣跨敤閿欒鍒楄〃绐楀彛鏌ョ湅閿欒 +// 5. 杞埌鈥滈」鐩>鈥滄坊鍔犳柊椤光濅互鍒涘缓鏂扮殑浠g爜鏂囦欢锛屾垨杞埌鈥滈」鐩>鈥滄坊鍔犵幇鏈夐」鈥濅互灏嗙幇鏈変唬鐮佹枃浠舵坊鍔犲埌椤圭洰 +// 6. 灏嗘潵锛岃嫢瑕佸啀娆℃墦寮姝ら」鐩紝璇疯浆鍒扳滄枃浠垛>鈥滄墦寮鈥>鈥滈」鐩濆苟閫夋嫨 .sln 鏂囦欢 diff --git a/workpieceHolePositioning_test/workpieceHolePositioning_test.vcxproj b/workpieceHolePositioning_test/workpieceHolePositioning_test.vcxproj new file mode 100644 index 0000000..76e8f5b --- /dev/null +++ b/workpieceHolePositioning_test/workpieceHolePositioning_test.vcxproj @@ -0,0 +1,157 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {ca8cba7f-4d72-4630-af6d-bf872306502c} + workpieceHolePositioningtest + 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;workpieceHolePositioning.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;workpieceHolePositioning.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file