ccOctree.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_OCTREE_HEADER
  18. #define CC_OCTREE_HEADER
  19. //Local
  20. #include "ccGenericGLDisplay.h"
  21. #include "ccHObject.h"
  22. //CCCoreLib
  23. #include <DgmOctree.h>
  24. #include <ReferenceCloud.h>
  25. //Qt
  26. #include <QObject>
  27. class ccGenericPointCloud;
  28. class ccOctreeFrustumIntersector;
  29. class ccCameraSensor;
  30. //! Octree structure
  31. /** Extends the CCCoreLib::DgmOctree class.
  32. **/
  33. class QCC_DB_LIB_API ccOctree : public QObject, public CCCoreLib::DgmOctree
  34. {
  35. Q_OBJECT
  36. public: //GENERAL METHODS
  37. //! Shared pointer
  38. typedef QSharedPointer<ccOctree> Shared;
  39. //! Default constructor
  40. /** \param cloud a point cloud
  41. **/
  42. explicit ccOctree(ccGenericPointCloud* cloud);
  43. //! Destructor
  44. virtual ~ccOctree();
  45. //! Multiplies the bounding-box of the octree
  46. /** If the cloud coordinates are simply multiplied by the same factor,
  47. there is no use in recomputing the octree structure. It's sufficient
  48. to update its bounding-box.
  49. \param multFactor multiplication factor
  50. **/
  51. void multiplyBoundingBox(const PointCoordinateType multFactor);
  52. //! Translates the bounding-box of the octree
  53. /** If the cloud has been simply translated, there is no use to recompute
  54. the octree structure. It's sufficient to update its bounding-box.
  55. \param T translation vector
  56. **/
  57. void translateBoundingBox(const CCVector3& T);
  58. //! Returns the octree (square) bounding-box
  59. ccBBox getSquareBB() const;
  60. //! Returns the points bounding-box
  61. ccBBox getPointsBB() const;
  62. //inherited from DgmOctree
  63. virtual void clear() override;
  64. public: //RENDERING
  65. //! Returns the currently displayed octree level
  66. int getDisplayedLevel() const { return m_displayedLevel; }
  67. //! Sets the currently displayed octree level
  68. void setDisplayedLevel(int level);
  69. //! Octree displaying methods
  70. enum DisplayMode {
  71. WIRE = 0, /**< The octree is displayed as wired boxes (one box per cell) */
  72. MEAN_POINTS = 1, /**< The octree is displayed as points (one point per cell = the center of gravity of the points lying in it) */
  73. MEAN_CUBES = 2 /**< The octree is displayed as plain 3D cubes (one cube per cell) */
  74. };
  75. //! Returns the currently display mode
  76. DisplayMode getDisplayMode() const { return m_displayMode; }
  77. //! Sets the currently display mode
  78. void setDisplayMode(DisplayMode mode);
  79. //! Draws the octree
  80. void draw(CC_DRAW_CONTEXT& context, ccColor::Rgb* pickingColor = nullptr);
  81. //! Intersects octree with a camera sensor
  82. bool intersectWithFrustum( ccCameraSensor* sensor,
  83. std::vector<unsigned>& inCameraFrustum);
  84. //! Octree-driven point picking algorithm
  85. bool pointPicking( const CCVector2d& clickPos,
  86. const ccGLCameraParameters& camera,
  87. PointDescriptor& output,
  88. double pickWidth_pix = 3.0) const;
  89. public: //HELPERS
  90. //! Computes the average color of a set of points
  91. static ccColor::Rgb ComputeAverageColor( CCCoreLib::ReferenceCloud* subset,
  92. ccGenericPointCloud* sourceCloud);
  93. //! Computes the average normal of a set of points
  94. static CCVector3 ComputeAverageNorm(CCCoreLib::ReferenceCloud* subset,
  95. ccGenericPointCloud* sourceCloud);
  96. //! Tries to guess a very naive 'local radius' for octree-based computation
  97. /** \param cloud point cloud on which to process the normals.
  98. \return naive radius (one percent of the cloud largest dimension by default, unless the cloud have very few points)
  99. **/
  100. static PointCoordinateType GuessNaiveRadius(ccGenericPointCloud* cloud);
  101. //! Parameters for the GuessBestRadius method
  102. struct BestRadiusParams
  103. {
  104. int aimedPopulationPerCell = 16; //!< Aimed poulation per octree cell
  105. int aimedPopulationRange = 4; //!< Aimed poulation range per octree cell
  106. int minCellPopulation = 6; //!< Minimum cell poulation
  107. double minAboveMinRatio = 0.97; //!< Ratio of cells above the 'minCellPopulation' thershold
  108. };
  109. //! Tries to guess the best 'local radius' for octree-based computation
  110. /** The ideal radius is determined by randomly sampling up to 200 points and looking at
  111. their neighborhood.
  112. \param cloud point cloud on which to process the normals.
  113. \param params parameters
  114. \param cloudOctree input cloud octree (optional)
  115. \param progressCb progress notification (optional)
  116. \return the best radius (strictly positive value) or 0 if an error occurred
  117. **/
  118. static PointCoordinateType GuessBestRadius( ccGenericPointCloud* cloud,
  119. const BestRadiusParams& params,
  120. CCCoreLib::DgmOctree* cloudOctree = nullptr,
  121. CCCoreLib::GenericProgressCallback* progressCb = nullptr);
  122. //! Tries to guess the best 'local radius' for octree-based computation (auto-computes the octree if necessary)
  123. /** The ideal radius is determined by randomly sampling up to 200 points and looking at
  124. their neighborhood.
  125. \param cloud point cloud on which to process the normals.
  126. \param params parameters
  127. \param parentWidget parent widget (for the progress dialog, if any has to be shown)
  128. \return the best radius (strictly positive value) or 0 if an error occurred
  129. **/
  130. static PointCoordinateType GuessBestRadiusAutoComputeOctree(ccGenericPointCloud* cloud,
  131. const BestRadiusParams& params,
  132. QWidget* parentWidget = nullptr);
  133. Q_SIGNALS:
  134. //! Signal sent when the octree organization is modified (cleared, etc.)
  135. void updated();
  136. protected: ////RENDERING
  137. static bool DrawCellAsABox( const CCCoreLib::DgmOctree::octreeCell& cell,
  138. void** additionalParameters,
  139. CCCoreLib::NormalizedProgress* nProgress = 0);
  140. static bool DrawCellAsAPoint( const CCCoreLib::DgmOctree::octreeCell& cell,
  141. void** additionalParameters,
  142. CCCoreLib::NormalizedProgress* nProgress = 0);
  143. static bool DrawCellAsAPrimitive( const CCCoreLib::DgmOctree::octreeCell& cell,
  144. void** additionalParameters,
  145. CCCoreLib::NormalizedProgress* nProgress = 0);
  146. protected: //MEMBERS
  147. //! Associated cloud (as a ccGenericPointCloud)
  148. ccGenericPointCloud* m_theAssociatedCloudAsGPC;
  149. //! Displayed level
  150. int m_displayedLevel;
  151. //! Display mode
  152. DisplayMode m_displayMode;
  153. //! OpenGL display list
  154. GLuint m_glListID;
  155. //! Whether the display (list) should be refreshed or not
  156. bool m_glListIsDeprecated;
  157. //! For frustum intersection
  158. ccOctreeFrustumIntersector* m_frustumIntersector;
  159. };
  160. #endif //CC_OCTREE_HEADER