-
Notifications
You must be signed in to change notification settings - Fork 33
/
test.cpp
141 lines (120 loc) · 4.79 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
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>
#ifdef WITH_SERIALIZATION
#include "serialization.h"
#endif
struct PlySaver{
PlySaver(boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> cloud, bool binary, bool use_camera, K2G & k2g):
cloud_(cloud), binary_(binary), use_camera_(use_camera), k2g_(k2g){}
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>> cloud_;
bool binary_;
bool use_camera_;
K2G & k2g_;
};
void KeyboardEventOccurred(const pcl::visualization::KeyboardEvent &event, void * data)
{
std::string 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;
}
if(pressed == "m")
{
s->k2g_.mirror();
}
#ifdef WITH_SERIALIZATION
if(pressed == "z")
{
if(!(s->k2g_.serialize_status())){
std::cout << "serialization enabled" << std::endl;
s->k2g_.enableSerialization();
}
else{
std::cout << "serialization disabled" << std::endl;
s->k2g_.disableSerialization();
}
}
#endif
if(pressed == "x")
{
s->k2g_.storeParameters();
std::cout << "stored calibration parameters" << 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";
std::cout << "Press \'s\' to store a cloud" << std::endl;
std::cout << "Press \'x\' to store the calibrations." << std::endl;
#ifdef WITH_SERIALIZATION
std::cout << "Press \'z\' to start/stop serialization." << std::endl;
#endif
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();
k2g.printParameters();
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, k2g);
viewer->registerKeyboardCallback(KeyboardEventOccurred, (void*)&ps);
cv::Mat color, depth;
while(!viewer->wasStopped()){
viewer->spinOnce ();
std::chrono::high_resolution_clock::time_point tnow = std::chrono::high_resolution_clock::now();
k2g.get(color, depth, cloud);
// Showing only color since depth is float and needs conversion
cv::imshow("color", color);
int c = cv::waitKey(1);
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;
}