Octree.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  28. {
  29. class Octree;
  30. static const int NUM_OCTANTS = 8;
  31. static const unsigned ROOT_INDEX = M_MAX_UNSIGNED;
  32. /// %Octree octant
  33. class ATOMIC_API Octant
  34. {
  35. public:
  36. /// Construct.
  37. Octant(const BoundingBox& box, unsigned level, Octant* parent, Octree* root, unsigned index = ROOT_INDEX);
  38. /// Destruct. Move drawables to root if available (detach if not) and free child octants.
  39. virtual ~Octant();
  40. /// Return or create a child octant.
  41. Octant* GetOrCreateChild(unsigned index);
  42. /// Delete child octant.
  43. void DeleteChild(unsigned index);
  44. /// Insert a drawable object by checking for fit recursively.
  45. void InsertDrawable(Drawable* drawable);
  46. /// Check if a drawable object fits.
  47. bool CheckDrawableFit(const BoundingBox& box) const;
  48. /// Add a drawable object to this octant.
  49. void AddDrawable(Drawable* drawable)
  50. {
  51. drawable->SetOctant(this);
  52. drawables_.Push(drawable);
  53. IncDrawableCount();
  54. }
  55. /// Remove a drawable object from this octant.
  56. void RemoveDrawable(Drawable* drawable, bool resetOctant = true)
  57. {
  58. if (drawables_.Remove(drawable))
  59. {
  60. if (resetOctant)
  61. drawable->SetOctant(0);
  62. DecDrawableCount();
  63. }
  64. }
  65. /// Return world-space bounding box.
  66. const BoundingBox& GetWorldBoundingBox() const { return worldBoundingBox_; }
  67. /// Return bounding box used for fitting drawable objects.
  68. const BoundingBox& GetCullingBox() const { return cullingBox_; }
  69. /// Return subdivision level.
  70. unsigned GetLevel() const { return level_; }
  71. /// Return parent octant.
  72. Octant* GetParent() const { return parent_; }
  73. /// Return octree root.
  74. Octree* GetRoot() const { return root_; }
  75. /// Return number of drawables.
  76. unsigned GetNumDrawables() const { return numDrawables_; }
  77. /// Return true if there are no drawable objects in this octant and child octants.
  78. bool IsEmpty() { return numDrawables_ == 0; }
  79. /// Reset root pointer recursively. Called when the whole octree is being destroyed.
  80. void ResetRoot();
  81. /// Draw bounds to the debug graphics recursively.
  82. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  83. protected:
  84. /// Initialize bounding box.
  85. void Initialize(const BoundingBox& box);
  86. /// Return drawable objects by a query, called internally.
  87. void GetDrawablesInternal(OctreeQuery& query, bool inside) const;
  88. /// Return drawable objects by a ray query, called internally.
  89. void GetDrawablesInternal(RayOctreeQuery& query) const;
  90. /// Return drawable objects only for a threaded ray query, called internally.
  91. void GetDrawablesOnlyInternal(RayOctreeQuery& query, PODVector<Drawable*>& drawables) const;
  92. /// Increase drawable object count recursively.
  93. void IncDrawableCount()
  94. {
  95. ++numDrawables_;
  96. if (parent_)
  97. parent_->IncDrawableCount();
  98. }
  99. /// Decrease drawable object count recursively and remove octant if it becomes empty.
  100. void DecDrawableCount()
  101. {
  102. Octant* parent = parent_;
  103. --numDrawables_;
  104. if (!numDrawables_)
  105. {
  106. if (parent)
  107. parent->DeleteChild(index_);
  108. }
  109. if (parent)
  110. parent->DecDrawableCount();
  111. }
  112. /// World bounding box.
  113. BoundingBox worldBoundingBox_;
  114. /// Bounding box used for drawable object fitting.
  115. BoundingBox cullingBox_;
  116. /// Drawable objects.
  117. PODVector<Drawable*> drawables_;
  118. /// Child octants.
  119. Octant* children_[NUM_OCTANTS];
  120. /// World bounding box center.
  121. Vector3 center_;
  122. /// World bounding box half size.
  123. Vector3 halfSize_;
  124. /// Subdivision level.
  125. unsigned level_;
  126. /// Number of drawable objects in this octant and child octants.
  127. unsigned numDrawables_;
  128. /// Parent octant.
  129. Octant* parent_;
  130. /// Octree root.
  131. Octree* root_;
  132. /// Octant index relative to its siblings or ROOT_INDEX for root octant
  133. unsigned index_;
  134. };
  135. /// %Octree component. Should be added only to the root scene node
  136. class ATOMIC_API Octree : public Component, public Octant
  137. {
  138. friend void RaycastDrawablesWork(const WorkItem* item, unsigned threadIndex);
  139. ATOMIC_OBJECT(Octree, Component);
  140. public:
  141. /// Construct.
  142. Octree(Context* context);
  143. /// Destruct.
  144. ~Octree();
  145. /// Register object factory.
  146. static void RegisterObject(Context* context);
  147. /// Handle attribute change.
  148. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  149. /// Visualize the component as debug geometry.
  150. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  151. /// Set size and maximum subdivision levels. If octree is not empty, drawable objects will be temporarily moved to the root.
  152. void SetSize(const BoundingBox& box, unsigned numLevels);
  153. /// Update and reinsert drawable objects.
  154. void Update(const FrameInfo& frame);
  155. /// Add a drawable manually.
  156. void AddManualDrawable(Drawable* drawable);
  157. /// Remove a manually added drawable.
  158. void RemoveManualDrawable(Drawable* drawable);
  159. /// Return drawable objects by a query.
  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. unsigned GetNumLevels() const { return numLevels_; }
  167. /// Mark drawable object as requiring an update and a reinsertion.
  168. void QueueUpdate(Drawable* drawable);
  169. /// Cancel drawable object's update.
  170. void CancelUpdate(Drawable* drawable);
  171. /// Visualize the component as debug geometry.
  172. void DrawDebugGeometry(bool depthTest);
  173. private:
  174. /// Handle render update in case of headless execution.
  175. void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
  176. /// Drawable objects that require update.
  177. PODVector<Drawable*> drawableUpdates_;
  178. /// Drawable objects that were inserted during threaded update phase.
  179. PODVector<Drawable*> threadedDrawableUpdates_;
  180. /// Mutex for octree reinsertions.
  181. Mutex octreeMutex_;
  182. /// Ray query temporary list of drawables.
  183. mutable PODVector<Drawable*> rayQueryDrawables_;
  184. /// Subdivision level.
  185. unsigned numLevels_;
  186. };
  187. }