octree_definition.inc 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  1. // DO NOT ADD INCLUDE GUARDS OR PRAGMA ONCE.
  2. // This file will be included more than once.
  3. /*************************************************************************/
  4. /* octree_definition.inc */
  5. /*************************************************************************/
  6. /* This file is part of: */
  7. /* GODOT ENGINE */
  8. /* https://godotengine.org */
  9. /*************************************************************************/
  10. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  11. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  12. /* */
  13. /* Permission is hereby granted, free of charge, to any person obtaining */
  14. /* a copy of this software and associated documentation files (the */
  15. /* "Software"), to deal in the Software without restriction, including */
  16. /* without limitation the rights to use, copy, modify, merge, publish, */
  17. /* distribute, sublicense, and/or sell copies of the Software, and to */
  18. /* permit persons to whom the Software is furnished to do so, subject to */
  19. /* the following conditions: */
  20. /* */
  21. /* The above copyright notice and this permission notice shall be */
  22. /* included in all copies or substantial portions of the Software. */
  23. /* */
  24. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  25. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  26. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  27. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  28. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  29. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  30. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  31. /*************************************************************************/
  32. #include "core/list.h"
  33. #include "core/local_vector.h"
  34. #include "core/map.h"
  35. #include "core/math/aabb.h"
  36. #include "core/math/geometry.h"
  37. #include "core/math/vector3.h"
  38. #include "core/os/os.h"
  39. #include "core/print_string.h"
  40. #include "core/variant.h"
  41. typedef uint32_t OctreeElementID;
  42. // macro to reduce boiler plate code when providing function implementations
  43. #define OCTREE_FUNC(RET_VALUE) template <class T, bool use_pairs, class AL> \
  44. RET_VALUE OCTREE_CLASS_NAME<T, use_pairs, AL>
  45. #define OCTREE_FUNC_CONSTRUCTOR template <class T, bool use_pairs, class AL> \
  46. OCTREE_CLASS_NAME<T, use_pairs, AL>
  47. template <class T, bool use_pairs = false, class AL = DefaultAllocator>
  48. class OCTREE_CLASS_NAME {
  49. public:
  50. typedef void *(*PairCallback)(void *, OctreeElementID, T *, int, OctreeElementID, T *, int);
  51. typedef void (*UnpairCallback)(void *, OctreeElementID, T *, int, OctreeElementID, T *, int, void *);
  52. private:
  53. enum {
  54. NEG = 0,
  55. POS = 1,
  56. };
  57. enum {
  58. OCTANT_NX_NY_NZ,
  59. OCTANT_PX_NY_NZ,
  60. OCTANT_NX_PY_NZ,
  61. OCTANT_PX_PY_NZ,
  62. OCTANT_NX_NY_PZ,
  63. OCTANT_PX_NY_PZ,
  64. OCTANT_NX_PY_PZ,
  65. OCTANT_PX_PY_PZ
  66. };
  67. struct PairKey {
  68. union {
  69. struct {
  70. OctreeElementID A;
  71. OctreeElementID B;
  72. };
  73. uint64_t key;
  74. };
  75. _FORCE_INLINE_ bool operator<(const PairKey &p_pair) const {
  76. return key < p_pair.key;
  77. }
  78. _FORCE_INLINE_ PairKey(OctreeElementID p_A, OctreeElementID p_B) {
  79. if (p_A < p_B) {
  80. A = p_A;
  81. B = p_B;
  82. } else {
  83. B = p_A;
  84. A = p_B;
  85. }
  86. }
  87. _FORCE_INLINE_ PairKey() {}
  88. };
  89. struct Element;
  90. #ifdef OCTREE_USE_CACHED_LISTS
  91. // instead of iterating the linked list every time within octants,
  92. // we can cache a linear list of prepared elements containing essential data
  93. // for fast traversal, and rebuild it only when an octant changes.
  94. struct CachedList {
  95. LocalVector<AABB> aabbs;
  96. LocalVector<Element *> elements;
  97. void update(List<Element *, AL> &p_elements) {
  98. // make sure local vector doesn't delete the memory
  99. // no need to be thrashing allocations
  100. aabbs.clear();
  101. elements.clear();
  102. typename List<Element *, AL>::Element *E = p_elements.front();
  103. while (E) {
  104. Element *e = E->get();
  105. aabbs.push_back(e->aabb);
  106. elements.push_back(e);
  107. E = E->next();
  108. }
  109. }
  110. };
  111. #endif
  112. struct Octant {
  113. // cached for FAST plane check
  114. AABB aabb;
  115. uint64_t last_pass;
  116. Octant *parent;
  117. Octant *children[8];
  118. int children_count; // cache for amount of childrens (fast check for removal)
  119. int parent_index; // cache for parent index (fast check for removal)
  120. List<Element *, AL> pairable_elements;
  121. List<Element *, AL> elements;
  122. #ifdef OCTREE_USE_CACHED_LISTS
  123. // cached lists are linear in memory so are faster than using linked list
  124. CachedList clist_pairable;
  125. CachedList clist;
  126. // use dirty flag to indicate when cached lists need updating
  127. // this avoids having to update the cached list on lots of octants
  128. // if nothing is moving in them.
  129. bool dirty;
  130. void update_cached_lists() {
  131. if (!dirty) {
  132. #ifdef TOOLS_ENABLED
  133. //#define OCTREE_CACHED_LIST_ERROR_CHECKS
  134. #endif
  135. #ifdef OCTREE_CACHED_LIST_ERROR_CHECKS
  136. // debug - this will slow down performance a lot,
  137. // only enable these error checks for testing that the cached
  138. // lists are up to date.
  139. int hash_before_P = clist_pairable.aabbs.size();
  140. int hash_before_N = clist.aabbs.size();
  141. clist_pairable.update(pairable_elements);
  142. clist.update(elements);
  143. int hash_after_P = clist_pairable.aabbs.size();
  144. int hash_after_N = clist.aabbs.size();
  145. ERR_FAIL_COND(hash_before_P != hash_after_P);
  146. ERR_FAIL_COND(hash_before_N != hash_after_N);
  147. #endif
  148. return;
  149. }
  150. clist_pairable.update(pairable_elements);
  151. clist.update(elements);
  152. dirty = false;
  153. }
  154. #endif
  155. Octant() {
  156. children_count = 0;
  157. parent_index = -1;
  158. last_pass = 0;
  159. parent = nullptr;
  160. #ifdef OCTREE_USE_CACHED_LISTS
  161. dirty = true;
  162. #endif
  163. for (int i = 0; i < 8; i++) {
  164. children[i] = nullptr;
  165. }
  166. }
  167. ~Octant() {
  168. /*
  169. for (int i=0;i<8;i++)
  170. memdelete_notnull(children[i]);
  171. */
  172. }
  173. };
  174. struct PairData;
  175. struct Element {
  176. OCTREE_CLASS_NAME *octree;
  177. T *userdata;
  178. int subindex;
  179. bool pairable;
  180. uint32_t pairable_mask;
  181. uint32_t pairable_type;
  182. uint64_t last_pass;
  183. OctreeElementID _id;
  184. Octant *common_parent;
  185. AABB aabb;
  186. AABB container_aabb;
  187. List<PairData *, AL> pair_list;
  188. struct OctantOwner {
  189. Octant *octant;
  190. typename List<Element *, AL>::Element *E;
  191. }; // an element can be in max 8 octants
  192. List<OctantOwner, AL> octant_owners;
  193. #ifdef OCTREE_USE_CACHED_LISTS
  194. // when moving we need make all owner octants dirty, because the AABB can change.
  195. void moving() {
  196. for (typename List<typename Element::OctantOwner, AL>::Element *F = octant_owners.front(); F;) {
  197. Octant *o = F->get().octant;
  198. o->dirty = true;
  199. F = F->next();
  200. }
  201. }
  202. #endif
  203. Element() {
  204. last_pass = 0;
  205. _id = 0;
  206. pairable = false;
  207. subindex = 0;
  208. userdata = nullptr;
  209. octree = nullptr;
  210. pairable_mask = 0;
  211. pairable_type = 0;
  212. common_parent = nullptr;
  213. }
  214. };
  215. struct PairData {
  216. int refcount;
  217. bool intersect;
  218. Element *A, *B;
  219. void *ud;
  220. typename List<PairData *, AL>::Element *eA, *eB;
  221. };
  222. typedef Map<OctreeElementID, Element, Comparator<OctreeElementID>, AL> ElementMap;
  223. typedef Map<PairKey, PairData, Comparator<PairKey>, AL> PairMap;
  224. ElementMap element_map;
  225. PairMap pair_map;
  226. PairCallback pair_callback;
  227. UnpairCallback unpair_callback;
  228. void *pair_callback_userdata;
  229. void *unpair_callback_userdata;
  230. OctreeElementID last_element_id;
  231. uint64_t pass;
  232. real_t unit_size;
  233. Octant *root;
  234. int octant_count;
  235. int pair_count;
  236. int octant_elements_limit;
  237. _FORCE_INLINE_ void _pair_check(PairData *p_pair) {
  238. bool intersect = p_pair->A->aabb.intersects_inclusive(p_pair->B->aabb);
  239. if (intersect != p_pair->intersect) {
  240. if (intersect) {
  241. if (pair_callback) {
  242. 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);
  243. }
  244. pair_count++;
  245. } else {
  246. if (unpair_callback) {
  247. 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);
  248. }
  249. pair_count--;
  250. }
  251. p_pair->intersect = intersect;
  252. }
  253. }
  254. _FORCE_INLINE_ void _pair_reference(Element *p_A, Element *p_B) {
  255. if (p_A == p_B || (p_A->userdata == p_B->userdata && p_A->userdata)) {
  256. return;
  257. }
  258. if (!(p_A->pairable_type & p_B->pairable_mask) &&
  259. !(p_B->pairable_type & p_A->pairable_mask)) {
  260. return; // none can pair with none
  261. }
  262. PairKey key(p_A->_id, p_B->_id);
  263. typename PairMap::Element *E = pair_map.find(key);
  264. if (!E) {
  265. PairData pdata;
  266. pdata.refcount = 1;
  267. pdata.A = p_A;
  268. pdata.B = p_B;
  269. pdata.intersect = false;
  270. E = pair_map.insert(key, pdata);
  271. E->get().eA = p_A->pair_list.push_back(&E->get());
  272. E->get().eB = p_B->pair_list.push_back(&E->get());
  273. /*
  274. if (pair_callback)
  275. pair_callback(pair_callback_userdata,p_A->userdata,p_B->userdata);
  276. */
  277. } else {
  278. E->get().refcount++;
  279. }
  280. }
  281. _FORCE_INLINE_ void _pair_unreference(Element *p_A, Element *p_B) {
  282. if (p_A == p_B) {
  283. return;
  284. }
  285. PairKey key(p_A->_id, p_B->_id);
  286. typename PairMap::Element *E = pair_map.find(key);
  287. if (!E) {
  288. return; // no pair
  289. }
  290. E->get().refcount--;
  291. if (E->get().refcount == 0) {
  292. // bye pair
  293. if (E->get().intersect) {
  294. if (unpair_callback) {
  295. 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);
  296. }
  297. pair_count--;
  298. }
  299. if (p_A == E->get().B) {
  300. //may be reaching inverted
  301. SWAP(p_A, p_B);
  302. }
  303. p_A->pair_list.erase(E->get().eA);
  304. p_B->pair_list.erase(E->get().eB);
  305. pair_map.erase(E);
  306. }
  307. }
  308. _FORCE_INLINE_ void _element_check_pairs(Element *p_element) {
  309. typename List<PairData *, AL>::Element *E = p_element->pair_list.front();
  310. while (E) {
  311. _pair_check(E->get());
  312. E = E->next();
  313. }
  314. }
  315. _FORCE_INLINE_ void _optimize() {
  316. while (root && root->children_count < 2 && !root->elements.size() && !(use_pairs && root->pairable_elements.size())) {
  317. Octant *new_root = nullptr;
  318. if (root->children_count == 1) {
  319. for (int i = 0; i < 8; i++) {
  320. if (root->children[i]) {
  321. new_root = root->children[i];
  322. root->children[i] = nullptr;
  323. break;
  324. }
  325. }
  326. ERR_FAIL_COND(!new_root);
  327. new_root->parent = nullptr;
  328. new_root->parent_index = -1;
  329. }
  330. memdelete_allocator<Octant, AL>(root);
  331. octant_count--;
  332. root = new_root;
  333. }
  334. }
  335. void _insert_element(Element *p_element, Octant *p_octant);
  336. void _ensure_valid_root(const AABB &p_aabb);
  337. bool _remove_element_pair_and_remove_empty_octants(Element *p_element, Octant *p_octant, Octant *p_limit = nullptr);
  338. void _remove_element(Element *p_element);
  339. void _pair_element(Element *p_element, Octant *p_octant);
  340. void _unpair_element(Element *p_element, Octant *p_octant);
  341. struct _CullConvexData {
  342. const Plane *planes;
  343. int plane_count;
  344. const Vector3 *points;
  345. int point_count;
  346. T **result_array;
  347. int *result_idx;
  348. int result_max;
  349. uint32_t mask;
  350. };
  351. void _cull_convex(Octant *p_octant, _CullConvexData *p_cull);
  352. 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);
  353. 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);
  354. 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);
  355. void _remove_tree(Octant *p_octant) {
  356. if (!p_octant) {
  357. return;
  358. }
  359. for (int i = 0; i < 8; i++) {
  360. if (p_octant->children[i]) {
  361. _remove_tree(p_octant->children[i]);
  362. }
  363. }
  364. memdelete_allocator<Octant, AL>(p_octant);
  365. }
  366. #ifdef TOOLS_ENABLED
  367. String debug_aabb_to_string(const AABB &aabb) const;
  368. void debug_octant(const Octant &oct, int depth = 0);
  369. #endif
  370. public:
  371. 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);
  372. void move(OctreeElementID p_id, const AABB &p_aabb);
  373. void set_pairable(OctreeElementID p_id, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1);
  374. void erase(OctreeElementID p_id);
  375. bool is_pairable(OctreeElementID p_id) const;
  376. T *get(OctreeElementID p_id) const;
  377. int get_subindex(OctreeElementID p_id) const;
  378. int cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
  379. int cull_aabb(const AABB &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  380. int cull_segment(const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  381. int cull_point(const Vector3 &p_point, T **p_result_array, int p_result_max, int *p_subindex_array = nullptr, uint32_t p_mask = 0xFFFFFFFF);
  382. void set_pair_callback(PairCallback p_callback, void *p_userdata);
  383. void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
  384. int get_octant_count() const { return octant_count; }
  385. int get_pair_count() const { return pair_count; }
  386. void set_octant_elements_limit(int p_limit) { octant_elements_limit = p_limit; }
  387. // just convenience for project settings, as users don't need to know exact numbers
  388. void set_balance(float p_bal) // 0.0 is optimized for multiple tests, 1.0 is for multiple edits (moves etc)
  389. {
  390. float v = CLAMP(p_bal, 0.0f, 1.0f);
  391. v *= v;
  392. v *= v;
  393. v *= 8096.0f; // these values have been found empirically
  394. int l = 0 + v;
  395. set_octant_elements_limit(l);
  396. }
  397. #ifdef TOOLS_ENABLED
  398. void debug_octants();
  399. #endif
  400. OCTREE_CLASS_NAME(real_t p_unit_size = 1.0);
  401. ~OCTREE_CLASS_NAME() { _remove_tree(root); }
  402. };
  403. /* PRIVATE FUNCTIONS */
  404. OCTREE_FUNC(T *)::get(OctreeElementID p_id) const {
  405. const typename ElementMap::Element *E = element_map.find(p_id);
  406. ERR_FAIL_COND_V(!E, nullptr);
  407. return E->get().userdata;
  408. }
  409. OCTREE_FUNC(bool)::is_pairable(OctreeElementID p_id) const {
  410. const typename ElementMap::Element *E = element_map.find(p_id);
  411. ERR_FAIL_COND_V(!E, false);
  412. return E->get().pairable;
  413. }
  414. OCTREE_FUNC(int)::get_subindex(OctreeElementID p_id) const {
  415. const typename ElementMap::Element *E = element_map.find(p_id);
  416. ERR_FAIL_COND_V(!E, -1);
  417. return E->get().subindex;
  418. }
  419. #define OCTREE_DIVISOR 4
  420. OCTREE_FUNC(void)::_insert_element(Element *p_element, Octant *p_octant) {
  421. real_t element_size = p_element->aabb.get_longest_axis_size() * 1.01; // avoid precision issues
  422. // don't create new child octants unless there is more than a certain number in
  423. // this octant. This prevents runaway creation of too many octants, and is more efficient
  424. // because brute force is faster up to a certain point.
  425. bool can_split = true;
  426. if (p_element->pairable) {
  427. if (p_octant->pairable_elements.size() < octant_elements_limit) {
  428. can_split = false;
  429. }
  430. } else {
  431. if (p_octant->elements.size() < octant_elements_limit) {
  432. can_split = false;
  433. }
  434. }
  435. if (!can_split || (element_size > (p_octant->aabb.size.x / OCTREE_DIVISOR))) {
  436. /* at smallest possible size for the element */
  437. typename Element::OctantOwner owner;
  438. owner.octant = p_octant;
  439. if (use_pairs && p_element->pairable) {
  440. p_octant->pairable_elements.push_back(p_element);
  441. owner.E = p_octant->pairable_elements.back();
  442. } else {
  443. p_octant->elements.push_back(p_element);
  444. owner.E = p_octant->elements.back();
  445. }
  446. #ifdef OCTREE_USE_CACHED_LISTS
  447. p_octant->dirty = true;
  448. #endif
  449. p_element->octant_owners.push_back(owner);
  450. if (p_element->common_parent == nullptr) {
  451. p_element->common_parent = p_octant;
  452. p_element->container_aabb = p_octant->aabb;
  453. } else {
  454. p_element->container_aabb.merge_with(p_octant->aabb);
  455. }
  456. if (use_pairs && p_octant->children_count > 0) {
  457. pass++; //elements below this only get ONE reference added
  458. for (int i = 0; i < 8; i++) {
  459. if (p_octant->children[i]) {
  460. _pair_element(p_element, p_octant->children[i]);
  461. }
  462. }
  463. }
  464. } else {
  465. /* not big enough, send it to subitems */
  466. int splits = 0;
  467. bool candidate = p_element->common_parent == nullptr;
  468. for (int i = 0; i < 8; i++) {
  469. if (p_octant->children[i]) {
  470. /* element exists, go straight to it */
  471. if (p_octant->children[i]->aabb.intersects_inclusive(p_element->aabb)) {
  472. _insert_element(p_element, p_octant->children[i]);
  473. splits++;
  474. }
  475. } else {
  476. /* check against AABB where child should be */
  477. AABB aabb = p_octant->aabb;
  478. aabb.size *= 0.5;
  479. if (i & 1) {
  480. aabb.position.x += aabb.size.x;
  481. }
  482. if (i & 2) {
  483. aabb.position.y += aabb.size.y;
  484. }
  485. if (i & 4) {
  486. aabb.position.z += aabb.size.z;
  487. }
  488. if (aabb.intersects_inclusive(p_element->aabb)) {
  489. /* if actually intersects, create the child */
  490. Octant *child = memnew_allocator(Octant, AL);
  491. p_octant->children[i] = child;
  492. child->parent = p_octant;
  493. child->parent_index = i;
  494. child->aabb = aabb;
  495. p_octant->children_count++;
  496. _insert_element(p_element, child);
  497. octant_count++;
  498. splits++;
  499. }
  500. }
  501. }
  502. if (candidate && splits > 1) {
  503. p_element->common_parent = p_octant;
  504. }
  505. }
  506. if (use_pairs) {
  507. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  508. while (E) {
  509. _pair_reference(p_element, E->get());
  510. E = E->next();
  511. }
  512. if (p_element->pairable) {
  513. // and always test non-pairable if element is pairable
  514. E = p_octant->elements.front();
  515. while (E) {
  516. _pair_reference(p_element, E->get());
  517. E = E->next();
  518. }
  519. }
  520. }
  521. }
  522. OCTREE_FUNC(void)::_ensure_valid_root(const AABB &p_aabb) {
  523. if (!root) {
  524. // octre is empty
  525. AABB base(Vector3(), Vector3(1.0, 1.0, 1.0) * unit_size);
  526. while (!base.encloses(p_aabb)) {
  527. if (ABS(base.position.x + base.size.x) <= ABS(base.position.x)) {
  528. /* grow towards positive */
  529. base.size *= 2.0;
  530. } else {
  531. base.position -= base.size;
  532. base.size *= 2.0;
  533. }
  534. }
  535. root = memnew_allocator(Octant, AL);
  536. root->parent = nullptr;
  537. root->parent_index = -1;
  538. root->aabb = base;
  539. octant_count++;
  540. } else {
  541. AABB base = root->aabb;
  542. while (!base.encloses(p_aabb)) {
  543. ERR_FAIL_COND_MSG(base.size.x > OCTREE_SIZE_LIMIT, "Octree upper size limit reached, does the AABB supplied contain NAN?");
  544. Octant *gp = memnew_allocator(Octant, AL);
  545. octant_count++;
  546. root->parent = gp;
  547. if (ABS(base.position.x + base.size.x) <= ABS(base.position.x)) {
  548. /* grow towards positive */
  549. base.size *= 2.0;
  550. gp->aabb = base;
  551. gp->children[0] = root;
  552. root->parent_index = 0;
  553. } else {
  554. base.position -= base.size;
  555. base.size *= 2.0;
  556. gp->aabb = base;
  557. gp->children[(1 << 0) | (1 << 1) | (1 << 2)] = root; // add at all-positive
  558. root->parent_index = (1 << 0) | (1 << 1) | (1 << 2);
  559. }
  560. gp->children_count = 1;
  561. root = gp;
  562. }
  563. }
  564. }
  565. OCTREE_FUNC(bool)::_remove_element_pair_and_remove_empty_octants(Element *p_element, Octant *p_octant, Octant *p_limit) {
  566. bool octant_removed = false;
  567. while (true) {
  568. // check all exit conditions
  569. if (p_octant == p_limit) { // reached limit, nothing to erase, exit
  570. return octant_removed;
  571. }
  572. bool unpaired = false;
  573. if (use_pairs && p_octant->last_pass != pass) {
  574. // check whether we should unpair stuff
  575. // always test pairable
  576. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  577. while (E) {
  578. _pair_unreference(p_element, E->get());
  579. E = E->next();
  580. }
  581. if (p_element->pairable) {
  582. // and always test non-pairable if element is pairable
  583. E = p_octant->elements.front();
  584. while (E) {
  585. _pair_unreference(p_element, E->get());
  586. E = E->next();
  587. }
  588. }
  589. p_octant->last_pass = pass;
  590. unpaired = true;
  591. }
  592. bool removed = false;
  593. Octant *parent = p_octant->parent;
  594. if (p_octant->children_count == 0 && p_octant->elements.empty() && p_octant->pairable_elements.empty()) {
  595. // erase octant
  596. if (p_octant == root) { // won't have a parent, just erase
  597. root = nullptr;
  598. } else {
  599. ERR_FAIL_INDEX_V(p_octant->parent_index, 8, octant_removed);
  600. parent->children[p_octant->parent_index] = nullptr;
  601. parent->children_count--;
  602. }
  603. memdelete_allocator<Octant, AL>(p_octant);
  604. octant_count--;
  605. removed = true;
  606. octant_removed = true;
  607. }
  608. if (!removed && !unpaired) {
  609. return octant_removed; // no reason to keep going up anymore! was already visited and was not removed
  610. }
  611. p_octant = parent;
  612. }
  613. return octant_removed;
  614. }
  615. OCTREE_FUNC(void)::_unpair_element(Element *p_element, Octant *p_octant) {
  616. // always test pairable
  617. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  618. while (E) {
  619. if (E->get()->last_pass != pass) { // only remove ONE reference
  620. _pair_unreference(p_element, E->get());
  621. E->get()->last_pass = pass;
  622. }
  623. E = E->next();
  624. }
  625. if (p_element->pairable) {
  626. // and always test non-pairable if element is pairable
  627. E = p_octant->elements.front();
  628. while (E) {
  629. if (E->get()->last_pass != pass) { // only remove ONE reference
  630. _pair_unreference(p_element, E->get());
  631. E->get()->last_pass = pass;
  632. }
  633. E = E->next();
  634. }
  635. }
  636. p_octant->last_pass = pass;
  637. if (p_octant->children_count == 0) {
  638. return; // small optimization for leafs
  639. }
  640. for (int i = 0; i < 8; i++) {
  641. if (p_octant->children[i]) {
  642. _unpair_element(p_element, p_octant->children[i]);
  643. }
  644. }
  645. }
  646. OCTREE_FUNC(void)::_pair_element(Element *p_element, Octant *p_octant) {
  647. // always test pairable
  648. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  649. while (E) {
  650. if (E->get()->last_pass != pass) { // only get ONE reference
  651. _pair_reference(p_element, E->get());
  652. E->get()->last_pass = pass;
  653. }
  654. E = E->next();
  655. }
  656. if (p_element->pairable) {
  657. // and always test non-pairable if element is pairable
  658. E = p_octant->elements.front();
  659. while (E) {
  660. if (E->get()->last_pass != pass) { // only get ONE reference
  661. _pair_reference(p_element, E->get());
  662. E->get()->last_pass = pass;
  663. }
  664. E = E->next();
  665. }
  666. }
  667. p_octant->last_pass = pass;
  668. if (p_octant->children_count == 0) {
  669. return; // small optimization for leafs
  670. }
  671. for (int i = 0; i < 8; i++) {
  672. if (p_octant->children[i]) {
  673. _pair_element(p_element, p_octant->children[i]);
  674. }
  675. }
  676. }
  677. OCTREE_FUNC(void)::_remove_element(Element *p_element) {
  678. pass++; // will do a new pass for this
  679. typename List<typename Element::OctantOwner, AL>::Element *I = p_element->octant_owners.front();
  680. for (; I; I = I->next()) {
  681. Octant *o = I->get().octant;
  682. if (!use_pairs) {
  683. o->elements.erase(I->get().E);
  684. } else {
  685. // erase children pairs, they are erased ONCE even if repeated
  686. pass++;
  687. for (int i = 0; i < 8; i++) {
  688. if (o->children[i]) {
  689. _unpair_element(p_element, o->children[i]);
  690. }
  691. }
  692. if (p_element->pairable) {
  693. o->pairable_elements.erase(I->get().E);
  694. } else {
  695. o->elements.erase(I->get().E);
  696. }
  697. }
  698. #ifdef OCTREE_USE_CACHED_LISTS
  699. o->dirty = true;
  700. #endif
  701. _remove_element_pair_and_remove_empty_octants(p_element, o);
  702. }
  703. p_element->octant_owners.clear();
  704. if (use_pairs) {
  705. int remaining = p_element->pair_list.size();
  706. //p_element->pair_list.clear();
  707. ERR_FAIL_COND(remaining);
  708. }
  709. }
  710. OCTREE_FUNC(OctreeElementID)::create(T *p_userdata, const AABB &p_aabb, int p_subindex, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) {
  711. // check for AABB validity
  712. #ifdef DEBUG_ENABLED
  713. ERR_FAIL_COND_V(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15, 0);
  714. ERR_FAIL_COND_V(p_aabb.position.y > 1e15 || p_aabb.position.y < -1e15, 0);
  715. ERR_FAIL_COND_V(p_aabb.position.z > 1e15 || p_aabb.position.z < -1e15, 0);
  716. ERR_FAIL_COND_V(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0, 0);
  717. ERR_FAIL_COND_V(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0, 0);
  718. ERR_FAIL_COND_V(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0, 0);
  719. ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.x), 0);
  720. ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.y), 0);
  721. ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.z), 0);
  722. #endif
  723. typename ElementMap::Element *E = element_map.insert(last_element_id++,
  724. Element());
  725. Element &e = E->get();
  726. e.aabb = p_aabb;
  727. e.userdata = p_userdata;
  728. e.subindex = p_subindex;
  729. e.last_pass = 0;
  730. e.octree = this;
  731. e.pairable = p_pairable;
  732. e.pairable_type = p_pairable_type;
  733. e.pairable_mask = p_pairable_mask;
  734. e._id = last_element_id - 1;
  735. if (!e.aabb.has_no_surface()) {
  736. _ensure_valid_root(p_aabb);
  737. _insert_element(&e, root);
  738. if (use_pairs) {
  739. _element_check_pairs(&e);
  740. }
  741. }
  742. return last_element_id - 1;
  743. }
  744. OCTREE_FUNC(void)::move(OctreeElementID p_id, const AABB &p_aabb) {
  745. #ifdef DEBUG_ENABLED
  746. // check for AABB validity
  747. ERR_FAIL_COND(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15);
  748. ERR_FAIL_COND(p_aabb.position.y > 1e15 || p_aabb.position.y < -1e15);
  749. ERR_FAIL_COND(p_aabb.position.z > 1e15 || p_aabb.position.z < -1e15);
  750. ERR_FAIL_COND(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0);
  751. ERR_FAIL_COND(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0);
  752. ERR_FAIL_COND(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0);
  753. ERR_FAIL_COND(Math::is_nan(p_aabb.size.x));
  754. ERR_FAIL_COND(Math::is_nan(p_aabb.size.y));
  755. ERR_FAIL_COND(Math::is_nan(p_aabb.size.z));
  756. #endif
  757. typename ElementMap::Element *E = element_map.find(p_id);
  758. ERR_FAIL_COND(!E);
  759. Element &e = E->get();
  760. bool old_has_surf = !e.aabb.has_no_surface();
  761. bool new_has_surf = !p_aabb.has_no_surface();
  762. if (old_has_surf != new_has_surf) {
  763. if (old_has_surf) {
  764. _remove_element(&e); // removing
  765. e.common_parent = nullptr;
  766. e.aabb = AABB();
  767. _optimize();
  768. } else {
  769. _ensure_valid_root(p_aabb); // inserting
  770. e.common_parent = nullptr;
  771. e.aabb = p_aabb;
  772. _insert_element(&e, root);
  773. if (use_pairs) {
  774. _element_check_pairs(&e);
  775. }
  776. }
  777. return;
  778. }
  779. if (!old_has_surf) { // doing nothing
  780. return;
  781. }
  782. // it still is enclosed in the same AABB it was assigned to
  783. if (e.container_aabb.encloses(p_aabb)) {
  784. e.aabb = p_aabb;
  785. if (use_pairs) {
  786. _element_check_pairs(&e); // must check pairs anyway
  787. }
  788. #ifdef OCTREE_USE_CACHED_LISTS
  789. e.moving();
  790. #endif
  791. return;
  792. }
  793. AABB combined = e.aabb;
  794. combined.merge_with(p_aabb);
  795. _ensure_valid_root(combined);
  796. ERR_FAIL_COND(e.octant_owners.front() == nullptr);
  797. /* FIND COMMON PARENT */
  798. List<typename Element::OctantOwner, AL> owners = e.octant_owners; // save the octant owners
  799. Octant *common_parent = e.common_parent;
  800. ERR_FAIL_COND(!common_parent);
  801. //src is now the place towards where insertion is going to happen
  802. pass++;
  803. while (common_parent && !common_parent->aabb.encloses(p_aabb)) {
  804. common_parent = common_parent->parent;
  805. }
  806. ERR_FAIL_COND(!common_parent);
  807. //prepare for reinsert
  808. e.octant_owners.clear();
  809. e.common_parent = nullptr;
  810. e.aabb = p_aabb;
  811. _insert_element(&e, common_parent); // reinsert from this point
  812. pass++;
  813. for (typename List<typename Element::OctantOwner, AL>::Element *F = owners.front(); F;) {
  814. Octant *o = F->get().octant;
  815. typename List<typename Element::OctantOwner, AL>::Element *N = F->next();
  816. /*
  817. if (!use_pairs)
  818. o->elements.erase( F->get().E );
  819. */
  820. if (use_pairs && e.pairable) {
  821. o->pairable_elements.erase(F->get().E);
  822. } else {
  823. o->elements.erase(F->get().E);
  824. }
  825. #ifdef OCTREE_USE_CACHED_LISTS
  826. o->dirty = true;
  827. #endif
  828. if (_remove_element_pair_and_remove_empty_octants(&e, o, common_parent->parent)) {
  829. owners.erase(F);
  830. }
  831. F = N;
  832. }
  833. if (use_pairs) {
  834. //unpair child elements in anything that survived
  835. for (typename List<typename Element::OctantOwner, AL>::Element *F = owners.front(); F; F = F->next()) {
  836. Octant *o = F->get().octant;
  837. // erase children pairs, unref ONCE
  838. pass++;
  839. for (int i = 0; i < 8; i++) {
  840. if (o->children[i]) {
  841. _unpair_element(&e, o->children[i]);
  842. }
  843. }
  844. }
  845. _element_check_pairs(&e);
  846. }
  847. _optimize();
  848. }
  849. OCTREE_FUNC(void)::set_pairable(OctreeElementID p_id, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) {
  850. typename ElementMap::Element *E = element_map.find(p_id);
  851. ERR_FAIL_COND(!E);
  852. Element &e = E->get();
  853. if (p_pairable == e.pairable && e.pairable_type == p_pairable_type && e.pairable_mask == p_pairable_mask) {
  854. return; // no changes, return
  855. }
  856. if (!e.aabb.has_no_surface()) {
  857. _remove_element(&e);
  858. }
  859. e.pairable = p_pairable;
  860. e.pairable_type = p_pairable_type;
  861. e.pairable_mask = p_pairable_mask;
  862. e.common_parent = nullptr;
  863. if (!e.aabb.has_no_surface()) {
  864. _ensure_valid_root(e.aabb);
  865. _insert_element(&e, root);
  866. if (use_pairs) {
  867. _element_check_pairs(&e);
  868. }
  869. }
  870. }
  871. OCTREE_FUNC(void)::erase(OctreeElementID p_id) {
  872. typename ElementMap::Element *E = element_map.find(p_id);
  873. ERR_FAIL_COND(!E);
  874. Element &e = E->get();
  875. if (!e.aabb.has_no_surface()) {
  876. _remove_element(&e);
  877. }
  878. element_map.erase(p_id);
  879. _optimize();
  880. }
  881. OCTREE_FUNC(void)::_cull_convex(Octant *p_octant, _CullConvexData *p_cull) {
  882. if (*p_cull->result_idx == p_cull->result_max) {
  883. return; //pointless
  884. }
  885. if (!p_octant->elements.empty()) {
  886. #ifdef OCTREE_USE_CACHED_LISTS
  887. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  888. p_octant->update_cached_lists();
  889. int num_elements = p_octant->clist.elements.size();
  890. for (int n = 0; n < num_elements; n++) {
  891. const AABB &aabb = p_octant->clist.aabbs[n];
  892. Element *e = p_octant->clist.elements[n];
  893. // in most cases with the cached linear list tests we will do the AABB checks BEFORE last pass and cull mask.
  894. // The reason is that the later checks are more expensive because they are not in cache, and many of the AABB
  895. // tests will fail so we can avoid these cache misses.
  896. if (aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  897. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask))) {
  898. continue;
  899. }
  900. e->last_pass = pass;
  901. if (*p_cull->result_idx < p_cull->result_max) {
  902. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  903. (*p_cull->result_idx)++;
  904. } else {
  905. return; // pointless to continue
  906. }
  907. }
  908. } // for n
  909. #else
  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. const AABB &aabb = e->aabb;
  915. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask))) {
  916. continue;
  917. }
  918. e->last_pass = pass;
  919. if (aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  920. if (*p_cull->result_idx < p_cull->result_max) {
  921. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  922. (*p_cull->result_idx)++;
  923. } else {
  924. return; // pointless to continue
  925. }
  926. }
  927. }
  928. #endif
  929. } // if elements not empty
  930. if (use_pairs && !p_octant->pairable_elements.empty()) {
  931. #ifdef OCTREE_USE_CACHED_LISTS
  932. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  933. p_octant->update_cached_lists();
  934. int num_elements = p_octant->clist_pairable.elements.size();
  935. for (int n = 0; n < num_elements; n++) {
  936. const AABB &aabb = p_octant->clist_pairable.aabbs[n];
  937. Element *e = p_octant->clist_pairable.elements[n];
  938. if (aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  939. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask))) {
  940. continue;
  941. }
  942. e->last_pass = pass;
  943. if (*p_cull->result_idx < p_cull->result_max) {
  944. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  945. (*p_cull->result_idx)++;
  946. } else {
  947. return; // pointless to continue
  948. }
  949. }
  950. }
  951. #else
  952. typename List<Element *, AL>::Element *I;
  953. I = p_octant->pairable_elements.front();
  954. for (; I; I = I->next()) {
  955. Element *e = I->get();
  956. const AABB &aabb = e->aabb;
  957. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask))) {
  958. continue;
  959. }
  960. e->last_pass = pass;
  961. if (aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  962. if (*p_cull->result_idx < p_cull->result_max) {
  963. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  964. (*p_cull->result_idx)++;
  965. } else {
  966. return; // pointless to continue
  967. }
  968. }
  969. }
  970. #endif
  971. }
  972. for (int i = 0; i < 8; i++) {
  973. if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  974. _cull_convex(p_octant->children[i], p_cull);
  975. }
  976. }
  977. }
  978. OCTREE_FUNC(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) {
  979. if (*p_result_idx == p_result_max) {
  980. return; //pointless
  981. }
  982. if (!p_octant->elements.empty()) {
  983. #ifdef OCTREE_USE_CACHED_LISTS
  984. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  985. p_octant->update_cached_lists();
  986. int num_elements = p_octant->clist.elements.size();
  987. for (int n = 0; n < num_elements; n++) {
  988. const AABB &aabb = p_octant->clist.aabbs[n];
  989. Element *e = p_octant->clist.elements[n];
  990. if (p_aabb.intersects_inclusive(aabb)) {
  991. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  992. continue;
  993. }
  994. e->last_pass = pass;
  995. if (*p_result_idx < p_result_max) {
  996. p_result_array[*p_result_idx] = e->userdata;
  997. if (p_subindex_array) {
  998. p_subindex_array[*p_result_idx] = e->subindex;
  999. }
  1000. (*p_result_idx)++;
  1001. } else {
  1002. return; // pointless to continue
  1003. }
  1004. }
  1005. }
  1006. #else
  1007. typename List<Element *, AL>::Element *I;
  1008. I = p_octant->elements.front();
  1009. for (; I; I = I->next()) {
  1010. Element *e = I->get();
  1011. const AABB &aabb = e->aabb;
  1012. if (p_aabb.intersects_inclusive(aabb)) {
  1013. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1014. continue;
  1015. }
  1016. e->last_pass = pass;
  1017. if (*p_result_idx < p_result_max) {
  1018. p_result_array[*p_result_idx] = e->userdata;
  1019. if (p_subindex_array) {
  1020. p_subindex_array[*p_result_idx] = e->subindex;
  1021. }
  1022. (*p_result_idx)++;
  1023. } else {
  1024. return; // pointless to continue
  1025. }
  1026. }
  1027. }
  1028. #endif
  1029. }
  1030. if (use_pairs && !p_octant->pairable_elements.empty()) {
  1031. #ifdef OCTREE_USE_CACHED_LISTS
  1032. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  1033. p_octant->update_cached_lists();
  1034. int num_elements = p_octant->clist_pairable.elements.size();
  1035. for (int n = 0; n < num_elements; n++) {
  1036. const AABB &aabb = p_octant->clist_pairable.aabbs[n];
  1037. Element *e = p_octant->clist_pairable.elements[n];
  1038. if (p_aabb.intersects_inclusive(aabb)) {
  1039. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1040. continue;
  1041. }
  1042. e->last_pass = pass;
  1043. if (*p_result_idx < p_result_max) {
  1044. p_result_array[*p_result_idx] = e->userdata;
  1045. if (p_subindex_array) {
  1046. p_subindex_array[*p_result_idx] = e->subindex;
  1047. }
  1048. (*p_result_idx)++;
  1049. } else {
  1050. return; // pointless to continue
  1051. }
  1052. }
  1053. }
  1054. #else
  1055. typename List<Element *, AL>::Element *I;
  1056. I = p_octant->pairable_elements.front();
  1057. for (; I; I = I->next()) {
  1058. Element *e = I->get();
  1059. const AABB &aabb = e->aabb;
  1060. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1061. continue;
  1062. }
  1063. e->last_pass = pass;
  1064. if (p_aabb.intersects_inclusive(aabb)) {
  1065. if (*p_result_idx < p_result_max) {
  1066. p_result_array[*p_result_idx] = e->userdata;
  1067. if (p_subindex_array) {
  1068. p_subindex_array[*p_result_idx] = e->subindex;
  1069. }
  1070. (*p_result_idx)++;
  1071. } else {
  1072. return; // pointless to continue
  1073. }
  1074. }
  1075. }
  1076. #endif
  1077. }
  1078. for (int i = 0; i < 8; i++) {
  1079. if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_inclusive(p_aabb)) {
  1080. _cull_aabb(p_octant->children[i], p_aabb, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
  1081. }
  1082. }
  1083. }
  1084. OCTREE_FUNC(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) {
  1085. if (*p_result_idx == p_result_max) {
  1086. return; //pointless
  1087. }
  1088. if (!p_octant->elements.empty()) {
  1089. #ifdef OCTREE_USE_CACHED_LISTS
  1090. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  1091. p_octant->update_cached_lists();
  1092. int num_elements = p_octant->clist.elements.size();
  1093. for (int n = 0; n < num_elements; n++) {
  1094. const AABB &aabb = p_octant->clist.aabbs[n];
  1095. Element *e = p_octant->clist.elements[n];
  1096. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1097. continue;
  1098. }
  1099. e->last_pass = pass;
  1100. if (aabb.intersects_segment(p_from, p_to)) {
  1101. if (*p_result_idx < p_result_max) {
  1102. p_result_array[*p_result_idx] = e->userdata;
  1103. if (p_subindex_array) {
  1104. p_subindex_array[*p_result_idx] = e->subindex;
  1105. }
  1106. (*p_result_idx)++;
  1107. } else {
  1108. return; // pointless to continue
  1109. }
  1110. }
  1111. }
  1112. #else
  1113. typename List<Element *, AL>::Element *I;
  1114. I = p_octant->elements.front();
  1115. for (; I; I = I->next()) {
  1116. Element *e = I->get();
  1117. const AABB &aabb = e->aabb;
  1118. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1119. continue;
  1120. }
  1121. e->last_pass = pass;
  1122. if (aabb.intersects_segment(p_from, p_to)) {
  1123. if (*p_result_idx < p_result_max) {
  1124. p_result_array[*p_result_idx] = e->userdata;
  1125. if (p_subindex_array) {
  1126. p_subindex_array[*p_result_idx] = e->subindex;
  1127. }
  1128. (*p_result_idx)++;
  1129. } else {
  1130. return; // pointless to continue
  1131. }
  1132. }
  1133. }
  1134. #endif
  1135. }
  1136. if (use_pairs && !p_octant->pairable_elements.empty()) {
  1137. #ifdef OCTREE_USE_CACHED_LISTS
  1138. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  1139. p_octant->update_cached_lists();
  1140. int num_elements = p_octant->clist_pairable.elements.size();
  1141. for (int n = 0; n < num_elements; n++) {
  1142. const AABB &aabb = p_octant->clist_pairable.aabbs[n];
  1143. Element *e = p_octant->clist_pairable.elements[n];
  1144. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1145. continue;
  1146. }
  1147. e->last_pass = pass;
  1148. if (aabb.intersects_segment(p_from, p_to)) {
  1149. if (*p_result_idx < p_result_max) {
  1150. p_result_array[*p_result_idx] = e->userdata;
  1151. if (p_subindex_array) {
  1152. p_subindex_array[*p_result_idx] = e->subindex;
  1153. }
  1154. (*p_result_idx)++;
  1155. } else {
  1156. return; // pointless to continue
  1157. }
  1158. }
  1159. }
  1160. #else
  1161. typename List<Element *, AL>::Element *I;
  1162. I = p_octant->pairable_elements.front();
  1163. for (; I; I = I->next()) {
  1164. Element *e = I->get();
  1165. const AABB &aabb = e->aabb;
  1166. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1167. continue;
  1168. }
  1169. e->last_pass = pass;
  1170. if (aabb.intersects_segment(p_from, p_to)) {
  1171. if (*p_result_idx < p_result_max) {
  1172. p_result_array[*p_result_idx] = e->userdata;
  1173. if (p_subindex_array) {
  1174. p_subindex_array[*p_result_idx] = e->subindex;
  1175. }
  1176. (*p_result_idx)++;
  1177. } else {
  1178. return; // pointless to continue
  1179. }
  1180. }
  1181. }
  1182. #endif
  1183. }
  1184. for (int i = 0; i < 8; i++) {
  1185. if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_segment(p_from, p_to)) {
  1186. _cull_segment(p_octant->children[i], p_from, p_to, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
  1187. }
  1188. }
  1189. }
  1190. OCTREE_FUNC(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) {
  1191. if (*p_result_idx == p_result_max) {
  1192. return; //pointless
  1193. }
  1194. if (!p_octant->elements.empty()) {
  1195. #ifdef OCTREE_USE_CACHED_LISTS
  1196. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  1197. p_octant->update_cached_lists();
  1198. int num_elements = p_octant->clist.elements.size();
  1199. for (int n = 0; n < num_elements; n++) {
  1200. const AABB &aabb = p_octant->clist.aabbs[n];
  1201. Element *e = p_octant->clist.elements[n];
  1202. if (aabb.has_point(p_point)) {
  1203. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1204. continue;
  1205. }
  1206. e->last_pass = pass;
  1207. if (*p_result_idx < p_result_max) {
  1208. p_result_array[*p_result_idx] = e->userdata;
  1209. if (p_subindex_array) {
  1210. p_subindex_array[*p_result_idx] = e->subindex;
  1211. }
  1212. (*p_result_idx)++;
  1213. } else {
  1214. return; // pointless to continue
  1215. }
  1216. }
  1217. }
  1218. #else
  1219. typename List<Element *, AL>::Element *I;
  1220. I = p_octant->elements.front();
  1221. for (; I; I = I->next()) {
  1222. Element *e = I->get();
  1223. const AABB &aabb = e->aabb;
  1224. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1225. continue;
  1226. }
  1227. e->last_pass = pass;
  1228. if (aabb.has_point(p_point)) {
  1229. if (*p_result_idx < p_result_max) {
  1230. p_result_array[*p_result_idx] = e->userdata;
  1231. if (p_subindex_array) {
  1232. p_subindex_array[*p_result_idx] = e->subindex;
  1233. }
  1234. (*p_result_idx)++;
  1235. } else {
  1236. return; // pointless to continue
  1237. }
  1238. }
  1239. }
  1240. #endif
  1241. }
  1242. if (use_pairs && !p_octant->pairable_elements.empty()) {
  1243. #ifdef OCTREE_USE_CACHED_LISTS
  1244. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  1245. p_octant->update_cached_lists();
  1246. int num_elements = p_octant->clist_pairable.elements.size();
  1247. for (int n = 0; n < num_elements; n++) {
  1248. const AABB &aabb = p_octant->clist_pairable.aabbs[n];
  1249. Element *e = p_octant->clist_pairable.elements[n];
  1250. if (aabb.has_point(p_point)) {
  1251. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1252. continue;
  1253. }
  1254. e->last_pass = pass;
  1255. if (*p_result_idx < p_result_max) {
  1256. p_result_array[*p_result_idx] = e->userdata;
  1257. if (p_subindex_array) {
  1258. p_subindex_array[*p_result_idx] = e->subindex;
  1259. }
  1260. (*p_result_idx)++;
  1261. } else {
  1262. return; // pointless to continue
  1263. }
  1264. }
  1265. }
  1266. #else
  1267. typename List<Element *, AL>::Element *I;
  1268. I = p_octant->pairable_elements.front();
  1269. for (; I; I = I->next()) {
  1270. Element *e = I->get();
  1271. const AABB &aabb = e->aabb;
  1272. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask))) {
  1273. continue;
  1274. }
  1275. e->last_pass = pass;
  1276. if (aabb.has_point(p_point)) {
  1277. if (*p_result_idx < p_result_max) {
  1278. p_result_array[*p_result_idx] = e->userdata;
  1279. if (p_subindex_array) {
  1280. p_subindex_array[*p_result_idx] = e->subindex;
  1281. }
  1282. (*p_result_idx)++;
  1283. } else {
  1284. return; // pointless to continue
  1285. }
  1286. }
  1287. }
  1288. #endif
  1289. }
  1290. for (int i = 0; i < 8; i++) {
  1291. //could be optimized..
  1292. if (p_octant->children[i] && p_octant->children[i]->aabb.has_point(p_point)) {
  1293. _cull_point(p_octant->children[i], p_point, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
  1294. }
  1295. }
  1296. }
  1297. OCTREE_FUNC(int)::cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask) {
  1298. if (!root || p_convex.size() == 0) {
  1299. return 0;
  1300. }
  1301. Vector<Vector3> convex_points = Geometry::compute_convex_mesh_points(&p_convex[0], p_convex.size());
  1302. if (convex_points.size() == 0) {
  1303. return 0;
  1304. }
  1305. int result_count = 0;
  1306. pass++;
  1307. _CullConvexData cdata;
  1308. cdata.planes = &p_convex[0];
  1309. cdata.plane_count = p_convex.size();
  1310. cdata.points = &convex_points[0];
  1311. cdata.point_count = convex_points.size();
  1312. cdata.result_array = p_result_array;
  1313. cdata.result_max = p_result_max;
  1314. cdata.result_idx = &result_count;
  1315. cdata.mask = p_mask;
  1316. _cull_convex(root, &cdata);
  1317. return result_count;
  1318. }
  1319. OCTREE_FUNC(int)::cull_aabb(const AABB &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
  1320. if (!root) {
  1321. return 0;
  1322. }
  1323. int result_count = 0;
  1324. pass++;
  1325. _cull_aabb(root, p_aabb, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
  1326. return result_count;
  1327. }
  1328. OCTREE_FUNC(int)::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) {
  1329. if (!root) {
  1330. return 0;
  1331. }
  1332. int result_count = 0;
  1333. pass++;
  1334. _cull_segment(root, p_from, p_to, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
  1335. return result_count;
  1336. }
  1337. OCTREE_FUNC(int)::cull_point(const Vector3 &p_point, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) {
  1338. if (!root) {
  1339. return 0;
  1340. }
  1341. int result_count = 0;
  1342. pass++;
  1343. _cull_point(root, p_point, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
  1344. return result_count;
  1345. }
  1346. OCTREE_FUNC(void)::set_pair_callback(PairCallback p_callback, void *p_userdata) {
  1347. pair_callback = p_callback;
  1348. pair_callback_userdata = p_userdata;
  1349. }
  1350. OCTREE_FUNC(void)::set_unpair_callback(UnpairCallback p_callback, void *p_userdata) {
  1351. unpair_callback = p_callback;
  1352. unpair_callback_userdata = p_userdata;
  1353. }
  1354. OCTREE_FUNC_CONSTRUCTOR::OCTREE_CLASS_NAME(real_t p_unit_size) {
  1355. last_element_id = 1;
  1356. pass = 1;
  1357. unit_size = p_unit_size;
  1358. root = nullptr;
  1359. octant_count = 0;
  1360. pair_count = 0;
  1361. octant_elements_limit = OCTREE_DEFAULT_OCTANT_LIMIT;
  1362. pair_callback = nullptr;
  1363. unpair_callback = nullptr;
  1364. pair_callback_userdata = nullptr;
  1365. unpair_callback_userdata = nullptr;
  1366. }
  1367. #ifdef TOOLS_ENABLED
  1368. OCTREE_FUNC(String)::debug_aabb_to_string(const AABB &aabb) const {
  1369. String sz;
  1370. sz = "( " + String(aabb.position);
  1371. sz += " ) - ( ";
  1372. Vector3 max = aabb.position + aabb.size;
  1373. sz += String(max) + " )";
  1374. return sz;
  1375. }
  1376. OCTREE_FUNC(void)::debug_octants() {
  1377. if (root) {
  1378. debug_octant(*root);
  1379. }
  1380. }
  1381. OCTREE_FUNC(void)::debug_octant(const Octant &oct, int depth) {
  1382. String sz = "";
  1383. for (int d = 0; d < depth; d++) {
  1384. sz += "\t";
  1385. }
  1386. sz += "Octant " + debug_aabb_to_string(oct.aabb);
  1387. sz += "\tnum_children " + itos(oct.children_count);
  1388. sz += ", num_eles " + itos(oct.elements.size());
  1389. sz += ", num_paired_eles" + itos(oct.pairable_elements.size());
  1390. print_line(sz);
  1391. for (int n = 0; n < 8; n++) {
  1392. const Octant *pChild = oct.children[n];
  1393. if (pChild) {
  1394. debug_octant(*pChild, depth + 1);
  1395. }
  1396. }
  1397. }
  1398. #endif // TOOLS_ENABLED
  1399. #undef OCTREE_FUNC