Subversion Repositories seema-scanner

Rev

Rev 169 | Rev 176 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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