Octree.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  29. {
  30. class Octree;
  31. static const int NUM_OCTANTS = 8;
  32. /// %Octree octant
  33. class Octant
  34. {
  35. public:
  36. /// Construct.
  37. Octant(const BoundingBox& box, unsigned level, Octant* parent, Octree* root);
  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. /// Delete child octant by pointer.
  45. void DeleteChild(Octant* octant);
  46. /// Insert a drawable object by checking for fit recursively.
  47. void InsertDrawable(Drawable* drawable, const Vector3& boxCenter, const Vector3& boxSize);
  48. /// Check if a drawable object fits.
  49. bool CheckDrawableSize(const Vector3& boxSize) const;
  50. /// Add a drawable object to this octant.
  51. void AddDrawable(Drawable* drawable)
  52. {
  53. drawable->SetOctant(this);
  54. drawables_.Push(drawable);
  55. IncDrawableCount();
  56. }
  57. /// Remove a drawable object from this octant.
  58. void RemoveDrawable(Drawable* drawable, bool resetOctant = true)
  59. {
  60. PODVector<Drawable*>::Iterator i = drawables_.Find(drawable);
  61. if (i != drawables_.End())
  62. {
  63. if (resetOctant)
  64. drawable->SetOctant(0);
  65. drawables_.Erase(i);
  66. DecDrawableCount();
  67. }
  68. }
  69. /// Return world bounding box.
  70. const BoundingBox& GetWorldBoundingBox() const { return worldBoundingBox_; }
  71. /// Return bounding box used for fitting drawable objects.
  72. const BoundingBox& GetCullingBox() const { return cullingBox_; }
  73. /// Return subdivision level.
  74. unsigned GetLevel() const { return level_; }
  75. /// Return parent octant.
  76. Octant* GetParent() const { return parent_; }
  77. /// Return octree root.
  78. Octree* GetRoot() const { return root_; }
  79. /// Return number of drawables.
  80. unsigned GetNumDrawables() const { return numDrawables_; }
  81. /// Return true if there are no drawable objects in this octant and child octants.
  82. bool IsEmpty() { return numDrawables_ == 0; }
  83. /// Reset root pointer recursively. Called when the whole octree is being destroyed.
  84. void ResetRoot();
  85. /// Draw bounds to the debug graphics recursively.
  86. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  87. protected:
  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. /// Free child octants. If drawable objects still exist, move them to root.
  95. void Release();
  96. /// Increase drawable object count recursively.
  97. void IncDrawableCount()
  98. {
  99. ++numDrawables_;
  100. if (parent_)
  101. parent_->IncDrawableCount();
  102. }
  103. /// Decrease drawable object count recursively and remove octant if it becomes empty.
  104. void DecDrawableCount()
  105. {
  106. Octant* parent = parent_;
  107. --numDrawables_;
  108. if (!numDrawables_)
  109. {
  110. if (parent)
  111. parent->DeleteChild(this);
  112. }
  113. if (parent)
  114. parent->DecDrawableCount();
  115. }
  116. /// World bounding box.
  117. BoundingBox worldBoundingBox_;
  118. /// Bounding box used for drawable object fitting.
  119. BoundingBox cullingBox_;
  120. /// Drawable objects.
  121. PODVector<Drawable*> drawables_;
  122. /// Child octants.
  123. Octant* children_[NUM_OCTANTS];
  124. /// World bounding box center.
  125. Vector3 center_;
  126. /// World bounding box half size.
  127. Vector3 halfSize_;
  128. /// Subdivision level.
  129. unsigned level_;
  130. /// Number of drawable objects in this octant and child octants.
  131. unsigned numDrawables_;
  132. /// Parent octant.
  133. Octant* parent_;
  134. /// Octree root.
  135. Octree* root_;
  136. };
  137. /// %Octree component. Should be added only to the root scene node
  138. class Octree : public Component, public Octant
  139. {
  140. friend void RaycastDrawablesWork(const WorkItem* item, unsigned threadIndex);
  141. OBJECT(Octree);
  142. public:
  143. /// Construct.
  144. Octree(Context* context);
  145. /// Destruct.
  146. ~Octree();
  147. /// Register object factory.
  148. static void RegisterObject(Context* context);
  149. /// Handle attribute change.
  150. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  151. /// Visualize the component as debug geometry.
  152. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  153. /// Resize octree. If octree is not empty, drawable objects will be temporarily moved to the root.
  154. void Resize(const BoundingBox& box, unsigned numLevels);
  155. /// Update and reinsert drawable objects.
  156. void Update(const FrameInfo& frame);
  157. /// Add a drawable manually.
  158. void AddManualDrawable(Drawable* drawable);
  159. /// Remove a manually added drawable.
  160. void RemoveManualDrawable(Drawable* drawable);
  161. /// Return drawable objects by a query.
  162. void GetDrawables(OctreeQuery& query) const;
  163. /// Return drawable objects by a ray query.
  164. void Raycast(RayOctreeQuery& query) const;
  165. /// Return the closest drawable object by a ray query.
  166. void RaycastSingle(RayOctreeQuery& query) const;
  167. /// Return subdivision levels.
  168. unsigned GetNumLevels() const { return numLevels_; }
  169. /// Mark drawable object as requiring an update.
  170. void QueueUpdate(Drawable* drawable);
  171. /// Mark drawable object as requiring a reinsertion. Is thread-safe.
  172. void QueueReinsertion(Drawable* drawable);
  173. /// Visualize the component as debug geometry.
  174. void DrawDebugGeometry(bool depthTest);
  175. private:
  176. /// Update drawable objects marked for update. Updates are executed in worker threads.
  177. void UpdateDrawables(const FrameInfo& frame);
  178. /// Reinsert moved drawable objects into the octree.
  179. void ReinsertDrawables(const FrameInfo& frame);
  180. /// Drawable objects that require update.
  181. Vector<WeakPtr<Drawable> > drawableUpdates_;
  182. /// Drawable objects that require reinsertion.
  183. Vector<WeakPtr<Drawable> > drawableReinsertions_;
  184. /// Mutex for octree reinsertions.
  185. Mutex octreeMutex_;
  186. /// Current threaded ray query.
  187. mutable RayOctreeQuery* rayQuery_;
  188. /// Drawable list for threaded ray query.
  189. mutable PODVector<Drawable*> rayQueryDrawables_;
  190. /// Threaded ray query intermediate results.
  191. mutable Vector<PODVector<RayQueryResult> > rayQueryResults_;
  192. /// Subdivision level.
  193. unsigned numLevels_;
  194. };
  195. }