ccCommandLineInterface.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #pragma once
  2. //##########################################################################
  3. //# #
  4. //# CLOUDCOMPARE #
  5. //# #
  6. //# This program is free software; you can redistribute it and/or modify #
  7. //# it under the terms of the GNU General Public License as published by #
  8. //# the Free Software Foundation; version 2 of the License. #
  9. //# #
  10. //# This program is distributed in the hope that it will be useful, #
  11. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  12. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  13. //# GNU General Public License for more details. #
  14. //# #
  15. //# COPYRIGHT: CloudCompare project #
  16. //# #
  17. //##########################################################################
  18. #include "CCPluginAPI.h"
  19. //qCC_db
  20. #include <ccPointCloud.h>
  21. //qCC_io
  22. #include <FileIOFilter.h>
  23. //Qt
  24. #include <QSharedPointer>
  25. #include <QString>
  26. //System
  27. #include <vector>
  28. class ccGenericMesh;
  29. class ccProgressDialog;
  30. class QDialog;
  31. class QStringList;
  32. enum class CL_ENTITY_TYPE {
  33. GROUP,
  34. CLOUD,
  35. MESH
  36. };
  37. //! Loaded entity description
  38. struct CCPLUGIN_LIB_API CLEntityDesc
  39. {
  40. QString basename;
  41. QString path;
  42. int indexInFile;
  43. CLEntityDesc( const QString &name );
  44. CLEntityDesc( const QString &filename, int _indexInFile );
  45. CLEntityDesc( const QString &_basename, const QString &_path, int _indexInFile = -1 );
  46. virtual ~CLEntityDesc() = default;
  47. virtual ccHObject* getEntity() = 0;
  48. virtual const ccHObject* getEntity() const = 0;
  49. virtual CL_ENTITY_TYPE getCLEntityType() const = 0;
  50. };
  51. //! Loaded group description
  52. struct CCPLUGIN_LIB_API CLGroupDesc : CLEntityDesc
  53. {
  54. ccHObject* groupEntity;
  55. CLGroupDesc( ccHObject* group,
  56. const QString& basename,
  57. const QString& path = QString() );
  58. ~CLGroupDesc() override = default;
  59. ccHObject* getEntity() override;
  60. const ccHObject* getEntity() const override;
  61. CL_ENTITY_TYPE getCLEntityType() const override;
  62. };
  63. //! Loaded cloud description
  64. struct CCPLUGIN_LIB_API CLCloudDesc : CLEntityDesc
  65. {
  66. ccPointCloud* pc;
  67. CLCloudDesc();
  68. CLCloudDesc( ccPointCloud* cloud,
  69. const QString& filename = QString(),
  70. int index = -1 );
  71. CLCloudDesc( ccPointCloud* cloud,
  72. const QString& basename,
  73. const QString& path,
  74. int index = -1 );
  75. ~CLCloudDesc() override = default;
  76. ccHObject* getEntity() override;
  77. const ccHObject* getEntity() const override;
  78. CL_ENTITY_TYPE getCLEntityType() const override;
  79. };
  80. //! Loaded mesh description
  81. struct CCPLUGIN_LIB_API CLMeshDesc : CLEntityDesc
  82. {
  83. ccGenericMesh* mesh;
  84. CLMeshDesc();
  85. CLMeshDesc( ccGenericMesh* _mesh,
  86. const QString& filename = QString(),
  87. int index = -1 );
  88. CLMeshDesc( ccGenericMesh* _mesh,
  89. const QString& basename,
  90. const QString& path,
  91. int index = -1 );
  92. ~CLMeshDesc() override = default;
  93. ccHObject* getEntity() override;
  94. const ccHObject* getEntity() const override;
  95. CL_ENTITY_TYPE getCLEntityType() const override;
  96. };
  97. //! Command line interface
  98. class CCPLUGIN_LIB_API ccCommandLineInterface
  99. {
  100. public: //constructor
  101. //! Default constructor
  102. ccCommandLineInterface();
  103. //! Destructor
  104. virtual ~ccCommandLineInterface() = default;
  105. //! Select Entities options
  106. struct SelectEntitiesOptions
  107. {
  108. bool reverse = false;
  109. bool selectRegex = false;
  110. bool selectFirst = false;
  111. bool selectLast = false;
  112. bool selectAll = false;
  113. unsigned firstNr = 0;
  114. unsigned lastNr = 0;
  115. QRegExp regex;
  116. };
  117. //! Export options
  118. enum class ExportOption
  119. {
  120. NoOptions = 0x0,
  121. ForceCloud = 0x1,
  122. ForceMesh = 0x2,
  123. ForceHierarchy = 0x4,
  124. ForceNoTimestamp = 0x8
  125. };
  126. Q_DECLARE_FLAGS(ExportOptions, ExportOption)
  127. public: //commands
  128. //! Generic command interface
  129. struct CCPLUGIN_LIB_API Command
  130. {
  131. //! Shared type
  132. using Shared = QSharedPointer<Command>;
  133. //! Default constructor
  134. Command(const QString& name, const QString& keyword);
  135. virtual ~Command() = default;
  136. //! Main process
  137. virtual bool process(ccCommandLineInterface& cmd) = 0;
  138. //! Command name
  139. QString m_name;
  140. //! Command keyword
  141. QString m_keyword;
  142. };
  143. //! Test whether a command line token is a valid command keyword or not
  144. static bool IsCommand(const QString& token, const char* command);
  145. public: //virtual methods
  146. //! Registers a new command
  147. /** \return success
  148. **/
  149. virtual bool registerCommand(Command::Shared command) = 0;
  150. //! Returns the name of a to-be-exported entity
  151. virtual QString getExportFilename( const CLEntityDesc& entityDesc,
  152. QString extension = QString(),
  153. QString suffix = QString(),
  154. QString* baseOutputFilename = nullptr,
  155. bool forceNoTimestamp = false) const = 0;
  156. //! Exports a cloud or a mesh
  157. /** \return error string (if any)
  158. **/
  159. virtual QString exportEntity( CLEntityDesc& entityDesc,
  160. const QString &suffix = QString(),
  161. QString* outputFilename = nullptr,
  162. ccCommandLineInterface::ExportOptions options = ExportOption::NoOptions) = 0;
  163. //! Saves all clouds
  164. /** \param suffix optional suffix
  165. \param allAtOnce whether to save all clouds in the same file or one cloud per file
  166. \return success
  167. **/
  168. virtual bool saveClouds(QString suffix = QString(), bool allAtOnce = false, const QString* allAtOnceFileName = nullptr) = 0;
  169. //! Saves all meshes
  170. /** \param suffix optional suffix
  171. \param allAtOnce whether to save all meshes in the same file or one mesh per file
  172. \return success
  173. **/
  174. virtual bool saveMeshes(QString suffix = QString(), bool allAtOnce = false, const QString* allAtOnceFileName = nullptr) = 0;
  175. //! Removes all clouds (or only the last one ;)
  176. virtual void removeClouds(bool onlyLast = false) = 0;
  177. //! Removes all meshes (or only the last one ;)
  178. virtual void removeMeshes(bool onlyLast = false) = 0;
  179. //! Keep only the selected clouds in the active set (m_clouds) and stores the others in an separate set (m_unselectedClouds)
  180. virtual bool selectClouds(const SelectEntitiesOptions& options) = 0;
  181. //! Keep only the selected meshes in the active set (m_meshes) and stores the others in an separate set (m_unselectedMeshes)
  182. virtual bool selectMeshes(const SelectEntitiesOptions& options) = 0;
  183. //! Returns the list of arguments
  184. virtual QStringList& arguments() = 0;
  185. //! Returns the list of arguments (const version)
  186. virtual const QStringList& arguments() const = 0;
  187. //! Returns a (shared) progress dialog (if any is available)
  188. virtual ccProgressDialog* progressDialog();
  189. //! Returns a (widget) parent (if any is available)
  190. virtual QDialog* widgetParent();
  191. public: //file I/O
  192. //Extended file loading parameters
  193. struct CCPLUGIN_LIB_API CLLoadParameters : public FileIOFilter::LoadParameters
  194. {
  195. CLLoadParameters();
  196. bool coordinatesShiftEnabled;
  197. CCVector3d coordinatesShift;
  198. };
  199. //! File loading parameters
  200. virtual CLLoadParameters& fileLoadingParams();
  201. //! Global Shift options
  202. struct GlobalShiftOptions
  203. {
  204. enum Mode { NO_GLOBAL_SHIFT, AUTO_GLOBAL_SHIFT, FIRST_GLOBAL_SHIFT, CUSTOM_GLOBAL_SHIFT };
  205. Mode mode = NO_GLOBAL_SHIFT;
  206. CCVector3d customGlobalShift;
  207. };
  208. //! Sets the global shift options
  209. /** \warning Should be called before calling fileLoadingParams() if importFile has not been called already.
  210. **/
  211. virtual void setGlobalShiftOptions(const GlobalShiftOptions& globalShiftOptions) = 0;
  212. //! Loads a file with a specific filter
  213. /** Automatically dispatches the entities between the clouds and meshes sets.
  214. **/
  215. virtual bool importFile(QString filename, const GlobalShiftOptions& globalShiftOptions, FileIOFilter::Shared filter = FileIOFilter::Shared(nullptr)) = 0;
  216. //! Updates the internal state of the stored global shift information
  217. virtual void updateInteralGlobalShift(const GlobalShiftOptions& globalShiftOptions) = 0;
  218. //! Returns the current cloud(s) export format
  219. virtual QString cloudExportFormat() const = 0;
  220. //! Returns the current cloud(s) export extension (warning: can be anything)
  221. virtual QString cloudExportExt() const = 0;
  222. //! Returns the current mesh(es) export format
  223. virtual QString meshExportFormat() const = 0;
  224. //! Returns the current mesh(es) export extension (warning: can be anything)
  225. virtual QString meshExportExt() const = 0;
  226. //! Returns the current hierarchy(ies) export format
  227. virtual QString hierarchyExportFormat() const = 0;
  228. //! Returns the current hierarchy(ies) export extension (warning: can be anything)
  229. virtual QString hierarchyExportExt() const = 0;
  230. //! Sets the current cloud(s) export format and extension
  231. virtual void setCloudExportFormat(QString format, QString ext) = 0;
  232. //! Sets the current mesh(es) export format and extension
  233. virtual void setMeshExportFormat(QString format, QString ext) = 0;
  234. //! Sets the current hierarchy(ies) export format and extension
  235. virtual void setHierarchyExportFormat(QString format, QString ext) = 0;
  236. public: //logging
  237. //logging
  238. virtual void printVerbose(const QString& message) const = 0;
  239. virtual void print(const QString& message) const = 0;
  240. virtual void printHigh(const QString& message) const = 0;
  241. virtual void printDebug(const QString& message) const = 0;
  242. virtual void warning(const QString& message) const = 0;
  243. virtual void warningDebug(const QString& message) const = 0;
  244. virtual bool error(const QString& message) const = 0; //must always return false!
  245. virtual bool errorDebug(const QString& message) const = 0; //must always return false!
  246. public: //access to data
  247. //! Currently opened point clouds and their filename
  248. virtual std::vector< CLCloudDesc >& clouds();
  249. //! Currently opened point clouds and their filename (const version)
  250. virtual const std::vector< CLCloudDesc >& clouds() const;
  251. //! Currently opened meshes and their filename
  252. virtual std::vector< CLMeshDesc >& meshes();
  253. //! Currently opened meshes and their filename (const version)
  254. virtual const std::vector< CLMeshDesc >& meshes() const;
  255. //! Toggles silent mode
  256. /** Must be called BEFORE calling start. **/
  257. void toggleSilentMode(bool state);
  258. //! Returns the silent mode
  259. bool silentMode() const;
  260. //! Sets whether files should be automatically saved (after each process) or not
  261. void toggleAutoSaveMode(bool state);
  262. //! Returns whether files should be automatically saved (after each process) or not
  263. bool autoSaveMode() const;
  264. //! Sets whether a timestamp should be automatically added to output files or not
  265. void toggleAddTimestamp(bool state);
  266. //! Returns whether a timestamp should be automatically added to output files or not
  267. bool addTimestamp() const;
  268. //! Sets the numerical precision
  269. void setNumericalPrecision(int p);
  270. //! Returns the numerical precision
  271. int numericalPrecision() const;
  272. public: //Global shift management
  273. //! Returns whether the next command is the '-GLOBAL_SHIFT' option
  274. bool nextCommandIsGlobalShift() const;
  275. //! Check the current command line argument stack against the 'COMMAND_OPEN_SHIFT_ON_LOAD' keyword and process the following commands if necessary
  276. /** \warning This method assumes the 'COMMAND_OPEN_SHIFT_ON_LOAD' argument has already been removed from the argument stack
  277. **/
  278. bool processGlobalShiftCommand(GlobalShiftOptions& options);
  279. protected: //members
  280. //! Currently opened AND SELECTED point clouds and their respective filename
  281. std::vector< CLCloudDesc > m_clouds;
  282. //! Currently opened BUT NOT SELECTED point clouds and their respective filename
  283. std::vector< CLCloudDesc > m_unselectedClouds;
  284. //! Currently opened AND SELECTED meshes and their respective filename
  285. std::vector< CLMeshDesc > m_meshes;
  286. //! Currently opened BUT NOT SELECTED meshes and their respective filename
  287. std::vector< CLMeshDesc > m_unselectedMeshes;
  288. //! Silent mode
  289. bool m_silentMode;
  290. //! Whether files should be automatically saved (after each process) or not
  291. bool m_autoSaveMode;
  292. //! Whether a timestamp should be automatically added to output files or not
  293. bool m_addTimestamp;
  294. //! Default numerical precision for ASCII output
  295. int m_precision;
  296. //! File loading parameters
  297. CLLoadParameters m_loadingParameters;
  298. };