Subversion Repositories seema-scanner

Rev

Rev 2 | Rev 33 | 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>
4
 
2 jakw 5
#include <vtkWindowToImageFilter.h>
6
#include <vtkPNGWriter.h>
7
#include <vtkRenderWindow.h>
8
 
9
#include <pcl/point_cloud.h>
10
#include <pcl/point_types.h>
11
#include <pcl/common/io.h>
12
#include <pcl/visualization/point_cloud_color_handlers.h>
13
#include <pcl/geometry/quad_mesh.h>
14
 
15
#include <pcl/io/pcd_io.h>
16
#include <pcl/io/ascii_io.h>
17
#include <pcl/io/ply_io.h>
18
#include <pcl/io/png_io.h>
19
#include <pcl/io/vtk_io.h>
20
#include <vtkPolyDataWriter.h>
21
#include <pcl/conversions.h>
22
 
23
#include <fstream>
24
 
25
#include <QFileDialog>
31 jakw 26
#include <QSettings>
2 jakw 27
 
31 jakw 28
#include "SMCalibrationParameters.h"
2 jakw 29
 
30
SMPointCloudWidget::SMPointCloudWidget(QWidget *parent) : QVTKWidget(parent) {
31
 
32
    visualizer = new pcl::visualization::PCLVisualizer("PCLVisualizer", false);
33
    this->SetRenderWindow(visualizer->getRenderWindow());
34
 
35
    visualizer->setShowFPS(false);
36
 
37
    // Create point cloud viewport
38
    visualizer->setBackgroundColor(0, 0, 0);
31 jakw 39
    visualizer->addCoordinateSystem(50, 0);
2 jakw 40
#ifndef __APPLE__
41
    // this leads to a segfault on OS X
42
    visualizer->setCameraPosition(0,0,-50,0,0,0,0,-1,0);
43
#endif
44
 
45
    // Initialize point cloud color handler
46
    colorHandler = new pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB>();
47
 
48
    //time.start();
49
}
50
 
51
void SMPointCloudWidget::updatePointCloud(PointCloudConstPtr _pointCloudPCL){
52
 
53
//    time.restart();
54
 
55
    pointCloudPCL = _pointCloudPCL;
56
 
57
    // Note: using the color handler makes a copy of the rgb fields
58
    colorHandler->setInputCloud(_pointCloudPCL);
59
 
60
    if(!visualizer->updatePointCloud(pointCloudPCL, *colorHandler, "pointCloudPCL")){
61
        visualizer->addPointCloud(pointCloudPCL, *colorHandler, "pointCloudPCL");
62
        visualizer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2.0, "pointCloudPCL");
63
     }
64
 
65
    this->update();
66
    emit newPointCloudDisplayed();
67
 
68
//    std::cout << "PCL Widget: " << time.restart() << "ms" << std::endl;
69
}
70
 
71
void SMPointCloudWidget::savePointCloud(){
72
 
73
    QString selectedFilter;
74
    QString fileName = QFileDialog::getSaveFileName(this, "Save Point Cloud", QString(), "*.pcd;;*.ply;;*.vtk;;*.png;;*.txt", &selectedFilter);
75
    QFileInfo info(fileName);
76
    QString type = info.suffix();
77
    if(type == ""){
78
        fileName.append(selectedFilter.remove(0,1));
79
        type = selectedFilter.remove(0,1);
80
    }
81
 
82
    if(type == "pcd"){
83
        pcl::io::savePCDFileASCII(fileName.toStdString(), *pointCloudPCL);
84
    } else if(type == "ply"){
85
        //pcl::io::savePLYFileBinary(fileName.toStdString(), *pointCloudPCL);
86
        pcl::PLYWriter w;
87
        // Write to ply in binary without camera
88
        w.write<pcl::PointXYZRGB> (fileName.toStdString(), *pointCloudPCL, true, false);
89
    } else if(type == "vtk"){
90
        pcl::PCLPointCloud2 pointCloud2;
91
        pcl::toPCLPointCloud2(*pointCloudPCL, pointCloud2);
92
        pcl::io::saveVTKFile(fileName.toStdString(), pointCloud2);
93
 
94
//        vtkPolyData polyData;
95
//        pcl::io::pointCloudTovtkPolyData(*pointCloudPCL, polyData);
96
//        vtkPolyDataWriter::Pointer writer = vtkPolyDataWriter::New();
97
//        writer->SetInput(polyData);
98
//        writer->SetFileName(fileName.toStdString());
99
//        writer->Update();
100
    } else if(type == "png"){
101
        pcl::io::savePNGFile(fileName.toStdString(), *pointCloudPCL, "rgb");
102
    } else if(type == "txt"){
103
        std::ofstream s(fileName.toLocal8Bit());
104
        for(unsigned int r=0; r<pointCloudPCL->height; r++){
105
            for(unsigned int c=0; c<pointCloudPCL->width; c++){
106
                pcl::PointXYZRGB p = pointCloudPCL->at(c, r);
107
                if(p.x == p.x)
108
                    s << p.x << " " << p.y << " " << p.z << "\r\n";
109
            }
110
        }
111
        std::flush(s);
112
        s.close();
113
    }
114
 
115
}
116
 
117
void SMPointCloudWidget::saveScreenShot(){
118
 
119
    vtkWindowToImageFilter* filter = vtkWindowToImageFilter::New();
120
    //filter->SetInput(visualizer->getRenderWindow());
121
    filter->Modified();
122
 
123
    QString fileName = QFileDialog::getSaveFileName(this, "Save Screen Shot", QString(), "*.png");
124
    QFileInfo info(fileName);
125
    QString type = info.suffix();
126
    if(type == "")
127
        fileName.append(".png");
128
 
129
    vtkPNGWriter* writer = vtkPNGWriter::New();
130
    writer->SetInput(filter->GetOutput());
131
    writer->SetFileName(qPrintable(fileName));
132
    writer->Write();
133
    writer->Delete();
134
}
135
 
31 jakw 136
void SMPointCloudWidget::updateCalibrationParameters(){
137
 
138
    QSettings settings;
139
    SMCalibrationParameters calibration = settings.value("calibration").value<SMCalibrationParameters>();
140
 
141
    // camera 0
142
    //...
143
 
144
    // rotation axis at (0,0,0) pointing (0,1,0) in rotation stage frame
145
    cv::Point3f O(0.0, 0.0, 0.0);
146
    cv::Vec3f v(0.0, 1.0, 0.0);
147
 
148
    // determine coefficients in camera 0 frame
149
    O = calibration.Rr*O + calibration.Tr;
150
    v = calibration.Rr*v + calibration.Tr;
151
 
152
    pcl::ModelCoefficients lineCoefficients;
153
    lineCoefficients.values.resize(6);
154
    lineCoefficients.values[0] = O[0];
155
    lineCoefficients.values[1] = O[1];
156
    lineCoefficients.values[2] = O[2];
157
    lineCoefficients.values[3] = v[0];
158
    lineCoefficients.values[4] = v[1];
159
    lineCoefficients.values[5] = v[2];
160
    visualizer->addLine(lineCoefficients);
161
}
162
 
2 jakw 163
SMPointCloudWidget::~SMPointCloudWidget(){
164
 
165
    //delete visualizer;
166
 
167
}