Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
66f46c6
OpenMP inflation layer
tonynajjar Feb 5, 2026
1800c72
restart tests
tonynajjar Feb 5, 2026
879c9c9
disable openmp
tonynajjar Feb 5, 2026
d2d1def
enable flag
tonynajjar Feb 5, 2026
c858390
limit thread number
tonynajjar Feb 5, 2026
232d01c
enable openmp for testing
tonynajjar Feb 5, 2026
d4cea55
check docker cores
tonynajjar Feb 5, 2026
bf4edb2
set OMP_NUM_THREADS to 4
tonynajjar Feb 5, 2026
90e2530
dynamic num_thread
tonynajjar Feb 6, 2026
fdc10cc
remove openmp from packages.xml
tonynajjar Feb 6, 2026
83dcc0f
turn on by default
tonynajjar Feb 6, 2026
517bd1f
hardcode COST_LUT_PRECISION
tonynajjar Feb 6, 2026
75396c3
test out big test
tonynajjar Feb 6, 2026
66bea4d
add back legacy layer
tonynajjar Feb 6, 2026
ff3eaaa
set to 100
tonynajjar Feb 6, 2026
21fa149
fix legacy inflation layer
tonynajjar Feb 6, 2026
ef9bdb3
Revert "fix legacy inflation layer"
tonynajjar Feb 9, 2026
07c98b6
off by default
tonynajjar Feb 9, 2026
81ec3ef
base class approach
tonynajjar Feb 9, 2026
5cf5c55
simplified interface
tonynajjar Feb 11, 2026
3149503
add test coverage
tonynajjar Feb 11, 2026
de85132
Merge remote-tracking branch 'upstream/main' into openmp-inflation-layer
tonynajjar Feb 11, 2026
06d51fb
remove cpp file
tonynajjar Feb 12, 2026
5409e22
Merge remote-tracking branch 'nav/main' into openmp-inflation-layer
tonynajjar Mar 4, 2026
7fcd0e8
Fix broken build
tonynajjar Mar 4, 2026
1003230
Add tests for legacy inflation layer
tonynajjar Mar 4, 2026
943c7f4
Enable OpenMP support and add legacy inflation layer test configuration
tonynajjar Mar 4, 2026
8cecb7f
lint
tonynajjar Mar 4, 2026
bff1368
lint
tonynajjar Mar 4, 2026
3ae197c
Merge branch 'main' into openmp-inflation-layer
tonynajjar Mar 5, 2026
12571d5
remove -DENABLE_OPENMP=ON
tonynajjar Mar 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ _environments:
RCUTILS_LOGGING_USE_STDOUT: "0"
DEBIAN_FRONTEND: "noninteractive"
PYTHONUNBUFFERED: "1"
OMP_NUM_THREADS: "4"

executors:
release_exec:
Expand Down
23 changes: 23 additions & 0 deletions nav2_costmap_2d/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ find_package(nav2_ros_common REQUIRED)
find_package(tf2_sensor_msgs REQUIRED)
find_package(visualization_msgs REQUIRED)
find_package(point_cloud_transport REQUIRED)
find_package(Eigen3 REQUIRED)

option(ENABLE_OPENMP "Enable OpenMP parallelization" OFF)

if(ENABLE_OPENMP)
find_package(OpenMP REQUIRED)
endif()

nav2_package()

