Subversion Repositories seema-scanner

Rev

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