octree.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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 <reduzio@gmail.com>
  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