ccDrawableObject.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_DRAWABLE_OBJECT_HEADER
  18. #define CC_DRAWABLE_OBJECT_HEADER
  19. //Local
  20. #include "ccGLDrawContext.h"
  21. //CCCoreLib
  22. #include <CCGeom.h>
  23. class ccGenericGLDisplay;
  24. //! Simple (clipping) plane equation
  25. struct ccClipPlane
  26. {
  27. Tuple4Tpl<double> equation;
  28. };
  29. using ccClipPlaneSet = std::vector<ccClipPlane>;
  30. //! Generic interface for (3D) drawable entities
  31. class QCC_DB_LIB_API ccDrawableObject
  32. {
  33. public:
  34. //! Default constructor
  35. ccDrawableObject();
  36. //! Copy constructor
  37. ccDrawableObject(const ccDrawableObject& object);
  38. virtual ~ccDrawableObject() = default;
  39. public: //drawing and drawing options
  40. //! Draws entity and its children
  41. virtual void draw(CC_DRAW_CONTEXT& context) = 0;
  42. //! Returns whether entity is visible or not
  43. inline virtual bool isVisible() const { return m_visible; }
  44. //! Sets entity visibility
  45. inline virtual void setVisible(bool state) { m_visible = state; }
  46. //! Toggles visibility
  47. inline virtual void toggleVisibility() { setVisible(!isVisible()); }
  48. //! Returns whether visibility is locked or not
  49. inline virtual bool isVisibilityLocked() const { return m_lockedVisibility; }
  50. //! Locks/unlocks visibility
  51. /** If visibility is locked, the user won't be able to modify it
  52. (via the properties tree for instance).
  53. **/
  54. inline virtual void lockVisibility(bool state) { m_lockedVisibility = state; }
  55. //! Returns whether entity is selected or not
  56. inline virtual bool isSelected() const { return m_selected; }
  57. //! Selects/unselects entity
  58. inline virtual void setSelected(bool state) { m_selected = state; }
  59. //! Returns main OpenGL parameters for this entity
  60. /** These parameters are deduced from the visibility states
  61. of its different features (points, normals, etc.).
  62. \param params a glDrawParams structure
  63. **/
  64. virtual void getDrawingParameters(glDrawParams& params) const;
  65. //! Returns whether colors are enabled or not
  66. inline virtual bool hasColors() const { return false; }
  67. //! Returns whether colors are shown or not
  68. inline virtual bool colorsShown() const { return m_colorsDisplayed; }
  69. //! Sets colors visibility
  70. inline virtual void showColors(bool state) { m_colorsDisplayed = state; }
  71. //! Toggles colors display state
  72. inline virtual void toggleColors() { showColors(!colorsShown()); }
  73. //! Returns whether normals are enabled or not
  74. inline virtual bool hasNormals() const { return false; }
  75. //! Returns whether normals are shown or not
  76. inline virtual bool normalsShown() const { return m_normalsDisplayed; }
  77. //! Sets normals visibility
  78. inline virtual void showNormals(bool state) { m_normalsDisplayed = state; }
  79. //! Toggles normals display state
  80. inline virtual void toggleNormals() { showNormals(!normalsShown()); }
  81. public: //scalar fields
  82. //! Returns whether an active scalar field is available or not
  83. inline virtual bool hasDisplayedScalarField() const { return false; }
  84. //! Returns whether one or more scalar fields are instantiated
  85. /** \warning doesn't mean a scalar field is currently displayed
  86. (see ccDrawableObject::hasDisplayedScalarField).
  87. **/
  88. inline virtual bool hasScalarFields() const { return false; }
  89. //! Sets active scalarfield visibility
  90. inline virtual void showSF(bool state) { m_sfDisplayed = state; }
  91. //! Toggles SF display state
  92. inline virtual void toggleSF() { showSF(!sfShown()); }
  93. //! Returns whether active scalar field is visible
  94. inline virtual bool sfShown() const { return m_sfDisplayed; }
  95. public: //(Mesh) materials
  96. //! Toggles material display state
  97. virtual void toggleMaterials() {} //does nothing by default!
  98. public: //Name display in 3D
  99. //! Sets whether name should be displayed in 3D or not
  100. inline virtual void showNameIn3D(bool state) { m_showNameIn3D = state; if (!state) m_nameIn3DPosIsValid = false; }
  101. //! Returns whether name is displayed in 3D or not
  102. inline virtual bool nameShownIn3D() const { return m_showNameIn3D; }
  103. //! Toggles name in 3D display state
  104. inline virtual void toggleShowName() { showNameIn3D(!nameShownIn3D()); }
  105. public: //Temporary color
  106. //! Returns whether colors are currently overridden by a temporary (unique) color
  107. /** See ccDrawableObject::setTempColor.
  108. **/
  109. inline virtual bool isColorOverridden() const { return m_colorIsOverridden; }
  110. //! Returns current temporary (unique) color
  111. inline virtual const ccColor::Rgba& getTempColor() const { return m_tempColor; }
  112. //! Sets current temporary (unique)
  113. /** \param col rgba color
  114. \param autoActivate auto activates temporary color
  115. **/
  116. virtual void setTempColor(const ccColor::Rgba& col, bool autoActivate = true);
  117. //! Sets current temporary (unique)
  118. /** \param col rgb color
  119. \param autoActivate auto activates temporary color
  120. **/
  121. virtual void setTempColor(const ccColor::Rgb& col, bool autoActivate = true);
  122. //! Set temporary color activation state
  123. inline virtual void enableTempColor(bool state) { m_colorIsOverridden = state; }
  124. public: //associated display management
  125. //! Unlinks entity from a GL display (only if it belongs to it of course)
  126. virtual void removeFromDisplay(const ccGenericGLDisplay* win);
  127. //! Sets associated GL display
  128. virtual void setDisplay(ccGenericGLDisplay* win);
  129. //! Returns associated GL display
  130. inline virtual ccGenericGLDisplay* getDisplay() const { return m_currentDisplay; }
  131. //! Redraws associated GL display
  132. virtual void redrawDisplay();
  133. //! Sets associated GL display 'refreshable' before global refresh
  134. /** Only tagged displays will be refreshed when ccGenericGLDisplay::refresh
  135. is called (see also MainWindow::RefreshAllGLWindow,
  136. MainWindow::refreshAll and ccDrawableObject::refreshDisplay).
  137. **/
  138. virtual void prepareDisplayForRefresh();
  139. //! Refreshes associated GL display
  140. /** See ccGenericGLDisplay::refresh. The display will only be updated
  141. if it has been 'prepared for refresh' (see prepareDisplayForRefresh).
  142. **/
  143. virtual void refreshDisplay(bool only2D = false);
  144. public: //Transformation matrix management (for display only)
  145. //! Associates entity with a GL transformation (rotation + translation)
  146. /** \warning FOR DISPLAY PURPOSE ONLY (i.e. should only be temporary)
  147. If the associated GL transformation is enabled (see
  148. ccDrawableObject::enableGLTransformation), it will
  149. be applied before displaying this entity.
  150. However it will not be taken into account by any CCCoreLib algorithm
  151. (distance computation, etc.) for instance.
  152. Note: GL transformation is automatically enabled.
  153. **/
  154. virtual void setGLTransformation(const ccGLMatrix& trans);
  155. //! Enables/disables associated GL transformation
  156. /** See ccDrawableObject::setGLTransformation.
  157. **/
  158. virtual void enableGLTransformation(bool state);
  159. //! Returns whether a GL transformation is enabled or not
  160. inline virtual bool isGLTransEnabled() const { return m_glTransEnabled; }
  161. //! Returns associated GL transformation
  162. /** See ccDrawableObject::setGLTransformation.
  163. **/
  164. inline virtual const ccGLMatrix& getGLTransformation() const { return m_glTrans; }
  165. //! Resets associated GL transformation
  166. /** GL transformation is reset to identity.
  167. Note: GL transformation is automatically disabled.
  168. See ccDrawableObject::setGLTransformation.
  169. **/
  170. virtual void resetGLTransformation();
  171. //! Multiplies (left) current GL transformation by a rotation matrix
  172. /** 'GLtrans = M * GLtrans'
  173. Note: GL transformation is automatically enabled.
  174. See ccDrawableObject::setGLTransformation.
  175. **/
  176. virtual void rotateGL(const ccGLMatrix& rotMat);
  177. //! Translates current GL transformation by a rotation matrix
  178. /** 'GLtrans = GLtrans + T'
  179. Note: GL transformation is automatically enabled.
  180. See ccDrawableObject::setGLTransformation.
  181. **/
  182. virtual void translateGL(const CCVector3& trans);
  183. public: //clipping planes
  184. //! Removes all clipping planes (if any)
  185. virtual void removeAllClipPlanes() { m_clipPlanes.resize(0); }
  186. //! Registers a new clipping plane
  187. /** \return false if the planes couldn't be added (not enough memory)
  188. **/
  189. virtual bool addClipPlanes(const ccClipPlane& plane);
  190. //! Enables or disables clipping planes (OpenGL)
  191. /** \warning If enabling the clipping planes, be sure to call this method AFTER the modelview matrix has been set.
  192. **/
  193. virtual void toggleClipPlanes(CC_DRAW_CONTEXT& context, bool enable);
  194. public: //push and pop display state
  195. //! Display state
  196. struct DisplayState
  197. {
  198. DisplayState() {}
  199. DisplayState(const ccDrawableObject& dobj);
  200. using Shared = QSharedPointer<DisplayState>;
  201. bool visible = false;
  202. bool colorsDisplayed = false;
  203. bool normalsDisplayed = false;
  204. bool sfDisplayed = false;
  205. bool colorIsOverridden = false;
  206. bool showNameIn3D = false;
  207. ccGenericGLDisplay* display = nullptr;
  208. };
  209. //! Pushes the current display state
  210. virtual bool pushDisplayState();
  211. //! Pops the last pushed display state
  212. virtual void popDisplayState(bool apply = true);
  213. //! Applies a display state
  214. virtual void applyDisplayState(const DisplayState& state);
  215. protected: //members
  216. //! Specifies whether the object is visible or not
  217. /** Note: this does not influence the children visibility
  218. **/
  219. bool m_visible;
  220. //! Specifies whether the object is selected or not
  221. bool m_selected;
  222. //! Specifies whether the visibility can be changed by user or not
  223. bool m_lockedVisibility;
  224. //! Specifies whether colors should be displayed
  225. bool m_colorsDisplayed;
  226. //! Specifies whether normals should be displayed
  227. bool m_normalsDisplayed;
  228. //! Specifies whether scalar field should be displayed
  229. bool m_sfDisplayed;
  230. //! Temporary (unique) color
  231. ccColor::Rgba m_tempColor;
  232. //! Temporary (unique) color activation state
  233. bool m_colorIsOverridden;
  234. //! Current GL transformation
  235. /** See ccDrawableObject::setGLTransformation.
  236. **/
  237. ccGLMatrix m_glTrans;
  238. //! Current GL transformation activation state
  239. /** See ccDrawableObject::setGLTransformation.
  240. **/
  241. bool m_glTransEnabled;
  242. //! Whether name is displayed in 3D or not
  243. bool m_showNameIn3D;
  244. //! Last 2D position of the '3D' name
  245. CCVector3d m_nameIn3DPos;
  246. //! Whether the last 2D position of the '3D' name is valid or not
  247. bool m_nameIn3DPosIsValid;
  248. //! Currently associated GL display
  249. ccGenericGLDisplay* m_currentDisplay;
  250. //! Active clipping planes (used for display only)
  251. ccClipPlaneSet m_clipPlanes;
  252. //! The stack of pushed display states
  253. std::vector<DisplayState::Shared> m_displayStateStack;
  254. };
  255. #endif //CC_DRAWABLE_OBJECT_HEADER