forked from giacomodabisias/libfreenect2pclgrabber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
110 lines (87 loc) · 3.81 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright 2015, Giacomo Dabisias"
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
@Author
Giacomo Dabisias, PhD Student
PERCRO, (Laboratory of Perceptual Robotics)
Scuola Superiore Sant'Anna
via Luigi Alamanni 13D, San Giuliano Terme 56010 (PI), Italy
*/
#include "k2g.h"
#include <pcl/visualization/cloud_viewer.h>
#include <chrono>
// extra headers for writing out ply file
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <pcl/io/ply_io.h>
struct PlySaver{
PlySaver(boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> cloud, bool binary, bool use_camera):
cloud_(cloud), binary_(binary), use_camera_(use_camera){}
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> cloud_;
bool binary_;
bool use_camera_;
};
void
KeyboardEventOccurred(const pcl::visualization::KeyboardEvent &event, void * data)
{
std::string pressed;
pressed = event.getKeySym();
PlySaver * s = (PlySaver*)data;
if(event.keyDown ())
{
if(pressed == "s")
{
pcl::PLYWriter writer;
std::chrono::high_resolution_clock::time_point p = std::chrono::high_resolution_clock::now();
std::string now = std::to_string((long)std::chrono::duration_cast<std::chrono::milliseconds>(p.time_since_epoch()).count());
writer.write ("cloud_" + now, *(s->cloud_), s->binary_, s->use_camera_);
std::cout << "saved " << "cloud_" + now + ".ply" << std::endl;
}
}
}
int main(int argc, char *argv[])
{
std::cout << "Syntax is: " << argv[0] << " [-processor 0|1|2] -processor options 0,1,2,3 correspond to CPU, OPENCL, OPENGL, CUDA respectively\n";
processor freenectprocessor = OPENGL;
std::vector<int> ply_file_indices;
if(argc > 1){
freenectprocessor = static_cast<processor>(atoi(argv[1]));
}
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> cloud;
K2G k2g(freenectprocessor);
std::cout << "getting cloud" << std::endl;
cloud = k2g.getCloud();
cloud->sensor_orientation_.w() = 0.0;
cloud->sensor_orientation_.x() = 1.0;
cloud->sensor_orientation_.y() = 0.0;
cloud->sensor_orientation_.z() = 0.0;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB>(cloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
PlySaver ps(cloud, false, false);
viewer->registerKeyboardCallback(KeyboardEventOccurred, (void*)&ps);
bool done = false;
while((!viewer->wasStopped()) && (!done)){
viewer->spinOnce ();
std::chrono::high_resolution_clock::time_point tnow = std::chrono::high_resolution_clock::now();
cloud = k2g.updateCloud(cloud);
std::chrono::high_resolution_clock::time_point tpost = std::chrono::high_resolution_clock::now();
std::cout << "delta " << std::chrono::duration_cast<std::chrono::duration<double>>(tpost-tnow).count() * 1000 << std::endl;
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->updatePointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud");
}
k2g.shutDown();
return 0;
}