octree.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. /*************************************************************************/
  2. /* octree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef OCTREE_H
  31. #define OCTREE_H
  32. #include "core/list.h"
  33. #include "core/map.h"
  34. #include "core/math/aabb.h"
  35. #include "core/math/geometry.h"
  36. #include "core/math/vector3.h"
  37. #include "core/print_string.h"
  38. #include "core/variant.h"
  39. typedef uint32_t OctreeElementID;
  40. #define OCTREE_ELEMENT_INVALID_ID 0
  41. #define OCTREE_SIZE_LIMIT 1e15
  42. template <class T, bool use_pairs = false, class AL = DefaultAllocator>
  43. class Octree {
  44. public:
  45. typedef void *(*PairCallback)(void *, OctreeElementID, T *, int, OctreeElementID, T *, int);
  46. typedef void (*UnpairCallback)(void *, OctreeElementID, T *, int, OctreeElementID, T *, int, void *);
  47. private:
  48. enum {
  49. NEG = 0,
  50. POS = 1,
  51. };
  52. enum {
  53. OCTANT_NX_NY_NZ,
  54. OCTANT_PX_NY_NZ,
  55. OCTANT_NX_PY_NZ,
  56. OCTANT_PX_PY_NZ,
  57. OCTANT_NX_NY_PZ,
  58. OCTANT_PX_NY_PZ,
  59. OCTANT_NX_PY_PZ,
  60. OCTANT_PX_PY_PZ
  61. };
  62. struct PairKey {
  63. union {
  64. struct {
  65. OctreeElementID A;
  66. OctreeElementID B;
  67. };
  68. uint64_t key;
  69. };
  70. _FORCE_INLINE_ bool operator<(const PairKey &p_pair) const {
  71. return key < p_pair.key;
  72. }
  73. _FORCE_INLINE_ PairKey(OctreeElementID p_A, OctreeElementID p_B) {
  74. if (p_A < p_B) {
  75. A = p_A;
  76. B = p_B;
  77. } else {
  78. B = p_A;
  79. A = p_B;
  80. }
  81. }
  82. _FORCE_INLINE_ PairKey() {}
  83. };
  84. struct Element;
  85. struct Octant {
  86. // cached for FAST plane check
  87. AABB aabb;
  88. uint64_t last_pass = 0;
  89. Octant *parent = nullptr;
  90. Octant *children[8] = { nullptr };
  91. int children_count = 0; // cache for amount of childrens (fast check for removal)
  92. int parent_index = -1; // cache for parent index (fast check for removal)
  93. List<Element *, AL> pairable_elements;
  94. List<Element *, AL> elements;
  95. Octant() {}
  96. ~Octant() {}
  97. };
  98. struct PairData;
  99. struct Element {
  100. Octree *octree = nullptr;
  101. T *userdata = nullptr;
  102. int subindex = 0;
  103. bool pairable = false;
  104. uint32_t pairable_mask = 0;
  105. uint32_t pairable_type = 0;
  106. uint64_t last_pass = 0;
  107. OctreeElementID _id = 0;
  108. Octant *common_parent = nullptr;
  109. AABB aabb;
  110. AABB container_aabb;
  111. List<PairData *, AL> pair_list;
  112. struct OctantOwner {
  113. Octant *octant;
  114. typename List<Element *, AL>::Element *E;
  115. }; // an element can be in max 8 octants
  116. List<OctantOwner, AL> octant_owners;
  117. Element() {}
  118. };
  119. struct PairData {
  120. int refcount;
  121. bool intersect;
  122. Element *A, *B;
  123. void *ud;
  124. typename List<PairData *, AL>::Element *eA, *eB;
  125. };
  126. typedef Map<OctreeElementID, Element, Comparator<OctreeElementID>, AL> ElementMap;
  127. typedef Map<PairKey, PairData, Comparator<PairKey>, AL> PairMap;
  128. ElementMap element_map;
  129. PairMap pair_map;
  130. PairCallback pair_callback;
  131. UnpairCallback unpair_callback;
  132. void *pair_callback_userdata;
  133. void *unpair_callback_userdata;
  134. OctreeElementID last_element_id;
  135. uint64_t pass;
  136. real_t unit_size;
  137. Octant *root;
  138. int octant_count;
  139. int pair_count;
  140. _FORCE_INLINE_ void _pair_check(PairData *p_pair) {
  141. bool intersect = p_pair->A->aabb.intersects_inclusive(p_pair->B->aabb);
  142. if (intersect != p_pair->intersect) {
  143. if (intersect) {
  144. if (pair_callback) {
  145. p_pair->ud = pair_callback(pair_callback_userdata, p_pair->A->_id, p_pair->A->userdata, p_pair->A->subindex, p_pair->B->_id, p_pair->B->userdata, p_pair->B->subindex);
  146. }
  147. pair_count++;
  148. } else {
  149. if (unpair_callback) {
  150. unpair_callback(pair_callback_userdata, p_pair->A->_id, p_pair->A->userdata, p_pair->A->subindex, p_pair->B->_id, p_pair->B->userdata, p_pair->B->subindex, p_pair->ud);
  151. }
  152. pair_count--;
  153. }
  154. p_pair->intersect = intersect;
  155. }
  156. }
  157. _FORCE_INLINE_ void _pair_reference(Element *p_A, Element *p_B) {
  158. if (p_A == p_B || (p_A->userdata == p_B->userdata && p_A->userdata)) {
  159. return;
  160. }
  161. if (!(p_A->pairable_type & p_B->pairable_mask) &&
  162. !(p_B->pairable_type & p_A->pairable_mask)) {
  163. return; // none can pair with none
  164. }
  165. PairKey key(p_A->_id, p_B->_id);
  166. typename PairMap::Element *E = pair_map.find(key);
  167. if (!E) {
  168. PairData pdata;
  169. pdata.refcount = 1;
  170. pdata.A = p_A;
  171. pdata.B = p_B;
  172. pdata.intersect = false;
  173. E = pair_map.insert(key, pdata);
  174. E->get().eA = p_A->pair_list.push_back(&E->get());
  175. E->get().eB = p_B->pair_list.push_back(&E->get());
  176. /*
  177. if (pair_callback)
  178. pair_callback(pair_callback_userdata,p_A->userdata,p_B->userdata);
  179. */
  180. } else {
  181. E->get().refcount++;
  182. }
  183. }
  184. _FORCE_INLINE_ void _pair_unreference(Element *p_A, Element *p_B) {
  185. if (p_A == p_B) {
  186. return;
  187. }
  188. PairKey key(p_A->_id, p_B->_id);
  189. typename PairMap::Element *E = pair_map.find(key);
  190. if (!E) {
  191. return; // no pair
  192. }
  193. E->get().refcount--;
  194. if (E->get().refcount == 0) {
  195. // bye pair
  196. if (E->get().intersect) {
  197. if (unpair_callback) {
  198. unpair_callback(pair_callback_userdata, p_A->_id, p_A->userdata, p_A->subindex, p_B->_id, p_B->userdata, p_B->subindex, E->get().ud);
  199. }
  200. pair_count--;
  201. }
  202. if (p_A == E->get().B) {
  203. //may be reaching inverted
  204. SWAP(p_A, p_B);
  205. }
  206. p_A->pair_list.erase(E->get().eA);
  207. p_B->pair_list.erase(E->get().eB);
  208. pair_map.erase(E);
  209. }
  210. }
  211. _FORCE_INLINE_ void _element_check_pairs(Element *p_element) {
  212. typename List<PairData *, AL>::Element *E = p_element->pair_list.front();
  213. while (E) {
  214. _pair_check(E->get());
  215. E = E->next();
  216. }
  217. }
  218. _FORCE_INLINE_ void _optimize() {
  219. while (root && root->children_count < 2 && !root->elements.size() && !(use_pairs && root->pairable_elements.size())) {
  220. Octant *new_root = nullptr;
  221. if (root->children_count == 1) {
  222. for (int i = 0; i < 8; i++) {
  223. if (root->children[i]) {
  224. new_root = root->children[i];
  225. root->children[i] = nullptr;
  226. break;
  227. }
  228. }
  229. ERR_FAIL_COND(!new_root);
  230. new_root->parent = nullptr;
  231. new_root->parent_index = -1;
  232. }
  233. memdelete_allocator<Octant, AL>(root);
  234. octant_count--;
  235. root = new_root;
  236. }
  237. }
  238. void _insert_element(Element *p_element, Octant *p_octant);
  239. void _ensure_valid_root(const AABB &p_aabb);
  240. bool _remove_element_from_octant(Element *p_element, Octant *p_octant, Octant *p_limit = nullptr);
  241. void _remove_element(Element *p_element);
  242. void _pair_element(Element *p_element, Octant *p_octant);
  243. void _unpair_element(Element *p_element, Octant *p_octant);
  244. struct _CullConvexData {
  245. const Plane *planes;
  246. int plane_count;
  247. const Vector3 *points;
  248. int point_count;
  249. T **result_array;
  250. int *result_idx;
  251. int result_max;
  252. uint32_t mask;
  253. };
  254. void _cull_convex(Octant *p_octant, _CullConvexData *p_cull);
  255. void _cull_aabb(Octant *p_octant, const AABB &p_aabb, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask);
  256. void _cull_segment(Octant *p_octant, const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask);
  257. void _cull_point(Octant *p_octant, const Vector3 &p_point, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask);
  258. void _remove_tree(Octant *p_octant) {
  259. if (!p_octant) {
  260. return;
  261. }
  262. for (int i = 0; i < 8; i++) {
  263. if (p_octant->children[i]) {
  264. _remove_tree(p_octant->children[i]);
  265. }
  266. }
  267. memdelete_allocator<Octant, AL>(p_octant);
  268. }
  269. public:
  270. OctreeElementID create(T *p_userdata, const AABB &p_aabb = AABB(), int p_subindex = 0, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1);
  271. void move(OctreeElementID p_id, const AABB &p_aabb);
  272. void set_pairable(OctreeElementID p_id, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1);
  273. void erase(OctreeElementID p_id);
  274. bool is_pairable(OctreeElementID p_id) const;
  275. T *get(OctreeElementID p_id) const;
  276. int get_subindex(OctreeElementID p_id) const;
  277. int cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
  278. int cull_aabb(const AABB &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  279. int cull_segment(const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  280. int cull_point(const Vector3 &p_point, T **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  281. void set_pair_callback(PairCallback p_callback, void *p_userdata);
  282. void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
  283. int get_octant_count() const { return octant_count; }
  284. int get_pair_count() const { return pair_count; }
  285. Octree(real_t p_unit_size = 1.0);
  286. ~Octree() { _remove_tree(root); }
  287. };
  288. /* PRIVATE FUNCTIONS */
  289. template <class T, bool use_pairs, class AL>
  290. T *Octree<T, use_pairs, AL>::get(OctreeElementID p_id) const {
  291. const typename ElementMap::Element *E = element_map.find(p_id);
  292. ERR_FAIL_COND_V(!E, nullptr);
  293. return E->get().userdata;
  294. }
  295. template <class T, bool use_pairs, class AL>
  296. bool Octree<T, use_pairs, AL>::is_pairable(OctreeElementID p_id) const {
  297. const typename ElementMap::Element *E = element_map.find(p_id);
  298. ERR_FAIL_COND_V(!E, false);
  299. return E->get().pairable;
  300. }
  301. template <class T, bool use_pairs, class AL>
  302. int Octree<T, use_pairs, AL>::get_subindex(OctreeElementID p_id) const {
  303. const typename ElementMap::Element *E = element_map.find(p_id);
  304. ERR_FAIL_COND_V(!E, -1);
  305. return E->get().subindex;
  306. }
  307. #define OCTREE_DIVISOR 4
  308. template <class T, bool use_pairs, class AL>
  309. void Octree<T, use_pairs, AL>::_insert_element(Element *p_element, Octant *p_octant) {
  310. real_t element_size = p_element->aabb.get_longest_axis_size() * 1.01; // avoid precision issues
  311. if (p_octant->aabb.size.x / OCTREE_DIVISOR < element_size) {
  312. //if (p_octant->aabb.size.x*0.5 < element_size) {
  313. /* at smallest possible size for the element */
  314. typename Element::OctantOwner owner;
  315. owner.octant = p_octant;
  316. if (use_pairs && p_element->pairable) {
  317. p_octant->pairable_elements.push_back(p_element);
  318. owner.E = p_octant->pairable_elements.back();
  319. } else {
  320. p_octant->elements.push_back(p_element);
  321. owner.E = p_octant->elements.back();
  322. }
  323. p_element->octant_owners.push_back(owner);
  324. if (p_element->common_parent == nullptr) {
  325. p_element->common_parent = p_octant;
  326. p_element->container_aabb = p_octant->aabb;
  327. } else {
  328. p_element->container_aabb.merge_with(p_octant->aabb);
  329. }
  330. if (use_pairs && p_octant->children_count > 0) {
  331. pass++; //elements below this only get ONE reference added
  332. for (int i = 0; i < 8; i++) {
  333. if (p_octant->children[i]) {
  334. _pair_element(p_element, p_octant->children[i]);
  335. }
  336. }
  337. }
  338. } else {
  339. /* not big enough, send it to subitems */
  340. int splits = 0;
  341. bool candidate = p_element->common_parent == nullptr;
  342. for (int i = 0; i < 8; i++) {
  343. if (p_octant->children[i]) {
  344. /* element exists, go straight to it */
  345. if (p_octant->children[i]->aabb.intersects_inclusive(p_element->aabb)) {
  346. _insert_element(p_element, p_octant->children[i]);
  347. splits++;
  348. }
  349. } else {
  350. /* check against AABB where child should be */
  351. AABB aabb = p_octant->aabb;
  352. aabb.size *= 0.5;
  353. if (i & 1) {
  354. aabb.position.x += aabb.size.x;
  355. }
  356. if (i & 2) {
  357. aabb.position.y += aabb.size.y;
  358. }
  359. if (i & 4) {
  360. aabb.position.z += aabb.size.z;
  361. }
  362. if (aabb.intersects_inclusive(p_element->aabb)) {
  363. /* if actually intersects, create the child */
  364. Octant *child = memnew_allocator(Octant, AL);
  365. p_octant->children[i] = child;
  366. child->parent = p_octant;
  367. child->parent_index = i;
  368. child->aabb = aabb;
  369. p_octant->children_count++;
  370. _insert_element(p_element, child);
  371. octant_count++;
  372. splits++;
  373. }
  374. }
  375. }
  376. if (candidate && splits > 1) {
  377. p_element->common_parent = p_octant;
  378. }
  379. }
  380. if (use_pairs) {
  381. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  382. while (E) {
  383. _pair_reference(p_element, E->get());
  384. E = E->next();
  385. }
  386. if (p_element->pairable) {
  387. // and always test non-pairable if element is pairable
  388. E = p_octant->elements.front();
  389. while (E) {
  390. _pair_reference(p_element, E->get());
  391. E = E->next();
  392. }
  393. }
  394. }
  395. }
  396. template <class T, bool use_pairs, class AL>
  397. void Octree<T, use_pairs, AL>::_ensure_valid_root(const AABB &p_aabb) {
  398. if (!root) {
  399. // octre is empty
  400. AABB base(Vector3(), Vector3(1.0, 1.0, 1.0) * unit_size);
  401. while (!base.encloses(p_aabb)) {
  402. if (ABS(base.position.x + base.size.x) <= ABS(base.position.x)) {
  403. /* grow towards positive */
  404. base.size *= 2.0;
  405. } else {
  406. base.position -= base.size;
  407. base.size *= 2.0;
  408. }
  409. }
  410. root = memnew_allocator(Octant, AL);
  411. root->parent = nullptr;
  412. root->parent_index = -1;
  413. root->aabb = base;
  414. octant_count++;
  415. } else {
  416. AABB base = root->aabb;
  417. while (!base.encloses(p_aabb)) {
  418. ERR_FAIL_COND_MSG(base.size.x > OCTREE_SIZE_LIMIT, "Octree upper size limit reached, does the AABB supplied contain NAN?");
  419. Octant *gp = memnew_allocator(Octant, AL);
  420. octant_count++;
  421. root->parent = gp;
  422. if (ABS(base.position.x + base.size.x) <= ABS(base.position.x)) {
  423. /* grow towards positive */
  424. base.size *= 2.0;
  425. gp->aabb = base;
  426. gp->children[0] = root;
  427. root->parent_index = 0;
  428. } else {
  429. base.position -= base.size;
  430. base.size *= 2.0;
  431. gp->aabb = base;
  432. gp->children[(1 << 0) | (1 << 1) | (1 << 2)] = root; // add at all-positive
  433. root->parent_index = (1 << 0) | (1 << 1) | (1 << 2);
  434. }
  435. gp->children_count = 1;
  436. root = gp;
  437. }
  438. }
  439. }
  440. template <class T, bool use_pairs, class AL>
  441. bool Octree<T, use_pairs, AL>::_remove_element_from_octant(Element *p_element, Octant *p_octant, Octant *p_limit) {
  442. bool octant_removed = false;
  443. while (true) {
  444. // check all exit conditions
  445. if (p_octant == p_limit) { // reached limit, nothing to erase, exit
  446. return octant_removed;
  447. }
  448. bool unpaired = false;
  449. if (use_pairs && p_octant->last_pass != pass) {
  450. // check whether we should unpair stuff
  451. // always test pairable
  452. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  453. while (E) {
  454. _pair_unreference(p_element, E->get());
  455. E = E->next();
  456. }
  457. if (p_element->pairable) {
  458. // and always test non-pairable if element is pairable
  459. E = p_octant->elements.front();
  460. while (E) {
  461. _pair_unreference(p_element, E->get());
  462. E = E->next();
  463. }
  464. }
  465. p_octant->last_pass = pass;
  466. unpaired = true;
  467. }
  468. bool removed = false;
  469. Octant *parent = p_octant->parent;
  470. if (p_octant->children_count == 0 && p_octant->elements.empty() && p_octant->pairable_elements.empty()) {
  471. // erase octant
  472. if (p_octant == root) { // won't have a parent, just erase
  473. root = nullptr;
  474. } else {
  475. ERR_FAIL_INDEX_V(p_octant->parent_index, 8, octant_removed);
  476. parent->children[p_octant->parent_index] = nullptr;
  477. parent->children_count--;
  478. }
  479. memdelete_allocator<Octant, AL>(p_octant);
  480. octant_count--;
  481. removed = true;
  482. octant_removed = true;
  483. }
  484. if (!removed && !unpaired) {
  485. return octant_removed; // no reason to keep going up anymore! was already visited and was not removed
  486. }
  487. p_octant = parent;
  488. }
  489. return octant_removed;
  490. }
  491. template <class T, bool use_pairs, class AL>
  492. void Octree<T, use_pairs, AL>::_unpair_element(Element *p_element, Octant *p_octant) {
  493. // always test pairable
  494. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  495. while (E) {
  496. if (E->get()->last_pass != pass) { // only remove ONE reference
  497. _pair_unreference(p_element, E->get());
  498. E->get()->last_pass = pass;
  499. }
  500. E = E->next();
  501. }
  502. if (p_element->pairable) {
  503. // and always test non-pairable if element is pairable
  504. E = p_octant->elements.front();
  505. while (E) {
  506. if (E->get()->last_pass != pass) { // only remove ONE reference
  507. _pair_unreference(p_element, E->get());
  508. E->get()->last_pass = pass;
  509. }
  510. E = E->next();
  511. }
  512. }
  513. p_octant->last_pass = pass;
  514. if (p_octant->children_count == 0) {
  515. return; // small optimization for leafs
  516. }
  517. for (int i = 0; i < 8; i++) {
  518. if (p_octant->children[i]) {
  519. _unpair_element(p_element, p_octant->children[i]);
  520. }
  521. }
  522. }
  523. template <class T, bool use_pairs, class AL>
  524. void Octree<T, use_pairs, AL>::_pair_element(Element *p_element, Octant *p_octant) {
  525. // always test pairable
  526. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  527. while (E) {
  528. if (E->get()->last_pass != pass) { // only get ONE reference
  529. _pair_reference(p_element, E->get());
  530. E->get()->last_pass = pass;
  531. }
  532. E = E->next();
  533. }
  534. if (p_element->pairable) {
  535. // and always test non-pairable if element is pairable
  536. E = p_octant->elements.front();
  537. while (E) {
  538. if (E->get()->last_pass != pass) { // only get ONE reference
  539. _pair_reference(p_element, E->get());
  540. E->get()->last_pass = pass;
  541. }
  542. E = E->next();
  543. }
  544. }
  545. p_octant->last_pass = pass;
  546. if (p_octant->children_count == 0) {
  547. return; // small optimization for leafs
  548. }
  549. for (int i = 0; i < 8; i++) {
  550. if (p_octant->children[i]) {
  551. _pair_element(p_element, p_octant->children[i]);
  552. }
  553. }
  554. }
  555. template <class T, bool use_pairs, class AL>
  556. void Octree<T, use_pairs, AL>::_remove_element(Element *p_element) {
  557. pass++; // will do a new pass for this
  558. typename List<typename Element::OctantOwner, AL>::Element *I = p_element->octant_owners.front();
  559. /* FIRST remove going up normally */
  560. for (; I; I = I->next()) {
  561. Octant *o = I->get().octant;
  562. if (!use_pairs) { // small speedup
  563. o->elements.erase(I->get().E);
  564. }
  565. _remove_element_from_octant(p_element, o);
  566. }
  567. /* THEN remove going down */
  568. I = p_element->octant_owners.front();
  569. if (use_pairs) {
  570. for (; I; I = I->next()) {
  571. Octant *o = I->get().octant;
  572. // erase children pairs, they are erased ONCE even if repeated
  573. pass++;
  574. for (int i = 0; i < 8; i++) {
  575. if (o->children[i]) {
  576. _unpair_element(p_element, o->children[i]);
  577. }
  578. }
  579. if (p_element->pairable) {
  580. o->pairable_elements.erase(I->get().E);
  581. } else {
  582. o->elements.erase(I->get().E);
  583. }
  584. }
  585. }
  586. p_element->octant_owners.clear();
  587. if (use_pairs) {
  588. int remaining = p_element->pair_list.size();
  589. //p_element->pair_list.clear();
  590. ERR_FAIL_COND(remaining);
  591. }
  592. }
  593. template <class T, bool use_pairs, class AL>
  594. OctreeElementID Octree<T, use_pairs, AL>::create(T *p_userdata, const AABB &p_aabb, int p_subindex, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) {
  595. // check for AABB validity
  596. #ifdef DEBUG_ENABLED
  597. ERR_FAIL_COND_V(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15, 0);
  598. ERR_FAIL_COND_V(p_aabb.position.y > 1e15 || p_aabb.position.y < -1e15, 0);
  599. ERR_FAIL_COND_V(p_aabb.position.z > 1e15 || p_aabb.position.z < -1e15, 0);
  600. ERR_FAIL_COND_V(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0, 0);
  601. ERR_FAIL_COND_V(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0, 0);
  602. ERR_FAIL_COND_V(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0, 0);
  603. ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.x), 0);
  604. ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.y), 0);
  605. ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.z), 0);
  606. #endif
  607. typename ElementMap::Element *E = element_map.insert(last_element_id++,
  608. Element());
  609. Element &e = E->get();
  610. e.aabb = p_aabb;
  611. e.userdata = p_userdata;
  612. e.subindex = p_subindex;
  613. e.last_pass = 0;
  614. e.octree = this;
  615. e.pairable = p_pairable;
  616. e.pairable_type = p_pairable_type;
  617. e.pairable_mask = p_pairable_mask;
  618. e._id = last_element_id - 1;
  619. if (!e.aabb.has_no_surface()) {
  620. _ensure_valid_root(p_aabb);
  621. _insert_element(&e, root);
  622. if (use_pairs) {
  623. _element_check_pairs(&e);
  624. }
  625. }
  626. return last_element_id - 1;
  627. }
  628. template <class T, bool use_pairs, class AL>
  629. void Octree<T, use_pairs, AL>::move(OctreeElementID p_id, const AABB &p_aabb) {
  630. #ifdef DEBUG_ENABLED
  631. // check for AABB validity
  632. ERR_FAIL_COND(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15);
  633. ERR_FAIL_COND(p_aabb.position.y > 1e15 || p_aabb.position.y < -1e15);
  634. ERR_FAIL_COND(p_aabb.position.z > 1e15 || p_aabb.position.z < -1e15);
  635. ERR_FAIL_COND(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0);
  636. ERR_FAIL_COND(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0);
  637. ERR_FAIL_COND(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0);
  638. ERR_FAIL_COND(Math::is_nan(p_aabb.size.x));
  639. ERR_FAIL_COND(Math::is_nan(p_aabb.size.y));
  640. ERR_FAIL_COND(Math::is_nan(p_aabb.size.z));
  641. #endif
  642. typename ElementMap::Element *E = element_map.find(p_id);
  643. ERR_FAIL_COND(!E);
  644. Element &e = E->get();
  645. bool old_has_surf = !e.aabb.has_no_surface();
  646. bool new_has_surf = !p_aabb.has_no_surface();
  647. if (old_has_surf != new_has_surf) {
  648. if (old_has_surf) {
  649. _remove_element(&e); // removing
  650. e.common_parent = nullptr;
  651. e.aabb = AABB();
  652. _optimize();
  653. } else {
  654. _ensure_valid_root(p_aabb); // inserting
  655. e.common_parent = nullptr;
  656. e.aabb = p_aabb;
  657. _insert_element(&e, root);
  658. if (use_pairs) {
  659. _element_check_pairs(&e);
  660. }
  661. }
  662. return;
  663. }
  664. if (!old_has_surf) { // doing nothing
  665. return;
  666. }
  667. // it still is enclosed in the same AABB it was assigned to
  668. if (e.container_aabb.encloses(p_aabb)) {
  669. e.aabb = p_aabb;
  670. if (use_pairs) {
  671. _element_check_pairs(&e); // must check pairs anyway
  672. }
  673. return;
  674. }
  675. AABB combined = e.aabb;
  676. combined.merge_with(p_aabb);
  677. _ensure_valid_root(combined);
  678. ERR_FAIL_COND(e.octant_owners.front() == nullptr);
  679. /* FIND COMMON PARENT */
  680. List<typename Element::OctantOwner, AL> owners = e.octant_owners; // save the octant owners
  681. Octant *common_parent = e.common_parent;
  682. ERR_FAIL_COND(!common_parent);
  683. //src is now the place towards where insertion is going to happen
  684. pass++;
  685. while (common_parent && !common_parent->aabb.encloses(p_aabb)) {
  686. common_parent = common_parent->parent;
  687. }
  688. ERR_FAIL_COND(!common_parent);
  689. //prepare for reinsert
  690. e.octant_owners.clear();
  691. e.common_parent = nullptr;
  692. e.aabb = p_aabb;
  693. _insert_element(&e, common_parent); // reinsert from this point
  694. pass++;
  695. for (typename List<typename Element::OctantOwner, AL>::Element *F = owners.front(); F;) {
  696. Octant *o = F->get().octant;
  697. typename List<typename Element::OctantOwner, AL>::Element *N = F->next();
  698. /*
  699. if (!use_pairs)
  700. o->elements.erase( F->get().E );
  701. */
  702. if (use_pairs && e.pairable) {
  703. o->pairable_elements.erase(F->get().E);
  704. } else {
  705. o->elements.erase(F->get().E);
  706. }
  707. if (_remove_element_from_octant(&e, o, common_parent->parent)) {
  708. owners.erase(F);
  709. }
  710. F = N;
  711. }
  712. if (use_pairs) {
  713. //unpair child elements in anything that survived
  714. for (typename List<typename Element::OctantOwner, AL>::Element *F = owners.front(); F; F = F->next()) {
  715. Octant *o = F->get().octant;
  716. // erase children pairs, unref ONCE
  717. pass++;
  718. for (int i = 0; i < 8; i++) {
  719. if (o->children[i]) {
  720. _unpair_element(&e, o->children[i]);
  721. }
  722. }
  723. }
  724. _element_check_pairs(&e);
  725. }
  726. _optimize();
  727. }
  728. template <class T, bool use_pairs, class AL>
  729. void Octree<T, use_pairs, AL>::set_pairable(OctreeElementID p_id, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) {
  730. typename ElementMap::Element *E = element_map.find(p_id);
  731. ERR_FAIL_COND(!E);
  732. Element &e = E->get();
  733. if (p_pairable == e.pairable && e.pairable_type == p_pairable_type && e.pairable_mask == p_pairable_mask) {
  734. return; // no changes, return
  735. }
  736. if (!e.aabb.has_no_surface()) {
  737. _remove_element(&e);
  738. }
  739. e.pairable = p_pairable;
  740. e.pairable_type = p_pairable_type;
  741. e.pairable_mask = p_pairable_mask;
  742. e.common_parent = nullptr;
  743. if (!e.aabb.has_no_surface()) {
  744. _ensure_valid_root(e.aabb);
  745. _insert_element(&e, root);
  746. if (use_pairs) {
  747. _element_check_pairs(&e);
  748. }
  749. }
  750. }
  751. template <class T, bool use_pairs, class AL>
  752. void Octree<T, use_pairs, AL>::erase(OctreeElementID p_id) {
  753. typename ElementMap::Element *E = element_map.find(p_id);
  754. ERR_FAIL_COND(!E);
  755. Element &e = E->get();
  756. if (!e.aabb.has_no_surface()) {
  757. _remove_element(&e);
  758. }
  759. element_map.erase(p_id);
  760. _optimize();
  761. }
  762. template <class T, bool use_pairs, class AL>
  763. void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p_cull) {
  764. if (*p_cull->result_idx == p_cull->result_max) {
  765. return; //pointless
  766. }
  767. if (!p_octant->elements.empty()) {
  768. typename List<Element *, AL>::Element *I;
  769. I = p_octant->elements.front();
  770. for (; I; I = I->next()) {
  771. Element *e = I->get();
  772. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask))) {
  773. continue;
  774. }
  775. e->last_pass = pass;
  776. if (e->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  777. if (*p_cull->result_idx < p_cull->result_max) {
  778. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  779. (*p_cull->result_idx)++;
  780. } else {
  781. return; // pointless to continue
  782. }
  783. }
  784. }
  785. }
  786. if (use_pairs && !p_octant->pairable_elements.empty()) {
  787. typename List<Element *, AL>::Element *I;
  788. I = p_octant->pairable_elements.front();
  789. for (; I; I = I->next()) {
  790. Element *e = I->get();
  791. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask))) {
  792. continue;
  793. }
  794. e->last_pass = pass;
  795. if (e->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  796. if (*p_cull->result_idx < p_cull->result_max) {
  797. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  798. (*p_cull->result_idx)++;
  799. } else {
  800. return; // pointless to continue
  801. }
  802. }
  803. }
  804. }
  805. for (int i = 0; i < 8; i++) {
  806. if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  807. _cull_convex(p_octant->children[i], p_cull);
  808. }
  809. }
  810. }
  811. template <class T, bool use_pairs, class AL>
  812. void Octree<T, use_pairs, AL>::_cull_aabb(Octant *p_octant, const AABB &p_aabb, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
  813. if (*p_result_idx == p_result_max) {
  814. return; //pointless
  815. }
  816. if (!p_octant->elements.empty()) {
  817. typename List<Element *, AL>::Element *I;
  818. I = p_octant->elements.front();
  819. for (; I; I = I->next()) {
  820. Element *e = I->get();
  821. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  822. continue;
  823. }
  824. e->last_pass = pass;
  825. if (p_aabb.intersects_inclusive(e->aabb)) {
  826. if (*p_result_idx < p_result_max) {
  827. p_result_array[*p_result_idx] = e->userdata;
  828. if (p_subindex_array) {
  829. p_subindex_array[*p_result_idx] = e->subindex;
  830. }
  831. (*p_result_idx)++;
  832. } else {
  833. return; // pointless to continue
  834. }
  835. }
  836. }
  837. }
  838. if (use_pairs && !p_octant->pairable_elements.empty()) {
  839. typename List<Element *, AL>::Element *I;
  840. I = p_octant->pairable_elements.front();
  841. for (; I; I = I->next()) {
  842. Element *e = I->get();
  843. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  844. continue;
  845. }
  846. e->last_pass = pass;
  847. if (p_aabb.intersects_inclusive(e->aabb)) {
  848. if (*p_result_idx < p_result_max) {
  849. p_result_array[*p_result_idx] = e->userdata;
  850. if (p_subindex_array) {
  851. p_subindex_array[*p_result_idx] = e->subindex;
  852. }
  853. (*p_result_idx)++;
  854. } else {
  855. return; // pointless to continue
  856. }
  857. }
  858. }
  859. }
  860. for (int i = 0; i < 8; i++) {
  861. if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_inclusive(p_aabb)) {
  862. _cull_aabb(p_octant->children[i], p_aabb, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
  863. }
  864. }
  865. }
  866. template <class T, bool use_pairs, class AL>
  867. void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
  868. if (*p_result_idx == p_result_max) {
  869. return; //pointless
  870. }
  871. if (!p_octant->elements.empty()) {
  872. typename List<Element *, AL>::Element *I;
  873. I = p_octant->elements.front();
  874. for (; I; I = I->next()) {
  875. Element *e = I->get();
  876. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  877. continue;
  878. }
  879. e->last_pass = pass;
  880. if (e->aabb.intersects_segment(p_from, p_to)) {
  881. if (*p_result_idx < p_result_max) {
  882. p_result_array[*p_result_idx] = e->userdata;
  883. if (p_subindex_array) {
  884. p_subindex_array[*p_result_idx] = e->subindex;
  885. }
  886. (*p_result_idx)++;
  887. } else {
  888. return; // pointless to continue
  889. }
  890. }
  891. }
  892. }
  893. if (use_pairs && !p_octant->pairable_elements.empty()) {
  894. typename List<Element *, AL>::Element *I;
  895. I = p_octant->pairable_elements.front();
  896. for (; I; I = I->next()) {
  897. Element *e = I->get();
  898. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  899. continue;
  900. }
  901. e->last_pass = pass;
  902. if (e->aabb.intersects_segment(p_from, p_to)) {
  903. if (*p_result_idx < p_result_max) {
  904. p_result_array[*p_result_idx] = e->userdata;
  905. if (p_subindex_array) {
  906. p_subindex_array[*p_result_idx] = e->subindex;
  907. }
  908. (*p_result_idx)++;
  909. } else {
  910. return; // pointless to continue
  911. }
  912. }
  913. }
  914. }
  915. for (int i = 0; i < 8; i++) {
  916. if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_segment(p_from, p_to)) {
  917. _cull_segment(p_octant->children[i], p_from, p_to, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
  918. }
  919. }
  920. }
  921. template <class T, bool use_pairs, class AL>
  922. void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_point, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
  923. if (*p_result_idx == p_result_max) {
  924. return; //pointless
  925. }
  926. if (!p_octant->elements.empty()) {
  927. typename List<Element *, AL>::Element *I;
  928. I = p_octant->elements.front();
  929. for (; I; I = I->next()) {
  930. Element *e = I->get();
  931. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  932. continue;
  933. }
  934. e->last_pass = pass;
  935. if (e->aabb.has_point(p_point)) {
  936. if (*p_result_idx < p_result_max) {
  937. p_result_array[*p_result_idx] = e->userdata;
  938. if (p_subindex_array) {
  939. p_subindex_array[*p_result_idx] = e->subindex;
  940. }
  941. (*p_result_idx)++;
  942. } else {
  943. return; // pointless to continue
  944. }
  945. }
  946. }
  947. }
  948. if (use_pairs && !p_octant->pairable_elements.empty()) {
  949. typename List<Element *, AL>::Element *I;
  950. I = p_octant->pairable_elements.front();
  951. for (; I; I = I->next()) {
  952. Element *e = I->get();
  953. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  954. continue;
  955. }
  956. e->last_pass = pass;
  957. if (e->aabb.has_point(p_point)) {
  958. if (*p_result_idx < p_result_max) {
  959. p_result_array[*p_result_idx] = e->userdata;
  960. if (p_subindex_array) {
  961. p_subindex_array[*p_result_idx] = e->subindex;
  962. }
  963. (*p_result_idx)++;
  964. } else {
  965. return; // pointless to continue
  966. }
  967. }
  968. }
  969. }
  970. for (int i = 0; i < 8; i++) {
  971. //could be optimized..
  972. if (p_octant->children[i] && p_octant->children[i]->aabb.has_point(p_point)) {
  973. _cull_point(p_octant->children[i], p_point, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
  974. }
  975. }
  976. }
  977. template <class T, bool use_pairs, class AL>
  978. int Octree<T, use_pairs, AL>::cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask) {
  979. if (!root || p_convex.size() == 0) {
  980. return 0;
  981. }
  982. Vector<Vector3> convex_points = Geometry::compute_convex_mesh_points(&p_convex[0], p_convex.size());
  983. if (convex_points.size() == 0) {
  984. return 0;
  985. }
  986. int result_count = 0;
  987. pass++;
  988. _CullConvexData cdata;
  989. cdata.planes = &p_convex[0];
  990. cdata.plane_count = p_convex.size();
  991. cdata.points = &convex_points[0];
  992. cdata.point_count = convex_points.size();
  993. cdata.result_array = p_result_array;
  994. cdata.result_max = p_result_max;
  995. cdata.result_idx = &result_count;
  996. cdata.mask = p_mask;
  997. _cull_convex(root, &cdata);
  998. return result_count;
  999. }
  1000. template <class T, bool use_pairs, class AL>
  1001. int Octree<T, use_pairs, AL>::cull_aabb(const AABB &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
  1002. if (!root) {
  1003. return 0;
  1004. }
  1005. int result_count = 0;
  1006. pass++;
  1007. _cull_aabb(root, p_aabb, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
  1008. return result_count;
  1009. }
  1010. template <class T, bool use_pairs, class AL>
  1011. int Octree<T, use_pairs, AL>::cull_segment(const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
  1012. if (!root) {
  1013. return 0;
  1014. }
  1015. int result_count = 0;
  1016. pass++;
  1017. _cull_segment(root, p_from, p_to, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
  1018. return result_count;
  1019. }
  1020. template <class T, bool use_pairs, class AL>
  1021. int Octree<T, use_pairs, AL>::cull_point(const Vector3 &p_point, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
  1022. if (!root) {
  1023. return 0;
  1024. }
  1025. int result_count = 0;
  1026. pass++;
  1027. _cull_point(root, p_point, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
  1028. return result_count;
  1029. }
  1030. template <class T, bool use_pairs, class AL>
  1031. void Octree<T, use_pairs, AL>::set_pair_callback(PairCallback p_callback, void *p_userdata) {
  1032. pair_callback = p_callback;
  1033. pair_callback_userdata = p_userdata;
  1034. }
  1035. template <class T, bool use_pairs, class AL>
  1036. void Octree<T, use_pairs, AL>::set_unpair_callback(UnpairCallback p_callback, void *p_userdata) {
  1037. unpair_callback = p_callback;
  1038. unpair_callback_userdata = p_userdata;
  1039. }
  1040. template <class T, bool use_pairs, class AL>
  1041. Octree<T, use_pairs, AL>::Octree(real_t p_unit_size) {
  1042. last_element_id = 1;
  1043. pass = 1;
  1044. unit_size = p_unit_size;
  1045. root = nullptr;
  1046. octant_count = 0;
  1047. pair_count = 0;
  1048. pair_callback = nullptr;
  1049. unpair_callback = nullptr;
  1050. pair_callback_userdata = nullptr;
  1051. unpair_callback_userdata = nullptr;
  1052. }
  1053. #endif // OCTREE_H