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 "cv.h"
34 #include <utility>
35 #include <vector>
36 #include <algorithm>
37 
38 namespace alvar {
39 
40 template <class T> class Container3d;
41 
43 template <class T>
45 protected:
46  CvPoint3D32f orig;
48 public:
49  Container3dSortDist(Container3d<T> &_container, const CvPoint3D32f _orig) : container(_container), orig(_orig) {}
50  bool operator()(size_t i1, size_t i2)
51  {
52  float x1 = container[i1].first.x-orig.x, x2 = container[i2].first.x-orig.x;
53  float y1 = container[i1].first.y-orig.y, y2 = container[i2].first.y-orig.y;
54  float z1 = container[i1].first.z-orig.z, z2 = container[i2].first.z-orig.z;
55  float d1 = x1*x1 + y1*y1 + z1*z1;
56  float d2 = x2*x2 + y2*y2 + z2*z2;
57  return d1<d2;
58  }
59 };
60 
62 template <class T>
64 protected:
66 public:
67  Container3dSortSize(Container3d<T> &_container) : container(_container) {}
68  bool operator()(size_t i1, size_t i2)
69  {
70  return (container[i1].second->size() > container[i2].second->size());
71  }
72 };
73 
75 template <class T>
77 protected:
79  CvPoint3D32f orig;
80  float dist_limit;
81 public:
82  Container3dLimitDist(Container3d<T> &_container, const CvPoint3D32f _orig, float _dist_limit)
83  : container(_container),orig(_orig),dist_limit(_dist_limit) {}
84  bool operator()(size_t i) const {
85  float x = container[i].first.x-orig.x;
86  float y = container[i].first.y-orig.y;
87  float z = container[i].first.z-orig.z;
88  float d = x*x + y*y + z*z;
89  if (d <= dist_limit*dist_limit) return true;
90  return false;
91  }
92 };
93 
165 template <class T>
166 class Container3d
167 {
168  public:
170  typedef std::pair<CvPoint3D32f, T> node_type;
171  protected:
173  std::vector<node_type> data;
175  std::vector<size_t> search_space;
176 
177  public:
179  void Add(const CvPoint3D32f& _pos, const T& _data){
180  data.push_back(node_type(_pos, _data));
181  search_space.push_back(data.size()-1);
182  }
184  void Clear() {
185  data.clear();
186  search_space.clear();
187  }
190  search_space.resize(data.size());
191  for (size_t i=0; i<search_space.size(); i++)
192  {
193  search_space[i] = i;
194  }
195  }
197  void Erase(size_t index) {
198  typename std::vector<node_type>::iterator iter_d;
199  iter_d = data.begin();
200  for (size_t i=0; i<index; i++) iter_d++;
201  data.erase(iter_d);
202  ResetSearchSpace();
203  }
205  template <typename Compare>
206  int Sort(Compare comp) {
207  stable_sort(search_space.begin(), search_space.end(), comp);
208  return search_space.size();
209  }
210 
212  template <typename Test>
213  int Limit(Test test) {
214  std::vector<size_t>::iterator iter;
215  for (iter = search_space.begin(); iter != search_space.end();) {
216  if (!test(*iter)) {
217  iter = search_space.erase(iter);
218  } else {
219  iter++;
220  }
221  }
222  return search_space.size();
223  }
224 
231  class Iterator : public std::iterator<std::forward_iterator_tag, node_type>
232  {
233  protected:
235  std::vector<size_t>::iterator iter;
236  public:
237  Iterator() {}
238  Iterator(Container3d<T> *_container, std::vector<size_t>::iterator _iter) : container(_container), iter(_iter) {}
239  node_type &operator*() const { return container->data[*iter]; }
240  node_type *operator->() const { return &(operator*()); }
241  virtual Iterator& operator++() { ++iter; return *this; }
242  bool operator==(const Iterator& _m) const { return iter == _m.iter; }
243  bool operator!=(const Iterator& _m) const { return iter != _m.iter; }
244  size_t GetIndex() { return *iter; }
245  };
246 
249  return Iterator(this, search_space.begin());
250  }
251 
254  return Iterator(this, search_space.end());
255  }
256 
258  size_t size() const { return data.size(); }
259 
261  size_t GetIndex(Iterator &iter) {
262  return iter.GetIndex();
263  }
264 
266  node_type &operator[](size_t index) {
267  return data[index];
268  }
269 
271  size_t GetIndex(T *p) {
272  size_t i=0;
273  for (; i<data.size(); ++i)
274  {
275  if (data[i].second.get() == p) break;
276  }
277  return i;
278  }
279 };
280 
281 } // namespace alvar
282 
283 #endif
284 
void Clear()
Clear the container.
Definition: Container3d.h:184
Main ALVAR namespace.
Definition: Alvar.h:174
TFSIMD_FORCE_INLINE Vector3 operator*(const Matrix3x3 &m, const Vector3 &v)
std::vector< node_type > data
the actual data in using node_type: pair<CvPoint3D32f, T>
Definition: Container3d.h:173
Container3d< T > * container
Definition: Container3d.h:234
Container3dLimitDist(Container3d< T > &_container, const CvPoint3D32f _orig, float _dist_limit)
Definition: Container3d.h:82
Functor class for Container3d Sort() to sort the search base using distance to specified origin...
Definition: Container3d.h:44
node_type & operator*() const
Definition: Container3d.h:239
Container3d< T > & container
Definition: Container3d.h:47
int Sort(Compare comp)
Sort using external Compare method.
Definition: Container3d.h:206
bool operator!=(const Iterator &_m) const
Definition: Container3d.h:243
std::vector< size_t > search_space
Possibly limited set of indices for data in somehow "optimal" search order.
Definition: Container3d.h:175
size_t GetIndex(Iterator &iter)
Get absolute reference usable with operator[]() based on the iterator.
Definition: Container3d.h:261
virtual Iterator & operator++()
Definition: Container3d.h:241
Container3dSortDist(Container3d< T > &_container, const CvPoint3D32f _orig)
Definition: Container3d.h:49
Functor class for Container3d Sort() to sort the search base using content size.
Definition: Container3d.h:63
TFSIMD_FORCE_INLINE const tfScalar & y() const
Iterator end()
Provides an iterator pointing to the end of the limited/sorted 3D content.
Definition: Container3d.h:253
Functor class for Container3d Limit() to limit the search space with distance.
Definition: Container3d.h:76
Iterator begin()
Provides an iterator pointing to the beginning of the limited/sorted 3D content.
Definition: Container3d.h:248
Container3d< T > & container
Definition: Container3d.h:65
size_t GetIndex(T *p)
Get absolute reference usable with operator[]() based on the content.
Definition: Container3d.h:271
std::vector< size_t >::iterator iter
Definition: Container3d.h:235
TFSIMD_FORCE_INLINE const tfScalar & x() const
void Add(const CvPoint3D32f &_pos, const T &_data)
Add _data in the container and associate it with 3D position _pos.
Definition: Container3d.h:179
node_type & operator[](size_t index)
Instead of Iterator we can use also absolute references for data with operator[]() ...
Definition: Container3d.h:266
Drawable d[32]
node_type * operator->() const
Definition: Container3d.h:240
Iterator(Container3d< T > *_container, std::vector< size_t >::iterator _iter)
Definition: Container3d.h:238
TFSIMD_FORCE_INLINE const tfScalar & z() const
void Erase(size_t index)
Erase item in the container.
Definition: Container3d.h:197
Generic container to store any information in 3D (features, photos, ...)
Definition: Container3d.h:40
bool operator()(size_t i1, size_t i2)
Definition: Container3d.h:50
int Limit(Test test)
Limit the search space with external limitation.
Definition: Container3d.h:213
Container3dSortSize(Container3d< T > &_container)
Definition: Container3d.h:67
Iterator for going through the items in Container3d in the specified order.
Definition: Container3d.h:231
bool operator==(const Iterator &_m) const
Definition: Container3d.h:242
bool operator()(size_t i1, size_t i2)
Definition: Container3d.h:68
bool operator()(size_t i) const
Definition: Container3d.h:84
void ResetSearchSpace()
Reset the search space to contain whole data.
Definition: Container3d.h:189
std::pair< CvPoint3D32f, T > node_type
node_type for storing data. 3D-position is paired with the data content.
Definition: Container3d.h:170
Container3d< T > & container
Definition: Container3d.h:78
size_t size() const
Get number of items that can be referenced using operator[]()
Definition: Container3d.h:258


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Thu Jun 6 2019 19:27:23