octree.h 35 KB

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