1 // -*- C++ -*-
2 /**
3 * @brief The test file of class NaiveNearestNeighbour for standard methods
4 *
5 * Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21 #include "openturns/OT.hxx"
22 #include "openturns/OTtestcode.hxx"
23
24 using namespace OT;
25 using namespace OT::Test;
26
27 namespace
28 {
debug_squared_minimum_distance(const Point & point,const Sample & sample)29 UnsignedInteger debug_squared_minimum_distance(const Point & point, const Sample & sample)
30 {
31 Scalar result = SpecFunc::MaxScalar;
32 UnsignedInteger bestIndex = sample.getSize();
33 for(UnsignedInteger i = 0; i < sample.getSize(); ++i)
34 {
35 const Scalar distance2 = Point(sample[i] - point).normSquare();
36 if (distance2 < result)
37 {
38 result = distance2;
39 bestIndex = i;
40 }
41 }
42 return bestIndex;
43 }
44 }
45
main(int,char * [])46 int main(int, char *[])
47 {
48 TESTPREAMBLE;
49 OStream fullprint(std::cout);
50
51 const Sample sample(Normal().getSample(10));
52 const NearestNeighbour1D tree(sample);
53 fullprint << "tree=" << tree << std::endl;
54 const Sample test(Normal().getSample(20));
55 for (UnsignedInteger i = 0; i < test.getSize(); ++i)
56 {
57 const UnsignedInteger expected = debug_squared_minimum_distance(test[i], sample);
58 const UnsignedInteger index = tree.query(test[i]);
59 const Point neighbour(sample[tree.query(test[i])]);
60 fullprint << "Nearest neighbour of " << test[i] << "=" << neighbour << " (index=" << index << ")" << std::endl;
61 if (index != expected)
62 {
63 fullprint << "Wrong nearest neighbour of " << test[i] << " (index=" << index << ", expected=" << expected << ")" << std::endl;
64 return ExitCode::Error;
65 }
66 }
67
68 const UnsignedInteger k = 4;
69 for (UnsignedInteger i = 0; i < test.getSize(); ++i)
70 {
71 Indices indices = tree.queryK(test[i], k, true);
72 fullprint << k << " nearest neighbours of " << test[i] << "=" << " (indices=" << indices << ")" << std::endl;
73 // Check that returned neighbours are sorted
74 Scalar last = 0.0;
75 for(UnsignedInteger j = 0; j < indices.getSize(); ++j)
76 {
77 const Scalar current = Point(test[i] - sample[indices[j]]).normSquare();
78 if (last > current)
79 {
80 fullprint << "Wrong nearest neighbour of " << test[i] << " (indices=" << indices << ")" << std::endl;
81 return ExitCode::Error;
82 }
83 last = current;
84 }
85 }
86
87 return ExitCode::Success;
88 }
89