2 |
jakw |
1 |
#include "SMPointCloudWidget.h"
|
|
|
2 |
|
|
|
3 |
#include <vtkWindowToImageFilter.h>
|
|
|
4 |
#include <vtkPNGWriter.h>
|
|
|
5 |
#include <vtkRenderWindow.h>
|
|
|
6 |
|
|
|
7 |
#include <pcl/point_cloud.h>
|
|
|
8 |
#include <pcl/point_types.h>
|
|
|
9 |
#include <pcl/common/io.h>
|
|
|
10 |
#include <pcl/visualization/point_cloud_color_handlers.h>
|
|
|
11 |
#include <pcl/geometry/quad_mesh.h>
|
|
|
12 |
|
|
|
13 |
#include <pcl/io/pcd_io.h>
|
|
|
14 |
#include <pcl/io/ascii_io.h>
|
|
|
15 |
#include <pcl/io/ply_io.h>
|
|
|
16 |
#include <pcl/io/png_io.h>
|
|
|
17 |
#include <pcl/io/vtk_io.h>
|
|
|
18 |
#include <vtkPolyDataWriter.h>
|
|
|
19 |
#include <pcl/conversions.h>
|
|
|
20 |
|
|
|
21 |
#include <fstream>
|
|
|
22 |
|
|
|
23 |
#include <QFileDialog>
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
SMPointCloudWidget::SMPointCloudWidget(QWidget *parent) : QVTKWidget(parent) {
|
|
|
27 |
|
|
|
28 |
visualizer = new pcl::visualization::PCLVisualizer("PCLVisualizer", false);
|
|
|
29 |
this->SetRenderWindow(visualizer->getRenderWindow());
|
|
|
30 |
|
|
|
31 |
visualizer->setShowFPS(false);
|
|
|
32 |
|
|
|
33 |
// Create point cloud viewport
|
|
|
34 |
visualizer->setBackgroundColor(0, 0, 0);
|
|
|
35 |
visualizer->addCoordinateSystem(50);
|
|
|
36 |
#ifndef __APPLE__
|
|
|
37 |
// this leads to a segfault on OS X
|
|
|
38 |
visualizer->setCameraPosition(0,0,-50,0,0,0,0,-1,0);
|
|
|
39 |
#endif
|
|
|
40 |
|
|
|
41 |
// Initialize point cloud color handler
|
|
|
42 |
colorHandler = new pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB>();
|
|
|
43 |
|
|
|
44 |
//time.start();
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
void SMPointCloudWidget::updatePointCloud(PointCloudConstPtr _pointCloudPCL){
|
|
|
48 |
|
|
|
49 |
// time.restart();
|
|
|
50 |
|
|
|
51 |
pointCloudPCL = _pointCloudPCL;
|
|
|
52 |
|
|
|
53 |
// Note: using the color handler makes a copy of the rgb fields
|
|
|
54 |
colorHandler->setInputCloud(_pointCloudPCL);
|
|
|
55 |
|
|
|
56 |
if(!visualizer->updatePointCloud(pointCloudPCL, *colorHandler, "pointCloudPCL")){
|
|
|
57 |
visualizer->addPointCloud(pointCloudPCL, *colorHandler, "pointCloudPCL");
|
|
|
58 |
visualizer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2.0, "pointCloudPCL");
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
this->update();
|
|
|
62 |
emit newPointCloudDisplayed();
|
|
|
63 |
|
|
|
64 |
// std::cout << "PCL Widget: " << time.restart() << "ms" << std::endl;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
void SMPointCloudWidget::savePointCloud(){
|
|
|
68 |
|
|
|
69 |
QString selectedFilter;
|
|
|
70 |
QString fileName = QFileDialog::getSaveFileName(this, "Save Point Cloud", QString(), "*.pcd;;*.ply;;*.vtk;;*.png;;*.txt", &selectedFilter);
|
|
|
71 |
QFileInfo info(fileName);
|
|
|
72 |
QString type = info.suffix();
|
|
|
73 |
if(type == ""){
|
|
|
74 |
fileName.append(selectedFilter.remove(0,1));
|
|
|
75 |
type = selectedFilter.remove(0,1);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if(type == "pcd"){
|
|
|
79 |
pcl::io::savePCDFileASCII(fileName.toStdString(), *pointCloudPCL);
|
|
|
80 |
} else if(type == "ply"){
|
|
|
81 |
//pcl::io::savePLYFileBinary(fileName.toStdString(), *pointCloudPCL);
|
|
|
82 |
pcl::PLYWriter w;
|
|
|
83 |
// Write to ply in binary without camera
|
|
|
84 |
w.write<pcl::PointXYZRGB> (fileName.toStdString(), *pointCloudPCL, true, false);
|
|
|
85 |
} else if(type == "vtk"){
|
|
|
86 |
pcl::PCLPointCloud2 pointCloud2;
|
|
|
87 |
pcl::toPCLPointCloud2(*pointCloudPCL, pointCloud2);
|
|
|
88 |
pcl::io::saveVTKFile(fileName.toStdString(), pointCloud2);
|
|
|
89 |
|
|
|
90 |
// vtkPolyData polyData;
|
|
|
91 |
// pcl::io::pointCloudTovtkPolyData(*pointCloudPCL, polyData);
|
|
|
92 |
// vtkPolyDataWriter::Pointer writer = vtkPolyDataWriter::New();
|
|
|
93 |
// writer->SetInput(polyData);
|
|
|
94 |
// writer->SetFileName(fileName.toStdString());
|
|
|
95 |
// writer->Update();
|
|
|
96 |
} else if(type == "png"){
|
|
|
97 |
pcl::io::savePNGFile(fileName.toStdString(), *pointCloudPCL, "rgb");
|
|
|
98 |
} else if(type == "txt"){
|
|
|
99 |
std::ofstream s(fileName.toLocal8Bit());
|
|
|
100 |
for(unsigned int r=0; r<pointCloudPCL->height; r++){
|
|
|
101 |
for(unsigned int c=0; c<pointCloudPCL->width; c++){
|
|
|
102 |
pcl::PointXYZRGB p = pointCloudPCL->at(c, r);
|
|
|
103 |
if(p.x == p.x)
|
|
|
104 |
s << p.x << " " << p.y << " " << p.z << "\r\n";
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
std::flush(s);
|
|
|
108 |
s.close();
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
void SMPointCloudWidget::saveScreenShot(){
|
|
|
114 |
|
|
|
115 |
vtkWindowToImageFilter* filter = vtkWindowToImageFilter::New();
|
|
|
116 |
//filter->SetInput(visualizer->getRenderWindow());
|
|
|
117 |
filter->Modified();
|
|
|
118 |
|
|
|
119 |
QString fileName = QFileDialog::getSaveFileName(this, "Save Screen Shot", QString(), "*.png");
|
|
|
120 |
QFileInfo info(fileName);
|
|
|
121 |
QString type = info.suffix();
|
|
|
122 |
if(type == "")
|
|
|
123 |
fileName.append(".png");
|
|
|
124 |
|
|
|
125 |
vtkPNGWriter* writer = vtkPNGWriter::New();
|
|
|
126 |
writer->SetInput(filter->GetOutput());
|
|
|
127 |
writer->SetFileName(qPrintable(fileName));
|
|
|
128 |
writer->Write();
|
|
|
129 |
writer->Delete();
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
SMPointCloudWidget::~SMPointCloudWidget(){
|
|
|
133 |
|
|
|
134 |
//delete visualizer;
|
|
|
135 |
|
|
|
136 |
}
|