-
Notifications
You must be signed in to change notification settings - Fork 33
/
multi_test.cpp
163 lines (134 loc) · 5.97 KB
/
multi_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
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 K2G_generator{
public:
K2G_generator(Processor freenectprocessor, bool mirroring, char ** argv): freenectprocessor_(freenectprocessor), mirroring_(mirroring), argv_(argv),n_(0){}
K2G * operator ()(){return new K2G(freenectprocessor_, mirroring_, argv_[n_++ + 2]);}
private:
unsigned int n_;
Processor freenectprocessor_;
bool mirroring_;
char ** argv_;
};
struct PlySaver{
PlySaver(std::vector<boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>>> & clouds, bool binary, bool use_camera, std::vector<K2G *> & kinects):
binary_(binary), use_camera_(use_camera), clouds_(clouds), kinects_(kinects){}
std::vector<boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>>> & clouds_;
std::vector<K2G *> & kinects_;
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());
cv::Mat color;
for(size_t i = 0; i < s->kinects_.size(); ++i){
writer.write ("cloud_"+ std::to_string(i) + "_" + now + ".ply", *(s->clouds_[i]), s->binary_, s->use_camera_);
s->kinects_[i]->getColor(color);
cv::imwrite("color_" + std::to_string(i) + "_" + now + ".jpg", color);
}
std::cout << "saved " << "cloud and color " + now << std::endl;
}
if(pressed == "x")
{
for(auto & k : s->kinects_){
k->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" << std::endl;
std::cout << "followed by the kinect2 serials and a last 1 for mirroring" << std::endl;
std::cout << "Press \'s\' to store both clouds and color images." << std::endl;
std::cout << "Press \'x\' to store both calibrations." << std::endl;
if(argc < 3){
std::cout << "Wrong syntax! specify at least processor and one serial" << std::endl;
return -1;
}
// Set te processor to input value or to default OPENGL
Processor freenectprocessor = OPENGL;
freenectprocessor = static_cast<Processor>(atoi(argv[1]));
// check if mirroring is enabled
bool mirroring = atoi(argv[argc - 1]) == 1 ? true : false;
if(mirroring)
std::cout << "mirroring enabled" << std::endl;
// Count number of input serials which represent the number of active kinects
int kinect2_count = mirroring ? argc - 3 : argc - 2;
std::cout << "loading " << kinect2_count << " devices" << std::endl;
// Initialize container structures
std::vector<K2G *> kinects(kinect2_count);
std::vector<boost::shared_ptr<pcl::PointCloud<pcl::PointXYZRGB>>> clouds(kinect2_count);
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer ("3D viewer"));
viewer->setBackgroundColor (0, 0, 0);
// Generate all the kinect grabbers
K2G_generator kinect_generator(freenectprocessor, mirroring, argv);
std::generate(kinects.begin(), kinects.end(), kinect_generator);
// Initialize clouds and viewer viewpoints
for(size_t i = 0; i < kinect2_count; ++i)
{
clouds[i] = kinects[i]->getCloud();
clouds[i]->sensor_orientation_.w() = 0.0;
clouds[i]->sensor_orientation_.x() = 1.0;
clouds[i]->sensor_orientation_.y() = 0.0;
clouds[i]->sensor_orientation_.z() = 0.0;
viewer->addPointCloud<pcl::PointXYZRGB>(clouds[i], pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB>(clouds[i]), "sample cloud_" + i);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud_" + i);
}
// Add keyboards callbacks
PlySaver ps(clouds, false, false, kinects);
viewer->registerKeyboardCallback(KeyboardEventOccurred, (void*)&ps);
std::cout << "starting cycle" << std::endl;
std::chrono::high_resolution_clock::time_point tnow, tpost;
// Start visualization cycle
while(!viewer->wasStopped()){
viewer->spinOnce ();
tnow = std::chrono::high_resolution_clock::now();
for(size_t i = 0; i < kinect2_count; ++i)
clouds[i] = kinects[i]->updateCloud(clouds[i]);
tpost = std::chrono::high_resolution_clock::now();
std::cout << "delta " << std::chrono::duration_cast<std::chrono::duration<float>>(tpost - tnow).count() * 1000 << std::endl;
for(size_t i = 0; i < kinect2_count; ++i)
viewer->updatePointCloud<pcl::PointXYZRGB>(clouds[i], pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> (clouds[i]), "sample cloud_" + i);
}
// Close all kinect grabbers
for(auto & k : kinects)
k->shutDown();
return 0;
}