Subversion Repositories seema-scanner

Rev

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