-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move code to C++ library and add support for other projection models
- Loading branch information
1 parent
60983e7
commit 21a6143
Showing
14 changed files
with
272 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
library/cpp/include/wavemap/core/utils/render/impl/raycast_inl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef WAVEMAP_CORE_UTILS_RENDER_IMPL_RAYCAST_INL_H_ | ||
#define WAVEMAP_CORE_UTILS_RENDER_IMPL_RAYCAST_INL_H_ | ||
|
||
namespace wavemap::raycast { | ||
template <typename MapT> | ||
std::optional<Index3D> first_collision_index( | ||
MapT& map, const Point3D& W_start_point, const Point3D& W_end_point, | ||
FloatingPoint log_odds_occupancy_threshold) { | ||
const Ray ray(W_start_point, W_end_point, map.getMinCellWidth()); | ||
for (const Index3D& ray_voxel_index : ray) { | ||
if (log_odds_occupancy_threshold < map.getCellValue(ray_voxel_index)) { | ||
return ray_voxel_index; | ||
} | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
template <typename MapT> | ||
std::optional<FloatingPoint> first_collision_distance( | ||
MapT& map, const Point3D& W_start_point, const Point3D& W_end_point, | ||
FloatingPoint log_odds_occupancy_threshold) { | ||
const auto colliding_index = first_collision_index<MapT>( | ||
map, W_start_point, W_end_point, log_odds_occupancy_threshold); | ||
if (colliding_index) { | ||
const FloatingPoint min_cell_width = map.getMinCellWidth(); | ||
const Point3D voxel_center = | ||
convert::indexToCenterPoint(colliding_index.value(), min_cell_width); | ||
return (voxel_center - W_start_point).norm(); | ||
} | ||
return std::nullopt; | ||
} | ||
} // namespace wavemap::raycast | ||
|
||
#endif // WAVEMAP_CORE_UTILS_RENDER_IMPL_RAYCAST_INL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef WAVEMAP_CORE_UTILS_RENDER_RAYCAST_H_ | ||
#define WAVEMAP_CORE_UTILS_RENDER_RAYCAST_H_ | ||
|
||
#include "wavemap/core/common.h" | ||
#include "wavemap/core/utils/iterate/ray_iterator.h" | ||
|
||
namespace wavemap::raycast { | ||
// NOTE: The map can either be a raw map, or a QueryAccelerator instance. We | ||
// strongly recommend passing a QueryAccelerator to get good performance. | ||
template <typename MapT> | ||
std::optional<Index3D> first_collision_index( | ||
MapT& map, const Point3D& W_start_point, const Point3D& W_end_point, | ||
FloatingPoint log_odds_occupancy_threshold); | ||
|
||
// NOTE: The map can either be a raw map, or a QueryAccelerator instance. We | ||
// strongly recommend passing a QueryAccelerator to get good performance. | ||
template <typename MapT> | ||
std::optional<FloatingPoint> first_collision_distance( | ||
MapT& map, const Point3D& W_start_point, const Point3D& W_end_point, | ||
FloatingPoint log_odds_occupancy_threshold); | ||
} // namespace wavemap::raycast | ||
|
||
#include "wavemap/core/utils/render/impl/raycast_inl.h" | ||
|
||
#endif // WAVEMAP_CORE_UTILS_RENDER_RAYCAST_H_ |
43 changes: 43 additions & 0 deletions
43
library/cpp/include/wavemap/core/utils/render/raycasting_renderer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef WAVEMAP_CORE_UTILS_RENDER_RAYCASTING_RENDERER_H_ | ||
#define WAVEMAP_CORE_UTILS_RENDER_RAYCASTING_RENDERER_H_ | ||
|
||
#include <memory> | ||
|
||
#include "wavemap/core/data_structure/image.h" | ||
#include "wavemap/core/integrator/projection_model/projector_base.h" | ||
#include "wavemap/core/map/map_base.h" | ||
#include "wavemap/core/utils/thread_pool.h" | ||
|
||
namespace wavemap { | ||
class RaycastingRenderer { | ||
public: | ||
RaycastingRenderer(MapBase::ConstPtr occupancy_map, | ||
ProjectorBase::ConstPtr projection_model, | ||
FloatingPoint log_odds_occupancy_threshold = 1e-3f, | ||
FloatingPoint min_range = 0.f, | ||
FloatingPoint max_range = 10.f, | ||
FloatingPoint default_depth_value = -1.f, | ||
std::shared_ptr<ThreadPool> thread_pool = nullptr); | ||
|
||
const Image<>& render(const Transformation3D& T_W_C); | ||
|
||
private: | ||
static constexpr int kPatchWidth = 64; | ||
|
||
const MapBase::ConstPtr map_; | ||
const std::shared_ptr<ThreadPool> thread_pool_; | ||
|
||
const ProjectorBase::ConstPtr projection_model_; | ||
const FloatingPoint min_range_; | ||
const FloatingPoint max_range_; | ||
const FloatingPoint log_odds_occupancy_threshold_; | ||
|
||
Image<FloatingPoint> depth_image_; | ||
|
||
template <typename MapT> | ||
void renderPatch(const MapT& map, const Transformation3D& T_W_C, | ||
const Index2D& patch_index); | ||
}; | ||
} // namespace wavemap | ||
|
||
#endif // WAVEMAP_CORE_UTILS_RENDER_RAYCASTING_RENDERER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "wavemap/core/utils/render/raycasting_renderer.h" | ||
|
||
#include <memory> | ||
#include <utility> | ||
|
||
#include "wavemap/core/utils/iterate/grid_iterator.h" | ||
#include "wavemap/core/utils/query/query_accelerator.h" | ||
#include "wavemap/core/utils/render/raycast.h" | ||
|
||
namespace wavemap { | ||
RaycastingRenderer::RaycastingRenderer( | ||
MapBase::ConstPtr occupancy_map, ProjectorBase::ConstPtr projection_model, | ||
FloatingPoint log_odds_occupancy_threshold, FloatingPoint min_range, | ||
FloatingPoint max_range, FloatingPoint default_depth_value, | ||
std::shared_ptr<ThreadPool> thread_pool) | ||
: map_(CHECK_NOTNULL(occupancy_map)), | ||
thread_pool_(thread_pool ? std::move(thread_pool) | ||
: std::make_shared<ThreadPool>()), | ||
projection_model_(CHECK_NOTNULL(projection_model)), | ||
min_range_(min_range), | ||
max_range_(max_range), | ||
log_odds_occupancy_threshold_(log_odds_occupancy_threshold), | ||
depth_image_(projection_model_->getDimensions(), default_depth_value) {} | ||
|
||
template <typename MapT> | ||
void RaycastingRenderer::renderPatch(const MapT& map, | ||
const Transformation3D& T_W_C, | ||
const Index2D& patch_index) { | ||
QueryAccelerator query_accelerator(map); | ||
|
||
const Index2D patch_min = patch_index * kPatchWidth; | ||
const Index2D patch_max = (patch_min.array() + kPatchWidth) | ||
.min(depth_image_.getDimensions().array()) - | ||
1; | ||
|
||
for (const Index2D& index : Grid(patch_min, patch_max)) { | ||
FloatingPoint& depth_pixel = depth_image_.at(index); | ||
const Vector2D image_xy = projection_model_->indexToImage(index); | ||
const Point3D C_start_point = | ||
projection_model_->sensorToCartesian({image_xy, min_range_}); | ||
const Point3D C_end_point = | ||
projection_model_->sensorToCartesian({image_xy, max_range_}); | ||
const Point3D W_start_point = T_W_C * C_start_point; | ||
const Point3D W_end_point = T_W_C * C_end_point; | ||
const auto d_start_collision = raycast::first_collision_distance( | ||
query_accelerator, W_start_point, W_end_point, | ||
log_odds_occupancy_threshold_); | ||
if (d_start_collision) { | ||
depth_pixel = d_start_collision.value(); | ||
} | ||
} | ||
} | ||
|
||
const Image<>& RaycastingRenderer::render(const Transformation3D& T_W_C) { | ||
depth_image_.resetToInitialValue(); | ||
|
||
const Index2D num_patches = | ||
int_math::div_round_up(depth_image_.getDimensions(), kPatchWidth); | ||
for (const Index2D& patch_index : | ||
Grid<2>(Index2D::Zero(), num_patches - Index2D::Ones())) { | ||
thread_pool_->add_task([this, &T_W_C, patch_index]() { | ||
if (const auto* map = | ||
dynamic_cast<const HashedWaveletOctree*>(map_.get()); | ||
map) { | ||
renderPatch(*map, T_W_C, patch_index); | ||
} | ||
if (const auto* map = | ||
dynamic_cast<const HashedChunkedWaveletOctree*>(map_.get()); | ||
map) { | ||
renderPatch(*map, T_W_C, patch_index); | ||
} | ||
}); | ||
} | ||
thread_pool_->wait_all(); | ||
|
||
return depth_image_; | ||
} | ||
} // namespace wavemap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef PYWAVEMAP_RENDER_H_ | ||
#define PYWAVEMAP_RENDER_H_ | ||
|
||
#include <nanobind/nanobind.h> | ||
|
||
namespace nb = nanobind; | ||
|
||
namespace wavemap { | ||
void add_render_bindings(nb::module_& m); | ||
} // namespace wavemap | ||
|
||
#endif // PYWAVEMAP_RENDER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.