forked from NNU-GISA/ALS_Refine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataio.cpp
2030 lines (1775 loc) · 57.9 KB
/
dataio.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
//
// This file is used for the Reading, Writing and Displaying of Point Cloud of various formats.
// Dependent 3rd Libs: PCL (>1.7) liblas VTK
// Author: Zhen Dong , Yue Pan et al. @ WHU LIESMARS
//
#include "dataio.h"
#include "utility.h"
#include "GeoTran.h"
#include "voxelFilter.h"
#include <pcl/visualization/common/common.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/pcd_io.h>
#include <glog/logging.h>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
using namespace utility;
using namespace boost::filesystem;
template <typename PointT>
bool DataIo<PointT>::readCloudFile(const string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud)
{
string extension;
extension = fileName.substr(fileName.find_last_of('.') + 1); //Get the suffix of the file;
if (!strcmp(extension.c_str(), "pcd"))
{
readPcdFile(fileName, pointCloud);
cout << "A pcd file has been imported" << endl;
}
else if (!strcmp(extension.c_str(), "las"))
{
bool global_shift_or_not=0;
cout << "Would you like to do a global shift ? 0. No 1. Yes [default 0]" << endl;
cin >> global_shift_or_not;
if (!global_shift_or_not)
{
readLasFile(fileName, pointCloud);
}
else
{
bool use_automatic_drift = 0;
cout << "Using the automatic shift or enter the global shift yourself ? "<<endl
<< "0. Read a global shift file 1.Use the automatic shift [default 0]" << endl;
cin >> use_automatic_drift;
readLasFile(fileName, pointCloud,use_automatic_drift);
}
cout << "A las file has been imported" << endl;
}
else if (!strcmp(extension.c_str(), "ply"))
{
readPlyFile(fileName, pointCloud);
cout << "A ply file has been imported" << endl;
}
else if (!strcmp(extension.c_str(), "txt"))
{
readTxtFile(fileName, pointCloud);
cout << "A txt file has been imported" << endl;
}
else
{
cout << "Undefined Point Cloud Format." << endl;
return 0;
}
}
template <typename PointT>
bool DataIo<PointT>::writeCloudFile(const string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud)
{
string extension;
extension = fileName.substr(fileName.find_last_of('.') + 1); //Get the suffix of the file;
if (!strcmp(extension.c_str(), "pcd"))
{
writePcdFile(fileName, pointCloud);
cout << "A pcd file has been exported" << endl;
}
else if (!strcmp(extension.c_str(), "las"))
{
bool global_shift_or_not = 0;
cout << "Would you like to do a global shift ? 0. No 1. Yes [default 0]" << endl;
cin >> global_shift_or_not;
if (!global_shift_or_not)
{
writeLasFile(fileName, pointCloud);
}
else
{
bool use_automatic_drift = 0;
cout << "Using the automatic shift or enter the global shift yourself ? " << endl
<< "0. Read a global shift file 1.Use the automatic shift [default 0]" << endl;
cin >> use_automatic_drift;
writeLasFile(fileName, pointCloud, use_automatic_drift);
}
cout << "A las file has been exported" << endl;
}
else if (!strcmp(extension.c_str(), "ply"))
{
writePlyFile(fileName, pointCloud);
cout << "A ply file has been exported" << endl;
}
else if (!strcmp(extension.c_str(), "txt"))
{
writeTxtFile(fileName, pointCloud);
cout << "A txt file has been exported" << endl;
}
else
{
cout << "Undefined Point Cloud Format." << endl;
return 0;
}
}
template <typename PointT>
bool DataIo<PointT>::readPcdFile(const std::string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud)
{
if (pcl::io::loadPCDFile<PointT>(fileName, *pointCloud) == -1)
{
PCL_ERROR("Couldn't read file\n");
return false;
}
return true;
}
template <typename PointT>
bool DataIo<PointT>::writePcdFile(const std::string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud)
{
if (pcl::io::savePCDFileBinary(fileName, *pointCloud) == -1)
{
PCL_ERROR("Couldn't write file\n");
return false;
}
return true;
}
template <typename PointT>
bool DataIo<PointT>::readLasFileHeader(const std::string &fileName, liblas::Header& header)
{
if (fileName.substr(fileName.rfind('.')).compare(".las"))
{
return 0;
}
else
{
std::ifstream ifs;
ifs.open(fileName, std::ios::in | std::ios::binary);
if (ifs.bad())
{
return 0;
}
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
header = reader.GetHeader();
}
return 1;
}
template <typename PointT>
bool DataIo<PointT>::readLasFile(const std::string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud) //Without translation
{
//cout << "A global translation or gravitization should be done to keep the precision of point cloud when adopting pcl to do las file point cloud processing" << endl;
if (fileName.substr(fileName.rfind('.')).compare(".las"))
{
return 0;
}
std::ifstream ifs;
ifs.open(fileName, std::ios::in | std::ios::binary);
if (ifs.bad())
{
cout << "Matched Terms are not found." << endl;
}
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
liblas::Header const& header = reader.GetHeader();
//Bounding box Information
double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
Xmin = header.GetMinX();
Ymin = header.GetMinY();
Zmin = header.GetMinZ();
Xmax = header.GetMaxX();
Ymax = header.GetMaxY();
Zmax = header.GetMaxZ();
while (reader.ReadNextPoint())
{
const liblas::Point& p = reader.GetPoint();
PointT pt;
pt.x = p.GetX();
pt.y = p.GetY();
pt.z = p.GetZ();
//------------------------------------------------Assign Intensity--------------------------------------------------//
//If the Point template PointT has intensity, you can assign the intensity with any feature of the point cloud in las.
//If the Point template PointT is without intensity, you should comment the line.
//pt.intensity = p.GetIntensity();
//pt.intensity = p.GetTime();
//pt.intensity = p.GetScanAngleRank();
//pt.intensity = p.GetNumberOfReturns();
//pt.intensity = p.GetScanDirection();
//---------------------------------------------------Assign Color--------------------------------------------------//
//If the Point template PointT has RGB, you can assign the Color according to the point cloud in las.
//If the Point template PointT is without RGB, you should comment the line.
//liblas::Color lasColor;
//lasColor= p.GetColor();
//pt.r = lasColor.GetRed();
//pt.g = lasColor.GetGreen();
//pt.b = lasColor.GetBlue();
pointCloud->points.push_back(pt);
}
return 1;
}
template <typename PointT>
bool DataIo<PointT>::writeLasFile(const std::string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud) //Without translation
{
Bounds bound;
getCloudBound(*pointCloud, bound);
ofstream ofs;
ofs.open(fileName, std::ios::out | std::ios::binary);
if (ofs.is_open())
{
liblas::Header header;
header.SetDataFormatId(liblas::ePointFormat2);
header.SetVersionMajor(1);
header.SetVersionMinor(2);
header.SetMin(bound.min_x, bound.min_y, bound.min_z);
header.SetMax(bound.max_x, bound.max_y, bound.max_z);
header.SetOffset((bound.min_x + bound.max_x) / 2.0, (bound.min_y + bound.max_y) / 2.0, (bound.min_z + bound.max_z) / 2.0);
header.SetScale(0.01, 0.01, 0.01);
header.SetPointRecordsCount(pointCloud->points.size());
liblas::Writer writer(ofs, header);
liblas::Point pt(&header);
for (int i = 0; i < pointCloud->points.size(); i++)
{
pt.SetCoordinates(double(pointCloud->points[i].x), double(pointCloud->points[i].y), double(pointCloud->points[i].z));
//If the Point template PointT is without intensity, you should comment the line.
//pt.SetIntensity(pointCloud->points[i].intensity);
//If the Point template PointT is without RGB, you should comment the line.
//liblas::Color lasColor;
//lasColor.SetRed(pointCloud->points[i].r);
//lasColor.SetGreen(pointCloud->points[i].g);
//lasColor.SetBlue(pointCloud->points[i].b);
//pt.SetColor(lasColor);
writer.WritePoint(pt);
}
ofs.flush();
ofs.close();
}
return 1;
}
template <typename PointT>
bool DataIo<PointT>::readLasFile(const std::string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud, bool automatic_shift_or_not) //With translation
{
global_shift.resize(3);
//cout << "A global translation or gravitization should be done to keep the precision of point cloud when adopting pcl to do las file point cloud processing" << endl;
if (fileName.substr(fileName.rfind('.')).compare(".las"))
{
return 0;
}
std::ifstream ifs;
ifs.open(fileName, std::ios::in | std::ios::binary);
if (ifs.bad())
{
cout << "Matched Terms are not found." << endl;
}
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
liblas::Header const& header = reader.GetHeader();
//Bounding box Information
double Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
Xmin = header.GetMinX();
Ymin = header.GetMinY();
Zmin = header.GetMinZ();
Xmax = header.GetMaxX();
Ymax = header.GetMaxY();
Zmax = header.GetMaxZ();
if (automatic_shift_or_not)
{
// Automatic Gloabl Shift Value;
global_shift[0] = -Xmin;
global_shift[1] = -Ymin;
global_shift[2] = -Zmin;
ofstream out("GlobalShift.txt", ios::out);
out << setiosflags(ios::fixed) << setprecision(8) << global_shift[0] << endl;
out << setiosflags(ios::fixed) << setprecision(8) << global_shift[1] << endl;
out << setiosflags(ios::fixed) << setprecision(8) << global_shift[2] << endl;
out.close();
//cout << "A txt File named GlobalShift.txt is saved in current Folder" << endl;
LOG(INFO) << "A txt File named GlobalShift.txt is saved in current Folder";
}
else
{
string fileGlobalShift;
cout << "Please enter or drag in the Global Shift File" << endl
<<"Example [GlobalShift.txt] :"<<endl
<<"-366370.90"<<endl
<<"-3451297.82"<<endl
<<"-14.29"<<endl;
cin >> fileGlobalShift;
ifstream in(fileGlobalShift, ios::in);
in >> global_shift[0];
in >> global_shift[1];
in >> global_shift[2];
in.close();
}
while (reader.ReadNextPoint())
{
const liblas::Point& p = reader.GetPoint();
PointT pt;
//A translation to keep the precision
//做一个平移,否则在UTM WGS84下的点坐标太大了,会造成精度损失的. 因为las的读取点数据是double的,而pcd是float的;
pt.x = p.GetX() + global_shift[0];
pt.y = p.GetY() + global_shift[1];
pt.z = p.GetZ() + global_shift[2];
//------------------------------------------------Assign Intensity--------------------------------------------------//
//If the Point template PointT has intensity, you can assign the intensity with any feature of the point cloud in las.
//If the Point template PointT is without intensity, you should comment the line.
//pt.intensity = p.GetIntensity();
//pt.intensity = p.GetTime();
//pt.intensity = p.GetScanAngleRank();
//pt.intensity = p.GetNumberOfReturns();
//pt.intensity = p.GetScanDirection();
//---------------------------------------------------Assign Color--------------------------------------------------//
//If the Point template PointT has RGB, you can assign the Color according to the point cloud in las.
//If the Point template PointT is without RGB, you should comment the line.
//liblas::Color lasColor;
//lasColor= p.GetColor();
//pt.r = lasColor.GetRed();
//pt.g = lasColor.GetGreen();
//pt.b = lasColor.GetBlue();
pointCloud->points.push_back(pt);
}
return 1;
}
template <typename PointT>
bool DataIo<PointT>::writeLasFile(const std::string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud, bool automatic_shift_or_not) //With translation
{
global_shift.resize(3);
Bounds bound;
getCloudBound(*pointCloud, bound);
if (!automatic_shift_or_not)
{
cout << "Use default (last input) global shift or not" << endl
<< "1. Yes 0. No" << endl;
bool use_last_shift;
cin >> use_last_shift;
if (!use_last_shift)
{
string fileGlobalShift;
cout << "Please enter or drag in the Global Shift File" << endl
<< "Example [GlobalShift.txt] :" << endl
<< "-366370.90" << endl
<< "-3451297.82" << endl
<< "-14.29" << endl;
cin >> fileGlobalShift;
ifstream in(fileGlobalShift, ios::in);
in >> global_shift[0];
in >> global_shift[1];
in >> global_shift[2];
in.close();
}
}
ofstream ofs;
ofs.open(fileName, std::ios::out | std::ios::binary);
if (ofs.is_open())
{
liblas::Header header;
header.SetDataFormatId(liblas::ePointFormat2);
header.SetVersionMajor(1);
header.SetVersionMinor(2);
header.SetMin(bound.min_x - global_shift[0], bound.min_y - global_shift[1], bound.min_z - global_shift[2]);
header.SetMax(bound.max_x - global_shift[0], bound.max_y - global_shift[1], bound.max_z - global_shift[2]);
header.SetOffset((bound.min_x + bound.max_x) / 2.0 - global_shift[0], (bound.min_y + bound.max_y) / 2.0 - global_shift[1], (bound.min_z + bound.max_z) / 2.0 - global_shift[2]);
header.SetScale(0.01, 0.01, 0.01);
header.SetPointRecordsCount(pointCloud->points.size());
liblas::Writer writer(ofs, header);
liblas::Point pt(&header);
for (size_t i = 0; i < pointCloud->points.size(); i++)
{
pt.SetCoordinates(double(pointCloud->points[i].x) - global_shift[0], double(pointCloud->points[i].y) - global_shift[1], double(pointCloud->points[i].z) - global_shift[2]);
//If the Point template PointT is without intensity, you should comment the line.
//pt.SetIntensity(pointCloud->points[i].intensity);
//If the Point template PointT is without RGB, you should comment the line.
//liblas::Color lasColor;
//lasColor.SetRed(pointCloud->points[i].r);
//lasColor.SetGreen(pointCloud->points[i].g);
//lasColor.SetBlue(pointCloud->points[i].b);
//pt.SetColor(lasColor);
writer.WritePoint(pt);
}
ofs.flush();
ofs.close();
}
return 1;
}
template <typename PointT>
bool DataIo<PointT>::readLasFileLast(const string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud)
{
if (fileName.substr(fileName.rfind('.')).compare(".las"))
{
return 0;
}
std::ifstream ifs;
ifs.open(fileName, std::ios::in | std::ios::binary);
if (ifs.bad())
{
cout << "Matched Terms are not found." << endl;
}
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
liblas::Header const& header = reader.GetHeader();
while (reader.ReadNextPoint())
{
const liblas::Point& p = reader.GetPoint();
PointT pt;
//A translation to keep the precision
//做一个平移,否则在UTM WGS84下的点坐标太大了,会造成精度损失的. 因为las的读取点数据是double的,而pcd是float的;
pt.x = p.GetX() + global_shift[0];
pt.y = p.GetY() + global_shift[1];
pt.z = p.GetZ() + global_shift[2];
//------------------------------------------------Assign Intensity--------------------------------------------------//
//If the Point template PointT has intensity, you can assign the intensity with any feature of the point cloud in las.
//If the Point template PointT is without intensity, you should comment the line.
//pt.intensity = p.GetIntensity();
//pt.intensity = p.GetTime();
//pt.intensity = p.GetScanAngleRank();
//pt.intensity = p.GetNumberOfReturns();
//pt.intensity = p.GetScanDirection();
//---------------------------------------------------Assign Color--------------------------------------------------//
//If the Point template PointT has RGB, you can assign the Color according to the point cloud in las.
//If the Point template PointT is without RGB, you should comment the line.
//liblas::Color lasColor;
//lasColor= p.GetColor();
//pt.r = lasColor.GetRed();
//pt.g = lasColor.GetGreen();
//pt.b = lasColor.GetBlue();
pointCloud->points.push_back(pt);
}
return 1;
}
template <typename PointT>
bool DataIo<PointT>::readPlyFile(const string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud)
{
if (pcl::io::loadPLYFile<PointT>(fileName, *pointCloud) == -1) //* load the file
{
PCL_ERROR("Couldn't read file \n");
return (-1);
}
}
template <typename PointT>
bool DataIo<PointT>::writePlyFile(const string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud)
{
if (pcl::io::savePLYFile<PointT>(fileName, *pointCloud) == -1) //* load the file
{
PCL_ERROR("Couldn't write file \n");
return (-1);
}
}
template <typename PointT>
bool DataIo<PointT>::batchReadFileNamesInFolders(const std::string &folderName, const std::string & extension, std::vector<std::string> &fileNames)
{
if (!exists(folderName))
{
return 0;
}
else
{
directory_iterator end_iter;
for (directory_iterator iter(folderName); iter != end_iter; ++iter)
{
if (is_regular_file(iter->status()))
{
string fileName;
fileName = iter->path().string();
path dir(fileName);
if (!dir.extension().string().empty())
{
if (!fileName.substr(fileName.rfind('.')).compare(extension))
{
fileNames.push_back(fileName);
}
}
}
}
}
return 1;
}
template <typename PointT>
bool DataIo<PointT>::batchReadFileNamesInFoldersAndSubFolders(const std::string &folderName, const std::string & extension, std::vector<std::string> &fileNames)
{
boost::filesystem::path fullpath(folderName);
if (!exists(fullpath))
{
return false;
}
recursive_directory_iterator end_iter;
for (recursive_directory_iterator iter(fullpath); iter != end_iter; iter++)
{
try
{
if (is_directory(*iter))
{
}
else
{
std::string sFileName = iter->path().string();
path dir(sFileName);
if (!dir.extension().string().empty())
{
if (!sFileName.substr(sFileName.rfind('.')).compare(extension))
{
fileNames.push_back(sFileName);
}
}
}
}
catch (const std::exception & ex)
{
std::cerr << ex.what() << std::endl;
continue;
}
}
return true;
}
template <typename PointT>
bool DataIo<PointT>::batchReadFileNamesInSubFolders(const std::string &folderName, const std::string & extension, std::vector<std::vector<std::string>> &fileNames)
{
int subfolder_num = 0;
if (!exists(folderName))
{
return 0;
}
else
{
directory_iterator end_iter;
for (directory_iterator iter(folderName); iter != end_iter; ++iter)
{
if (is_directory(iter->status()))
{
string subfoldername;
subfoldername = iter->path().string();
std::vector<std::string> fileNames_in_subfolder;
batchReadFileNamesInFolders(subfoldername, extension, fileNames_in_subfolder);
fileNames.push_back(fileNames_in_subfolder);
subfolder_num++;
}
}
}
//cout << subfolder_num << " Sub-folders in the folder have been processed" << endl;
return 1;
}
template <typename PointT>
void DataIo<PointT>::batchReadMultiSourceFileNamesInDataFolders(const std::string &ALS_folder, const std::string &TLS_folder, const std::string &MLS_folder, const std::string &BPLS_folder,
std::vector<std::vector<std::string>> &ALS_strip_files, std::vector<std::string> &TLS_files, std::vector<std::string> &MLS_files, std::vector<std::string> &BPLS_files)
{
batchReadFileNamesInSubFolders(ALS_folder, ".las", ALS_strip_files);
batchReadFileNamesInFolders(TLS_folder, ".las", TLS_files);
batchReadFileNamesInFolders(MLS_folder, ".las", MLS_files);
batchReadFileNamesInFolders(BPLS_folder, ".las", BPLS_files);
LOG(INFO) << "All Filenames have been imported ...";
}
template <typename PointT>
bool DataIo<PointT>::readTxtFile(const string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud)
{
ifstream in(fileName, ios::in);
if (!in)
{
return 0;
}
double x_ = 0, y_ = 0, z_ = 0;
int i = 0;
while (!in.eof())
{
in >> x_ >> y_ >> z_;
if (in.fail())
{
break;
}
PointT Pt;
Pt.x = x_;
Pt.y = y_;
Pt.z = z_;
pointCloud->points.push_back(Pt);
++i;
}
in.close();
//cout << "Import finished ... ..." << endl;
return 1;
}
template <typename PointT>
bool DataIo<PointT>::writeTxtFile(const string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud)
{
ofstream ofs;
ofs.open(fileName);
if (ofs.is_open())
{
for (size_t i = 0; i < pointCloud->size(); ++i)
{
ofs << setiosflags(ios::fixed) << setprecision(5) << pointCloud->points[i].x << " "
<< setiosflags(ios::fixed) << setprecision(5) << pointCloud->points[i].y << " "
<< setiosflags(ios::fixed) << setprecision(5) << pointCloud->points[i].z
//<<" "<< setiosflags(ios::fixed) << setprecision(5) << pointCloud->points[i].intensity
<< endl;
}
ofs.close();
}
else{ return 0; }
//cout << "Output finished ... ..." << endl;
return 1;
}
template <typename PointT>
bool DataIo<PointT>::writeTxtFile(const string &fileName, const typename pcl::PointCloud<PointT>::Ptr &pointCloud, int subsample_ratio)
{
ofstream ofs;
ofs.open(fileName);
if (ofs.is_open())
{
for (size_t i = 0; i < pointCloud->size(); ++i)
{
if (i % subsample_ratio == 0)//Subsampling;
{
ofs << setiosflags(ios::fixed) << setprecision(5) << pointCloud->points[i].x << " "
<< setiosflags(ios::fixed) << setprecision(5) << pointCloud->points[i].y << " "
<< setiosflags(ios::fixed) << setprecision(5) << pointCloud->points[i].z
//<<" "<< setiosflags(ios::fixed) << setprecision(5) << pointCloud->points[i].intensity
<< endl;
}
}
ofs.close();
}
else{ return 0; }
//cout << "Output finished ... ..." << endl;
return 1;
}
template <typename PointT>
void DataIo<PointT>::display(const typename pcl::PointCloud<PointT>::Ptr &Cloud1, const typename pcl::PointCloud<PointT>::Ptr &Cloud2, string displayname)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer(displayname));
viewer->setBackgroundColor(255, 255, 255);
char t[256];
string s;
int n = 0;
pcXYZRGBPtr pointcloud1(new pcXYZRGB());
pcXYZRGBPtr pointcloud2(new pcXYZRGB());
for (size_t i = 0; i < Cloud1->points.size(); ++i)
{
pcl::PointXYZRGB pt;
pt.x = Cloud1->points[i].x;
pt.y = Cloud1->points[i].y;
pt.z = Cloud1->points[i].z;
pt.r = 255;
pt.g = 215;
pt.b = 0;
pointcloud1->points.push_back(pt);
} // Golden
viewer->addPointCloud(pointcloud1, "pointcloudT");
for (size_t i = 0; i < Cloud2->points.size(); ++i)
{
pcl::PointXYZRGB pt;
pt.x = Cloud2->points[i].x;
pt.y = Cloud2->points[i].y;
pt.z = Cloud2->points[i].z;
pt.r = 233;
pt.g = 233;
pt.b = 216;
pointcloud2->points.push_back(pt);
} // Silver
viewer->addPointCloud(pointcloud2, "pointcloudS");
cout << "Click X(close) to continue..." << endl;
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
template <typename PointT>
void DataIo<PointT>::displaymulti(const typename pcl::PointCloud<PointT>::Ptr &Cloud0, const typename pcl::PointCloud<PointT>::Ptr &Cloud1, const typename pcl::PointCloud<PointT>::Ptr &Cloud2, string displayname)
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer(displayname));
viewer->setBackgroundColor(255, 255, 255);
char t[256];
string s;
int n = 0;
pcXYZRGBPtr pointcloud0(new pcXYZRGB());
pcXYZRGBPtr pointcloud1(new pcXYZRGB());
pcXYZRGBPtr pointcloud2(new pcXYZRGB());
for (size_t i = 0; i < Cloud0->points.size(); ++i)
{
pcl::PointXYZRGB pt;
pt.x = Cloud0->points[i].x;
pt.y = Cloud0->points[i].y;
pt.z = Cloud0->points[i].z;
pt.r = 255;
pt.g = 0;
pt.b = 0;
pointcloud0->points.push_back(pt);
} // Red
viewer->addPointCloud(pointcloud0, "pointcloud_reference");
for (size_t i = 0; i < Cloud1->points.size(); ++i)
{
pcl::PointXYZRGB pt;
pt.x = Cloud1->points[i].x;
pt.y = Cloud1->points[i].y;
pt.z = Cloud1->points[i].z;
pt.r = 0;
pt.g = 0;
pt.b = 255;
pointcloud1->points.push_back(pt);
} // Blue
viewer->addPointCloud(pointcloud1, "pointcloud_reg_method1");
for (size_t i = 0; i < Cloud2->points.size(); ++i)
{
pcl::PointXYZRGB pt;
pt.x = Cloud2->points[i].x;
pt.y = Cloud2->points[i].y;
pt.z = Cloud2->points[i].z;
pt.r = 0;
pt.g = 255;
pt.b = 0;
pointcloud2->points.push_back(pt);
} // Green
viewer->addPointCloud(pointcloud2, "pointcloud_reg_method2");
cout << "Click X(close) to continue..." << endl;
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
template <typename PointT>
void DataIo<PointT>::ALS_block_by_time(const typename pcl::PointCloud<PointT>::Ptr &pointCloud, typename vector<pcl::PointCloud<PointT>> &CloudBlocks, float time_step_in_second)
{
float time_max = -FLT_MAX;
float time_min = FLT_MAX;
float time_range;
for (size_t i = 0; i < pointCloud->size(); i++)
{
if (pointCloud->points[i].intensity > time_max) time_max = pointCloud->points[i].intensity;
if (pointCloud->points[i].intensity < time_min) time_min = pointCloud->points[i].intensity;
}
time_range = time_max - time_min;
CloudBlocks.resize(int(time_range / time_step_in_second) + 1);
for (size_t i = 0; i < pointCloud->size(); i++)
{
int index = int(1.0 * (pointCloud->points[i].intensity - time_min) / time_step_in_second);
CloudBlocks[index].push_back(pointCloud->points[i]);
}
}
template <typename PointT>
bool DataIo<PointT>::batchWriteBlockInColor(const string &fileName, typename vector<pcl::PointCloud<PointT>> &CloudBlocks, bool automatic_shift_or_not)
{
global_shift.resize(3);
if (!automatic_shift_or_not)
{
string fileGlobalShift;
cout << "Please enter or drag in the Global Shift File" << endl
<< "Example [GlobalShift.txt] :" << endl
<< "-366370.90" << endl
<< "-3451297.82" << endl
<< "-14.29" << endl;
cin >> fileGlobalShift;
ifstream in(fileGlobalShift, ios::in);
in >> global_shift[0];
in >> global_shift[1];
in >> global_shift[2];
in.close();
}
for (int j = 0; j < CloudBlocks.size(); j++)
{
Bounds bound;
getCloudBound(CloudBlocks[j], bound);
liblas::Color lasColor;
lasColor.SetRed(255 * (rand() / (1.0 + RAND_MAX)));
lasColor.SetGreen(255 * (rand() / (1.0 + RAND_MAX)));
lasColor.SetBlue(255 * (rand() / (1.0 + RAND_MAX)));
string BlockFolder, BlockFilename;
ostringstream oss;
oss.setf(ios::right); //设置对齐方式为右对齐
oss.fill('0'); //设置填充方式,不足位补0
oss.width(3); //设置宽度为2,只对下条输出有用
oss << j;
// 此处存疑,按000,001,002这样的顺序命名;
BlockFolder = fileName.substr(0, fileName.rfind("."));
if (!boost::filesystem::exists(BlockFolder))
{
boost::filesystem::create_directory(BlockFolder);
}
BlockFilename = BlockFolder + fileName.substr(fileName.rfind("\\"), fileName.rfind(".") - fileName.rfind("\\"))+ "_" + oss.str() + ".las";
if (j == 0) cout << BlockFilename << endl;
ofstream ofs;
ofs.open(BlockFilename, std::ios::out | std::ios::binary);
if (ofs.is_open())
{
liblas::Header header;
header.SetDataFormatId(liblas::ePointFormat2);
header.SetVersionMajor(1);
header.SetVersionMinor(2);
header.SetMin(bound.min_x - global_shift[0], bound.min_y - global_shift[1], bound.min_z - global_shift[2]);
header.SetMax(bound.max_x - global_shift[0], bound.max_y - global_shift[1], bound.max_z - global_shift[2]);
header.SetOffset((bound.min_x + bound.max_x) / 2.0 - global_shift[0], (bound.min_y + bound.max_y) / 2.0 - global_shift[1], (bound.min_z + bound.max_z) / 2.0 - global_shift[2]);
header.SetScale(0.01, 0.01, 0.01);
header.SetPointRecordsCount(CloudBlocks[j].points.size());
liblas::Writer writer(ofs, header);
liblas::Point pt(&header);
for (size_t i = 0; i < CloudBlocks[j].points.size(); i++)
{
pt.SetCoordinates(double(CloudBlocks[j].points[i].x) - global_shift[0], double(CloudBlocks[j].points[i].y) - global_shift[1], double(CloudBlocks[j].points[i].z) - global_shift[2]);
pt.SetColor(lasColor);
pt.SetIntensity(CloudBlocks[j].points[i].intensity);
writer.WritePoint(pt);
}
ofs.flush();
ofs.close();
}
}
return 1;
}
template <typename PointT>
bool DataIo<PointT>::readindiceslist(std::vector<int> & indicesA, std::vector<int> & indicesB)
{
string indiceslistFile;
cout << "Please enter or drag in the Correspondence Tie Point Indices List File" << endl
<< "Example [IndicesListFile.txt] :" << endl
<< "107562 934051 " << endl
<< "275003 18204" << endl
<< "872055 462058" << endl
<< "... ..." << endl;
cin >> indiceslistFile;
ifstream in(indiceslistFile, ios::in);
if (!in)
{
return 0;
}
int i = 0;
while (!in.eof())
{
int p1, p2;
in >> p1 >> p2;
if (in.fail())
{
break;
}
indicesA.push_back(p1);
indicesB.push_back(p2);
++i;
}
in.close();
}
template <typename PointT>
bool DataIo<PointT>::readindiceslist(const typename pcl::PointCloud<PointT>::Ptr &CloudA, const typename pcl::PointCloud<PointT>::Ptr &CloudB, std::vector <std::vector<double>> & coordinatesA, std::vector <std::vector<double>> & coordinatesB)
{
string indiceslistFile;
cout << "Please enter or drag in the Correspondence Tie Point Indices List File" << endl
<< "Example [IndicesListFile.txt] :" << endl
<< "107562 934051 " << endl
<< "275003 18204" << endl
<< "872055 462058" << endl
<< "... ..." << endl;
cin >> indiceslistFile;
ifstream in(indiceslistFile, ios::in);
if (!in)
{
return 0;
}
vector<int> pointlistA;
vector<int> pointlistB;
int i = 0;
while (!in.eof())
{
int p1, p2;
in >> p1 >> p2;
if (in.fail())
{
break;
}
pointlistA.push_back(p1);
pointlistB.push_back(p2);
++i;
}
in.close();
for (int j = 0; j < pointlistA.size(); j++)
{
std::vector<double> pointA(3);
pointA[0] = CloudA->points[pointlistA[j]].x;