-
Notifications
You must be signed in to change notification settings - Fork 125
/
rviz_cloud_annotation_class.cpp
2765 lines (2310 loc) · 90.3 KB
/
rviz_cloud_annotation_class.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
#include "rviz_cloud_annotation_class.h"
RVizCloudAnnotation::RVizCloudAnnotation(ros::NodeHandle &nh1) : m_nh(nh1) {
nh = &nh1;
std::string param_stringA;
std::string param_stringA1;
std::string param_stringB;
std::string param_stringC;
std::string param_stringD;
std::string param_stringE;
std::string param_stringF;
std::string param_stringO;
m_nh.param<std::string>(PARAM_NAME_NORMAL_SOURCE, param_string2,
PARAM_DEFAULT_NORMAL_SOURCE); // 点云类型
m_nh.param<std::string>(DATASET_FOLDER, param_stringA,
DATASET_FOLDER_DEFAULT); // 加载点云数据集路径
m_dataset_folder = param_stringA;
m_nh.param<std::string>(IMAGE_FOLDER, param_stringA1,
IMAGE_FOLDER_DEFAULT); // 加载图片数据集路径
m_image_folder = param_stringA1;
m_nh.param<std::string>(ANNOTATION_CLOUD_FOLDER, param_stringB,
ANNOTATION_CLOUD_FOLDER_DEFAULT); // 加载点云输出路径
m_annotation_cloud_folder = param_stringB;
m_nh.param<std::string>(ANNOTATION_FILE_FOLDER, param_stringC,
ANNOTATION_FILE_FOLDER_DEFAULT); // 加载标注记录路径
m_annotation_file_folder = param_stringC;
m_nh.param<std::string>(BBOX_NAME_FOLDER, param_stringD,
BBOX_NAME_FOLDER_DEFAULT); // 加载BBOX输出路径
m_bbox_folder = param_stringD;
m_nh.param<std::string>(LABEL_NAME_FOLDER, param_stringE,
LABEL_NAME_FOLDER_DEFAULT); // 加载标签输出路径
m_label_folder = param_stringE;
m_nh.param<std::string>(LINE_NAME_FOLDER, param_stringF,
LINE_NAME_FOLDER_DEFAULT); // 加载Line输出路径
m_line_folder = param_stringF;
m_nh.param<std::string>(LOG_FILE, param_stringO,
LOG_FILE_DEFAULT); // 加载标注日志路径
m_log_file = param_stringO;
ROS_INFO("rviz_cloud_annotation: log file: %s", m_log_file.c_str());
//根据以上路径创建文件夹
mkdir(param_stringA.c_str(), S_IRWXU);
mkdir(param_stringA1.c_str(), S_IRWXU);
mkdir(param_stringB.c_str(), S_IRWXU);
mkdir(param_stringC.c_str(), S_IRWXU);
mkdir(param_stringD.c_str(), S_IRWXU);
mkdir(param_stringE.c_str(), S_IRWXU);
//加载所有待标注数据集
LoadDataSet(param_stringA, param_stringA1, param_stringB, param_stringC,
param_stringD, param_stringE, param_stringF);
//初始化一帧点云,并开启标记
InitNewCloud(*nh);
ROS_INFO("rviz_cloud_annotation: return");
}
//加载数据集及路径信息
void RVizCloudAnnotation::LoadDataSet(std::string A, std::string A1,
std::string B, std::string C,
std::string D, std::string E,
std::string F) {
StringVector label_files;
std::ifstream ifile;
ifile.open(m_log_file.c_str());
std::string s;
while (std::getline(ifile, s)) {
label_files.push_back(s);
// ROS_INFO("rviz_cloud_annotation: label_files: %s", s.c_str());
}
PointCloudFilesTool *pcft;
StringVector m_pcd_files;
pcft->getFilesList(A, m_pcd_files);
for (int k = 0; k < m_pcd_files.size(); k++) {
bool ifNew = true;
std::string FILE_NAME = m_pcd_files[k];
std::string name = A + FILE_NAME;
for (int i = 0; i < label_files.size(); i++) {
if (name.compare(label_files[i]) == 0) {
ifNew = false;
break;
}
}
if (ifNew) {
m_dataset_files.push_back(name.c_str());
std::string index = FILE_NAME.substr(0, FILE_NAME.length() - 4);
name = A1 + index + std::string(".png");
m_image_files.push_back(name.c_str());
name = B + index + std::string(".pcd");
// ROS_INFO("rviz_cloud_annotation: Annotation Clouds: %s", name.c_str());
m_annotation_cloud_files.push_back(name.c_str());
name = C + index + std::string(".ann");
// ROS_INFO("rviz_cloud_annotation: Annotation Files: %s", name.c_str());
m_annotation_file_files.push_back(name.c_str());
name = D + index + std::string(".txt");
// ROS_INFO("rviz_cloud_annotation: BBox: %s", name.c_str());
m_bbox_files.push_back(name.c_str());
name = E + index + std::string(".txt");
// ROS_INFO("rviz_cloud_annotation: Label: %s", name.c_str());
m_label_files.push_back(name.c_str());
}
}
}
//打开新的一帧
void RVizCloudAnnotation::onNew(const std_msgs::UInt32 &label_msg) {
//保存
Save(false);
//清除旧Markers
for (int i = 0; i <= BBOX_ID; i++) {
if (BBOX_SET[i][10] > 1) {
EmptyBboxToMarker(i);
ROS_INFO("rviz_cloud_annotation: Delete BBOX ID: %i", i);
}
}
for (int i = 0; i <= KERB_ID; i++) {
EmptyKerbToMarker(i);
}
for (int i = 0; i <= LANE_ID; i++) {
EmptyLaneToMarker(i);
}
EmptyPlaneToMarker(PLANE_ID);
if (FILE_ID < m_dataset_files.size() - 1) {
FILE_ID++;
//加载新的一帧并开启标注
InitNewCloud(*nh);
}
}
//初始化新的一帧点云,并开启标注
void RVizCloudAnnotation::InitNewCloud(ros::NodeHandle &nh) {
//初始化Markers参数
BBOX_ID = 0;
KERB_ID = 0;
LANE_ID = 0;
PLANE_ID = 0;
BBOX_YAW = 0;
BBOX_YAW_ANGLE = 0;
if_tilt = false;
m_label.clear();
for (int i = 0; i < BBOXNUMBER_LINEPOINTNUMBER; i++) {
ids_in_bbox[i].clear();
m_bbox_occluded[i] = 0;
for (int j = 0; j < 11; j++) {
if (j < 6) {
m_box_bias[i][j] = 0;
}
BBOX_SET[i][j] = 0;
if (j < 10) {
BBOX_LABEL_SET[i][j] = 0;
}
}
}
//初始化Kerb
for (int k = 0; k < LINENUMBER; k++) {
for (int i = 0; i < KERB_SIZE[k]; i++) {
KERB_SET[k][i][0] = 0;
KERB_SET[k][i][1] = 0;
KERB_SET[k][i][2] = 0;
KERB_SET[k][i][3] = 0;
}
ids_in_kerb[k].clear();
KERB_SIZE[k] = 0;
}
//初始化Lane
for (int k = 0; k < LINENUMBER; k++) {
for (int i = 0; i < LANE_SIZE[k]; i++) {
LANE_SET[k][i][0] = 0;
LANE_SET[k][i][1] = 0;
LANE_SET[k][i][2] = 0;
LANE_SET[k][i][3] = 0;
}
ids_in_lane[k].clear();
LANE_SIZE[k] = 0;
}
//初始化Plane
ids_in_plane.clear();
ids_in_plane_flag.clear();
//初始化系统参数
std::string param_string;
double param_double;
int param_int;
m_nh.param<std::string>(PARAM_NAME_UPDATE_TOPIC, param_string,
PARAM_DEFAULT_UPDATE_TOPIC);
m_interactive_marker_server =
InteractiveMarkerServerPtr(new InteractiveMarkerServer(param_string));
m_cloud = PointXYZRGBNormalCloud::Ptr(new PointXYZRGBNormalCloud);
if (!m_dataset_files.empty()) {
ROS_INFO("rviz_cloud_annotation: files exist:");
try {
std::string file = m_dataset_files[FILE_ID];
std::string file1 = "$(env HOME)/Path_Call.txt";
ROS_INFO("rviz_cloud_annotation: file_now: %s", file.c_str());
LoadCloud(file, param_string2, *m_cloud); //加载点云
} catch (const std::string &msg) {
ROS_FATAL("rviz_cloud_annotation: %s", msg.c_str());
std::exit(1);
}
m_kdtree = KdTree::Ptr(new KdTree);
m_kdtree->setInputCloud(m_cloud);
{
PointNeighborhood::Conf conf;
m_nh.param<int>(PARAM_NAME_NEIGH_SEARCH_TYPE, param_int,
PARAM_DEFAULT_NEIGH_SEARCH_TYPE);
m_nh.param<std::string>(PARAM_NAME_NEIGH_SEARCH_PARAMS, param_string,
PARAM_DEFAULT_NEIGH_SEARCH_PARAMS);
if (param_int == PARAM_DEFAULT_NEIGH_SEARCH_TYPE &&
param_string == PARAM_DEFAULT_NEIGH_SEARCH_PARAMS) {
ROS_INFO("rviz_cloud_annotation: parameter %s is at default value, "
"using %s instead.",
PARAM_NAME_NEIGH_SEARCH_PARAMS,
PARAM_NAME_NEIGH_SEARCH_DISTANCE);
m_nh.param<double>(PARAM_NAME_NEIGH_SEARCH_DISTANCE, param_double,
PARAM_DEFAULT_NEIGH_SEARCH_DISTANCE);
param_string = boost::lexical_cast<std::string>(param_double);
}
try {
conf.searcher =
PointNeighborhoodSearch::CreateFromString(param_int, param_string);
} catch (const PointNeighborhoodSearch::ParserException &ex) {
ROS_ERROR("rviz_cloud_annotation: could not configure point "
"neighborhood search from ROS param: %s",
ex.message.c_str());
conf.searcher = PointNeighborhoodSearch::CreateFromString(
PARAM_DEFAULT_NEIGH_SEARCH_TYPE,
boost::lexical_cast<std::string>(
PARAM_DEFAULT_NEIGH_SEARCH_DISTANCE));
}
m_nh.param<double>(PARAM_NAME_COLOR_IMPORTANCE, param_double,
PARAM_DEFAULT_COLOR_IMPORTANCE);
conf.color_importance = param_double;
m_nh.param<double>(PARAM_NAME_NORMAL_IMPORTANCE, param_double,
PARAM_DEFAULT_NORMAL_IMPORTANCE);
conf.normal_importance = param_double;
m_nh.param<double>(PARAM_NAME_POSITION_IMPORTANCE, param_double,
PARAM_DEFAULT_POSITION_IMPORTANCE);
conf.position_importance = param_double;
m_nh.param<double>(PARAM_NAME_MAX_DISTANCE, param_double,
PARAM_DEFAULT_MAX_DISTANCE);
conf.max_distance = param_double;
ROS_INFO("rviz_cloud_annotation: building point neighborhood...");
m_point_neighborhood =
PointNeighborhood::ConstPtr(new PointNeighborhood(m_cloud, conf));
ROS_INFO("rviz_cloud_annotation: done.");
}
}
m_nh.param<int>(PARAM_NAME_WEIGHT_STEPS, param_int,
PARAM_DEFAULT_WEIGHT_STEPS);
m_control_point_max_weight = std::max<uint32>(1, param_int);
m_nh.param<std::string>(PARAM_NAME_CONTROL_POINT_WEIGHT_TOPIC, param_string,
PARAM_DEFAULT_CONTROL_POINT_WEIGHT_TOPIC);
m_control_points_weight_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onControlPointWeightChange, this);
m_nh.param<std::string>(PARAM_NAME_CONTROL_POINT_MAX_WEIGHT_TOPIC,
param_string,
PARAM_DEFAULT_CONTROL_POINT_MAX_WEIGHT_TOPIC);
m_control_point_weight_max_weight_pub =
nh.advertise<std_msgs::Int32>(param_string, 1, true);
m_nh.param<int>(PARAM_NAME_YAW_MAX, param_int, PARAM_DEFAULT_YAW_MAX);
m_control_yaw_max = std::max<int32>(1, param_int);
m_nh.param<int>(PARAM_NAME_YAW_MIN, param_int, PARAM_DEFAULT_YAW_MIN);
m_control_yaw_min = std::min<int32>(1, param_int);
RVizCloudAnnotationPoints::Ptr default_annotation =
RVizCloudAnnotationPoints::Ptr(new RVizCloudAnnotationPoints(
m_cloud->size(), m_control_point_max_weight, m_point_neighborhood));
m_annotation = default_annotation;
m_undo_redo.SetAnnotation(default_annotation);
std::string frame_id;
m_nh.param<std::string>(PARAM_NAME_FRAME_ID, frame_id,
PARAM_DEFAULT_FRAME_ID);
std::string index;
int2str(FILE_ID, index);
m_frame_id = frame_id;
ROS_INFO("rviz_cloud_annotation: %s", m_frame_id.c_str());
m_nh.param<double>(PARAM_NAME_POINT_SIZE, param_double,
PARAM_DEFAULT_POINT_SIZE);
m_point_size = param_double;
m_nh.param<double>(PARAM_NAME_LABEL_SIZE, param_double,
PARAM_DEFAULT_LABEL_SIZE);
m_label_size = param_double;
m_nh.param<double>(PARAM_NAME_CONTROL_LABEL_SIZE, param_double,
PARAM_DEFAULT_CONTROL_LABEL_SIZE);
m_control_label_size = param_double;
m_nh.param<bool>(PARAM_NAME_SHOW_POINTS_BACK_LABELS,
m_show_points_back_labels,
PARAM_DEFAULT_SHOW_POINTS_BACK_LABELS);
m_nh.param<std::string>(PARAM_NAME_CONTROL_POINT_VISUAL, param_string,
PARAM_DEFAULT_CONTROL_POINT_VISUAL);
if (param_string == PARAM_VALUE_CONTROL_POINT_VISUAL_LINE)
m_control_points_visual = CONTROL_POINT_VISUAL_LINE;
else if (param_string == PARAM_VALUE_CONTROL_POINT_VISUAL_SPHERE)
m_control_points_visual = CONTROL_POINT_VISUAL_SPHERE;
else if (param_string == PARAM_VALUE_CONTROL_POINT_VISUAL_THREE_SPHERES)
m_control_points_visual = CONTROL_POINT_VISUAL_THREE_SPHERES;
else {
ROS_ERROR("rviz_cloud_annotation: invalid value for parameter %s: %s",
PARAM_NAME_CONTROL_POINT_VISUAL, param_string.c_str());
m_control_points_visual = CONTROL_POINT_VISUAL_SPHERE;
}
m_nh.param<double>(PARAM_NAME_CP_WEIGHT_SCALE_FRACTION, param_double,
PARAM_DEFAULT_CP_WEIGHT_SCALE_FRACTION);
m_cp_weight_scale_fraction =
std::min<float>(1.0, std::max(0.0, param_double));
m_nh.param<bool>(PARAM_NAME_ZERO_WEIGHT_CP_SHOW,
m_show_zero_weight_control_points,
PARAM_DEFAULT_ZERO_WEIGHT_CP_SHOW);
m_nh.param<std::string>(PARAM_NAME_RECT_SELECTION_TOPIC, param_string,
PARAM_DEFAULT_RECT_SELECTION_TOPIC);
m_rect_selection_sub =
m_nh.subscribe(param_string, 1,
&RVizCloudAnnotation::onRectangleSelectionViewport, this);
m_nh.param<std::string>(PARAM_NAME_SAVE_TOPIC, param_string,
PARAM_DEFAULT_SAVE_TOPIC);
m_save_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onSave, this);
m_nh.param<std::string>(PARAM_NAME_RESTORE_TOPIC, param_string,
PARAM_DEFAULT_RESTORE_TOPIC);
m_restore_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onRestore, this);
m_nh.param<std::string>(PARAM_NAME_CLEAR_TOPIC, param_string,
PARAM_DEFAULT_CLEAR_TOPIC);
m_clear_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onClear, this);
m_nh.param<std::string>(PARAM_NAME_NEW_TOPIC, param_string,
PARAM_DEFAULT_NEW_TOPIC);
m_new_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onNew, this);
m_nh.param<std::string>(PARAM_NAME_SET_EDIT_MODE_TOPIC, param_string,
PARAM_DEFAULT_SET_EDIT_MODE_TOPIC);
m_set_edit_mode_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onSetEditMode, this);
m_nh.param<std::string>(PARAM_NAME_TOGGLE_NONE_TOPIC, param_string,
PARAM_DEFAULT_TOGGLE_NONE_TOPIC);
m_toggle_none_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onToggleNoneMode, this);
m_nh.param<std::string>(PARAM_NAME_SET_CURRENT_LABEL_TOPIC, param_string,
PARAM_DEFAULT_SET_CURRENT_LABEL_TOPIC);
m_set_current_label_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onSetCurrentLabel, this);
m_nh.param<std::string>(PARAM_NAME_SET_EDIT_MODE_TOPIC2, param_string,
PARAM_DEFAULT_SET_EDIT_MODE_TOPIC2);
m_set_edit_mode_pub = m_nh.advertise<std_msgs::UInt32>(param_string, 1);
m_nh.param<std::string>(PARAM_NAME_CURRENT_LABEL_TOPIC, param_string,
PARAM_DEFAULT_CURRENT_LABEL_TOPIC);
m_set_current_label_pub = m_nh.advertise<std_msgs::UInt32>(param_string, 1);
m_nh.param<std::string>(PARAM_NAME_POINT_COUNT_UPDATE_TOPIC, param_string,
PARAM_DEFAULT_POINT_COUNT_UPDATE_TOPIC);
m_point_count_update_pub =
m_nh.advertise<std_msgs::UInt64MultiArray>(param_string, 1, true);
m_nh.param<std::string>(SAVE_BBOX_NAME, m_bbox_save, SAVE_BBOX_DEFAULT_NAME);
m_nh.param<std::string>(PARAM_NAME_SET_NAME_TOPIC, param_string,
PARAM_DEFAULT_SET_NAME_TOPIC);
m_set_name_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onSetName, this);
bbox_marker_pub =
m_nh.advertise<visualization_msgs::Marker>("bbox_marker", 1, true);
bbox_head_marker_pub =
m_nh.advertise<visualization_msgs::Marker>("bbox_head_marker", 1, true);
lane_marker_pub =
m_nh.advertise<visualization_msgs::Marker>("lane_marker", 1, true);
kerb_marker_pub =
m_nh.advertise<visualization_msgs::Marker>("kerb_marker", 1, true);
plane_marker_pub =
m_nh.advertise<visualization_msgs::Marker>("plane_marker", 1, true);
m_nh.param<std::string>(PARAM_NAME_SET_NAME_TOPIC2, param_string,
PARAM_DEFAULT_SET_NAME_TOPIC2);
m_set_name_pub = m_nh.advertise<std_msgs::String>(param_string, 1, true);
m_nh.param<std::string>(PARAM_NAME_VIEW_CONTROL_POINTS_TOPIC, param_string,
PARAM_DEFAULT_VIEW_CONTROL_POINTS_TOPIC);
m_view_control_points_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onViewControlPoints, this);
m_nh.param<std::string>(PARAM_NAME_VIEW_CLOUD_TOPIC, param_string,
PARAM_DEFAULT_VIEW_CLOUD_TOPIC);
m_view_cloud_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onViewCloud, this);
m_nh.param<std::string>(PARAM_NAME_VIEW_LABEL_TOPIC, param_string,
PARAM_DEFAULT_VIEW_LABEL_TOPIC);
m_view_labels_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onViewLabels, this);
m_nh.param<std::string>(PARAM_NAME_UNDO_REDO_STATE_TOPIC, param_string,
PARAM_DEFAULT_UNDO_REDO_STATE_TOPIC);
m_undo_redo_state_pub = m_nh.advertise<rviz_cloud_annotation::UndoRedoState>(
param_string, 1, true);
m_nh.param<std::string>(PARAM_NAME_UNDO_TOPIC, param_string,
PARAM_DEFAULT_UNDO_TOPIC);
m_undo_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onUndo, this);
m_nh.param<std::string>(PARAM_NAME_REDO_TOPIC, param_string,
PARAM_DEFAULT_REDO_TOPIC);
m_redo_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onRedo, this);
m_nh.param<std::string>(PARAM_NAME_NEXT_TOPIC, param_string,
PARAM_DEFAULT_NEXT_TOPIC);
m_next_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onNextObject, this);
m_nh.param<std::string>(PARAM_NAME_PRE_TOPIC, param_string,
PARAM_DEFAULT_PRE_TOPIC);
m_pre_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onPreObject, this);
m_nh.param<std::string>(PARAM_NAME_POINT_SIZE_CHANGE_TOPIC, param_string,
PARAM_DEFAULT_POINT_SIZE_CHANGE_TOPIC);
m_point_size_change_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onPointSizeChange, this);
m_nh.param<float>(PARAM_NAME_POINT_SIZE_CHANGE_MULT,
m_point_size_change_multiplier,
PARAM_DEFAULT_POINT_SIZE_CHANGE_MULT);
m_nh.param<std::string>(PARAM_NAME_YAW_TOPIC, param_string,
PARAM_DEFAULT_YAW_TOPIC);
m_control_yaw_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onControlYawChange, this);
m_nh.param<std::string>(PARAM_NAME_OCCLUDED_TOPIC, param_string,
PARAM_DEFAULT_OCCLUDED_TOPIC);
m_bbox_occluded_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onControlOccludedChange, this);
m_nh.param<std::string>(PARAM_NAME_YAW_MAX_TOPIC, param_string,
PARAM_DEFAULT_YAW_MAX_TOPIC);
m_control_yaw_max_pub = nh.advertise<std_msgs::Int32>(param_string, 1, true);
m_nh.param<std::string>(PARAM_NAME_YAW_MIN_TOPIC, param_string,
PARAM_DEFAULT_YAW_MIN_TOPIC);
m_control_yaw_min_pub = nh.advertise<std_msgs::Int32>(param_string, 1, true);
m_nh.param<std::string>(PARAM_NAME_BIAS_TOPIC, param_string,
PARAM_DEFAULT_BIAS_TOPIC);
m_control_bias_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onControlBiasChange, this);
m_nh.param<std::string>(PARAM_NAME_BIAS_ZERO_TOPIC, param_string,
PARAM_DEFAULT_BIAS_ZERO_TOPIC);
m_control_bias_zero_pub =
nh.advertise<std_msgs::Empty>(param_string, 1, true);
m_nh.param<std::string>(PARAM_NAME_GOTO_FIRST_UNUSED_TOPIC, param_string,
PARAM_DEFAULT_GOTO_FIRST_UNUSED_TOPIC);
m_goto_first_unused_sub = nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onGotoFirstUnused, this);
m_nh.param<std::string>(PARAM_NAME_GOTO_LAST_UNUSED_TOPIC, param_string,
PARAM_DEFAULT_GOTO_LAST_UNUSED_TOPIC);
m_goto_last_unused_sub = nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onGotoLastUnused, this);
m_nh.param<std::string>(PARAM_NAME_GOTO_FIRST_TOPIC, param_string,
PARAM_DEFAULT_GOTO_FIRST_TOPIC);
m_goto_first_sub =
nh.subscribe(param_string, 1, &RVizCloudAnnotation::onGotoFirst, this);
m_nh.param<std::string>(PARAM_NAME_GOTO_NEXT_UNUSED_TOPIC, param_string,
PARAM_DEFAULT_GOTO_NEXT_UNUSED_TOPIC);
m_goto_next_unused_sub = nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onGotoNextUnused, this);
m_nh.param<std::string>(PARAM_NAME_ANNOTATION_TYPE_TOPIC, param_string,
PARAM_DEFAULT_ANNOTATION_TYPE_TOPIC);
m_on_set_annotation_type_sub = m_nh.subscribe(
param_string, 1, &RVizCloudAnnotation::onSetAnnotationType, this);
m_nh.param<std::string>(PARAM_NAME_OBJECT_ID_TOPIC, param_string,
PARAM_DEFAULT_OBJECT_ID_TOPIC);
m_object_id_pub = nh.advertise<std_msgs::Int32>(param_string, 1, true);
m_nh.param<std::string>(PARAM_AUTO_PLANE_TOPIC, param_string,
PARAM_DEFAULT_AUTO_PLANE_TOPIC);
m_on_auto_plane_sub =
m_nh.subscribe(param_string, 1, &RVizCloudAnnotation::onAutoPlane, this);
m_nh.param<double>(PARAM_NAME_AUTOSAVE_TIME, param_double,
PARAM_DEFAULT_AUTOSAVE_TIME);
if (param_double >= 1.0) {
m_autosave_timer =
m_nh.createTimer(ros::Duration(param_double),
&RVizCloudAnnotation::onAutosave, this, false, false);
ROS_INFO("rviz_cloud_annotation: autosave every %f seconds.",
float(param_double));
}
m_nh.param<bool>(PARAM_NAME_AUTOSAVE_TIMESTAMP, m_autosave_append_timestamp,
PARAM_DEFAULT_AUTOSAVE_TIMESTAMP);
m_current_label = 1;
m_edit_mode = EDIT_MODE_NONE;
m_prev_edit_mode = EDIT_MODE_NONE;
m_point_size_multiplier = 1.0;
m_control_point_weight_step = m_control_point_max_weight;
m_control_yaw_step = m_control_yaw_max;
m_view_cloud = m_view_labels = m_view_control_points = true;
SendCloudMarker(true);
SendControlPointMaxWeight();
SendYawMax();
SendYawMin();
SendBiasZero();
// Restore(m_annotation_filename_in); //恢复标记
m_autosave_timer.start();
}
//加载一帧点云
void RVizCloudAnnotation::LoadCloud(const std::string &filename,
const std::string &normal_source,
PointXYZRGBNormalCloud &cloud) {
std::string pcd_type;
m_nh.param<std::string>(PARAM_NAME_PCD_TYPE, pcd_type,
PARAM_DEFAULT_PCD_TYPE);
ROS_INFO("rviz_cloud_annotation: expected pcd format: %s", pcd_type.c_str());
cloud.clear();
pcl::PCLPointCloud2 cloud2;
if (pcl::io::loadPCDFile(filename, cloud2)) // read pcd file
{
throw std::string(std::string("could not load cloud: ") + filename);
}
if (pcd_type == "XYZI") {
PointXYZICloud cloud_in;
pcl::fromPCLPointCloud2(cloud2, cloud_in); // input pointCloud as XYZI
// transform XYZI to XYZRGB
PointXYZRGBCloud xyz_rgb_cloud;
for (int64 i = 0; i < cloud_in.size(); i++) {
PointXYZRGB point;
point.x = cloud_in[i].x;
point.y = cloud_in[i].y;
point.z = cloud_in[i].z;
// float radius = m_sqrt(point.x * point.x + point.y * point.y);
if (true) // radius < DISTANCE_LIMMIT && point.z < HEIGHT_LIMMIT)
{
colorize_point_cloud(cloud_in[i].intensity,
&point); // Transform intensity to RGB
xyz_rgb_cloud.push_back(point);
}
}
pcl::copyPointCloud(xyz_rgb_cloud,
cloud); // Transform to XYZRGBNormal finally
} else if (pcd_type == "XYZRGB") {
PointXYZRGBCloud cloud_in;
pcl::fromPCLPointCloud2(cloud2, cloud_in); // input pointCloud as XYZRGB
pcl::copyPointCloud(cloud_in, cloud); // Transform to XYZRGBNormal finally
} else if (pcd_type == "XYZ") {
PointXYZCloud cloud_in;
pcl::fromPCLPointCloud2(cloud2, cloud_in); // input pointCloud as XYZ
pcl::copyPointCloud(cloud_in, cloud); // Transform to XYZRGBNormal finally
}
ROS_INFO("rviz_cloud_annotation: cloud size: %ld", cloud.size());
for (uint64 i = 0; i < cloud.size(); i++) {
ids_in_plane_flag.push_back(0);
}
// code for plane annotation, should be removed if you don't need plane
// annotation for decreasing running time.
// generateDefaultPlane(cloud);
//
}
//对点云进行 强度 --> RGB转换
void RVizCloudAnnotation::colorize_point_cloud(double intensity,
PointXYZRGB *sample) {
if (intensity > 1) {
intensity = intensity / 100;
}
intensity = intensity * 255;
int r, g, b;
double intensity_range = 255;
double wavelength;
double min_wavelength = 470;
if (intensity <= intensity_range)
wavelength =
intensity / intensity_range * (780 - min_wavelength) + min_wavelength;
else
wavelength = 780;
if ((wavelength >= 380) && (wavelength < 440)) {
r = (-(wavelength - 440) / (440 - 380)) * 255;
g = 0;
b = 255;
} else if ((wavelength >= 440) && (wavelength < 490)) {
r = 0;
g = ((wavelength - 440) / (490 - 440)) * 255;
b = 255;
} else if ((wavelength >= 490) && (wavelength < 510)) {
r = 0;
g = 255;
b = (-(wavelength - 510) / (510 - 490)) * 255;
} else if ((wavelength >= 510) && (wavelength < 580)) {
r = ((wavelength - 510) / (580 - 510)) * 255;
g = 255;
b = 0;
} else if ((wavelength >= 580) && (wavelength < 645)) {
r = 255;
g = (-(wavelength - 645) / (645 - 580)) * 255;
b = 0;
} else if ((wavelength >= 645) && (wavelength < 781)) {
r = 255;
g = 0;
b = 0;
} else {
r = 0;
g = 0;
b = 0;
}
uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b);
sample->rgb = *reinterpret_cast<float *>(&rgb);
}
//取消上一次操作
void RVizCloudAnnotation::onUndo(const std_msgs::Empty &) {
if (!m_undo_redo.IsUndoEnabled())
return;
ROS_INFO("rviz_cloud_annotation: Undo.");
const Uint64Vector changed = m_undo_redo.Undo();
SendControlPointsMarker(changed, true);
SendPointCounts(changed);
SendName();
SendUndoRedoState();
}
//恢复上一次操作
void RVizCloudAnnotation::onRedo(const std_msgs::Empty &) {
if (!m_undo_redo.IsRedoEnabled())
return;
ROS_INFO("rviz_cloud_annotation: Redo.");
const Uint64Vector changed = m_undo_redo.Redo();
SendControlPointsMarker(changed, true);
SendPointCounts(changed);
SendName();
SendUndoRedoState();
}
//清除当前标记的物体 或 清除当前帧所有标记
void RVizCloudAnnotation::onClear(const std_msgs::UInt32 &label_msg) {
const uint64 old_max_label = m_annotation->GetNextLabel();
const uint64 clear_label = label_msg.data;
if (clear_label >= old_max_label)
return;
if (clear_label != 0) {
const Uint64Vector changed = m_undo_redo.ClearLabel(clear_label);
SendControlPointsMarker(changed, true);
SendPointCounts(changed);
SendName();
SendUndoRedoState();
if (ANNOTATION_TYPE == BBOX) {
EmptyBboxToMarker(BBOX_ID);
ids_in_bbox[BBOX_ID].clear();
} else if (ANNOTATION_TYPE == KERB) {
for (int i = 0; i < KERB_SIZE[KERB_ID]; i++) {
KERB_SET[KERB_ID][i][0] = 0;
KERB_SET[KERB_ID][i][1] = 0;
KERB_SET[KERB_ID][i][2] = 0;
KERB_SET[KERB_ID][i][3] = 0;
}
ids_in_kerb[KERB_ID].clear();
KERB_SIZE[KERB_ID] = 0;
EmptyKerbToMarker(KERB_ID);
} else if (ANNOTATION_TYPE == LANE) {
for (int i = 0; i < LANE_SIZE[LANE_ID]; i++) {
LANE_SET[LANE_ID][i][0] = 0;
LANE_SET[LANE_ID][i][1] = 0;
LANE_SET[LANE_ID][i][2] = 0;
LANE_SET[LANE_ID][i][3] = 0;
}
ids_in_lane[LANE_ID].clear();
LANE_SIZE[LANE_ID] = 0;
EmptyLaneToMarker(LANE_ID);
} else if (ANNOTATION_TYPE == PLANE) {
ids_in_plane.clear();
ids_in_plane_flag.clear();
EmptyPlaneToMarker(PLANE_ID);
}
return;
}
const Uint64Vector changed = m_undo_redo.Clear();
SendPointCounts(changed);
SendControlPointsMarker(changed, true);
SendName();
SendUndoRedoState();
for (int i = 0; i <= BBOX_ID; i++) {
if (BBOX_SET[i][10] > 1) // >1 则不为空BBOX
{
EmptyBboxToMarker(i);
}
ids_in_bbox[i].clear();
}
for (int k = 0; k <= KERB_ID; k++) {
for (int i = 0; i < KERB_SIZE[k]; i++) {
KERB_SET[k][i][0] = 0;
KERB_SET[k][i][1] = 0;
KERB_SET[k][i][2] = 0;
KERB_SET[k][i][3] = 0;
}
ids_in_kerb[k].clear();
KERB_SIZE[k] = 0;
EmptyKerbToMarker(k);
}
for (int k = 0; k <= LANE_ID; k++) {
for (int i = 0; i < LANE_SIZE[k]; i++) {
LANE_SET[k][i][0] = 0;
LANE_SET[k][i][1] = 0;
LANE_SET[k][i][2] = 0;
LANE_SET[k][i][3] = 0;
}
ids_in_lane[k].clear();
LANE_SIZE[k] = 0;
EmptyLaneToMarker(k);
}
ids_in_plane.clear();
ids_in_plane_flag.clear();
EmptyPlaneToMarker(PLANE_ID);
}
//根据标志记录文件恢复当前帧的标注
void RVizCloudAnnotation::Restore(const std::string &filename) {
std::ifstream ifile(filename.c_str());
if (!ifile) {
ROS_ERROR("rviz_cloud_annotation: could not open file: %s",
filename.c_str());
return;
}
ROS_INFO("rviz_cloud_annotation: loading file: %s", filename.c_str());
RVizCloudAnnotationPoints::Ptr maybe_new_annotation;
try {
maybe_new_annotation = RVizCloudAnnotationPoints::Deserialize(
ifile, m_control_point_max_weight, m_point_neighborhood);
} catch (const RVizCloudAnnotationPoints::IOE &e) {
ROS_ERROR("rviz_cloud_annotation: could not load file %s, reason: %s.",
filename.c_str(), e.description.c_str());
return;
}
if (maybe_new_annotation->GetCloudSize() != m_annotation->GetCloudSize()) {
const uint new_size = maybe_new_annotation->GetCloudSize();
const uint old_size = m_annotation->GetCloudSize();
ROS_ERROR("rviz_cloud_annotation: file was created for a cloud of size %u, "
"but it is %u. Load operation aborted.",
new_size, old_size);
return;
}
ClearControlPointsMarker(RangeUint64(1, m_annotation->GetNextLabel()), false);
m_annotation = maybe_new_annotation;
m_undo_redo.SetAnnotation(maybe_new_annotation);
SendControlPointsMarker(RangeUint64(1, m_annotation->GetNextLabel()), true);
SendName();
SendPointCounts(RangeUint64(1, m_annotation->GetNextLabel()));
SendUndoRedoState();
ROS_INFO("rviz_cloud_annotation: file loaded.");
}
//保存当前帧的标记信息的Label
void RVizCloudAnnotation::Save(const bool is_autosave) {
if (is_autosave)
ROS_INFO("rviz_cloud_annotation: auto-saving.");
if (!m_annotation_file_files.empty()) {
std::string filename = m_annotation_file_files[FILE_ID];
if (is_autosave && m_autosave_append_timestamp) {
filename = AppendTimestampBeforeExtension(filename);
}
std::ofstream ofile(filename.c_str(), std::ios::trunc);
if (!ofile) {
ROS_ERROR("rviz_cloud_annotation: could not create file: %s",
filename.c_str());
return;
}
ROS_INFO("rviz_cloud_annotation: saving file: %s", filename.c_str());
try {
m_annotation->Serialize(ofile); //输出序列化
} catch (const RVizCloudAnnotationPoints::IOE &e) {
ROS_ERROR("rviz_cloud_annotation: could not save file %s, reason: %s.",
filename.c_str(), e.description.c_str());
}
ROS_INFO("rviz_cloud_annotation: done.");
}
if (!m_annotation_cloud_files.empty()) {
//输出点云 .PCD格式
std::string cloud_filename = m_annotation_cloud_files[FILE_ID];
if (is_autosave && m_autosave_append_timestamp)
cloud_filename = AppendTimestampBeforeExtension(cloud_filename);
ROS_INFO("rviz_cloud_annotation: saving cloud: %s", cloud_filename.c_str());
{
PointXYZRGBLCloud cloud_out;
pcl::copyPointCloud(*m_cloud, cloud_out);
if (pcl::io::savePCDFileBinary(cloud_filename, cloud_out))
ROS_ERROR("rviz_cloud_annotation: could not save labeled cloud.");
}
ROS_INFO("rviz_cloud_annotation: done.");
FinalLabel(*m_cloud);
PointXYZRGBLCloud cloud_out1;
pcl::copyPointCloud(*m_cloud, cloud_out1);
std::string save_label_name = m_label_files[FILE_ID];
std::ofstream ofile1;
ofile1.open(save_label_name.c_str(), std::ios::trunc);
ROS_INFO("rviz_cloud_annotation: filename: %s", save_label_name.c_str());
for (int i = 0; i < cloud_out1.size(); i++) {
std::string m_str_ = label2Name(m_label[i]);
ofile1 << i << "\t" << cloud_out1[i].x << "\t" << cloud_out1[i].y << "\t"
<< cloud_out1[i].z << "\t" << m_str_ << "\n";
}
ofile1.close();
m_label.clear();
}
if (!m_annotation_cloud_files.empty()) {
std::string save_bbox_name = m_bbox_files[FILE_ID];
std::ofstream ofile2;
ofile2.open(save_bbox_name.c_str(), std::ios::trunc);
ROS_INFO("rviz_cloud_annotation: bbox name: %s", save_bbox_name.c_str());
for (int i = 0; i <= BBOX_ID; i++) {
if (BBOX_SET[i][10] > 1) {
ROS_INFO("rviz_cloud_annotation: bbox id: %d", i);
ofile2 << label2Name((int)(BBOX_LABEL_SET[i][0])) << "\t";
for (int j = 1; j < 10; j++) {
ofile2 << BBOX_LABEL_SET[i][j] << "\t";
}
ofile2 << "\n";
}
}
ofile2.close();
}
if (!m_dataset_files.empty()) {
std::ofstream ofile3;
ofile3.open(m_log_file.c_str(), std::ios::app);
ofile3 << m_dataset_files[FILE_ID].c_str() << "\n";
}
}
//点的 标签序号 --> Name
std::string RVizCloudAnnotation::label2Name(int label) {
std::string m_str_ = "No";
if (label > 1000) {
return m_str_;
}
if (label == -1) {
m_str_ = "Kerb";
return m_str_;
} else if (label == -2) {
m_str_ = "Lane";
return m_str_;
} else if (label == -3) {
m_str_ = "Ground";
return m_str_;
}
int64 m_label_ = (label - 1) % 20;
m_label_ = m_label_ / 5;
switch (m_label_) {
case 0:
m_str_ = "Car";
break;
case 1:
m_str_ = "Cart";
break;
case 2:
m_str_ = "Pedestrian";
break;
case 3:
m_str_ = "Cyclist";
break;
}
return m_str_;
}
//设置操作模式
void RVizCloudAnnotation::SetEditMode(const uint64 new_edit_mode) {
if (m_edit_mode == new_edit_mode)
return;
const char *info_string;
bool send_cloud = false;
switch (new_edit_mode) {
case EDIT_MODE_NONE:
if (m_edit_mode != EDIT_MODE_NONE)
send_cloud = true;
info_string = "Move";
break;
case EDIT_MODE_CONTROL_POINT:
if (m_edit_mode == EDIT_MODE_NONE)
send_cloud = true;
info_string = "Point";
break;