2 |
jakw |
1 |
#include "SMPointCloudWidget.h"
|
|
|
2 |
|
31 |
jakw |
3 |
#include <opencv2/opencv.hpp>
|
33 |
jakw |
4 |
#include <opencv2/core/eigen.hpp>
|
31 |
jakw |
5 |
|
2 |
jakw |
6 |
#include <vtkWindowToImageFilter.h>
|
|
|
7 |
#include <vtkPNGWriter.h>
|
|
|
8 |
#include <vtkRenderWindow.h>
|
|
|
9 |
|
|
|
10 |
#include <pcl/point_cloud.h>
|
|
|
11 |
#include <pcl/point_types.h>
|
|
|
12 |
#include <pcl/common/io.h>
|
|
|
13 |
#include <pcl/visualization/point_cloud_color_handlers.h>
|
|
|
14 |
#include <pcl/geometry/quad_mesh.h>
|
|
|
15 |
|
|
|
16 |
#include <pcl/io/pcd_io.h>
|
|
|
17 |
#include <pcl/io/ascii_io.h>
|
|
|
18 |
#include <pcl/io/ply_io.h>
|
|
|
19 |
#include <pcl/io/png_io.h>
|
|
|
20 |
#include <pcl/io/vtk_io.h>
|
|
|
21 |
#include <vtkPolyDataWriter.h>
|
|
|
22 |
#include <pcl/conversions.h>
|
|
|
23 |
|
|
|
24 |
#include <fstream>
|
|
|
25 |
|
|
|
26 |
#include <QFileDialog>
|
31 |
jakw |
27 |
#include <QSettings>
|
2 |
jakw |
28 |
|
31 |
jakw |
29 |
#include "SMCalibrationParameters.h"
|
2 |
jakw |
30 |
|
|
|
31 |
SMPointCloudWidget::SMPointCloudWidget(QWidget *parent) : QVTKWidget(parent) {
|
|
|
32 |
|
|
|
33 |
visualizer = new pcl::visualization::PCLVisualizer("PCLVisualizer", false);
|
|
|
34 |
this->SetRenderWindow(visualizer->getRenderWindow());
|
|
|
35 |
|
|
|
36 |
visualizer->setShowFPS(false);
|
|
|
37 |
|
|
|
38 |
// Create point cloud viewport
|
|
|
39 |
visualizer->setBackgroundColor(0, 0, 0);
|
33 |
jakw |
40 |
visualizer->addCoordinateSystem(100, 0);
|
2 |
jakw |
41 |
#ifndef __APPLE__
|
|
|
42 |
// this leads to a segfault on OS X
|
|
|
43 |
visualizer->setCameraPosition(0,0,-50,0,0,0,0,-1,0);
|
|
|
44 |
#endif
|
|
|
45 |
|
|
|
46 |
// Initialize point cloud color handler
|
|
|
47 |
colorHandler = new pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB>();
|
|
|
48 |
|
33 |
jakw |
49 |
updateCalibrationParameters();
|
2 |
jakw |
50 |
//time.start();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
void SMPointCloudWidget::updatePointCloud(PointCloudConstPtr _pointCloudPCL){
|
|
|
54 |
|
|
|
55 |
// time.restart();
|
|
|
56 |
|
|
|
57 |
pointCloudPCL = _pointCloudPCL;
|
|
|
58 |
|
|
|
59 |
// Note: using the color handler makes a copy of the rgb fields
|
|
|
60 |
colorHandler->setInputCloud(_pointCloudPCL);
|
|
|
61 |
|
|
|
62 |
if(!visualizer->updatePointCloud(pointCloudPCL, *colorHandler, "pointCloudPCL")){
|
|
|
63 |
visualizer->addPointCloud(pointCloudPCL, *colorHandler, "pointCloudPCL");
|
|
|
64 |
visualizer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2.0, "pointCloudPCL");
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
this->update();
|
|
|
68 |
emit newPointCloudDisplayed();
|
|
|
69 |
|
|
|
70 |
// std::cout << "PCL Widget: " << time.restart() << "ms" << std::endl;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
void SMPointCloudWidget::savePointCloud(){
|
|
|
74 |
|
|
|
75 |
QString selectedFilter;
|
|
|
76 |
QString fileName = QFileDialog::getSaveFileName(this, "Save Point Cloud", QString(), "*.pcd;;*.ply;;*.vtk;;*.png;;*.txt", &selectedFilter);
|
|
|
77 |
QFileInfo info(fileName);
|
|
|
78 |
QString type = info.suffix();
|
|
|
79 |
if(type == ""){
|
|
|
80 |
fileName.append(selectedFilter.remove(0,1));
|
|
|
81 |
type = selectedFilter.remove(0,1);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
if(type == "pcd"){
|
|
|
85 |
pcl::io::savePCDFileASCII(fileName.toStdString(), *pointCloudPCL);
|
|
|
86 |
} else if(type == "ply"){
|
|
|
87 |
//pcl::io::savePLYFileBinary(fileName.toStdString(), *pointCloudPCL);
|
|
|
88 |
pcl::PLYWriter w;
|
|
|
89 |
// Write to ply in binary without camera
|
|
|
90 |
w.write<pcl::PointXYZRGB> (fileName.toStdString(), *pointCloudPCL, true, false);
|
|
|
91 |
} else if(type == "vtk"){
|
|
|
92 |
pcl::PCLPointCloud2 pointCloud2;
|
|
|
93 |
pcl::toPCLPointCloud2(*pointCloudPCL, pointCloud2);
|
|
|
94 |
pcl::io::saveVTKFile(fileName.toStdString(), pointCloud2);
|
|
|
95 |
|
|
|
96 |
// vtkPolyData polyData;
|
|
|
97 |
// pcl::io::pointCloudTovtkPolyData(*pointCloudPCL, polyData);
|
|
|
98 |
// vtkPolyDataWriter::Pointer writer = vtkPolyDataWriter::New();
|
|
|
99 |
// writer->SetInput(polyData);
|
|
|
100 |
// writer->SetFileName(fileName.toStdString());
|
|
|
101 |
// writer->Update();
|
|
|
102 |
} else if(type == "png"){
|
|
|
103 |
pcl::io::savePNGFile(fileName.toStdString(), *pointCloudPCL, "rgb");
|
|
|
104 |
} else if(type == "txt"){
|
|
|
105 |
std::ofstream s(fileName.toLocal8Bit());
|
|
|
106 |
for(unsigned int r=0; r<pointCloudPCL->height; r++){
|
|
|
107 |
for(unsigned int c=0; c<pointCloudPCL->width; c++){
|
|
|
108 |
pcl::PointXYZRGB p = pointCloudPCL->at(c, r);
|
|
|
109 |
if(p.x == p.x)
|
|
|
110 |
s << p.x << " " << p.y << " " << p.z << "\r\n";
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
std::flush(s);
|
|
|
114 |
s.close();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
void SMPointCloudWidget::saveScreenShot(){
|
|
|
120 |
|
|
|
121 |
vtkWindowToImageFilter* filter = vtkWindowToImageFilter::New();
|
|
|
122 |
//filter->SetInput(visualizer->getRenderWindow());
|
|
|
123 |
filter->Modified();
|
|
|
124 |
|
|
|
125 |
QString fileName = QFileDialog::getSaveFileName(this, "Save Screen Shot", QString(), "*.png");
|
|
|
126 |
QFileInfo info(fileName);
|
|
|
127 |
QString type = info.suffix();
|
|
|
128 |
if(type == "")
|
|
|
129 |
fileName.append(".png");
|
|
|
130 |
|
|
|
131 |
vtkPNGWriter* writer = vtkPNGWriter::New();
|
|
|
132 |
writer->SetInput(filter->GetOutput());
|
|
|
133 |
writer->SetFileName(qPrintable(fileName));
|
|
|
134 |
writer->Write();
|
|
|
135 |
writer->Delete();
|
|
|
136 |
}
|
|
|
137 |
|
31 |
jakw |
138 |
void SMPointCloudWidget::updateCalibrationParameters(){
|
|
|
139 |
|
|
|
140 |
QSettings settings;
|
33 |
jakw |
141 |
SMCalibrationParameters calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
|
31 |
jakw |
142 |
|
34 |
jakw |
143 |
// camera 0 coordinate system
|
|
|
144 |
visualizer->removeCoordinateSystem(0);
|
33 |
jakw |
145 |
visualizer->addCoordinateSystem(100, 0);
|
31 |
jakw |
146 |
|
33 |
jakw |
147 |
// camera 1 coordinate system
|
|
|
148 |
cv::Mat Transform1CV(3, 4, CV_32F);
|
|
|
149 |
cv::Mat(calibration.R1).copyTo(Transform1CV.colRange(0, 3));
|
|
|
150 |
cv::Mat(calibration.T1).copyTo(Transform1CV.col(3));
|
|
|
151 |
Eigen::Affine3f Transform1;
|
|
|
152 |
cv::cv2eigen(Transform1CV, Transform1.matrix());
|
|
|
153 |
|
34 |
jakw |
154 |
//visualizer->addCoordinateSystem(100, Transform1.inverse());
|
33 |
jakw |
155 |
|
31 |
jakw |
156 |
// rotation axis at (0,0,0) pointing (0,1,0) in rotation stage frame
|
33 |
jakw |
157 |
cv::Vec3f O(0.0, 0.0, 0.0);
|
31 |
jakw |
158 |
cv::Vec3f v(0.0, 1.0, 0.0);
|
|
|
159 |
|
|
|
160 |
// determine coefficients in camera 0 frame
|
|
|
161 |
O = calibration.Rr*O + calibration.Tr;
|
|
|
162 |
v = calibration.Rr*v + calibration.Tr;
|
|
|
163 |
|
|
|
164 |
pcl::ModelCoefficients lineCoefficients;
|
|
|
165 |
lineCoefficients.values.resize(6);
|
|
|
166 |
lineCoefficients.values[0] = O[0];
|
|
|
167 |
lineCoefficients.values[1] = O[1];
|
|
|
168 |
lineCoefficients.values[2] = O[2];
|
|
|
169 |
lineCoefficients.values[3] = v[0];
|
|
|
170 |
lineCoefficients.values[4] = v[1];
|
|
|
171 |
lineCoefficients.values[5] = v[2];
|
33 |
jakw |
172 |
visualizer->removeShape("line");
|
31 |
jakw |
173 |
visualizer->addLine(lineCoefficients);
|
|
|
174 |
}
|
|
|
175 |
|
2 |
jakw |
176 |
SMPointCloudWidget::~SMPointCloudWidget(){
|
|
|
177 |
|
|
|
178 |
//delete visualizer;
|
|
|
179 |
|
|
|
180 |
}
|