-
Notifications
You must be signed in to change notification settings - Fork 4
/
ccCommandLineParser.cpp
3461 lines (3058 loc) · 101 KB
/
ccCommandLineParser.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 "ccCommandLineParser.h"
//CCLib
#include <CloudSamplingTools.h>
#include <WeibullDistribution.h>
#include <NormalDistribution.h>
#include <StatisticalTestingTools.h>
#include <Neighbourhood.h>
//qCC_db
#include <ccProgressDialog.h>
#include <ccOctree.h>
#include <ccPlane.h>
#include <ccNormalVectors.h>
#include <ccPolyline.h>
#include <ccScalarField.h>
//qCC_io
#include <BundlerFilter.h>
#include <AsciiFilter.h>
#include <FBXFilter.h>
#include <BinFilter.h>
#include <PlyFilter.h>
//qCC
#include "ccCommon.h"
#include <ui_commandLineDlg.h>
#include "ccConsole.h"
#include "mainwindow.h"
#include "ccComparisonDlg.h"
#include "ccRegistrationTools.h"
#include "ccCropTool.h"
//Qt
#include <QMessageBox>
#include <QDialog>
#include <QFile>
#include <QFileInfo>
#include <QDateTime>
#include <QElapsedTimer>
#include <QStringList>
#include <QTextStream>
#include<qdir.h>
//system
#include <set>
static const char COMMAND_SILENT_MODE[] = "SILENT";
static const char COMMAND_OPEN[] = "O"; //+file name
static const char COMMAND_OPEN_SKIP_LINES[] = "SKIP"; //+number of lines to skip
static const char COMMAND_OPEN_SHIFT_ON_LOAD[] = "GLOBAL_SHIFT"; //+global shift
static const char COMMAND_KEYWORD_AUTO[] = "AUTO"; //"AUTO" keyword
static const char COMMAND_SUBSAMPLE[] = "SS"; //+ method (RANDOM/SPATIAL/OCTREE) + parameter (resp. point count / spatial step / octree level)
static const char COMMAND_CURVATURE[] = "CURV"; //+ curvature type (MEAN/GAUSS) +
static const char COMMAND_DENSITY[] = "DENSITY"; //+ sphere radius
static const char COMMAND_DENSITY_TYPE[] = "TYPE"; //+ density type
static const char COMMAND_APPROX_DENSITY[] = "APPROX_DENSITY";
static const char COMMAND_SF_GRADIENT[] = "SF_GRAD";
static const char COMMAND_ROUGHNESS[] = "ROUGH";
static const char COMMAND_BUNDLER[] = "BUNDLER_IMPORT"; //Import Bundler file + orthorectification
static const char COMMAND_BUNDLER_ALT_KEYPOINTS[] = "ALT_KEYPOINTS";
static const char COMMAND_BUNDLER_SCALE_FACTOR[] = "SCALE_FACTOR";
static const char COMMAND_BUNDLER_UNDISTORT[] = "UNDISTORT";
static const char COMMAND_BUNDLER_COLOR_DTM[] = "COLOR_DTM";
static const char COMMAND_C2M_DIST[] = "C2M_DIST";
static const char COMMAND_C2M_DIST_FLIP_NORMALS[] = "FLIP_NORMS";
static const char COMMAND_C2C_DIST[] = "C2C_DIST";
static const char COMMAND_C2C_SPLIT_XYZ[] = "SPLIT_XYZ";
static const char COMMAND_C2C_LOCAL_MODEL[] = "MODEL";
static const char COMMAND_MAX_DISTANCE[] = "MAX_DIST";
static const char COMMAND_OCTREE_LEVEL[] = "OCTREE_LEVEL";
static const char COMMAND_SAMPLE_MESH[] = "SAMPLE_MESH";
static const char COMMAND_MERGE_CLOUDS[] = "MERGE_CLOUDS";
static const char COMMAND_STAT_TEST[] = "STAT_TEST";
static const char COMMAND_FILTER_SF_BY_VALUE[] = "FILTER_SF";
static const char COMMAND_CLEAR_CLOUDS[] = "CLEAR_CLOUDS";
static const char COMMAND_CLEAR_MESHES[] = "CLEAR_MESHES";
static const char COMMAND_CLEAR[] = "CLEAR";
static const char COMMAND_BEST_FIT_PLANE[] = "BEST_FIT_PLANE";
static const char COMMAND_BEST_FIT_PLANE_MAKE_HORIZ[] = "MAKE_HORIZ";
static const char COMMAND_BEST_FIT_PLANE_KEEP_LOADED[] = "KEEP_LOADED";
static const char COMMAND_MATCH_BB_CENTERS[] = "MATCH_CENTERS";
static const char COMMAND_ICP[] = "ICP";
static const char COMMAND_ICP_REFERENCE_IS_FIRST[] = "REFERENCE_IS_FIRST";
static const char COMMAND_ICP_MIN_ERROR_DIIF[] = "MIN_ERROR_DIFF";
static const char COMMAND_ICP_ITERATION_COUNT[] = "ITER";
static const char COMMAND_ICP_OVERLAP[] = "OVERLAP";
static const char COMMAND_ICP_ADJUST_SCALE[] = "ADJUST_SCALE";
static const char COMMAND_ICP_RANDOM_SAMPLING_LIMIT[] = "RANDOM_SAMPLING_LIMIT";
static const char COMMAND_ICP_ENABLE_FARTHEST_REMOVAL[] = "FARTHEST_REMOVAL";
static const char COMMAND_CLOUD_EXPORT_FORMAT[] = "C_EXPORT_FMT";
static const char COMMAND_ASCII_EXPORT_PRECISION[] = "PREC";
static const char COMMAND_ASCII_EXPORT_SEPARATOR[] = "SEP";
static const char COMMAND_PLY_EXPORT_FORMAT[] = "PLY_EXPORT_FMT";
static const char COMMAND_FBX_EXPORT_FORMAT[] = "FBX_EXPORT_FMT";
static const char COMMAND_MESH_EXPORT_FORMAT[] = "M_EXPORT_FMT";
static const char COMMAND_EXPORT_EXTENSION[] = "EXT";
static const char COMMAND_NO_TIMESTAMP[] = "NO_TIMESTAMP";
static const char COMMAND_CROP[] = "CROP";
static const char COMMAND_CROP_2D[] = "CROP2D";
static const char COMMAND_COLOR_BANDING[] = "CBANDING";
static const char COMMAND_CROP_OUTSIDE[] = "OUTSIDE";
static const char COMMAND_SAVE_CLOUDS[] = "SAVE_CLOUDS";
static const char COMMAND_SAVE_MESHES[] = "SAVE_MESHES";
static const char COMMAND_AUTO_SAVE[] = "AUTO_SAVE";
static const char COMMAND_SET_ACTIVE_SF[] = "SET_ACTIVE_SF";
static const char COMMAND_REMOVE_ALL_SFS[] = "REMOVE_ALL_SFS";
static const char COMMAND_COMPUTE_GRIDDED_NORMALS[] = "COMPUTE_NORMALS";
static const char COMMAND_APPLY_TRANSFORMATION[] = "APPLY_TRANS";
static const char COMMAND_DELAUNAY[] = "DELAUNAY";
static const char COMMAND_DELAUNAY_AA[] = "AA";
static const char COMMAND_DELAUNAY_BF[] = "BEST_FIT";
static const char COMMAND_DELAUNAY_MAX_EDGE_LENGTH[] = "MAX_EDGE_LENGTH";
static const char COMMAND_CROSS_SECTION[] = "CROSS_SECTION";
static const char COMMAND_LOG_FILE[] = "LOG_FILE";
static const char OPTION_ALL_AT_ONCE[] = "ALL_AT_ONCE";
static const char OPTION_ON[] = "ON";
static const char OPTION_OFF[] = "OFF";
//Current cloud(s) export format (can be modified with the 'COMMAND_CLOUD_EXPORT_FORMAT' option)
static QString s_CloudExportFormat(BinFilter::GetFileFilter());
//Current cloud(s) export extension (warning: can be anything)
static QString s_CloudExportExt(BinFilter::GetDefaultExtension());
//Current mesh(es) export format (can be modified with the 'COMMAND_MESH_EXPORT_FORMAT' option)
static QString s_MeshExportFormat(BinFilter::GetFileFilter());
//Current mesh(es) export extension (warning: can be anything)
static QString s_MeshExportExt(BinFilter::GetDefaultExtension());
//Default numerical precision for ASCII output
static int s_precision = 12;
//Whether a timestamp should be automatically added to output files or not
static bool s_addTimestamp = true;
//Whether silent mode is activated or not
static bool s_silentMode = false;
//Whether files should be automatically saved (after each process) or not
static bool s_autoSaveMode = true;
//Loading parameters
struct CmdLineLoadParameters : public FileIOFilter::LoadParameters
{
CmdLineLoadParameters()
: FileIOFilter::LoadParameters()
, m_coordinatesShiftEnabled(false)
, m_coordinatesShift(0,0,0)
{
shiftHandlingMode = ccGlobalShiftManager::NO_DIALOG;
alwaysDisplayLoadDialog = false;
coordinatesShiftEnabled = &m_coordinatesShiftEnabled;
coordinatesShift = &m_coordinatesShift;
}
bool m_coordinatesShiftEnabled;
CCVector3d m_coordinatesShift;
};
static CmdLineLoadParameters s_loadParameters;
bool IsCommand(const QString& token, const char* command)
{
return token.startsWith("-") && token.mid(1).toUpper() == QString(command);
}
int ccCommandLineParser::Parse(int nargs, char** args)
{
if (!args || nargs < 2)
{
assert(false);
return EXIT_SUCCESS;
}
//reset default behavior(s)
s_loadParameters.autoComputeNormals = ccGriddedTools::NEVER;
s_MeshExportFormat = s_CloudExportFormat = BinFilter::GetFileFilter();
s_MeshExportExt = s_CloudExportExt = BinFilter::GetDefaultExtension();
s_precision = 12;
s_addTimestamp = true;
s_silentMode = false;
s_autoSaveMode = true;
//load arguments
QStringList arguments;
{
for (int i=1; i<nargs; ++i) //'i=1' because first argument is always program executable file!
arguments.push_back(QString(args[i]));
}
assert(!arguments.empty());
//specific command: silent mode (will prevent the console dialog from appearing!
if (IsCommand(arguments.front(),COMMAND_SILENT_MODE))
{
arguments.pop_front();
s_silentMode = true;
}
QDialog consoleDlg;
if (!s_silentMode)
{
//show console
Ui_commandLineDlg commandLineDlg;
commandLineDlg.setupUi(&consoleDlg);
consoleDlg.show();
ccConsole::Init(commandLineDlg.consoleWidget,&consoleDlg);
}
//parse input
int result = ccCommandLineParser().parse(arguments,&consoleDlg);
if (!s_silentMode)
{
if (result == EXIT_SUCCESS)
QMessageBox::information(&consoleDlg,"Processed finished","Job done");
else
QMessageBox::warning(&consoleDlg,"Processed finished","An error occurred! Check console");
}
ccConsole::ReleaseInstance();
return result;
}
ccCommandLineParser::ccCommandLineParser()
{
}
ccCommandLineParser::~ccCommandLineParser()
{
removeClouds();
removeMeshes();
}
static void Print(const QString& message)
{
ccConsole::Print(message);
if (s_silentMode)
printf("%s\n",qPrintable(message));
}
static void Warning(const QString& message)
{
ccConsole::Warning(message);
if (s_silentMode)
printf("[WARNING] %s\n",qPrintable(message));
}
static bool Error(const QString& message)
{
ccConsole::Error(message);
if (s_silentMode)
printf("[ERROR] %s\n",qPrintable(message));
return false;
}
void ccCommandLineParser::removeClouds()
{
while (!m_clouds.empty())
{
if (m_clouds.back().pc)
delete m_clouds.back().pc;
m_clouds.pop_back();
}
}
void ccCommandLineParser::removeMeshes()
{
while (!m_meshes.empty())
{
MeshDesc& desc = m_meshes.back();
if (desc.mesh)
delete desc.mesh;
m_meshes.pop_back();
}
}
bool ccCommandLineParser::saveClouds(QString suffix/*=QString()*/, bool allAtOnce/*=false*/)
{
//all-at-once: all clouds in a single file
if (allAtOnce)
{
FileIOFilter::Shared filter = FileIOFilter::GetFilter(s_CloudExportFormat,false);
bool multiple = false, exclusive = true;
if (filter)
filter->canSave(CC_TYPES::POINT_CLOUD,multiple,exclusive);
if (multiple)
{
ccHObject tempContainer("Clouds");
{
for (unsigned i=0; i<m_clouds.size(); ++i)
tempContainer.addChild(m_clouds[i].getEntity(),ccHObject::DP_NONE);
}
//save output
GroupDesc desc(&tempContainer,"AllClouds",m_clouds.front().path);
QString errorStr = Export(desc,suffix,0,true);
if (!errorStr.isEmpty())
return Error(errorStr);
else
return true;
}
else
{
ccConsole::Error(QString("The currently selected ouput format for clouds (%1) doesn't handle multiple entities at once!").arg(s_CloudExportFormat));
//will proceed with the standard way
}
}
//standard way: one file per cloud
{
for (unsigned i=0; i<m_clouds.size(); ++i)
{
//save output
QString errorStr = Export(m_clouds[i],suffix);
if (!errorStr.isEmpty())
return Error(errorStr);
}
}
return true;
}
bool ccCommandLineParser::saveMeshes(QString suffix/*=QString()*/, bool allAtOnce/*=false*/)
{
//all-at-once: all meshes in a single file
if (allAtOnce)
{
FileIOFilter::Shared filter = FileIOFilter::GetFilter(s_MeshExportFormat,false);
bool multiple = false, exclusive = true;
if (filter)
filter->canSave(CC_TYPES::MESH,multiple,exclusive);
if (multiple)
{
ccHObject tempContainer("Meshes");
{
for (unsigned i=0; i<m_meshes.size(); ++i)
tempContainer.addChild(m_meshes[i].getEntity(),ccHObject::DP_NONE);
}
//save output
GroupDesc desc(&tempContainer,"AllMeshes",m_meshes.front().path);
QString errorStr = Export(desc,suffix,0,false);
if (!errorStr.isEmpty())
return Error(errorStr);
else
return true;
}
else
{
ccConsole::Error(QString("The currently selected ouput format for meshes (%1) doesn't handle multiple entities at once!").arg(s_MeshExportFormat));
//will proceed with the standard way
}
}
//standard way: one file per mesh
{
for (unsigned i=0; i<m_meshes.size(); ++i)
{
//save output
QString errorStr = Export(m_meshes[i],suffix);
if (!errorStr.isEmpty())
return Error(errorStr);
}
}
return true;
}
ccCommandLineParser::EntityDesc::EntityDesc(QString filename, int _indexInFile/*=-1*/)
: indexInFile(-1)
{
if (filename.isNull())
{
basename = "unknown";
path = QApplication::applicationDirPath();
}
else
{
QFileInfo fi(filename);
basename = fi.baseName();
path = fi.path();
}
}
ccCommandLineParser::EntityDesc::EntityDesc(QString _basename, QString _path, int _indexInFile/*=-1*/)
: basename(_basename)
, path(_path)
, indexInFile(_indexInFile)
{
}
QString ccCommandLineParser::Export(EntityDesc& entDesc, QString suffix/*=QString()*/, QString* _outputFilename/*=0*/, bool forceIsCloud/*=false*/)
{
Print("[SAVING]");
//fetch the real entity
ccHObject* entity = entDesc.getEntity();
if (!entity)
{
assert(false);
return QString("[Export] Internal error: invalid input entity!");
}
//get its name
QString entName = entity->getName();
if (entName.isEmpty())
entName = entDesc.basename;
//sub-item?
if (entDesc.indexInFile >= 0)
{
if (suffix.isEmpty())
suffix = QString("%1").arg(entDesc.indexInFile);
else
suffix.prepend(QString("%1_").arg(entDesc.indexInFile));
}
//specific case: clouds
bool isCloud = entity->isA(CC_TYPES::POINT_CLOUD);
isCloud |= forceIsCloud;
if (!suffix.isEmpty())
entName += QString("_") + suffix;
entity->setName(entName);
QString baseName = entDesc.basename;
if (!suffix.isEmpty())
baseName += QString("_") + suffix;
QString outputFilename = baseName;
if (s_addTimestamp)
outputFilename += QString("_%1").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_hh'h'mm_ss"));
QString extension = isCloud ? s_CloudExportExt : s_MeshExportExt;
if (!extension.isEmpty())
outputFilename += QString(".%1").arg(extension);
if (_outputFilename)
*_outputFilename = outputFilename;
if (!entDesc.path.isEmpty())
outputFilename.prepend(QString("%1/").arg(entDesc.path));
if (entity->isKindOf(CC_TYPES::MESH))
{
ccGenericMesh* mesh = static_cast<ccGenericMesh*>(entity);
ccGenericPointCloud* vertices = mesh->getAssociatedCloud();
//we must save the vertices cloud as well if it's not a child of the mesh!
if (vertices && !mesh->isAncestorOf(vertices))
{
//we save the cloud first!
vertices->addChild(mesh,ccHObject::DP_NONE); //we simply add a fake dependency
entity = vertices;
}
}
//save file
FileIOFilter::SaveParameters parameters;
{
//no dialog by default for command line mode!
parameters.alwaysDisplaySaveDialog = false;
}
if (FileIOFilter::SaveToFile( entity,
qPrintable(outputFilename),
parameters,
isCloud ? s_CloudExportFormat : s_MeshExportFormat) != CC_FERR_NO_ERROR)
{
return QString("Failed to save result in file '%1'").arg(outputFilename);
}
return QString();
}
bool ccCommandLineParser::commandLoad(QStringList& arguments)
{
Print("[LOADING]");
if (arguments.empty())
return Error(QString("Missing parameter: filename after \"-%1\"").arg(COMMAND_OPEN));
//optional parameters
int skipLines = 0;
while (!arguments.empty())
{
QString argument = arguments.front();
if (IsCommand(argument,COMMAND_OPEN_SKIP_LINES))
{
//local option confirmed, we can move on
arguments.pop_front();
if (arguments.empty())
return Error(QString("Missing parameter: number of lines after '%1'").arg(COMMAND_OPEN_SKIP_LINES));
bool ok;
skipLines = arguments.takeFirst().toInt(&ok);
if (!ok)
return Error(QString("Invalid parameter: number of lines after '%1'").arg(COMMAND_OPEN_SKIP_LINES));
Print(QString("Will skip %1 lines").arg(skipLines));
}
else if (IsCommand(argument,COMMAND_OPEN_SHIFT_ON_LOAD))
{
//local option confirmed, we can move on
arguments.pop_front();
if (arguments.empty())
return Error(QString("Missing parameter: global shift vector or %1 after '%2'").arg(COMMAND_KEYWORD_AUTO).arg(COMMAND_OPEN_SHIFT_ON_LOAD));
QString firstParam = arguments.takeFirst();
s_loadParameters.shiftHandlingMode = ccGlobalShiftManager::NO_DIALOG;
s_loadParameters.m_coordinatesShiftEnabled = false;
s_loadParameters.m_coordinatesShift = CCVector3d(0,0,0);
if (firstParam.toUpper() == COMMAND_KEYWORD_AUTO)
{
//let CC handles the global shift automatically
s_loadParameters.shiftHandlingMode = ccGlobalShiftManager::NO_DIALOG_AUTO_SHIFT;
}
else if (arguments.size() < 2)
{
return Error(QString("Missing parameter: global shift vector after '%1' (3 values expected)").arg(COMMAND_OPEN_SHIFT_ON_LOAD));
}
else
{
bool ok = true;
CCVector3d shiftOnLoadVec;
shiftOnLoadVec.x = firstParam.toDouble(&ok);
if (!ok)
return Error(QString("Invalid parameter: X coordinate of the global shift vector after '%1'").arg(COMMAND_OPEN_SKIP_LINES));
shiftOnLoadVec.y = arguments.takeFirst().toDouble(&ok);
if (!ok)
return Error(QString("Invalid parameter: Y coordinate of the global shift vector after '%1'").arg(COMMAND_OPEN_SKIP_LINES));
shiftOnLoadVec.z = arguments.takeFirst().toDouble(&ok);
if (!ok)
return Error(QString("Invalid parameter: Z coordinate of the global shift vector after '%1'").arg(COMMAND_OPEN_SKIP_LINES));
//set the user defined shift vector as default shift information
s_loadParameters.m_coordinatesShiftEnabled = true;
s_loadParameters.m_coordinatesShift = shiftOnLoadVec;
}
}
else
{
break;
}
}
if (skipLines > 0)
{
QSharedPointer<AsciiOpenDlg> openDialog = AsciiFilter::GetOpenDialog();
assert(openDialog);
openDialog->setSkippedLines(skipLines);
}
//open specified file
QString filename(arguments.takeFirst());
Print(QString("Opening file: '%1'").arg(filename));
ccHObject* db = FileIOFilter::LoadFromFile(filename,s_loadParameters,QString());
if (!db)
return false/*Error(QString("Failed to open file '%1'").arg(filename))*/;
std::set<unsigned> verticesIDs;
//first look for meshes inside loaded DB (so that we don't consider mesh vertices as clouds!)
{
ccHObject::Container meshes;
size_t count = 0;
//first look for all REAL meshes (so as to no consider sub-meshes)
if (db->filterChildren(meshes,true,CC_TYPES::MESH,true) != 0)
{
count += meshes.size();
for (size_t i=0; i<meshes.size(); ++i)
{
ccGenericMesh* mesh = ccHObjectCaster::ToGenericMesh(meshes[i]);
if (mesh->getParent())
mesh->getParent()->detachChild(mesh);
ccGenericPointCloud* vertices = mesh->getAssociatedCloud();
if (vertices)
{
verticesIDs.insert(vertices->getUniqueID());
Print(QString("Found one mesh with %1 faces and %2 vertices: '%3'").arg(mesh->size()).arg(mesh->getAssociatedCloud()->size()).arg(mesh->getName()));
m_meshes.push_back(MeshDesc(mesh,filename,count == 1 ? -1 : static_cast<int>(i)));
}
else
{
delete mesh;
mesh = 0;
assert(false);
}
}
}
//then look for the other meshes
meshes.clear();
if (db->filterChildren(meshes,true,CC_TYPES::MESH,false) != 0)
{
size_t countBefore = count;
count += meshes.size();
for (size_t i=0; i<meshes.size(); ++i)
{
ccGenericMesh* mesh = ccHObjectCaster::ToGenericMesh(meshes[i]);
if (mesh->getParent())
mesh->getParent()->detachChild(mesh);
ccGenericPointCloud* vertices = mesh->getAssociatedCloud();
if (vertices)
{
verticesIDs.insert(vertices->getUniqueID());
Print(QString("Found one kind of mesh with %1 faces and %2 vertices: '%3'").arg(mesh->size()).arg(mesh->getAssociatedCloud()->size()).arg(mesh->getName()));
m_meshes.push_back(MeshDesc(mesh,filename,count == 1 ? -1 : static_cast<int>(countBefore+i)));
}
else
{
delete mesh;
mesh = 0;
assert(false);
}
}
}
}
//now look for the remaining clouds inside loaded DB
{
ccHObject::Container clouds;
db->filterChildren(clouds,false,CC_TYPES::POINT_CLOUD);
size_t count = clouds.size();
for (size_t i=0; i<count; ++i)
{
ccPointCloud* pc = static_cast<ccPointCloud*>(clouds[i]);
if (pc->getParent())
pc->getParent()->detachChild(pc);
//if the cloud is a set of vertices, we ignore it!
if (verticesIDs.find(pc->getUniqueID()) != verticesIDs.end())
{
m_orphans.addChild(pc);
continue;
}
Print(QString("Found one cloud with %1 points").arg(pc->size()));
m_clouds.push_back(CloudDesc(pc,filename,count == 1 ? -1 : static_cast<int>(i)));
}
}
delete db;
db = 0;
return true;
}
bool ccCommandLineParser::commandSubsample(QStringList& arguments, ccProgressDialog* pDlg/*=0*/)
{
Print("[SUBSAMPLING]");
if (m_clouds.empty())
return Error(QString("No point cloud to resample (be sure to open one with \"-%1 [cloud filename]\" before \"-%2\")").arg(COMMAND_OPEN).arg(COMMAND_SUBSAMPLE));
if (arguments.empty())
return Error(QString("Missing parameter: resampling method after \"-%1\"").arg(COMMAND_SUBSAMPLE));
QString method = arguments.takeFirst().toUpper();
Print(QString("\tMethod: ")+method);
if (method == "RANDOM")
{
if (arguments.empty())
return Error(QString("Missing parameter: number of points after \"-%1 RANDOM\"").arg(COMMAND_SUBSAMPLE));
bool ok;
unsigned count = arguments.takeFirst().toInt(&ok);
if (!ok)
return Error("Invalid number of points for random resampling!");
Print(QString("\tOutput points: %1").arg(count));
for (unsigned i=0; i<m_clouds.size(); ++i)
{
ccPointCloud* cloud = m_clouds[i].pc;
Print(QString("\tProcessing cloud #%1 (%2)").arg(i+1).arg(!cloud->getName().isEmpty() ? cloud->getName() : "no name"));
CCLib::ReferenceCloud* refCloud = CCLib::CloudSamplingTools::subsampleCloudRandomly(cloud,count,pDlg);
if (!refCloud)
return Error("Subsampling process failed!");
Print(QString("\tResult: %1 points").arg(refCloud->size()));
//save output
ccPointCloud* result = cloud->partialClone(refCloud);
delete refCloud;
refCloud = 0;
if (result)
{
result->setName(m_clouds[i].pc->getName() + QString(".subsampled"));
if (s_autoSaveMode)
{
CloudDesc cloudDesc(result,m_clouds[i].basename,m_clouds[i].path,m_clouds[i].indexInFile);
QString errorStr = Export(cloudDesc,"RANDOM_SUBSAMPLED");
if (!errorStr.isEmpty())
{
delete result;
return Error(errorStr);
}
}
//replace current cloud by this one
delete m_clouds[i].pc;
m_clouds[i].pc = result;
m_clouds[i].basename += QString("_SUBSAMPLED");
//delete result;
//result = 0;
}
else
{
return Error("Not enough memory!");
}
}
}
else if (method == "SPATIAL")
{
if (arguments.empty())
return Error(QString("Missing parameter: spatial step after \"-%1 SPATIAL\"").arg(COMMAND_SUBSAMPLE));
bool ok;
double step = arguments.takeFirst().toDouble(&ok);
if (!ok || step <= 0)
return Error("Invalid step value for spatial resampling!");
Print(QString("\tSpatial step: %1").arg(step));
for (unsigned i=0; i<m_clouds.size(); ++i)
{
ccPointCloud* cloud = m_clouds[i].pc;
Print(QString("\tProcessing cloud #%1 (%2)").arg(i+1).arg(!cloud->getName().isEmpty() ? cloud->getName() : "no name"));
CCLib::CloudSamplingTools::SFModulationParams modParams(false);
CCLib::ReferenceCloud* refCloud = CCLib::CloudSamplingTools::resampleCloudSpatially(cloud,static_cast<PointCoordinateType>(step),modParams,0,pDlg);
if (!refCloud)
return Error("Subsampling process failed!");
Print(QString("\tResult: %1 points").arg(refCloud->size()));
//save output
ccPointCloud* result = cloud->partialClone(refCloud);
delete refCloud;
refCloud = 0;
if (result)
{
result->setName(m_clouds[i].pc->getName() + QString(".subsampled"));
if (s_autoSaveMode)
{
CloudDesc cloudDesc(result,m_clouds[i].basename,m_clouds[i].path,m_clouds[i].indexInFile);
QString errorStr = Export(cloudDesc,"SPATIAL_SUBSAMPLED");
if (!errorStr.isEmpty())
{
delete result;
return Error(errorStr);
}
}
//replace current cloud by this one
delete m_clouds[i].pc;
m_clouds[i].pc = result;
m_clouds[i].basename += QString("_SUBSAMPLED");
//delete result;
//result = 0;
}
else
{
return Error("Not enough memory!");
}
}
}
else if (method == "OCTREE")
{
if (arguments.empty())
return Error(QString("Missing parameter: octree level after \"-%1 OCTREE\"").arg(COMMAND_SUBSAMPLE));
bool ok = false;
int octreeLevel = arguments.takeFirst().toInt(&ok);
if (!ok || octreeLevel < 1 || octreeLevel > CCLib::DgmOctree::MAX_OCTREE_LEVEL)
return Error("Invalid octree level!");
Print(QString("\tOctree level: %1").arg(octreeLevel));
for (unsigned i=0; i<m_clouds.size(); ++i)
{
ccPointCloud* cloud = m_clouds[i].pc;
Print(QString("\tProcessing cloud #%1 (%2)").arg(i+1).arg(!cloud->getName().isEmpty() ? cloud->getName() : "no name"));
CCLib::ReferenceCloud* refCloud = CCLib::CloudSamplingTools::subsampleCloudWithOctreeAtLevel(cloud,static_cast<uchar>(octreeLevel),CCLib::CloudSamplingTools::NEAREST_POINT_TO_CELL_CENTER,pDlg);
if (!refCloud)
return Error("Subsampling process failed!");
Print(QString("\tResult: %1 points").arg(refCloud->size()));
//save output
ccPointCloud* result = cloud->partialClone(refCloud);
delete refCloud;
refCloud = 0;
if (result)
{
result->setName(m_clouds[i].pc->getName() + QString(".subsampled"));
if (s_autoSaveMode)
{
CloudDesc cloudDesc(result,m_clouds[i].basename,m_clouds[i].path,m_clouds[i].indexInFile);
QString errorStr = Export(cloudDesc,QString("OCTREE_LEVEL_%1_SUBSAMPLED").arg(octreeLevel));
if (!errorStr.isEmpty())
{
delete result;
return Error(errorStr);
}
}
//replace current cloud by this one
delete m_clouds[i].pc;
m_clouds[i].pc = result;
m_clouds[i].basename += QString("_SUBSAMPLED");
//delete result;
//result = 0;
}
else
{
return Error("Not enough memory!");
}
}
}
else
{
return Error("Unknown method!");
}
return true;
}
bool ccCommandLineParser::commandCurvature(QStringList& arguments, QDialog* parent/*=0*/)
{
Print("[CURVATURE]");
if (arguments.empty())
return Error(QString("Missing parameter: curvature type after \"-%1\"").arg(COMMAND_CURVATURE));
QString curvTypeStr = arguments.takeFirst().toUpper();
CCLib::Neighbourhood::CC_CURVATURE_TYPE curvType = CCLib::Neighbourhood::MEAN_CURV;
if (curvTypeStr == "MEAN")
{
//curvType = CCLib::Neighbourhood::MEAN_CURV;
}
else if (curvTypeStr == "GAUSS")
{
curvType = CCLib::Neighbourhood::GAUSSIAN_CURV;
}
else
{
return Error(QString("Invalid curvature type after \"-%1\". Got '%2' instead of MEAN or GAUSS.").arg(COMMAND_CURVATURE).arg(curvTypeStr));
}
if (arguments.empty())
return Error("Missing parameter: kernel size after curvature type");
bool paramOk = false;
QString kernelStr = arguments.takeFirst();
PointCoordinateType kernelSize = static_cast<PointCoordinateType>(kernelStr.toDouble(¶mOk));
if (!paramOk)
return Error(QString("Failed to read a numerical parameter: kernel size (after curvature type). Got '%1' instead.").arg(kernelStr));
Print(QString("\tKernel size: %1").arg(kernelSize));
if (m_clouds.empty())
return Error(QString("No point cloud on which to compute curvature! (be sure to open one with \"-%1 [cloud filename]\" before \"-%2\")").arg(COMMAND_OPEN).arg(COMMAND_CURVATURE));
//Call MainWindow generic method
void* additionalParameters[2] = {&curvType, &kernelSize};
ccHObject::Container entities;
entities.resize(m_clouds.size());
for (unsigned i=0; i<m_clouds.size(); ++i)
entities[i] = m_clouds[i].pc;
if (MainWindow::ApplyCCLibAlgorithm(MainWindow::CCLIB_ALGO_CURVATURE,entities,parent,additionalParameters))
{
//save output
if (s_autoSaveMode && !saveClouds(QString("%1_CURVATURE_KERNEL_%2").arg(curvTypeStr).arg(kernelSize)))
return false;
}
return true;
}
bool ReadDensityType(QStringList& arguments, CCLib::GeometricalAnalysisTools::Density& density)
{
if (arguments.empty())
return Error(QString("Missing parameter: density type after \"-%1\" (KNN/SURFACE/VOLUME)").arg(COMMAND_DENSITY_TYPE));
//read option confirmed, we can move on
QString typeArg = arguments.takeFirst().toUpper();
if (typeArg == "KNN")
{
density = CCLib::GeometricalAnalysisTools::DENSITY_KNN;
}
else if (typeArg == "SURFACE")
{
density = CCLib::GeometricalAnalysisTools::DENSITY_2D;
}
else if (typeArg == "VOLUME")
{
density = CCLib::GeometricalAnalysisTools::DENSITY_3D;
}
else
{
return Error(QString("Invalid parameter: density type is expected after \"-%1\" (KNN/SURFACE/VOLUME)").arg(COMMAND_DENSITY_TYPE));
}
return true;
}
bool ccCommandLineParser::commandApproxDensity(QStringList& arguments, QDialog* parent/*=0*/)
{
Print("[APPROX DENSITY]");
if (m_clouds.empty())
return Error(QString("No point cloud on which to compute approx. density! (be sure to open one with \"-%1 [cloud filename]\" before \"-%2\")").arg(COMMAND_OPEN).arg(COMMAND_APPROX_DENSITY));
//Call MainWindow generic method
ccHObject::Container entities;
entities.resize(m_clouds.size());
for (size_t i=0; i<m_clouds.size(); ++i)
entities[i] = m_clouds[i].pc;
//optional parameter: density type
CCLib::GeometricalAnalysisTools::Density densityType = CCLib::GeometricalAnalysisTools::DENSITY_3D;
if (!arguments.empty())
{
QString argument = arguments.front();
if (IsCommand(argument,COMMAND_DENSITY_TYPE))
{
//local option confirmed, we can move on
arguments.pop_front();
if (arguments.empty())
return Error(QString("Missing parameter: density type after \"-%1\" (KNN/SURFACE/VOLUME)").arg(COMMAND_DENSITY_TYPE));
//read option confirmed, we can move on
if (!ReadDensityType(arguments,densityType))
return false;
}
}
void* additionalParameters[] = { &densityType };
if (MainWindow::ApplyCCLibAlgorithm(MainWindow::CCLIB_ALGO_APPROX_DENSITY,entities,parent,additionalParameters))
{
//save output
if (s_autoSaveMode && !saveClouds("APPROX_DENSITY"))
return false;
}
return true;
}
bool ccCommandLineParser::commandDensity(QStringList& arguments, QDialog* parent/*=0*/)
{
Print("[DENSITY]");
if (arguments.empty())
return Error(QString("Missing parameter: sphere radius after \"-%1\"").arg(COMMAND_DENSITY));
bool paramOk = false;
QString kernelStr = arguments.takeFirst();
PointCoordinateType kernelSize = static_cast<PointCoordinateType>(kernelStr.toDouble(¶mOk));
if (!paramOk)
return Error(QString("Failed to read a numerical parameter: sphere radius (after \"-%1\"). Got '%2' instead.").arg(COMMAND_DENSITY).arg(kernelStr));
Print(QString("\tSphere radius: %1").arg(kernelSize));
//optional parameter: density type
CCLib::GeometricalAnalysisTools::Density densityType = CCLib::GeometricalAnalysisTools::DENSITY_3D;
if (!arguments.empty())
{
QString argument = arguments.front();
if (IsCommand(argument,COMMAND_DENSITY_TYPE))
{
//local option confirmed, we can move on
arguments.pop_front();
if (arguments.empty())
return Error(QString("Missing parameter: density type after \"-%1\" (KNN/SURFACE/VOLUME)").arg(COMMAND_DENSITY_TYPE));
//read option confirmed, we can move on
if (!ReadDensityType(arguments,densityType))
return false;
}
}
if (m_clouds.empty())
return Error(QString("No point cloud on which to compute density! (be sure to open one with \"-%1 [cloud filename]\" before \"-%2\")").arg(COMMAND_OPEN).arg(COMMAND_DENSITY));
//Call MainWindow generic method
void* additionalParameters[] = { &kernelSize, &densityType };
ccHObject::Container entities;
entities.resize(m_clouds.size());
for (unsigned i=0; i<m_clouds.size(); ++i)
entities[i] = m_clouds[i].pc;
if (MainWindow::ApplyCCLibAlgorithm(MainWindow::CCLIB_ALGO_ACCURATE_DENSITY,entities,parent,additionalParameters))
{
//save output
if (s_autoSaveMode && !saveClouds("DENSITY"))
return false;
}
return true;
}
bool ccCommandLineParser::commandSFGradient(QStringList& arguments, QDialog* parent/*=0*/)
{
Print("[SF GRADIENT]");
if (arguments.empty())
return Error(QString("Missing parameter: boolean (whether SF is euclidean or not) after \"-%1\"").arg(COMMAND_SF_GRADIENT));
QString euclideanStr = arguments.takeFirst().toUpper();
bool euclidean = false;
if (euclideanStr == "TRUE")