Skip to content

Commit f3afe53

Browse files
committed
Added getPoint method
1 parent 8c06c5b commit f3afe53

File tree

6 files changed

+92
-21
lines changed

6 files changed

+92
-21
lines changed

doc/manual.pdf

401 Bytes
Binary file not shown.

doc/manual.tex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ \subsubsection{flann::Index}
322322
float rebuild_threshold = 2);
323323
324324
void removePoint(size_t point_id);
325+
326+
ElementType* getPoint(size_t point_id);
325327
326328
int knnSearch(const Matrix<ElementType>& queries,
327329
Matrix<int>& indices,
@@ -637,6 +639,12 @@ \subsubsection{flann::Index::removePoint}
637639
void removePoint(size_t point_id);
638640
\end{Verbatim}
639641

642+
\subsubsection{flann::Index::getPoint}
643+
The \texttt{getPoint} method returns a pointer to the data point with the specified \texttt{point\_id}.
644+
645+
\begin{Verbatim}[fontsize=\footnotesize,frame=single]
646+
ElementType* getPoint(size_t point_id);
647+
\end{Verbatim}
640648

641649
\subsubsection{flann::Index::knnSearch}
642650
Performs a K-nearest neighbor search for a set of query points. There are two signatures for this

src/cpp/flann/algorithms/kdtree_cuda_3d_index.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ class KDTreeCuda3dIndex : public NNIndex<Distance>
149149
throw FLANNException( "removePoint not implemented for this index type!" );
150150
}
151151

152+
ElementType* getPoint(size_t id)
153+
{
154+
return dataset_[id];
155+
}
156+
152157
void saveIndex(FILE* stream)
153158
{
154159
throw FLANNException( "Index saving not implemented!" );

src/cpp/flann/algorithms/nn_index.h

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,30 @@ class NNIndex : public IndexBase
157157
last_id_ = size_;
158158
}
159159

160-
size_t point_index = id;
161-
if (ids_[point_index]!=id) {
162-
// binary search
163-
size_t start = 0;
164-
size_t end = size();
165-
166-
while (start<end) {
167-
size_t mid = (start+end)/2;
168-
if (ids_[mid]==id) {
169-
point_index = mid;
170-
break;
171-
}
172-
else if (ids_[mid]<id) {
173-
start = mid + 1;
174-
}
175-
else {
176-
end = mid;
177-
}
178-
}
160+
size_t point_index = id_to_index(id);
161+
if (point_index!=size_t(-1)) {
162+
removed_points_.set(point_index);
179163
}
180-
181-
removed_points_.set(point_index);
182164
removed_ = true;
183165
}
184166

167+
168+
/**
169+
* Get point with specific id
170+
* @param id
171+
* @return
172+
*/
173+
virtual ElementType* getPoint(size_t id)
174+
{
175+
size_t index = id_to_index(id);
176+
if (index!=size_t(-1)) {
177+
return points_[index];
178+
}
179+
else {
180+
return NULL;
181+
}
182+
}
183+
185184
/**
186185
* @return number of features in this index.
187186
*/
@@ -683,6 +682,38 @@ class NNIndex : public IndexBase
683682

684683
protected:
685684

685+
size_t id_to_index(size_t id)
686+
{
687+
if (ids_.size()==0) {
688+
return id;
689+
}
690+
size_t point_index = size_t(-1);
691+
if (ids_[id]==id) {
692+
return id;
693+
}
694+
else {
695+
// binary search
696+
size_t start = 0;
697+
size_t end = size();
698+
699+
while (start<end) {
700+
size_t mid = (start+end)/2;
701+
if (ids_[mid]==id) {
702+
point_index = mid;
703+
break;
704+
}
705+
else if (ids_[mid]<id) {
706+
start = mid + 1;
707+
}
708+
else {
709+
end = mid;
710+
}
711+
}
712+
}
713+
return point_index;
714+
}
715+
716+
686717
void indices_to_ids(const size_t* in, size_t* out, size_t size) const
687718
{
688719
if (removed_) {

src/cpp/flann/flann.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,20 @@ class Index
160160
nnIndex_->removePoint(point_id);
161161
}
162162

163+
/**
164+
* Returns pointer to a data point with the specified id.
165+
* @param point_id the id of point to retrieve
166+
* @return
167+
*/
168+
ElementType* getPoint(size_t point_id)
169+
{
170+
return nnIndex_->getPoint(point_id);
171+
}
163172

173+
/**
174+
* Save index to file
175+
* @param filename
176+
*/
164177
void save(std::string filename)
165178
{
166179
FILE* fout = fopen(filename.c_str(), "wb");

test/flann_kdtree_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ TEST_F(KDTree_SIFT10K, TestIncremental)
5353
float precision = compute_precision(match, indices);
5454
EXPECT_GE(precision, 0.75);
5555
printf("Precision: %g\n", precision);
56+
57+
for (size_t i=0;i<indices.rows;++i) {
58+
for (size_t j=0;j<indices.cols;++j) {
59+
EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
60+
}
61+
}
5662
}
5763

5864
TEST_F(KDTree_SIFT10K, TestIncremental2)
@@ -74,6 +80,12 @@ TEST_F(KDTree_SIFT10K, TestIncremental2)
7480
float precision = compute_precision(match, indices);
7581
EXPECT_GE(precision, 0.75);
7682
printf("Precision: %g\n", precision);
83+
84+
for (size_t i=0;i<indices.rows;++i) {
85+
for (size_t j=0;j<indices.cols;++j) {
86+
EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
87+
}
88+
}
7789
}
7890

7991

@@ -116,6 +128,7 @@ TEST_F(KDTree_SIFT10K, TestRemove)
116128
for (size_t j=0;j<indices.cols;++j) {
117129
EXPECT_GE(indices[i][j], offset);
118130
EXPECT_TRUE(neighbors.find(indices[i][j])==neighbors.end());
131+
EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
119132
}
120133
}
121134

@@ -130,6 +143,7 @@ TEST_F(KDTree_SIFT10K, TestRemove)
130143
for (size_t j=0;j<indices.cols;++j) {
131144
EXPECT_GE(indices[i][j], offset);
132145
EXPECT_TRUE(neighbors.find(indices[i][j])==neighbors.end());
146+
EXPECT_EQ(index.getPoint(indices[i][j]), data[indices[i][j]]);
133147
}
134148
}
135149
}

0 commit comments

Comments
 (0)