octree.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /*************************************************************************/
  2. /* octree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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