1 #ifndef KDTreeLinkerAlgoTemplated_h 2 #define KDTreeLinkerAlgoTemplated_h 13 template <
unsigned DIM = 2>
17 template <
typename... Ts>
19 static_assert(
sizeof...(dimargs) == 2 *
DIM,
"Constructor requires 2*DIM args");
20 std::vector<float> dims = {dimargs...};
21 for (
unsigned i = 0;
i <
DIM; ++
i) {
33 template <
typename DATA,
unsigned DIM = 2>
36 std::array<float, DIM>
dims;
41 template <
typename... Ts>
43 template <
typename... Ts>
49 template <
typename DATA,
unsigned DIM = 2>
64 for (
auto &dim :
dims) {
69 right.shrink_to_fit();
83 for (
auto &dim :
dims) {
104 template <
typename DATA,
unsigned int DIM = 2>
149 template <
typename DATA,
unsigned int DIM>
152 if (!eltList.empty()) {
153 initialEltList = &eltList;
155 size_t size = initialEltList->size();
156 nodePool_.build(
size);
162 initialEltList =
nullptr;
167 template <
typename DATA,
unsigned int DIM>
170 int median = (nbrElts & 1) ? nbrElts / 2 : nbrElts / 2 - 1;
184 const unsigned thedim = treeDepth %
DIM;
185 while ((*initialEltList)[
i].dims[thedim] < elt.
dims[thedim])
187 while ((*initialEltList)[
j].dims[thedim] > elt.
dims[thedim])
191 std::swap((*initialEltList)[
i], (*initialEltList)[
j]);
205 template <
typename DATA,
unsigned int DIM>
209 recSearch(0, trackBox, 0);
210 closestNeighbour =
nullptr;
214 template <
typename DATA,
unsigned int DIM>
222 int right = nodePool_.right[current];
223 if (nodePool_.isLeaf(right)) {
229 bool isInside =
true;
230 for (
unsigned i = 0;
i <
DIM; ++
i) {
231 float dimCurr = nodePool_.dims[
i][current];
232 isInside &= (dimCurr >= trackBox.
dimmin[
i]) && (dimCurr <= trackBox.
dimmax[
i]);
235 closestNeighbour->push_back(nodePool_.data[current]);
239 float median = nodePool_.dims[dimIndex][current];
245 if (goLeft & goRight) {
246 int left = current + 1;
247 recSearch(left, trackBox,
depth);
252 }
else if (goRight) {
261 template <
typename DATA,
unsigned int DIM>
265 if (portionSize == 1) {
266 int leaf = nodePool_.getNextNode();
268 nodePool_.right[leaf] = 0;
269 for (
unsigned i = 0;
i <
DIM; ++
i) {
270 nodePool_.dims[
i][leaf] =
info.dims[
i];
272 nodePool_.data[leaf] =
info.data;
281 float medianVal = (*initialEltList)[medianId].dims[dimIndex];
284 int nodeInd = nodePool_.getNextNode();
290 int left = recBuild(
low, medianId,
depth);
291 assert(nodeInd + 1 == left);
292 int right = recBuild(medianId,
high,
depth);
293 nodePool_.right[nodeInd] = right;
295 nodePool_.dims[dimIndex][nodeInd] = medianVal;
KDTreeNodes< DATA, DIM > nodePool_
void search(const KDTreeBox< DIM > &searchBox, std::vector< DATA > &resRecHitList)
std::array< std::vector< float >, DIM > dims
int recBuild(int low, int hight, int depth)
std::vector< DATA > * closestNeighbour
std::array< float, DIM > dimmin
void swap(Association< C > &lhs, Association< C > &rhs)
bool isLeafIndex(int index) const
void recSearch(int current, const KDTreeBox< DIM > &trackBox, int depth=0) const
constexpr bool isLeaf(int right) const
int medianSearch(int low, int high, int treeDepth) const
std::array< float, DIM > dims
void build(std::vector< KDTreeNodeInfo< DATA, DIM > > &eltList, const KDTreeBox< DIM > ®ion)
std::vector< KDTreeNodeInfo< DATA, DIM > > * initialEltList
KDTreeNodeInfo(const DATA &d, Ts... dimargs)
std::array< float, DIM > dimmax
bool operator>(const KDTreeNodeInfo &rhs) const