Subversion Repositories seema-scanner

Rev

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