My Project
Container3d.h
Go to the documentation of this file.
1 /*
2  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
3  *
4  * Copyright 2007-2012 VTT Technical Research Centre of Finland
5  *
6  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
7  * <http://www.vtt.fi/multimedia/alvar.html>
8  *
9  * ALVAR is free software; you can redistribute it and/or modify it under the
10  * terms of the GNU Lesser General Public License as published by the Free
11  * Software Foundation; either version 2.1 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with ALVAR; if not, see
21  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
22  */
23 
24 #ifndef CONTAINER_3D
25 #define CONTAINER_3D
26 
33 #include <algorithm>
34 #include <opencv2/core.hpp>
35 #include <utility>
36 #include <vector>
37 
38 namespace alvar {
39 
40 template <class T>
41 class Container3d;
42 
44 template <class T>
46 {
47 protected:
48  cv::Point3f orig;
49  Container3d<T> &container;
50 
51 public:
52  Container3dSortDist(Container3d<T> &_container, const cv::Point3f _orig)
53  : container(_container), orig(_orig)
54  {
55  }
56  bool
57  operator()(size_t i1, size_t i2)
58  {
59  float x1 = container[i1].first.x - orig.x, x2 = container[i2].first.x - orig.x;
60  float y1 = container[i1].first.y - orig.y, y2 = container[i2].first.y - orig.y;
61  float z1 = container[i1].first.z - orig.z, z2 = container[i2].first.z - orig.z;
62  float d1 = x1 * x1 + y1 * y1 + z1 * z1;
63  float d2 = x2 * x2 + y2 * y2 + z2 * z2;
64  return d1 < d2;
65  }
66 };
67 
69 template <class T>
71 {
72 protected:
73  Container3d<T> &container;
74 
75 public:
76  Container3dSortSize(Container3d<T> &_container) : container(_container)
77  {
78  }
79  bool
80  operator()(size_t i1, size_t i2)
81  {
82  return (container[i1].second->size() > container[i2].second->size());
83  }
84 };
85 
87 template <class T>
89 {
90 protected:
91  Container3d<T> &container;
92  cv::Point3f orig;
93  float dist_limit;
94 
95 public:
96  Container3dLimitDist(Container3d<T> &_container, const cv::Point3f _orig, float _dist_limit)
97  : container(_container), orig(_orig), dist_limit(_dist_limit)
98  {
99  }
100  bool
101  operator()(size_t i) const
102  {
103  float x = container[i].first.x - orig.x;
104  float y = container[i].first.y - orig.y;
105  float z = container[i].first.z - orig.z;
106  float d = x * x + y * y + z * z;
107  if (d <= dist_limit * dist_limit)
108  return true;
109  return false;
110  }
111 };
112 
184 template <class T>
186 {
187 public:
189  typedef std::pair<cv::Point3f, T> node_type;
190 
191 protected:
193  std::vector<node_type> data;
195  std::vector<size_t> search_space;
196 
197 public:
199  void
200  Add(const cv::Point3f &_pos, const T &_data)
201  {
202  data.push_back(node_type(_pos, _data));
203  search_space.push_back(data.size() - 1);
204  }
206  void
208  {
209  data.clear();
210  search_space.clear();
211  }
213  void
215  {
216  search_space.resize(data.size());
217  for (size_t i = 0; i < search_space.size(); i++) {
218  search_space[i] = i;
219  }
220  }
222  void
223  Erase(size_t index)
224  {
225  typename std::vector<node_type>::iterator iter_d;
226  iter_d = data.begin();
227  for (size_t i = 0; i < index; i++)
228  iter_d++;
229  data.erase(iter_d);
231  }
233  template <typename Compare>
234  int
235  Sort(Compare comp)
236  {
237  stable_sort(search_space.begin(), search_space.end(), comp);
238  return search_space.size();
239  }
240 
242  template <typename Test>
243  int
244  Limit(Test test)
245  {
246  std::vector<size_t>::iterator iter;
247  for (iter = search_space.begin(); iter != search_space.end();) {
248  if (!test(*iter)) {
249  iter = search_space.erase(iter);
250  } else {
251  iter++;
252  }
253  }
254  return search_space.size();
255  }
256 
263  class Iterator : public std::iterator<std::forward_iterator_tag, node_type>
264  {
265  protected:
266  Container3d<T> * container;
267  std::vector<size_t>::iterator iter;
268 
269  public:
270  Iterator()
271  {
272  }
273  Iterator(Container3d<T> *_container, std::vector<size_t>::iterator _iter)
274  : container(_container), iter(_iter)
275  {
276  }
277  node_type &
278  operator*() const
279  {
280  return container->data[*iter];
281  }
282  node_type *
283  operator->() const
284  {
285  return &(operator*());
286  }
287  virtual Iterator &
288  operator++()
289  {
290  ++iter;
291  return *this;
292  }
293  bool
294  operator==(const Iterator &_m) const
295  {
296  return iter == _m.iter;
297  }
298  bool
299  operator!=(const Iterator &_m) const
300  {
301  return iter != _m.iter;
302  }
303  size_t
304  GetIndex()
305  {
306  return *iter;
307  }
308  };
309 
311  Iterator
313  {
314  return Iterator(this, search_space.begin());
315  }
316 
318  Iterator
319  end()
320  {
321  return Iterator(this, search_space.end());
322  }
323 
325  size_t
326  size() const
327  {
328  return data.size();
329  }
330 
332  size_t
334  {
335  return iter.GetIndex();
336  }
337 
339  node_type &
340  operator[](size_t index)
341  {
342  return data[index];
343  }
344 
346  size_t
347  GetIndex(T *p)
348  {
349  size_t i = 0;
350  for (; i < data.size(); ++i) {
351  if (data[i].second.get() == p)
352  break;
353  }
354  return i;
355  }
356 };
357 
358 } // namespace alvar
359 
360 #endif
Iterator for going through the items in Container3d in the specified order.
Definition: Container3d.h:264
Generic container to store any information in 3D (features, photos, ...)
Definition: Container3d.h:186
int Limit(Test test)
Limit the search space with external limitation.
Definition: Container3d.h:244
Iterator begin()
Provides an iterator pointing to the beginning of the limited/sorted 3D content.
Definition: Container3d.h:312
size_t size() const
Get number of items that can be referenced using operator[]()
Definition: Container3d.h:326
void ResetSearchSpace()
Reset the search space to contain whole data.
Definition: Container3d.h:214
std::vector< size_t > search_space
Possibly limited set of indices for data in somehow "optimal" search order.
Definition: Container3d.h:195
void Add(const cv::Point3f &_pos, const T &_data)
Add _data in the container and associate it with 3D position _pos.
Definition: Container3d.h:200
node_type & operator[](size_t index)
Instead of Iterator we can use also absolute references for data with operator[]()
Definition: Container3d.h:340
void Clear()
Clear the container.
Definition: Container3d.h:207
int Sort(Compare comp)
Sort using external Compare method.
Definition: Container3d.h:235
Iterator end()
Provides an iterator pointing to the end of the limited/sorted 3D content.
Definition: Container3d.h:319
size_t GetIndex(Iterator &iter)
Get absolute reference usable with operator[]() based on the iterator.
Definition: Container3d.h:333
std::vector< node_type > data
the actual data in using node_type: pair<cv::Point3f, T>
Definition: Container3d.h:193
size_t GetIndex(T *p)
Get absolute reference usable with operator[]() based on the content.
Definition: Container3d.h:347
void Erase(size_t index)
Erase item in the container.
Definition: Container3d.h:223
std::pair< cv::Point3f, T > node_type
node_type for storing data. 3D-position is paired with the data content.
Definition: Container3d.h:189
Functor class for Container3d Limit() to limit the search space with distance.
Definition: Container3d.h:89
Functor class for Container3d Sort() to sort the search base using distance to specified origin.
Definition: Container3d.h:46
Functor class for Container3d Sort() to sort the search base using content size.
Definition: Container3d.h:71
Main ALVAR namespace.
Definition: Alvar.h:174