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