Subversion Repositories seema-scanner

Rev

Rev 187 | Rev 208 | 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
 
191 jakw 29
    visualizer->setupInteractor(this->GetInteractor(), this->GetRenderWindow());
30
 
172 jakw 31
    // Disable double buffering (which causes lag in VTK6)
32
    visualizer->getRenderWindow()->SetDoubleBuffer(0);
33
 
2 jakw 34
    visualizer->setShowFPS(false);
35
 
36
    // Create point cloud viewport
37
    visualizer->setBackgroundColor(0, 0, 0);
76 jakw 38
    visualizer->addCoordinateSystem(100, "camera0", 0);
2 jakw 39
    visualizer->setCameraPosition(0,0,-50,0,0,0,0,-1,0);
40
 
185 jakw 41
 
2 jakw 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
 
2 jakw 116
void SMPointCloudWidget::saveScreenShot(){
117
 
118
    vtkWindowToImageFilter* filter = vtkWindowToImageFilter::New();
119
    //filter->SetInput(visualizer->getRenderWindow());
120
    filter->Modified();
121
 
122
    QString fileName = QFileDialog::getSaveFileName(this, "Save Screen Shot", QString(), "*.png");
123
    QFileInfo info(fileName);
124
    QString type = info.suffix();
125
    if(type == "")
126
        fileName.append(".png");
127
 
128
    vtkPNGWriter* writer = vtkPNGWriter::New();
129
    writer->SetFileName(qPrintable(fileName));
167 jakw 130
    writer->SetInputConnection(filter->GetOutputPort());
2 jakw 131
    writer->Write();
132
    writer->Delete();
133
}
134
 
31 jakw 135
void SMPointCloudWidget::updateCalibrationParameters(){
136
 
137
    QSettings settings;
169 jakw 138
    QVariant calibrationVariant = settings.value("calibration/parameters");
139
    SMCalibrationParameters calibration = calibrationVariant.value<SMCalibrationParameters>();
31 jakw 140
 
34 jakw 141
    // camera 0 coordinate system
187 jakw 142
    visualizer->removeCoordinateSystem("camera0", 0);
76 jakw 143
    visualizer->addCoordinateSystem(100, "camera0", 0);
31 jakw 144
 
33 jakw 145
    // camera 1 coordinate system
146
    cv::Mat Transform1CV(3, 4, CV_32F);
147
    cv::Mat(calibration.R1).copyTo(Transform1CV.colRange(0, 3));
148
    cv::Mat(calibration.T1).copyTo(Transform1CV.col(3));
149
    Eigen::Affine3f Transform1;
150
    cv::cv2eigen(Transform1CV, Transform1.matrix());
151
 
187 jakw 152
    visualizer->removeCoordinateSystem("camera1", 0);
76 jakw 153
    visualizer->addCoordinateSystem(100, Transform1.inverse(), "camera1", 0);
33 jakw 154
 
176 jakw 155
//    // rotation axis coordinate system
36 jakw 156
//    cv::Mat TransformRCV(3, 4, CV_32F);
157
//    cv::Mat(calibration.Rr).copyTo(TransformRCV.colRange(0, 3));
158
//    cv::Mat(calibration.Tr).copyTo(TransformRCV.col(3));
159
//    Eigen::Affine3f TransformR;
176 jakw 160
 
36 jakw 161
//    cv::cv2eigen(TransformRCV, TransformR.matrix());
176 jakw 162
//    visualizer->removeCoordinateSystem("camera1", 0);
163
//    visualizer->addCoordinateSystem(100, TransformR.inverse(), "rotation", 0);
36 jakw 164
 
81 jakw 165
    // rotation axis pointing (0,1,0) in rotation stage frame
176 jakw 166
    cv::Vec3f O(0.0, 0.0, 0.0);
44 jakw 167
    cv::Vec3f v(0.0, 600.0, 0.0);
31 jakw 168
 
169
    // determine coefficients in camera 0 frame
36 jakw 170
    O = calibration.Rr.t()*O + -calibration.Rr.t()*calibration.Tr;
148 jakw 171
    v = calibration.Rr.t()*v + -calibration.Rr.t()*calibration.Tr - O;
31 jakw 172
 
173
    pcl::ModelCoefficients lineCoefficients;
174
    lineCoefficients.values.resize(6);
175
    lineCoefficients.values[0] = O[0];
176
    lineCoefficients.values[1] = O[1];
177
    lineCoefficients.values[2] = O[2];
178
    lineCoefficients.values[3] = v[0];
179
    lineCoefficients.values[4] = v[1];
180
    lineCoefficients.values[5] = v[2];
33 jakw 181
    visualizer->removeShape("line");
148 jakw 182
    visualizer->addLine(lineCoefficients);
36 jakw 183
 
148 jakw 184
 
185
 
31 jakw 186
}
187
 
2 jakw 188
SMPointCloudWidget::~SMPointCloudWidget(){
189
 
190
    //delete visualizer;
191
 
192
}