// gasFillingPortPosition_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include #include #include #include #include #include "direct.h" #include #include "rodAndBarDetection_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 wdReadLaserScanPointFromFile_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 wd_gridScan_GetROIData(std::vector>& scanData, SVzNLRangeD roi_y, std::vector>& roiData) { int lineNum = (int)scanData.size(); int linePtNum = (int)scanData[0].size(); int globalPtStart = INT_MAX; int globalPtEnd = 0; int lineStart = INT_MAX; int lineEnd = 0; for (int line = 0; line < lineNum; line++) { std::vector< SVzNL3DPosition >& lineData = scanData[line]; int ptSize = (int)lineData.size(); int vldNum = 0; int ptStart = INT_MAX; int ptEnd = 0; for (int i = 0; i < ptSize; i++) { if (lineData[i].pt3D.z > 1e-4) { if ((lineData[i].pt3D.y < roi_y.min) || (lineData[i].pt3D.y > roi_y.max)) lineData[i].pt3D = { 0.0, 0.0, 0.0 }; } if (lineData[i].pt3D.z > 1e-4) { if (ptStart > i) ptStart = i; ptEnd = i; vldNum++; } } if (vldNum > 0) { if (globalPtStart > ptStart) globalPtStart = ptStart; if (globalPtEnd < ptEnd) globalPtEnd = ptEnd; if (lineStart > line) lineStart = line; lineEnd = line; } } int vldLineNum = lineEnd - lineStart + 1; int vldPtNum = globalPtEnd - globalPtStart + 1; roiData.resize(vldLineNum); for (int line = 0; line < vldLineNum; line++) { roiData[line].resize(vldPtNum); for (int i = 0; i < vldPtNum; i++) roiData[line][i] = scanData[line + lineStart][i + globalPtStart]; } return; } void _outputScanDataFile(char* fileName, std::vector>& scanData, float lineV, int maxTimeStamp, int clockPerSecond) { std::ofstream sw(fileName); int lineNum = (int)scanData.size(); 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)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; sw << "{ " << x << "," << y << "," << z << " }-"; sw << "{0,0}-{0,0}" << 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 _outputChanneltInfo(char* fileName, std::vector& screwInfo) { std::ofstream sw(fileName); char dataStr[250]; int objNum = (int)screwInfo.size(); for (int i = 0; i < objNum; i++) { sprintf_s(dataStr, 250, "螺杆_%d: center_( %g, %g, %g ), dir_( %g, %g, %g )", i + 1, screwInfo[i].center.x, screwInfo[i].center.y, screwInfo[i].center.z, screwInfo[i].axialDir.x, screwInfo[i].axialDir.y, screwInfo[i].axialDir.z); sw << dataStr << std::endl; } sw.close(); } void _outputPlatePiseInfo(char* fileName, SSX_pointPoseInfo& centerInfo) { std::ofstream sw(fileName); char dataStr[250]; sprintf_s(dataStr, 250, "定位盘: center_( %g, %g, %g ), normalDir_( %g, %g, %g ), xDir_( %g, %g, %g ), yDir_( %g, %g, %g )", centerInfo.center.x, centerInfo.center.y, centerInfo.center.z, centerInfo.normalDir.x, centerInfo.normalDir.y, centerInfo.normalDir.z, centerInfo.xDir.x, centerInfo.xDir.y, centerInfo.xDir.z, centerInfo.yDir.x, centerInfo.yDir.y, centerInfo.yDir.z); sw << dataStr << std::endl; sw.close(); } void _outputRodtInfo(char* fileName, std::vector& rodInfo) { std::ofstream sw(fileName); char dataStr[250]; int objNum = (int)rodInfo.size(); for (int i = 0; i < objNum; i++) { sprintf_s(dataStr, 250, "螺杆_%d: center_( %g, %g, %g ), dir_axia( %g, %g, %g ), dir_normal_( %g, %g, %g )", i + 1, rodInfo[i].center.x, rodInfo[i].center.y, rodInfo[i].center.z, rodInfo[i].axialDir.x, rodInfo[i].axialDir.y, rodInfo[i].axialDir.z, rodInfo[i].normalDir.x, rodInfo[i].normalDir.y, rodInfo[i].normalDir.z); sw << dataStr << std::endl; } sw.close(); } void _outputWeldSeamInfo(char* fileName, std::vector& weldSeamInfo) { std::ofstream sw(fileName); char dataStr[250]; int objNum = (int)weldSeamInfo.size(); for (int i = 0; i < objNum; i++) { sprintf_s(dataStr, 250, "螺杆_%d: center_( %g, %g, %g ), dir_normal_( %g, %g, %g ), x_dir_(", i + 1, weldSeamInfo[i].center.x, weldSeamInfo[i].center.y, weldSeamInfo[i].center.z, weldSeamInfo[i].normalDir.x, weldSeamInfo[i].normalDir.y, weldSeamInfo[i].normalDir.z); sw << dataStr << std::endl; } sw.close(); } void _outputRGBDScan_RGBD( char* fileName, std::vector>& scanLines, std::vector& screwInfo ) { int lineNum = (int)scanLines.size(); std::ofstream sw(fileName); int realLines = lineNum; int objNum = (int)screwInfo.size(); if (objNum > 0) realLines += 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 == 1) { rgb = objColor[pt3D->nPointIdx]; size = 3; } else if (pt3D->nPointIdx == 2) { rgb = { 250, 0, 0 }; size = 5; } else //if (pt3D->nPointIdx == 0) { 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 (objNum > 0) { sw << "Line_" << lineIdx << "_0_" << objNum << std::endl; rgb = { 250, 0, 0 }; size = 8; for (int i = 0; i < objNum; i++) { float x = (float)screwInfo[i].center.x; float y = (float)screwInfo[i].center.y; float z = (float)screwInfo[i].center.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } //多输出一个,修正显示工具bug float x = (float)screwInfo[0].center.x; float y = (float)screwInfo[0].center.y; float z = (float)screwInfo[0].center.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; //输出法向 size = 1; double len = 60; lineIdx = 0; for (int i = 0; i < objNum; i++) { SVzNL3DPoint pt0 = { screwInfo[i].center.x - len * screwInfo[i].axialDir.x, screwInfo[i].center.y - len * screwInfo[i].axialDir.y, screwInfo[i].center.z - len * screwInfo[i].axialDir.z }; SVzNL3DPoint pt1 = { screwInfo[i].center.x + len * screwInfo[i].axialDir.x, screwInfo[i].center.y + len * screwInfo[i].axialDir.y, screwInfo[i].center.z + len * screwInfo[i].axialDir.z }; //显示法向量 sw << "Poly_" << lineIdx << "_2" << std::endl; sw << "{" << (float)pt0.x << "," << (float)pt0.y << "," << (float)pt0.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; sw << "{" << pt1.x << "," << pt1.y << "," << pt1.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; lineIdx++; } //多输出一个,修正显示工具bug SVzNL3DPoint pt0 = { screwInfo[0].center.x - len * screwInfo[0].axialDir.x, screwInfo[0].center.y - len * screwInfo[0].axialDir.y, screwInfo[0].center.z - len * screwInfo[0].axialDir.z }; SVzNL3DPoint pt1 = { screwInfo[0].center.x + len * screwInfo[0].axialDir.x, screwInfo[0].center.y + len * screwInfo[0].axialDir.y, screwInfo[0].center.z + len * screwInfo[0].axialDir.z }; //显示法向量 sw << "Poly_" << lineIdx << "_2" << std::endl; sw << "{" << (float)pt0.x << "," << (float)pt0.y << "," << (float)pt0.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; sw << "{" << pt1.x << "," << pt1.y << "," << pt1.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; lineIdx++; } sw.close(); } void _outputRGBDScan_RGBD_centerPose( char* fileName, std::vector>& scanLines, SSX_pointPoseInfo& poseInfo ) { int lineNum = (int)scanLines.size(); std::ofstream sw(fileName); int realLines = lineNum; realLines += 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 == 2) { rgb = { 250, 0, 0 }; size = 5; } //else if (pt3D->nPointIdx == 1) //{ // rgb = { 0, 0, 250 }; // size = 3; //} else //if (pt3D->nPointIdx == 0) { 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; } } { sw << "Line_" << lineIdx << "_0_1" << std::endl; rgb = { 250, 0, 0 }; size = 8; float x = (float)poseInfo.center.x; float y = (float)poseInfo.center.y; float z = (float)poseInfo.center.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; //输出法向 size = 1; double len = 60; lineIdx = 0; { SVzNL3DPoint pt0 = { poseInfo.center.x - len * poseInfo.normalDir.x, poseInfo.center.y - len * poseInfo.normalDir.y, poseInfo.center.z - len * poseInfo.normalDir.z }; SVzNL3DPoint pt1 = { poseInfo.center.x + len * poseInfo.normalDir.x, poseInfo.center.y + len * poseInfo.normalDir.y, poseInfo.center.z + len * poseInfo.normalDir.z }; SVzNL3DPoint basePt = poseInfo.center; SVzNL3DPoint pt2 = { poseInfo.center.x + len * poseInfo.xDir.x, poseInfo.center.y + len * poseInfo.xDir.y, poseInfo.center.z + len * poseInfo.xDir.z }; SVzNL3DPoint pt3 = { poseInfo.center.x + len * poseInfo.yDir.x, poseInfo.center.y + len * poseInfo.yDir.y, poseInfo.center.z + len * poseInfo.yDir.z }; //显示法向量 sw << "Poly_" << lineIdx << "_2" << std::endl; sw << "{" << (float)pt0.x << "," << (float)pt0.y << "," << (float)pt0.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; sw << "{" << pt1.x << "," << pt1.y << "," << pt1.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; lineIdx++; rgb = { 0, 250, 0 }; sw << "Poly_" << lineIdx << "_2" << std::endl; sw << "{" << (float)basePt.x << "," << (float)basePt.y << "," << (float)basePt.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; sw << "{" << pt2.x << "," << pt2.y << "," << pt2.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; lineIdx++; rgb = { 0, 0, 250 }; sw << "Poly_" << lineIdx << "_2" << std::endl; sw << "{" << (float)basePt.x << "," << (float)basePt.y << "," << (float)basePt.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; sw << "{" << pt3.x << "," << pt3.y << "," << pt3.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; lineIdx++; } } sw.close(); } void _outputRGBDScan_RGBD_rodInfo( char* fileName, std::vector>& scanLines, std::vector& rodInfo ) { int lineNum = (int)scanLines.size(); std::ofstream sw(fileName); int realLines = lineNum; int objNum = (int)rodInfo.size(); if (objNum > 0) realLines += 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 centerFlag = pt3D->nPointIdx >> 4; if (centerFlag > 0) { rgb = { 180, 0, 0 }; size = 2; } else { rgb = objColor[pt3D->nPointIdx % 8]; size = 2; } } else //if (pt3D->nPointIdx == 0) { 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 (objNum > 0) { sw << "Line_" << lineIdx << "_0_" << objNum << std::endl; size = 12; for (int i = 0; i < objNum; i++) { if(i == 0) rgb = { 250, 255, 0 }; else rgb = { 250, 0, 0 }; float x = (float)rodInfo[i].center.x; float y = (float)rodInfo[i].center.y; float z = (float)rodInfo[i].center.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } //输出法向 size = 8; double len = 60; lineIdx = 0; for (int i = 0; i < objNum; i++) { if (i == 0) rgb = { 250, 255, 0 }; else rgb = { 250, 0, 0 }; SVzNL3DPoint pt0 = { rodInfo[i].center.x, rodInfo[i].center.y, rodInfo[i].center.z }; SVzNL3DPoint pt1 = { rodInfo[i].center.x + len * rodInfo[i].axialDir.x, rodInfo[i].center.y + len * rodInfo[i].axialDir.y, rodInfo[i].center.z + len * rodInfo[i].axialDir.z }; SVzNL3DPoint pt2 = { rodInfo[i].center.x + len * rodInfo[i].normalDir.x, rodInfo[i].center.y + len * rodInfo[i].normalDir.y, rodInfo[i].center.z + len * rodInfo[i].normalDir.z }; //显示轴向量 sw << "Poly_" << lineIdx << "_2" << std::endl; sw << "{" << (float)rodInfo[i].startPt.x << "," << (float)rodInfo[i].startPt.y << "," << (float)rodInfo[i].startPt.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; sw << "{" << (float)rodInfo[i].endPt.x << "," << (float)rodInfo[i].endPt.y << "," << (float)rodInfo[i].endPt.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)pt0.x << "," << (float)pt0.y << "," << (float)pt0.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; sw << "{" << (float)pt2.x << "," << (float)pt2.y << "," << (float)pt2.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; lineIdx++; } lineIdx++; } sw.close(); } void _outputRGBDScan_RGBD_weldSeam( char* fileName, std::vector>& scanLines, std::vector& weldSeamInfo ) { int lineNum = (int)scanLines.size(); std::ofstream sw(fileName); int realLines = lineNum; int objNum = (int)weldSeamInfo.size(); if (objNum > 0) realLines += 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 centerFlag = pt3D->nPointIdx >> 4; if (centerFlag > 0) { rgb = { 180, 0, 0 }; size = 2; } else { rgb = objColor[pt3D->nPointIdx % 8]; size = 2; } } else //if (pt3D->nPointIdx == 0) { 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 (objNum > 0) { sw << "Line_" << lineIdx << "_0_" << objNum << std::endl; size = 12; for (int i = 0; i < objNum; i++) { if (i == 0) rgb = { 250, 255, 0 }; else rgb = { 250, 0, 0 }; float x = (float)weldSeamInfo[i].center.x; float y = (float)weldSeamInfo[i].center.y; float z = (float)weldSeamInfo[i].center.z; sw << "{" << x << "," << y << "," << z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << rgb.r << "," << rgb.g << "," << rgb.b << "," << size << " }" << std::endl; } //输出法向 size = 8; double len = 60; lineIdx = 0; for (int i = 0; i < objNum; i++) { if (i == 0) rgb = { 250, 255, 0 }; else rgb = { 250, 0, 0 }; SVzNL3DPoint pt0 = { weldSeamInfo[i].center.x, weldSeamInfo[i].center.y, weldSeamInfo[i].center.z }; SVzNL3DPoint pt2 = { weldSeamInfo[i].center.x + len * weldSeamInfo[i].normalDir.x, weldSeamInfo[i].center.y + len * weldSeamInfo[i].normalDir.y, weldSeamInfo[i].center.z + len * weldSeamInfo[i].normalDir.z }; //显示轴向量 sw << "Poly_" << lineIdx << "_2" << std::endl; sw << "{" << (float)weldSeamInfo[i].startPt.x << "," << (float)weldSeamInfo[i].startPt.y << "," << (float)weldSeamInfo[i].startPt.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; sw << "{" << (float)weldSeamInfo[i].endPt.x << "," << (float)weldSeamInfo[i].endPt.y << "," << (float)weldSeamInfo[i].endPt.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)pt0.x << "," << (float)pt0.y << "," << (float)pt0.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; sw << "{" << (float)pt2.x << "," << (float)pt2.y << "," << (float)pt2.z << "}-"; sw << "{0,0}-{0,0}-"; sw << "{" << (int)rgb.r << "," << (int)rgb.g << "," << (int)rgb.b << "," << size << "}" << std::endl; lineIdx++; } lineIdx++; } sw.close(); } #define SCREW_TEST_GROUP 1 void screwTest(void) { const char* dataPath[SCREW_TEST_GROUP] = { "F:/ShangGu/项目/冠钦项目/螺杆测量/数据/模拟数据/", //0 }; SVzNLRange fileIdx[SCREW_TEST_GROUP] = { {1,4}, }; const char* ver = wd_rodAndBarDetectionVersion(); printf("ver:%s\n", ver); for (int grp = 0; grp < SCREW_TEST_GROUP; grp++) { for (int fidx = fileIdx[grp].nMin; fidx <= fileIdx[grp].nMax; fidx++) { //fidx =7; char _scan_file[256]; sprintf_s(_scan_file, "%sLaserData_%d.txt", dataPath[grp], fidx); std::vector> scanLines; wdReadLaserScanPointFromFile_XYZ_vector(_scan_file, scanLines); //转成plyTxt格式 //sprintf_s(_scan_file, "%s%d_ply_Hi229229.txt", dataPath[grp], fidx); //wdSavePlyTxt(_scan_file, scanLines); long t1 = (long)GetTickCount64();//统计时间 double rodDiameter = 10.0; SSG_cornerParam cornerParam; cornerParam.cornerTh = 60; //45度角 cornerParam.scale = rodDiameter/4; // algoParam.bagParam.bagH / 8; // 15; // algoParam.bagParam.bagH / 8; cornerParam.minEndingGap = 20; // algoParam.bagParam.bagW / 4; cornerParam.minEndingGap_z = 5.0; cornerParam.jumpCornerTh_1 = 15; //水平角度,小于此角度视为水平 cornerParam.jumpCornerTh_2 = 60; SSG_outlierFilterParam filterParam; filterParam.continuityTh = 20.0; //噪声滤除。当相邻点的z跳变大于此门限时,检查是否为噪声。若长度小于outlierLen, 视为噪声 filterParam.outlierTh = 5; SSG_treeGrowParam growParam; growParam.maxLineSkipNum = 10; growParam.yDeviation_max = 20.0; growParam.maxSkipDistance = 20.0; growParam.zDeviation_max = 50.0;// growParam.minLTypeTreeLen = 10; //mm, 螺杆长度 growParam.minVTypeTreeLen = 10; //mm bool isHorizonScan = true; //true:激光线平行槽道;false:激光线垂直槽道 int errCode = 0; std::vector screwInfo; sx_hexHeadScrewMeasure( scanLines, isHorizonScan, //true:激光线平行槽道;false:激光线垂直槽道 cornerParam, filterParam, growParam, rodDiameter, screwInfo, &errCode); long t2 = (long)GetTickCount64(); printf("%s: %d(ms)!\n", _scan_file, (int)(t2 - t1)); //输出测试结果 sprintf_s(_scan_file, "%sresult\\%d_result.txt", dataPath[grp], fidx); _outputRGBDScan_RGBD(_scan_file, scanLines, screwInfo); sprintf_s(_scan_file, "%sresult\\%d_screw_info.txt", dataPath[grp], fidx); _outputChanneltInfo(_scan_file, screwInfo); } } } #define LOCATING_PALTE_TEST_GROUP 1 void locatingPlateTest(void) { const char* dataPath[SCREW_TEST_GROUP] = { "F:/ShangGu/项目/冠钦项目/螺杆测量/配天现场点云/定位盘点云/", //0 }; SVzNLRange fileIdx[LOCATING_PALTE_TEST_GROUP] = { {1,16}, }; const char* ver = wd_rodAndBarDetectionVersion(); printf("ver:%s\n", ver); for (int grp = 0; grp < LOCATING_PALTE_TEST_GROUP; grp++) { 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; wdReadLaserScanPointFromFile_XYZ_vector(_scan_file, scanLines); //转成plyTxt格式 //sprintf_s(_scan_file, "%s%d_ply_Hi229229.txt", dataPath[grp], fidx); //wdSavePlyTxt(_scan_file, scanLines); long t1 = (long)GetTickCount64();//统计时间 double rodDiameter = 10.0; SSG_cornerParam cornerParam; cornerParam.cornerTh = 60; //45度角 cornerParam.scale = rodDiameter / 4; // algoParam.bagParam.bagH / 8; // 15; // algoParam.bagParam.bagH / 8; cornerParam.minEndingGap = 20; // algoParam.bagParam.bagW / 4; cornerParam.minEndingGap_z = 5.0; cornerParam.jumpCornerTh_1 = 15; //水平角度,小于此角度视为水平 cornerParam.jumpCornerTh_2 = 60; int errCode = 0; SSX_pointPoseInfo centerPose = sx_getLocationPlatePose( scanLines, cornerParam, & errCode); long t2 = (long)GetTickCount64(); printf("%s: %d(ms)!\n", _scan_file, (int)(t2 - t1)); //输出测试结果 sprintf_s(_scan_file, "%sresult\\%d_result.txt", dataPath[grp], fidx); _outputRGBDScan_RGBD_centerPose(_scan_file, scanLines, centerPose); sprintf_s(_scan_file, "%sresult\\%d_screw_info.txt", dataPath[grp], fidx); _outputPlatePiseInfo(_scan_file, centerPose); } } } #define ROD_POSITION_TEST_GROUP 1 void rodPositionTest(void) { const char* dataPath[ROD_POSITION_TEST_GROUP] = { "F:/ShangGu/项目/冠钦项目/棒材抓取/", //0 }; SVzNLRange fileIdx[ROD_POSITION_TEST_GROUP] = { {1,8}, }; const char* ver = wd_rodAndBarDetectionVersion(); printf("ver:%s\n", ver); for (int grp = 0; grp < ROD_POSITION_TEST_GROUP; grp++) { 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]; //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 =1; char _scan_file[256]; sprintf_s(_scan_file, "%sLaserData_%d.txt", dataPath[grp], fidx); std::vector> scanLines; wdReadLaserScanPointFromFile_XYZ_vector(_scan_file, scanLines); //转成plyTxt格式 //sprintf_s(_scan_file, "%s%d_ply_Hi229229.txt", dataPath[grp], fidx); //wdSavePlyTxt(_scan_file, scanLines); long t1 = (long)GetTickCount64();//统计时间 SSX_rodParam rodParam; rodParam.diameter = 52.0; //圆棒直径 rodParam.len = 290; SSG_cornerParam cornerParam; cornerParam.cornerTh = 60; //45度角 cornerParam.scale = rodParam.diameter / 4; // algoParam.bagParam.bagH / 8; // 15; // algoParam.bagParam.bagH / 8; cornerParam.minEndingGap = 20; // algoParam.bagParam.bagW / 4; cornerParam.minEndingGap_z = 5.0; cornerParam.jumpCornerTh_1 = 15; //水平角度,小于此角度视为水平 cornerParam.jumpCornerTh_2 = 60; SSG_outlierFilterParam filterParam; filterParam.continuityTh = 5.0; //噪声滤除。当相邻点的z跳变大于此门限时,检查是否为噪声。若长度小于outlierLen, 视为噪声 filterParam.outlierTh = 5; SSG_treeGrowParam growParam; growParam.maxLineSkipNum = 5; growParam.yDeviation_max = 10.0; growParam.maxSkipDistance = 10.0; growParam.zDeviation_max = 10.0;// growParam.minLTypeTreeLen = 100; //mm, 螺杆长度 growParam.minVTypeTreeLen = 100; //mm bool isHorizonScan = true; //true:激光线平行槽道;false:激光线垂直槽道 int errCode = 0; std::vector rodInfo; sx_rodPositioning( scanLines, poseCalibPara, cornerParam, filterParam, growParam, rodParam, rodInfo, &errCode); long t2 = (long)GetTickCount64(); printf("%s: %d(ms)!\n", _scan_file, (int)(t2 - t1)); //输出测试结果 sprintf_s(_scan_file, "%sresult\\%d_result.txt", dataPath[grp], fidx); _outputRGBDScan_RGBD_rodInfo(_scan_file, scanLines, rodInfo); sprintf_s(_scan_file, "%sresult\\%d_screw_info.txt", dataPath[grp], fidx); _outputRodtInfo(_scan_file, rodInfo); } } } #define WELD_SEAM_TEST_GROUP 2 void rodWeldSeamPosition_test(void) { const char* dataPath[WELD_SEAM_TEST_GROUP] = { "F:/ShangGu/项目/冠钦项目/筑裕视觉钢结构焊接/视觉照片1/", //0 "F:/ShangGu/项目/冠钦项目/筑裕视觉钢结构焊接/视觉照片2/", //0 }; SVzNLRange fileIdx[WELD_SEAM_TEST_GROUP] = { {1,20},{1,20}, }; const char* ver = wd_rodAndBarDetectionVersion(); printf("ver:%s\n", ver); for (int grp = 0; grp < WELD_SEAM_TEST_GROUP; grp++) { 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]; //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 =1; char _scan_file[256]; sprintf_s(_scan_file, "%sLaserData_%d.txt", dataPath[grp], fidx); std::vector> scanLines; wdReadLaserScanPointFromFile_XYZ_vector(_scan_file, scanLines); //转成plyTxt格式 //sprintf_s(_scan_file, "%s%d_ply_Hi229229.txt", dataPath[grp], fidx); //wdSavePlyTxt(_scan_file, scanLines); long t1 = (long)GetTickCount64();//统计时间 SSX_rodParam rodParam; rodParam.diameter = 52.0; //圆棒直径 rodParam.len = 290; SSG_cornerParam cornerParam; cornerParam.cornerTh = 60; //45度角 cornerParam.scale = rodParam.diameter / 4; // algoParam.bagParam.bagH / 8; // 15; // algoParam.bagParam.bagH / 8; cornerParam.minEndingGap = 20; // algoParam.bagParam.bagW / 4; cornerParam.minEndingGap_z = 5.0; cornerParam.jumpCornerTh_1 = 15; //水平角度,小于此角度视为水平 cornerParam.jumpCornerTh_2 = 60; SSG_outlierFilterParam filterParam; filterParam.continuityTh = 5.0; //噪声滤除。当相邻点的z跳变大于此门限时,检查是否为噪声。若长度小于outlierLen, 视为噪声 filterParam.outlierTh = 5; SSG_treeGrowParam growParam; growParam.maxLineSkipNum = 5; growParam.yDeviation_max = 10.0; growParam.maxSkipDistance = 10.0; growParam.zDeviation_max = 10.0;// growParam.minLTypeTreeLen = 100; //mm, 螺杆长度 growParam.minVTypeTreeLen = 100; //mm bool isHorizonScan = true; //true:激光线平行槽道;false:激光线垂直槽道 int errCode = 0; std::vector weldSeamInfo; sx_rebarWeldSeamPositioning( scanLines, poseCalibPara, cornerParam, filterParam, growParam, rodParam, weldSeamInfo, &errCode); long t2 = (long)GetTickCount64(); printf("%s: %d(ms)!\n", _scan_file, (int)(t2 - t1)); //输出测试结果 sprintf_s(_scan_file, "%sresult\\%d_result.txt", dataPath[grp], fidx); _outputRGBDScan_RGBD_weldSeam(_scan_file, scanLines, weldSeamInfo); sprintf_s(_scan_file, "%sresult\\%d_screw_info.txt", dataPath[grp], fidx); _outputWeldSeamInfo(_scan_file, weldSeamInfo); } } } int main() { #if 1 //螺杆定位测试 #if 0 screwTest(); #else locatingPlateTest(); #endif #else //棒材抓取定位测试 #if 0 rodPositionTest(); #else rodWeldSeamPosition_test(); #endif #endif return 0; } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件