Octree.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/List.h"
  24. #include "../Core/Mutex.h"
  25. #include "../Graphics/Drawable.h"
  26. #include "../Graphics/OctreeQuery.h"
  27. namespace Urho3D
  28. {
  29. class Octree;
  30. static const int NUM_OCTANTS = 8;
  31. static const unsigned ROOT_INDEX = M_MAX_UNSIGNED;
  32. /// %Octree octant.
  33. /// @nobind
  34. class URHO3D_API Octant
  35. {
  36. public:
  37. /// Construct.
  38. Octant(const BoundingBox& box, unsigned level, Octant* parent, Octree* root, unsigned index = ROOT_INDEX);
  39. /// Destruct. Move drawables to root if available (detach if not) and free child octants.
  40. virtual ~Octant();
  41. /// Return or create a child octant.
  42. Octant* GetOrCreateChild(unsigned index);
  43. /// Delete child octant.
  44. void DeleteChild(unsigned index);
  45. /// Insert a drawable object by checking for fit recursively.
  46. void InsertDrawable(Drawable* drawable);
  47. /// Check if a drawable object fits.
  48. bool CheckDrawableFit(const BoundingBox& box) const;
  49. /// Add a drawable object to this octant.
  50. void AddDrawable(Drawable* drawable)
  51. {
  52. drawable->SetOctant(this);
  53. drawables_.Push(drawable);
  54. IncDrawableCount();
  55. }
  56. /// Remove a drawable object from this octant.
  57. void RemoveDrawable(Drawable* drawable, bool resetOctant = true)
  58. {
  59. if (drawables_.Remove(drawable))
  60. {
  61. if (resetOctant)
  62. drawable->SetOctant(nullptr);
  63. DecDrawableCount();
  64. }
  65. }
  66. /// Return world-space bounding box.
  67. /// @property
  68. const BoundingBox& GetWorldBoundingBox() const { return worldBoundingBox_; }
  69. /// Return bounding box used for fitting drawable objects.
  70. const BoundingBox& GetCullingBox() const { return cullingBox_; }
  71. /// Return subdivision level.
  72. unsigned GetLevel() const { return level_; }
  73. /// Return parent octant.
  74. Octant* GetParent() const { return parent_; }
  75. /// Return octree root.
  76. Octree* GetRoot() const { return root_; }
  77. /// Return number of drawables.
  78. unsigned GetNumDrawables() const { return numDrawables_; }
  79. /// Return true if there are no drawable objects in this octant and child octants.
  80. bool IsEmpty() { return numDrawables_ == 0; }
  81. /// Reset root pointer recursively. Called when the whole octree is being destroyed.
  82. void ResetRoot();
  83. /// Draw bounds to the debug graphics recursively.
  84. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  85. protected:
  86. /// Initialize bounding box.
  87. void Initialize(const BoundingBox& box);
  88. /// Return drawable objects by a query, called internally.
  89. void GetDrawablesInternal(OctreeQuery& query, bool inside) const;
  90. /// Return drawable objects by a ray query, called internally.
  91. void GetDrawablesInternal(RayOctreeQuery& query) const;
  92. /// Return drawable objects only for a threaded ray query, called internally.
  93. void GetDrawablesOnlyInternal(RayOctreeQuery& query, PODVector<Drawable*>& drawables) const;
  94. /// Increase drawable object count recursively.
  95. void IncDrawableCount()
  96. {
  97. ++numDrawables_;
  98. if (parent_)
  99. parent_->IncDrawableCount();
  100. }
  101. /// Decrease drawable object count recursively and remove octant if it becomes empty.
  102. void DecDrawableCount()
  103. {
  104. Octant* parent = parent_;
  105. --numDrawables_;
  106. if (!numDrawables_)
  107. {
  108. if (parent)
  109. parent->DeleteChild(index_);
  110. }
  111. if (parent)
  112. parent->DecDrawableCount();
  113. }
  114. /// World bounding box.
  115. BoundingBox worldBoundingBox_;
  116. /// Bounding box used for drawable object fitting.
  117. BoundingBox cullingBox_;
  118. /// Drawable objects.
  119. PODVector<Drawable*> drawables_;
  120. /// Child octants.
  121. Octant* children_[NUM_OCTANTS]{};
  122. /// World bounding box center.
  123. Vector3 center_;
  124. /// World bounding box half size.
  125. Vector3 halfSize_;
  126. /// Subdivision level.
  127. unsigned level_;
  128. /// Number of drawable objects in this octant and child octants.
  129. unsigned numDrawables_{};
  130. /// Parent octant.
  131. Octant* parent_;
  132. /// Octree root.
  133. Octree* root_;
  134. /// Octant index relative to its siblings or ROOT_INDEX for root octant.
  135. unsigned index_;
  136. };
  137. /// %Octree component. Should be added only to the root scene node.
  138. class URHO3D_API Octree : public Component, public Octant
  139. {
  140. URHO3D_OBJECT(Octree, Component);
  141. public:
  142. /// Construct.
  143. explicit Octree(Context* context);
  144. /// Destruct.
  145. ~Octree() override;
  146. /// Register object factory.
  147. static void RegisterObject(Context* context);
  148. /// Visualize the component as debug geometry.
  149. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;
  150. /// Set size and maximum subdivision levels. If octree is not empty, drawable objects will be temporarily moved to the root.
  151. void SetSize(const BoundingBox& box, unsigned numLevels);
  152. /// Update and reinsert drawable objects.
  153. void Update(const FrameInfo& frame);
  154. /// Add a drawable manually.
  155. void AddManualDrawable(Drawable* drawable);
  156. /// Remove a manually added drawable.
  157. void RemoveManualDrawable(Drawable* drawable);
  158. /// Return drawable objects by a query.
  159. /// @nobind
  160. void GetDrawables(OctreeQuery& query) const;
  161. /// Return drawable objects by a ray query.
  162. void Raycast(RayOctreeQuery& query) const;
  163. /// Return the closest drawable object by a ray query.
  164. void RaycastSingle(RayOctreeQuery& query) const;
  165. /// Return subdivision levels.
  166. /// @property
  167. unsigned GetNumLevels() const { return numLevels_; }
  168. /// Mark drawable object as requiring an update and a reinsertion.
  169. void QueueUpdate(Drawable* drawable);
  170. /// Cancel drawable object's update.
  171. void CancelUpdate(Drawable* drawable);
  172. /// Visualize the component as debug geometry.
  173. void DrawDebugGeometry(bool depthTest);
  174. private:
  175. /// Handle render update in case of headless execution.
  176. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  177. /// Update octree size.
  178. void UpdateOctreeSize() { SetSize(worldBoundingBox_, numLevels_); }
  179. /// Drawable objects that require update.
  180. PODVector<Drawable*> drawableUpdates_;
  181. /// Drawable objects that were inserted during threaded update phase.
  182. PODVector<Drawable*> threadedDrawableUpdates_;
  183. /// Mutex for octree reinsertions.
  184. Mutex octreeMutex_;
  185. /// Ray query temporary list of drawables.
  186. mutable PODVector<Drawable*> rayQueryDrawables_;
  187. /// Subdivision level.
  188. unsigned numLevels_;
  189. };
  190. }