搞定了点云格式(pcd)的 保存与导出

master
剑古敛锋 2024-04-03 11:34:39 +08:00
parent 4635cc287d
commit 7e6f2b435d
3 changed files with 28 additions and 20 deletions

View File

@ -27,7 +27,7 @@
#include <vtkUnstructuredGridWriter.h> #include <vtkUnstructuredGridWriter.h>
#include <QDebug> #include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include <vtkPointCloudFilter.h>
// debug // debug
#include "Common/DebugLogger.h" #include "Common/DebugLogger.h"
@ -114,13 +114,12 @@ namespace MeshData {
} }
bool PointClouddataExchange::write() bool PointClouddataExchange::write()
{ {
QFileInfo info(_fileName); QFileInfo info(_fileName);
if (!info.exists())
return false;
QString name = info.fileName(); QString name = info.fileName();
QString path = info.filePath(); QString path = info.filePath();
QString suffix = info.suffix().toLower(); QString suffix = info.suffix().toLower();
DebugInfo("PointClouddataExchange::writePCD FileName :%s \n",_fileName.toUtf8().constData());
if (suffix == "pcd") if (suffix == "pcd")
{ {
return writePCD(); return writePCD();
@ -219,18 +218,19 @@ namespace MeshData {
{ {
QTextCodec *codec = QTextCodec::codecForName("GB18030"); QTextCodec *codec = QTextCodec::codecForName("GB18030");
QByteArray ba = codec->fromUnicode(_fileName); QByteArray ba = codec->fromUnicode(_fileName);
vtkUnstructuredGrid *ungird = vtkUnstructuredGrid::New(); DebugInfo("PointClouddataExchange::writePCD FileName :%s ",ba);
vtkSmartPointer<vtkAppendFilter> filter = vtkSmartPointer<vtkAppendFilter>::New();
// 使用 vtkGeometryFilter 提取几何信息并转换为 vtkPolyData
vtkSmartPointer<vtkGeometryFilter> geometryFilter = vtkSmartPointer<vtkGeometryFilter>::New();
DebugInfo("PointClouddataExchange::writePCD Filter prepare :ok \n"); // 使用 vtkGeometryFilter 提取几何信息并转换为 vtkPolyData
vtkSmartPointer<vtkGeometryFilter> geometryFilter = vtkSmartPointer<vtkGeometryFilter>::New();
DebugInfo("PointClouddataExchange::writePCD Filter prepare :ok modelID :%d\n",_modelId);
if (_modelId > 0) if (_modelId > 0)
{ {
MeshKernal *kernal = _meshData->getKernalByID(_modelId); MeshKernal *kernal = _meshData->getKernalByID(_modelId);
if (kernal != nullptr) if (kernal != nullptr)
{ {
vtkDataSet *dataset = kernal->getMeshData(); vtkDataSet *dataset = kernal->getMeshData();
if (dataset != nullptr) if (dataset != nullptr)
geometryFilter->AddInputData(dataset); geometryFilter->AddInputData(dataset);
@ -239,6 +239,7 @@ namespace MeshData {
else else
{ {
const int n = _meshData->getKernalCount(); const int n = _meshData->getKernalCount();
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
{ {
if (!_threadRuning) if (!_threadRuning)
@ -247,16 +248,19 @@ namespace MeshData {
if (kernal != nullptr) if (kernal != nullptr)
{ {
vtkDataSet *dataset = kernal->getMeshData(); vtkDataSet *dataset = kernal->getMeshData();
DebugInfo("PointClouddataExchange::writePCD writer over : iKernal %d , %d is nullptr ,points Count %d\n",i,(int)(dataset==nullptr),dataset->GetNumberOfPoints());
if (dataset != nullptr) if (dataset != nullptr)
geometryFilter->AddInputData(dataset); geometryFilter->AddInputData(dataset);
} }
} }
} }
filter->Update(); geometryFilter->Update();
vtkSmartPointer<vtkPolyData> polyData = geometryFilter->GetOutput(); vtkSmartPointer<vtkPolyData> outputDataSet = geometryFilter->GetOutput();
DebugInfo("PointClouddataExchange::writePCD writer over :ok outputDataSet Point Count : %d\n",outputDataSet->GetNumberOfPoints());
// 将输出的点云数据集转换为 PCL 格式
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_with_rgba(new pcl::PointCloud<pcl::PointXYZRGBA>); pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud_with_rgba(new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::vtkPolyDataToPointCloud(polyData,*cloud_with_rgba); pcl::io::vtkPolyDataToPointCloud(outputDataSet,*cloud_with_rgba);
DebugInfo("PointClouddataExchange::writePCD writer prepare :ok \n"); DebugInfo("PointClouddataExchange::writePCD writer over :ok cloud_with_rgba Point Count : %d\n",cloud_with_rgba->size());
std::string outFileName=ba.data(); std::string outFileName=ba.data();
pcl::io::savePCDFileBinary(outFileName,*cloud_with_rgba); pcl::io::savePCDFileBinary(outFileName,*cloud_with_rgba);
DebugInfo("PointClouddataExchange::writePCD writer over :ok \n"); DebugInfo("PointClouddataExchange::writePCD writer over :ok \n");
@ -293,6 +297,7 @@ namespace MeshData {
if (kernal != nullptr) if (kernal != nullptr)
{ {
vtkDataSet *dataset = kernal->getMeshData(); vtkDataSet *dataset = kernal->getMeshData();
if (dataset != nullptr) if (dataset != nullptr)
geometryFilter->AddInputData(dataset); geometryFilter->AddInputData(dataset);
} }

View File

@ -133,8 +133,6 @@ namespace MeshData
bool VTKdataExchange::write() bool VTKdataExchange::write()
{ {
QFileInfo info(_fileName); QFileInfo info(_fileName);
if (!info.exists())
return false;
QString name = info.fileName(); QString name = info.fileName();
QString path = info.filePath(); QString path = info.filePath();
QString suffix = info.suffix().toLower(); QString suffix = info.suffix().toLower();

View File

@ -338,10 +338,12 @@ bool MESHDATAEXCHANGEPLUGINAPI FOAMexportMesh(QString AbFileName, int id)
return false; return false;
} }
bool MESHDATAEXCHANGEPLUGINAPI XYZimportPCL(QString AbFileName, int modelId) bool MESHDATAEXCHANGEPLUGINAPI XYZimportPCL(QString AbFileName, int id)
{ {
auto xyzReader = new MeshData::PointClouddataExchange(
AbFileName, MeshData::MESH_READ, MeshData::MeshDataExchangePlugin::getMWpt(), id);
ModuleBase::ThreadControl* tc = new ModuleBase::ThreadControl(xyzReader);
emit tc->threadStart(); // emit MSHwriter->start();
return false; return false;
} }
@ -354,9 +356,12 @@ bool MESHDATAEXCHANGEPLUGINAPI PCDimportPCL(QString AbFileName, int id)
return false; return false;
} }
bool MESHDATAEXCHANGEPLUGINAPI PLYimportPCL(QString AbFileName, int modelId) bool MESHDATAEXCHANGEPLUGINAPI PLYimportPCL(QString AbFileName, int id)
{ {
auto plyReader = new MeshData::PointClouddataExchange(
AbFileName, MeshData::MESH_READ, MeshData::MeshDataExchangePlugin::getMWpt(), id);
ModuleBase::ThreadControl* tc = new ModuleBase::ThreadControl(plyReader);
emit tc->threadStart(); // emit MSHwriter->start();
return false; return false;
} }