octree_definition.inc 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  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 = NULL;
  160. #ifdef OCTREE_USE_CACHED_LISTS
  161. dirty = true;
  162. #endif
  163. for (int i = 0; i < 8; i++)
  164. children[i] = NULL;
  165. }
  166. ~Octant() {
  167. /*
  168. for (int i=0;i<8;i++)
  169. memdelete_notnull(children[i]);
  170. */
  171. }
  172. };
  173. struct PairData;
  174. struct Element {
  175. OCTREE_CLASS_NAME *octree;
  176. T *userdata;
  177. int subindex;
  178. bool pairable;
  179. uint32_t pairable_mask;
  180. uint32_t pairable_type;
  181. uint64_t last_pass;
  182. OctreeElementID _id;
  183. Octant *common_parent;
  184. AABB aabb;
  185. AABB container_aabb;
  186. List<PairData *, AL> pair_list;
  187. struct OctantOwner {
  188. Octant *octant;
  189. typename List<Element *, AL>::Element *E;
  190. }; // an element can be in max 8 octants
  191. List<OctantOwner, AL> octant_owners;
  192. #ifdef OCTREE_USE_CACHED_LISTS
  193. // when moving we need make all owner octants dirty, because the AABB can change.
  194. void moving() {
  195. for (typename List<typename Element::OctantOwner, AL>::Element *F = octant_owners.front(); F;) {
  196. Octant *o = F->get().octant;
  197. o->dirty = true;
  198. F = F->next();
  199. }
  200. }
  201. #endif
  202. Element() {
  203. last_pass = 0;
  204. _id = 0;
  205. pairable = false;
  206. subindex = 0;
  207. userdata = 0;
  208. octree = 0;
  209. pairable_mask = 0;
  210. pairable_type = 0;
  211. common_parent = NULL;
  212. }
  213. };
  214. struct PairData {
  215. int refcount;
  216. bool intersect;
  217. Element *A, *B;
  218. void *ud;
  219. typename List<PairData *, AL>::Element *eA, *eB;
  220. };
  221. typedef Map<OctreeElementID, Element, Comparator<OctreeElementID>, AL> ElementMap;
  222. typedef Map<PairKey, PairData, Comparator<PairKey>, AL> PairMap;
  223. ElementMap element_map;
  224. PairMap pair_map;
  225. PairCallback pair_callback;
  226. UnpairCallback unpair_callback;
  227. void *pair_callback_userdata;
  228. void *unpair_callback_userdata;
  229. OctreeElementID last_element_id;
  230. uint64_t pass;
  231. real_t unit_size;
  232. Octant *root;
  233. int octant_count;
  234. int pair_count;
  235. int octant_elements_limit;
  236. _FORCE_INLINE_ void _pair_check(PairData *p_pair) {
  237. bool intersect = p_pair->A->aabb.intersects_inclusive(p_pair->B->aabb);
  238. if (intersect != p_pair->intersect) {
  239. if (intersect) {
  240. if (pair_callback) {
  241. 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);
  242. }
  243. pair_count++;
  244. } else {
  245. if (unpair_callback) {
  246. 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);
  247. }
  248. pair_count--;
  249. }
  250. p_pair->intersect = intersect;
  251. }
  252. }
  253. _FORCE_INLINE_ void _pair_reference(Element *p_A, Element *p_B) {
  254. if (p_A == p_B || (p_A->userdata == p_B->userdata && p_A->userdata))
  255. return;
  256. if (!(p_A->pairable_type & p_B->pairable_mask) &&
  257. !(p_B->pairable_type & p_A->pairable_mask))
  258. return; // none can pair with none
  259. PairKey key(p_A->_id, p_B->_id);
  260. typename PairMap::Element *E = pair_map.find(key);
  261. if (!E) {
  262. PairData pdata;
  263. pdata.refcount = 1;
  264. pdata.A = p_A;
  265. pdata.B = p_B;
  266. pdata.intersect = false;
  267. E = pair_map.insert(key, pdata);
  268. E->get().eA = p_A->pair_list.push_back(&E->get());
  269. E->get().eB = p_B->pair_list.push_back(&E->get());
  270. /*
  271. if (pair_callback)
  272. pair_callback(pair_callback_userdata,p_A->userdata,p_B->userdata);
  273. */
  274. } else {
  275. E->get().refcount++;
  276. }
  277. }
  278. _FORCE_INLINE_ void _pair_unreference(Element *p_A, Element *p_B) {
  279. if (p_A == p_B)
  280. return;
  281. PairKey key(p_A->_id, p_B->_id);
  282. typename PairMap::Element *E = pair_map.find(key);
  283. if (!E) {
  284. return; // no pair
  285. }
  286. E->get().refcount--;
  287. if (E->get().refcount == 0) {
  288. // bye pair
  289. if (E->get().intersect) {
  290. if (unpair_callback) {
  291. 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);
  292. }
  293. pair_count--;
  294. }
  295. if (p_A == E->get().B) {
  296. //may be reaching inverted
  297. SWAP(p_A, p_B);
  298. }
  299. p_A->pair_list.erase(E->get().eA);
  300. p_B->pair_list.erase(E->get().eB);
  301. pair_map.erase(E);
  302. }
  303. }
  304. _FORCE_INLINE_ void _element_check_pairs(Element *p_element) {
  305. typename List<PairData *, AL>::Element *E = p_element->pair_list.front();
  306. while (E) {
  307. _pair_check(E->get());
  308. E = E->next();
  309. }
  310. }
  311. _FORCE_INLINE_ void _optimize() {
  312. while (root && root->children_count < 2 && !root->elements.size() && !(use_pairs && root->pairable_elements.size())) {
  313. Octant *new_root = NULL;
  314. if (root->children_count == 1) {
  315. for (int i = 0; i < 8; i++) {
  316. if (root->children[i]) {
  317. new_root = root->children[i];
  318. root->children[i] = NULL;
  319. break;
  320. }
  321. }
  322. ERR_FAIL_COND(!new_root);
  323. new_root->parent = NULL;
  324. new_root->parent_index = -1;
  325. }
  326. memdelete_allocator<Octant, AL>(root);
  327. octant_count--;
  328. root = new_root;
  329. }
  330. }
  331. void _insert_element(Element *p_element, Octant *p_octant);
  332. void _ensure_valid_root(const AABB &p_aabb);
  333. bool _remove_element_pair_and_remove_empty_octants(Element *p_element, Octant *p_octant, Octant *p_limit = NULL);
  334. void _remove_element(Element *p_element);
  335. void _pair_element(Element *p_element, Octant *p_octant);
  336. void _unpair_element(Element *p_element, Octant *p_octant);
  337. struct _CullConvexData {
  338. const Plane *planes;
  339. int plane_count;
  340. const Vector3 *points;
  341. int point_count;
  342. T **result_array;
  343. int *result_idx;
  344. int result_max;
  345. uint32_t mask;
  346. };
  347. void _cull_convex(Octant *p_octant, _CullConvexData *p_cull);
  348. 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);
  349. 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);
  350. 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);
  351. void _remove_tree(Octant *p_octant) {
  352. if (!p_octant)
  353. return;
  354. for (int i = 0; i < 8; i++) {
  355. if (p_octant->children[i])
  356. _remove_tree(p_octant->children[i]);
  357. }
  358. memdelete_allocator<Octant, AL>(p_octant);
  359. }
  360. #ifdef TOOLS_ENABLED
  361. String debug_aabb_to_string(const AABB &aabb) const;
  362. void debug_octant(const Octant &oct, int depth = 0);
  363. #endif
  364. public:
  365. 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);
  366. void move(OctreeElementID p_id, const AABB &p_aabb);
  367. void set_pairable(OctreeElementID p_id, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1);
  368. void erase(OctreeElementID p_id);
  369. bool is_pairable(OctreeElementID p_id) const;
  370. T *get(OctreeElementID p_id) const;
  371. int get_subindex(OctreeElementID p_id) const;
  372. int cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF);
  373. 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);
  374. 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);
  375. 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);
  376. void set_pair_callback(PairCallback p_callback, void *p_userdata);
  377. void set_unpair_callback(UnpairCallback p_callback, void *p_userdata);
  378. int get_octant_count() const { return octant_count; }
  379. int get_pair_count() const { return pair_count; }
  380. void set_octant_elements_limit(int p_limit) { octant_elements_limit = p_limit; }
  381. // just convenience for project settings, as users don't need to know exact numbers
  382. void set_balance(float p_bal) // 0.0 is optimized for multiple tests, 1.0 is for multiple edits (moves etc)
  383. {
  384. float v = CLAMP(p_bal, 0.0f, 1.0f);
  385. v *= v;
  386. v *= v;
  387. v *= 8096.0f; // these values have been found empirically
  388. int l = 0 + v;
  389. set_octant_elements_limit(l);
  390. }
  391. #ifdef TOOLS_ENABLED
  392. void debug_octants();
  393. #endif
  394. OCTREE_CLASS_NAME(real_t p_unit_size = 1.0);
  395. ~OCTREE_CLASS_NAME() { _remove_tree(root); }
  396. };
  397. /* PRIVATE FUNCTIONS */
  398. OCTREE_FUNC(T *)::get(OctreeElementID p_id) const {
  399. const typename ElementMap::Element *E = element_map.find(p_id);
  400. ERR_FAIL_COND_V(!E, NULL);
  401. return E->get().userdata;
  402. }
  403. OCTREE_FUNC(bool)::is_pairable(OctreeElementID p_id) const {
  404. const typename ElementMap::Element *E = element_map.find(p_id);
  405. ERR_FAIL_COND_V(!E, false);
  406. return E->get().pairable;
  407. }
  408. OCTREE_FUNC(int)::get_subindex(OctreeElementID p_id) const {
  409. const typename ElementMap::Element *E = element_map.find(p_id);
  410. ERR_FAIL_COND_V(!E, -1);
  411. return E->get().subindex;
  412. }
  413. #define OCTREE_DIVISOR 4
  414. OCTREE_FUNC(void)::_insert_element(Element *p_element, Octant *p_octant) {
  415. real_t element_size = p_element->aabb.get_longest_axis_size() * 1.01; // avoid precision issues
  416. // don't create new child octants unless there is more than a certain number in
  417. // this octant. This prevents runaway creation of too many octants, and is more efficient
  418. // because brute force is faster up to a certain point.
  419. bool can_split = true;
  420. if (p_element->pairable) {
  421. if (p_octant->pairable_elements.size() < octant_elements_limit)
  422. can_split = false;
  423. } else {
  424. if (p_octant->elements.size() < octant_elements_limit)
  425. can_split = false;
  426. }
  427. if (!can_split || (element_size > (p_octant->aabb.size.x / OCTREE_DIVISOR))) {
  428. /* at smallest possible size for the element */
  429. typename Element::OctantOwner owner;
  430. owner.octant = p_octant;
  431. if (use_pairs && p_element->pairable) {
  432. p_octant->pairable_elements.push_back(p_element);
  433. owner.E = p_octant->pairable_elements.back();
  434. } else {
  435. p_octant->elements.push_back(p_element);
  436. owner.E = p_octant->elements.back();
  437. }
  438. #ifdef OCTREE_USE_CACHED_LISTS
  439. p_octant->dirty = true;
  440. #endif
  441. p_element->octant_owners.push_back(owner);
  442. if (p_element->common_parent == NULL) {
  443. p_element->common_parent = p_octant;
  444. p_element->container_aabb = p_octant->aabb;
  445. } else {
  446. p_element->container_aabb.merge_with(p_octant->aabb);
  447. }
  448. if (use_pairs && p_octant->children_count > 0) {
  449. pass++; //elements below this only get ONE reference added
  450. for (int i = 0; i < 8; i++) {
  451. if (p_octant->children[i]) {
  452. _pair_element(p_element, p_octant->children[i]);
  453. }
  454. }
  455. }
  456. } else {
  457. /* not big enough, send it to subitems */
  458. int splits = 0;
  459. bool candidate = p_element->common_parent == NULL;
  460. for (int i = 0; i < 8; i++) {
  461. if (p_octant->children[i]) {
  462. /* element exists, go straight to it */
  463. if (p_octant->children[i]->aabb.intersects_inclusive(p_element->aabb)) {
  464. _insert_element(p_element, p_octant->children[i]);
  465. splits++;
  466. }
  467. } else {
  468. /* check against AABB where child should be */
  469. AABB aabb = p_octant->aabb;
  470. aabb.size *= 0.5;
  471. if (i & 1)
  472. aabb.position.x += aabb.size.x;
  473. if (i & 2)
  474. aabb.position.y += aabb.size.y;
  475. if (i & 4)
  476. aabb.position.z += aabb.size.z;
  477. if (aabb.intersects_inclusive(p_element->aabb)) {
  478. /* if actually intersects, create the child */
  479. Octant *child = memnew_allocator(Octant, AL);
  480. p_octant->children[i] = child;
  481. child->parent = p_octant;
  482. child->parent_index = i;
  483. child->aabb = aabb;
  484. p_octant->children_count++;
  485. _insert_element(p_element, child);
  486. octant_count++;
  487. splits++;
  488. }
  489. }
  490. }
  491. if (candidate && splits > 1) {
  492. p_element->common_parent = p_octant;
  493. }
  494. }
  495. if (use_pairs) {
  496. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  497. while (E) {
  498. _pair_reference(p_element, E->get());
  499. E = E->next();
  500. }
  501. if (p_element->pairable) {
  502. // and always test non-pairable if element is pairable
  503. E = p_octant->elements.front();
  504. while (E) {
  505. _pair_reference(p_element, E->get());
  506. E = E->next();
  507. }
  508. }
  509. }
  510. }
  511. OCTREE_FUNC(void)::_ensure_valid_root(const AABB &p_aabb) {
  512. if (!root) {
  513. // octre is empty
  514. AABB base(Vector3(), Vector3(1.0, 1.0, 1.0) * unit_size);
  515. while (!base.encloses(p_aabb)) {
  516. if (ABS(base.position.x + base.size.x) <= ABS(base.position.x)) {
  517. /* grow towards positive */
  518. base.size *= 2.0;
  519. } else {
  520. base.position -= base.size;
  521. base.size *= 2.0;
  522. }
  523. }
  524. root = memnew_allocator(Octant, AL);
  525. root->parent = NULL;
  526. root->parent_index = -1;
  527. root->aabb = base;
  528. octant_count++;
  529. } else {
  530. AABB base = root->aabb;
  531. while (!base.encloses(p_aabb)) {
  532. ERR_FAIL_COND_MSG(base.size.x > OCTREE_SIZE_LIMIT, "Octree upper size limit reached, does the AABB supplied contain NAN?");
  533. Octant *gp = memnew_allocator(Octant, AL);
  534. octant_count++;
  535. root->parent = gp;
  536. if (ABS(base.position.x + base.size.x) <= ABS(base.position.x)) {
  537. /* grow towards positive */
  538. base.size *= 2.0;
  539. gp->aabb = base;
  540. gp->children[0] = root;
  541. root->parent_index = 0;
  542. } else {
  543. base.position -= base.size;
  544. base.size *= 2.0;
  545. gp->aabb = base;
  546. gp->children[(1 << 0) | (1 << 1) | (1 << 2)] = root; // add at all-positive
  547. root->parent_index = (1 << 0) | (1 << 1) | (1 << 2);
  548. }
  549. gp->children_count = 1;
  550. root = gp;
  551. }
  552. }
  553. }
  554. OCTREE_FUNC(bool)::_remove_element_pair_and_remove_empty_octants(Element *p_element, Octant *p_octant, Octant *p_limit) {
  555. bool octant_removed = false;
  556. while (true) {
  557. // check all exit conditions
  558. if (p_octant == p_limit) // reached limit, nothing to erase, exit
  559. return octant_removed;
  560. bool unpaired = false;
  561. if (use_pairs && p_octant->last_pass != pass) {
  562. // check whether we should unpair stuff
  563. // always test pairable
  564. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  565. while (E) {
  566. _pair_unreference(p_element, E->get());
  567. E = E->next();
  568. }
  569. if (p_element->pairable) {
  570. // and always test non-pairable if element is pairable
  571. E = p_octant->elements.front();
  572. while (E) {
  573. _pair_unreference(p_element, E->get());
  574. E = E->next();
  575. }
  576. }
  577. p_octant->last_pass = pass;
  578. unpaired = true;
  579. }
  580. bool removed = false;
  581. Octant *parent = p_octant->parent;
  582. if (p_octant->children_count == 0 && p_octant->elements.empty() && p_octant->pairable_elements.empty()) {
  583. // erase octant
  584. if (p_octant == root) { // won't have a parent, just erase
  585. root = NULL;
  586. } else {
  587. ERR_FAIL_INDEX_V(p_octant->parent_index, 8, octant_removed);
  588. parent->children[p_octant->parent_index] = NULL;
  589. parent->children_count--;
  590. }
  591. memdelete_allocator<Octant, AL>(p_octant);
  592. octant_count--;
  593. removed = true;
  594. octant_removed = true;
  595. }
  596. if (!removed && !unpaired)
  597. return octant_removed; // no reason to keep going up anymore! was already visited and was not removed
  598. p_octant = parent;
  599. }
  600. return octant_removed;
  601. }
  602. OCTREE_FUNC(void)::_unpair_element(Element *p_element, Octant *p_octant) {
  603. // always test pairable
  604. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  605. while (E) {
  606. if (E->get()->last_pass != pass) { // only remove ONE reference
  607. _pair_unreference(p_element, E->get());
  608. E->get()->last_pass = pass;
  609. }
  610. E = E->next();
  611. }
  612. if (p_element->pairable) {
  613. // and always test non-pairable if element is pairable
  614. E = p_octant->elements.front();
  615. while (E) {
  616. if (E->get()->last_pass != pass) { // only remove ONE reference
  617. _pair_unreference(p_element, E->get());
  618. E->get()->last_pass = pass;
  619. }
  620. E = E->next();
  621. }
  622. }
  623. p_octant->last_pass = pass;
  624. if (p_octant->children_count == 0)
  625. return; // small optimization for leafs
  626. for (int i = 0; i < 8; i++) {
  627. if (p_octant->children[i])
  628. _unpair_element(p_element, p_octant->children[i]);
  629. }
  630. }
  631. OCTREE_FUNC(void)::_pair_element(Element *p_element, Octant *p_octant) {
  632. // always test pairable
  633. typename List<Element *, AL>::Element *E = p_octant->pairable_elements.front();
  634. while (E) {
  635. if (E->get()->last_pass != pass) { // only get ONE reference
  636. _pair_reference(p_element, E->get());
  637. E->get()->last_pass = pass;
  638. }
  639. E = E->next();
  640. }
  641. if (p_element->pairable) {
  642. // and always test non-pairable if element is pairable
  643. E = p_octant->elements.front();
  644. while (E) {
  645. if (E->get()->last_pass != pass) { // only get ONE reference
  646. _pair_reference(p_element, E->get());
  647. E->get()->last_pass = pass;
  648. }
  649. E = E->next();
  650. }
  651. }
  652. p_octant->last_pass = pass;
  653. if (p_octant->children_count == 0)
  654. return; // small optimization for leafs
  655. for (int i = 0; i < 8; i++) {
  656. if (p_octant->children[i])
  657. _pair_element(p_element, p_octant->children[i]);
  658. }
  659. }
  660. OCTREE_FUNC(void)::_remove_element(Element *p_element) {
  661. pass++; // will do a new pass for this
  662. typename List<typename Element::OctantOwner, AL>::Element *I = p_element->octant_owners.front();
  663. for (; I; I = I->next()) {
  664. Octant *o = I->get().octant;
  665. if (!use_pairs) {
  666. o->elements.erase(I->get().E);
  667. } else {
  668. // erase children pairs, they are erased ONCE even if repeated
  669. pass++;
  670. for (int i = 0; i < 8; i++) {
  671. if (o->children[i]) {
  672. _unpair_element(p_element, o->children[i]);
  673. }
  674. }
  675. if (p_element->pairable) {
  676. o->pairable_elements.erase(I->get().E);
  677. } else {
  678. o->elements.erase(I->get().E);
  679. }
  680. }
  681. #ifdef OCTREE_USE_CACHED_LISTS
  682. o->dirty = true;
  683. #endif
  684. _remove_element_pair_and_remove_empty_octants(p_element, o);
  685. }
  686. p_element->octant_owners.clear();
  687. if (use_pairs) {
  688. int remaining = p_element->pair_list.size();
  689. //p_element->pair_list.clear();
  690. ERR_FAIL_COND(remaining);
  691. }
  692. }
  693. 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) {
  694. // check for AABB validity
  695. #ifdef DEBUG_ENABLED
  696. ERR_FAIL_COND_V(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15, 0);
  697. ERR_FAIL_COND_V(p_aabb.position.y > 1e15 || p_aabb.position.y < -1e15, 0);
  698. ERR_FAIL_COND_V(p_aabb.position.z > 1e15 || p_aabb.position.z < -1e15, 0);
  699. ERR_FAIL_COND_V(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0, 0);
  700. ERR_FAIL_COND_V(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0, 0);
  701. ERR_FAIL_COND_V(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0, 0);
  702. ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.x), 0);
  703. ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.y), 0);
  704. ERR_FAIL_COND_V(Math::is_nan(p_aabb.size.z), 0);
  705. #endif
  706. typename ElementMap::Element *E = element_map.insert(last_element_id++,
  707. Element());
  708. Element &e = E->get();
  709. e.aabb = p_aabb;
  710. e.userdata = p_userdata;
  711. e.subindex = p_subindex;
  712. e.last_pass = 0;
  713. e.octree = this;
  714. e.pairable = p_pairable;
  715. e.pairable_type = p_pairable_type;
  716. e.pairable_mask = p_pairable_mask;
  717. e._id = last_element_id - 1;
  718. if (!e.aabb.has_no_surface()) {
  719. _ensure_valid_root(p_aabb);
  720. _insert_element(&e, root);
  721. if (use_pairs)
  722. _element_check_pairs(&e);
  723. }
  724. return last_element_id - 1;
  725. }
  726. OCTREE_FUNC(void)::move(OctreeElementID p_id, const AABB &p_aabb) {
  727. #ifdef DEBUG_ENABLED
  728. // check for AABB validity
  729. ERR_FAIL_COND(p_aabb.position.x > 1e15 || p_aabb.position.x < -1e15);
  730. ERR_FAIL_COND(p_aabb.position.y > 1e15 || p_aabb.position.y < -1e15);
  731. ERR_FAIL_COND(p_aabb.position.z > 1e15 || p_aabb.position.z < -1e15);
  732. ERR_FAIL_COND(p_aabb.size.x > 1e15 || p_aabb.size.x < 0.0);
  733. ERR_FAIL_COND(p_aabb.size.y > 1e15 || p_aabb.size.y < 0.0);
  734. ERR_FAIL_COND(p_aabb.size.z > 1e15 || p_aabb.size.z < 0.0);
  735. ERR_FAIL_COND(Math::is_nan(p_aabb.size.x));
  736. ERR_FAIL_COND(Math::is_nan(p_aabb.size.y));
  737. ERR_FAIL_COND(Math::is_nan(p_aabb.size.z));
  738. #endif
  739. typename ElementMap::Element *E = element_map.find(p_id);
  740. ERR_FAIL_COND(!E);
  741. Element &e = E->get();
  742. bool old_has_surf = !e.aabb.has_no_surface();
  743. bool new_has_surf = !p_aabb.has_no_surface();
  744. if (old_has_surf != new_has_surf) {
  745. if (old_has_surf) {
  746. _remove_element(&e); // removing
  747. e.common_parent = NULL;
  748. e.aabb = AABB();
  749. _optimize();
  750. } else {
  751. _ensure_valid_root(p_aabb); // inserting
  752. e.common_parent = NULL;
  753. e.aabb = p_aabb;
  754. _insert_element(&e, root);
  755. if (use_pairs)
  756. _element_check_pairs(&e);
  757. }
  758. return;
  759. }
  760. if (!old_has_surf) // doing nothing
  761. return;
  762. // it still is enclosed in the same AABB it was assigned to
  763. if (e.container_aabb.encloses(p_aabb)) {
  764. e.aabb = p_aabb;
  765. if (use_pairs)
  766. _element_check_pairs(&e); // must check pairs anyway
  767. #ifdef OCTREE_USE_CACHED_LISTS
  768. e.moving();
  769. #endif
  770. return;
  771. }
  772. AABB combined = e.aabb;
  773. combined.merge_with(p_aabb);
  774. _ensure_valid_root(combined);
  775. ERR_FAIL_COND(e.octant_owners.front() == NULL);
  776. /* FIND COMMON PARENT */
  777. List<typename Element::OctantOwner, AL> owners = e.octant_owners; // save the octant owners
  778. Octant *common_parent = e.common_parent;
  779. ERR_FAIL_COND(!common_parent);
  780. //src is now the place towards where insertion is going to happen
  781. pass++;
  782. while (common_parent && !common_parent->aabb.encloses(p_aabb))
  783. common_parent = common_parent->parent;
  784. ERR_FAIL_COND(!common_parent);
  785. //prepare for reinsert
  786. e.octant_owners.clear();
  787. e.common_parent = NULL;
  788. e.aabb = p_aabb;
  789. _insert_element(&e, common_parent); // reinsert from this point
  790. pass++;
  791. for (typename List<typename Element::OctantOwner, AL>::Element *F = owners.front(); F;) {
  792. Octant *o = F->get().octant;
  793. typename List<typename Element::OctantOwner, AL>::Element *N = F->next();
  794. /*
  795. if (!use_pairs)
  796. o->elements.erase( F->get().E );
  797. */
  798. if (use_pairs && e.pairable)
  799. o->pairable_elements.erase(F->get().E);
  800. else
  801. o->elements.erase(F->get().E);
  802. #ifdef OCTREE_USE_CACHED_LISTS
  803. o->dirty = true;
  804. #endif
  805. if (_remove_element_pair_and_remove_empty_octants(&e, o, common_parent->parent)) {
  806. owners.erase(F);
  807. }
  808. F = N;
  809. }
  810. if (use_pairs) {
  811. //unpair child elements in anything that survived
  812. for (typename List<typename Element::OctantOwner, AL>::Element *F = owners.front(); F; F = F->next()) {
  813. Octant *o = F->get().octant;
  814. // erase children pairs, unref ONCE
  815. pass++;
  816. for (int i = 0; i < 8; i++) {
  817. if (o->children[i])
  818. _unpair_element(&e, o->children[i]);
  819. }
  820. }
  821. _element_check_pairs(&e);
  822. }
  823. _optimize();
  824. }
  825. OCTREE_FUNC(void)::set_pairable(OctreeElementID p_id, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) {
  826. typename ElementMap::Element *E = element_map.find(p_id);
  827. ERR_FAIL_COND(!E);
  828. Element &e = E->get();
  829. if (p_pairable == e.pairable && e.pairable_type == p_pairable_type && e.pairable_mask == p_pairable_mask)
  830. return; // no changes, return
  831. if (!e.aabb.has_no_surface()) {
  832. _remove_element(&e);
  833. }
  834. e.pairable = p_pairable;
  835. e.pairable_type = p_pairable_type;
  836. e.pairable_mask = p_pairable_mask;
  837. e.common_parent = NULL;
  838. if (!e.aabb.has_no_surface()) {
  839. _ensure_valid_root(e.aabb);
  840. _insert_element(&e, root);
  841. if (use_pairs)
  842. _element_check_pairs(&e);
  843. }
  844. }
  845. OCTREE_FUNC(void)::erase(OctreeElementID p_id) {
  846. typename ElementMap::Element *E = element_map.find(p_id);
  847. ERR_FAIL_COND(!E);
  848. Element &e = E->get();
  849. if (!e.aabb.has_no_surface()) {
  850. _remove_element(&e);
  851. }
  852. element_map.erase(p_id);
  853. _optimize();
  854. }
  855. OCTREE_FUNC(void)::_cull_convex(Octant *p_octant, _CullConvexData *p_cull) {
  856. if (*p_cull->result_idx == p_cull->result_max)
  857. return; //pointless
  858. if (!p_octant->elements.empty()) {
  859. #ifdef OCTREE_USE_CACHED_LISTS
  860. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  861. p_octant->update_cached_lists();
  862. int num_elements = p_octant->clist.elements.size();
  863. for (int n = 0; n < num_elements; n++) {
  864. const AABB &aabb = p_octant->clist.aabbs[n];
  865. Element *e = p_octant->clist.elements[n];
  866. // in most cases with the cached linear list tests we will do the AABB checks BEFORE last pass and cull mask.
  867. // The reason is that the later checks are more expensive because they are not in cache, and many of the AABB
  868. // tests will fail so we can avoid these cache misses.
  869. if (aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  870. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask)))
  871. continue;
  872. e->last_pass = pass;
  873. if (*p_cull->result_idx < p_cull->result_max) {
  874. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  875. (*p_cull->result_idx)++;
  876. } else {
  877. return; // pointless to continue
  878. }
  879. }
  880. } // for n
  881. #else
  882. typename List<Element *, AL>::Element *I;
  883. I = p_octant->elements.front();
  884. for (; I; I = I->next()) {
  885. Element *e = I->get();
  886. const AABB &aabb = e->aabb;
  887. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask)))
  888. continue;
  889. e->last_pass = pass;
  890. if (aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  891. if (*p_cull->result_idx < p_cull->result_max) {
  892. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  893. (*p_cull->result_idx)++;
  894. } else {
  895. return; // pointless to continue
  896. }
  897. }
  898. }
  899. #endif
  900. } // if elements not empty
  901. if (use_pairs && !p_octant->pairable_elements.empty()) {
  902. #ifdef OCTREE_USE_CACHED_LISTS
  903. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  904. p_octant->update_cached_lists();
  905. int num_elements = p_octant->clist_pairable.elements.size();
  906. for (int n = 0; n < num_elements; n++) {
  907. const AABB &aabb = p_octant->clist_pairable.aabbs[n];
  908. Element *e = p_octant->clist_pairable.elements[n];
  909. if (aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  910. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask)))
  911. continue;
  912. e->last_pass = pass;
  913. if (*p_cull->result_idx < p_cull->result_max) {
  914. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  915. (*p_cull->result_idx)++;
  916. } else {
  917. return; // pointless to continue
  918. }
  919. }
  920. }
  921. #else
  922. typename List<Element *, AL>::Element *I;
  923. I = p_octant->pairable_elements.front();
  924. for (; I; I = I->next()) {
  925. Element *e = I->get();
  926. const AABB &aabb = e->aabb;
  927. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_cull->mask)))
  928. continue;
  929. e->last_pass = pass;
  930. if (aabb.intersects_convex_shape(p_cull->planes, p_cull->plane_count, p_cull->points, p_cull->point_count)) {
  931. if (*p_cull->result_idx < p_cull->result_max) {
  932. p_cull->result_array[*p_cull->result_idx] = e->userdata;
  933. (*p_cull->result_idx)++;
  934. } else {
  935. return; // pointless to continue
  936. }
  937. }
  938. }
  939. #endif
  940. }
  941. for (int i = 0; i < 8; i++) {
  942. 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)) {
  943. _cull_convex(p_octant->children[i], p_cull);
  944. }
  945. }
  946. }
  947. 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) {
  948. if (*p_result_idx == p_result_max)
  949. return; //pointless
  950. if (!p_octant->elements.empty()) {
  951. #ifdef OCTREE_USE_CACHED_LISTS
  952. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  953. p_octant->update_cached_lists();
  954. int num_elements = p_octant->clist.elements.size();
  955. for (int n = 0; n < num_elements; n++) {
  956. const AABB &aabb = p_octant->clist.aabbs[n];
  957. Element *e = p_octant->clist.elements[n];
  958. if (p_aabb.intersects_inclusive(aabb)) {
  959. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  960. continue;
  961. e->last_pass = pass;
  962. if (*p_result_idx < p_result_max) {
  963. p_result_array[*p_result_idx] = e->userdata;
  964. if (p_subindex_array)
  965. p_subindex_array[*p_result_idx] = e->subindex;
  966. (*p_result_idx)++;
  967. } else {
  968. return; // pointless to continue
  969. }
  970. }
  971. }
  972. #else
  973. typename List<Element *, AL>::Element *I;
  974. I = p_octant->elements.front();
  975. for (; I; I = I->next()) {
  976. Element *e = I->get();
  977. const AABB &aabb = e->aabb;
  978. if (p_aabb.intersects_inclusive(aabb)) {
  979. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  980. continue;
  981. e->last_pass = pass;
  982. if (*p_result_idx < p_result_max) {
  983. p_result_array[*p_result_idx] = e->userdata;
  984. if (p_subindex_array)
  985. p_subindex_array[*p_result_idx] = e->subindex;
  986. (*p_result_idx)++;
  987. } else {
  988. return; // pointless to continue
  989. }
  990. }
  991. }
  992. #endif
  993. }
  994. if (use_pairs && !p_octant->pairable_elements.empty()) {
  995. #ifdef OCTREE_USE_CACHED_LISTS
  996. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  997. p_octant->update_cached_lists();
  998. int num_elements = p_octant->clist_pairable.elements.size();
  999. for (int n = 0; n < num_elements; n++) {
  1000. const AABB &aabb = p_octant->clist_pairable.aabbs[n];
  1001. Element *e = p_octant->clist_pairable.elements[n];
  1002. if (p_aabb.intersects_inclusive(aabb)) {
  1003. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1004. continue;
  1005. e->last_pass = pass;
  1006. if (*p_result_idx < p_result_max) {
  1007. p_result_array[*p_result_idx] = e->userdata;
  1008. if (p_subindex_array)
  1009. p_subindex_array[*p_result_idx] = e->subindex;
  1010. (*p_result_idx)++;
  1011. } else {
  1012. return; // pointless to continue
  1013. }
  1014. }
  1015. }
  1016. #else
  1017. typename List<Element *, AL>::Element *I;
  1018. I = p_octant->pairable_elements.front();
  1019. for (; I; I = I->next()) {
  1020. Element *e = I->get();
  1021. const AABB &aabb = e->aabb;
  1022. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1023. continue;
  1024. e->last_pass = pass;
  1025. if (p_aabb.intersects_inclusive(aabb)) {
  1026. if (*p_result_idx < p_result_max) {
  1027. p_result_array[*p_result_idx] = e->userdata;
  1028. if (p_subindex_array)
  1029. p_subindex_array[*p_result_idx] = e->subindex;
  1030. (*p_result_idx)++;
  1031. } else {
  1032. return; // pointless to continue
  1033. }
  1034. }
  1035. }
  1036. #endif
  1037. }
  1038. for (int i = 0; i < 8; i++) {
  1039. if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_inclusive(p_aabb)) {
  1040. _cull_aabb(p_octant->children[i], p_aabb, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
  1041. }
  1042. }
  1043. }
  1044. 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) {
  1045. if (*p_result_idx == p_result_max)
  1046. return; //pointless
  1047. if (!p_octant->elements.empty()) {
  1048. #ifdef OCTREE_USE_CACHED_LISTS
  1049. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  1050. p_octant->update_cached_lists();
  1051. int num_elements = p_octant->clist.elements.size();
  1052. for (int n = 0; n < num_elements; n++) {
  1053. const AABB &aabb = p_octant->clist.aabbs[n];
  1054. Element *e = p_octant->clist.elements[n];
  1055. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1056. continue;
  1057. e->last_pass = pass;
  1058. if (aabb.intersects_segment(p_from, p_to)) {
  1059. if (*p_result_idx < p_result_max) {
  1060. p_result_array[*p_result_idx] = e->userdata;
  1061. if (p_subindex_array)
  1062. p_subindex_array[*p_result_idx] = e->subindex;
  1063. (*p_result_idx)++;
  1064. } else {
  1065. return; // pointless to continue
  1066. }
  1067. }
  1068. }
  1069. #else
  1070. typename List<Element *, AL>::Element *I;
  1071. I = p_octant->elements.front();
  1072. for (; I; I = I->next()) {
  1073. Element *e = I->get();
  1074. const AABB &aabb = e->aabb;
  1075. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1076. continue;
  1077. e->last_pass = pass;
  1078. if (aabb.intersects_segment(p_from, p_to)) {
  1079. if (*p_result_idx < p_result_max) {
  1080. p_result_array[*p_result_idx] = e->userdata;
  1081. if (p_subindex_array)
  1082. p_subindex_array[*p_result_idx] = e->subindex;
  1083. (*p_result_idx)++;
  1084. } else {
  1085. return; // pointless to continue
  1086. }
  1087. }
  1088. }
  1089. #endif
  1090. }
  1091. if (use_pairs && !p_octant->pairable_elements.empty()) {
  1092. #ifdef OCTREE_USE_CACHED_LISTS
  1093. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  1094. p_octant->update_cached_lists();
  1095. int num_elements = p_octant->clist_pairable.elements.size();
  1096. for (int n = 0; n < num_elements; n++) {
  1097. const AABB &aabb = p_octant->clist_pairable.aabbs[n];
  1098. Element *e = p_octant->clist_pairable.elements[n];
  1099. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1100. continue;
  1101. e->last_pass = pass;
  1102. if (aabb.intersects_segment(p_from, p_to)) {
  1103. if (*p_result_idx < p_result_max) {
  1104. p_result_array[*p_result_idx] = e->userdata;
  1105. if (p_subindex_array)
  1106. p_subindex_array[*p_result_idx] = e->subindex;
  1107. (*p_result_idx)++;
  1108. } else {
  1109. return; // pointless to continue
  1110. }
  1111. }
  1112. }
  1113. #else
  1114. typename List<Element *, AL>::Element *I;
  1115. I = p_octant->pairable_elements.front();
  1116. for (; I; I = I->next()) {
  1117. Element *e = I->get();
  1118. const AABB &aabb = e->aabb;
  1119. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1120. continue;
  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. (*p_result_idx)++;
  1128. } else {
  1129. return; // pointless to continue
  1130. }
  1131. }
  1132. }
  1133. #endif
  1134. }
  1135. for (int i = 0; i < 8; i++) {
  1136. if (p_octant->children[i] && p_octant->children[i]->aabb.intersects_segment(p_from, p_to)) {
  1137. _cull_segment(p_octant->children[i], p_from, p_to, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
  1138. }
  1139. }
  1140. }
  1141. 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) {
  1142. if (*p_result_idx == p_result_max)
  1143. return; //pointless
  1144. if (!p_octant->elements.empty()) {
  1145. #ifdef OCTREE_USE_CACHED_LISTS
  1146. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  1147. p_octant->update_cached_lists();
  1148. int num_elements = p_octant->clist.elements.size();
  1149. for (int n = 0; n < num_elements; n++) {
  1150. const AABB &aabb = p_octant->clist.aabbs[n];
  1151. Element *e = p_octant->clist.elements[n];
  1152. if (aabb.has_point(p_point)) {
  1153. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1154. continue;
  1155. e->last_pass = pass;
  1156. if (*p_result_idx < p_result_max) {
  1157. p_result_array[*p_result_idx] = e->userdata;
  1158. if (p_subindex_array)
  1159. p_subindex_array[*p_result_idx] = e->subindex;
  1160. (*p_result_idx)++;
  1161. } else {
  1162. return; // pointless to continue
  1163. }
  1164. }
  1165. }
  1166. #else
  1167. typename List<Element *, AL>::Element *I;
  1168. I = p_octant->elements.front();
  1169. for (; I; I = I->next()) {
  1170. Element *e = I->get();
  1171. const AABB &aabb = e->aabb;
  1172. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1173. continue;
  1174. e->last_pass = pass;
  1175. if (aabb.has_point(p_point)) {
  1176. if (*p_result_idx < p_result_max) {
  1177. p_result_array[*p_result_idx] = e->userdata;
  1178. if (p_subindex_array)
  1179. p_subindex_array[*p_result_idx] = e->subindex;
  1180. (*p_result_idx)++;
  1181. } else {
  1182. return; // pointless to continue
  1183. }
  1184. }
  1185. }
  1186. #endif
  1187. }
  1188. if (use_pairs && !p_octant->pairable_elements.empty()) {
  1189. #ifdef OCTREE_USE_CACHED_LISTS
  1190. // make sure cached list of element pointers and aabbs is up to date if this octant is dirty
  1191. p_octant->update_cached_lists();
  1192. int num_elements = p_octant->clist_pairable.elements.size();
  1193. for (int n = 0; n < num_elements; n++) {
  1194. const AABB &aabb = p_octant->clist_pairable.aabbs[n];
  1195. Element *e = p_octant->clist_pairable.elements[n];
  1196. if (aabb.has_point(p_point)) {
  1197. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1198. continue;
  1199. e->last_pass = pass;
  1200. if (*p_result_idx < p_result_max) {
  1201. p_result_array[*p_result_idx] = e->userdata;
  1202. if (p_subindex_array)
  1203. p_subindex_array[*p_result_idx] = e->subindex;
  1204. (*p_result_idx)++;
  1205. } else {
  1206. return; // pointless to continue
  1207. }
  1208. }
  1209. }
  1210. #else
  1211. typename List<Element *, AL>::Element *I;
  1212. I = p_octant->pairable_elements.front();
  1213. for (; I; I = I->next()) {
  1214. Element *e = I->get();
  1215. const AABB &aabb = e->aabb;
  1216. if (e->last_pass == pass || (use_pairs && !(e->pairable_type & p_mask)))
  1217. continue;
  1218. e->last_pass = pass;
  1219. if (aabb.has_point(p_point)) {
  1220. if (*p_result_idx < p_result_max) {
  1221. p_result_array[*p_result_idx] = e->userdata;
  1222. if (p_subindex_array)
  1223. p_subindex_array[*p_result_idx] = e->subindex;
  1224. (*p_result_idx)++;
  1225. } else {
  1226. return; // pointless to continue
  1227. }
  1228. }
  1229. }
  1230. #endif
  1231. }
  1232. for (int i = 0; i < 8; i++) {
  1233. //could be optimized..
  1234. if (p_octant->children[i] && p_octant->children[i]->aabb.has_point(p_point)) {
  1235. _cull_point(p_octant->children[i], p_point, p_result_array, p_result_idx, p_result_max, p_subindex_array, p_mask);
  1236. }
  1237. }
  1238. }
  1239. OCTREE_FUNC(int)::cull_convex(const Vector<Plane> &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask) {
  1240. if (!root || p_convex.size() == 0)
  1241. return 0;
  1242. Vector<Vector3> convex_points = Geometry::compute_convex_mesh_points(&p_convex[0], p_convex.size());
  1243. if (convex_points.size() == 0)
  1244. return 0;
  1245. int result_count = 0;
  1246. pass++;
  1247. _CullConvexData cdata;
  1248. cdata.planes = &p_convex[0];
  1249. cdata.plane_count = p_convex.size();
  1250. cdata.points = &convex_points[0];
  1251. cdata.point_count = convex_points.size();
  1252. cdata.result_array = p_result_array;
  1253. cdata.result_max = p_result_max;
  1254. cdata.result_idx = &result_count;
  1255. cdata.mask = p_mask;
  1256. _cull_convex(root, &cdata);
  1257. return result_count;
  1258. }
  1259. 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) {
  1260. if (!root)
  1261. return 0;
  1262. int result_count = 0;
  1263. pass++;
  1264. _cull_aabb(root, p_aabb, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
  1265. return result_count;
  1266. }
  1267. 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) {
  1268. if (!root)
  1269. return 0;
  1270. int result_count = 0;
  1271. pass++;
  1272. _cull_segment(root, p_from, p_to, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
  1273. return result_count;
  1274. }
  1275. 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) {
  1276. if (!root)
  1277. return 0;
  1278. int result_count = 0;
  1279. pass++;
  1280. _cull_point(root, p_point, p_result_array, &result_count, p_result_max, p_subindex_array, p_mask);
  1281. return result_count;
  1282. }
  1283. OCTREE_FUNC(void)::set_pair_callback(PairCallback p_callback, void *p_userdata) {
  1284. pair_callback = p_callback;
  1285. pair_callback_userdata = p_userdata;
  1286. }
  1287. OCTREE_FUNC(void)::set_unpair_callback(UnpairCallback p_callback, void *p_userdata) {
  1288. unpair_callback = p_callback;
  1289. unpair_callback_userdata = p_userdata;
  1290. }
  1291. OCTREE_FUNC_CONSTRUCTOR::OCTREE_CLASS_NAME(real_t p_unit_size) {
  1292. last_element_id = 1;
  1293. pass = 1;
  1294. unit_size = p_unit_size;
  1295. root = NULL;
  1296. octant_count = 0;
  1297. pair_count = 0;
  1298. octant_elements_limit = OCTREE_DEFAULT_OCTANT_LIMIT;
  1299. pair_callback = NULL;
  1300. unpair_callback = NULL;
  1301. pair_callback_userdata = NULL;
  1302. unpair_callback_userdata = NULL;
  1303. }
  1304. #ifdef TOOLS_ENABLED
  1305. OCTREE_FUNC(String)::debug_aabb_to_string(const AABB &aabb) const {
  1306. String sz;
  1307. sz = "( " + String(aabb.position);
  1308. sz += " ) - ( ";
  1309. Vector3 max = aabb.position + aabb.size;
  1310. sz += String(max) + " )";
  1311. return sz;
  1312. }
  1313. OCTREE_FUNC(void)::debug_octants() {
  1314. if (root)
  1315. debug_octant(*root);
  1316. }
  1317. OCTREE_FUNC(void)::debug_octant(const Octant &oct, int depth) {
  1318. String sz = "";
  1319. for (int d = 0; d < depth; d++)
  1320. sz += "\t";
  1321. sz += "Octant " + debug_aabb_to_string(oct.aabb);
  1322. sz += "\tnum_children " + itos(oct.children_count);
  1323. sz += ", num_eles " + itos(oct.elements.size());
  1324. sz += ", num_paired_eles" + itos(oct.pairable_elements.size());
  1325. print_line(sz);
  1326. for (int n = 0; n < 8; n++) {
  1327. const Octant *pChild = oct.children[n];
  1328. if (pChild) {
  1329. debug_octant(*pChild, depth + 1);
  1330. }
  1331. }
  1332. }
  1333. #endif // TOOLS_ENABLED
  1334. #undef OCTREE_FUNC