helpers.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2010--2011, Stephane Magnenat, ASL, ETHZ, Switzerland
4 You can contact the author at <stephane at magnenat dot net>
5 
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10  * Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15  * Neither the name of the <organization> nor the
16  names of its contributors may be used to endorse or promote products
17  derived from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL ETH-ASL BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 */
31 
32 #ifndef __NABE_TEST_HELPERS_H
33 #define __NABE_TEST_HELPERS_H
34 
35 #include "nabo/nabo.h"
36 #include <chrono>
37 #include <limits>
38 #include <iostream>
39 #include <fstream>
40 
41 #include <cstdint>
42 
43 using namespace std;
44 using namespace Nabo;
45 
46 template<typename T>
47 typename NearestNeighbourSearch<T>::Matrix load(const char *fileName)
48 {
49  typedef typename NearestNeighbourSearch<T>::Matrix Matrix;
50 
51  ifstream ifs(fileName);
52  if (!ifs.good())
53  {
54  cerr << "Cannot open file "<< fileName << endl;
55  exit(1);
56  }
57 
58  vector<T> data;
59  int dim(0);
60  bool firstLine(true);
61 
62  while (!ifs.eof())
63  {
64  char line[1024];
65  ifs.getline(line, sizeof(line));
66  line[sizeof(line)-1] = 0;
67 
68  char *token = strtok(line, " \t,;");
69  while (token)
70  {
71  if (firstLine)
72  ++dim;
73  data.push_back(atof(token));
74  token = strtok(NULL, " \t,;"); // FIXME: non reentrant, use strtok_r
75  }
76  firstLine = false;
77  }
78 
79  return Matrix::Map(&data[0], dim, data.size() / dim);
80 }
81 
82 template<typename T>
83 typename NearestNeighbourSearch<T>::Vector createQuery(const typename NearestNeighbourSearch<T>::Matrix& d, const NearestNeighbourSearch<T>& kdt, const int i, const int method)
84 {
85  typedef typename NearestNeighbourSearch<T>::Vector VectorT;
86  if (method < 0)
87  {
88  VectorT q = d.col(i % d.cols());
89  T absBound = 0;
90  for (int j = 0; j < q.size(); ++j)
91  absBound += kdt.maxBound(j) - kdt.minBound(j);
92  absBound /= (-method); // divided by -method
93 #ifdef EIGEN3_API
94  if (i < d.cols())
95  q.array() += absBound;
96  else
97  q.array() -= absBound;
98 #else // EIGEN3_API
99  if (i < d.cols())
100  q.cwise() += absBound;
101  else
102  q.cwise() -= absBound;
103 #endif // EIGEN3_API
104  return q;
105  }
106  else
107  {
108  VectorT q(kdt.minBound.size());
109  for (int j = 0; j < q.size(); ++j)
110  q(j) = kdt.minBound(j) + T(rand()) * (kdt.maxBound(j) - kdt.minBound(j)) / T(RAND_MAX);
111  return q;
112  }
113 }
114 
115 template<typename T>
116 typename NearestNeighbourSearch<T>::Matrix createQuery(const typename NearestNeighbourSearch<T>::Matrix& d, const int itCount, const int method)
117 {
118  typedef typename NearestNeighbourSearch<T>::Matrix MatrixT;
120  NNS* nns = NNS::create(d, d.rows(), typename NNS::SearchType(0));
121  MatrixT q(d.rows(), itCount);
122  for (int i = 0; i < itCount; ++i)
123  q.col(i) = createQuery<T>(d, *nns, i, method);
124  delete nns;
125  return q;
126 }
127 
128 struct timer
129 {
130  using Clock = std::chrono::high_resolution_clock;
131  using Time = Clock::time_point;
132 
133  timer():_start_time(curTime()){ }
134  void restart() { _start_time = curTime(); }
135  double elapsed() const // return elapsed time in seconds
136  {
137  using namespace std::chrono;
138  return duration_cast<duration<double>>(curTime() - _start_time).count();
139  }
140 
141 private:
142  Time curTime() const {
143  return Clock::now();
144  }
146 };
147 
148 #endif // __NABE_TEST_HELPERS_H
149 
150 /* vim: set ts=8 sw=8 tw=0 noexpandtab cindent softtabstop=8 :*/
timer::Clock
std::chrono::high_resolution_clock Clock
Definition: helpers.h:130
Nabo
Namespace for Nabo.
Definition: experimental/kdtree_cpu.cpp:40
timer
Definition: helpers.h:128
nabo.h
public interface
Nabo::NearestNeighbourSearch::maxBound
const Vector maxBound
the high bound of the search space (axis-aligned bounding box)
Definition: nabo.h:282
SearchType
NNSNabo::SearchType SearchType
Definition: python/nabo.cpp:12
timer::restart
void restart()
Definition: helpers.h:134
timer::_start_time
Time _start_time
Definition: helpers.h:145
Nabo::NearestNeighbourSearch
Nearest neighbour search interface, templatized on scalar type.
Definition: nabo.h:258
createQuery
NearestNeighbourSearch< T >::Vector createQuery(const typename NearestNeighbourSearch< T >::Matrix &d, const NearestNeighbourSearch< T > &kdt, const int i, const int method)
Definition: helpers.h:83
test.nns
nns
Definition: test.py:7
timer::Time
Clock::time_point Time
Definition: helpers.h:131
timer::elapsed
double elapsed() const
Definition: helpers.h:135
timer::curTime
Time curTime() const
Definition: helpers.h:142
Nabo::NearestNeighbourSearch::minBound
const Vector minBound
the low bound of the search space (axis-aligned bounding box)
Definition: nabo.h:280
std
Definition: any.hpp:450
timer::timer
timer()
Definition: helpers.h:133
test.q
q
Definition: test.py:8
load
NearestNeighbourSearch< T >::Matrix load(const char *fileName)
Definition: helpers.h:47


libnabo
Author(s): Stéphane Magnenat
autogenerated on Fri Aug 2 2024 08:39:11