-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New inflation layer with optional OpenMP acceleration #5933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SteveMacenski
merged 31 commits into
ros-navigation:main
from
botsandus:openmp-inflation-layer
Mar 6, 2026
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
66f46c6
OpenMP inflation layer
tonynajjar 1800c72
restart tests
tonynajjar 879c9c9
disable openmp
tonynajjar d2d1def
enable flag
tonynajjar c858390
limit thread number
tonynajjar 232d01c
enable openmp for testing
tonynajjar d4cea55
check docker cores
tonynajjar bf4edb2
set OMP_NUM_THREADS to 4
tonynajjar 90e2530
dynamic num_thread
tonynajjar fdc10cc
remove openmp from packages.xml
tonynajjar 83dcc0f
turn on by default
tonynajjar 517bd1f
hardcode COST_LUT_PRECISION
tonynajjar 75396c3
test out big test
tonynajjar 66bea4d
add back legacy layer
tonynajjar ff3eaaa
set to 100
tonynajjar 21fa149
fix legacy inflation layer
tonynajjar ef9bdb3
Revert "fix legacy inflation layer"
tonynajjar 07c98b6
off by default
tonynajjar 81ec3ef
base class approach
tonynajjar 5cf5c55
simplified interface
tonynajjar 3149503
add test coverage
tonynajjar de85132
Merge remote-tracking branch 'upstream/main' into openmp-inflation-layer
tonynajjar 06d51fb
remove cpp file
tonynajjar 5409e22
Merge remote-tracking branch 'nav/main' into openmp-inflation-layer
tonynajjar 7fcd0e8
Fix broken build
tonynajjar 1003230
Add tests for legacy inflation layer
tonynajjar 943c7f4
Enable OpenMP support and add legacy inflation layer test configuration
tonynajjar 8cecb7f
lint
tonynajjar bff1368
lint
tonynajjar 3ae197c
Merge branch 'main' into openmp-inflation-layer
tonynajjar 12571d5
remove -DENABLE_OPENMP=ON
tonynajjar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
179 changes: 179 additions & 0 deletions
179
nav2_costmap_2d/include/nav2_costmap_2d/distance_transform.hpp
This file contains hidden or 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,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_ | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.