Octree.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 <set>
  26. class Drawable;
  27. class Octree;
  28. class OctreeQuery;
  29. class RayOctreeQuery;
  30. static const int NUM_OCTANTS = 8;
  31. /// Octree octant
  32. class Octant
  33. {
  34. public:
  35. /// Construct
  36. Octant(const BoundingBox& box, unsigned level, Octant* parent, Octree* root);
  37. /// Destruct. Move drawables to root if available (detach if not) and free child octants
  38. virtual ~Octant();
  39. /// Return or create a child octant
  40. Octant* GetOrCreateChild(unsigned index);
  41. /// Delete child octant
  42. void DeleteChild(unsigned index);
  43. /// Delete child octant by pointer
  44. void DeleteChild(Octant* octant);
  45. /// Insert a drawable object by checking for fit recursively
  46. void InsertDrawable(Drawable* drawable);
  47. /// Check if a drawable object fits
  48. bool CheckDrawableSize(Drawable* drawable) const;
  49. /// Add a drawable object to this octant
  50. void AddDrawable(Drawable* drawable)
  51. {
  52. drawable->SetOctant(this);
  53. drawables_.push_back(drawable);
  54. IncDrawableCount();
  55. }
  56. /// Remove a drawable object from this octant
  57. void RemoveDrawable(Drawable* drawable, bool reSetOctant = true)
  58. {
  59. for (std::vector<Drawable*>::iterator i = drawables_.begin(); i != drawables_.end(); ++i)
  60. {
  61. if (*i == drawable)
  62. {
  63. if (reSetOctant)
  64. drawable->SetOctant(0);
  65. drawables_.erase(i);
  66. DecDrawableCount();
  67. return;
  68. }
  69. }
  70. }
  71. /// Return world bounding box
  72. const BoundingBox& GetWorldBoundingBox() const { return worldBoundingBox_; }
  73. /// Return bounding box used for fitting drawable objects
  74. const BoundingBox& GetCullingBox() const { return cullingBox_; }
  75. /// Return subdivision level
  76. unsigned GetLevel() const { return level_; }
  77. /// Return parent octant
  78. Octant* GetParent() const { return parent_; }
  79. /// Return octree root
  80. Octree* GetRoot() const { return root_; }
  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, unsigned mask) const;
  90. /// Return drawable objects by a ray query, called internally
  91. void GetDrawablesInternal(RayOctreeQuery& query) 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. /// Subdivision level
  119. unsigned level_;
  120. /// Parent octant
  121. Octant* parent_;
  122. /// Child octants
  123. Octant* children_[NUM_OCTANTS];
  124. /// Octree root
  125. Octree* root_;
  126. /// Drawable objects
  127. std::vector<Drawable*> drawables_;
  128. /// Number of drawable objects in this octant and child octants
  129. unsigned numDrawables_;
  130. };
  131. /// Octree component. Should be added only to the root scene node
  132. class Octree : public Component, public Octant
  133. {
  134. OBJECT(Octree);
  135. public:
  136. /// Construct
  137. Octree(Context* context);
  138. /// Destruct
  139. ~Octree();
  140. /// Register object factory
  141. static void RegisterObject(Context* context);
  142. /// Handle attribute change
  143. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& value);
  144. /// Resize octree. If octree is not empty, drawable objects will be temporarily moved to the root
  145. void Resize(const BoundingBox& box, unsigned numLevels);
  146. /// Update and reinsert drawable objects
  147. void Update(const FrameInfo& frame);
  148. /// Return drawable objects by a query
  149. void GetDrawables(OctreeQuery& query) const;
  150. /// Return drawable objects by a ray query
  151. void GetDrawables(RayOctreeQuery& query) const;
  152. /// Return subdivision levels
  153. unsigned GetNumLevels() const { return numLevels_; }
  154. /// Mark drawable object as requiring an update
  155. void QueueUpdate(Drawable* drawable);
  156. /// Mark drawable object as requiring a reinsertion
  157. void QueueReinsertion(Drawable* drawable);
  158. /// Remove drawable object from update list
  159. void CancelUpdate(Drawable* drawable);
  160. /// Remove drawable object from reinsertion list
  161. void CancelReinsertion(Drawable* drawable);
  162. /// Add debug geometry to the debug graphics
  163. void DrawDebugGeometry(bool depthTest);
  164. private:
  165. /// Set of drawable objects that require update
  166. std::set<Drawable*> drawableUpdates_;
  167. /// Set of drawable objects that require reinsertion
  168. std::set<Drawable*> drawableReinsertions_;
  169. /// Subdivision level
  170. unsigned numLevels_;
  171. };