diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b26f41c..0da3d2c 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,4 +1,13 @@ +add_executable(visitors visitors.cpp) +target_link_libraries(visitors PUBLIC bonxai_core) + +############################################### +add_executable(test_serialization test_serialization.cpp) +target_link_libraries(test_serialization PUBLIC bonxai_core) + +############################################### + add_executable(bonxai_map_playground bonxai_map_playground.cpp) target_include_directories(bonxai_map_playground PUBLIC ${octomap_INCLUDE_DIRS}) @@ -8,8 +17,3 @@ target_link_libraries(bonxai_map_playground PUBLIC ${OCTOMAP_LIBRARIES} ) -############################################### -add_executable(test_serialization test_serialization.cpp) - -target_link_libraries(test_serialization PUBLIC - bonxai_core) diff --git a/examples/visitors.cpp b/examples/visitors.cpp new file mode 100644 index 0000000..44fa8dc --- /dev/null +++ b/examples/visitors.cpp @@ -0,0 +1,45 @@ +#include "bonxai/bonxai.hpp" +#include + +int main() +{ + const double VOXEL_RESOLUTION = 0.1; + + Bonxai::VoxelGrid grid(VOXEL_RESOLUTION); + // to modify a grid, we need a mutable accessor + auto accessor = grid.createAccessor(); + + int count = 0; + for (double x = -0.5; x < 0.5; x += VOXEL_RESOLUTION) + { + for (double y = -0.5; y < 0.5; y += VOXEL_RESOLUTION) + { + for (double z = -0.5; z < 0.5; z += VOXEL_RESOLUTION) + { + accessor.setValue(grid.posToCoord(x, y, z), count++); + } + } + } + + // To iterate throught all the cells, we can use visitors. + auto mutableVisitor = [](int& value, const Bonxai::CoordT&) + { + value = 1; + }; + auto& gridRef = grid; + gridRef.forEachCell(mutableVisitor); + + // Const version of the visitor + auto constVisitor = [](const int& value, const Bonxai::CoordT&) + { + if(value != 1) + { + throw std::runtime_error("unexpected"); + } + }; + const auto& gridConstRef = grid; + gridConstRef.forEachCell(constVisitor); + + std::cout << "DONE\n"; + return 0; +}