Subversion Repositories seema-scanner

Rev

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