LAMPCAE/src/PointCloudData/PointCloudSingleton.cpp

342 lines
9.1 KiB
C++
Raw Normal View History

#include "PointCloudSingleton.h"
#include "PointCloudKernal.h"
#include "PointCloudSet.h"
#include "PointCloudFactory.h"
#include <assert.h>
#include <QDataStream>
#include <QCryptographicHash>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNodeList>
#include <QFileInfo>
#include <QDebug>
namespace PointCloudData
{
PointCloudData* PointCloudData::_instance = nullptr;
PointCloudData* PointCloudData::getInstance()
{
if (_instance == nullptr)
{
_instance = new PointCloudData;
}
return _instance;
}
PointCloudData::~PointCloudData()
{
for (int i = 0; i < _pointcloudList.size(); ++i)
{
PointCloudKernal* k = _pointcloudList.at(i);
delete k;
}
_pointcloudList.clear();
}
void PointCloudData::appendPointCloudKernal(PointCloudKernal* keneral)
{
_pointcloudList.append(keneral);
}
int PointCloudData::getKernalCount()
{
return _pointcloudList.size();
}
PointCloudKernal* PointCloudData::getKernalAt(const int index)
{
if(index >= 0 && index < _pointcloudList.size())
return _pointcloudList.at(index);
return nullptr;
}
PointCloudKernal* PointCloudData::getKernalByID(const int id)
{
const int n = _pointcloudList.size();
for (int i = 0; i < n; ++i)
{
PointCloudKernal* k = _pointcloudList.at(i);
if (k->getID() == id)
return k;
}
return nullptr;
}
void PointCloudData::removeKernalAt(const int index)
{
PointCloudKernal* k = getKernalAt(index);
QList<PointCloudSet*> setlist{};
for (int i = 0; i < _setList.size(); ++i)
{
PointCloudSet* s = _setList.at(i);
// if (s->getDataSetID() == k->getID())
int kid = k->getID();
if (s->isContainsKernal(kid))
setlist.append(s);
}
for (int i = 0; i < setlist.size(); ++i)
{
PointCloudSet* s = setlist.at(i);
_setList.removeOne(s);
delete s;
}
delete k;
_pointcloudList.removeAt(index);
}
void PointCloudData::removeKernalByID(const int id)
{
auto k = this->getKernalByID(id);
int index = _pointcloudList.indexOf(k);
if (index < 0) return;
this->removeKernalAt(index);
}
void PointCloudData::clear()
{
int n = this->getKernalCount();
for (int i = 0; i < n; ++i)
{
PointCloudKernal* k = this->getKernalAt(i);
delete k;
}
_pointcloudList.clear();
n = _setList.size();
for (int i = 0; i < n; ++i)
{
PointCloudSet *s = _setList.at(i);
delete s;
}
_setList.clear();
PointCloudKernal::resetOffset();
DataProperty::ComponentBase::resetMaxID();
}
QString PointCloudData::getMD5()
{
const int n = _pointcloudList.size();
if (n < 1) return"";
QDataStream stream;
for (int i = 0; i < n; ++i)
{
_pointcloudList[i]->dataToStream(&stream);
}
for (auto set : _setList)
{
set->dataToStream(&stream);
}
char* s;
stream >> s;
QString md5 = QCryptographicHash::hash(s, QCryptographicHash::Md5);
return md5;
}
QDomElement& PointCloudData::writeToProjectFile(QDomDocument* doc, QDomElement* parent)
{
QDomElement meshNode = doc->createElement("PointCloud");
const int n = _pointcloudList.size();
QDomElement meshKernalList = doc->createElement("Kernel");
for (int i = 0; i < n; ++i)
{
PointCloudKernal* k = _pointcloudList.at(i);
k->writeToProjectFile(doc, &meshKernalList);
}
meshNode.appendChild(meshKernalList);
const int nset = _setList.size();
QDomElement setList = doc->createElement("Set");
for (int i = 0; i < nset; ++i)
{
PointCloudSet* s = _setList.at(i);
s->writeToProjectFile(doc, &setList);
}
meshNode.appendChild(setList);
parent->appendChild(meshNode);
return meshNode;
}
void PointCloudData::readFromProjectFile(QDomNodeList* nodelist)
{
QDomElement meshRoot = nodelist->at(0).toElement();
QDomNodeList meshList = meshRoot.elementsByTagName("PointCloudKernel");
const int nPointCloud = meshList.size();
for (int i = 0; i < nPointCloud; ++i)
{
QDomElement meshKernelEle = meshList.at(i).toElement();
{ //废弃代码
// QDomNodeList pathlist = meshKernelEle.elementsByTagName("Path");
// if (pathlist.size() != 1) continue;
// QDomElement pathele = pathlist.at(0).toElement();
// QString fpath = pathele.text();
// QFileInfo finfo(fpath);
// if (!finfo.exists()) return;
// QString suffix = finfo.suffix().toLower();
// if (suffix == "vtk" || suffix == "stl")
// {
// VTKdataExchange reader(fpath);
// if (!reader.read()) continue;
// }
// else if (suffix == "neu")
// {
// NEUdataExchange reader(fpath);
// if (!reader.read()) continue;
// }
}
PointCloudKernal* k = new PointCloudKernal;
_pointcloudList.append(k);
k->readDataFromProjectFile(&meshKernelEle);
}
QDomNodeList setList = meshRoot.elementsByTagName("PointCloudSet");
const int nSet = setList.size();
for (int i = 0; i < nSet; ++i)
{
PointCloudSet* s = nullptr;
QDomElement setEle = setList.at(i).toElement();
s = new PointCloudSet;
_setList.append(s);
s->readDataFromProjectFile(&setEle); //保存属性
// s->generateDisplayDataSet();
}
}
int PointCloudData::getIDByDataSet(vtkDataSet* datset)
{
const int n = _pointcloudList.size();
for (int i = 0; i < n; ++i)
{
PointCloudKernal* k = _pointcloudList.at(i);
vtkDataSet* s = k->getPointCloudData();
if (s == datset)
return k->getID();
}
return -1;
}
void PointCloudData::appendPointCloudSet(PointCloudSet* s)
{
_setList.append(s);
}
int PointCloudData::getPointCloudSetCount()
{
return _setList.size();
}
PointCloudSet* PointCloudData::getPointCloudSetAt(const int index)
{
assert(index >= 0 && index < _setList.size());
return _setList.at(index);
}
PointCloudSet* PointCloudData::getPointCloudSetByID(const int id)
{
const int n = _setList.size();
for (int i = 0; i < n; ++i)
{
PointCloudSet* set = _setList.at(i);
int sid = set->getID();
if (sid == id) return set;
}
return nullptr;
}
PointCloudSet* PointCloudData::getPointCloudSetByName(const QString name)
{
const int n = _setList.size();
for (int i = 0; i < n; ++i)
{
PointCloudSet* set = _setList.at(i);
QString n = set->getName();
if (name == n) return set;
}
return nullptr;
}
void PointCloudData::removePointCloudSetAt(const int index)
{
assert(index >= 0 && index < _setList.size());
PointCloudSet* s = _setList.at(index);
delete s;
_setList.removeAt(index);
}
QList<int> PointCloudData::getSetIDFromKernal(int kid)
{
QList<int> ids;
int n = _setList.size();
for (int i = 0; i < n; ++i)
{
PointCloudSet* set = _setList.at(i);
//int id = set->getDataSetID();
//if (id == kid)
if (set->isContainsKernal(kid))
ids.append(set->getID());
}
return ids;
}
bool PointCloudData::isContainsKernal(PointCloudKernal* ker)
{
return _pointcloudList.contains(ker);
}
void PointCloudData::generateDisplayDataSet()
{
const int n = this->getPointCloudSetCount();
for (int i = 0; i < n; ++i)
{
PointCloudSet* s = this->getPointCloudSetAt(i);
s->generateDisplayDataSet();
}
}
void PointCloudData::writeBinaryFile(QDataStream* dataStream)
{
//写出二进制文件
const int nk = _pointcloudList.size();
*dataStream << nk;
for (int i = 0; i < nk; ++i)
{
PointCloudKernal* k = _pointcloudList.at(i);
k->writePCDFile(dataStream);
}
const int ns = _setList.size();
*dataStream << ns;
for (int i = 0; i < ns; ++i)
{
PointCloudSet* s = _setList.at(i);
s->writeBinaryFile(dataStream);
}
}
void PointCloudData::readBinaryFile(QDataStream* dataStream)
{
//读入二进制文件
int nk = 0;
int ns = 0;
*dataStream >> nk;
for (int i = 0; i < nk; ++i)
{
PointCloudKernal* k = new PointCloudKernal;
_pointcloudList.append(k);
k->readBinaryFile(dataStream);
}
*dataStream >> ns;
for (int i = 0; i < ns; ++i)
{
int type = 0;
*dataStream >> type;
PointCloudSet* s = PointCloudFactory::CreatePointCloudSet(type);
// switch (type)
// {
// case 1:
// case 2: s = new PointCloudSet(QString(), SetType(type)); break;
// case 3: s = new CgnsFamily; break;
// case 4: s = new CgnsBCZone; break;
// default:break;
// }
if (s == nullptr) continue;
_setList.append(s);
s->readBinaryFile(dataStream);
}
}
}