-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproj.cxx
361 lines (315 loc) · 10.5 KB
/
proj.cxx
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <iostream>
#include <stdexcept>
#include <cstdlib>
#include <string>
#include <SFML/Graphics.hpp>
#include "menu_overlay.hxx"
#include "utility.hxx"
#include "widgets.hxx"
#include "options.hxx"
#include "kinect.hxx"
class DrawOptions
{
public:
DrawOptions(
bool draw_depth = true,
bool draw_fps = false,
bool draw_users = true,
bool draw_joints = true,
bool draw_menu = false
) :
draw_depth_(draw_depth),
draw_fps_(draw_fps),
draw_users_(draw_users),
draw_joints_(draw_joints),
draw_menu_(draw_menu)
{}
bool draw_depth() const
{
if (draw_menu_)
return false;
else
return draw_depth_;
}
void set_draw_depth(bool draw_depth)
{
draw_depth_ = draw_depth;
if (draw_depth)
draw_menu_ = false;
}
bool draw_fps() const
{
return draw_fps_;
}
void set_draw_fps(bool draw_fps)
{
draw_fps_ = draw_fps;
}
bool draw_users() const
{
if (draw_menu_)
return false;
else
return draw_users_;
}
void set_draw_users(bool draw_users)
{
draw_users_ = draw_users;
if (draw_users)
draw_menu_ = false;
}
bool draw_joints() const
{
if (draw_menu_)
return false;
else
return draw_joints_;
}
void set_draw_joints(bool draw_joints)
{
draw_joints_ = draw_joints;
if (draw_joints)
draw_menu_ = false;
}
bool draw_menu() const
{
return draw_menu_;
}
void set_draw_menu(bool draw_menu)
{
draw_menu_ = draw_menu;
}
private:
bool draw_depth_;
bool draw_fps_;
bool draw_users_;
bool draw_joints_;
bool draw_menu_;
};
int main(int argc, char** argv)
{
using namespace std;
using namespace kin;
// Read the xml file from command line.
if (argc != 2)
throw runtime_error("Wrong number of arguments.");
string xml_filename = argv[1];
// The drawing options.
DrawOptions draw_opts;
bool FULLSCREEN = true;
draw_opts.set_draw_menu(true);
// Window width and height.
size_t WIDTH = 800;
size_t HEIGHT = 600;
if (FULLSCREEN)
{
auto mode = sf::VideoMode::getDesktopMode();
WIDTH = mode.width;
HEIGHT = mode.height;
}
// Create the kinect sensor.
kin::KinectSensor k;
double const SCALE_X = WIDTH / (double) k.x_res();
double const SCALE_Y = HEIGHT / (double) k.y_res();
// Load the default font.
opts.load_default_font("fonts/opensans/OpenSans-Regular.ttf");
TextWidget::set_default_font(opts.default_font());
// Create the mouse widget.
opts.mouse_ = make_shared<AnimatedWidget>("animations/hand_load_2s.pf", 999);
opts.mouse_->overwrite_render_rectangle({0, 0, 75, 75});
opts.mouse_->hoverable_ = false;
opts.mouse_->stop();
opts.mouse_->repeatable_ = false;
// Create the menu overlay.
kin::MenuOverlay overlay(xml_filename, WIDTH, HEIGHT);
// Create the callback for the menu item clicks.
string call_command;
bool clicked_item = false;
overlay.handle_menu_item_click_ = [&](std::string const & s)
{
call_command = s;
clicked_item = true;
};
// Measure the FPS.
FPS fps_measure(1.0f);
sf::Text fps_text;
fps_text.setFont(opts.default_font());
fps_text.setCharacterSize(16);
// Create the sprite for the depth RGBA.
Array2D<sf::Color> depth_rgba(k.x_res(), k.y_res());
sf::Texture depth_texture;
depth_texture.create(k.x_res(), k.y_res());
sf::Sprite depth_sprite(depth_texture);
depth_sprite.setScale(SCALE_X, SCALE_Y);
// Create the sprite for the user RGBA.
Array2D<sf::Color> user_rgba(k.x_res(), k.y_res());
sf::Texture user_texture;
user_texture(k.x_res(), k.y_res());
sf::Sprite user_sprite(user_texture);
user_sprite.setScale(SCALE_X, SCALE_Y);
// Create the texture for the user joints.
sf::Image joint_texture;
joint_texture.create(3, 3, sf::Color(255, 255, 255, 255));
std::vector<sf::Sprite> joint_sprites;
// Window open/close loop.
do
{
// Reset the call command.
call_command = "";
clicked_item = false;
// Create the window and go into the main loop.
float mouse_x = 0;
float mouse_y = 0;
// float mouse_z = 0;
auto style = sf::Style::Close;
if (FULLSCREEN)
style = sf::Style::Fullscreen;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Kinect menu", style);
window.setMouseCursorVisible(false);
overlay.handle_close_ = [&]()
{
window.Close();
};
while (window.isOpen())
{
///////////////////////////////////////////////
// Process the input //
///////////////////////////////////////////////
// Handle the window events.
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::TextEntered)
{
if (tolower(event.text.unicode) == 'u')
draw_opts.set_draw_users(!draw_opts.draw_users());
if (tolower(event.text.unicode) == 'f')
draw_opts.set_draw_fps(!draw_opts.draw_fps());
if (tolower(event.text.unicode) == 'j')
draw_opts.set_draw_joints(!draw_opts.draw_joints());
if (tolower(event.text.unicode) == 'm')
draw_opts.set_draw_menu(!draw_opts.draw_menu());
}
}
// Process the input.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.Close();
break;
}
///////////////////////////////////////////////
// Update everything //
///////////////////////////////////////////////
// Compute the fps.
auto fps = fps_measure.update();
auto elapsed_time = fps_measure.elapsed_time();
// Update the kinect data.
auto updates = k.update(elapsed_time);
if (updates.depth_)
{
depth_to_rgba(k.depth_data(), k.z_res(), depth_rgba);
depth_texture.loadFromPixels(k.x_res(), k.y_res(), uint8_ptr(depth_rgba));
}
if (updates.user_)
{
user_to_rgba(k.user_data(), user_rgba);
user_texture.create(k.x_res(), k.y_res());
user_texture.update(uint8_ptr(user_rgba));
joint_sprites.clear();
for (auto const & user : k.users())
{
for (auto const & p : user.joints_)
{
joint_sprites.emplace_back(joint_texture);
joint_sprites.back().setPosition(SCALE_X*p.second.proj_position_.X,
SCALE_Y*p.second.proj_position_.Y);
}
}
}
// Get the hand positions.
auto hand_left = k.hand_left();
auto hand_right = k.hand_right();
bool hand_left_visible = k.hand_left_visible() && hand_left.Z >= 0.0;
bool hand_right_visible = k.hand_right_visible() && hand_right.Z >= 0.0;
bool hand_visible = hand_left_visible || hand_right_visible;
if (hand_visible)
{
bool both_visible = hand_left_visible && hand_right_visible;
if (!hand_right_visible || (both_visible && hand_left.Z > hand_right.Z))
{
mouse_x = hand_left.X * WIDTH;
mouse_y = hand_left.Y * HEIGHT;
// mouse_z = (1.5 - hand_left.Z ) * HEIGHT;
}
else
{
mouse_x = hand_right.X * WIDTH;
mouse_y = hand_right.Y * HEIGHT;
// mouse_z = (1.5 - hand_right.Z) * HEIGHT;
}
// Check that the mouse is actually visible.
if (mouse_x < 0 || mouse_x >= WIDTH || mouse_y < 0 || mouse_y >= HEIGHT)
hand_visible = false;
}
if (!hand_visible)
{
mouse_x = -1;
mouse_y = -1;
}
// Update the menu.
if (draw_opts.draw_menu())
{
if (mouse_x == -1 || mouse_y == -1)
overlay.hide_mouse();
else
overlay.hover(mouse_x, mouse_y);
overlay.update(elapsed_time);
if (clicked_item)
window.close();
}
///////////////////////////////////////////////
// Draw everything //
///////////////////////////////////////////////
// Clear to black.
window.clear();
// Draw the depth map.
if (draw_opts.draw_depth())
{
window.draw(depth_sprite);
}
// Draw the users.
if (draw_opts.draw_users())
{
window.draw(user_sprite);
}
// Draw the joints.
if (draw_opts.draw_joints())
{
for (auto const & spr : joint_sprites)
{
window.draw(spr);
}
}
// Draw the menu.
if (draw_opts.draw_menu())
overlay.render(window);
// Draw the FPS text.
if (draw_opts.draw_fps())
{
fps_text.setString("FPS: " + to_string(fps));
window.draw(fps_text);
}
// Show the drawed stuff on the window.
window.display();
}
// Start the next command.
if (call_command.size() > 0)
{
std::cout << "Starting: " << call_command << std::endl;
auto ret = system(call_command.c_str());
std::cout << "Returned with value: " << ret << std::endl;
}
} while (call_command.size() > 0);
}