Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Aug 1, 2024
1 parent 36311d9 commit 0fb3160
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
14 changes: 9 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
Expand All @@ -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)
45 changes: 45 additions & 0 deletions examples/visitors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "bonxai/bonxai.hpp"
#include <iostream>

int main()
{
const double VOXEL_RESOLUTION = 0.1;

Bonxai::VoxelGrid<int> 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;
}

0 comments on commit 0fb3160

Please sign in to comment.