264 lines
6.4 KiB
C++
264 lines
6.4 KiB
C++
#include "PointCloudKernal.h"
|
|
#include <vtkDataSet.h>
|
|
#include <vtkPoints.h>
|
|
#include <vtkCell.h>
|
|
#include <QDataStream>
|
|
#include <QString>
|
|
#include <QDomDocument>
|
|
#include <QDomElement>
|
|
#include <QDomAttr>
|
|
#include <QDomText>
|
|
#include <vtkIdList.h>
|
|
#include <vtkUnstructuredGrid.h>
|
|
#include <QDebug>
|
|
|
|
namespace PointCloudData
|
|
{
|
|
int PointCloudKernal::idOffset = 0;
|
|
int PointCloudKernal::pointIDOffset = 1;
|
|
|
|
PointCloudKernal::PointCloudKernal()
|
|
{
|
|
idOffset++;
|
|
setID(idOffset);
|
|
_pointIDOffset = pointIDOffset;
|
|
// _PointCloud = vtkSmartPointer<vtkUnstructuredGrid>::New();
|
|
_specificColor.first = false;
|
|
appendProperty(QObject::tr("Visible"), _visible);
|
|
}
|
|
|
|
void PointCloudKernal::setPointCloudData(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr dataset)
|
|
{
|
|
_PointCloud = dataset;
|
|
_pointIDOffset = pointIDOffset;
|
|
|
|
pointIDOffset += dataset->size();
|
|
|
|
appendProperty(QObject::tr("Points"), (int)dataset->size());
|
|
}
|
|
|
|
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr PointCloudKernal::getPointCloudData()
|
|
{
|
|
return _PointCloud;
|
|
}
|
|
|
|
pcl::PointXYZRGBA PointCloudKernal::getPointAt(const int index)
|
|
{
|
|
return _PointCloud->at(index);
|
|
}
|
|
|
|
bool PointCloudKernal::isVisible()
|
|
{
|
|
return _visible;
|
|
}
|
|
|
|
void PointCloudKernal::setVisible(bool v)
|
|
{
|
|
_visible = v;
|
|
appendProperty(QObject::tr("Visible"), _visible);
|
|
}
|
|
|
|
void PointCloudKernal::dataToStream(QDataStream* s)
|
|
{
|
|
*s << _id << _name << _PointCloud->size() ;
|
|
}
|
|
|
|
void PointCloudKernal::setID(int id)
|
|
{
|
|
DataBase::setID(id);
|
|
if (id > idOffset)
|
|
idOffset = id;
|
|
}
|
|
|
|
void PointCloudKernal::setPath(const QString& path)
|
|
{
|
|
_path = path;
|
|
}
|
|
|
|
QString PointCloudKernal::getPath()
|
|
{
|
|
return _path;
|
|
}
|
|
|
|
void PointCloudKernal::setPointIDOFfset(int offset)
|
|
{
|
|
_pointIDOffset = offset;
|
|
if (pointIDOffset < offset)
|
|
pointIDOffset = offset;
|
|
}
|
|
|
|
|
|
QDomElement& PointCloudKernal::writeToProjectFile(QDomDocument* doc, QDomElement* parent)
|
|
{
|
|
QDomElement kernelele = doc->createElement("PointCloudKernel");
|
|
QDomAttr idAttr = doc->createAttribute("ID");
|
|
idAttr.setValue(QString::number(_id));
|
|
kernelele.setAttributeNode(idAttr);
|
|
QDomAttr visible = doc->createAttribute("Visible");
|
|
visible.setValue("True");
|
|
if (!_visible) visible.setValue("False");
|
|
kernelele.setAttributeNode(visible);
|
|
QDomElement nameele = doc->createElement("Name");
|
|
QDomText nameText = doc->createTextNode(_name);
|
|
nameele.appendChild(nameText);
|
|
kernelele.appendChild(nameele);
|
|
|
|
if (!_path.isEmpty())
|
|
{
|
|
QDomElement pathele = doc->createElement("Path");
|
|
QDomText pathtext = doc->createTextNode(_path);
|
|
pathele.appendChild(pathtext);
|
|
kernelele.appendChild(pathele);
|
|
this->writePCDFile(_path);
|
|
}
|
|
|
|
return kernelele;
|
|
}
|
|
|
|
void PointCloudKernal::readDataFromProjectFile(QDomElement* kernelele)
|
|
{
|
|
QString sid = kernelele->attribute("ID");
|
|
this->setID(sid.toInt());
|
|
QString svis = kernelele->attribute("Visible");
|
|
bool visible = true;
|
|
if (svis.toLower() == "false") visible = false;
|
|
this->setVisible(visible);
|
|
|
|
QDomNodeList nameNodeList = kernelele->elementsByTagName("Name");
|
|
if (nameNodeList.size() != 1) return;
|
|
QString name = nameNodeList.at(0).toElement().text();
|
|
this->setName(name);
|
|
QDomNodeList pathList = kernelele->elementsByTagName("Path");
|
|
if (pathList.size() > 0)
|
|
{
|
|
QString path = pathList.at(0).toElement().text();
|
|
this->setPath(path);
|
|
this->readBinaryFile(path);
|
|
}
|
|
}
|
|
|
|
int PointCloudKernal::getPointCount()
|
|
{
|
|
if (_PointCloud != nullptr)
|
|
|
|
return _PointCloud->size();
|
|
return -1;
|
|
}
|
|
|
|
|
|
void PointCloudKernal::resetOffset()
|
|
{
|
|
idOffset = 0;
|
|
pointIDOffset = 1;
|
|
}
|
|
|
|
void PointCloudKernal::setDimension(int d)
|
|
{
|
|
_dimension = d;
|
|
}
|
|
|
|
int PointCloudKernal::getDimension()
|
|
{
|
|
return _dimension;
|
|
}
|
|
|
|
//写出二进制文件
|
|
void PointCloudKernal::writeBinaryFile(QDataStream* dataStream){
|
|
*dataStream << _id << _visible << _name << _path; // 保存存储文件的相对文件名
|
|
}
|
|
|
|
//读入二进制文件
|
|
void PointCloudKernal::readBinaryFile(QDataStream* dataStream){
|
|
int KernalID = 0; //KernalID
|
|
bool visible = false;
|
|
QString PointCloudName, PointCloudPath;
|
|
*dataStream >> KernalID >> visible >> PointCloudName >> PointCloudPath;
|
|
|
|
this->setID(KernalID);
|
|
this->setVisible(visible);
|
|
this->setName(PointCloudName);
|
|
this->setPath(PointCloudPath);
|
|
this->readPCDFile(PointCloudPath);
|
|
}
|
|
|
|
|
|
|
|
|
|
void PointCloudKernal::writePCDFile(const QString& datafilepath)
|
|
{
|
|
pcl::io::savePCDFileBinary(datafilepath.toUtf8().constData(),*(this->getPointCloudData()));
|
|
}
|
|
|
|
void PointCloudKernal::readPCDFile(const QString& dataStream)
|
|
{
|
|
// 定义点云
|
|
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);
|
|
pcl::PCLPointCloud2::Ptr cloud2(new pcl::PCLPointCloud2);
|
|
// 读取点云,失败返回-1
|
|
if (pcl::io::loadPCDFile(dataStream.toUtf8().constData(), *cloud2) == -1)
|
|
{
|
|
PCL_ERROR("couldn't read file\n");
|
|
}else{
|
|
pcl::fromPCLPointCloud2(*cloud2,*cloud);
|
|
this->setPointCloudData(cloud);
|
|
this->setPath(dataStream);
|
|
}
|
|
}
|
|
|
|
void PointCloudKernal::setPointCloudSetting(DataProperty::DataBase* data)
|
|
{
|
|
_PointCloudSetting = data;
|
|
}
|
|
|
|
DataProperty::DataBase* PointCloudKernal::getPointCloudSetting()
|
|
{
|
|
return _PointCloudSetting;
|
|
}
|
|
|
|
void PointCloudKernal::setSpecificColor(bool enable, QColor c)
|
|
{
|
|
_specificColor.first = enable;
|
|
_specificColor.second = c;
|
|
this->UpdatePointCloudCloudColor();
|
|
}
|
|
|
|
void PointCloudKernal::setSpecificColor(bool enable,std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t alpha)
|
|
{
|
|
_specificColor.first = enable;
|
|
_specificColor.second.setRedF(r);
|
|
_specificColor.second.setGreenF(g);
|
|
_specificColor.second.setBlueF(b);
|
|
_specificColor.second.setAlpha(alpha);
|
|
|
|
this->UpdatePointCloudCloudColor();
|
|
}
|
|
|
|
QColor PointCloudKernal::getSpecificColor(bool &isEnable)
|
|
{
|
|
isEnable = _specificColor.first;
|
|
return _specificColor.second;
|
|
this->UpdatePointCloudCloudColor();
|
|
}
|
|
void PointCloudKernal::UpdatePointCloudCloudColor() {
|
|
std::uint8_t r= _specificColor.second.redF();
|
|
std::uint8_t g= _specificColor.second.greenF();
|
|
std::uint8_t b= _specificColor.second.blueF();
|
|
std::uint8_t a= _specificColor.second.alphaF();
|
|
|
|
for(std::size_t i=0;i<_PointCloud->size();i++){
|
|
(*_PointCloud)[i].r=r;
|
|
}
|
|
for(std::size_t i=0;i<_PointCloud->size();i++){
|
|
(*_PointCloud)[i].g=g;
|
|
}
|
|
for(std::size_t i=0;i<_PointCloud->size();i++){
|
|
(*_PointCloud)[i].b=b;
|
|
}
|
|
for(std::size_t i=0;i<_PointCloud->size();i++){
|
|
(*_PointCloud)[i].a=a;
|
|
}
|
|
}
|
|
|
|
}
|
|
|