forked from zcZhangCheng/BinPickingSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetting.hpp
460 lines (408 loc) · 11.9 KB
/
setting.hpp
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#pragma once
#include <iostream>
#include <stdexcept>
#include <random>
#include <cstdlib>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/filesystem.hpp>
#include <pcl/console/parse.h>
const std::string default_setting_file = "./setting.ini";
#define LOAD_VALUE(key) load_value_impl(key, #key)
void error_exit(const std::string& message)
{
pcl::console::print_error(message.c_str());
std::exit(0);
//throw std::runtime_error(message);
}
class SettingBase
{
public:
SettingBase(int argc, char** argv, const std::string& section_name)
{
std::vector<int> ini_file_indices = pcl::console::parse_file_extension_argument(argc, argv, ".ini");
if (ini_file_indices.size() < 1)
{
setting_file = default_setting_file;
}
else
{
setting_file = argv[ini_file_indices[0]];
}
open_setting_file(section_name);
}
template<typename T>
void load_value_impl(T& retval, const std::string& key) const
{
if (!pt)
{
error_exit("Setting section does not loaded.");
}
const auto& val_opt = pt->get_optional<T>(key);
if (!val_opt)
{
error_exit("Cannot load " + key);
}
retval = *val_opt;
}
void open_setting_file(const std::string& key)
{
if (!setting_file)
{
error_exit("setting_file is not initialized.");
}
else
{
if (!boost::filesystem::exists(*setting_file))
{
error_exit("setting.ini does not exist.");
}
}
boost::property_tree::ptree pt_root;
boost::property_tree::read_ini(*setting_file, pt_root);
pt = pt_root.get_child_optional(key);
if (!pt)
{
error_exit(key + " does not found in setting file.");
}
}
void absolute_path(std::string& path) const
{
if (!setting_file)
{
error_exit("setting_file is not initialized.");
}
const auto& base_path = boost::filesystem::path(*setting_file).parent_path();
path = boost::filesystem::absolute(path, base_path).string();
}
boost::optional<boost::property_tree::ptree> pt;
boost::optional<std::string> setting_file;
};
class BinSceneMakerSetting : public SettingBase
{
public:
std::string load_model_path;
std::string save_centering_model_path;
std::string save_pointcloud_path;
std::string save_transforms_path;
std::string save_screenshots_dir;
int model_num;
double precapture_wait;
int falling_interval;
int postsimulation_wait;
int random_seed;
double cup_r;
double cup_h;
double cup_restitution;
double cup_friction;
double model_restitution;
double model_friction;
double normalize_length;
int downsample_target_points_num;
double downsample_initial_leaf_size;
double downsample_factor;
bool visualization;
bool capture_screenshots;
BinSceneMakerSetting(int argc, char** argv) : SettingBase(argc, argv, "binSceneMaker")
{
if (pcl::console::find_switch(argc, argv, "-h"))
{
show_help();
exit(0);
}
if (pcl::console::parse_argument(argc, argv, "--load_model_path", load_model_path) == -1)
{
LOAD_VALUE(load_model_path);
}
absolute_path(load_model_path);
if (!boost::filesystem::exists(load_model_path))
{
error_exit("load_model_path does not exist.");
}
if (pcl::console::parse_argument(argc, argv, "--save_centering_model_path", load_model_path) == -1)
{
LOAD_VALUE(save_centering_model_path);
}
absolute_path(save_centering_model_path);
boost::filesystem::create_directories(boost::filesystem::path(save_centering_model_path).parent_path());
if (pcl::console::parse_argument(argc, argv, "--save_pointcloud_path", save_pointcloud_path) == -1)
{
LOAD_VALUE(save_pointcloud_path);
}
absolute_path(save_pointcloud_path);
boost::filesystem::create_directories(boost::filesystem::path(save_pointcloud_path).parent_path());
if (pcl::console::parse_argument(argc, argv, "--save_transforms_path", save_transforms_path) == -1)
{
LOAD_VALUE(save_transforms_path);
}
absolute_path(save_transforms_path);
boost::filesystem::create_directories(boost::filesystem::path(save_transforms_path).parent_path());
if (pcl::console::parse_argument(argc, argv, "--model_num", model_num) == -1)
{
LOAD_VALUE(model_num);
}
if (model_num < 1)
{
error_exit("model_num should be larger than 0");
}
if (pcl::console::parse_argument(argc, argv, "--postsimulation_wait", postsimulation_wait) == -1)
{
LOAD_VALUE(postsimulation_wait);
}
if (postsimulation_wait < 0)
{
error_exit("postsimulation_wait should be larger than or equal to 0");
}
if (pcl::console::parse_argument(argc, argv, "--falling_interval", falling_interval) == -1)
{
LOAD_VALUE(falling_interval);
}
if (falling_interval < 1)
{
error_exit("falling_interval should be larger than 0");
}
if (pcl::console::parse_argument(argc, argv, "--random_seed", random_seed) == -1)
{
LOAD_VALUE(random_seed);
}
if (random_seed < 0)
{
random_seed = std::random_device()();
}
if (pcl::console::parse_argument(argc, argv, "--cup_r", cup_r) == -1)
{
LOAD_VALUE(cup_r);
}
if (cup_r < 0.0)
{
error_exit("cup_r should be larger than 0.0");
}
if (pcl::console::parse_argument(argc, argv, "--cup_h", cup_h) == -1)
{
LOAD_VALUE(cup_h);
}
if (cup_h < 0.0)
{
error_exit("cup_h should be larger than 0.0");
}
if (pcl::console::parse_argument(argc, argv, "--cup_restitution", cup_restitution) == -1)
{
LOAD_VALUE(cup_restitution);
}
if (cup_restitution < 0.0)
{
error_exit("cup_restitution should be larger than 0.0");
}
if (pcl::console::parse_argument(argc, argv, "--cup_friction", cup_friction) == -1)
{
LOAD_VALUE(cup_friction);
}
if (cup_friction < 0.0)
{
error_exit("cup_friction should be larger than 1.0");
}
if (pcl::console::parse_argument(argc, argv, "--model_restitution", model_restitution) == -1)
{
LOAD_VALUE(model_restitution);
}
if (model_restitution < 0.0)
{
error_exit("model_restitution should be larger than 1.0");
}
if (pcl::console::parse_argument(argc, argv, "--model_friction", model_friction) == -1)
{
LOAD_VALUE(model_friction);
}
if (model_friction < 0.0)
{
error_exit("model_friction should be larger than 1.0");
}
if (pcl::console::parse_argument(argc, argv, "--normalize_length", normalize_length) == -1)
{
LOAD_VALUE(normalize_length);
}
if (normalize_length < 0.0)
{
error_exit("normalize_length should be larger than 0.0");
}
if (pcl::console::parse_argument(argc, argv, "--downsample_target_points_num", downsample_target_points_num) == -1)
{
LOAD_VALUE(downsample_target_points_num);
}
if (downsample_target_points_num < 1)
{
error_exit("downsample_target_points_num should be larger than 0");
}
if (pcl::console::parse_argument(argc, argv, "--downsample_initial_leaf_size", downsample_initial_leaf_size) == -1)
{
LOAD_VALUE(downsample_initial_leaf_size);
}
if (downsample_initial_leaf_size < 0.0)
{
error_exit("downsample_initial_leaf_size should be larger than 0.0");
}
if (pcl::console::parse_argument(argc, argv, "--downsample_factor", downsample_factor) == -1)
{
LOAD_VALUE(downsample_factor);
}
if (downsample_factor < 1.0)
{
error_exit("downsample_factor should be larger than 1.0");
}
if (pcl::console::find_switch(argc, argv, "--visualization"))
{
visualization = true;
}
else
{
LOAD_VALUE(visualization);
}
if (pcl::console::find_switch(argc, argv, "--capture_screenshots"))
{
capture_screenshots = true;
}
else
{
LOAD_VALUE(capture_screenshots);
}
if (capture_screenshots)
{
if (!visualization)
{
pcl::console::print_error("Cannot enable capture_screenshots without visualization. capture_screenshots is set disabled.");
capture_screenshots = false;
}
else
{
if (pcl::console::parse_argument(argc, argv, "--precapture_wait", precapture_wait) == -1)
{
LOAD_VALUE(precapture_wait);
}
if (precapture_wait < 0.0)
{
error_exit("precapture_wait should be larger than 0.0");
}
if (pcl::console::parse_argument(argc, argv, "--save_screenshots_dir", save_screenshots_dir) == -1)
{
LOAD_VALUE(save_screenshots_dir);
}
absolute_path(save_screenshots_dir);
boost::filesystem::create_directories(save_screenshots_dir);
}
}
}
void show_help()
{
std::cout
<< "Usage: " << "visibleSceneMaker [setting.ini] [Options] \n\n"
<< "Options: \n"
<< " -h, --help : Show this help \n"
<< " --load_model_path [filepath] \n"
<< " --save_centering_model_path [filepath] \n"
<< " --save_pointcloud_path [filepath] \n"
<< " --save_transforms_path [filepath] \n"
<< " --save_screenshots_dir [dirpath] \n"
<< " --model_num [number] \n"
<< " --precapture_wait [time] \n"
<< " --falling_interval [number] \n"
<< " --postsimulation_wait [time] \n"
<< " --random_seed [number] \n"
<< " --cup_r [number] \n"
<< " --cup_h [number] \n"
<< " --cup_restitution [number] \n"
<< " --cup_friction [number] \n"
<< " --model_restitution [number] \n"
<< " --model_friction [number] \n"
<< " --normalize_length [number] \n"
<< " --downsample_target_points_num [number] \n"
<< " --downsample_initial_leaf_size [number] \n"
<< " --downsample_factor [number] \n"
<< " --visualization \n"
<< " --capture_screenshots \n"
<< " \n"
<< "Note: Command line options will override parameters in the setting.ini. \n"
<< std::endl;
}
};
class VisibleSceneMakerSetting : public SettingBase
{
public:
std::string load_path;
std::string save_path;
double lambda;
double camera_1_x;
double camera_1_y;
double camera_1_z;
double camera_2_x;
double camera_2_y;
double camera_2_z;
bool visualization;
VisibleSceneMakerSetting(int argc, char** argv) : SettingBase(argc, argv, "visibleSceneMaker")
{
if (pcl::console::find_switch(argc, argv, "-h"))
{
show_help();
exit(0);
}
if (pcl::console::parse_argument(argc, argv, "--load_path", load_path) == -1)
{
LOAD_VALUE(load_path);
}
absolute_path(load_path);
if (!boost::filesystem::exists(load_path))
{
error_exit("load_path does not exist.");
}
if (pcl::console::parse_argument(argc, argv, "--save_path", save_path) == -1)
{
LOAD_VALUE(save_path);
}
absolute_path(save_path);
boost::filesystem::create_directories(boost::filesystem::path(save_path).parent_path());
if (pcl::console::parse_argument(argc, argv, "--lambda", lambda) == -1)
{
LOAD_VALUE(lambda);
}
if (lambda < 0.0)
{
error_exit("lambda should be larger than 0.0");
}
if (pcl::console::find_switch(argc, argv, "--visualization"))
{
visualization = true;
}
else
{
LOAD_VALUE(visualization);
}
if (pcl::console::parse_3x_arguments(argc, argv, "--camera_1", camera_1_x, camera_1_y, camera_1_z) == -1)
{
LOAD_VALUE(camera_1_x);
LOAD_VALUE(camera_1_y);
LOAD_VALUE(camera_1_z);
}
if (pcl::console::parse_3x_arguments(argc, argv, "--camera_2", camera_2_x, camera_2_y, camera_2_z) == -1)
{
LOAD_VALUE(camera_2_x);
LOAD_VALUE(camera_2_y);
LOAD_VALUE(camera_2_z);
}
}
void show_help()
{
std::cout
<< "Usage: " << "visibleSceneMaker [setting.ini] [Options] \n\n"
<< "Options: \n"
<< " -h, --help : Show this help \n"
<< " --load_path [filepath] : load pointcloud, .ply or .pcd \n"
<< " --save_path [filepath] : save pointcloud, .ply or .pcd \n"
<< " --lambda [number] : density of pixel \n"
<< " --camera_1 [X],[Y],[Z] : Camera 1 position \n"
<< " --camera_2 [X],[Y],[Z] : Camera 2 position \n"
<< " --visualization : Enable visualization \n"
<< " \n"
<< "Note: Command line options will override parameters in the setting.ini. \n"
<< std::endl;
}
};
#undef LOAD_VALUE