octree.h 35 KB

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