octree.h 34 KB

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