Skip to content

Commit 670fafc

Browse files
committed
fix const accessor
1 parent 0fb3160 commit 670fafc

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

bonxai_core/include/bonxai/bonxai.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ class VoxelGrid
356356
const VoxelGrid& grid_;
357357
mutable CoordT prev_root_coord_ = { std::numeric_limits<int32_t>::max(), 0, 0 };
358358
mutable CoordT prev_inner_coord_ = { std::numeric_limits<int32_t>::max(), 0, 0 };
359-
mutable InnerGrid* prev_inner_ptr_ = nullptr;
360-
mutable LeafGrid* prev_leaf_ptr_ = nullptr;
359+
mutable const InnerGrid* prev_inner_ptr_ = nullptr;
360+
mutable const LeafGrid* prev_leaf_ptr_ = nullptr;
361361
};
362362

363363
/** Class to be used to set and get values of a cell of the Grid.
@@ -426,7 +426,7 @@ class VoxelGrid
426426

427427
Accessor createAccessor() { return Accessor(*this); }
428428

429-
ConstAccessor createConstAccessor() { return Accessor(*this); }
429+
ConstAccessor createConstAccessor() const { return ConstAccessor(*this); }
430430

431431
[[nodiscard]] CoordT getRootKey(const CoordT& coord) const;
432432

@@ -716,7 +716,8 @@ inline DataT* VoxelGrid<DataT>::Accessor::value(const CoordT& coord,
716716
}
717717

718718
template <typename DataT>
719-
inline const DataT* VoxelGrid<DataT>::ConstAccessor::value(const CoordT& coord) const {
719+
inline const DataT* VoxelGrid<DataT>::ConstAccessor::value(const CoordT& coord) const
720+
{
720721
const CoordT inner_key = grid_.getInnerKey(coord);
721722

722723
if (inner_key != prev_inner_coord_)
@@ -794,7 +795,9 @@ VoxelGrid<DataT>::Accessor::getLeafGrid(const CoordT& coord, bool create_if_miss
794795
{
795796
return nullptr;
796797
}
797-
it = mutable_grid_.root_map.insert({ root_key, InnerGrid(mutable_grid_.INNER_BITS) }).first;
798+
it = mutable_grid_.root_map
799+
.insert({ root_key, InnerGrid(mutable_grid_.INNER_BITS) })
800+
.first;
798801
}
799802
inner_ptr = &(it->second);
800803
// update the cache
@@ -827,7 +830,7 @@ template <typename DataT>
827830
inline const typename VoxelGrid<DataT>::LeafGrid*
828831
VoxelGrid<DataT>::ConstAccessor::getLeafGrid(const CoordT& coord) const
829832
{
830-
InnerGrid* inner_ptr = prev_inner_ptr_;
833+
const InnerGrid* inner_ptr = prev_inner_ptr_;
831834
const CoordT root_key = grid_.getRootKey(coord);
832835

833836
if (root_key != prev_root_coord_ || !inner_ptr)
@@ -844,7 +847,7 @@ VoxelGrid<DataT>::ConstAccessor::getLeafGrid(const CoordT& coord) const
844847
}
845848

846849
const uint32_t inner_index = grid_.getInnerIndex(coord);
847-
auto& inner_data = inner_ptr->cell(inner_index);
850+
const auto& inner_data = inner_ptr->cell(inner_index);
848851

849852
if (!inner_ptr->mask().isOn(inner_index))
850853
{

examples/visitors.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ int main()
4040
const auto& gridConstRef = grid;
4141
gridConstRef.forEachCell(constVisitor);
4242

43+
auto const_accessor = gridConstRef.createConstAccessor();
44+
std::cout << "value at origing: "<< *(const_accessor.value({0, 0, 0})) << "\n";
45+
4346
std::cout << "DONE\n";
4447
return 0;
4548
}

0 commit comments

Comments
 (0)