ccObject.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  15. //# #
  16. //##########################################################################
  17. #ifndef CC_OBJECT_HEADER
  18. #define CC_OBJECT_HEADER
  19. //Local
  20. #include "ccSerializableObject.h"
  21. //Qt
  22. #include <QSharedPointer>
  23. #include <QVariant>
  24. //! Object state flag
  25. enum CC_OBJECT_FLAG { //CC_UNUSED = 1, //DGM: not used anymore (former CC_FATHER_DEPENDENT)
  26. CC_ENABLED = 2,
  27. CC_LOCKED = 4,
  28. };
  29. //Bits for object type flags (64 bits)
  30. #define CC_HIERARCH_BIT 0x00000000000001 //Hierarchical object
  31. #define CC_LEAF_BIT 0x00000000000002 //Tree leaf (no children)
  32. #define CC_GROUP_BIT 0x00000000000004 //Group (no data, aggregation only)
  33. #define CC_PRIMITIVE_BIT 0x00000000000008 //Primitive (sphere, plane, torus, cylinder, etc.)
  34. #define CC_ARRAY_BIT 0x00000000000010 //Array
  35. #define CC_LABEL_BIT 0x00000000000020 //2D label
  36. #define CC_VIEWPORT_BIT 0x00000000000040 //2D viewport
  37. #define CC_CUSTOM_BIT 0x00000000000080 //For custom (plugin defined) objects
  38. #define CC_CLOUD_BIT 0x00000000000100 //Point Cloud
  39. #define CC_MESH_BIT 0x00000000000200 //Mesh
  40. #define CC_OCTREE_BIT 0x00000000000400 //Octree
  41. #define CC_POLYLINE_BIT 0x00000000000800 //Polyline
  42. #define CC_IMAGE_BIT 0x00000000001000 //Picture
  43. #define CC_SENSOR_BIT 0x00000000002000 //Sensor def.
  44. #define CC_PLANE_BIT 0x00000000004000 //Plane (primitive)
  45. #define CC_SPHERE_BIT 0x00000000008000 //Sphere (primitive)
  46. #define CC_TORUS_BIT 0x00000000010000 //Torus (primitive)
  47. #define CC_CYLINDER_BIT 0x00000000020000 //Cylinder (primitive)
  48. #define CC_CONE_BIT 0x00000000040000 //Cone (primitive)
  49. #define CC_BOX_BIT 0x00000000080000 //Box (primitive)
  50. #define CC_DISH_BIT 0x00000000100000 //Dish (primitive)
  51. #define CC_EXTRU_BIT 0x00000000200000 //Extrusion (primitive)
  52. #define CC_KDTREE_BIT 0x00000000400000 //Kd-tree
  53. #define CC_FACET_BIT 0x00000000800000 //Facet (composite object: cloud + 2D1/2 mesh + 2D1/2 polyline)
  54. #define CC_MATERIAL_BIT 0x00000001000000 //Material
  55. #define CC_CLIP_BOX_BIT 0x00000002000000 //Clipping box
  56. #define CC_TRANS_BUFFER_BIT 0x00000004000000 //Indexed transformation buffer
  57. #define CC_GROUND_BASED_BIT 0x00000008000000 //For Ground Based Lidar Sensors
  58. #define CC_RGB_COLOR_BIT 0x00000010000000 //Color (R,G,B)
  59. #define CC_NORMAL_BIT 0x00000020000000 //Normal (Nx,Ny,Nz)
  60. #define CC_COMPRESSED_NORMAL_BIT 0x00000040000000 //Compressed normal (index)
  61. #define CC_TEX_COORDS_BIT 0x00000080000000 //Texture coordinates (u,v)
  62. #define CC_CAMERA_BIT 0x00000100000000 //For camera sensors (projective sensors)
  63. #define CC_QUADRIC_BIT 0x00000200000000 //Quadric (primitive)
  64. #define CC_RGBA_COLOR_BIT 0x00000400000000 //Color (R,G,B,A)
  65. #define CC_COORDINATESYSTEM_BIT 0x00000800000000 //CoordinateSystem (primitive)
  66. #define CC_CLIP_BOX_PART_BIT 0x00001000000000 //Cliping-box component
  67. //#define CC_FREE_BIT 0x00002000000000
  68. //#define CC_FREE_BIT 0x00004000000000
  69. //#define CC_FREE_BIT 0x00008000000000
  70. //#define CC_FREE_BIT 0x00010000000000
  71. //#define CC_FREE_BIT ...
  72. //! Type of object type flags (64 bits)
  73. using CC_CLASS_ENUM = int64_t;
  74. //! CloudCompare object type flags
  75. namespace CC_TYPES
  76. {
  77. enum : CC_CLASS_ENUM {
  78. OBJECT = 0,
  79. HIERARCHY_OBJECT = CC_HIERARCH_BIT,
  80. POINT_CLOUD = HIERARCHY_OBJECT | CC_CLOUD_BIT,
  81. MESH = HIERARCHY_OBJECT | CC_MESH_BIT,
  82. SUB_MESH = HIERARCHY_OBJECT | CC_MESH_BIT | CC_LEAF_BIT,
  83. MESH_GROUP = MESH | CC_GROUP_BIT, //DEPRECATED; DEFINITION REMAINS FOR BACKWARD COMPATIBILITY ONLY
  84. FACET = HIERARCHY_OBJECT | CC_FACET_BIT,
  85. POINT_OCTREE = HIERARCHY_OBJECT | CC_OCTREE_BIT | CC_LEAF_BIT,
  86. POINT_KDTREE = HIERARCHY_OBJECT | CC_KDTREE_BIT | CC_LEAF_BIT,
  87. POLY_LINE = HIERARCHY_OBJECT | CC_POLYLINE_BIT,
  88. IMAGE = CC_HIERARCH_BIT | CC_IMAGE_BIT,
  89. CALIBRATED_IMAGE = IMAGE | CC_LEAF_BIT,
  90. SENSOR = CC_HIERARCH_BIT | CC_SENSOR_BIT,
  91. GBL_SENSOR = SENSOR | CC_GROUND_BASED_BIT,
  92. CAMERA_SENSOR = SENSOR | CC_CAMERA_BIT,
  93. PRIMITIVE = MESH | CC_PRIMITIVE_BIT, //primitives are meshes
  94. PLANE = PRIMITIVE | CC_PLANE_BIT,
  95. SPHERE = PRIMITIVE | CC_SPHERE_BIT,
  96. TORUS = PRIMITIVE | CC_TORUS_BIT,
  97. CONE = PRIMITIVE | CC_CONE_BIT,
  98. OLD_CYLINDER_ID = PRIMITIVE | CC_CYLINDER_BIT, //for backward compatibility
  99. CYLINDER = PRIMITIVE | CC_CYLINDER_BIT | CC_CONE_BIT, //cylinders are cones
  100. BOX = PRIMITIVE | CC_BOX_BIT,
  101. DISH = PRIMITIVE | CC_DISH_BIT,
  102. EXTRU = PRIMITIVE | CC_EXTRU_BIT,
  103. QUADRIC = PRIMITIVE | CC_QUADRIC_BIT,
  104. MATERIAL_SET = CC_MATERIAL_BIT | CC_GROUP_BIT | CC_LEAF_BIT,
  105. ARRAY = CC_ARRAY_BIT,
  106. NORMALS_ARRAY = CC_ARRAY_BIT | CC_NORMAL_BIT | CC_LEAF_BIT,
  107. NORMAL_INDEXES_ARRAY= CC_ARRAY_BIT | CC_COMPRESSED_NORMAL_BIT | CC_LEAF_BIT,
  108. RGB_COLOR_ARRAY = CC_ARRAY_BIT | CC_RGB_COLOR_BIT | CC_LEAF_BIT,
  109. RGBA_COLOR_ARRAY = CC_ARRAY_BIT | CC_RGBA_COLOR_BIT | CC_LEAF_BIT,
  110. TEX_COORDS_ARRAY = CC_ARRAY_BIT | CC_TEX_COORDS_BIT | CC_LEAF_BIT,
  111. LABEL_2D = HIERARCHY_OBJECT | CC_LABEL_BIT | CC_LEAF_BIT,
  112. VIEWPORT_2D_OBJECT = HIERARCHY_OBJECT | CC_VIEWPORT_BIT | CC_LEAF_BIT,
  113. VIEWPORT_2D_LABEL = VIEWPORT_2D_OBJECT | CC_LABEL_BIT,
  114. CLIPPING_BOX = CC_CLIP_BOX_BIT | CC_LEAF_BIT,
  115. CLIPPING_BOX_PART = CC_CLIP_BOX_PART_BIT| CC_LEAF_BIT,
  116. TRANS_BUFFER = HIERARCHY_OBJECT | CC_TRANS_BUFFER_BIT | CC_LEAF_BIT,
  117. COORDINATESYSTEM = PRIMITIVE | CC_COORDINATESYSTEM_BIT,
  118. // Custom types
  119. /** Custom objects are typically defined by plugins. They can be inserted in an object
  120. hierarchy or displayed in an OpenGL context like any other ccHObject.
  121. To differentiate custom objects, use the meta-data mechanism (see ccObject::getMetaData
  122. and ccObject::setMetaData). You can also define a custom icon (see ccHObject::getIcon).
  123. It is highly advised to use the ccCustomHObject and ccCustomLeafObject interfaces to
  124. define a custom types. Carefully read the ccCustomHObject::isDeserialized method's
  125. description and the warning below!
  126. Warning: custom objects can't be 'fully' serialized. Don't overload the
  127. 'ccSerializableObject::toFile' method for them as this would break the deserialization mechanism!
  128. They can only be serialized as plain ccHObject instances (CC_TYPES::HIERARCHY_OBJECT).
  129. Hierarchical custom objects (CC_TYPES::CUSTOM_H_OBJECT) will be deserialized as ccCustomHObject
  130. instances. Leaf custom objects (CC_TYPES::CUSTOM_LEAF_OBJECT) will be deserialized as
  131. ccCustomLeafObject instances.
  132. **/
  133. CUSTOM_H_OBJECT = HIERARCHY_OBJECT | CC_CUSTOM_BIT,
  134. CUSTOM_LEAF_OBJECT = CUSTOM_H_OBJECT | CC_LEAF_BIT,
  135. };
  136. }
  137. //! Unique ID generator (should be unique for the whole application instance - with plugins, etc.)
  138. class QCC_DB_LIB_API ccUniqueIDGenerator
  139. {
  140. public:
  141. static constexpr unsigned InvalidUniqueID = 0xFFFFFFFF;
  142. static constexpr unsigned MinUniqueID = 0x00000100;
  143. //! Shared type
  144. using Shared = QSharedPointer<ccUniqueIDGenerator>;
  145. //! Default constructor
  146. ccUniqueIDGenerator() : m_lastUniqueID(MinUniqueID) {}
  147. //! Resets the unique ID
  148. void reset() { m_lastUniqueID = MinUniqueID; }
  149. //! Returns a (new) unique ID
  150. unsigned fetchOne() { return ++m_lastUniqueID; }
  151. //! Returns the value of the last generated unique ID
  152. unsigned getLast() const { return m_lastUniqueID; }
  153. //! Updates the value of the last generated unique ID with the current one
  154. void update(unsigned ID) { if (ID > m_lastUniqueID) m_lastUniqueID = ID; }
  155. protected:
  156. unsigned m_lastUniqueID;
  157. };
  158. //! Generic "CloudCompare Object" template
  159. class QCC_DB_LIB_API ccObject : public ccSerializableObject
  160. {
  161. public:
  162. //! Default constructor
  163. /** \param name object name (optional)
  164. \param uniqueID unique ID (handle with care! Will be auto generated if equal to ccUniqueIDGenerator::InvalidUniqueID)
  165. **/
  166. ccObject(const QString& name = QString(), unsigned uniqueID = ccUniqueIDGenerator::InvalidUniqueID);
  167. //! Copy constructor
  168. ccObject(const ccObject& object);
  169. //! Returns current database version
  170. static unsigned GetCurrentDBVersion();
  171. //! Sets the unique ID generator
  172. static void SetUniqueIDGenerator(ccUniqueIDGenerator::Shared generator);
  173. //! Returns the unique ID generator
  174. static ccUniqueIDGenerator::Shared GetUniqueIDGenerator();
  175. //! Returns class ID
  176. virtual CC_CLASS_ENUM getClassID() const = 0;
  177. //! Returns object name
  178. virtual inline QString getName() const { return m_name; }
  179. //! Sets object name
  180. virtual inline void setName(const QString& name) { m_name = name; }
  181. //! Returns object unique ID
  182. virtual inline unsigned getUniqueID() const { return m_uniqueID; }
  183. //! Changes unique ID
  184. /** \warning HANDLE WITH CARE!
  185. Updates persistent settings (last unique ID) if necessary.
  186. **/
  187. virtual void setUniqueID(unsigned ID);
  188. //! Returns whether the object is enabled or not
  189. /** Shortcut to access flag CC_ENABLED
  190. **/
  191. virtual inline bool isEnabled() const { return getFlagState(CC_ENABLED); }
  192. //! Sets the "enabled" property
  193. /** Shortcut to modify flag CC_ENABLED
  194. **/
  195. virtual inline void setEnabled(bool state) { setFlagState(CC_ENABLED,state); }
  196. //! Toggles the "enabled" property
  197. virtual inline void toggleActivation() { setEnabled(!isEnabled()); }
  198. //! Returns whether the object is locked or not
  199. /** Shortcut to access flag CC_LOCKED
  200. **/
  201. virtual inline bool isLocked() const { return getFlagState(CC_LOCKED); }
  202. //! Sets the "enabled" property
  203. /** Shortcut to modify flag CC_LOCKED
  204. **/
  205. virtual inline void setLocked(bool state) { setFlagState(CC_LOCKED,state); }
  206. //shortcuts
  207. inline bool isLeaf() const {return (getClassID() & CC_LEAF_BIT) != 0; }
  208. inline bool isCustom() const {return (getClassID() & CC_CUSTOM_BIT) != 0; }
  209. inline bool isHierarchy() const { return (getClassID() & CC_HIERARCH_BIT) != 0; }
  210. inline bool isKindOf(CC_CLASS_ENUM type) const { return (getClassID() & type) == type; }
  211. inline bool isA(CC_CLASS_ENUM type) const { return (getClassID() == type); }
  212. //! Returns a new unassigned unique ID
  213. /** Unique IDs are handled with persistent settings
  214. in order to assure consistency between main app
  215. and plugins!
  216. **/
  217. static unsigned GetNextUniqueID();
  218. //! Returns last assigned unique ID
  219. /** Unique IDs are handled with persistent settings
  220. in order to assure consistency between main app
  221. and plugins!
  222. **/
  223. static unsigned GetLastUniqueID();
  224. //! Helper: reads out class ID from a binary stream
  225. /** Must be called before 'fromFile'!
  226. **/
  227. static CC_CLASS_ENUM ReadClassIDFromFile(QFile& in, short dataVersion);
  228. //! Returns a given associated meta data
  229. /** \param key meta data unique identifier (case sensitive)
  230. \return meta data (if any) or an invalid QVariant
  231. **/
  232. QVariant getMetaData(const QString& key) const;
  233. //! Removes a given associated meta-data
  234. /** \param key meta-data unique identifier (case sensitive)
  235. \return success
  236. **/
  237. bool removeMetaData(const QString& key);
  238. //! Sets a meta-data element
  239. /** \param key meta-data unique identifier (case sensitive)
  240. \param data data
  241. **/
  242. void setMetaData(const QString& key, const QVariant& data);
  243. //! Sets several meta-data elements at a time
  244. /** \param dataset meta-data set
  245. \param overwrite whether existing meta-data elements should be replaced by the input ones (with the same key) or not
  246. **/
  247. void setMetaData(const QVariantMap& dataset, bool overwrite = false);
  248. //! Returns whether a meta-data element with the given key exists or not
  249. /** \param key meta-data unique identifier (case sensitive)
  250. \return whether the element exists or not
  251. **/
  252. bool hasMetaData(const QString& key) const;
  253. //! Returns meta-data map (const only)
  254. const QVariantMap& metaData() const { return m_metaData; }
  255. protected:
  256. //! Returns flag state
  257. virtual inline bool getFlagState(CC_OBJECT_FLAG flag) const { return (m_flags & flag); }
  258. //! Sets flag state
  259. /** \param flag object flag to set
  260. \param state flag state
  261. **/
  262. virtual void setFlagState(CC_OBJECT_FLAG flag, bool state);
  263. //inherited from ccSerializableObject
  264. bool toFile(QFile& out, short dataVersion) const override;
  265. short minimumFileVersion() const override;
  266. //! Reimplemented from ccSerializableObject::fromFile
  267. /** Be sure to call ccObject::ReadClassIDFromFile (once)
  268. before calling this method, as the classID is voluntarily
  269. skipped (in order to let the user instantiate the object first)
  270. **/
  271. bool fromFile(QFile& in, short dataVersion, int flags, LoadedIDMap& oldToNewIDMap) override;
  272. //! Object name
  273. QString m_name;
  274. //! Object flags
  275. unsigned m_flags;
  276. //! Associated meta-data
  277. QVariantMap m_metaData;
  278. private:
  279. //! Object unique ID
  280. unsigned m_uniqueID;
  281. };
  282. #endif //CC_OBJECT_HEADER