Subversion Repositories seema-scanner

Rev

Rev 191 | Rev 248 | 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);
208 flgw 59
        visualizer->setPointCloudRenderingProperties(
60
                    pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2.0, stringId);
47 jakw 61
     }
41 jakw 62
 
44 jakw 63
    // transformation matrix in Eigen format
64
    cv::Mat TransformCV(3, 4, CV_32F);
65
    cv::Mat(pointCloud.R).copyTo(TransformCV.colRange(0, 3));
66
    cv::Mat(pointCloud.T).copyTo(TransformCV.col(3));
67
    Eigen::Affine3f Transform;
68
    cv::cv2eigen(TransformCV, Transform.matrix());
69
 
47 jakw 70
    visualizer->updatePointCloudPose(stringId, Transform.inverse());
44 jakw 71
 
41 jakw 72
    this->update();
77 jakw 73
 
44 jakw 74
    emit pointCloudDataChanged();
41 jakw 75
 
76
}
77
 
44 jakw 78
void SMPointCloudWidget::updatePointCloud(SMPointCloud pointCloud, int id){
2 jakw 79
 
46 jakw 80
    std::string stringId = QString::number(id).toStdString();
81
//    std::string stringId = QString("id%1").arg(id).toStdString();
45 jakw 82
 
2 jakw 83
    // Note: using the color handler makes a copy of the rgb fields
44 jakw 84
    colorHandler->setInputCloud(pointCloud.pointCloud);
2 jakw 85
 
45 jakw 86
    if(!visualizer->updatePointCloud(pointCloud.pointCloud, *colorHandler, stringId)){
87
        visualizer->addPointCloud(pointCloud.pointCloud, *colorHandler, stringId);
208 flgw 88
        visualizer->setPointCloudRenderingProperties(
89
                    pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2.0, stringId);
2 jakw 90
     }
91
 
92
    this->update();
44 jakw 93
    emit pointCloudDataChanged();
2 jakw 94
 
95
}
96
 
44 jakw 97
void SMPointCloudWidget::removePointCloud(int id){
2 jakw 98
 
46 jakw 99
    std::string stringId = QString::number(id).toStdString();
100
//    std::string stringId = QString("id%1").arg(id).toStdString();
2 jakw 101
 
44 jakw 102
    visualizer->removePointCloud(stringId);
2 jakw 103
 
44 jakw 104
    this->update();
105
    emit pointCloudDataChanged();
2 jakw 106
 
107
}
108
 
139 jakw 109
void SMPointCloudWidget::removeAllPointClouds(){
110
 
111
    visualizer->removeAllPointClouds();
112
 
113
    this->update();
114
    emit pointCloudDataChanged();
115
 
116
}
117
 
2 jakw 118
void SMPointCloudWidget::saveScreenShot(){
119
 
120
    vtkWindowToImageFilter* filter = vtkWindowToImageFilter::New();
121
    //filter->SetInput(visualizer->getRenderWindow());
122
    filter->Modified();
123
 
124
    QString fileName = QFileDialog::getSaveFileName(this, "Save Screen Shot", QString(), "*.png");
125
    QFileInfo info(fileName);
126
    QString type = info.suffix();
127
    if(type == "")
128
        fileName.append(".png");
129
 
130
    vtkPNGWriter* writer = vtkPNGWriter::New();
131
    writer->SetFileName(qPrintable(fileName));
167 jakw 132
    writer->SetInputConnection(filter->GetOutputPort());
2 jakw 133
    writer->Write();
134
    writer->Delete();
135
}
136
 
31 jakw 137
void SMPointCloudWidget::updateCalibrationParameters(){
138
 
139
    QSettings settings;
169 jakw 140
    QVariant calibrationVariant = settings.value("calibration/parameters");
141
    SMCalibrationParameters calibration = calibrationVariant.value<SMCalibrationParameters>();
31 jakw 142
 
34 jakw 143
    // camera 0 coordinate system
187 jakw 144
    visualizer->removeCoordinateSystem("camera0", 0);
76 jakw 145
    visualizer->addCoordinateSystem(100, "camera0", 0);
31 jakw 146
 
33 jakw 147
    // camera 1 coordinate system
148
    cv::Mat Transform1CV(3, 4, CV_32F);
149
    cv::Mat(calibration.R1).copyTo(Transform1CV.colRange(0, 3));
150
    cv::Mat(calibration.T1).copyTo(Transform1CV.col(3));
151
    Eigen::Affine3f Transform1;
152
    cv::cv2eigen(Transform1CV, Transform1.matrix());
153
 
187 jakw 154
    visualizer->removeCoordinateSystem("camera1", 0);
76 jakw 155
    visualizer->addCoordinateSystem(100, Transform1.inverse(), "camera1", 0);
33 jakw 156
 
176 jakw 157
//    // rotation axis coordinate system
36 jakw 158
//    cv::Mat TransformRCV(3, 4, CV_32F);
159
//    cv::Mat(calibration.Rr).copyTo(TransformRCV.colRange(0, 3));
160
//    cv::Mat(calibration.Tr).copyTo(TransformRCV.col(3));
161
//    Eigen::Affine3f TransformR;
176 jakw 162
 
36 jakw 163
//    cv::cv2eigen(TransformRCV, TransformR.matrix());
176 jakw 164
//    visualizer->removeCoordinateSystem("camera1", 0);
165
//    visualizer->addCoordinateSystem(100, TransformR.inverse(), "rotation", 0);
36 jakw 166
 
81 jakw 167
    // rotation axis pointing (0,1,0) in rotation stage frame
176 jakw 168
    cv::Vec3f O(0.0, 0.0, 0.0);
44 jakw 169
    cv::Vec3f v(0.0, 600.0, 0.0);
31 jakw 170
 
171
    // determine coefficients in camera 0 frame
36 jakw 172
    O = calibration.Rr.t()*O + -calibration.Rr.t()*calibration.Tr;
148 jakw 173
    v = calibration.Rr.t()*v + -calibration.Rr.t()*calibration.Tr - O;
31 jakw 174
 
175
    pcl::ModelCoefficients lineCoefficients;
176
    lineCoefficients.values.resize(6);
177
    lineCoefficients.values[0] = O[0];
178
    lineCoefficients.values[1] = O[1];
179
    lineCoefficients.values[2] = O[2];
180
    lineCoefficients.values[3] = v[0];
181
    lineCoefficients.values[4] = v[1];
182
    lineCoefficients.values[5] = v[2];
33 jakw 183
    visualizer->removeShape("line");
148 jakw 184
    visualizer->addLine(lineCoefficients);
36 jakw 185
 
148 jakw 186
 
187
 
31 jakw 188
}
189
 
2 jakw 190
SMPointCloudWidget::~SMPointCloudWidget(){
191
 
192
    //delete visualizer;
193
 
194
}