octree.h 35 KB

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