PageRenderTime 32ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/lloctree.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 783 lines | 580 code | 125 blank | 78 comment | 99 complexity | 0e107fa818ed930cef58c988380ad24d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lloctree.h
  3. * @brief Octree declaration.
  4. *
  5. * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLOCTREE_H
  27. #define LL_LLOCTREE_H
  28. #include "lltreenode.h"
  29. #include "v3math.h"
  30. #include "llvector4a.h"
  31. #include <vector>
  32. #include <set>
  33. #define OCT_ERRS LL_WARNS("OctreeErrors")
  34. extern U32 gOctreeMaxCapacity;
  35. /*#define LL_OCTREE_PARANOIA_CHECK 0
  36. #if LL_DARWIN
  37. #define LL_OCTREE_MAX_CAPACITY 32
  38. #else
  39. #define LL_OCTREE_MAX_CAPACITY 128
  40. #endif*/
  41. template <class T> class LLOctreeNode;
  42. template <class T>
  43. class LLOctreeListener: public LLTreeListener<T>
  44. {
  45. public:
  46. typedef LLTreeListener<T> BaseType;
  47. typedef LLOctreeNode<T> oct_node;
  48. virtual void handleChildAddition(const oct_node* parent, oct_node* child) = 0;
  49. virtual void handleChildRemoval(const oct_node* parent, const oct_node* child) = 0;
  50. };
  51. template <class T>
  52. class LLOctreeTraveler
  53. {
  54. public:
  55. virtual void traverse(const LLOctreeNode<T>* node);
  56. virtual void visit(const LLOctreeNode<T>* branch) = 0;
  57. };
  58. template <class T>
  59. class LLOctreeTravelerDepthFirst : public LLOctreeTraveler<T>
  60. {
  61. public:
  62. virtual void traverse(const LLOctreeNode<T>* node);
  63. };
  64. template <class T>
  65. class LLOctreeNode : public LLTreeNode<T>
  66. {
  67. public:
  68. typedef LLOctreeTraveler<T> oct_traveler;
  69. typedef LLTreeTraveler<T> tree_traveler;
  70. typedef typename std::set<LLPointer<T> > element_list;
  71. typedef typename element_list::iterator element_iter;
  72. typedef typename element_list::const_iterator const_element_iter;
  73. typedef typename std::vector<LLTreeListener<T>*>::iterator tree_listener_iter;
  74. typedef typename std::vector<LLOctreeNode<T>* > child_list;
  75. typedef LLTreeNode<T> BaseType;
  76. typedef LLOctreeNode<T> oct_node;
  77. typedef LLOctreeListener<T> oct_listener;
  78. /*void* operator new(size_t size)
  79. {
  80. return ll_aligned_malloc_16(size);
  81. }
  82. void operator delete(void* ptr)
  83. {
  84. ll_aligned_free_16(ptr);
  85. }*/
  86. LLOctreeNode( const LLVector4a& center,
  87. const LLVector4a& size,
  88. BaseType* parent,
  89. U8 octant = 255)
  90. : mParent((oct_node*)parent),
  91. mOctant(octant)
  92. {
  93. mCenter = center;
  94. mSize = size;
  95. updateMinMax();
  96. if ((mOctant == 255) && mParent)
  97. {
  98. mOctant = ((oct_node*) mParent)->getOctant(mCenter);
  99. }
  100. mElementCount = 0;
  101. clearChildren();
  102. }
  103. virtual ~LLOctreeNode()
  104. {
  105. BaseType::destroyListeners();
  106. for (U32 i = 0; i < getChildCount(); i++)
  107. {
  108. delete getChild(i);
  109. }
  110. }
  111. inline const BaseType* getParent() const { return mParent; }
  112. inline void setParent(BaseType* parent) { mParent = (oct_node*) parent; }
  113. inline const LLVector4a& getCenter() const { return mCenter; }
  114. inline const LLVector4a& getSize() const { return mSize; }
  115. inline void setCenter(const LLVector4a& center) { mCenter = center; }
  116. inline void setSize(const LLVector4a& size) { mSize = size; }
  117. inline oct_node* getNodeAt(T* data) { return getNodeAt(data->getPositionGroup(), data->getBinRadius()); }
  118. inline U8 getOctant() const { return mOctant; }
  119. inline const oct_node* getOctParent() const { return (const oct_node*) getParent(); }
  120. inline oct_node* getOctParent() { return (oct_node*) getParent(); }
  121. U8 getOctant(const LLVector4a& pos) const //get the octant pos is in
  122. {
  123. return (U8) (pos.greaterThan(mCenter).getGatheredBits() & 0x7);
  124. }
  125. inline bool isInside(const LLVector4a& pos, const F32& rad) const
  126. {
  127. return rad <= mSize[0]*2.f && isInside(pos);
  128. }
  129. inline bool isInside(T* data) const
  130. {
  131. return isInside(data->getPositionGroup(), data->getBinRadius());
  132. }
  133. bool isInside(const LLVector4a& pos) const
  134. {
  135. S32 gt = pos.greaterThan(mMax).getGatheredBits() & 0x7;
  136. if (gt)
  137. {
  138. return false;
  139. }
  140. S32 lt = pos.lessEqual(mMin).getGatheredBits() & 0x7;
  141. if (lt)
  142. {
  143. return false;
  144. }
  145. return true;
  146. }
  147. void updateMinMax()
  148. {
  149. mMax.setAdd(mCenter, mSize);
  150. mMin.setSub(mCenter, mSize);
  151. }
  152. inline oct_listener* getOctListener(U32 index)
  153. {
  154. return (oct_listener*) BaseType::getListener(index);
  155. }
  156. inline bool contains(T* xform)
  157. {
  158. return contains(xform->getBinRadius());
  159. }
  160. bool contains(F32 radius)
  161. {
  162. if (mParent == NULL)
  163. { //root node contains nothing
  164. return false;
  165. }
  166. F32 size = mSize[0];
  167. F32 p_size = size * 2.f;
  168. return (radius <= 0.001f && size <= 0.001f) ||
  169. (radius <= p_size && radius > size);
  170. }
  171. static void pushCenter(LLVector4a &center, const LLVector4a &size, const T* data)
  172. {
  173. const LLVector4a& pos = data->getPositionGroup();
  174. LLVector4Logical gt = pos.greaterThan(center);
  175. LLVector4a up;
  176. up = _mm_and_ps(size, gt);
  177. LLVector4a down;
  178. down = _mm_andnot_ps(gt, size);
  179. center.add(up);
  180. center.sub(down);
  181. }
  182. void accept(oct_traveler* visitor) { visitor->visit(this); }
  183. virtual bool isLeaf() const { return mChild.empty(); }
  184. U32 getElementCount() const { return mElementCount; }
  185. element_list& getData() { return mData; }
  186. const element_list& getData() const { return mData; }
  187. U32 getChildCount() const { return mChildCount; }
  188. oct_node* getChild(U32 index) { return mChild[index]; }
  189. const oct_node* getChild(U32 index) const { return mChild[index]; }
  190. child_list& getChildren() { return mChild; }
  191. const child_list& getChildren() const { return mChild; }
  192. void accept(tree_traveler* visitor) const { visitor->visit(this); }
  193. void accept(oct_traveler* visitor) const { visitor->visit(this); }
  194. void validateChildMap()
  195. {
  196. for (U32 i = 0; i < 8; i++)
  197. {
  198. U8 idx = mChildMap[i];
  199. if (idx != 255)
  200. {
  201. LLOctreeNode<T>* child = mChild[idx];
  202. if (child->getOctant() != i)
  203. {
  204. llerrs << "Invalid child map, bad octant data." << llendl;
  205. }
  206. if (getOctant(child->getCenter()) != child->getOctant())
  207. {
  208. llerrs << "Invalid child octant compared to position data." << llendl;
  209. }
  210. }
  211. }
  212. }
  213. oct_node* getNodeAt(const LLVector4a& pos, const F32& rad)
  214. {
  215. LLOctreeNode<T>* node = this;
  216. if (node->isInside(pos, rad))
  217. {
  218. //do a quick search by octant
  219. U8 octant = node->getOctant(pos);
  220. //traverse the tree until we find a node that has no node
  221. //at the appropriate octant or is smaller than the object.
  222. //by definition, that node is the smallest node that contains
  223. // the data
  224. U8 next_node = node->mChildMap[octant];
  225. while (next_node != 255 && node->getSize()[0] >= rad)
  226. {
  227. node = node->getChild(next_node);
  228. octant = node->getOctant(pos);
  229. next_node = node->mChildMap[octant];
  230. }
  231. }
  232. else if (!node->contains(rad) && node->getParent())
  233. { //if we got here, data does not exist in this node
  234. return ((LLOctreeNode<T>*) node->getParent())->getNodeAt(pos, rad);
  235. }
  236. return node;
  237. }
  238. virtual bool insert(T* data)
  239. {
  240. if (data == NULL)
  241. {
  242. OCT_ERRS << "!!! INVALID ELEMENT ADDED TO OCTREE BRANCH !!!" << llendl;
  243. return false;
  244. }
  245. LLOctreeNode<T>* parent = getOctParent();
  246. //is it here?
  247. if (isInside(data->getPositionGroup()))
  248. {
  249. if ((getElementCount() < gOctreeMaxCapacity && contains(data->getBinRadius()) ||
  250. (data->getBinRadius() > getSize()[0] && parent && parent->getElementCount() >= gOctreeMaxCapacity)))
  251. { //it belongs here
  252. //if this is a redundant insertion, error out (should never happen)
  253. llassert(mData.find(data) == mData.end());
  254. mData.insert(data);
  255. BaseType::insert(data);
  256. mElementCount = mData.size();
  257. return true;
  258. }
  259. else
  260. {
  261. //find a child to give it to
  262. oct_node* child = NULL;
  263. for (U32 i = 0; i < getChildCount(); i++)
  264. {
  265. child = getChild(i);
  266. if (child->isInside(data->getPositionGroup()))
  267. {
  268. child->insert(data);
  269. return false;
  270. }
  271. }
  272. //it's here, but no kids are in the right place, make a new kid
  273. LLVector4a center = getCenter();
  274. LLVector4a size = getSize();
  275. size.mul(0.5f);
  276. //push center in direction of data
  277. LLOctreeNode<T>::pushCenter(center, size, data);
  278. // handle case where floating point number gets too small
  279. LLVector4a val;
  280. val.setSub(center, getCenter());
  281. val.setAbs(val);
  282. S32 lt = val.lessThan(LLVector4a::getEpsilon()).getGatheredBits() & 0x7;
  283. if( lt == 0x7 )
  284. {
  285. mData.insert(data);
  286. BaseType::insert(data);
  287. mElementCount = mData.size();
  288. return true;
  289. }
  290. #if LL_OCTREE_PARANOIA_CHECK
  291. if (getChildCount() == 8)
  292. {
  293. //this really isn't possible, something bad has happened
  294. OCT_ERRS << "Octree detected floating point error and gave up." << llendl;
  295. return false;
  296. }
  297. //make sure no existing node matches this position
  298. for (U32 i = 0; i < getChildCount(); i++)
  299. {
  300. if (mChild[i]->getCenter().equals3(center))
  301. {
  302. OCT_ERRS << "Octree detected duplicate child center and gave up." << llendl;
  303. return false;
  304. }
  305. }
  306. #endif
  307. //make the new kid
  308. child = new LLOctreeNode<T>(center, size, this);
  309. addChild(child);
  310. child->insert(data);
  311. }
  312. }
  313. else
  314. {
  315. //it's not in here, give it to the root
  316. OCT_ERRS << "Octree insertion failed, starting over from root!" << llendl;
  317. oct_node* node = this;
  318. while (parent)
  319. {
  320. node = parent;
  321. parent = node->getOctParent();
  322. }
  323. node->insert(data);
  324. }
  325. return false;
  326. }
  327. bool remove(T* data)
  328. {
  329. if (mData.find(data) != mData.end())
  330. { //we have data
  331. mData.erase(data);
  332. mElementCount = mData.size();
  333. notifyRemoval(data);
  334. checkAlive();
  335. return true;
  336. }
  337. else if (isInside(data))
  338. {
  339. oct_node* dest = getNodeAt(data);
  340. if (dest != this)
  341. {
  342. return dest->remove(data);
  343. }
  344. }
  345. //SHE'S GONE MISSING...
  346. //none of the children have it, let's just brute force this bastard out
  347. //starting with the root node (UGLY CODE COMETH!)
  348. oct_node* parent = getOctParent();
  349. oct_node* node = this;
  350. while (parent != NULL)
  351. {
  352. node = parent;
  353. parent = node->getOctParent();
  354. }
  355. //node is now root
  356. llwarns << "!!! OCTREE REMOVING FACE BY ADDRESS, SEVERE PERFORMANCE PENALTY |||" << llendl;
  357. node->removeByAddress(data);
  358. return true;
  359. }
  360. void removeByAddress(T* data)
  361. {
  362. if (mData.find(data) != mData.end())
  363. {
  364. mData.erase(data);
  365. mElementCount = mData.size();
  366. notifyRemoval(data);
  367. llwarns << "FOUND!" << llendl;
  368. checkAlive();
  369. return;
  370. }
  371. for (U32 i = 0; i < getChildCount(); i++)
  372. { //we don't contain data, so pass this guy down
  373. LLOctreeNode<T>* child = (LLOctreeNode<T>*) getChild(i);
  374. child->removeByAddress(data);
  375. }
  376. }
  377. void clearChildren()
  378. {
  379. mChild.clear();
  380. mChildCount = 0;
  381. U32* foo = (U32*) mChildMap;
  382. foo[0] = foo[1] = 0xFFFFFFFF;
  383. }
  384. void validate()
  385. {
  386. #if LL_OCTREE_PARANOIA_CHECK
  387. for (U32 i = 0; i < getChildCount(); i++)
  388. {
  389. mChild[i]->validate();
  390. if (mChild[i]->getParent() != this)
  391. {
  392. llerrs << "Octree child has invalid parent." << llendl;
  393. }
  394. }
  395. #endif
  396. }
  397. virtual bool balance()
  398. {
  399. return false;
  400. }
  401. void destroy()
  402. {
  403. for (U32 i = 0; i < getChildCount(); i++)
  404. {
  405. mChild[i]->destroy();
  406. delete mChild[i];
  407. }
  408. }
  409. void addChild(oct_node* child, BOOL silent = FALSE)
  410. {
  411. #if LL_OCTREE_PARANOIA_CHECK
  412. if (child->getSize().equals3(getSize()))
  413. {
  414. OCT_ERRS << "Child size is same as parent size!" << llendl;
  415. }
  416. for (U32 i = 0; i < getChildCount(); i++)
  417. {
  418. if(!mChild[i]->getSize().equals3(child->getSize()))
  419. {
  420. OCT_ERRS <<"Invalid octree child size." << llendl;
  421. }
  422. if (mChild[i]->getCenter().equals3(child->getCenter()))
  423. {
  424. OCT_ERRS <<"Duplicate octree child position." << llendl;
  425. }
  426. }
  427. if (mChild.size() >= 8)
  428. {
  429. OCT_ERRS <<"Octree node has too many children... why?" << llendl;
  430. }
  431. #endif
  432. mChildMap[child->getOctant()] = mChildCount;
  433. mChild.push_back(child);
  434. ++mChildCount;
  435. child->setParent(this);
  436. if (!silent)
  437. {
  438. for (U32 i = 0; i < this->getListenerCount(); i++)
  439. {
  440. oct_listener* listener = getOctListener(i);
  441. listener->handleChildAddition(this, child);
  442. }
  443. }
  444. }
  445. void removeChild(S32 index, BOOL destroy = FALSE)
  446. {
  447. for (U32 i = 0; i < this->getListenerCount(); i++)
  448. {
  449. oct_listener* listener = getOctListener(i);
  450. listener->handleChildRemoval(this, getChild(index));
  451. }
  452. if (destroy)
  453. {
  454. mChild[index]->destroy();
  455. delete mChild[index];
  456. }
  457. mChild.erase(mChild.begin() + index);
  458. --mChildCount;
  459. //rebuild child map
  460. U32* foo = (U32*) mChildMap;
  461. foo[0] = foo[1] = 0xFFFFFFFF;
  462. for (U32 i = 0; i < mChildCount; ++i)
  463. {
  464. mChildMap[mChild[i]->getOctant()] = i;
  465. }
  466. checkAlive();
  467. }
  468. void checkAlive()
  469. {
  470. if (getChildCount() == 0 && getElementCount() == 0)
  471. {
  472. oct_node* parent = getOctParent();
  473. if (parent)
  474. {
  475. parent->deleteChild(this);
  476. }
  477. }
  478. }
  479. void deleteChild(oct_node* node)
  480. {
  481. for (U32 i = 0; i < getChildCount(); i++)
  482. {
  483. if (getChild(i) == node)
  484. {
  485. removeChild(i, TRUE);
  486. return;
  487. }
  488. }
  489. OCT_ERRS << "Octree failed to delete requested child." << llendl;
  490. }
  491. protected:
  492. typedef enum
  493. {
  494. CENTER = 0,
  495. SIZE = 1,
  496. MAX = 2,
  497. MIN = 3
  498. } eDName;
  499. LLVector4a mCenter;
  500. LLVector4a mSize;
  501. LLVector4a mMax;
  502. LLVector4a mMin;
  503. oct_node* mParent;
  504. U8 mOctant;
  505. child_list mChild;
  506. U8 mChildMap[8];
  507. U32 mChildCount;
  508. element_list mData;
  509. U32 mElementCount;
  510. };
  511. //just like a regular node, except it might expand on insert and compress on balance
  512. template <class T>
  513. class LLOctreeRoot : public LLOctreeNode<T>
  514. {
  515. public:
  516. typedef LLOctreeNode<T> BaseType;
  517. typedef LLOctreeNode<T> oct_node;
  518. LLOctreeRoot(const LLVector4a& center,
  519. const LLVector4a& size,
  520. BaseType* parent)
  521. : BaseType(center, size, parent)
  522. {
  523. }
  524. bool balance()
  525. {
  526. if (this->getChildCount() == 1 &&
  527. !(this->mChild[0]->isLeaf()) &&
  528. this->mChild[0]->getElementCount() == 0)
  529. { //if we have only one child and that child is an empty branch, make that child the root
  530. oct_node* child = this->mChild[0];
  531. //make the root node look like the child
  532. this->setCenter(this->mChild[0]->getCenter());
  533. this->setSize(this->mChild[0]->getSize());
  534. this->updateMinMax();
  535. //reset root node child list
  536. this->clearChildren();
  537. //copy the child's children into the root node silently
  538. //(don't notify listeners of addition)
  539. for (U32 i = 0; i < child->getChildCount(); i++)
  540. {
  541. addChild(child->getChild(i), TRUE);
  542. }
  543. //destroy child
  544. child->clearChildren();
  545. delete child;
  546. return false;
  547. }
  548. return true;
  549. }
  550. // LLOctreeRoot::insert
  551. bool insert(T* data)
  552. {
  553. if (data == NULL)
  554. {
  555. OCT_ERRS << "!!! INVALID ELEMENT ADDED TO OCTREE ROOT !!!" << llendl;
  556. return false;
  557. }
  558. if (data->getBinRadius() > 4096.0)
  559. {
  560. OCT_ERRS << "!!! ELEMENT EXCEEDS MAXIMUM SIZE IN OCTREE ROOT !!!" << llendl;
  561. return false;
  562. }
  563. LLVector4a MAX_MAG;
  564. MAX_MAG.splat(1024.f*1024.f);
  565. const LLVector4a& v = data->getPositionGroup();
  566. LLVector4a val;
  567. val.setSub(v, BaseType::mCenter);
  568. val.setAbs(val);
  569. S32 lt = val.lessThan(MAX_MAG).getGatheredBits() & 0x7;
  570. if (lt != 0x7)
  571. {
  572. //OCT_ERRS << "!!! ELEMENT EXCEEDS RANGE OF SPATIAL PARTITION !!!" << llendl;
  573. return false;
  574. }
  575. if (this->getSize()[0] > data->getBinRadius() && isInside(data->getPositionGroup()))
  576. {
  577. //we got it, just act like a branch
  578. oct_node* node = getNodeAt(data);
  579. if (node == this)
  580. {
  581. LLOctreeNode<T>::insert(data);
  582. }
  583. else
  584. {
  585. node->insert(data);
  586. }
  587. }
  588. else if (this->getChildCount() == 0)
  589. {
  590. //first object being added, just wrap it up
  591. while (!(this->getSize()[0] > data->getBinRadius() && isInside(data->getPositionGroup())))
  592. {
  593. LLVector4a center, size;
  594. center = this->getCenter();
  595. size = this->getSize();
  596. LLOctreeNode<T>::pushCenter(center, size, data);
  597. this->setCenter(center);
  598. size.mul(2.f);
  599. this->setSize(size);
  600. this->updateMinMax();
  601. }
  602. LLOctreeNode<T>::insert(data);
  603. }
  604. else
  605. {
  606. while (!(this->getSize()[0] > data->getBinRadius() && isInside(data->getPositionGroup())))
  607. {
  608. //the data is outside the root node, we need to grow
  609. LLVector4a center(this->getCenter());
  610. LLVector4a size(this->getSize());
  611. //expand this node
  612. LLVector4a newcenter(center);
  613. LLOctreeNode<T>::pushCenter(newcenter, size, data);
  614. this->setCenter(newcenter);
  615. LLVector4a size2 = size;
  616. size2.mul(2.f);
  617. this->setSize(size2);
  618. this->updateMinMax();
  619. //copy our children to a new branch
  620. LLOctreeNode<T>* newnode = new LLOctreeNode<T>(center, size, this);
  621. for (U32 i = 0; i < this->getChildCount(); i++)
  622. {
  623. LLOctreeNode<T>* child = this->getChild(i);
  624. newnode->addChild(child);
  625. }
  626. //clear our children and add the root copy
  627. this->clearChildren();
  628. addChild(newnode);
  629. }
  630. //insert the data
  631. insert(data);
  632. }
  633. return false;
  634. }
  635. };
  636. //========================
  637. // LLOctreeTraveler
  638. //========================
  639. template <class T>
  640. void LLOctreeTraveler<T>::traverse(const LLOctreeNode<T>* node)
  641. {
  642. node->accept(this);
  643. for (U32 i = 0; i < node->getChildCount(); i++)
  644. {
  645. traverse(node->getChild(i));
  646. }
  647. }
  648. template <class T>
  649. void LLOctreeTravelerDepthFirst<T>::traverse(const LLOctreeNode<T>* node)
  650. {
  651. for (U32 i = 0; i < node->getChildCount(); i++)
  652. {
  653. traverse(node->getChild(i));
  654. }
  655. node->accept(this);
  656. }
  657. #endif