Subversion Repositories seema-scanner

Rev

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