octree.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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