-
Notifications
You must be signed in to change notification settings - Fork 145
/
knnvalidate.cpp
253 lines (229 loc) · 8.54 KB
/
knnvalidate.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
Copyright (c) 2010--2011, Stephane Magnenat, ASL, ETHZ, Switzerland
You can contact the author at <stephane at magnenat dot net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "nabo/nabo.h"
#include "helpers.h"
//#include "experimental/nabo_experimental.h"
#include <iostream>
#include <fstream>
#include <stdexcept>
using namespace std;
using namespace Nabo;
template<typename T, typename CloudType>
struct Loader
{
void loadMatrix(const char *fileName)
{
data = load<T>(fileName);
}
CloudType getValue() const
{
return data;
}
private:
CloudType data;
};
template<typename T>
struct Loader<T, Eigen::Map<const Eigen::Matrix<T, 3, Eigen::Dynamic>, Eigen::Aligned> >
{
void loadMatrix(const char *fileName)
{
data = load<T>(fileName);
}
Eigen::Map<const Eigen::Matrix<T, 3, Eigen::Dynamic>, Eigen::Aligned> getValue() const
{
return Eigen::Map<const Eigen::Matrix<T, 3, Eigen::Dynamic>, Eigen::Aligned>(data.data(), 3, data.cols());
}
private:
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> data;
};
template<typename T, typename CloudType>
void validate(const char *fileName, const int K, const int dim, const int method, const T maxRadius)
{
typedef Nabo::NearestNeighbourSearch<T, CloudType> NNS;
typedef vector<NNS*> NNSV;
typedef typename NNS::Matrix Matrix;
typedef typename NNS::Vector Vector;
typedef typename NNS::IndexMatrix IndexMatrix;
Loader<T, CloudType> loader;
loader.loadMatrix(fileName);
// check if file is ok
const CloudType d = loader.getValue();
if (d.rows() != dim)
{
cerr << "Provided data has " << d.rows() << " dimensions, but the requested dimensions were " << dim << endl;
exit(2);
}
if (K >= d.cols())
{
cerr << "Requested more nearest neighbour than points in the data set" << endl;
exit(2);
}
// create different methods
NNSV nnss;
unsigned searchTypeCount(NNS::SEARCH_TYPE_COUNT);
#ifndef HAVE_OPENCL
searchTypeCount -= 3;
#endif // HAVE_OPENCL
for (unsigned i = 0; i < searchTypeCount; ++i)
nnss.push_back(NNS::create(d, d.rows(), typename NNS::SearchType(i)));
//nnss.push_back(new KDTreeBalancedPtInLeavesStack<T>(d, false));
// check methods together
const int itCount(method != -1 ? method : d.cols() * 2);
/*
// element-by-element search
typedef typename NNS::IndexVector IndexVector;
for (int i = 0; i < itCount; ++i)
{
const Vector q(createQuery<T>(d, *nnss[0], i, method));
const IndexVector indexes_bf(nnss[0]->knn(q, K, 0, NNS::SORT_RESULTS));
for (size_t j = 1; j < nnss.size(); ++j)
{
const IndexVector indexes_kdtree(nnss[j]->knn(q, K, 0, NNS::SORT_RESULTS));
if (indexes_bf.size() != K)
{
cerr << "Different number of points found between brute force and request" << endl;
exit(3);
}
if (indexes_bf.size() != indexes_kdtree.size())
{
cerr << "Different number of points found between brute force and NNS type "<< j << endl;
exit(3);
}
for (size_t k = 0; k < size_t(K); ++k)
{
Vector pbf(d.col(indexes_bf[k]));
//cerr << indexes_kdtree[k] << endl;
Vector pkdtree(d.col(indexes_kdtree[k]));
if (fabsf((pbf-q).squaredNorm() - (pkdtree-q).squaredNorm()) >= numeric_limits<float>::epsilon())
{
cerr << "Method " << j << ", cloud point " << i << ", neighbour " << k << " of " << K << " is different between bf and kdtree (dist " << (pbf-pkdtree).norm() << ")\n";
cerr << "* query:\n";
cerr << q << "\n";
cerr << "* indexes " << indexes_bf[k] << " (bf) vs " << indexes_kdtree[k] << " (kdtree)\n";
cerr << "* coordinates:\n";
cerr << "bf: (dist " << (pbf-q).norm() << ")\n";
cerr << pbf << "\n";
cerr << "kdtree (dist " << (pkdtree-q).norm() << ")\n";
cerr << pkdtree << endl;
exit(4);
}
}
}
}
*/
// create big query
// check all-in-one query
Matrix q(createQuery<T>(d, itCount, method));
IndexMatrix indexes_bf(K, q.cols());
Matrix dists2_bf(K, q.cols());
nnss[0]->knn(q, indexes_bf, dists2_bf, K, 0, NNS::SORT_RESULTS, maxRadius);
assert(indexes_bf.cols() == q.cols());
for (size_t j = 1; j < nnss.size(); ++j)
{
IndexMatrix indexes_kdtree(K, q.cols());
Matrix dists2_kdtree(K, q.cols());
nnss[j]->knn(q, indexes_kdtree, dists2_kdtree, K, 0, NNS::SORT_RESULTS, maxRadius);
if (indexes_bf.rows() != K)
{
cerr << "Different number of points found between brute force and request" << endl;
exit(3);
}
if (indexes_bf.cols() != indexes_kdtree.cols())
{
cerr << "Different number of points found between brute force and NNS type "<< j << endl;
exit(3);
}
for (int i = 0; i < q.cols(); ++i)
{
for (size_t k = 0; k < size_t(K); ++k)
{
if (dists2_bf(k,i) == numeric_limits<float>::infinity())
continue;
const int pbfi(indexes_bf(k,i));
const Vector pbf(d.col(pbfi));
const int pkdt(indexes_kdtree(k,i));
if (pkdt < 0 || pkdt >= d.cols())
{
cerr << "Method " << j << ", query point " << i << ", neighbour " << k << " of " << K << " has invalid index " << pkdt << " out of range [0:" << d.cols() << "[" << endl;
exit(4);
}
const Vector pkdtree(d.col(pkdt));
const Vector pq(q.col(i));
const float distDiff(fabsf((pbf-pq).squaredNorm() - (pkdtree-pq).squaredNorm()));
if (distDiff > numeric_limits<float>::epsilon())
{
cerr << "Method " << j << ", query point " << i << ", neighbour " << k << " of " << K << " is different between bf and kdtree (dist2 " << distDiff << ")\n";
cerr << "* query point:\n";
cerr << pq << "\n";
cerr << "* indexes " << pbfi << " (bf) vs " << pkdt << " (kdtree)\n";
cerr << "* coordinates:\n";
cerr << "bf: (dist " << (pbf-pq).norm() << ")\n";
cerr << pbf << "\n";
cerr << "kdtree (dist " << (pkdtree-pq).norm() << ")\n";
cerr << pkdtree << endl;
cerr << "* bf neighbours:\n";
for (int l = 0; l < K; ++l)
cerr << indexes_bf(l,i) << " (dist " << (d.col(indexes_bf(l,i))-pq).norm() << ")\n";
cerr << "* kdtree neighbours:\n";
for (int l = 0; l < K; ++l)
cerr << indexes_kdtree(l,i) << " (dist " << (d.col(indexes_kdtree(l,i))-pq).norm() << ")\n";
exit(4);
}
}
}
}
// cout << "\tstats kdtree: "
// << kdt.getStatistics().totalVisitCount << " on "
// << (long long)(itCount) * (long long)(d.cols()) << " ("
// << (100. * double(kdt.getStatistics().totalVisitCount)) / (double(itCount) * double(d.cols())) << " %"
// << ")\n" << endl;
// delete searches
for (typename NNSV::iterator it(nnss.begin()); it != nnss.end(); ++it)
delete (*it);
}
int main(int argc, char* argv[])
{
if (argc < 4)
{
cerr << "Usage " << argv[0] << " DATA K DIM METHOD [MAX_RADIUS]" << endl;
return 1;
}
const int K(atoi(argv[2]));
const int dim(atoi(argv[3]));
const int method(atoi(argv[4]));
const float maxRadius(argc >= 6 ? float(atof(argv[5])) : numeric_limits<float>::infinity());
if (dim == 3)
{
validate<float, Eigen::MatrixXf>(argv[1], K, dim, method, maxRadius);
validate<float, Eigen::Matrix3Xf>(argv[1], K, dim, method, maxRadius);
validate<float, Eigen::Map<const Eigen::Matrix3Xf, Eigen::Aligned> >(argv[1], K, dim, method, maxRadius);
} else
{
validate<float, Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> >(argv[1], K, dim, method, maxRadius);
}
//validate<double>(argv[1], K, method);
return 0;
}