supereight
iterator.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2020-2021 Smart Robotics Lab, Imperial College London, Technical University of Munich
3  * SPDX-FileCopyrightText: 2020-2021 Nils Funk
4  * SPDX-FileCopyrightText: 2019-2021 Sotiris Papatheodorou
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef SE_ITERATOR_HPP
9 #define SE_ITERATOR_HPP
10 
11 #include <stack>
12 
13 #include "octree.hpp"
15 
16 
17 
18 namespace se {
19 
20 template<typename DerivedT>
21 struct BaseTraits;
22 
38 template<typename DerivedT>
39 class BaseIterator {
40  public:
44 
45  BaseIterator();
46 
47  BaseIterator(OctreeType* octree_ptr);
48 
49  BaseIterator(const BaseIterator& other);
50 
52 
54 
55  bool operator==(const BaseIterator& other) const;
56 
57  bool operator!=(const BaseIterator& other) const;
58 
59  se::OctantBase* operator*() const;
60 
61  // Iterator traits
62  using difference_type = long;
66  using iterator_category = std::forward_iterator_tag;
67 
68  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
69 
70  protected:
71  void init();
72 
73  private:
74  DerivedT& underlying()
75  {
76  return static_cast<DerivedT&>(*this);
77  }
78  const DerivedT& underlying() const
79  {
80  return static_cast<const DerivedT&>(*this);
81  }
82 
83  // Find the next Volume with valid data.
84  void nextData();
85 
86  // Reset the iterator state to invalid. Used when finished iterating.
87  void clear();
88 
89  // The 3 stacks should always be kept in sync.
90  // Pointers to the Nodes that haven't been checked yet.
91  std::stack<se::OctantBase*> octant_stack_;
92 
93  se::OctantBase* octant_;
94 
95  OctreeType* octree_ptr_ = nullptr;
96 };
97 
98 
99 
100 template<typename OctreeT>
101 class OctreeIterator : public BaseIterator<OctreeIterator<OctreeT>> {
102  public:
104 
105  OctreeIterator(OctreeT* octree_ptr) : BaseIterator<OctreeIterator<OctreeT>>(octree_ptr)
106  {
107  this->init();
108  };
109 
110  static constexpr bool has_ignore_condition = false;
111 
112  bool isNext(se::OctantBase* /* octant_ptr */)
113  {
114  return true;
115  }
116 
117  protected:
118  friend class BaseIterator<OctreeIterator<OctreeT>>;
119 };
120 
121 
122 
123 template<typename OctreeT>
124 class NodesIterator : public BaseIterator<NodesIterator<OctreeT>> {
125  public:
127 
128  NodesIterator(OctreeT* octree_ptr) : BaseIterator<NodesIterator<OctreeT>>(octree_ptr)
129  {
130  this->init();
131  };
132 
133  static constexpr bool has_ignore_condition = false;
134 
135  bool isNext(se::OctantBase* octant_ptr)
136  {
137  return !octant_ptr->isBlock();
138  }
139 
140  protected:
141  friend class BaseIterator<NodesIterator<OctreeT>>;
142 };
143 
144 
145 
146 template<typename OctreeT>
147 class BlocksIterator : public BaseIterator<BlocksIterator<OctreeT>> {
148  public:
150 
151  BlocksIterator(OctreeT* octree_ptr) : BaseIterator<BlocksIterator<OctreeT>>(octree_ptr)
152  {
153  this->init();
154  };
155 
156  static constexpr bool has_ignore_condition = false;
157 
158  bool isNext(se::OctantBase* octant_ptr)
159  {
160  return octant_ptr->isBlock();
161  }
162 
163  protected:
164  friend class BaseIterator<BlocksIterator<OctreeT>>;
165 };
166 
167 
168 
169 template<typename OctreeT>
170 class LeavesIterator : public BaseIterator<LeavesIterator<OctreeT>> {
171  public:
173 
174  LeavesIterator(OctreeT* octree_ptr) : BaseIterator<LeavesIterator<OctreeT>>(octree_ptr)
175  {
176  this->init();
177  };
178 
179  static constexpr bool has_ignore_condition = false;
180 
181  bool isNext(se::OctantBase* octant_ptr)
182  {
183  return octant_ptr->getChildrenMask() == 0;
184  }
185 
186  protected:
187  friend class BaseIterator<LeavesIterator<OctreeT>>;
188 };
189 
190 
191 
192 template<typename OctreeT>
193 class UpdateIterator : public BaseIterator<UpdateIterator<OctreeT>> {
194  public:
195  UpdateIterator() : BaseIterator<UpdateIterator<OctreeT>>(), time_stamp_(0){};
196 
197  UpdateIterator(OctreeT* octree_ptr, const unsigned int time_stamp) :
198  BaseIterator<UpdateIterator<OctreeT>>(octree_ptr), time_stamp_(time_stamp)
199  {
200  this->init();
201  };
202 
203  static constexpr bool has_ignore_condition = true;
204 
205  bool isNext(se::OctantBase* octant_ptr)
206  {
207  return octant_ptr->isBlock() && octant_ptr->getTimeStamp() >= time_stamp_;
208  }
209 
210  bool doIgnore(se::OctantBase* octant_ptr)
211  {
212  return octant_ptr->getTimeStamp() < time_stamp_;
213  }
214 
215  const unsigned int time_stamp_;
216 
217  protected:
218  friend class BaseIterator<UpdateIterator<OctreeT>>;
219 };
220 
221 
222 template<typename MapT, typename SensorT>
223 class FrustumIterator : public BaseIterator<FrustumIterator<MapT, SensorT>> {
224  public:
225  typedef typename MapT::OctreeType OctreeType;
226  typedef typename MapT::OctreeType::NodeType NodeType;
227  typedef typename MapT::OctreeType::BlockType BlockType;
228 
229 
231 
232  FrustumIterator(MapT& map, const SensorT& sensor, const Eigen::Matrix4f& T_SM) :
233  BaseIterator<FrustumIterator<MapT, SensorT>>(map.getOctree().get()),
234  map_ptr_(&map),
235  sensor_ptr_(&sensor),
236  T_SM_(T_SM)
237  {
238  this->init();
239  }
240 
241  protected:
242  bool isNext(se::OctantBase* octant_ptr)
243  {
244  return octant_ptr->isBlock();
245  }
246 
247  bool doIgnore(se::OctantBase* octant_ptr)
248  {
249  Eigen::Vector3f octant_centre_point_M;
250  const int octant_size =
251  se::octantops::octant_to_size<typename MapT::OctreeType>(octant_ptr);
252  map_ptr_->voxelToPoint(octant_ptr->getCoord(), octant_size, octant_centre_point_M);
253  // Convert it to the sensor frame.
254  const Eigen::Vector3f octant_centre_point_S =
255  (T_SM_ * octant_centre_point_M.homogeneous()).template head<3>();
256 
257  float octant_radius = std::sqrt(3.0f) / 2.0f * map_ptr_->getRes() * octant_size;
258  bool do_ignore = !sensor_ptr_->sphereInFrustum(octant_centre_point_S, octant_radius);
259  return do_ignore;
260  }
261 
262  static constexpr bool has_ignore_condition = true;
263 
264  MapT* map_ptr_;
265  const SensorT* sensor_ptr_;
266  const Eigen::Matrix4f T_SM_; // TODO: Needs to be ref?
267 
268  friend class BaseIterator<FrustumIterator<MapT, SensorT>>;
269 };
270 
271 
272 // Declare and define a base_traits specialization for the octree iterator:
273 template<typename OctreeT>
274 struct BaseTraits<OctreeIterator<OctreeT>> {
275  typedef OctreeT OctreeType;
276  typedef typename OctreeT::NodeType NodeType;
277  typedef typename OctreeT::BlockType BlockType;
278 };
279 
280 
281 
282 // Declare and define a base_traits specialization for the nodes iterator:
283 template<typename OctreeT>
284 struct BaseTraits<NodesIterator<OctreeT>> {
285  typedef OctreeT OctreeType;
286  typedef typename OctreeT::NodeType NodeType;
287  typedef typename OctreeT::BlockType BlockType;
288 };
289 
290 
291 
292 // Declare and define a base_traits specialization for the block iterator:
293 template<typename OctreeT>
294 struct BaseTraits<BlocksIterator<OctreeT>> {
295  typedef OctreeT OctreeType;
296  typedef typename OctreeT::NodeType NodeType;
297  typedef typename OctreeT::BlockType BlockType;
298 };
299 
300 
301 
302 // Declare and define a base_traits specialization for the leaves iterator:
303 template<typename OctreeT>
304 struct BaseTraits<LeavesIterator<OctreeT>> {
305  typedef OctreeT OctreeType;
306  typedef typename OctreeT::NodeType NodeType;
307  typedef typename OctreeT::BlockType BlockType;
308 };
309 
310 
311 
312 // Declare and define a base_traits specialization for the update iterator:
313 template<typename OctreeT>
314 struct BaseTraits<UpdateIterator<OctreeT>> {
315  typedef OctreeT OctreeType;
316  typedef typename OctreeT::NodeType NodeType;
317  typedef typename OctreeT::BlockType BlockType;
318 };
319 
320 // Declare and define a base_traits specialization for the update iterator:
321 template<typename MapT, typename SensorT>
322 struct BaseTraits<FrustumIterator<MapT, SensorT>> {
323  typedef typename MapT::OctreeType OctreeType;
324  typedef typename MapT::OctreeType::NodeType NodeType;
325  typedef typename MapT::OctreeType::BlockType BlockType;
326 };
327 
328 
329 } // namespace se
330 
331 #include "impl/iterator_impl.hpp"
332 
333 #endif // SE_ITERATOR_HPP
OctreeIterator(OctreeT *octree_ptr)
Definition: iterator.hpp:105
bool doIgnore(se::OctantBase *octant_ptr)
Definition: iterator.hpp:210
BaseTraits< DerivedT >::NodeType NodeType
Definition: iterator.hpp:42
OctreeT::NodeType NodeType
Definition: iterator.hpp:276
BaseTraits< DerivedT >::OctreeType OctreeType
Definition: iterator.hpp:41
Definition: iterator.hpp:193
int getTimeStamp() const
Get the time stamp of an octant.
Definition: octant.hpp:77
OctreeT::NodeType NodeType
Definition: iterator.hpp:316
MapT::OctreeType::NodeType NodeType
Definition: iterator.hpp:324
FrustumIterator(MapT &map, const SensorT &sensor, const Eigen::Matrix4f &T_SM)
Definition: iterator.hpp:232
OctreeT::NodeType NodeType
Definition: iterator.hpp:296
OctreeT OctreeType
Definition: iterator.hpp:275
OctreeIterator()
Definition: iterator.hpp:103
Definition: iterator.hpp:101
long difference_type
Definition: iterator.hpp:62
OctreeT OctreeType
Definition: iterator.hpp:315
MapT::OctreeType::BlockType BlockType
Definition: iterator.hpp:325
MapT::OctreeType OctreeType
Definition: iterator.hpp:323
bool isNext(se::OctantBase *octant_ptr)
Definition: iterator.hpp:135
BaseIterator & operator++()
LeavesIterator()
Definition: iterator.hpp:172
OctreeT::BlockType BlockType
Definition: iterator.hpp:307
Definition: iterator.hpp:147
OctreeT::BlockType BlockType
Definition: iterator.hpp:297
OctreeT OctreeType
Definition: iterator.hpp:295
MapT::OctreeType::BlockType BlockType
Definition: iterator.hpp:227
Definition: iterator.hpp:21
Definition: iterator.hpp:124
bool doIgnore(se::OctantBase *octant_ptr)
Definition: iterator.hpp:247
std::forward_iterator_tag iterator_category
Definition: iterator.hpp:66
BlocksIterator(OctreeT *octree_ptr)
Definition: iterator.hpp:151
bool isNext(se::OctantBase *)
Definition: iterator.hpp:112
se::OctantBase * operator*() const
const SensorT * sensor_ptr_
Definition: iterator.hpp:265
bool isNext(se::OctantBase *octant_ptr)
Definition: iterator.hpp:242
OctreeT OctreeType
Definition: iterator.hpp:305
unsigned int getChildrenMask() const
Definition: octant.hpp:114
bool operator!=(const BaseIterator &other) const
const unsigned int time_stamp_
Definition: iterator.hpp:215
LeavesIterator(OctreeT *octree_ptr)
Definition: iterator.hpp:174
OctreeT::NodeType NodeType
Definition: iterator.hpp:306
FrustumIterator()
Definition: iterator.hpp:230
MapT * map_ptr_
Definition: iterator.hpp:264
MapT::OctreeType::NodeType NodeType
Definition: iterator.hpp:226
bool isBlock() const
Verify if an octant is a block.
Definition: octant.hpp:40
OctreeT::BlockType BlockType
Definition: iterator.hpp:287
OctreeT OctreeType
Definition: iterator.hpp:285
Definition: iterator.hpp:223
Definition: iterator.hpp:170
bool isNext(se::OctantBase *octant_ptr)
Definition: iterator.hpp:158
bool isNext(se::OctantBase *octant_ptr)
Definition: iterator.hpp:205
OctreeT::BlockType BlockType
Definition: iterator.hpp:277
Iterates over all valid data in the octree at the last scale it was updated at.
Definition: iterator.hpp:39
bool isNext(se::OctantBase *octant_ptr)
Definition: iterator.hpp:181
const Eigen::Matrix4f T_SM_
Definition: iterator.hpp:266
bool operator==(const BaseIterator &other) const
NodesIterator()
Definition: iterator.hpp:126
MapT::OctreeType OctreeType
Definition: iterator.hpp:225
This class only helps to dynamic cast the octant to the right type and builds the base of nodes and b...
Definition: octant.hpp:24
OctreeT::BlockType BlockType
Definition: iterator.hpp:317
BaseTraits< DerivedT >::BlockType BlockType
Definition: iterator.hpp:43
OctreeT::NodeType NodeType
Definition: iterator.hpp:286
UpdateIterator(OctreeT *octree_ptr, const unsigned int time_stamp)
Definition: iterator.hpp:197
UpdateIterator()
Definition: iterator.hpp:195
Eigen::Vector3i getCoord() const
Get the voxel coordinates of the octant.
Definition: octant.hpp:50
Helper wrapper to allocate and de-allocate octants in the octree.
Definition: colour_utils.hpp:17
NodesIterator(OctreeT *octree_ptr)
Definition: iterator.hpp:128
BlocksIterator()
Definition: iterator.hpp:149