supereight2
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
31 BlockSingleRes(const DataType init_data = DataType());
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, Id IdB, int BlockSize, typename DerivedT>
61class BlockMultiRes<Data<FldT, ColB, IdB>, BlockSize, DerivedT> {
62};
63
64
65
66template<Colour ColB, Id IdB, int BlockSize, typename DerivedT>
67class BlockMultiRes<Data<Field::TSDF, ColB, IdB>, BlockSize, DerivedT> {
68 public:
70
75
76 BlockMultiRes(const DataType init_data = DataType());
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, Id IdB, 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;
185
186 BlockMultiRes(const DataType init_data = DataType());
187
189
191
193
194 int getVoxelIdx(const Eigen::Vector3i& voxel_coord, const int scale) const;
195
196
197
199
200 const DataType& data(const Eigen::Vector3i& voxel_coord) const;
201
202 DataType& data(const Eigen::Vector3i& voxel_coord);
203
205
206 const DataType&
207 data(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out) const;
208
209 DataType& data(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out);
210
212
213 const DataType& data(const Eigen::Vector3i& voxel_coord, const int scale) const;
214
215 DataType& data(const Eigen::Vector3i& voxel_coord, const int scale);
216
217 const DataType& minData(const Eigen::Vector3i& voxel_coord) const;
218
219 DataType& minData(const Eigen::Vector3i& voxel_coord);
220
221 const DataType&
222 minData(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out) const;
223
224 DataType& minData(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out);
225
226 const DataType& minData(const Eigen::Vector3i& voxel_coord, const int scale) const;
227
228 DataType& minData(const Eigen::Vector3i& voxel_coord, const int scale);
229
230 const DataType& maxData(const Eigen::Vector3i& voxel_coord) const;
231
232 DataType& maxData(const Eigen::Vector3i& voxel_coord);
233
234 const DataType&
235 maxData(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out) const;
236
237 DataType& maxData(const Eigen::Vector3i& voxel_coord, const int scale_in, int& scale_out);
238
239 const DataType& maxData(const Eigen::Vector3i& voxel_coord, const int scale) const;
240
241 DataType& maxData(const Eigen::Vector3i& voxel_coord, const int scale);
242
246 void allocateDownTo(const int new_min_scale = 0);
247
251 void deleteUpTo(const int new_min_scale);
252
260 const DataType& data() const
261 {
262 assert(!block_data_.empty());
263 assert(block_data_.front());
264 return block_data_[0][0];
265 }
266
272 const DataType& minData() const
273 {
274 assert(!block_min_data_.empty());
275 assert(block_min_data_.front());
276 return block_min_data_[0][0];
277 }
278
286 const DataType& maxData() const
287 {
288 assert(!block_max_data_.empty());
289 assert(block_max_data_.front());
290 return block_max_data_[0][0];
291 }
292
293 const std::vector<DataType*>& blockData() const
294 {
295 return block_data_;
296 }
297
301 size_t currIntegrCount() const
302 {
303 return curr_integr_count_;
304 }
305
309 size_t currObservedCount() const
310 {
311 return curr_observed_count_;
312 }
313
318 {
319 curr_integr_count_++;
320 }
321
328
333
340
344 int buffer_scale() const
345 {
346 return buffer_scale_;
347 }
348 size_t bufferIntegrCount() const
349 {
350 return buffer_integr_count_;
351 }
352 size_t bufferObservedCount() const
353 {
354 return buffer_observed_count_;
355 }
356
361 void incrBufferIntegrCount(const bool do_increment = true);
362
363
369 void incrBufferObservedCount(const bool do_increment = true);
374
379
385 void initBuffer(const int buffer_scale);
386
393
403 const DataType& bufferData(const Eigen::Vector3i& voxel_coord) const;
404
414 DataType& bufferData(const Eigen::Vector3i& voxel_coord);
415
425 const DataType& bufferData(const int voxel_idx) const
426 {
427 assert(buffer_data_);
428 assert(voxel_idx >= 0);
429 assert(voxel_idx < math::cu(BlockSize >> buffer_scale_));
430 return buffer_data_[voxel_idx];
431 }
432
443 {
444 assert(buffer_data_);
445 assert(voxel_idx >= 0);
446 assert(voxel_idx < math::cu(BlockSize >> buffer_scale_));
447 return buffer_data_[voxel_idx];
448 }
449
459 const DataType& currData(const int voxel_idx) const
460 {
461 assert(curr_data_);
462 assert(voxel_idx >= 0);
463 assert(voxel_idx < math::cu(BlockSize >> current_scale));
464 return curr_data_[voxel_idx];
465 }
466
477 {
478 assert(curr_data_);
479 assert(voxel_idx >= 0);
480 assert(voxel_idx < math::cu(BlockSize >> current_scale));
481 return curr_data_[voxel_idx];
482 }
483
492 DataType* blockDataAtScale(const int scale);
493
502 DataType* blockMinDataAtScale(const int scale);
511 DataType* blockMaxDataAtScale(const int scale);
512
513 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
514
515 private:
516 std::vector<DataType*> block_data_;
517 std::vector<DataType*> block_min_data_;
518 std::vector<DataType*> block_max_data_;
519
520 DataType* curr_data_ = nullptr;
521 size_t curr_integr_count_;
522 size_t curr_observed_count_;
523
547 DataType* buffer_data_ = nullptr;
548 int buffer_scale_ = -1;
549 size_t
550 buffer_integr_count_;
551 size_t buffer_observed_count_;
552
553 const DerivedT* underlying() const
554 {
555 return static_cast<const DerivedT*>(this);
556 }
557};
558
559
560
567template<typename DataT, Res ResT, int BlockSize>
568class Block : public OctantBase,
569 public std::conditional<
570 ResT == Res::Single,
571 BlockSingleRes<DataT, BlockSize, Block<DataT, ResT, BlockSize>>,
572 BlockMultiRes<DataT, BlockSize, Block<DataT, ResT, BlockSize>>>::type
573
574{
575 public:
577
579 static constexpr int size = BlockSize;
581 static constexpr int size_sq = math::sq(BlockSize);
583 static constexpr int size_cu = math::cu(BlockSize);
584
589 Block(Node<DataT, ResT>* parent_ptr, const int child_idx, const DataT init_data);
590
592
594};
595
596} // namespace se
597
598#include "impl/block_impl.hpp"
599
600#endif // SE_BLOCK_HPP
DataType & bufferData(const int voxel_idx)
Get a reference to the voxel data in the buffer at the voxel index.
Definition block.hpp:442
const DataType & maxData(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out) const
bool switchData()
Check if the scale should be switched from the current scale to the recommended.
void initCurrCout()
When a block is initialised from an observed block (i.e.
void incrCurrIntegrCount()
Increment the number of integrations at the current scale by 1.
Definition block.hpp:317
BlockMultiRes(const Block< Data< Field::Occupancy, ColB, IdB >, Res::Multi, BlockSize > &block)
const DataType & data(const Eigen::Vector3i &voxel_coord, const int scale) const
Get data at scale.
DataType & minData(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out)
void incrCurrObservedCount(bool do_increment=true)
Increment the number of observed voxels in at the current scale by 1.
const DataType & minData() const
Get the block's min data at the coarsest scale.
Definition block.hpp:272
void initBuffer(const int buffer_scale)
Init buffer variables.
const DataType & maxData() const
Get the block's max data at the coarsest scale.
Definition block.hpp:286
DataType & data(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out)
void operator=(const Block< Data< Field::Occupancy, ColB, IdB >, Res::Multi, BlockSize > &block)
const DataType & data(const Eigen::Vector3i &voxel_coord) const
Get data at current scale.
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 * blockMaxDataAtScale(const int scale)
Get a pointer to the max block data array at a given scale.
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:425
DataType * blockDataAtScale(const int scale)
Get a pointer to the mean block data array at a given scale.
int getVoxelIdx(const Eigen::Vector3i &voxel_coord, const int scale) const
size_t currObservedCount() const
Get the number of observed voxels at the current scale.
Definition block.hpp:309
const DataType & data(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out) const
Get data at current scale or coarser.
void allocateDownTo(const int new_min_scale=0)
Allocate the mip-mapped scales down to 'new_min_scale'.
void incrBufferIntegrCount(const bool do_increment=true)
Increment the buffer count if incrementation criterion is met.
DataType & maxData(const Eigen::Vector3i &voxel_coord, const int scale)
DataType & data(const Eigen::Vector3i &voxel_coord, const int scale)
DataType & minData(const Eigen::Vector3i &voxel_coord, const int scale)
void resetBufferCount()
Reset the buffer integration and observation count to 0.
const DataType & minData(const Eigen::Vector3i &voxel_coord, const int scale) const
void resetBuffer()
Reset buffer variables to the initial values and free the buffer data if applicable.
const DataType & maxData(const Eigen::Vector3i &voxel_coord) const
Data< Field::Occupancy, ColB, IdB > DataType
Definition block.hpp:179
const DataType & minData(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out) const
DataType & bufferData(const Eigen::Vector3i &voxel_coord)
Get a reference to the voxel data in the buffer at the voxel coordinates.
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:476
const DataType & maxData(const Eigen::Vector3i &voxel_coord, const int scale) const
size_t currIntegrCount() const
Get the number of integrations at the current scale.
Definition block.hpp:301
const std::vector< DataType * > & blockData() const
Definition block.hpp:293
const DataType & minData(const Eigen::Vector3i &voxel_coord) const
DataType * blockMinDataAtScale(const int scale)
Get a pointer to the min block data array at a given scale.
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:459
DataType & maxData(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out)
void deleteUpTo(const int new_min_scale)
Delete the mip-mapped scales up to 'new_min_scale'.
const DataType & data() const
Get the block's data at the coarsest scale.
Definition block.hpp:260
void incrBufferObservedCount(const bool do_increment=true)
Increment the number of observed voxels at the buffers scale by 1.
void resetCurrCount()
Reset the current integration and observation count to 0.
DataType & data(const Eigen::Vector3i &voxel_coord, const int scale_in, int &scale_out)
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) const
Get data at scale.
Data< Field::TSDF, ColB, IdB > DataType
Definition block.hpp:69
DataUnion dataUnion(const Eigen::Vector3i &voxel_coord, const int scale)
const DataType & data(const int voxel_idx) const
Get data at current scale.
const DataType & data() const
Get coarsest block data.
int getVoxelIdx(const Eigen::Vector3i &voxel_coord, const int scale) const
const DataType & data(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.
DataType & data(const Eigen::Vector3i &voxel_coord, const int scale)
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:574
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition block.hpp:593
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:581
DataT DataType
Definition block.hpp:576
static constexpr int size_cu
The volume of the block in voxels.
Definition block.hpp:583
static constexpr int size
The edge length of the block in voxels.
Definition block.hpp:579
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