Subversion Repositories seema-scanner

Rev

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