Octree.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef GUL_OCTREE_H
  2. #define GUL_OCTREE_H
  3. #include <glm/glm.hpp>
  4. #include <vector>
  5. #include "aabb.h"
  6. #include <unordered_map>
  7. #include <algorithm>
  8. namespace gul
  9. {
  10. template<typename T>
  11. class OctreeNode
  12. {
  13. public:
  14. using value_type = T;
  15. using vec_type = glm::vec3;
  16. using bb_type = bb3f;
  17. using node_type = OctreeNode<T>;
  18. using V = std::pair<value_type, bb_type>;
  19. OctreeNode(vec_type const & centerPoint, float powerOfTwo) : boundingBox(centerPoint-vec_type(powerOfTwo),centerPoint+vec_type(powerOfTwo))
  20. {
  21. }
  22. OctreeNode(vec_type const & lB, vec_type const & uB) : boundingBox(lB,uB)
  23. {
  24. }
  25. size_t size() const
  26. {
  27. auto c = objects.size();
  28. for(auto & CH : children)
  29. {
  30. c += CH.size();
  31. }
  32. return c;
  33. }
  34. void erase(T const & v)
  35. {
  36. auto it = std::remove_if(objects.begin(), objects.end(), [&](auto & p) {return p.first==v;});
  37. objects.erase(it, objects.end());
  38. }
  39. OctreeNode* insert(T const & value, bb_type const & b)
  40. {
  41. // if there are no objects in this node
  42. // then we can place it directly in its
  43. // object list
  44. if(objects.size() == 0)
  45. {
  46. objects.emplace_back(value, b);
  47. return this;
  48. }
  49. else
  50. {
  51. // if the object was not inserted,
  52. // we need to split the node and into 8 children
  53. if(children.size() == 0)
  54. {
  55. split();
  56. }
  57. for(auto & c : children)
  58. {
  59. // check if the object can be fully contained within
  60. // the child node
  61. if( c.boundingBox.contains(b))
  62. {
  63. return c.insert(value, b);
  64. }
  65. }
  66. objects.emplace_back(value, b);
  67. }
  68. return nullptr;
  69. }
  70. /**
  71. * @brief split
  72. *
  73. * Split the box into 8 children
  74. */
  75. void split()
  76. {
  77. auto bb = boundingBox;
  78. bb.upperBound = mix(bb.lowerBound, bb.upperBound, 0.5f);
  79. auto D = bb.upperBound-bb.lowerBound;
  80. static constexpr vec_type offsets[] = { {0,0,0},
  81. {0,0,1},
  82. {0,1,0},
  83. {0,1,1},
  84. {1,0,0},
  85. {1,0,1},
  86. {1,1,0},
  87. {1,1,1}
  88. };
  89. children.reserve(8);
  90. for(auto & o : offsets)
  91. {
  92. children.emplace_back( bb.lowerBound + D*o, bb.upperBound + D*o);
  93. }
  94. }
  95. template<typename geometry_type, typename D>
  96. void query(geometry_type const & b, D && callable) const
  97. {
  98. if( intersects(boundingBox,b))
  99. {
  100. for(auto & o : objects)
  101. {
  102. if(intersects(o.second,b))
  103. {
  104. callable(o.first);
  105. }
  106. }
  107. for(auto & c : children)
  108. {
  109. c.query(b, callable);
  110. }
  111. }
  112. }
  113. bb_type boundingBox;
  114. std::vector<node_type> children;
  115. std::vector<V> objects;
  116. };
  117. template<typename T>
  118. class Octree
  119. {
  120. public:
  121. using value_type = T;
  122. using vec_type = glm::vec3;
  123. using bb_type = bb3f;
  124. using node_type = OctreeNode<T>;
  125. struct RegistryData
  126. {
  127. bb_type box;
  128. node_type *node;
  129. };
  130. Octree(vec_type const & centerPoint, float powerOfTwo) : m_node(centerPoint-vec_type(powerOfTwo),centerPoint+vec_type(powerOfTwo))
  131. {
  132. }
  133. Octree(vec_type const & lB, vec_type const & uB) : m_node(lB,uB)
  134. {
  135. }
  136. size_t size() const
  137. {
  138. return m_node.size();
  139. }
  140. bool erase(value_type const & v)
  141. {
  142. auto it = m_objPosition.find(v);
  143. if(it != m_objPosition.end())
  144. {
  145. it->second.node->erase(v);
  146. return true;
  147. }
  148. return false;
  149. }
  150. void insert(value_type const & v, bb_type const & box)
  151. {
  152. auto np = m_node.insert(v, box);
  153. if(np)
  154. {
  155. auto & K = m_objPosition[v];
  156. K.box = box;
  157. K.node = np;
  158. }
  159. }
  160. template<typename geometry_type, typename D>
  161. void query( std::remove_cv_t<geometry_type> const & b, D && callable) const
  162. {
  163. m_node.query(b, callable);
  164. }
  165. protected:
  166. std::unordered_map<T, RegistryData> m_objPosition;
  167. node_type m_node;
  168. };
  169. }
  170. #endif