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