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