OverSim
NTreeHelper.cc
Go to the documentation of this file.
1 #include "NTreeHelper.h"
2 
4 {
5  size = -1;
6 }
7 
8 NTreeScope::NTreeScope(const Vector2D& _origin, const double _size) :
9  origin(_origin), size(_size)
10 {
11  // Boundary checking?
12 }
13 
14 void NTreeScope::resize(const Vector2D& _origin, const double _size)
15 {
16  origin = _origin;
17  size = _size;
18 }
19 
20 bool NTreeScope::contains(const Vector2D& point) const
21 {
22  if( !isValid() ) return false;
23  return origin.xyMaxDistance(point)*2.0 <= size;
24 }
25 
26 NTreeScope NTreeScope::getSubScope( unsigned int quadrant ) const
27 {
28  if( !isValid() ) return NTreeScope();
29 
30  Vector2D newOrigin = origin;
31  double newSize = size/2.0;
32  if( quadrant < 2 ) {
33  // right half
34  newOrigin.x += newSize / 2.0;
35  } else {
36  newOrigin.x -= newSize / 2.0;
37  }
38  if( quadrant == 0 || quadrant == 3 ) {
39  // upper half
40  newOrigin.y += newSize / 2.0;
41  } else {
42  newOrigin.y -= newSize / 2.0;
43  }
44  return NTreeScope( newOrigin, newSize );
45 }
46 
47 bool operator<(const NTreeScope& a, const NTreeScope& b)
48 {
49  // for sorting only. This results in the biggest scope comming first
50  if( a.size == b.size ) {
51  return a.origin < b.origin;
52  }
53  return a.size > b.size;
54 }
55 
56 bool operator==(const NTreeScope& a, const NTreeScope& b)
57 {
58  return a.origin == b.origin && a.size == b.size;
59 }
60 
61 std::ostream& operator<<(std::ostream& Stream, const NTreeScope& scope)
62 {
63  Stream << "[" << scope.origin << " - " << scope.size << "]";
64  return Stream;
65 }
66 
68  scope(_scope)
69 {
70  dividePending = false;
71 }
72 
73 NTreeGroup::NTreeGroup(const Vector2D& _origin, const double _size) :
74  scope(_origin,_size)
75 {
76  dividePending = false;
77 }
78 
79 bool NTreeGroup::isInScope(const Vector2D& point) const
80 {
81  return scope.contains(point);
82 }
83 
84 bool operator<(const NTreeGroup& a, const NTreeGroup& b)
85 {
86  return a.scope < b.scope;
87 }
88 
89 bool operator==(const NTreeGroup& a, const NTreeGroup& b)
90 {
91  return a.scope == b.scope;
92 }
93 
94 std::ostream& operator<<(std::ostream& Stream, const NTreeGroup& group)
95 {
96  Stream << group.scope << " Leader: " << group.leader;
97  for( std::set<NodeHandle>::iterator it = group.members.begin(); it != group.members.end(); ++it ){
98  Stream << "\n" << it->getIp();
99  }
100  return Stream;
101 }
102 
103 
105  scope(_scope)
106 {
107  for( unsigned int i = 0; i < 4; ++i ){
108  aggChildCount[i] = 0;
109  }
110  group = 0;
111  parentIsRoot = false;
112 }
113 
114 NTreeNode::NTreeNode(const Vector2D& _origin, const double _size) :
115  scope(_origin,_size)
116 {
117  for( unsigned int i = 0; i < 4; ++i ){
118  aggChildCount[i] = 0;
119  }
120  group = 0;
121  parentIsRoot = false;
122 }
123 
124 bool NTreeNode::isInScope(const Vector2D& point) const
125 {
126  return scope.contains(point);
127 }
128 
129 
131 {
132  if (!isInScope( pos ) ) return NodeHandle::UNSPECIFIED_NODE;
133  return children[ scope.origin.getQuadrant(pos) ];
134 }
135 
136 bool operator==(const NTreeNode& a, const NTreeNode& b)
137 {
138  return a.scope== b.scope;
139 }
140 
141 bool operator<(const NTreeNode& a, const NTreeNode& b)
142 {
143  return a.scope < b.scope;
144 }
145 
146 std::ostream& operator<<(std::ostream& Stream, const NTreeNode& node)
147 {
148  Stream << node.scope << "\nParent: " << node.parent.getIp();
149  if( node.group ) {
150  Stream << "\nNode is leaf";
151  } else {
152  for( unsigned int i = 0; i < 4; ++i ){
153  Stream << "\nChild " << i << ": " << node.children[i];
154  }
155  }
156  return Stream;
157 }
158 
159 NTreePingContext::NTreePingContext(const NTreeScope& _scope, unsigned int _quadrant) :
160  nodeScope(_scope), quadrant(_quadrant)
161 {
162 }
163