-
Notifications
You must be signed in to change notification settings - Fork 4
/
KNearestItems.h
153 lines (130 loc) · 5.51 KB
/
KNearestItems.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* $Id: KNearestItems.h 298 2015-03-25 13:00:40Z bernardt.duvenhage $
*/
/*
* KNearestItems.h
* StitchEngine
*
* Created by Bernardt Duvenhage on 2009/09/17.
* Copyright $Date: 2015-03-25 15:00:40 +0200 (Wed, 25 Mar 2015) $ Bernardt Duvenhage. All rights reserved.
*
*
* This file is part of StitchEngine.
* StitchEngine is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* StitchEngine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with StitchEngine. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef STITCH_KNEARESTITEMS_H
#define STITCH_KNEARESTITEMS_H
namespace stitch {
class KNearestItems;
}
#include "Math/Vec3.h"
#include "BoundingVolume.h"
namespace stitch
{
//!Stores k-nearest items in binary heap during kd-tree query.
class KNearestItems
{
private:
//!Copy constructor (Private because some of the class members are constant).
KNearestItems(const KNearestItems &lValue) :
searchRadiusSq_(lValue.searchRadiusSq_),
centre_(lValue.centre_),
k_(lValue.k_),
numItems_(0)
{
heapArray_=new std::pair<float, BoundingVolume const *>[k_];
}
//!Assignment operator (Private because some of the class members are constant).
KNearestItems &operator = (const KNearestItems &lValue)
{
return *this;
}
public:
KNearestItems(const Vec3 ¢re, const float searchRadiusSq, const size_t k) :
searchRadiusSq_(searchRadiusSq),
centre_(centre),
k_(k),
numItems_(0)
{
heapArray_=new std::pair<float, BoundingVolume const *>[k_];
}
~KNearestItems()
{
delete [] heapArray_;
}
//!Insert an item into the k-nearest-neighbour heap.
inline void insert(BoundingVolume const * const item)
{
const float itemDistSq=Vec3::calcDistToPointSq(item->centre_, centre_);
if (itemDistSq<=searchRadiusSq_)
{
if (numItems_<k_)
{//Add to bottom of heap and up-heap.
heapArray_[numItems_]=std::make_pair(itemDistSq, item);
++numItems_;
if (numItems_>1)
{
//Up-heap.
size_t childNode=numItems_-1;
size_t parentNode=(childNode-1) >> 1;
while (heapArray_[childNode].first > heapArray_[parentNode].first)
{
std::swap(heapArray_[childNode], heapArray_[parentNode]);
if (parentNode!=0)
{
childNode=parentNode;
parentNode=(childNode-1) >> 1;
} else
{
break;
}
}
}
} else
{//numItems_==k_;
//if (itemDistSq < heapArray_[0].first) Already checked (itemDistSq<=searchRadiusSq_) and searchRadiusSq_==heapArray_[0].first
{//Replace root (max node) of heap and down-heap.
heapArray_[0]=std::make_pair(itemDistSq, item);
//Down-heap.
size_t parentNode=0;
size_t childNodeLeft=(parentNode<<1) + 1;
size_t childNodeRight=(parentNode<<1) + 2;
size_t childNodeBiggest=(heapArray_[childNodeLeft].first > heapArray_[childNodeRight].first) ? childNodeLeft : childNodeRight;
while (heapArray_[parentNode].first<heapArray_[childNodeBiggest].first)
{
std::swap(heapArray_[parentNode], heapArray_[childNodeBiggest]);
if (((childNodeBiggest<<1) + 1)<numItems_)
{
parentNode=childNodeBiggest;
childNodeLeft=(parentNode<<1) + 1;
childNodeRight=(parentNode<<1) + 2;
childNodeBiggest=(heapArray_[childNodeLeft].first > heapArray_[childNodeRight].first) ? childNodeLeft : childNodeRight;
} else
{
break;
}
}
searchRadiusSq_=heapArray_[0].first;
}
}
}
}
public:
float searchRadiusSq_;
const Vec3 centre_;
const size_t k_;
std::pair<float, BoundingVolume const *> *heapArray_;
size_t numItems_;
};
}
#endif// STITCH_KNEARESTITEMS_H