Octree.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Drawable.h"
  25. #include "List.h"
  26. #include "Mutex.h"
  27. #include "OctreeQuery.h"
  28. class Octree;
  29. static const int NUM_OCTANTS = 8;
  30. /// %Octree octant
  31. class Octant
  32. {
  33. public:
  34. /// Construct.
  35. Octant(const BoundingBox& box, unsigned level, Octant* parent, Octree* root);
  36. /// Destruct. Move drawables to root if available (detach if not) and free child octants.
  37. virtual ~Octant();
  38. /// Return or create a child octant.
  39. Octant* GetOrCreateChild(unsigned index);
  40. /// Delete child octant.
  41. void DeleteChild(unsigned index);
  42. /// Delete child octant by pointer.
  43. void DeleteChild(Octant* octant);
  44. /// Insert a drawable object by checking for fit recursively.
  45. void InsertDrawable(Drawable* drawable, const Vector3& boxCenter, const Vector3& boxSize);
  46. /// Check if a drawable object fits.
  47. bool CheckDrawableSize(const Vector3& boxSize) 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. PODVector<Drawable*>::Iterator i = drawables_.Find(drawable);
  59. if (i != drawables_.End())
  60. {
  61. if (resetOctant)
  62. drawable->SetOctant(0);
  63. drawables_.Erase(i);
  64. DecDrawableCount();
  65. }
  66. }
  67. /// Return world bounding box.
  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. /// 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. /// Free child octants. If drawable objects still exist, move them to root.
  93. void Release();
  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(this);
  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. };
  135. /// %Octree component. Should be added only to the root scene node
  136. class Octree : public Component, public Octant
  137. {
  138. friend void RaycastDrawablesWork(const WorkItem* item, unsigned threadIndex);
  139. OBJECT(Octree);
  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. /// Resize octree. If octree is not empty, drawable objects will be temporarily moved to the root.
  150. void Resize(const BoundingBox& box, unsigned numLevels);
  151. /// Update and reinsert drawable objects.
  152. void Update(const FrameInfo& frame);
  153. /// Add a drawable manually.
  154. void AddManualDrawable(Drawable* drawable);
  155. /// Remove a manually added drawable.
  156. void RemoveManualDrawable(Drawable* drawable);
  157. /// Return drawable objects by a query.
  158. void GetDrawables(OctreeQuery& query) const;
  159. /// Return drawable objects by a ray query.
  160. void Raycast(RayOctreeQuery& query) const;
  161. /// Return the closest drawable object by a ray query.
  162. void RaycastSingle(RayOctreeQuery& query) const;
  163. /// Return subdivision levels.
  164. unsigned GetNumLevels() const { return numLevels_; }
  165. /// Mark drawable object as requiring an update.
  166. void QueueUpdate(Drawable* drawable);
  167. /// Mark drawable object as requiring a reinsertion. Is thread-safe.
  168. void QueueReinsertion(Drawable* drawable);
  169. /// Add debug geometry to the debug renderer.
  170. void DrawDebugGeometry(bool depthTest);
  171. private:
  172. /// Update drawable objects marked for update. Updates are executed in worker threads.
  173. void UpdateDrawables(const FrameInfo& frame);
  174. /// Reinsert moved drawable objects into the octree.
  175. void ReinsertDrawables(const FrameInfo& frame);
  176. /// Drawable objects that require update.
  177. Vector<WeakPtr<Drawable> > drawableUpdates_;
  178. /// Drawable objects that require reinsertion.
  179. Vector<WeakPtr<Drawable> > drawableReinsertions_;
  180. /// Mutex for octree reinsertions.
  181. Mutex octreeMutex_;
  182. /// Current threaded ray query.
  183. mutable RayOctreeQuery* rayQuery_;
  184. /// Drawable list for threaded ray query.
  185. mutable PODVector<Drawable*> rayQueryDrawables_;
  186. /// Threaded ray query intermediate results.
  187. mutable Vector<PODVector<RayQueryResult> > rayQueryResults_;
  188. /// Subdivision level.
  189. unsigned numLevels_;
  190. };