Expand Down Expand Up @@ -66,6 +73,7 @@ target_link_libraries(nav2_costmap_2d_core PUBLIC

add_library(layers SHARED
plugins/inflation_layer.cpp
plugins/legacy_inflation_layer.cpp
plugins/static_layer.cpp
plugins/obstacle_layer.cpp
src/observation_buffer.cpp
Expand All @@ -90,7 +98,19 @@ target_link_libraries(layers PUBLIC
tf2::tf2
nav2_ros_common::nav2_ros_common
point_cloud_transport::point_cloud_transport
Eigen3::Eigen
)

# Link OpenMP if enabled
if(ENABLE_OPENMP)
message(STATUS "OpenMP ENABLED for nav2_costmap_2d layers (${OpenMP_CXX_VERSION})")
target_link_libraries(layers PRIVATE OpenMP::OpenMP_CXX)
target_compile_options(layers PRIVATE -fopenmp)
else()
message(STATUS "OpenMP DISABLED for nav2_costmap_2d layers")
target_compile_options(layers PRIVATE -Wno-unknown-pragmas)
endif()

target_link_libraries(layers PRIVATE
pluginlib::pluginlib
)
Expand Down Expand Up @@ -226,5 +246,8 @@ ament_export_dependencies(
nav2_ros_common
point_cloud_transport
)

ament_export_dependencies(Eigen3)

pluginlib_export_plugin_description_file(nav2_costmap_2d costmap_plugins.xml)
ament_package()
5 changes: 4 additions & 1 deletion nav2_costmap_2d/costmap_plugins.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<class_libraries>
<library path="layers">
<class type="nav2_costmap_2d::InflationLayer" base_class_type="nav2_costmap_2d::Layer">
<description>Inflates obstacles to speed collision checking and to make robot prefer to stay away from obstacles.</description>
<description>OpenMP-accelerated inflation layer using Felzenszwalb-Huttenlocher distance transform for improved performance.</description>
</class>
<class type="nav2_costmap_2d::LegacyInflationLayer" base_class_type="nav2_costmap_2d::Layer">
<description>Legacy inflation layer using traditional CellData-based algorithm (original Navigation2 implementation).</description>
</class>
<class type="nav2_costmap_2d::ObstacleLayer" base_class_type="nav2_costmap_2d::Layer">
<description>Listens to laser scan and point cloud messages and marks and clears grid cells.</description>
Expand Down
179 changes: 179 additions & 0 deletions nav2_costmap_2d/include/nav2_costmap_2d/distance_transform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright (c) 2026, Dexory (Tony Najjar)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef NAV2_COSTMAP_2D__DISTANCE_TRANSFORM_HPP_
#define NAV2_COSTMAP_2D__DISTANCE_TRANSFORM_HPP_

#include <limits>
#include <vector>
#ifdef _OPENMP
#include <omp.h>
#endif
#include <Eigen/Core>


namespace nav2_costmap_2d
{

/// Row-major float matrix type for efficient row-wise access
using MatrixXfRM = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;

/**
* @class DistanceTransform
* @brief Efficient Euclidean distance transform using the Felzenszwalb-Huttenlocher algorithm.
*
* This class provides a standalone implementation of the linear-time (O(n)) distance transform
* algorithm based on the lower envelope of parabolas method. It can be used for various
* applications including costmap inflation, obstacle detection, and path planning.
*
* The algorithm computes exact squared Euclidean distances in two separable 1D passes
* (rows and columns), making it highly efficient for large images/grids.
*
* Reference: Distance Transforms of Sampled Functions
* P. Felzenszwalb and D. Huttenlocher
* Theory of Computing, Vol. 8, No. 19, September 2012
*/
class DistanceTransform
{
public:
/// Infinity constant for distance transform
static constexpr float DT_INF = std::numeric_limits<float>::max();

/**
* @brief Perform 1D distance transform using lower envelope of parabolas.
*
* This is the core Felzenszwalb-Huttenlocher algorithm for computing squared
* Euclidean distances in 1D. It uses a lower envelope of parabolas to achieve
* linear time complexity.
*
* @param f Input array of squared distances (typically 0 for obstacles, INF for free space)
* @param d Output array for transformed squared distances (same size as f)
* @param n Length of the arrays
* @param v Buffer for parabola indices (size n)
* @param z Buffer for parabola boundaries (size n+1)
*/
static void distanceTransform1D(
const float * f, float * d, int n,
int * v, float * z)
{
if (!f || !d || !v || !z || n <= 0) {
return;
}

int k = 0;
v[0] = 0;
z[0] = -DT_INF;
z[1] = DT_INF;

for (int q = 1; q < n; q++) {
// Use integer arithmetic for squared values to avoid precision loss
// Only convert to float for the division operation
float s = (f[q] - f[v[k]] + static_cast<float>(q * q - v[k] * v[k])) /
(2.0f * static_cast<float>(q - v[k]));
while (s <= z[k]) {
k--;
s = (f[q] - f[v[k]] + static_cast<float>(q * q - v[k] * v[k])) /
(2.0f * static_cast<float>(q - v[k]));
}
k++;
v[k] = q;
z[k] = s;
z[k + 1] = DT_INF;
}

k = 0;
for (int q = 0; q < n; q++) {
while (z[k + 1] < static_cast<float>(q)) {
k++;
}
const int diff = q - v[k];
d[q] = static_cast<float>(diff * diff) + f[v[k]];
}
}

/**
* @brief Perform 2D Euclidean distance transform using separable passes.
*
* This method applies the 1D distance transform separately along columns and rows,
* exploiting the separability property of the Euclidean metric. The result is an
* exact Euclidean distance map computed in O(width × height) time.
*
* The algorithm is parallelized using OpenMP when available for improved performance
* on multi-core systems.
*
* @param img Input/output matrix (modified in place). Input values should be 0 for
* obstacles and DT_INF for free space. Output will contain Euclidean distances.
* @param height Number of rows in the matrix
* @param width Number of columns in the matrix
*/
static void distanceTransform2D(MatrixXfRM & img, int height, int width)
{
// Column pass (parallelizable)
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic, 16)
#endif
for (int x = 0; x < width; x++) {
// Thread-local buffers
std::vector<float> f(height);
std::vector<float> d(height);
std::vector<int> v(height);
std::vector<float> z(height + 1);

// Extract column
for (int y = 0; y < height; y++) {
f[y] = img(y, x);
}

// 1D transform
distanceTransform1D(f.data(), d.data(), height, v.data(), z.data());

// Write back
for (int y = 0; y < height; y++) {
img(y, x) = d[y];
}
}

// Row pass (parallelizable)
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic, 16)
#endif
for (int y = 0; y < height; y++) {
// Thread-local buffers
std::vector<float> f(width);
std::vector<float> d(width);
std::vector<int> v(width);
std::vector<float> z(width + 1);

// Extract row (already contiguous in row-major)
for (int x = 0; x < width; x++) {
f[x] = img(y, x);
}

// 1D transform
distanceTransform1D(f.data(), d.data(), width, v.data(), z.data());

// Write back
for (int x = 0; x < width; x++) {
img(y, x) = d[x];
}
}

// Square root to get Euclidean distance (not squared distance)
img = img.cwiseSqrt();
}
};

} // namespace nav2_costmap_2d

#endif // NAV2_COSTMAP_2D__DISTANCE_TRANSFORM_HPP_
Loading
Loading