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
|
#include "ccCommandLineParser.h"
//Local
#include "ccCommandLineCommands.h"
#include "ccCommandCrossSection.h"
#include "ccCommandRaster.h"
#include "ccPluginInterface.h"
//qCC_db
#include <ccProgressDialog.h>
//qCC_io
#include <AsciiFilter.h>
#include <BinFilter.h>
//qCC
#include "ccConsole.h"
//Qt
#include <QMessageBox>
#include <QElapsedTimer>
//system
#include <unordered_set>
//commands
static const char COMMAND_HELP[] = "HELP";
static const char COMMAND_SILENT_MODE[] = "SILENT";
/*****************************************************/
/*************** ccCommandLineParser *****************/
/*****************************************************/
void ccCommandLineParser::print(const QString& message) const
{
ccConsole::Print(message);
if (m_silentMode)
{
printf("%s\n", qPrintable(message));
}
}
void ccCommandLineParser::warning(const QString& message) const
{
ccConsole::Warning(message);
if (m_silentMode)
{
printf("[WARNING] %s\n", qPrintable(message));
}
}
bool ccCommandLineParser::error(const QString& message) const
{
ccConsole::Error(message);
if (m_silentMode)
{
printf("[ERROR] %s\n", qPrintable(message));
}
return false;
}
int ccCommandLineParser::Parse(int nargs, char** args, ccPluginInterfaceList& plugins)
{
if (!args || nargs < 2)
{
assert(false);
return EXIT_SUCCESS;
}
//load arguments
QScopedPointer<ccCommandLineParser> parser(new ccCommandLineParser);
{
for (int i = 1; i < nargs; ++i) //'i=1' because first argument is always program executable file!
{
parser->arguments().push_back(QString(args[i]));
}
}
assert(!parser->arguments().empty());
//specific command: silent mode (will prevent the console dialog from appearing!
if (ccCommandLineInterface::IsCommand(parser->arguments().front(), COMMAND_SILENT_MODE))
{
parser->arguments().pop_front();
parser->toggleSilentMode(true);
}
QScopedPointer<QDialog> consoleDlg(0);
if (!parser->silentMode())
{
//show console
consoleDlg.reset(new QDialog);
Ui_commandLineDlg commandLineDlg;
commandLineDlg.setupUi(consoleDlg.data());
consoleDlg->show();
ccConsole::Init(commandLineDlg.consoleWidget, consoleDlg.data());
parser->fileLoadingParams().parentWidget = consoleDlg.data();
}
//load the plugins commands
for ( ccPluginInterface *plugin : plugins )
{
if (!plugin)
{
assert(false);
continue;
}
plugin->registerCommands(parser.data());
}
//parse input
int result = parser->start(consoleDlg.data());
if (!parser->silentMode())
{
if (result == EXIT_SUCCESS)
QMessageBox::information(consoleDlg.data(), "Processed finished", "Job done");
else
QMessageBox::warning(consoleDlg.data(), "Processed finished", "An error occurred! Check console");
}
//release the parser before the console (as its dialogs may be chidren of the console)
parser.reset();
ccConsole::ReleaseInstance();
return result;
}
ccCommandLineParser::ccCommandLineParser()
: ccCommandLineInterface()
, m_cloudExportFormat(BinFilter::GetFileFilter())
, m_cloudExportExt(BinFilter::GetDefaultExtension())
, m_meshExportFormat(BinFilter::GetFileFilter())
, m_meshExportExt(BinFilter::GetDefaultExtension())
, m_orphans("orphans")
, m_progressDialog(0)
, m_parentWidget(0)
{
registerCommand(Command::Shared(new CommandLoad));
registerCommand(Command::Shared(new CommandSubsample));
registerCommand(Command::Shared(new CommandExtractCCs));
registerCommand(Command::Shared(new CommandCurvature));
registerCommand(Command::Shared(new CommandApproxDensity));
registerCommand(Command::Shared(new CommandDensity));
registerCommand(Command::Shared(new CommandSFGradient));
registerCommand(Command::Shared(new CommandRoughness));
registerCommand(Command::Shared(new CommandApplyTransformation));
registerCommand(Command::Shared(new CommandDropGlobalShift));
registerCommand(Command::Shared(new CommandFilterBySFValue));
registerCommand(Command::Shared(new CommandMergeClouds));
registerCommand(Command::Shared(new CommandMergeMeshes));
registerCommand(Command::Shared(new CommandSetActiveSF));
registerCommand(Command::Shared(new CommandRemoveAllSF));
registerCommand(Command::Shared(new CommandRemoveScanGrids));
registerCommand(Command::Shared(new CommandMatchBBCenters));
registerCommand(Command::Shared(new CommandMatchBestFitPlane));
registerCommand(Command::Shared(new CommandOrientNormalsMST));
registerCommand(Command::Shared(new CommandSORFilter));
registerCommand(Command::Shared(new CommandSampleMesh));
registerCommand(Command::Shared(new CommandCrossSection));
registerCommand(Command::Shared(new CommandCrop));
registerCommand(Command::Shared(new CommandCrop2D));
registerCommand(Command::Shared(new CommandCoordToSF));
registerCommand(Command::Shared(new CommandColorBanding));
registerCommand(Command::Shared(new CommandC2MDist));
registerCommand(Command::Shared(new CommandC2CDist));
registerCommand(Command::Shared(new CommandStatTest));
registerCommand(Command::Shared(new CommandDelaunayTri));
registerCommand(Command::Shared(new CommandSFArithmetic));
registerCommand(Command::Shared(new CommandSFOperation));
registerCommand(Command::Shared(new CommandICP));
registerCommand(Command::Shared(new CommandChangeCloudOutputFormat));
registerCommand(Command::Shared(new CommandChangeMeshOutputFormat));
registerCommand(Command::Shared(new CommandChangeFBXOutputFormat));
registerCommand(Command::Shared(new CommandChangePLYExportFormat));
registerCommand(Command::Shared(new CommandForceNormalsComputation));
registerCommand(Command::Shared(new CommandSaveClouds));
registerCommand(Command::Shared(new CommandSaveMeshes));
registerCommand(Command::Shared(new CommandAutoSave));
registerCommand(Command::Shared(new CommandLogFile));
registerCommand(Command::Shared(new CommandClear));
registerCommand(Command::Shared(new CommandClearClouds));
registerCommand(Command::Shared(new CommandPopClouds));
registerCommand(Command::Shared(new CommandClearMeshes));
registerCommand(Command::Shared(new CommandPopMeshes));
registerCommand(Command::Shared(new CommandSetNoTimestamp));
registerCommand(Command::Shared(new CommandVolume25D));
registerCommand(Command::Shared(new CommandRasterize));
registerCommand(Command::Shared(new CommandOctreeNormal));
registerCommand(Command::Shared(new CommandClearNormals));
registerCommand(Command::Shared(new CommandComputeMeshVolume));
registerCommand(Command::Shared(new CommandSFColorScale));
registerCommand(Command::Shared(new CommandSFConvertToRGB));
}
ccCommandLineParser::~ccCommandLineParser()
{
removeClouds();
removeMeshes();
if (m_progressDialog)
{
m_progressDialog->close();
m_progressDialog->deleteLater();
}
}
bool ccCommandLineParser::registerCommand(Command::Shared command)
{
if (!command)
{
assert(false);
return false;
}
if (m_commands.contains(command->m_keyword))
{
assert(false);
warning(QString("Internal error: keyword '%' already registered (by command '%2')").arg(command->m_keyword, m_commands[command->m_keyword]->m_name));
return false;
}
m_commands.insert(command->m_keyword, command);
return true;
}
QString ccCommandLineParser::getExportFilename( const CLEntityDesc& entityDesc,
QString extension/*=QString()*/,
QString suffix/*=QString()*/,
QString* baseOutputFilename/*=0*/,
bool forceNoTimestamp/*=false*/) const
{
//fetch the real entity
const ccHObject* entity = entityDesc.getEntity();
if (!entity)
{
assert(false);
warning("[ExportEntity] Internal error: invalid input entity!");
return QString();
}
//sub-item?
if (entityDesc.indexInFile >= 0)
{
if (suffix.isEmpty())
suffix = QString("%1").arg(entityDesc.indexInFile);
else
suffix.prepend(QString("%1_").arg(entityDesc.indexInFile));
}
QString baseName = entityDesc.basename;
if (!suffix.isEmpty())
{
baseName += QString("_") + suffix;
}
QString outputFilename = baseName;
if (m_addTimestamp && !forceNoTimestamp)
{
outputFilename += QString("_%1").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_hh'h'mm_ss_zzz"));
}
if (!extension.isEmpty())
{
outputFilename += QString(".%1").arg(extension);
}
if (baseOutputFilename)
{
*baseOutputFilename = outputFilename;
}
if (!entityDesc.path.isEmpty())
{
outputFilename.prepend(QString("%1/").arg(entityDesc.path));
}
return outputFilename;
}
QString ccCommandLineParser::exportEntity( CLEntityDesc& entityDesc,
QString suffix/*=QString()*/,
QString* baseOutputFilename/*=0*/,
bool forceIsCloud/*=false*/,
bool forceNoTimestamp/*=false*/)
{
print("[SAVING]");
//fetch the real entity
ccHObject* entity = entityDesc.getEntity();
if (!entity)
{
assert(false);
return "[ExportEntity] Internal error: invalid input entity!";
}
//specific case: clouds
bool isCloud = entity->isA(CC_TYPES::POINT_CLOUD);
isCloud |= forceIsCloud;
QString extension = isCloud ? m_cloudExportExt : m_meshExportExt;
QString outputFilename = getExportFilename(entityDesc, extension, suffix, baseOutputFilename, forceNoTimestamp);
if (outputFilename.isEmpty())
{
return QString();
}
//update the entity name as well
{
QString entName = entity->getName();
if (entName.isEmpty())
{
entName = entityDesc.basename;
}
if (!suffix.isEmpty())
{
entName += QString("_") + suffix;
}
entity->setName(entName);
}
bool tempDependencyCreated = false;
ccGenericMesh* mesh = 0;
if (entity->isKindOf(CC_TYPES::MESH) && m_meshExportFormat == BinFilter::GetFileFilter())
{
//in a BIN file we must save the vertices cloud as well if it's not a child of the mesh!
mesh = static_cast<ccGenericMesh*>(entity);
ccGenericPointCloud* vertices = mesh->getAssociatedCloud();
if (vertices && !mesh->isAncestorOf(vertices))
{
//we save the cloud first!
vertices->addChild(mesh, ccHObject::DP_NONE); //we simply add a fake dependency
entity = vertices;
tempDependencyCreated = true;
}
}
//save file
FileIOFilter::SaveParameters parameters;
{
//no dialog by default for command line mode!
parameters.alwaysDisplaySaveDialog = false;
if (!silentMode() && ccConsole::TheInstance())
{
parameters.parentWidget = ccConsole::TheInstance()->parentWidget();
}
}
#ifdef _DEBUG
print("Output filename: " + outputFilename);
#endif
CC_FILE_ERROR result = FileIOFilter::SaveToFile(entity,
outputFilename,
parameters,
isCloud ? m_cloudExportFormat : m_meshExportFormat);
//restore input state!
if (tempDependencyCreated)
{
if (mesh && entity)
{
entity->detachChild(mesh);
}
else
{
assert(false);
}
}
return (result != CC_FERR_NO_ERROR ? QString("Failed to save result in file '%1'").arg(outputFilename) : QString());
}
void ccCommandLineParser::removeClouds(bool onlyLast/*=false*/)
{
while (!m_clouds.empty())
{
if (m_clouds.back().pc)
delete m_clouds.back().pc;
m_clouds.pop_back();
if (onlyLast)
break;
}
}
void ccCommandLineParser::removeMeshes(bool onlyLast/*=false*/)
{
while (!m_meshes.empty())
{
CLMeshDesc& desc = m_meshes.back();
if (desc.mesh)
delete desc.mesh;
m_meshes.pop_back();
if (onlyLast)
break;
}
}
bool ccCommandLineParser::importFile(QString filename, FileIOFilter::Shared filter)
{
print(QString("Opening file: '%1'").arg(filename));
CC_FILE_ERROR result = CC_FERR_NO_ERROR;
ccHObject* db = 0;
if (filter)
{
db = FileIOFilter::LoadFromFile(filename, m_loadingParameters, filter, result);
}
else
{
db = FileIOFilter::LoadFromFile(filename, m_loadingParameters, result, QString());
}
if (!db)
{
return false/*cmd.error(QString("Failed to open file '%1'").arg(filename))*/; //Error message already issued
}
std::unordered_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.emplace_back(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.emplace_back(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.emplace_back(pc, filename, count == 1 ? -1 : static_cast<int>(i));
}
}
delete db;
db = 0;
return true;
}
bool ccCommandLineParser::saveClouds(QString suffix/*=QString()*/, bool allAtOnce/*=false*/, const QString* allAtOnceFileName/*=0*/)
{
//all-at-once: all clouds in a single file
if (allAtOnce)
{
FileIOFilter::Shared filter = FileIOFilter::GetFilter(m_cloudExportFormat, false);
bool multiple = false;
if (filter)
{
bool exclusive = true;
filter->canSave(CC_TYPES::POINT_CLOUD, multiple, exclusive);
}
if (multiple)
{
ccHObject tempContainer("Clouds");
{
for (CLCloudDesc& desc : m_clouds)
{
tempContainer.addChild(desc.getEntity(), ccHObject::DP_NONE);
}
}
//save output
CLGroupDesc desc(&tempContainer, "AllClouds", m_clouds.front().path);
if (allAtOnceFileName)
{
CommandSave::SetFileDesc(desc, *allAtOnceFileName);
}
QString errorStr = exportEntity(desc, suffix, 0, true);
if (!errorStr.isEmpty())
return error(errorStr);
else
return true;
}
else
{
error(QString("The currently selected ouput format for clouds (%1) doesn't handle multiple entities at once!").arg(m_cloudExportFormat));
//will proceed with the standard way
}
}
//standard way: one file per cloud
{
for (CLCloudDesc& desc : m_clouds)
{
//save output
QString errorStr = exportEntity(desc, suffix);
if (!errorStr.isEmpty())
return error(errorStr);
}
}
return true;
}
bool ccCommandLineParser::saveMeshes(QString suffix/*=QString()*/, bool allAtOnce/*=false*/, const QString* allAtOnceFileName/*=0*/)
{
//all-at-once: all meshes in a single file
if (allAtOnce)
{
FileIOFilter::Shared filter = FileIOFilter::GetFilter(m_meshExportFormat, false);
bool multiple = false;
if (filter)
{
bool exclusive = true;
filter->canSave(CC_TYPES::MESH, multiple, exclusive);
}
if (multiple)
{
ccHObject tempContainer("Meshes");
{
for (size_t i = 0; i < m_meshes.size(); ++i)
tempContainer.addChild(m_meshes[i].getEntity(), ccHObject::DP_NONE);
}
//save output
CLGroupDesc desc(&tempContainer, "AllMeshes", m_meshes.front().path);
if (allAtOnceFileName)
{
CommandSave::SetFileDesc(desc, *allAtOnceFileName);
}
QString errorStr = exportEntity(desc, suffix, 0, false);
if (!errorStr.isEmpty())
return error(errorStr);
else
return true;
}
else
{
error(QString("The currently selected ouput format for meshes (%1) doesn't handle multiple entities at once!").arg(m_meshExportFormat));
//will proceed with the standard way
}
}
//standard way: one file per mesh
{
for (size_t i = 0; i < m_meshes.size(); ++i)
{
//save output
QString errorStr = exportEntity(m_meshes[i], suffix);
if (!errorStr.isEmpty())
return error(errorStr);
}
}
return true;
}
int ccCommandLineParser::start(QDialog* parent/*=0*/)
{
if (m_arguments.empty())
{
assert(false);
return EXIT_FAILURE;
}
m_parentWidget = parent;
//if (!m_silentMode)
//{
// m_progressDialog = new ccProgressDialog(false, parent);
// //m_progressDialog->setAttribute(Qt::WA_DeleteOnClose);
// m_progressDialog->setAutoClose(false);
// m_progressDialog->hide();
//}
QElapsedTimer eTimer;
eTimer.start();
bool success = true;
while (success && !m_arguments.empty())
{
QString argument = m_arguments.takeFirst();
if (!argument.startsWith("-"))
{
error(QString("Command expected (commands start with '-'). Found '%1'").arg(argument));
success = false;
break;
}
QString keyword = argument.mid(1).toUpper();
if (m_commands.contains(keyword))
{
assert(m_commands[keyword]);
success = m_commands[keyword]->process(*this);
}
//silent mode (i.e. no console)
else if (keyword == COMMAND_SILENT_MODE)
{
warning(QString("Misplaced command: '%1' (must be first)").arg(COMMAND_SILENT_MODE));
}
else if (keyword == COMMAND_HELP)
{
print("Available commands:");
for (auto it = m_commands.constBegin(); it != m_commands.constEnd(); ++it)
{
print(QString("-%1: %2").arg(it.key().toUpper(), it.value()->m_name));
}
}
else
{
error(QString("Unknown or misplaced command: '%1'").arg(argument));
success = false;
break;
}
}
print(QString("Processed finished in %1 s.").arg(eTimer.elapsed() / 1.0e3, 0, 'f', 2));
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
|