From 1e55d01f11167afd07aed3578713bb5f5f9a6249 Mon Sep 17 00:00:00 2001 From: jerryzeng Date: Sun, 8 Jun 2025 11:08:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + bagPositioning/bagPositioning.vcxproj | 172 +++ baseAlgorithm/baseAlgorithm.vcxproj | 176 +++ .../beltTearingDetection.vcxproj | 172 +++ .../beltTearingDetection_test.cpp | 1174 +++++++++++++++++ .../beltTearingDetection_test.vcxproj | 160 +++ boxCarMeasure/boxCarMeasureDll.vcxproj | 173 +++ boxCarMeasure_test/boxCarMeasure.vcxproj | 158 +++ boxCarMeasure_test/boxCarMeasureTest.cpp | 983 ++++++++++++++ 9 files changed, 3169 insertions(+) create mode 100644 bagPositioning/bagPositioning.vcxproj create mode 100644 baseAlgorithm/baseAlgorithm.vcxproj create mode 100644 beltTearingDetection/beltTearingDetection.vcxproj create mode 100644 beltTearingDetection_test/beltTearingDetection_test.cpp create mode 100644 beltTearingDetection_test/beltTearingDetection_test.vcxproj create mode 100644 boxCarMeasure/boxCarMeasureDll.vcxproj create mode 100644 boxCarMeasure_test/boxCarMeasure.vcxproj create mode 100644 boxCarMeasure_test/boxCarMeasureTest.cpp diff --git a/.gitignore b/.gitignore index ee13ad2..d50d174 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ *.db *.aps *.user +*.filters **/build \ No newline at end of file diff --git a/bagPositioning/bagPositioning.vcxproj b/bagPositioning/bagPositioning.vcxproj new file mode 100644 index 0000000..1befe93 --- /dev/null +++ b/bagPositioning/bagPositioning.vcxproj @@ -0,0 +1,172 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {ad9c5f88-3ba7-4475-92b7-0b8fdc7216e5} + bagPositioning + 10.0 + + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + ..\..\..\codeHub\VzNLSDK\Inc;..\..\..\codeHub\opencv\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + $(SolutionDir)build\$(Platform)\$(Configuration)\ + + + false + ..\..\..\codeHub\VzNLSDK\Inc;..\..\..\codeHub\opencv\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + $(SolutionDir)build\$(Platform)\$(Configuration)\ + + + + Level3 + true + WIN32;_DEBUG;BAGPOSITIONING_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;BAGPOSITIONING_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + SG_API_LIBRARY;_DEBUG;BAGPOSITIONING_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\..\codeHub\opencv\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + false + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Debug;%(AdditionalLibraryDirectories) + opencv_world480d.lib;baseAlgorithm.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + SG_API_LIBRARY;NDEBUG;BAGPOSITIONING_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\..\codeHub\opencv\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + false + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Release;%(AdditionalLibraryDirectories) + opencv_world480.lib;baseAlgorithm.lib;%(AdditionalDependencies) + + + + + + + + + + + + \ No newline at end of file diff --git a/baseAlgorithm/baseAlgorithm.vcxproj b/baseAlgorithm/baseAlgorithm.vcxproj new file mode 100644 index 0000000..4676aa0 --- /dev/null +++ b/baseAlgorithm/baseAlgorithm.vcxproj @@ -0,0 +1,176 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {95dc3f1a-902a-490e-bd3b-b10463cf0ebd} + baseAlgorithm + 10.0 + + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + ..\..\..\codeHub\VzNLSDK\Inc;..\..\..\..\codeHub\opencv\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + $(SolutionDir)build\$(Platform)\$(Configuration)\ + + + false + ..\..\..\codeHub\VzNLSDK\Inc;..\..\..\..\codeHub\opencv\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + $(SolutionDir)build\$(Platform)\$(Configuration)\ + + + + Level3 + true + WIN32;_DEBUG;BASEALGORITHM_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;BASEALGORITHM_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + SG_API_LIBRARY;_DEBUG;BASEALGORITHM_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\..\codeHub\opencv\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + false + ..\..\..\codeHub\opencv\build\x64\vc16\lib;%(AdditionalLibraryDirectories) + opencv_world480d.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + SG_API_LIBRARY;NDEBUG;BASEALGORITHM_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\..\codeHub\opencv\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + false + ..\..\..\codeHub\opencv\build\x64\vc16\lib;%(AdditionalLibraryDirectories) + opencv_world480.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/beltTearingDetection/beltTearingDetection.vcxproj b/beltTearingDetection/beltTearingDetection.vcxproj new file mode 100644 index 0000000..134e399 --- /dev/null +++ b/beltTearingDetection/beltTearingDetection.vcxproj @@ -0,0 +1,172 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 16.0 + Win32Proj + {564c255d-40c0-4749-ac5c-410dbe0f135f} + beltTearingDetection + 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)\ + ..\..\..\codeHub\VzNLSDK\Inc;..\..\..\..\codeHub\opencv\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + + + false + $(SolutionDir)build\$(Platform)\$(Configuration)\ + ..\..\..\codeHub\VzNLSDK\Inc;..\..\..\..\codeHub\opencv\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + + + + Level3 + true + WIN32;_DEBUG;BELTTEARINGDETECTION_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;BELTTEARINGDETECTION_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + _DEBUG;BELTTEARINGDETECTION_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\..\codeHub\opencv\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + false + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Debug;%(AdditionalLibraryDirectories) + opencv_world480d.lib;baseAlgorithm.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;BELTTEARINGDETECTION_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\..\codeHub\opencv\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + false + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Release;%(AdditionalLibraryDirectories) + opencv_world480.lib;baseAlgorithm.lib;%(AdditionalDependencies) + + + + + + \ No newline at end of file diff --git a/beltTearingDetection_test/beltTearingDetection_test.cpp b/beltTearingDetection_test/beltTearingDetection_test.cpp new file mode 100644 index 0000000..b94371a --- /dev/null +++ b/beltTearingDetection_test/beltTearingDetection_test.cpp @@ -0,0 +1,1174 @@ +// motorStatorPosition_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 +// + +#include +#include +#include +#include +#include +#include "direct.h" +#include +#include "beltTearingDetection_Export.h" +#include +#include + +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; + +} + +#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) +{ + 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)) + { + dataFileVer = DATA_VER_NEW; + 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 (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 = (SVzNLXYZRGBDLaserLine*)malloc(sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); + memset(_scanLines, 0, sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); + if (scanLineNum) + *scanLineNum = lines; + } + if (_scanLines == NULL) + return NULL; + + int ptNum = 0; + int lineIdx = -1; + int ptIdx = 0; + SVzNLPointXYZRGBA* 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 = (SVzNLPointXYZRGBA*)malloc(sizeof(SVzNLPointXYZRGBA) * ptNum); + memset(p3DPoint, 0, sizeof(SVzNLPointXYZRGBA) * ptNum); + } + else + p3DPoint = NULL; + _scanLines[lineIdx].nPointCnt = 0; + _scanLines[lineIdx].nTimeStamp = timeStamp; + _scanLines[lineIdx].p3DPoint = p3DPoint; + + } + else if (0 == strncmp("{", linedata.c_str(), 1)) + { + float X, Y, Z; + int imageY = 0; + float leftX, leftY; + float rightX, rightY; + int r = -1, g = -1, b = -1; + 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].nPointCnt; + if (id < ptNum) + { + p3DPoint[id].x = X; + p3DPoint[id].y = Y; + p3DPoint[id].z = Z; + p3DPoint[id].nRGB = 0; + _scanLines[lineIdx].nPointCnt = 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 = (SVzNLPointXYZRGBA*)malloc(sizeof(SVzNLPointXYZRGBA) * VZ_LASER_LINE_PT_MAX_NUM); + memset(p3DPoint, 0, sizeof(SVzNLPointXYZRGBA) * VZ_LASER_LINE_PT_MAX_NUM); + _scanLines[lineIdx].nPointCnt = 0; + _scanLines[lineIdx].nTimeStamp = timeStamp; + _scanLines[lineIdx].p3DPoint = 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].nPointCnt; + if (id < VZ_LASER_LINE_PT_MAX_NUM) + { + p3DPoint[id].x = X; + p3DPoint[id].y = Y; + p3DPoint[id].z = Z; + p3DPoint[id].nRGB = 0; + _scanLines[lineIdx].nPointCnt = id + 1; + } + } + } + } + inputFile.close(); + return _scanLines; +} + +SVzNL3DLaserLine* _convertToGridData_XYZRGB(SVzNLXYZRGBDLaserLine* laser3DPoints, int lineNum, double _F, double* camPoseR, int* outLineNum) +{ + int min_y = 100000000; + int max_y = -10000000; + int validStartLine = -1; + int validEndLine = -1; + for (int line = 0; line < lineNum; line++) + { + if (laser3DPoints[line].nPointCnt > 0) + { + if (validStartLine < 0) + { + validStartLine = line; + validEndLine = line; + } + else + validEndLine = line; + } + + for (int i = 0; i < laser3DPoints[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA* a_pt = &laser3DPoints[line].p3DPoint[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 NULL; + + int vldLineNum = validEndLine - validStartLine + 1; + *outLineNum = vldLineNum; + int pt_counter = max_y - min_y + 1; + SVzNL3DLaserLine* gridData = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); + memset(gridData, 0, sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); + for (int line = validStartLine; line <= validEndLine; line++) + { + int gridLine = line - validStartLine; + gridData[gridLine].nPositionCnt = pt_counter; + gridData[gridLine].nTimeStamp = laser3DPoints[line].nTimeStamp; + gridData[gridLine].p3DPosition = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * pt_counter); + memset(gridData[gridLine].p3DPosition, 0, sizeof(SVzNL3DPosition) * pt_counter); + for (int i = 0; i < laser3DPoints[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA a_pt = laser3DPoints[line].p3DPoint[i]; + if (a_pt.z > 1e-4) + { + int pt_id = a_pt.nRGB - min_y; + SVzNL3DPoint tmp_pt = { a_pt.x, a_pt.y, a_pt.z }; + SVzNL3DPoint r_pt = _ptRotate(tmp_pt, camPoseR); + gridData[gridLine].p3DPosition[pt_id].pt3D.x = r_pt.x; + gridData[gridLine].p3DPosition[pt_id].pt3D.y = r_pt.y; + gridData[gridLine].p3DPosition[pt_id].pt3D.z = r_pt.z; + } + } + } + return gridData; +} + +SVzNL3DLaserLine* _yRngFilterData_XYZRGB(SVzNLXYZRGBDLaserLine* laser3DPoints, int lineNum, double y_min, double y_max, int* outLineNum) +{ + int validStartLine = -1; + int validEndLine = -1; + for (int line = 0; line < lineNum; line++) + { + if (laser3DPoints[line].nPointCnt > 0) + { + if (validStartLine < 0) + { + validStartLine = line; + validEndLine = line; + } + else + validEndLine = line; + } + } + + int vldLineNum = validEndLine - validStartLine + 1; + *outLineNum = vldLineNum; + SVzNL3DLaserLine* filterData = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); + memset(filterData, 0, sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); + for (int line = validStartLine; line <= validEndLine; line++) + { + std::vector vldPts; + for (int i = 0; i < laser3DPoints[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA a_pt = laser3DPoints[line].p3DPoint[i]; + if ((a_pt.z > 1e-4) && (a_pt.y >= y_min) && (a_pt.y <= y_max)) + { + vldPts.push_back(a_pt); + } + } + int filterLine = line - validStartLine; + int ptNum = vldPts.size(); + filterData[filterLine].nPositionCnt = ptNum; + filterData[filterLine].nTimeStamp = laser3DPoints[line].nTimeStamp; + filterData[filterLine].p3DPosition = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * ptNum); + memset(filterData[filterLine].p3DPosition, 0, sizeof(SVzNL3DPosition) * ptNum); + for(int i = 0, i_max = vldPts.size(); i < i_max; i ++) + { + filterData[filterLine].p3DPosition[i].nPointIdx = i; + filterData[filterLine].p3DPosition[i].pt3D.x = vldPts[i].x; + filterData[filterLine].p3DPosition[i].pt3D.y = vldPts[i].y; + filterData[filterLine].p3DPosition[i].pt3D.z = vldPts[i].z; + } + } + return filterData; +} + +SVzNL3DLaserLine* _linesDownSample_XYZRGB(SVzNLXYZRGBDLaserLine* laser3DPoints, int lineNum, int* outLineNum) +{ + int validStartLine = -1; + int validEndLine = -1; + for (int line = 0; line < lineNum; line++) + { + if (laser3DPoints[line].nPointCnt > 0) + { + if (validStartLine < 0) + { + validStartLine = line; + validEndLine = line; + } + else + validEndLine = line; + } + } + + int vldLineNum = (validEndLine - validStartLine + 1)/2; + *outLineNum = vldLineNum; + SVzNL3DLaserLine* filterData = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); + memset(filterData, 0, sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); + int totalLines = 0; + for (int line = validStartLine; line <= validEndLine; line+=2) + { + if (totalLines >= vldLineNum) + break; + std::vector vldPts; + for (int i = 0; i < laser3DPoints[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA a_pt = laser3DPoints[line].p3DPoint[i]; + if (a_pt.z > 1e-4) + { + vldPts.push_back(a_pt); + } + } + int filterLine = totalLines; + int ptNum = vldPts.size(); + filterData[filterLine].nPositionCnt = ptNum; + filterData[filterLine].nTimeStamp = laser3DPoints[line].nTimeStamp; + filterData[filterLine].p3DPosition = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * ptNum); + memset(filterData[filterLine].p3DPosition, 0, sizeof(SVzNL3DPosition) * ptNum); + for (int i = 0, i_max = vldPts.size(); i < i_max; i++) + { + filterData[filterLine].p3DPosition[i].nPointIdx = i; + filterData[filterLine].p3DPosition[i].pt3D.x = vldPts[i].x; + filterData[filterLine].p3DPosition[i].pt3D.y = vldPts[i].y; + filterData[filterLine].p3DPosition[i].pt3D.z = vldPts[i].z; + } + totalLines++; + } + return filterData; +} + +void _outputScanDataFile_self(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++) + { + sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << scanData[line].nPositionCnt << std::endl; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + 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(); +} + +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(); +} + +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; +} + +typedef struct +{ + int r; + int g; + int b; +}SG_color; +void _outputScanDataFile_RGBD_obj(char* fileName, SVzNL3DLaserLine* scanData, int lineNum, + float lineV, int maxTimeStamp, int clockPerSecond, + std::vector& total_tearings) +{ + std::ofstream sw(fileName); + int realLines = lineNum; + if (total_tearings.size() > 0) + realLines += 1; + + 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].nPositionCnt << 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; + + 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 (total_tearings.size() > 0) + { + int ptNum = 0; + for (int id = 0; id < total_tearings.size(); id++) + { + SSG_beltTearingInfo* a_tear = &total_tearings[id]; + ptNum += total_tearings[id].pts.size(); + } + sw << "Line_" << lineNum << "_" << nTimeStamp+1000 << "_" << ptNum << std::endl; + + for (int id = 0; id < total_tearings.size(); id++) + { + SSG_beltTearingInfo* a_tear = &total_tearings[id]; + for (int i = 0; i < a_tear->pts.size(); i++) + { + int objID = a_tear->pts[i].nPointIdx; + rgb = objColor[objID % 8]; + size = 3; + float x = (float)a_tear->pts[i].pt3D.x; + float y = (float)a_tear->pts[i].pt3D.y; + float z = (float)a_tear->pts[i].pt3D.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 _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 _XOYprojection(cv::Mat& img, + std::vector>& dataLines, + const double x_scale, const double y_scale, + const SVzNLRangeD x_range, + const SVzNLRangeD y_range, + std::vector& total_tearings) +{ + 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; + + rgb = { 200, 200, 200 }; + 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); + } + } + for (int i = 0, i_max = total_tearings.size(); i < i_max; i++) + { + SSG_beltTearingInfo* a_obj = &total_tearings[i]; + rgb = objColor[i % 8]; + int size = 3; + for (int j = 0, j_max = a_obj->pts.size(); j < j_max; j++) + { + double x = a_obj->pts[j].pt3D.x; + double y = a_obj->pts[j].pt3D.y; + int px = (int)((x - x_range.min) / x_scale + x_skip); + int py = (int)((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); + } + //画ROI + cv::Point2d vec2d[4]; + vec2d[0].x = a_obj->roi.left; vec2d[0].y = a_obj->roi.top; + vec2d[1].x = a_obj->roi.right; vec2d[1].y = a_obj->roi.top; + vec2d[2].x = a_obj->roi.right; vec2d[2].y = a_obj->roi.bottom; + vec2d[3].x = a_obj->roi.left; vec2d[3].y = a_obj->roi.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); + } + } +} + +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 _genXOYProjectionImage(cv::String& fileName, SVzNL3DLaserLine* scanData, int lineNum, + std::vector& total_tearings) +{ + //统计X和Y的范围 + 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 = 0.5; + double y_scale = 0.5; + double x_rows = (x_range.max - x_range.min) / x_scale; + double y_rows = (y_range.max - y_range.min) / y_scale; + int imgRows = (int)(y_rows + 0.5); + if (imgRows % 2 != 0) + imgRows += 1; + int imgCols = (int)(x_rows + 0.5); + if (imgCols % 2 != 0) + imgCols += 1; + + imgRows += 32; + imgCols += 32; + cv::Mat img = cv::Mat::zeros(imgRows, imgCols, CV_8UC3); + _XOYprojection(img, scan_lines, x_scale, y_scale, x_range, y_range, total_tearings); + cv::imwrite(fileName, img); + return; +} + +void _outputObjInfo(char* fileName, std::vector& total_tearings) +{ + std::ofstream sw(fileName); + int objNum = total_tearings.size(); + sw << "撕裂数目:" << objNum << std::endl; + for (int i = 0; i < objNum; i++) + { + sw << " obj_" << i << std::endl; + sw << " Depth=" << total_tearings[i].tearDepth << std::endl; + sw << " Width=" << total_tearings[i].tearWidth << std::endl; + sw << " ROI={" << total_tearings[i].roi.left << "," << total_tearings[i].roi.right << "," << total_tearings[i].roi.top << "," << total_tearings[i].roi.bottom << "}" << std::endl; + sw << " start=" << total_tearings[i].statLineIdx << ", end=" << total_tearings[i].endLineIdx << std::endl; + } + sw.close(); +} + +#define TEST_GROUP 3 +int main() +{ +#if 0 +#if 1 + //将数据转换成栅格格式格式 + 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:\\上古\\皮带撕裂点云\\SaveData\\"); + char _scan_file[256]; + double _F = 983.42; //f + for (int i = 11; i <= 11; i++) + { + sprintf_s(_scan_file, "%sSaveData_%d.txt", _scan_dir, i); + SVzNLXYZRGBDLaserLine* laser3DPoints_RGBD = vzReadLaserScanPointFromFile_XYZRGB(_scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); + if (laser3DPoints_RGBD == NULL) + continue; + + double camPoseR[9] = { + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0 }; + + int vldLineNum = 0; + SVzNL3DLaserLine* gridData = _convertToGridData_XYZRGB(laser3DPoints_RGBD, lineNum, _F, camPoseR, &vldLineNum); + //生成水平扫描数据 + int hLineNum = gridData[0].nPositionCnt; + SVzNL3DLaserLine* hScanData = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * hLineNum); + memset(hScanData, 0, sizeof(SVzNL3DLaserLine) * hLineNum); + for (int hLine = 0; hLine < hLineNum; hLine++) + { + hScanData[hLine].nPositionCnt = lineNum; + hScanData[hLine].nTimeStamp = hLine * 1000; + hScanData[hLine].p3DPosition = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * lineNum); + memset(hScanData[hLine].p3DPosition, 0, sizeof(SVzNL3DPosition) * lineNum); + for (int m = 0; m < lineNum; m++) + { + hScanData[hLine].p3DPosition[m].nPointIdx = m; + hScanData[hLine].p3DPosition[m].pt3D = gridData[m].p3DPosition[hLine].pt3D; + } + } + char _out_file[256]; + sprintf_s(_out_file, "%sLaserLine%d_grid.txt", _scan_dir, i); + _outputScanDataFile_self(_out_file, gridData, vldLineNum, + lineV, maxTimeStamp, clockPerSecond); + //sprintf_s(_out_file, "%sLaserLine%d_hScanData.txt", _scan_dir, i); + //_outputScanDataFile_removeZeros(_out_file, hScanData, hLineNum, + // lineV, maxTimeStamp, clockPerSecond); + printf("%s: convert done!\n", _scan_file); + } +#else +#if 0 + //点云过滤Y范围过滤 + //double y_min = -210.0, y_max = 50; //正面3 + //double y_min = -178.0, y_max = 90; //正面4 + //double y_min = -180.0, y_max = 60; //侧面3 + double y_min = -190.0, y_max = 70; //侧面4 + char _scan_file[256]; + int lineNum = 0; + float lineV = 0.0f; + int dataCalib = 0; + int maxTimeStamp = 0; + int clockPerSecond = 0; + sprintf_s(_scan_file, "F:\\3D数据\\铸造件(1)\\铸造件\\侧面\\侧面4.txt"); + SVzNLXYZRGBDLaserLine* laser3DPoints_RGBD = vzReadLaserScanPointFromFile_XYZRGB(_scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); + + int vldLineNum = 0; + SVzNL3DLaserLine* filterData = _yRngFilterData_XYZRGB(laser3DPoints_RGBD, lineNum, y_min, y_max, &vldLineNum); + + char _out_file[256]; + sprintf_s(_out_file, "F:\\3D数据\\铸造件(1)\\铸造件\\侧面\\侧面4_filter.txt"); + _outputScanDataFile_self(_out_file, filterData, vldLineNum, + lineV, maxTimeStamp, clockPerSecond); + printf("%s: convert done!\n", _scan_file); +#else + char _scan_file[256]; + int lineNum = 0; + float lineV = 0.0f; + int dataCalib = 0; + int maxTimeStamp = 0; + int clockPerSecond = 0; + sprintf_s(_scan_file, "F:\\客户\\天津阳润\\b侧替换标定数据\\004-b-u.txt"); + SVzNLXYZRGBDLaserLine* laser3DPoints_RGBD = vzReadLaserScanPointFromFile_XYZRGB(_scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); + + int vldLineNum = 0; + SVzNL3DLaserLine* filterData = _linesDownSample_XYZRGB(laser3DPoints_RGBD, lineNum, &vldLineNum); + + char _out_file[256]; + sprintf_s(_out_file, "F:\\客户\\天津阳润\\b侧替换标定数据\\004-b-u-filter.txt"); + _outputScanDataFile_self(_out_file, filterData, vldLineNum, + lineV, maxTimeStamp, clockPerSecond); + printf("%s: convert done!\n", _scan_file); +#endif +#endif +#else + + const char* dataPath[TEST_GROUP] = { + "F:\\上古\\皮带撕裂点云\\1-500-2000\\", //0 + "F:\\上古\\皮带撕裂点云\\2-1700-2000\\", //1 + "F:\\上古\\皮带撕裂点云\\SaveData\\" + }; + + SVzNLRange fileIdx[TEST_GROUP] = { + {10,24}, {2, 18}, {15,15} }; + + double camPoseR[9] = { + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0 }; + + SSG_beltTearingParam algoParam; + algoParam.differnceBinTh = 1.0; + algoParam.extractPara.gapChkWin = 7; + algoParam.extractPara.sameGapTh = 20.0; + algoParam.scanXScale = 4.0; + algoParam.scanYScale = 0.6; + algoParam.tearingMinGap = 50.0; //两个同位置的纵撕的最小间隔。大于此门限视为两个撕裂 + algoParam.tearingMinLen = 30.0; + + char _scan_file[256]; + int endGroup = TEST_GROUP - 1; + for (int grp = 2; grp <= endGroup; grp++) + { + for (int fidx = fileIdx[grp].nMin; fidx <= fileIdx[grp].nMax; fidx++) + { + //fidx = 4; + int lineNum = 0; + float lineV = 0.0f; + int dataCalib = 0; + int maxTimeStamp = 0; + int clockPerSecond = 0; + sprintf_s(_scan_file, "%sLaserLine%d_grid.txt", dataPath[grp], fidx); + SVzNL3DLaserLine* laser3DPoints = vzReadLaserScanPointFromFile_XYZ(_scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); + if (laser3DPoints == NULL) + continue; + + std::vector total_tearings; + + printf("%s: \n", _scan_file); + int errCode = 0; + int nPointNum = laser3DPoints->nPositionCnt; + std::vector hLineWorkers; + hLineWorkers.resize(nPointNum); + std::vector beltTearings_new; + std::vector beltTearings_growing; + std::vector beltTearings_ended; + std::vector beltTearings_unknown; //未判明,应用无需处理。 + int lineIdx = 0; + long t1 = GetTickCount64(); + + beltTearings_new.clear(); + beltTearings_new.shrink_to_fit(); + beltTearings_growing.clear(); + beltTearings_growing.shrink_to_fit(); + beltTearings_ended.clear(); + beltTearings_ended.shrink_to_fit(); + beltTearings_unknown.clear(); + beltTearings_unknown.shrink_to_fit(); + sg_detectBeltTearing( + NULL, //空扫描线,用于复位内部静态变量 + 0, + 0, + &errCode, + hLineWorkers, + beltTearings_new, + beltTearings_growing, + beltTearings_ended, + beltTearings_unknown, //未判明,应用无需处理。 + algoParam); + + for (int lineIdx = 0; lineIdx < lineNum; lineIdx++) + { + if ((lineIdx == 56) || (lineIdx == 857)) + int kkk = 1; + SVzNL3DLaserLine* a_line = &laser3DPoints[lineIdx]; + sg_detectBeltTearing( + a_line, //一条扫描线 + lineIdx, + a_line->nPositionCnt, + &errCode, + hLineWorkers, + beltTearings_new, + beltTearings_growing, + beltTearings_ended, + beltTearings_unknown, //未判明,应用无需处理。 + algoParam); + + long t2 = GetTickCount64(); + if (beltTearings_new.size() > 0) + int kkk = 1; + int newTearNum = beltTearings_new.size(); + if (newTearNum > 0) + printf(" line_%d: %d(ms), newTearNum=%d!\n", lineIdx, (int)(t2 - t1), newTearNum); +#if OUTPUT_TEARING_POINTS + if (beltTearings_ended.size() > 0) //收集撕裂点 + { + total_tearings.insert(total_tearings.end(), beltTearings_ended.begin(), beltTearings_ended.end()); + } + else if (lineIdx == lineNum - 1) //测试数据最后一条扫描线。(实际中没有) + { + total_tearings.insert(total_tearings.end(), beltTearings_growing.begin(), beltTearings_growing.end()); + } +#endif + } + + printf(" totalTearNum=%d!\n", (int)total_tearings.size()); + char _dbg_file[256]; +#if 1 + sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result.txt", dataPath[grp], fidx); + _outputScanDataFile_RGBD_obj(_dbg_file, laser3DPoints, lineNum, lineV, maxTimeStamp, clockPerSecond, total_tearings); + + sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result_img.png", dataPath[grp], fidx); + cv::String imgName(_dbg_file); + double rpy[3] = { 0, 0, 0 }; //{ 0,-45, 0 }; // + _genXOYProjectionImage(imgName, laser3DPoints, lineNum, total_tearings); + sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result_info.txt", dataPath[grp], fidx); + _outputObjInfo(_dbg_file, total_tearings); +#endif + + } + } + +#endif + printf("all done!\n"); +} + diff --git a/beltTearingDetection_test/beltTearingDetection_test.vcxproj b/beltTearingDetection_test/beltTearingDetection_test.vcxproj new file mode 100644 index 0000000..3bdbb55 --- /dev/null +++ b/beltTearingDetection_test/beltTearingDetection_test.vcxproj @@ -0,0 +1,160 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {ad67bc71-0a80-4c85-931e-5d76dd8479e5} + beltTearingDetectiontest + 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)\ + ..\..\..\codeHub\VzNLSDK\Inc;..\sourceCode;..\sourceCode\inc;$(IncludePath) + + + false + $(SolutionDir)build\$(Platform)\$(Configuration)\ + ..\..\..\codeHub\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 + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\..\codeHub\opencv\build\include; + + + Console + true + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Debug;%(AdditionalLibraryDirectories) + opencv_world480d.lib;beltTearingDetection.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\..\codeHub\opencv\build\include; + + + Console + true + true + true + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Release;%(AdditionalLibraryDirectories) + opencv_world480.lib;beltTearingDetection.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + + + + + + + + \ No newline at end of file diff --git a/boxCarMeasure/boxCarMeasureDll.vcxproj b/boxCarMeasure/boxCarMeasureDll.vcxproj new file mode 100644 index 0000000..8d90cfa --- /dev/null +++ b/boxCarMeasure/boxCarMeasureDll.vcxproj @@ -0,0 +1,173 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 16.0 + Win32Proj + {2229d085-7b79-4ca8-bcb3-dc1ba7ad6a1e} + boxCarMeasureDll + 10.0 + boxCarMeasure + + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + ..\..\..\codeHub\VzNLSDK\Inc;..\..\..\..\codeHub\opencv\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + $(SolutionDir)build\$(Platform)\$(Configuration)\ + + + false + ..\..\..\codeHub\VzNLSDK\Inc;..\..\..\..\codeHub\opencv\build\include;..\sourceCode;..\sourceCode\inc;$(IncludePath) + $(SolutionDir)build\$(Platform)\$(Configuration)\ + + + + Level3 + true + WIN32;_DEBUG;BOXCARMEASUREDLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;BOXCARMEASUREDLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + SG_API_LIBRARY;_DEBUG;BAGPOSITIONING_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\..\codeHub\opencv\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + false + opencv_world480d.lib;baseAlgorithm.lib;%(AdditionalDependencies) + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Debug;%(AdditionalLibraryDirectories) + + + + + Level3 + true + true + true + SG_API_LIBRARY;NDEBUG;BAGPOSITIONING_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + NotUsing + pch.h + ..\..\..\codeHub\opencv\build\include;%(AdditionalIncludeDirectories) + + + Windows + true + true + true + false + opencv_world480.lib;baseAlgorithm.lib;%(AdditionalDependencies) + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Release;%(AdditionalLibraryDirectories) + + + + + + \ No newline at end of file diff --git a/boxCarMeasure_test/boxCarMeasure.vcxproj b/boxCarMeasure_test/boxCarMeasure.vcxproj new file mode 100644 index 0000000..5b0a835 --- /dev/null +++ b/boxCarMeasure_test/boxCarMeasure.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {8a43b8a1-1df4-4a91-8fcf-b7518a54e3c9} + boxCarMeasure + 10.0 + boxCarMeasureTest + + + + 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)\ + ..\..\..\codeHub\VzNLSDK\Inc;..\sourceCode;..\sourceCode\inc;$(IncludePath) + + + false + $(SolutionDir)build\$(Platform)\$(Configuration)\ + ..\..\..\codeHub\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 + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\..\codeHub\opencv\build\include; + + + Console + true + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Debug;%(AdditionalLibraryDirectories) + opencv_world480d.lib;boxCarMeasure.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\..\codeHub\opencv\build\include; + + + Console + true + true + true + ..\..\..\codeHub\opencv\build\x64\vc16\lib;..\build\x64\Release;%(AdditionalLibraryDirectories) + opencv_world480.lib;boxCarMeasure.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/boxCarMeasure_test/boxCarMeasureTest.cpp b/boxCarMeasure_test/boxCarMeasureTest.cpp new file mode 100644 index 0000000..088d8a6 --- /dev/null +++ b/boxCarMeasure_test/boxCarMeasureTest.cpp @@ -0,0 +1,983 @@ +// boxCarMeasure.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 +// + +#include +#include +#include +#include +#include +#include "direct.h" +#include +#include "boxCarMeasure_Export.h" +#include +#include + +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; + +} + +#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) +{ + 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)) + { + dataFileVer = DATA_VER_NEW; + 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 (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 = (SVzNLXYZRGBDLaserLine*)malloc(sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); + memset(_scanLines, 0, sizeof(SVzNLXYZRGBDLaserLine) * (lineNum + 1)); + if (scanLineNum) + *scanLineNum = lines; + } + if (_scanLines == NULL) + return NULL; + + int ptNum = 0; + int lineIdx = -1; + int ptIdx = 0; + SVzNLPointXYZRGBA* 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 = (SVzNLPointXYZRGBA*)malloc(sizeof(SVzNLPointXYZRGBA) * ptNum); + memset(p3DPoint, 0, sizeof(SVzNLPointXYZRGBA) * ptNum); + } + else + p3DPoint = NULL; + _scanLines[lineIdx].nPointCnt = 0; + _scanLines[lineIdx].nTimeStamp = timeStamp; + _scanLines[lineIdx].p3DPoint = p3DPoint; + + } + else if (0 == strncmp("{", linedata.c_str(), 1)) + { + float X, Y, Z; + int imageY = 0; + float leftX, leftY; + float rightX, rightY; + int r = -1, g = -1, b = -1; + 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].nPointCnt; + if (id < ptNum) + { + p3DPoint[id].x = X; + p3DPoint[id].y = Y; + p3DPoint[id].z = Z; + p3DPoint[id].nRGB = 0; + _scanLines[lineIdx].nPointCnt = 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 = (SVzNLPointXYZRGBA*)malloc(sizeof(SVzNLPointXYZRGBA) * VZ_LASER_LINE_PT_MAX_NUM); + memset(p3DPoint, 0, sizeof(SVzNLPointXYZRGBA) * VZ_LASER_LINE_PT_MAX_NUM); + _scanLines[lineIdx].nPointCnt = 0; + _scanLines[lineIdx].nTimeStamp = timeStamp; + _scanLines[lineIdx].p3DPoint = 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].nPointCnt; + if (id < VZ_LASER_LINE_PT_MAX_NUM) + { + p3DPoint[id].x = X; + p3DPoint[id].y = Y; + p3DPoint[id].z = Z; + p3DPoint[id].nRGB = 0; + _scanLines[lineIdx].nPointCnt = id + 1; + } + } + } + } + inputFile.close(); + return _scanLines; +} + +SVzNL3DLaserLine* _convertToGridData_XYZRGB(SVzNLXYZRGBDLaserLine* laser3DPoints, int lineNum, double _F, double* camPoseR, int* outLineNum) +{ + int min_y = 100000000; + int max_y = -10000000; + int validStartLine = -1; + int validEndLine = -1; + for (int line = 0; line < lineNum; line++) + { + if (laser3DPoints[line].nPointCnt > 0) + { + if (validStartLine < 0) + { + validStartLine = line; + validEndLine = line; + } + else + validEndLine = line; + } + + for (int i = 0; i < laser3DPoints[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA* a_pt = &laser3DPoints[line].p3DPoint[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 NULL; + + int vldLineNum = validEndLine - validStartLine + 1; + *outLineNum = vldLineNum; + int pt_counter = max_y - min_y + 1; + SVzNL3DLaserLine* gridData = (SVzNL3DLaserLine*)malloc(sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); + memset(gridData, 0, sizeof(SVzNL3DLaserLine) * (vldLineNum + 1)); + for (int line = validStartLine; line <= validEndLine; line++) + { + int gridLine = line - validStartLine; + gridData[gridLine].nPositionCnt = pt_counter; + gridData[gridLine].nTimeStamp = laser3DPoints[line].nTimeStamp; + gridData[gridLine].p3DPosition = (SVzNL3DPosition*)malloc(sizeof(SVzNL3DPosition) * pt_counter); + memset(gridData[gridLine].p3DPosition, 0, sizeof(SVzNL3DPosition) * pt_counter); + for (int i = 0; i < laser3DPoints[line].nPointCnt; i++) + { + SVzNLPointXYZRGBA a_pt = laser3DPoints[line].p3DPoint[i]; + if (a_pt.z > 1e-4) + { + int pt_id = a_pt.nRGB - min_y; + SVzNL3DPoint tmp_pt = { a_pt.x, a_pt.y, a_pt.z }; + SVzNL3DPoint r_pt = _ptRotate(tmp_pt, camPoseR); + gridData[gridLine].p3DPosition[pt_id].pt3D.x = r_pt.x; + gridData[gridLine].p3DPosition[pt_id].pt3D.y = r_pt.y; + gridData[gridLine].p3DPosition[pt_id].pt3D.z = r_pt.z; + } + } + } + return gridData; +} + +void _outputScanDataFile_self(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++) + { + sw << "Line_" << line << "_" << scanData[line].nTimeStamp << "_" << scanData[line].nPositionCnt << std::endl; + for (int i = 0; i < scanData[line].nPositionCnt; i++) + { + 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(); +} + +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; +} + +typedef struct +{ + int r; + int g; + int b; +}SG_color; +void _outputScanDataFile_RGBD_obj(char* fileName, SVzNL3DLaserLine* scanData, int lineNum, + float lineV, int maxTimeStamp, int clockPerSecond, SSG_boxCarDimension* dimen) +{ + std::ofstream sw(fileName); + int realLines = lineNum; + if (dimen) + realLines += 1; + + 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].nPositionCnt << 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 >> 8) & 0xff; + vType = vType & 0x0f; + if (LINE_FEATURE_RIGHT_ANGLE_HR == vType) + { + rgb = { 255, 97, 0 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_HF == vType) + { + rgb = { 255, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_RH == vType) + { + rgb = { 255, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_FH == vType) + { + rgb = { 160, 82, 45 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_HR == hType) + { + rgb = { 0, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_HF == hType) + { + rgb = { 0, 255, 255 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_RH == hType) + { + rgb = { 0, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_FH == hType) + { + rgb = { 85, 107, 47 }; + size = 3; + } + 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 (dimen) + { + int ptNum = 4; + sw << "Line_" << lineNum << "_" << (nTimeStamp + 1000) << "_" << ptNum << std::endl; + for (int i = 0; i < 4; i++) + { + rgb = { 255, 0, 0 }; + size = 25; + float x = (float)dimen->endings[i].x; + float y = (float)dimen->endings[i].y; + float z = (float)dimen->endings[i].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 _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 _XOYprojection(cv::Mat& img, std::vector>& dataLines, + 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 >> 8) & 0xff; + vType = vType & 0x0f; + if (LINE_FEATURE_RIGHT_ANGLE_HR == vType) + { + rgb = { 255, 97, 0 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_HF == vType) + { + rgb = { 255, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_RH == vType) + { + rgb = { 255, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_FH == vType) + { + rgb = { 160, 82, 45 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_HR == hType) + { + rgb = { 0, 0, 255 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_HF == hType) + { + rgb = { 0, 255, 255 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_RH == hType) + { + rgb = { 0, 255, 0 }; + size = 3; + } + else if (LINE_FEATURE_RIGHT_ANGLE_FH == hType) + { + rgb = { 85, 107, 47 }; + size = 3; + } + else + { + rgb = { 200, 200, 200 }; + 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); + } + } +} + +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 _genXOYProjectionImage(cv::String& fileName, SVzNL3DLaserLine* scanData, int lineNum, double rpy[3]) +{ + //统计X和Y的范围 + 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; + _XOYprojection(img, scan_lines, x_scale, y_scale, x_range, y_range); + + //旋转视角显示 + double matrix3d[9]; + EulerRpyToRotation1(rpy, matrix3d); + + 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, rx_scale, ry_scale, rx_range, ry_range); + + cv::Mat dis_img; + cv::hconcat(img, r_img, dis_img); + cv::imwrite(fileName, dis_img); + return; +} + +#define TEST_GROUP 1 +int main() +{ +#if 0 + //将数据转换成栅格格式格式 + char _scan_dir[256]; + int lineNum = 0; + float lineV = 0.0f; + int dataCalib = 0; + int maxTimeStamp = 0; + int clockPerSecond = 0; + sprintf_s(_scan_dir, "E:\\上古\\车厢动态扫描\\"); + char _scan_file[256]; + double _F = 1210.06; //f + for (int i = 1; i <= 5; i++) + { + sprintf_s(_scan_file, "%sLaserLine%d.txt", _scan_dir, i); + SVzNLXYZRGBDLaserLine* laser3DPoints_RGBD = vzReadLaserScanPointFromFile_XYZRGB(_scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); + if (laser3DPoints_RGBD == NULL) + continue; + + double camPoseR[9] = { + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0 }; + + int vldLineNum = 0; + SVzNL3DLaserLine* gridData = _convertToGridData_XYZRGB(laser3DPoints_RGBD, lineNum, _F, camPoseR, &vldLineNum); + + char _out_file[256]; + sprintf_s(_out_file, "%sLaserLine%d_grid.txt", _scan_dir, i); + _outputScanDataFile_self(_out_file, gridData, vldLineNum, + lineV, maxTimeStamp, clockPerSecond); + printf("%s: convert done!\n", _scan_file); + } +#else + + const char* dataPath[TEST_GROUP] = { + "E:\\上古\\车厢动态扫描\\", //0 + }; + + SVzNLRange fileIdx[TEST_GROUP] = { + {1,5} }; + + double camPoseR[9] = { + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0 }; + + SG_boxCarMeasureParam algoParam; + algoParam.filterParam.zJumpTh = 20.0; //噪声滤除。当相邻点的z跳变大于此门限时,检查是否为噪声。若长度小于outlierLen, 视为噪声 + algoParam.filterParam.outlierLen = 5; + + algoParam.templatePara_HF.H_len = 25.0; //mm + algoParam.templatePara_HF.V_len = 100.0; //mm + algoParam.templatePara_HF.maxDelta = 8.0; //mm + algoParam.templatePara_FH.H_len = 100.0; //mm + algoParam.templatePara_FH.V_len = 100.0; //mm + algoParam.templatePara_FH.maxDelta = 8.0; //mm + algoParam.templatePara_HR.H_len = 100.0; //mm + algoParam.templatePara_HR.V_len = 100.0; //mm + algoParam.templatePara_HR.maxDelta = 8.0; //mm + algoParam.templatePara_RH.H_len = 25.0; //mm + algoParam.templatePara_RH.V_len = 100.0; //mm + algoParam.templatePara_RH.maxDelta = 8.0; //mm + algoParam.growParam.maxLineSkipNum = -1; + algoParam.growParam.maxSkipDistance = 100.0; + algoParam.growParam.yDeviation_max = 10.0; + algoParam.growParam.zDeviation_max = 10.0; + algoParam.growParam.minLTypeTreeLen = 50.0; //mm + algoParam.growParam.minVTypeTreeLen = 50.0; //mm + + char _scan_file[256]; + + int endGroup = TEST_GROUP - 1; + for (int grp = 0; grp <= endGroup; grp++) + { + for (int fidx = fileIdx[grp].nMin; fidx <= fileIdx[grp].nMax; fidx++) + { + //fidx = 4; + int lineNum = 0; + float lineV = 0.0f; + int dataCalib = 0; + int maxTimeStamp = 0; + int clockPerSecond = 0; + sprintf_s(_scan_file, "%sLaserLine%d_grid.txt", dataPath[grp], fidx); + SVzNL3DLaserLine* laser3DPoints = vzReadLaserScanPointFromFile_XYZ(_scan_file, &lineNum, &lineV, &dataCalib, &maxTimeStamp, &clockPerSecond); + if (laser3DPoints == NULL) + continue; + + long t1 = GetTickCount64(); + int errCode = 0; + std::vector all_vLineFeatures; + std::vector> all_vLineNoises; + for (int i = 0; i < lineNum; i++) + { +if (i == 1400) + int kkk = 1; + //行处理 + sg_boxCarMeasure_lineProc( + &laser3DPoints[i], + i, + &errCode, + all_vLineFeatures, + all_vLineNoises, + algoParam); + } + SSG_boxCarDimension dimen = sg_getBoxCarDimension( + laser3DPoints, + lineNum, + all_vLineFeatures, + all_vLineNoises, + algoParam); + 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, laser3DPoints, lineNum, lineV, maxTimeStamp, clockPerSecond, &dimen); + + sprintf_s(_dbg_file, "%sresult\\LaserLine%d_result_img.png", dataPath[grp], fidx); + cv::String imgName(_dbg_file); + double rpy[3] = { -30, 15, 0 }; //{ 0,-45, 0 }; // + _genXOYProjectionImage(imgName, laser3DPoints, lineNum, rpy); +#endif + printf("%s: %d(ms), L=%f, W=%f, Angle=%f!\n", _scan_file, (int)(t2 - t1), dimen.L, dimen.W, dimen.angle); + for (int i = 0; i < 4; i++) + { + printf(" (%0.1f, %.1f, %.1f)\n", dimen.endings[i].x, dimen.endings[i].y, dimen.endings[i].z); + } + + } + } + +#endif + printf("all done!\n"); +} + +