Belle II Software  release-08-01-10
QuadTreeNode.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 
11 #include <framework/logging/Logger.h>
12 
13 #include <vector>
14 
15 namespace Belle2 {
20  namespace TrackFindingCDC {
21 
28  template<typename AX, typename AY, class AItem>
29  class QuadTreeNode {
30 
31  public:
34 
36  using XSpan = std::array<AX, 2>;
37 
39  using YSpan = std::array<AY, 2>;
40 
42  using XBinBounds = std::array<AX, 4>;
43 
45  using YBinBounds = std::array<AY, 4>;
46 
48  using Children = std::vector<This>;
49 
56  // cppcheck-suppress passedByValue
57  QuadTreeNode(XSpan xSpan, YSpan ySpan, int level, This* parent)
58  : m_xBinBounds {
59  xSpan[0],
60  xSpan[0] + (xSpan[1] - xSpan[0]) / 2,
61  xSpan[1] - (xSpan[1] - xSpan[0]) / 2,
62  xSpan[1]
63  }
64  , m_yBinBounds(
65  {
66  ySpan[0],
67  ySpan[0] + (ySpan[1] - ySpan[0]) / 2,
68  ySpan[1] - (ySpan[1] - ySpan[0]) / 2,
69  ySpan[1]
70  })
71  , m_level(level)
72  , m_parent(level > 0 ? parent : nullptr)
73  , m_filled(false)
74  {
75  B2ASSERT("QuadTree datastructure only supports levels < 255", level < 255);
76  }
77 
79  void insertItem(AItem* item)
80  {
81  m_items.push_back(item);
82  }
83 
85  void reserveItems(int nItems)
86  {
87  m_items.reserve(nItems);
88  }
89 
91  std::vector<AItem*>& getItems()
92  {
93  return m_items;
94  }
95 
97  int getNItems() const
98  {
99  return m_items.size();
100  }
101 
103  void clearItems()
104  {
105  m_items.clear();
106  }
107 
110  {
111  return m_children;
112  }
113 
119  {
120  // automatically removes all lower level objects
121  m_children.clear();
122  m_filled = false;
123  }
124 
126  int getLevel() const
127  {
128  return m_level;
129  }
130 
132  bool checkFilled() const
133  {
134  return m_filled;
135  }
136 
138  void setFilled()
139  {
140  m_filled = true;
141  }
142 
144  This* getParent() const
145  {
146  return m_parent;
147  }
148 
150  constexpr int getXNbins() const
151  {
152  return m_xBinBounds.size() / 2;
153  }
154 
156  AX getXMin() const
157  {
158  return m_xBinBounds.front();
159  }
160 
162  AX getXMax() const
163  {
164  return m_xBinBounds.back();
165  }
166 
168  AX getXBinWidth(int iBin)
169  {
170  return std::abs(getXUpperBound(iBin) - getXLowerBound(iBin));
171  }
172 
174  AX getXLowerBound(int iBin) const
175  {
176  return m_xBinBounds[2 * iBin];
177  }
178 
180  AX getXUpperBound(int iBin) const
181  {
182  return m_xBinBounds[2 * iBin + 1];
183  }
184 
186  constexpr int getYNbins() const
187  {
188  return m_yBinBounds.size() / 2;
189  }
190 
192  AY getYMin() const
193  {
194  return m_yBinBounds.front();
195  }
196 
198  AY getYMax() const
199  {
200  return m_yBinBounds.back();
201  }
202 
204  AY getYBinWidth(int iBin)
205  {
206  return std::abs(getYUpperBound(iBin) - getYLowerBound(iBin));
207  }
209  AY getYLowerBound(int iBin) const
210  {
211  return m_yBinBounds[2 * iBin];
212  }
213 
215  AY getYUpperBound(int iBin) const
216  {
217  return m_yBinBounds[2 * iBin + 1];
218  }
219 
220  private:
223 
226 
228  int m_level;
229 
232 
234  std::vector<AItem*> m_items;
235 
237  std::vector<This> m_children;
238 
240  bool m_filled;
241  };
242  }
244 }
Class which holds quadtree structure.
Definition: QuadTreeNode.h:29
void clearChildren()
Clear items which the node holds and destroy all children below this node.
Definition: QuadTreeNode.h:118
This * getParent() const
Return pointer to the parent of the node.
Definition: QuadTreeNode.h:144
AY getYBinWidth(int iBin)
Getter for the width of the iBin bin in "r" direction.
Definition: QuadTreeNode.h:204
AX getXMax() const
Get maximal "Theta" value of the node.
Definition: QuadTreeNode.h:162
bool m_filled
Is the node has been filled with items.
Definition: QuadTreeNode.h:240
YBinBounds m_yBinBounds
bins range on r
Definition: QuadTreeNode.h:225
void insertItem(AItem *item)
Insert item into node.
Definition: QuadTreeNode.h:79
std::vector< This > Children
Type of the child node structure for this node.
Definition: QuadTreeNode.h:48
AY getYMax() const
Get maximal "r" value of the node.
Definition: QuadTreeNode.h:198
std::array< AY, 4 > YBinBounds
Type to store the minimum and maximum of the two bins in Y direction.
Definition: QuadTreeNode.h:45
std::array< AX, 4 > XBinBounds
Type to store the minimum and maximum of the two bins in X direction.
Definition: QuadTreeNode.h:42
void reserveItems(int nItems)
Reserve memory for holding items.
Definition: QuadTreeNode.h:85
int m_level
Level of node in the tree.
Definition: QuadTreeNode.h:228
std::array< AY, 2 > YSpan
Type for a span in the Y direction that is covered by the tree.
Definition: QuadTreeNode.h:39
XBinBounds m_xBinBounds
bins range on theta
Definition: QuadTreeNode.h:222
void setFilled()
Set status of node to "filled" (children nodes has been filled)
Definition: QuadTreeNode.h:138
AY getYMin() const
Get minimal "r" value of the node.
Definition: QuadTreeNode.h:192
int getLevel() const
Returns level of the node in tree (i.e., how much ancestors the node has)
Definition: QuadTreeNode.h:126
AX getXLowerBound(int iBin) const
Get lower "Theta" value of given bin.
Definition: QuadTreeNode.h:174
void clearItems()
Clear items which the node holds.
Definition: QuadTreeNode.h:103
QuadTreeNode(XSpan xSpan, YSpan ySpan, int level, This *parent)
Constructor setting up the potential division points.
Definition: QuadTreeNode.h:57
std::vector< AItem * > m_items
Vector of items which belongs to the node.
Definition: QuadTreeNode.h:234
std::vector< AItem * > & getItems()
Get items from node.
Definition: QuadTreeNode.h:91
AY getYUpperBound(int iBin) const
Get upper "r" value of given bin.
Definition: QuadTreeNode.h:215
std::array< AX, 2 > XSpan
Type for a span in the X direction that is covered by the tree.
Definition: QuadTreeNode.h:36
This * m_parent
Pointer to the parent node.
Definition: QuadTreeNode.h:231
AY getYLowerBound(int iBin) const
Get lower "r" value of given bin.
Definition: QuadTreeNode.h:209
bool checkFilled() const
Check whether node has been processed, i.e.
Definition: QuadTreeNode.h:132
int getNItems() const
Check if the node passes threshold on number of hits.
Definition: QuadTreeNode.h:97
AX getXBinWidth(int iBin)
Getter for the width of the iBin bin in "Theta" direction.
Definition: QuadTreeNode.h:168
std::vector< This > m_children
Pointers to the children nodes.
Definition: QuadTreeNode.h:237
constexpr int getXNbins() const
Get number of bins in "Theta" direction.
Definition: QuadTreeNode.h:150
constexpr int getYNbins() const
Get number of bins in "r" direction.
Definition: QuadTreeNode.h:186
Children & getChildren()
Returns the children structure of this node.
Definition: QuadTreeNode.h:109
AX getXMin() const
Get minimal "Theta" value of the node.
Definition: QuadTreeNode.h:156
AX getXUpperBound(int iBin) const
Get upper "Theta" value of given bin.
Definition: QuadTreeNode.h:180
Abstract base class for different kinds of events.