00001
00008 #include "TreeNode.h"
00009 #include "TreeNodePointer.h"
00010
00011 namespace ot {
00012
00013 void appendOctantsAtLevel(const ot::TreeNodePointer & ptrOctree,
00014 std::vector<ot::TreeNode> & wList, unsigned int lev) {
00015 if(ptrOctree.m_tnMe.getLevel() == lev) {
00016 wList.push_back(ptrOctree.m_tnMe);
00017 } else if(ptrOctree.m_tnMe.getLevel() < lev) {
00018 if(ptrOctree.m_tnpMyChildren) {
00019 for(int i = 0; i < 8; i++) {
00020 appendOctantsAtLevel(ptrOctree.m_tnpMyChildren[i], wList, lev);
00021 }
00022 }
00023 }
00024 }
00025
00026 void convertLinearToPointer(const std::vector<ot::TreeNode> & linOct,
00027 ot::TreeNodePointer & ptrOct) {
00028
00029 assert(!(linOct.empty()));
00030
00031 unsigned int dim = linOct[0].getDim();
00032 unsigned int maxDepth = linOct[0].getMaxDepth();
00033
00034
00035 ptrOct.m_tnMe = ot::TreeNode(dim, maxDepth);
00036 ptrOct.m_tnpMyChildren = NULL;
00037
00038 for(unsigned int i = 0; i < linOct.size(); i++) {
00039 addOctantToTreeNodePointer(ptrOct, linOct[i]);
00040 }
00041 }
00042
00043
00044 void addOctantToTreeNodePointer(ot::TreeNodePointer & ptrOct, const ot::TreeNode & octant) {
00045 if(ptrOct.m_tnpMyChildren == NULL) {
00046 ptrOct.m_tnpMyChildren = new ot::TreeNodePointer[8];
00047 std::vector<ot::TreeNode> tmpOcts;
00048 ptrOct.m_tnMe.addChildren(tmpOcts);
00049 for(unsigned int i = 0; i < 8; i++) {
00050 ptrOct.m_tnpMyChildren[i].m_tnMe = tmpOcts[i];
00051 ptrOct.m_tnpMyChildren[i].m_tnpMyChildren = NULL;
00052 }
00053 }
00054 for(unsigned int i = 0; i < 8; i++) {
00055 if(ptrOct.m_tnpMyChildren[i].m_tnMe.isAncestor(octant)) {
00056 addOctantToTreeNodePointer(ptrOct.m_tnpMyChildren[i], octant);
00057 break;
00058 }
00059 }
00060 }
00061
00062 void findOctantOrFinestAncestor(ot::TreeNodePointer & octree,
00063 const ot::TreeNode & key, ot::TreeNodePointer* & result) {
00064 if(octree.m_tnMe.isAncestor(key)) {
00065 if(octree.m_tnpMyChildren) {
00066 for(unsigned int i = 0; i < 8; i++) {
00067 if(octree.m_tnpMyChildren[i].m_tnMe.isAncestor(key)) {
00068 findOctantOrFinestAncestor(octree.m_tnpMyChildren[i], key, result);
00069 break;
00070 } else if (octree.m_tnpMyChildren[i].m_tnMe == key) {
00071 result = &(octree.m_tnpMyChildren[i]);
00072 break;
00073 }
00074 }
00075 } else {
00076 result = &octree;
00077 }
00078 } else if(octree.m_tnMe == key) {
00079 result = &octree;
00080 } else {
00081 assert(false);
00082 }
00083 }
00084
00085
00086 void convertPointerToLinear(std::vector<ot::TreeNode> & linOct,
00087 const ot::TreeNodePointer & ptrOct) {
00088 if(ptrOct.m_tnpMyChildren) {
00089 for(unsigned int i = 0; i < 8; i++) {
00090 convertPointerToLinear(linOct, ptrOct.m_tnpMyChildren[i]);
00091 }
00092 } else {
00093 linOct.push_back(ptrOct.m_tnMe);
00094 }
00095 }
00096
00097 void deleteTreeNodePointer(ot::TreeNodePointer & ptrOct) {
00098 if(ptrOct.m_tnpMyChildren) {
00099 for(unsigned int i = 0; i < 8; i++) {
00100 deleteTreeNodePointer((ptrOct.m_tnpMyChildren)[i]);
00101 }
00102 delete [] (ptrOct.m_tnpMyChildren);
00103 ptrOct.m_tnpMyChildren = NULL;
00104 }
00105 }
00106
00107 }
00108
00109