NTreeHelper.cc

Go to the documentation of this file.
00001 #include "NTreeHelper.h"
00002 
00003 NTreeScope::NTreeScope()
00004 {
00005     size = -1;
00006 }
00007 
00008 NTreeScope::NTreeScope(const Vector2D& _origin, const double _size) :
00009     origin(_origin), size(_size)
00010 {
00011     // Boundary checking?
00012 }
00013 
00014 void NTreeScope::resize(const Vector2D& _origin, const double _size)
00015 {
00016     origin = _origin;
00017     size = _size;
00018 }
00019 
00020 bool NTreeScope::contains(const Vector2D& point) const
00021 {
00022     if( !isValid() ) return false;
00023     return origin.xyMaxDistance(point)*2.0 <= size;
00024 }
00025 
00026 NTreeScope NTreeScope::getSubScope( unsigned int quadrant ) const
00027 {
00028     if( !isValid() ) return NTreeScope();
00029 
00030     Vector2D newOrigin = origin;
00031     double newSize = size/2.0;
00032     if( quadrant < 2 ) {
00033         // right half
00034         newOrigin.x += newSize / 2.0;
00035     } else {
00036         newOrigin.x -= newSize / 2.0;
00037     }
00038     if( quadrant == 0 || quadrant == 3 ) {
00039         // upper half
00040         newOrigin.y += newSize / 2.0;
00041     } else {
00042         newOrigin.y -= newSize / 2.0;
00043     }
00044     return NTreeScope( newOrigin, newSize );
00045 }
00046 
00047 bool operator<(const NTreeScope& a, const NTreeScope& b)
00048 {
00049     // for sorting only. This results in the biggest scope comming first
00050     if( a.size == b.size ) {
00051         return a.origin < b.origin;
00052     }
00053     return a.size > b.size;
00054 }
00055 
00056 bool operator==(const NTreeScope& a, const NTreeScope& b)
00057 {
00058     return a.origin == b.origin && a.size == b.size;
00059 }
00060 
00061 std::ostream& operator<<(std::ostream& Stream, const NTreeScope& scope)
00062 {
00063     Stream << "[" << scope.origin << " - " << scope.size << "]";
00064     return Stream;
00065 }
00066 
00067 NTreeGroup::NTreeGroup(const NTreeScope& _scope) :
00068     scope(_scope)
00069 {
00070     dividePending = false;
00071 }
00072 
00073 NTreeGroup::NTreeGroup(const Vector2D& _origin, const double _size) :
00074     scope(_origin,_size)
00075 {
00076     dividePending = false;
00077 }
00078 
00079 bool NTreeGroup::isInScope(const Vector2D& point) const
00080 {
00081     return scope.contains(point);
00082 }
00083 
00084 bool operator<(const NTreeGroup& a, const NTreeGroup& b)
00085 {
00086     return a.scope < b.scope;
00087 }
00088 
00089 bool operator==(const NTreeGroup& a, const NTreeGroup& b)
00090 {
00091     return a.scope == b.scope;
00092 }
00093 
00094 std::ostream& operator<<(std::ostream& Stream, const NTreeGroup& group)
00095 {
00096     Stream << group.scope << " Leader: " << group.leader;
00097     for( std::set<NodeHandle>::iterator it = group.members.begin(); it != group.members.end(); ++it ){
00098         Stream << "\n" << it->getIp();
00099     }
00100     return Stream;
00101 }
00102 
00103 
00104 NTreeNode::NTreeNode(const NTreeScope& _scope) :
00105     scope(_scope)
00106 {
00107     for( unsigned int i = 0; i < 4; ++i ){
00108         aggChildCount[i] = 0;
00109     }
00110     group = 0;
00111     parentIsRoot = false;
00112 }
00113 
00114 NTreeNode::NTreeNode(const Vector2D& _origin, const double _size) :
00115     scope(_origin,_size)
00116 {
00117     for( unsigned int i = 0; i < 4; ++i ){
00118         aggChildCount[i] = 0;
00119     }
00120     group = 0;
00121     parentIsRoot = false;
00122 }
00123 
00124 bool NTreeNode::isInScope(const Vector2D& point) const
00125 {
00126     return scope.contains(point);
00127 }
00128 
00129 
00130 const NodeHandle& NTreeNode::getChildForPos( const Vector2D& pos ) const
00131 {
00132     if (!isInScope( pos ) ) return NodeHandle::UNSPECIFIED_NODE;
00133     return children[ scope.origin.getQuadrant(pos) ];
00134 }
00135 
00136 bool operator==(const NTreeNode& a, const NTreeNode& b)
00137 {
00138     return a.scope== b.scope;
00139 }
00140 
00141 bool operator<(const NTreeNode& a, const NTreeNode& b)
00142 {
00143     return a.scope < b.scope;
00144 }
00145 
00146 std::ostream& operator<<(std::ostream& Stream, const NTreeNode& node)
00147 {
00148     Stream << node.scope << "\nParent: " << node.parent.getIp();
00149     if( node.group ) {
00150         Stream << "\nNode is leaf";
00151     } else {
00152         for( unsigned int i = 0; i < 4; ++i ){
00153             Stream << "\nChild " << i << ": " << node.children[i];
00154         }
00155     }
00156     return Stream;
00157 }
00158 
00159 NTreePingContext::NTreePingContext(const NTreeScope& _scope, unsigned int _quadrant) :
00160         nodeScope(_scope), quadrant(_quadrant)
00161 {
00162 }
00163