-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
1045 lines (941 loc) · 37.9 KB
/
main.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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
****************************************************************************
*
* MODULE: v.in.kinect
* AUTHOR(S): Anna Petrasova
* PURPOSE: Import points as vector from Kinect v2
* COPYRIGHT: (C) 2015 by the GRASS Development Team
*
* This program is free software under the GNU General
* Public License (>=v2). Read the file COPYING that
* comes with GRASS for details.
*
*****************************************************************************/
/* using the most-specific-first order of includes */
#include <pcl/filters/filter.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/passthrough.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/point_types.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/surface/mls.h>
#include <pcl/common/common.h>
#include <pcl/common/transforms.h>
#include <pcl/common/angles.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/io/ply_io.h>
#include "k4adriver.h"
#include "binning.h"
#include "binning_color.h"
#include "calibrate.h"
#include "interp.h"
#include "analyses.h"
#include "drawing.h"
extern "C" {
#include <grass/gis.h>
#include <grass/vector.h>
#include <grass/raster.h>
#include <grass/glocale.h>
}
#include <stdlib.h>
#include <signal.h>
static volatile sig_atomic_t signaled = 0;
static volatile sig_atomic_t signal_new_input = 0;
void terminate (int param)
{
signaled = 1;
}
void signal_read_new_input (int param)
{
signal_new_input = 1;
}
k4a_color_resolution_t color_camera(const char* resolution)
{
k4a_color_resolution_t res = K4A_COLOR_RESOLUTION_OFF;
if (strcmp(resolution, "720P") == 0)
res = K4A_COLOR_RESOLUTION_720P;
else if (strcmp(resolution, "1080P") == 0)
res = K4A_COLOR_RESOLUTION_1080P;
else if (strcmp(resolution, "1440P") == 0)
res = K4A_COLOR_RESOLUTION_1440P;
else if (strcmp(resolution, "2160P") == 0)
res = K4A_COLOR_RESOLUTION_2160P;
return res;
}
void update_input_region(char* raster, char* region, struct Cell_head &window, double &offset, bool ®ion3D) {
if (region){ /* region= */
G_get_element_window(&window, "windows", region, "");
offset = window.bottom;
if (window.top != window.bottom)
region3D = true;
}
else if (raster) {
struct FPRange range;
double zmin, zmax;
Rast_get_cellhd(raster, "", &window);
Rast_read_fp_range(raster, "", &range);
Rast_get_fp_range_min_max(&range, &zmin, &zmax);
offset = zmin;
}
else { // current region
G_get_set_window(&window);
offset = 0;
}
}
void get_draw_type(char* draw_type_string, int &vect_type){
vect_type = -1;
if (strcmp(draw_type_string, "point") == 0)
vect_type = GV_POINT;
else if (strcmp(draw_type_string, "line") == 0)
vect_type = GV_LINE;
else if (strcmp(draw_type_string, "area") == 0)
vect_type = GV_AREA;
}
// read, parse and set values of scanning variables when new input from stdin comes
void read_new_input(char* &routput, double &zrange_min, double &zrange_max,
double &clip_N, double &clip_S, double &clip_E, double &clip_W,
double &trim_tolerance, double &rotate, double &zexag, char* &method, char* &interp_method,
int &numscan, double &smooth, double &resolution, double &color_resolution, bool &use_equalized,
struct Cell_head &window, double &offset, bool ®ion3D,
char* &color_output, char* &voutput, char * &ply,
char* &contours_output, double &contours_step,
int &draw_type, int &draw_threshold, char* &draw_output, bool &paused, bool &resume_once,
k4a_color_resolution_t& k4a_resolution, bool& depth2color, char* &camera_resolution, bool& reinit_sensor) {
char buf[200];
char **tokens;
char **tokens2;
int line;
// TODO: this leaks
for (line = 1;; line++) {
if (G_getl2(buf, 200, stdin)) {
if(buf[0] == '\0')
break;
tokens = G_tokenize(buf, "=");
if (strcmp(tokens[0], "resolution") == 0)
resolution = atof(tokens[1]);
else if (strcmp(tokens[0], "color_resolution") == 0)
color_resolution = atof(tokens[1]);
else if (strcmp(tokens[0], "smooth_radius") == 0)
smooth = atof(tokens[1]);
else if (strcmp(tokens[0], "output") == 0) {
if (tokens[1][0] == '\0')
routput = NULL;
else
routput = G_store(tokens[1]);
}
else if (strcmp(tokens[0], "zrange") == 0) {
tokens2 = G_tokenize(tokens[1], ",");
zrange_min = atof(tokens2[0])/100;
zrange_max = atof(tokens2[1])/100;
G_free_tokens(tokens2);
}
else if (strcmp(tokens[0], "trim") == 0) {
tokens2 = G_tokenize(tokens[1], ",");
clip_N = atof(tokens2[0])/100;
clip_S = atof(tokens2[1])/100;
clip_E = atof(tokens2[2])/100;
clip_W = atof(tokens2[3])/100;
G_free_tokens(tokens2);
}
else if (strcmp(tokens[0], "trim_tolerance") == 0)
trim_tolerance = atof(tokens[1]);
else if (strcmp(tokens[0], "rotate") == 0)
rotate = pcl::deg2rad(atof(tokens[1]) + 180);
else if (strcmp(tokens[0], "zexag") == 0)
zexag = atof(tokens[1]);
else if (strcmp(tokens[0], "method") == 0)
method = G_store(tokens[1]);
else if (strcmp(tokens[0], "interpolation_method") == 0)
interp_method = G_store(tokens[1]);
else if (strcmp(tokens[0], "numscan") == 0)
numscan = atoi(tokens[1]);
else if (strcmp(tokens[0], "region") == 0)
update_input_region(NULL, tokens[1], window, offset, region3D);
else if (strcmp(tokens[0], "raster") == 0)
update_input_region(tokens[1], NULL, window, offset, region3D);
// export
else if (strcmp(tokens[0], "color_output") == 0) {
if (tokens[1][0] == '\0')
color_output = NULL;
else
color_output = G_store(tokens[1]);
}
else if (strcmp(tokens[0], "vector") == 0) {
if (tokens[1][0] == '\0')
voutput = NULL;
else
voutput = G_store(tokens[1]);
}
else if (strcmp(tokens[0], "ply") == 0) {
if (tokens[1][0] == '\0')
ply = NULL;
else
ply = G_store(tokens[1]);
}
else if (strcmp(tokens[0], "flags") == 0) {
if (G_strcasestr(tokens[1], "e"))
use_equalized = true;
else
use_equalized = false;
}
// contours
else if (strcmp(tokens[0], "contours") == 0) {
if (tokens[1][0] == '\0')
contours_output = NULL;
else
contours_output = G_store(tokens[1]);
}
else if (strcmp(tokens[0], "contours_step") == 0)
contours_step = atof(tokens[1]);
// drawing
else if (strcmp(tokens[0], "draw") == 0)
get_draw_type(tokens[1], draw_type);
else if (strcmp(tokens[0], "draw_threshold") == 0)
draw_threshold = atoi(tokens[1]);
else if (strcmp(tokens[0], "draw_output") == 0) {
if (tokens[1][0] == '\0')
draw_output = NULL;
else
draw_output = G_store(tokens[1]);
}
else if (strcmp(tokens[0], "pause") == 0) {
paused = true;
}
else if (strcmp(tokens[0], "resume") == 0) {
paused = false;
}
else if (strcmp(tokens[0], "resume_once") == 0) {
resume_once = true;
}
else if (strcmp(tokens[0], "camera_resolution") == 0) {
if (strcmp(camera_resolution, tokens[1]) != 0) {
camera_resolution = tokens[1];
reinit_sensor = true;
if(strcmp(camera_resolution, "depth") == 0) {
depth2color = false;
k4a_resolution = color_camera("720P");
}
else {
depth2color = true;
k4a_resolution = color_camera(camera_resolution);
}
}
}
G_free_tokens(tokens);
}
else
break;
}
}
template <typename PointT> inline void
getMinMax(const pcl::PointCloud< PointT > &cloud, struct bound_box &bbox) {
PointT minp, maxp;
pcl::getMinMax3D (cloud, minp, maxp);
bbox.W = minp.x;
bbox.S = minp.y;
bbox.B = minp.z;
bbox.E = maxp.x;
bbox.N = maxp.y;
bbox.T = maxp.z;
}
template<typename PointT>
inline void clipNSEW(pcl::shared_ptr<pcl::PointCloud<PointT>> &cloud, double clip_N, double clip_S, double clip_E, double clip_W) {
pcl::PassThrough<PointT> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("x");
pass.setFilterLimits(-clip_W, clip_E);
pass.filter (*cloud);
pass.setInputCloud(cloud);
pass.setFilterFieldName("y");
pass.setFilterLimits(-clip_S, clip_N);
pass.filter (*cloud);
}
template<typename PointT>
inline void trimNSEW(pcl::shared_ptr<pcl::PointCloud<PointT>> &cloud, double trim_N, double trim_S, double trim_E, double trim_W) {
struct bound_box bbox;
getMinMax(*cloud, bbox);
pcl::PassThrough<PointT> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("x");
pass.setFilterLimits(bbox.W + trim_W, bbox.E - trim_E);
pass.filter (*cloud);
pass.setInputCloud(cloud);
pass.setFilterFieldName("y");
pass.setFilterLimits(bbox.S + trim_S, bbox.N - trim_N);
pass.filter (*cloud);
}
template<typename PointT>
inline void rotate_Z(pcl::shared_ptr<pcl::PointCloud<PointT>> &cloud, double angle) {
Eigen::Affine3f transform_Z = Eigen::Affine3f::Identity();
// The same rotation matrix as before; tetha radians around Z axis
transform_Z.rotate (Eigen::AngleAxisf (angle, Eigen::Vector3f::UnitZ()));
// Executing the transformation
pcl::transformPointCloud (*cloud, *cloud, transform_Z);
}
template<typename PointT>
inline void trim_Z(pcl::shared_ptr<pcl::PointCloud<PointT>> &cloud,
double zrange_min, double zrange_max) {
pcl::PassThrough<PointT> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");
pass.setFilterLimits(-zrange_max, -zrange_min);
pass.filter(*cloud);
}
template<typename PointT>
inline void smooth(pcl::shared_ptr<pcl::PointCloud<PointT>> &cloud, double radius) {
// Create a KD-Tree
typename pcl::search::KdTree<PointT>::Ptr tree (new pcl::search::KdTree<PointT>);
// Output has the PointNormal type in order to store the normals calculated by MLS
pcl::shared_ptr<pcl::PointCloud<PointT>> mls_points (new pcl::PointCloud<PointT>());
// Init object (second point type is for the normals, even if unused)
pcl::MovingLeastSquares<PointT, PointT> mls;
mls.setComputeNormals (true);
// Set parameters
mls.setInputCloud (cloud);
mls.setPolynomialOrder (0);
mls.setSearchMethod (tree);
mls.setSearchRadius (radius);
// Reconstruct
mls.process (*mls_points);
mls_points.swap(cloud);
}
int median(std::vector<int> &v)
{
std::vector<int> new_;
new_.reserve(v.size());
for (int i = 0; i < v.size(); i++) {
if (v[i] != 0)
new_.push_back(v[i]);
}
size_t n = new_.size() / 2;
nth_element(new_.begin(), new_.begin()+n, new_.end());
return new_[n];
}
template<typename PointT>
void autotrim(pcl::shared_ptr<pcl::PointCloud<PointT>> &cloud, double &clip_N, double &clip_S, double &clip_E, double &clip_W, double tolerance) {
struct bound_box bbox;
getMinMax(*cloud, bbox);
double resolution = 0.003;
int length_x = (int)((bbox.E - bbox.W) / resolution + 0.5);
int length_y = (int)((bbox.N - bbox.S) / resolution + 0.5);
std::vector<int> x_array(length_x, 0);
std::vector<int> y_array(length_y, 0);
unsigned int idx;
for(typename pcl::PointCloud<PointT>::iterator it = cloud->begin(); it!= cloud->end(); it++){
idx = int((it->x - bbox.W) / resolution);
if (idx < x_array.size())
x_array[idx] += 1;
idx = int((it->y - bbox.S) / resolution);
if (idx < y_array.size())
y_array[idx] += 1;
}
int median_x = median(x_array);
int median_y = median(y_array);
clip_N = 0;
clip_S = 0;
clip_E = 0;
clip_W = 0;
for (int i = 0; i < x_array.size(); i++) {
if (x_array[i] < tolerance * median_x) {
clip_W = (i + 1) * resolution;
}
else
break;
}
for (int i = x_array.size() - 1; i >= 0; i--) {
if (x_array[i] < tolerance * median_x) {
clip_E = (x_array.size() - i) * resolution;
}
else
break;
}
for (int i = 0; i < y_array.size(); i++) {
if (y_array[i] < tolerance * median_y) {
clip_S = (i + 1) * resolution;
}
else
break;
}
for (int i = y_array.size() - 1; i >= 0; i--) {
if (y_array[i] < tolerance * median_y) {
clip_N = (y_array.size() - i) * resolution;
}
else
break;
}
//std::cout << clip_N << " " << clip_S << " " << clip_E << " " << clip_W << std::endl;
}
int main(int argc, char **argv)
{
struct GModule *module;
struct Option *voutput_opt, *routput_opt, *color_output_opt, *ply_opt, *zrange_opt, *trim_opt, *rotate_Z_opt,
*smooth_radius_opt, *region_opt, *raster_opt, *zexag_opt, *resolution_opt, *color_resolution_opt,
*color_camera_resolution_opt, *method_opt, *interp_method_opt, *calib_matrix_opt, *numscan_opt, *trim_tolerance_opt,
*contours_map, *contours_step_opt, *draw_opt, *draw_vector_opt, *draw_threshold_opt, *nprocs_interp,
*signal_file;
struct Flag *loop_flag, *calib_flag, *calib_model_flag, *equalize_flag, *sensor_info_flag;
struct Map_info Map;
struct line_pnts *Points;
struct line_cats *Cats;
int cat = 1;
G_gisinit(argv[0]);
module = G_define_module();
G_add_keyword(_("vector"));
G_add_keyword(_("scan"));
G_add_keyword(_("points"));
module->label = _("Imports a point cloud from Azure Kinect");
module->description = _("Imports a point cloud from Azure Kinect");
routput_opt = G_define_standard_option(G_OPT_R_OUTPUT);
routput_opt->guisection = _("Output");
routput_opt->required = NO;
resolution_opt = G_define_option();
resolution_opt->key = "resolution";
resolution_opt->type = TYPE_DOUBLE;
resolution_opt->required = NO;
resolution_opt->answer = const_cast<char*>("0.002");
resolution_opt->label = _("Raster resolution");
resolution_opt->description = _("Recommended values between 0.001-0.003");
resolution_opt->guisection = _("Output");
color_output_opt = G_define_standard_option(G_OPT_R_BASENAME_OUTPUT);
color_output_opt->key = "color_output";
color_output_opt->description = _("Basename for color output");
color_output_opt->guisection = _("Output");
color_output_opt->required = NO;
color_resolution_opt = G_define_option();
color_resolution_opt->key = "color_resolution";
color_resolution_opt->type = TYPE_DOUBLE;
color_resolution_opt->required = NO;
color_resolution_opt->label = _("Raster resolution of color output");
color_resolution_opt->description = _("Recommended values between 0.001-0.003");
color_resolution_opt->guisection = _("Output");
color_camera_resolution_opt = G_define_option();
color_camera_resolution_opt->key = "camera_resolution";
color_camera_resolution_opt->type = TYPE_STRING;
color_camera_resolution_opt->required = NO;
color_camera_resolution_opt->answer = const_cast<char*>("720P");;
color_camera_resolution_opt->options = "depth,720P,1080P,1440P,2160P";
color_camera_resolution_opt->label = _("Resolution of color camera");
color_camera_resolution_opt->description = _("Color camera resolution");
color_camera_resolution_opt->guisection = _("Output");
voutput_opt = G_define_standard_option(G_OPT_V_OUTPUT);
voutput_opt->required = NO;
voutput_opt->key = "vector";
voutput_opt->guisection = _("Output");
ply_opt = G_define_standard_option(G_OPT_F_OUTPUT);
ply_opt->required = NO;
ply_opt->key = "ply";
ply_opt->description = _("Name of output binary PLY file");
ply_opt->guisection = _("Output");
zrange_opt = G_define_option();
zrange_opt->key = "zrange";
zrange_opt->type = TYPE_DOUBLE;
zrange_opt->required = NO;
zrange_opt->key_desc = "min,max";
zrange_opt->label = _("Filter range for z data (min,max)");
zrange_opt->description = _("Z is distance from scanner in cm");
zrange_opt->guisection = _("Filter");
trim_opt = G_define_option();
trim_opt->key = "trim";
trim_opt->type = TYPE_DOUBLE;
trim_opt->required = NO;
trim_opt->key_desc = "N,S,E,W";
trim_opt->description = _("Clip box from center in cm");
trim_opt->guisection = _("Filter");
trim_tolerance_opt = G_define_option();
trim_tolerance_opt->key = "trim_tolerance";
trim_tolerance_opt->type = TYPE_DOUBLE;
trim_tolerance_opt->required = NO;
trim_tolerance_opt->description = _("Influences how much are model sides trimmed automatically, "
" should be higher for rectangular models");
trim_tolerance_opt->label = _("Trim tolerance between 0 and 1");
trim_tolerance_opt->options = "0-1";
trim_tolerance_opt->guisection = _("Filter");
rotate_Z_opt = G_define_option();
rotate_Z_opt->key = "rotate";
rotate_Z_opt->type = TYPE_DOUBLE;
rotate_Z_opt->required = NO;
rotate_Z_opt->answer = const_cast<char*>("0");
rotate_Z_opt->description = _("Rotate along Z axis");
rotate_Z_opt->guisection = _("Georeferencing");
smooth_radius_opt = G_define_option();
smooth_radius_opt->key = "smooth_radius";
smooth_radius_opt->type = TYPE_DOUBLE;
smooth_radius_opt->required = NO;
smooth_radius_opt->label = _("Smooth radius");
smooth_radius_opt->description = _("Recommended values between 0.006-0.009");
region_opt = G_define_option();
region_opt->key = "region";
region_opt->key_desc = "name";
region_opt->required = NO;
region_opt->multiple = NO;
region_opt->type = TYPE_STRING;
region_opt->description = _("Region of the resulting raster");
region_opt->gisprompt = "old,windows,region";
region_opt->guisection = _("Georeferencing");
raster_opt = G_define_standard_option(G_OPT_R_MAP);
raster_opt->key = "raster";
raster_opt->required = NO;
raster_opt->multiple = NO;
raster_opt->description = _("Match resulting raster to this raster map");
raster_opt->guisection = _("Georeferencing");
zexag_opt = G_define_option();
zexag_opt->key = "zexag";
zexag_opt->type = TYPE_DOUBLE;
zexag_opt->required = NO;
zexag_opt->required = NO;
zexag_opt->answer = const_cast<char*>("1");
zexag_opt->description = _("Vertical exaggeration");
zexag_opt->guisection = _("Georeferencing");
method_opt = G_define_option();
method_opt->key = "method";
method_opt->multiple = NO;
method_opt->required = NO;
method_opt->type = TYPE_STRING;
method_opt->options = "interpolation,mean,min,max";
method_opt->answer = const_cast<char*>("mean");
method_opt->description = _("Surface reconstruction method");
interp_method_opt = G_define_option();
interp_method_opt->key = "interpolation_method";
interp_method_opt->multiple = NO;
interp_method_opt->required = NO;
interp_method_opt->type = TYPE_STRING;
interp_method_opt->options = "idw,splines";
interp_method_opt->answer = const_cast<char*>("idw");
interp_method_opt->description = _("Surface interpolation method");
nprocs_interp = G_define_option();
nprocs_interp->key = "nprocs_interpolation";
nprocs_interp->multiple = NO;
nprocs_interp->type = TYPE_INTEGER;
nprocs_interp->answer = const_cast<char*>("1");
nprocs_interp->description = _("Number of processes for parallel interpolation");
calib_matrix_opt = G_define_option();
calib_matrix_opt->key = "calib_matrix";
calib_matrix_opt->multiple = YES;
calib_matrix_opt->type = TYPE_DOUBLE;
calib_matrix_opt->required = NO;
calib_matrix_opt->description = _("Calibration matrix");
calib_matrix_opt->guisection = _("Calibration");
numscan_opt = G_define_option();
numscan_opt->answer = const_cast<char*>("1");
numscan_opt->key = "numscan";
numscan_opt->type = TYPE_INTEGER;
numscan_opt->description = _("Number of scans to integrate");
numscan_opt->required = NO;
contours_map = G_define_standard_option(G_OPT_V_MAP);
contours_map->key = "contours";
contours_map->required = NO;
contours_map->description = _("Name of contour vector map");
contours_step_opt = G_define_option();
contours_step_opt->key = "contours_step";
contours_step_opt->description = _("Increment between contour levels");
contours_step_opt->type = TYPE_DOUBLE;
contours_step_opt->required = NO;
equalize_flag = G_define_flag();
equalize_flag->key = 'e';
equalize_flag->description = _("Histogram equalized color table");
loop_flag = G_define_flag();
loop_flag->key = 'l';
loop_flag->description = _("Keep scanning in a loop");
calib_flag = G_define_flag();
calib_flag->key = 'c';
calib_flag->description = _("Calibrate sensor tilting and distance from table");
calib_flag->guisection = _("Calibration");
calib_model_flag = G_define_flag();
calib_model_flag->key = 'm';
calib_model_flag->description = _("Calibrate model position");
calib_model_flag->guisection = _("Calibration");
draw_opt = G_define_option();
draw_opt->key = "draw";
draw_opt->description = _("Draw with laser pointer");
draw_opt->type = TYPE_STRING;
draw_opt->required = NO;
draw_opt->options = "point,line,area";
draw_opt->answer = const_cast<char*>("point");
draw_opt->guisection = _("Drawing");
draw_threshold_opt = G_define_option();
draw_threshold_opt->key = "draw_threshold";
draw_threshold_opt->description = _("Brightness threshold for detecting laser pointer");
draw_threshold_opt->type = TYPE_INTEGER;
draw_threshold_opt->required = YES;
draw_threshold_opt->answer = const_cast<char*>("760");
draw_threshold_opt->guisection = _("Drawing");
draw_vector_opt = G_define_standard_option(G_OPT_V_OUTPUT);
draw_vector_opt->key = "draw_output";
draw_vector_opt->guisection = _("Drawing");
draw_vector_opt->required = NO;
signal_file = G_define_standard_option(G_OPT_F_OUTPUT);
signal_file->key = "signal_file";
signal_file->required = NO;
signal_file->description = _("File signaling scanning cycle is done");
sensor_info_flag = G_define_flag();
sensor_info_flag->key = 'i';
sensor_info_flag->description = _("Print sensor info and exit");
G_option_required(calib_flag, calib_model_flag, routput_opt, sensor_info_flag,
color_output_opt, voutput_opt, ply_opt, draw_vector_opt, NULL);
G_option_exclusive(calib_flag, calib_model_flag, NULL);
G_option_requires(routput_opt, resolution_opt, NULL);
G_option_requires(color_output_opt, resolution_opt, color_resolution_opt, NULL);
G_option_requires(contours_map, contours_step_opt, routput_opt, NULL);
G_option_requires(equalize_flag, routput_opt, NULL);
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
if (sensor_info_flag->answer) {
fprintf(stdout, "sensor=k4a\n");
return EXIT_SUCCESS;
}
// initailization of variables
double resolution = 0.002;
if (resolution_opt->answer)
resolution = atof(resolution_opt->answer);
double smooth_radius = 0.008;
if (smooth_radius_opt->answer)
smooth_radius = atof(smooth_radius_opt->answer);
char* routput = NULL;
if (routput_opt->answer)
routput = routput_opt->answer;
/* parse zrange */
double zrange_min, zrange_max;
if (zrange_opt->answer != NULL) {
zrange_min = atof(zrange_opt->answers[0])/100;
zrange_max = atof(zrange_opt->answers[1])/100;
}
/* parse trim */
double clip_N, clip_S, clip_E, clip_W;
if (trim_opt->answer != NULL) {
clip_N = atof(trim_opt->answers[0])/100;
clip_S = atof(trim_opt->answers[1])/100;
clip_E = atof(trim_opt->answers[2])/100;
clip_W = atof(trim_opt->answers[3])/100;
}
double trim_tolerance;
if (trim_tolerance_opt->answer)
trim_tolerance = atof(trim_tolerance_opt->answer);
double angle = pcl::deg2rad(atof(rotate_Z_opt->answer) + 180);
double zexag = atof(zexag_opt->answer);
Eigen::Matrix4f transform_matrix;
if (calib_matrix_opt->answer) {
transform_matrix = read_matrix(calib_matrix_opt);
}
char *method = method_opt->answer;
char *interp_method = interp_method_opt->answer;
int threads;
sscanf(nprocs_interp->answer, "%d", &threads);
if (threads < 1)
{
G_warning(_("<%d> is not valid number of threads. Number of threads will be set on <%d>"),
threads, abs(threads));
threads = abs(threads);
}
int numscan = atoi(numscan_opt->answer);
char *color_output = color_output_opt->answer;
double color_resolution = resolution;
if (color_resolution_opt->answer) {
color_resolution = atof(color_resolution_opt->answer);
}
char *voutput = voutput_opt->answer;
char *ply = ply_opt->answer;
char *contours_output = contours_map->answer;
double contours_step;
if (contours_output)
contours_step = atof(contours_step_opt->answer);
bool use_equalized = false;
if (equalize_flag->answer)
use_equalized = true;
// drawing
int vect_type;
get_draw_type(draw_opt->answer, vect_type);
int draw_threshold = atoi(draw_threshold_opt->answer);
char* draw_output = NULL;
if (draw_vector_opt->answer)
draw_output = draw_vector_opt->answer;
std::vector<double> draw_x;
std::vector<double> draw_y;
std::vector<double> draw_z;
bool drawing = false;
unsigned int last_detected_loop_count = 1e6;
struct Map_info Map_draw;
struct line_pnts *Points_draw;
struct line_cats *Cats_draw;
Points_draw = Vect_new_line_struct();
Cats_draw = Vect_new_cats_struct();
Points = Vect_new_line_struct();
Cats = Vect_new_cats_struct();
// weights for binning IDW interpolation
// size must be max 50
int max_weight_size = 50;
double **weights = (double **)G_malloc(sizeof(double *) * max_weight_size);
for (int i = 0; i < max_weight_size; i++) {
weights[i] = (double *)G_malloc(sizeof(double) * max_weight_size);
}
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>());
struct bound_box bbox;
struct Cell_head cellhd, window;
double offset, scale;
bool region3D = false;
bool paused = false;
bool resume_once = false;
update_input_region(raster_opt->answer, region_opt->answer, window, offset, region3D);
bool depth2color = true;
bool reinit_sensor = false;
char* camera_resolution = color_camera_resolution_opt->answer;
k4a_color_resolution_t k4a_resolution = color_camera("720P");
if (strcmp(camera_resolution, "depth") == 0)
depth2color = false;
else
k4a_resolution = color_camera(color_camera_resolution_opt->answer);
K4ADriver k4a;
k4a.initialize(K4A_DEPTH_MODE_NFOV_UNBINNED, k4a_resolution);
int j = 0;
int failed = 0;
// get terminating signals
signal(SIGTERM, terminate);
signal(SIGINT, terminate);
signal(SIGUSR1, signal_read_new_input);
while (j < 1) {
if (signaled == 1) {
break;
}
if (signal_new_input == 1) {
signal_new_input = 0;
read_new_input(routput, zrange_min, zrange_max, clip_N, clip_S, clip_E, clip_W,
trim_tolerance, angle, zexag, method, interp_method, numscan, smooth_radius,
resolution, color_resolution, use_equalized,
window, offset, region3D,
color_output, voutput, ply,
contours_output, contours_step,
vect_type, draw_threshold, draw_output, paused, resume_once,
k4a_resolution, depth2color, camera_resolution, reinit_sensor);
if (reinit_sensor) {
k4a.shut_down();
k4a.initialize(K4A_DEPTH_MODE_NFOV_UNBINNED, k4a_resolution);
reinit_sensor = false;
}
}
bool use_depth = false;
bool use_color = false;
if (routput || voutput || ply || calib_flag->answer || calib_model_flag->answer)
use_depth = true;
if (color_output || drawing)
use_color = true;
if (paused) {
if (!resume_once)
continue;
else
resume_once = false;
}
try {
cloud = k4a.get_cloud(use_color, depth2color);
failed = 0;
}
catch (std::runtime_error& e) {
failed++;
if (failed > 10) {
k4a.shut_down();
G_fatal_error("%s", e.what());
}
else {
G_warning("%s", e.what());
continue;
}
}
if (!drawing) {
for (int s = 0; s < numscan - 1; s++)
*(cloud) += *(k4a.get_cloud(use_color, depth2color));
}
// calibration
if(calib_flag->answer) {
calibrate(cloud);
j++;
continue;
}
// rotation of the point cloud based on calibration
if (calib_matrix_opt->answer) {
rotate_with_matrix(cloud, transform_matrix);
}
// trim Z
if (zrange_opt->answer != NULL) {
trim_Z(cloud, zrange_min, zrange_max);
}
// rotation Z
rotate_Z(cloud, angle);
// specify bounding box from center
if (trim_opt->answer != NULL) {
clipNSEW(cloud, clip_N, clip_S, clip_E, clip_W);
}
// drawing
if (draw_output) {
int maxbright = 0;
int maxbright_idx = 0;
for (int i=0; i < cloud->points.size(); i++) {
Eigen::Vector3i rgbv = cloud->points[i].getRGBVector3i();
int sum = rgbv[0] + rgbv[1] + rgbv[2];
if (sum > maxbright) {
maxbright = sum;
maxbright_idx = i;
}
}
std::cout << maxbright << std::endl;
if (maxbright >= draw_threshold) {
drawing = true;
draw_x.push_back(cloud->points[maxbright_idx].x);
draw_y.push_back(cloud->points[maxbright_idx].y);
draw_z.push_back(cloud->points[maxbright_idx].z);
last_detected_loop_count = 0;
continue;
}
else {
last_detected_loop_count++;
if (last_detected_loop_count <= 2) {
continue;
}
}
}
pcl::StatisticalOutlierRemoval<pcl::PointXYZRGB> sor;
sor.setInputCloud(cloud);
sor.setMeanK(20);
sor.setStddevMulThresh(0.5);
sor.filter(*cloud);
if(calib_model_flag->answer) {
calibrate_bbox(cloud);
j++;
continue;
}
if (trim_tolerance_opt->answer != NULL) {
double autoclip_N, autoclip_S, autoclip_E, autoclip_W;
autotrim(cloud, autoclip_N, autoclip_S, autoclip_E, autoclip_W, trim_tolerance);
if (autoclip_E > 0 || autoclip_N > 0 || autoclip_S > 0 || autoclip_W > 0)
trimNSEW(cloud, autoclip_N, autoclip_S, autoclip_E, autoclip_W);
}
if (drawing) {
// get Z scaling
getMinMax(*cloud, bbox);
if ((vect_type == GV_AREA && draw_x.size() > 2) ||
(vect_type == GV_LINE && draw_x.size() > 1) ||
(vect_type == GV_POINT && draw_x.size() > 0)) {
save_vector(draw_output, Map_draw, Points_draw, Cats_draw,
bbox, window, draw_x, draw_y, draw_z, vect_type, offset, zexag);
}
else
G_warning(_("Tolopogically incorrect vector feature"));
drawing = false;
draw_x.clear();
draw_y.clear();
draw_z.clear();
last_detected_loop_count = 1e6;
}
getMinMax(*cloud, bbox);
if (voutput|| routput || ply) {
if (smooth_radius_opt->answer)
smooth(cloud, smooth_radius);
// get Z scaling
scale = ((window.north - window.south) / (bbox.N - bbox.S) +
(window.east - window.west) / (bbox.E - bbox.W)) / 2;
}
// write to vector
if (voutput || (routput && strcmp(method, "interpolation") == 0 && strcmp(interp_method, "splines") == 0)) {
double z;
int random = std::rand();
char tmp_name[50];
sprintf(tmp_name, "%s_%d", routput, random);
if (voutput) {
if (Vect_open_new(&Map, voutput, WITH_Z) < 0)
G_fatal_error(_("Unable to create temporary vector map <%s>"), voutput);
}
else {
if (Vect_open_tmp_new(&Map, tmp_name, WITH_Z) < 0)
G_fatal_error(_("Unable to create temporary vector map <%s>"), tmp_name);
}
for (int i=0; i < cloud->points.size(); i++) {
Vect_reset_line(Points);
Vect_reset_cats(Cats);
if (region3D)
z = (cloud->points[i].z + zrange_max) * scale / zexag + offset;
else
z = (cloud->points[i].z - bbox.B) * scale / zexag + offset;
Vect_append_point(Points, cloud->points[i].x,
cloud->points[i].y,
z);
Vect_cat_set(Cats, 1, cat);
Vect_write_line(&Map, GV_POINT, Points, Cats);
}
if (strcmp(method, "interpolation") == 0 && strcmp(interp_method, "splines") == 0) {
// interpolate
Vect_rewind(&Map);
interpolate(&Map, routput, 20, 2, 50, 40, -1,
&bbox, resolution, threads);
}
Vect_close(&Map);
}
if (routput) {
if (strcmp(interp_method, "splines") != 0) {
bool interpolate_idw = strcmp(method, "interpolation") == 0;
binning(cloud, routput, &bbox, resolution,
scale, zexag, region3D ? -zrange_max : bbox.B, offset, method,
interpolate_idw, weights);
}
Rast_get_cellhd(routput, "", &cellhd);
// georeference horizontally
window.rows = cellhd.rows;
window.cols = cellhd.cols;
G_adjust_Cell_head(&window, 1, 1);
cellhd.north = window.north;
cellhd.south = window.south;
cellhd.east = window.east;
cellhd.west = window.west;
cellhd.ns_res = window.ns_res;
cellhd.ew_res = window.ew_res;
Rast_put_cellhd(routput, &cellhd);
set_default_color(routput);
if (contours_output) {
contours(routput, contours_output, contours_step);
}
if (use_equalized) {
equalized(routput);
}
}
if (color_output) {
binning_color(cloud, color_output, &bbox, color_resolution);
char* output_r = get_color_name(color_output, "r");
char* output_g = get_color_name(color_output, "g");