supereight
Loading...
Searching...
No Matches
block.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016-2019 Emanuele Vespa
3 * SPDX-FileCopyrightText: 2019-2021 Smart Robotics Lab, Imperial College London, Technical University of Munich
4 * SPDX-FileCopyrightText: 2019-2021 Nils Funk
5 * SPDX-FileCopyrightText: 2019-2021 Sotiris Papatheodorou
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#ifndef SE_BLOCK_HPP
10#define SE_BLOCK_HPP
11
12namespace se {
13
14// Forward declare Node to avoid depending on the order of includes.
15template<typename DataT, Res ResT>
16class Node;
17
18
19
23template<typename DataT, int BlockSize, typename DerivedT>
25 public:
26 typedef DataT DataType;
27
28 static constexpr int min_scale = 0;
29 static constexpr int current_scale = 0;
30
32
33 const DataType& data(const int voxel_idx) const;
34
35 DataType& data(const int voxel_idx);
36
37 const DataType& data(const Eigen::Vector3i& voxel_coord) const;
38
39 DataType& data(const Eigen::Vector3i& voxel_coord);
40
41 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42
43 private:
44 std::array<DataType, BlockSize * BlockSize * BlockSize> block_data_;
45
46 const DerivedT* underlying() const
47 {
48 return static_cast<const DerivedT*>(this);
49 }
50};
51
52
53
54template<typename DataT, int BlockSize, typename DerivedT>
56};
57
58
59
60template<Field FldT, Colour ColB, Semantics SemB, int BlockSize, typename DerivedT>
61class BlockMultiRes<Data<FldT, ColB, SemB>, BlockSize, DerivedT> {
62};
63
64
65
66template<Colour ColB, Semantics SemB, int BlockSize, typename DerivedT>
68 public:
70
75
77
78 struct DataUnion {
79 const Eigen::Vector3i coord;
80 const int scale;
83 const int data_idx;
84 };
85
86 static constexpr int max_scale = math::log2_const(BlockSize);
87 int min_scale = -1;
88 int current_scale = -1;
89
90 int getVoxelIdx(const Eigen::Vector3i& voxel_coord, const int scale) const;
91
93
94 const DataType& data() const;
95
97
99
100 const DataType& data(const int voxel_idx) const;
101
103
104 const DataType& data(const Eigen::Vector3i& voxel_coord) const;
105
106 DataType& data(const Eigen::Vector3i& voxel_coord);
107
109
110 const DataType&
111 data(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out) const;
112
113 DataType& data(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out);
114
116
117 const DataType& data(const Eigen::Vector3i& voxel_coord, const int scale) const;
118
119 DataType& data(const Eigen::Vector3i& voxel_coord, const int scale);
120
121 DataUnion dataUnion(const Eigen::Vector3i& voxel_coord, const int scale);
122
123 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
124
125 private:
126 static constexpr int compute_num_voxels()
127 {
128 size_t voxel_count = 0;
129 for (int size_at_scale = BlockSize; size_at_scale > 0; size_at_scale /= 2) {
131 }
132 return voxel_count;
133 }
134
135 static constexpr std::array<int, max_scale + 1> compute_size_at_scales()
136 {
137 std::array<int, max_scale + 1> size_at_scales{};
138 for (int size_at_scale = BlockSize, scale = 0; size_at_scale > 0;
139 size_at_scale /= 2, scale++) {
140 size_at_scales[scale] = size_at_scale;
141 }
142 return size_at_scales;
143 }
144
145 static constexpr std::array<int, max_scale + 1> compute_scale_offsets()
146 {
147 std::array<int, max_scale + 1> scale_offsets{0};
148 for (int size_at_scale = BlockSize, scale = 1; size_at_scale > 1;
149 size_at_scale /= 2, scale++) {
150 scale_offsets[scale] = scale_offsets[scale - 1] + math::cu(size_at_scale);
151 }
152 return scale_offsets;
153 }
154
155 static constexpr int num_voxels_ = compute_num_voxels();
156 static constexpr std::array<int, max_scale + 1> size_at_scales_ = compute_size_at_scales();
157 static constexpr std::array<int, max_scale + 1> scale_offsets_ = compute_scale_offsets();
158
159 std::array<DataType, num_voxels_> block_data_;
160 std::array<PastDataType, num_voxels_> block_past_data_;
161
162 const DerivedT* underlying() const
163 {
164 return static_cast<const DerivedT*>(this);
165 }
166};
167
168
169
170// Forward decleration
171template<typename DataT, Res ResT, int BlockSize>
172class Block;
173
174
175
176template<Colour ColB, Semantics SemB, int BlockSize, typename DerivedT>
178 public:
180
181 static constexpr int max_scale = math::log2_const(BlockSize);
182 int min_scale = -1;
183 int current_scale = max_scale;
184
186
188
190
192
194
195 const DataType& initData() const
196 {
197 return init_data_;
198 };
199
201 {
202 return init_data_;
203 };
204
205 int getVoxelIdx(const Eigen::Vector3i& voxel_coord, const int scale) const;
206
207
208
210
211 const DataType& data(const Eigen::Vector3i& voxel_coord) const;
212
213 DataType& data(const Eigen::Vector3i& voxel_coord);
214
216
217 const DataType&
218 data(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out) const;
219
220 DataType& data(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out);
221
223
224 const DataType& data(const Eigen::Vector3i& voxel_coord, const int scale) const;
225
226 DataType& data(const Eigen::Vector3i& voxel_coord, const int scale);
227
228 const DataType& minData(const Eigen::Vector3i& voxel_coord) const;
229
230 DataType& minData(const Eigen::Vector3i& voxel_coord);
231
232 const DataType&
233 minData(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out) const;
234
235 DataType& minData(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out);
236
237 const DataType& minData(const Eigen::Vector3i& voxel_coord, const int scale) const;
238
239 DataType& minData(const Eigen::Vector3i& voxel_coord, const int scale);
240
241 const DataType& maxData(const Eigen::Vector3i& voxel_coord) const;
242
243 DataType& maxData(const Eigen::Vector3i& voxel_coord);
244
245 const DataType&
246 maxData(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out) const;
247
248 DataType& maxData(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out);
249
250 const DataType& maxData(const Eigen::Vector3i& voxel_coord, const int scale) const;
251
252 DataType& maxData(const Eigen::Vector3i& voxel_coord, const int scale);
253
257 void allocateDownTo(const int new_min_scale = 0);
258
262 void deleteUpTo(const int new_min_scale);
263
271 const DataType& data() const
272 {
273 assert(!block_data_.empty());
274 assert(block_data_.front());
275 return block_data_[0][0];
276 }
277
283 const DataType& minData() const
284 {
285 assert(!block_min_data_.empty());
286 assert(block_min_data_.front());
287 return block_min_data_[0][0];
288 }
289
297 const DataType& maxData() const
298 {
299 assert(!block_max_data_.empty());
300 assert(block_max_data_.front());
301 return block_max_data_[0][0];
302 }
303
304 const std::vector<DataType*>& blockData() const
305 {
306 return block_data_;
307 }
308
309 std::vector<DataType*>& blockData()
310 {
311 return block_data_;
312 }
313
314 const std::vector<DataType*>& blockMinData() const
315 {
316 return block_min_data_;
317 }
318
319 std::vector<DataType*>& blockMinData()
320 {
321 return block_min_data_;
322 }
323
324 const std::vector<DataType*>& blockMaxData() const
325 {
326 return block_max_data_;
327 }
328
329 std::vector<DataType*>& blockMaxData()
330 {
331 return block_max_data_;
332 }
333
337 size_t currIntegrCount() const
338 {
339 return curr_integr_count_;
340 }
341
345 size_t currObservedCount() const
346 {
347 return curr_observed_count_;
348 }
349
354 {
355 curr_integr_count_++;
356 }
357
364
369
376
380 int buffer_scale() const
381 {
382 return buffer_scale_;
383 }
384 size_t bufferIntegrCount() const
385 {
386 return buffer_integr_count_;
387 }
388 size_t bufferObservedCount() const
389 {
390 return buffer_observed_count_;
391 }
392
397 void incrBufferIntegrCount(const bool do_increment = true);
398
399
405 void incrBufferObservedCount(const bool do_increment = true);
410
415
421 void initBuffer(const int buffer_scale);
422
429
439 const DataType& bufferData(const Eigen::Vector3i& voxel_coord) const;
440
450 DataType& bufferData(const Eigen::Vector3i& voxel_coord);
451
461 const DataType& bufferData(const int voxel_idx) const
462 {
463 assert(buffer_data_);
464 assert(voxel_idx >= 0);
465 assert(voxel_idx < math::cu(BlockSize >> buffer_scale_));
466 return buffer_data_[voxel_idx];
467 }
468
479 {
480 assert(buffer_data_);
481 assert(voxel_idx >= 0);
482 assert(voxel_idx < math::cu(BlockSize >> buffer_scale_));
483 return buffer_data_[voxel_idx];
484 }
485
495 const DataType& currData(const int voxel_idx) const
496 {
497 assert(curr_data_);
498 assert(voxel_idx >= 0);
499 assert(voxel_idx < math::cu(BlockSize >> current_scale));
500 return curr_data_[voxel_idx];
501 }
502
513 {
514 assert(curr_data_);
515 assert(voxel_idx >= 0);
516 assert(voxel_idx < math::cu(BlockSize >> current_scale));
517 return curr_data_[voxel_idx];
518 }
519
528 DataType* blockDataAtScale(const int scale);
529
538 DataType* blockMinDataAtScale(const int scale);
547 DataType* blockMaxDataAtScale(const int scale);
548
549 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
550
551 private:
553 void initialiseData(DataType* voxel_data, const int num_voxels)
554 {
556 assert(num_voxels >= 0);
557 std::fill(voxel_data, voxel_data + num_voxels, init_data_);
558 }
559
560 std::vector<DataType*> block_data_;
561 std::vector<DataType*> block_min_data_;
562 std::vector<DataType*> block_max_data_;
563
564 DataType* curr_data_ = nullptr;
565 size_t curr_integr_count_;
566 size_t curr_observed_count_;
567
591 DataType* buffer_data_ = nullptr;
592 int buffer_scale_ = -1;
593 size_t
594 buffer_integr_count_;
595 size_t buffer_observed_count_;
596
597 DataType init_data_;
598
599 const DerivedT* underlying() const
600 {
601 return static_cast<const DerivedT*>(this);
602 }
603};
604
605
606
613template<typename DataT, Res ResT, int BlockSize>
614class Block : public OctantBase,
615 public std::conditional<
616 ResT == Res::Single,
617 BlockSingleRes<DataT, BlockSize, Block<DataT, ResT, BlockSize>>,
618 BlockMultiRes<DataT, BlockSize, Block<DataT, ResT, BlockSize>>>::type
619
620{
621 public:
623
625 static constexpr int size = BlockSize;
627 static constexpr int size_sq = math::sq(BlockSize);
629 static constexpr int size_cu = math::cu(BlockSize);
630
636
638
640};
641
642} // namespace se
643
644#include "impl/block_impl.hpp"
645
646#endif // SE_BLOCK_HPP
const std::vector< DataType * > & blockMinData() const
Definition block.hpp:314
const DataType & maxData(const Eigen::Vector3i &voxel_coord, const int scale) const
const DataType & bufferData(const int voxel_idx) const
Get a const reference to the voxel data in the buffer at the voxel index.
Definition block.hpp:461
const DataType & data(const Eigen::Vector3i &voxel_coord, const int scale) const
Get data at scale.
std::vector< DataType * > & blockData()
Definition block.hpp:309
DataType * blockMinDataAtScale(const int scale)
Get a pointer to the min block data array at a given scale.
DataType & data(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out)
const std::vector< DataType * > & blockMaxData() const
Definition block.hpp:324
void incrBufferObservedCount(const bool do_increment=true)
Increment the number of observed voxels at the buffers scale by 1.
const DataType & minData() const
Get the block's min data at the coarsest scale.
Definition block.hpp:283
DataType & currData(const int voxel_idx)
Get a reference to the mean voxel data at the current scale via the voxel index.
Definition block.hpp:512
Data< Field::Occupancy, ColB, SemB > DataType
Definition block.hpp:179
void incrCurrObservedCount(bool do_increment=true)
Increment the number of observed voxels in at the current scale by 1.
void incrCurrIntegrCount()
Increment the number of integrations at the current scale by 1.
Definition block.hpp:353
const DataType & initData() const
Get init block data.
Definition block.hpp:195
const DataType & maxData(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out) const
const DataType & minData(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out) const
DataType & data(const Eigen::Vector3i &voxel_coord, const int scale)
void resetBuffer()
Reset buffer variables to the initial values and free the buffer data if applicable.
const DataType & maxData() const
Get the block's max data at the coarsest scale.
Definition block.hpp:297
void resetCurrCount()
Reset the current integration and observation count to 0.
BlockMultiRes(const Block< Data< Field::Occupancy, ColB, SemB >, Res::Multi, BlockSize > &block)
size_t currIntegrCount() const
Get the number of integrations at the current scale.
Definition block.hpp:337
void initBuffer(const int buffer_scale)
Init buffer variables.
int getVoxelIdx(const Eigen::Vector3i &voxel_coord, const int scale) const
DataType & bufferData(const int voxel_idx)
Get a reference to the voxel data in the buffer at the voxel index.
Definition block.hpp:478
std::vector< DataType * > & blockMinData()
Definition block.hpp:319
void initCurrCout()
When a block is initialised from an observed block (i.e.
const std::vector< DataType * > & blockData() const
Definition block.hpp:304
void resetBufferCount()
Reset the buffer integration and observation count to 0.
void incrBufferIntegrCount(const bool do_increment=true)
Increment the buffer count if incrementation criterion is met.
const DataType & minData(const Eigen::Vector3i &voxel_coord, const int scale) const
void deleteUpTo(const int new_min_scale)
Delete the mip-mapped scales up to 'new_min_scale'.
DataType & minData(const Eigen::Vector3i &voxel_coord, const int scale)
std::vector< DataType * > & blockMaxData()
Definition block.hpp:329
size_t currObservedCount() const
Get the number of observed voxels at the current scale.
Definition block.hpp:345
const DataType & currData(const int voxel_idx) const
Get a const reference to the mean voxel data at the current scale via the voxel index.
Definition block.hpp:495
bool switchData()
Check if the scale should be switched from the current scale to the recommended.
DataType * blockDataAtScale(const int scale)
Get a pointer to the mean block data array at a given scale.
const DataType & data() const
Get the block's data at the coarsest scale.
Definition block.hpp:271
void allocateDownTo(const int new_min_scale=0)
Allocate the mip-mapped scales down to 'new_min_scale'.
DataType & maxData(const Eigen::Vector3i &voxel_coord, const int scale)
DataType & maxData(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out)
const DataType & data(const Eigen::Vector3i &voxel_coord) const
Get data at current scale.
const DataType & maxData(const Eigen::Vector3i &voxel_coord) const
const DataType & data(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out) const
Get data at current scale or coarser.
void operator=(const Block< Data< Field::Occupancy, ColB, SemB >, Res::Multi, BlockSize > &block)
DataType * blockMaxDataAtScale(const int scale)
Get a pointer to the max block data array at a given scale.
DataType & bufferData(const Eigen::Vector3i &voxel_coord)
Get a reference to the voxel data in the buffer at the voxel coordinates.
DataType & minData(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out)
const DataType & minData(const Eigen::Vector3i &voxel_coord) const
const DataType & bufferData(const Eigen::Vector3i &voxel_coord) const
Get a const reference to the voxel data in the buffer at the voxel coordinates.
DataType & data(const Eigen::Vector3i &voxel_coord, const int scale)
const DataType & data(const Eigen::Vector3i &voxel_coord, const int scale) const
Get data at scale.
int getVoxelIdx(const Eigen::Vector3i &voxel_coord, const int scale) const
DataType & data(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out)
const DataType & data(const Eigen::Vector3i &voxel_coord) const
const DataType & data() const
Get coarsest block data.
Data< Field::TSDF, ColB, SemB > DataType
Definition block.hpp:69
const DataType & data(const int voxel_idx) const
Get data at current scale.
DataUnion dataUnion(const Eigen::Vector3i &voxel_coord, const int scale)
DataType PastDataType
Contains data from a previous point in time which is used to compute changes over time for delta down...
Definition block.hpp:74
const DataType & data(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out) const
Get data at current scale or coarser.
Definition block.hpp:55
The base used for single-resolution blocks.
Definition block.hpp:24
DataType & data(const Eigen::Vector3i &voxel_coord)
const DataType & data(const Eigen::Vector3i &voxel_coord) const
DataT DataType
Definition block.hpp:26
DataType & data(const int voxel_idx)
static constexpr int min_scale
Definition block.hpp:28
BlockSingleRes(const DataType init_data=DataType())
const DataType & data(const int voxel_idx) const
static constexpr int current_scale
Definition block.hpp:29
A leaf node of an se::Octree.
Definition block.hpp:620
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition block.hpp:639
Block(Node< DataT, ResT > *parent_ptr, const int child_idx, const DataT init_data)
Construct the child block of parent_ptr with index child_idx and initialize its data at the coarsest ...
static constexpr int size_sq
The face area of the block in voxels.
Definition block.hpp:627
DataT DataType
Definition block.hpp:622
static constexpr int size_cu
The volume of the block in voxels.
Definition block.hpp:629
static constexpr int size
The edge length of the block in voxels.
Definition block.hpp:625
Definition image.hpp:19
The base class of all octants (se::Node and se::Block) in an se::Octree.
Definition octant.hpp:19
constexpr Scalar cu(Scalar a)
constexpr bool is_power_of_two(const T x)
constexpr int log2_const(int n)
constexpr Scalar sq(Scalar a)
Helper wrapper to allocate and de-allocate octants in the octree.
Definition bounded_vector.hpp:14
Field
Definition setup_util.hpp:18
Definition data.hpp:19