diff --git a/localization/autoware_ndt_scan_matcher/CMakeLists.txt b/localization/autoware_ndt_scan_matcher/CMakeLists.txt
index dbbe71129f..cce3cfd0d9 100644
--- a/localization/autoware_ndt_scan_matcher/CMakeLists.txt
+++ b/localization/autoware_ndt_scan_matcher/CMakeLists.txt
@@ -32,7 +32,6 @@ include_directories(${PCL_INCLUDE_DIRS})
ament_auto_add_library(multigrid_ndt_omp SHARED
src/ndt_omp/multi_voxel_grid_covariance_omp.cpp
src/ndt_omp/multigrid_ndt_omp.cpp
- src/ndt_omp/estimate_covariance.cpp
)
target_link_libraries(multigrid_ndt_omp ${PCL_LIBRARIES})
@@ -42,8 +41,11 @@ else()
message(WARNING "OpenMP not found")
endif()
-ament_auto_add_library(${PROJECT_NAME} SHARED
+# NO_TARGET_LINK_LIBRARIES: the node does not use ndt_omp. Only the backend "ndt_omp" does.
+ament_auto_add_library(${PROJECT_NAME} SHARED NO_TARGET_LINK_LIBRARIES
src/map_update_module.cpp
+ src/ndt_backend_loader.cpp
+ src/ndt_omp/estimate_covariance.cpp
src/ndt_scan_matcher_core.cpp
src/ndt_scan_matcher_helper.cpp
src/particle.cpp
@@ -51,7 +53,7 @@ ament_auto_add_library(${PROJECT_NAME} SHARED
)
link_directories(${PCL_LIBRARY_DIRS})
-target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES} multigrid_ndt_omp)
+target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES})
autoware_agnocast_wrapper_register_node(${PROJECT_NAME}
PLUGIN "autoware::ndt_scan_matcher::NDTScanMatcher"
@@ -60,7 +62,52 @@ autoware_agnocast_wrapper_register_node(${PROJECT_NAME}
AGNOCAST_EXECUTOR AgnocastOnlyCallbackIsolatedExecutor
)
+# The NDT backend "ndt_omp". The node loads it with pluginlib, so no other library may link it. It
+# is a plain library: ament_auto exports every library that ament_auto_add_library() adds, and it
+# links them into the later libraries and tests of this package and into the packages that depend on
+# it.
+add_library(${PROJECT_NAME}_omp_backend SHARED
+ src/omp_ndt_backend.cpp
+)
+target_link_libraries(${PROJECT_NAME}_omp_backend multigrid_ndt_omp ${PCL_LIBRARIES})
+ament_target_dependencies(${PROJECT_NAME}_omp_backend autoware_ndt_scan_matcher_common pluginlib)
+install(TARGETS ${PROJECT_NAME}_omp_backend
+ LIBRARY DESTINATION lib
+)
+pluginlib_export_plugin_description_file(autoware_ndt_scan_matcher_common plugins.xml)
+
if(BUILD_TESTING)
+ # Three test backends in an install prefix inside the build directory, for the tests of the
+ # error messages. pluginlib finds them only in a test that has this prefix in AMENT_PREFIX_PATH.
+ set(test_backend_prefix ${CMAKE_CURRENT_BINARY_DIR}/test_ndt_backend_prefix)
+ set(test_backend_index ${test_backend_prefix}/share/ament_index/resource_index)
+ file(WRITE ${test_backend_index}/packages/test_ndt_backend "")
+ file(WRITE
+ ${test_backend_index}/autoware_ndt_scan_matcher_common__pluginlib__plugin/test_ndt_backend
+ "share/test_ndt_backend/test_ndt_backend_plugins.xml\n")
+ file(WRITE ${test_backend_prefix}/share/test_ndt_backend/package.xml
+ "test_ndt_backend\n")
+ # configure_file() copies the file again when it changes.
+ # cspell: ignore COPYONLY
+ configure_file(test/test_ndt_backend_plugins.xml
+ ${test_backend_prefix}/share/test_ndt_backend/test_ndt_backend_plugins.xml COPYONLY)
+ foreach(variant constructor_throws initialize_throws other_abi)
+ add_library(test_ndt_backend_${variant} MODULE test/test_ndt_backend.cpp)
+ ament_target_dependencies(test_ndt_backend_${variant}
+ autoware_ndt_scan_matcher_common pluginlib)
+ # $<1:...> keeps a multi-config generator from adding a directory for each configuration.
+ set_target_properties(test_ndt_backend_${variant} PROPERTIES
+ LIBRARY_OUTPUT_DIRECTORY $<1:${test_backend_prefix}/lib>)
+ endforeach()
+ target_compile_definitions(test_ndt_backend_constructor_throws PRIVATE
+ TEST_NDT_BACKEND_CONSTRUCTOR_THROWS)
+ target_compile_definitions(test_ndt_backend_initialize_throws PRIVATE
+ TEST_NDT_BACKEND_INITIALIZE_THROWS)
+ # The other heap allocator of Eigen. test_ndt_backend_loader skips its test in a build where
+ # this definition does not change the allocator.
+ target_compile_definitions(test_ndt_backend_other_abi PRIVATE
+ EIGEN_MALLOC_ALREADY_ALIGNED=0)
+
if(NOT (DEFINED ENV{ENABLE_AGNOCAST} AND "$ENV{ENABLE_AGNOCAST}" STREQUAL "1"))
add_launch_test(
test/test_ndt_scan_matcher_launch.py
@@ -100,7 +147,9 @@ if(BUILD_TESTING)
ament_add_ros_isolated_gtest(test_ndt_scan_matcher_characteristics
test/test_ndt_scan_matcher_characteristics.cpp
TIMEOUT "600"
+ APPEND_ENV AMENT_PREFIX_PATH=${test_backend_prefix}
)
+ add_dependencies(test_ndt_scan_matcher_characteristics test_ndt_backend_initialize_throws)
target_link_libraries(test_ndt_scan_matcher_characteristics ${PROJECT_NAME})
ament_target_dependencies(test_ndt_scan_matcher_characteristics
ament_index_cpp rcl_yaml_param_parser)
@@ -110,7 +159,7 @@ if(BUILD_TESTING)
ament_auto_add_gtest(test_estimate_covariance
test/test_estimate_covariance.cpp
)
- target_link_libraries(test_estimate_covariance multigrid_ndt_omp ${PCL_LIBRARIES})
+ target_link_libraries(test_estimate_covariance ${PROJECT_NAME} ${PCL_LIBRARIES})
ament_auto_add_gtest(test_ndt_scan_matcher_helper
test/test_ndt_scan_matcher_helper.cpp
@@ -126,7 +175,23 @@ if(BUILD_TESTING)
ament_auto_add_gtest(test_map_update_module
test/test_map_update_module.cpp
)
- target_link_libraries(test_map_update_module ${PROJECT_NAME} multigrid_ndt_omp ${PCL_LIBRARIES})
+ target_link_libraries(test_map_update_module ${PROJECT_NAME} ${PCL_LIBRARIES})
+
+ # The OMP backend against ndt_omp, and its copies.
+ ament_auto_add_gtest(test_omp_ndt_backend
+ test/test_omp_ndt_backend.cpp
+ )
+ target_include_directories(test_omp_ndt_backend PRIVATE src)
+ target_link_libraries(test_omp_ndt_backend ${PROJECT_NAME}_omp_backend ${PCL_LIBRARIES})
+
+ # pluginlib loads the backend library here. The test does not link it.
+ ament_auto_add_gtest(test_ndt_backend_loader
+ test/test_ndt_backend_loader.cpp
+ APPEND_ENV AMENT_PREFIX_PATH=${test_backend_prefix}
+ )
+ target_link_libraries(test_ndt_backend_loader ${PROJECT_NAME} ${CMAKE_DL_LIBS})
+ add_dependencies(test_ndt_backend_loader
+ test_ndt_backend_constructor_throws test_ndt_backend_other_abi)
endif()
autoware_ament_auto_package(
diff --git a/localization/autoware_ndt_scan_matcher/README.md b/localization/autoware_ndt_scan_matcher/README.md
index 4cde8f97d6..e390982e1b 100644
--- a/localization/autoware_ndt_scan_matcher/README.md
+++ b/localization/autoware_ndt_scan_matcher/README.md
@@ -84,6 +84,14 @@ One optional function is regularization. Please see the regularization chapter i
{{ json_to_markdown("localization/autoware_ndt_scan_matcher/schema/sub/covariance.json") }}
+## NDT backends
+
+The node does the NDT computations through a backend, which it loads with [pluginlib](https://github.com/ros/pluginlib). The parameter `ndt.backend` sets the name of the backend. This package has one backend, `ndt_omp`, which is ndt_omp on the CPU. The parameter file of this package sets `ndt_omp`.
+
+A backend is a class that implements `autoware::ndt_scan_matcher::NdtBackend` from [autoware_ndt_scan_matcher_common](../autoware_ndt_scan_matcher_common/README.md). That README explains how another package adds a backend.
+
+If the node cannot load the backend, the node throws an exception at startup. For an unknown name, the message lists the available backends.
+
## Regularization
### Abstract
diff --git a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/hyper_parameters.hpp b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/hyper_parameters.hpp
index e0c18ab01f..4460127ca0 100644
--- a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/hyper_parameters.hpp
+++ b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/hyper_parameters.hpp
@@ -15,13 +15,14 @@
#ifndef AUTOWARE__NDT_SCAN_MATCHER__HYPER_PARAMETERS_HPP_
#define AUTOWARE__NDT_SCAN_MATCHER__HYPER_PARAMETERS_HPP_
-#include "ndt_omp/multigrid_ndt_omp.h"
+#include "autoware/ndt_scan_matcher_common/ndt_types.hpp"
#include
#include
#include
#include
+#include
#include
#include
@@ -55,7 +56,8 @@ struct HyperParameters
double required_distance{};
} sensor_points{};
- pclomp::NdtParams ndt{};
+ NdtParams ndt{};
+ std::string ndt_backend{};
bool ndt_regularization_enable{};
struct InitialPoseEstimation
@@ -124,6 +126,14 @@ struct HyperParameters
ndt.max_iterations = static_cast(node->declare_parameter("ndt.max_iterations"));
ndt.num_threads = static_cast(node->declare_parameter("ndt.num_threads"));
ndt.num_threads = std::max(ndt.num_threads, 1);
+ try {
+ ndt_backend = node->declare_parameter("ndt.backend");
+ } catch (const rclcpp::ParameterTypeException &) {
+ // The parameter is new, so a parameter file from before it does not have it.
+ throw std::runtime_error(
+ "The parameter 'ndt.backend' is not set. Add the line `backend: \"ndt_omp\"` under `ndt:` "
+ "in the parameter file of autoware_ndt_scan_matcher.");
+ }
ndt_regularization_enable = node->declare_parameter("ndt.regularization.enable");
ndt.regularization_scale_factor =
static_cast(node->declare_parameter("ndt.regularization.scale_factor"));
diff --git a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/map_update_module.hpp b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/map_update_module.hpp
index b2387d3666..c70fc340ae 100644
--- a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/map_update_module.hpp
+++ b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/map_update_module.hpp
@@ -15,10 +15,10 @@
#ifndef AUTOWARE__NDT_SCAN_MATCHER__MAP_UPDATE_MODULE_HPP_
#define AUTOWARE__NDT_SCAN_MATCHER__MAP_UPDATE_MODULE_HPP_
+#include "autoware/ndt_scan_matcher_common/ndt_backend.hpp"
#include "diagnostics_report.hpp"
#include "guarded.hpp"
#include "hyper_parameters.hpp"
-#include "ndt_omp/multigrid_ndt_omp.h"
#include
#include
@@ -46,8 +46,8 @@ class MapUpdateModule
public:
using PointSource = pcl::PointXYZ;
using PointTarget = pcl::PointXYZ;
- using NdtType = pclomp::MultiGridNormalDistributionsTransform;
- using NdtPtrType = std::shared_ptr;
+ using NdtType = NdtBackend;
+ using NdtPtrType = NdtBackend::SharedPtr;
using GetDifferentialPointCloudMap = autoware_map_msgs::srv::GetDifferentialPointCloudMap;
diff --git a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_backend_loader.hpp b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_backend_loader.hpp
new file mode 100644
index 0000000000..58b0b83521
--- /dev/null
+++ b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_backend_loader.hpp
@@ -0,0 +1,34 @@
+// Copyright 2026 Autoware Foundation
+//
+// 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 AUTOWARE__NDT_SCAN_MATCHER__NDT_BACKEND_LOADER_HPP_
+#define AUTOWARE__NDT_SCAN_MATCHER__NDT_BACKEND_LOADER_HPP_
+
+#include "autoware/ndt_scan_matcher_common/ndt_backend.hpp"
+
+#include
+
+namespace autoware::ndt_scan_matcher
+{
+
+/// Makes the NDT backend with this pluginlib lookup name. Throws std::runtime_error if the backend
+/// cannot be made. For an unknown name, the message lists the declared backends.
+///
+/// A backend library stays loaded until the process ends, so instances that a backend makes
+/// without pluginlib (for example, in clone()) stay valid.
+NdtBackend::SharedPtr load_ndt_backend(const std::string & backend_name);
+
+} // namespace autoware::ndt_scan_matcher
+
+#endif // AUTOWARE__NDT_SCAN_MATCHER__NDT_BACKEND_LOADER_HPP_
diff --git a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_omp/estimate_covariance.hpp b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_omp/estimate_covariance.hpp
index 1d962e34ac..03eec8357a 100644
--- a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_omp/estimate_covariance.hpp
+++ b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_omp/estimate_covariance.hpp
@@ -15,7 +15,7 @@
#ifndef AUTOWARE__NDT_SCAN_MATCHER__NDT_OMP__ESTIMATE_COVARIANCE_HPP_
#define AUTOWARE__NDT_SCAN_MATCHER__NDT_OMP__ESTIMATE_COVARIANCE_HPP_
-#include "multigrid_ndt_omp.h"
+#include "autoware/ndt_scan_matcher_common/ndt_backend.hpp"
#include
@@ -31,21 +31,21 @@ struct ResultOfMultiNdtCovarianceEstimation
Eigen::Vector2d mean;
Eigen::Matrix2d covariance;
std::vector ndt_initial_poses;
- std::vector ndt_results;
+ std::vector ndt_results;
};
/** \brief Estimate functions */
Eigen::Matrix2d estimate_xy_covariance_by_laplace_approximation(
const Eigen::Matrix & hessian);
ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt(
- const NdtResult & ndt_result,
- pclomp::MultiGridNormalDistributionsTransform & ndt_ref,
+ const autoware::ndt_scan_matcher::NdtResult & ndt_result,
+ autoware::ndt_scan_matcher::NdtBackend & ndt_ref,
const std::vector & poses_to_search,
const pcl::shared_ptr> & source);
ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt_score(
- const NdtResult & ndt_result,
- pclomp::MultiGridNormalDistributionsTransform & ndt_ref,
+ const autoware::ndt_scan_matcher::NdtResult & ndt_result,
+ autoware::ndt_scan_matcher::NdtBackend & ndt_ref,
const std::vector & poses_to_search,
const pcl::shared_ptr> & source, const double temperature);
@@ -55,7 +55,7 @@ ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt_score(
* (3) Propose search points by adding offset_x and offset_y to the center_pose
*/
std::vector propose_poses_to_search(
- const NdtResult & ndt_result, const std::vector & offset_x,
+ const autoware::ndt_scan_matcher::NdtResult & ndt_result, const std::vector & offset_x,
const std::vector & offset_y);
/** \brief Calculate weights by exponential */
diff --git a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_scan_matcher_core.hpp b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_scan_matcher_core.hpp
index 96987fbbaa..266d849a3a 100644
--- a/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_scan_matcher_core.hpp
+++ b/localization/autoware_ndt_scan_matcher/include/autoware/ndt_scan_matcher/ndt_scan_matcher_core.hpp
@@ -17,10 +17,10 @@
#define FMT_HEADER_ONLY
+#include "autoware/ndt_scan_matcher_common/ndt_backend.hpp"
#include "guarded.hpp"
#include "hyper_parameters.hpp"
#include "map_update_module.hpp"
-#include "ndt_omp/multigrid_ndt_omp.h"
#include "scan_match_diagnostics.hpp"
#include
@@ -73,8 +73,6 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node
{
using PointSource = pcl::PointXYZ;
using PointTarget = pcl::PointXYZ;
- using NormalDistributionsTransform =
- pclomp::MultiGridNormalDistributionsTransform;
public:
explicit NDTScanMatcher(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
@@ -121,7 +119,7 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node
std::tuple align_pose(
const geometry_msgs::msg::PoseWithCovarianceStamped & initial_pose_with_cov,
- NormalDistributionsTransform & ndt_ref);
+ NdtBackend & ndt_ref);
void transform_sensor_measurement(
const std::string & source_frame, const std::string & target_frame,
@@ -138,7 +136,7 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node
const pcl::shared_ptr> & sensor_points_in_map_ptr);
void publish_marker(
const rclcpp::Time & sensor_ros_time, const std::vector & pose_array,
- NormalDistributionsTransform & ndt_ref);
+ NdtBackend & ndt_ref);
void publish_initial_to_result(
const rclcpp::Time & sensor_ros_time, const geometry_msgs::msg::Pose & result_pose_msg,
const geometry_msgs::msg::PoseWithCovarianceStamped & initial_pose_cov_msg,
@@ -150,15 +148,16 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node
static int count_oscillation(const std::vector & result_pose_msg_array);
Eigen::Matrix2d estimate_covariance(
- const pclomp::NdtResult & ndt_result, const Eigen::Matrix4f & initial_pose_matrix,
- const rclcpp::Time & sensor_ros_time, NormalDistributionsTransform & ndt_ref);
+ const NdtResult & ndt_result, const Eigen::Matrix4f & initial_pose_matrix,
+ const rclcpp::Time & sensor_ros_time, NdtBackend & ndt_ref);
pcl::PointCloud::Ptr visualize_point_score(
const pcl::shared_ptr> & sensor_points_in_map_ptr,
- const float & lower_nvs, const float & upper_nvs, NormalDistributionsTransform & ndt_ref);
+ const float & lower_nvs, const float & upper_nvs, NdtBackend & ndt_ref);
- void add_regularization_pose(
- const rclcpp::Time & sensor_ros_time, NormalDistributionsTransform & ndt_ref);
+ void add_regularization_pose(const rclcpp::Time & sensor_ros_time, NdtBackend & ndt_ref);
+
+ NdtBackend::SharedPtr create_ndt_backend(const std::string & backend_name);
// Performs the pcd_loader service call for MapUpdateModule, returning nullptr on failure.
MapUpdateModule::GetDifferentialPointCloudMap::Response::ConstSharedPtr
@@ -219,8 +218,7 @@ class NDTScanMatcher : public autoware::agnocast_wrapper::Node
rclcpp::CallbackGroup::SharedPtr timer_callback_group_;
- Guarded> ndt_ptr_{
- std::make_shared()};
+ Guarded ndt_ptr_;
pcl::shared_ptr> sensor_points_in_baselink_frame_;
diff --git a/localization/autoware_ndt_scan_matcher/package.xml b/localization/autoware_ndt_scan_matcher/package.xml
index 2867e41955..154af0360d 100644
--- a/localization/autoware_ndt_scan_matcher/package.xml
+++ b/localization/autoware_ndt_scan_matcher/package.xml
@@ -19,6 +19,7 @@
autoware_internal_localization_msgs
autoware_localization_util
autoware_map_msgs
+ autoware_ndt_scan_matcher_common
autoware_qos_utils
autoware_utils_diagnostics
autoware_utils_geometry
@@ -34,6 +35,7 @@
libpcl-all
nav_msgs
pcl_conversions
+ pluginlib
rclcpp
rclcpp_components
sensor_msgs
@@ -51,6 +53,7 @@
ament_cmake_ros
ament_index_cpp
ament_lint_auto
+ console_bridge_vendor
launch_testing_ament_cmake
rcl_yaml_param_parser
ros_testing
diff --git a/localization/autoware_ndt_scan_matcher/plugins.xml b/localization/autoware_ndt_scan_matcher/plugins.xml
new file mode 100644
index 0000000000..d119da2426
--- /dev/null
+++ b/localization/autoware_ndt_scan_matcher/plugins.xml
@@ -0,0 +1,5 @@
+
+
+ NDT on the CPU with OpenMP (ndt_omp).
+
+
diff --git a/localization/autoware_ndt_scan_matcher/src/map_update_module.cpp b/localization/autoware_ndt_scan_matcher/src/map_update_module.cpp
index 9035740e04..e3297ff247 100644
--- a/localization/autoware_ndt_scan_matcher/src/map_update_module.cpp
+++ b/localization/autoware_ndt_scan_matcher/src/map_update_module.cpp
@@ -38,11 +38,9 @@ MapUpdateModule::MapUpdateModule(
// and ndt_ptr_ is only locked when swapping its pointer with
// secondary_ndt_ptr_.
builder_state.need_rebuild = true;
- builder_state.secondary_ndt_ptr.reset(new NdtType);
-
return ndt_ptr_.with([&](const auto & ptr) {
if (ptr) {
- *builder_state.secondary_ndt_ptr = *ptr;
+ builder_state.secondary_ndt_ptr = ptr->clone();
return true;
} else {
return false;
@@ -135,14 +133,11 @@ bool MapUpdateModule::update_map_internal(
if (builder_state.need_rebuild) {
bool updated = false;
ndt_ptr_.with([&](auto & ndt_ptr) {
- auto param = ndt_ptr->get_params();
-
- ndt_ptr.reset(new NdtType);
+ // A new backend of the same type and parameters, without a map.
+ ndt_ptr = ndt_ptr->clone_empty();
// The map is rebuilt from scratch, so reset the accumulated debug map as well.
loaded_pcd_map_.clear();
- ndt_ptr->set_params(param);
-
updated = update_ndt(position, *ndt_ptr, diagnostics);
});
@@ -158,10 +153,14 @@ bool MapUpdateModule::update_map_internal(
return false;
}
+ // Destroy the old secondary before the copy, outside the lock of ndt_ptr_, so that at most two
+ // maps exist at the same time. need_rebuild stays true until the copy exists. No caller catches
+ // an exception of the copy today. If a caller does that later, the next update is a rebuild,
+ // and the null secondary is never used.
+ builder_state.secondary_ndt_ptr.reset();
+ builder_state.secondary_ndt_ptr =
+ ndt_ptr_.with([](const auto & ndt_ptr) { return ndt_ptr->clone(); });
builder_state.need_rebuild = false;
-
- builder_state.secondary_ndt_ptr.reset(new NdtType);
- ndt_ptr_.with([&](const auto & ndt_ptr) { *builder_state.secondary_ndt_ptr = *ndt_ptr; });
} else {
// Load map to the secondary_ndt_ptr, which does not require a mutex lock
// Since the update of the secondary ndt ptr and the NDT align (done on
@@ -183,7 +182,7 @@ bool MapUpdateModule::update_map_internal(
// while only the fast pointer swap is performed inside the lock scope.
// 1. Clone the contents of secondary_ndt_ptr to create new_ndt_ptr.
- auto new_ndt_ptr = std::make_shared(*builder_state.secondary_ndt_ptr);
+ auto new_ndt_ptr = builder_state.secondary_ndt_ptr->clone();
// 2. Swap the pointers inside the ndt_ptr_'s lock.
// - During the swap, the reference count does not decrease to zero,
@@ -279,7 +278,7 @@ bool MapUpdateModule::update_ndt(
}
}
- ndt.create_voxel_kdtree();
+ ndt.commit_target();
const auto exe_end_time = std::chrono::system_clock::now();
const auto duration_micro_sec =
diff --git a/localization/autoware_ndt_scan_matcher/src/ndt_backend_loader.cpp b/localization/autoware_ndt_scan_matcher/src/ndt_backend_loader.cpp
new file mode 100644
index 0000000000..db82a371cf
--- /dev/null
+++ b/localization/autoware_ndt_scan_matcher/src/ndt_backend_loader.cpp
@@ -0,0 +1,71 @@
+// Copyright 2026 Autoware Foundation
+//
+// 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.
+
+#include "autoware/ndt_scan_matcher/ndt_backend_loader.hpp"
+
+#include
+
+#include
+#include
+#include
+
+namespace autoware::ndt_scan_matcher
+{
+namespace
+{
+
+// pluginlib unloads the libraries of a class loader when the loader is destroyed. This loader is
+// never destroyed, so a backend library stays loaded until the process ends.
+pluginlib::ClassLoader & class_loader()
+{
+ static auto & loader = *new pluginlib::ClassLoader( // NOLINT
+ "autoware_ndt_scan_matcher_common", "autoware::ndt_scan_matcher::NdtBackend");
+ return loader;
+}
+
+// pluginlib::ClassLoader is not thread safe, and several nodes can load backends at the same time
+// in one process.
+std::mutex & class_loader_mutex()
+{
+ static std::mutex mutex;
+ return mutex;
+}
+
+} // namespace
+
+NdtBackend::SharedPtr load_ndt_backend(const std::string & backend_name)
+{
+ const std::lock_guard lock(class_loader_mutex());
+ try {
+ return class_loader().createSharedInstance(backend_name);
+ } catch (const pluginlib::CreateClassException & e) {
+ // The library is loaded, but it has no factory for this class and this version of the
+ // interface. See the inline namespace in ndt_types.hpp.
+ throw std::runtime_error(
+ "Failed to create the NDT backend '" + backend_name + "'. " + std::string(e.what()) +
+ ". The backend was possibly built against another version of autoware_ndt_scan_matcher_common"
+ ", or with SIMD options that change the alignment of Eigen (for example AVX), or the class "
+ "type in its plugin description differs from the class that it exports.");
+ } catch (const pluginlib::PluginlibException & e) {
+ // The message of pluginlib lists the declared backends.
+ throw std::runtime_error(
+ "Failed to load the NDT backend '" + backend_name + "'. " + std::string(e.what()));
+ } catch (const std::exception & e) {
+ // From the constructor of the backend.
+ throw std::runtime_error(
+ "Failed to create the NDT backend '" + backend_name + "': " + std::string(e.what()));
+ }
+}
+
+} // namespace autoware::ndt_scan_matcher
diff --git a/localization/autoware_ndt_scan_matcher/src/ndt_omp/estimate_covariance.cpp b/localization/autoware_ndt_scan_matcher/src/ndt_omp/estimate_covariance.cpp
index a324b120d2..40d905ac62 100644
--- a/localization/autoware_ndt_scan_matcher/src/ndt_omp/estimate_covariance.cpp
+++ b/localization/autoware_ndt_scan_matcher/src/ndt_omp/estimate_covariance.cpp
@@ -14,6 +14,8 @@
#include
+#include
+
#include
#include
#include
@@ -33,8 +35,8 @@ Eigen::Matrix2d estimate_xy_covariance_by_laplace_approximation(
}
ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt(
- const NdtResult & ndt_result,
- pclomp::MultiGridNormalDistributionsTransform & ndt_ref,
+ const autoware::ndt_scan_matcher::NdtResult & ndt_result,
+ autoware::ndt_scan_matcher::NdtBackend & ndt_ref,
const std::vector & poses_to_search,
const pcl::shared_ptr> & source)
{
@@ -43,11 +45,9 @@ ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt(
std::vector ndt_pose_2d_vec{ndt_pose_2d};
// multiple searches
- std::vector ndt_results;
+ std::vector ndt_results;
for (const Eigen::Matrix4f & curr_pose : poses_to_search) {
- auto sub_output_cloud = std::make_shared>();
- ndt_ref.align(*sub_output_cloud, curr_pose, source);
- const NdtResult sub_ndt_result = ndt_ref.get_result();
+ const autoware::ndt_scan_matcher::NdtResult sub_ndt_result = ndt_ref.align(curr_pose, source);
ndt_results.push_back(sub_ndt_result);
const Eigen::Matrix4f sub_ndt_pose = sub_ndt_result.pose;
@@ -69,8 +69,8 @@ ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt(
}
ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt_score(
- const NdtResult & ndt_result,
- pclomp::MultiGridNormalDistributionsTransform & ndt_ref,
+ const autoware::ndt_scan_matcher::NdtResult & ndt_result,
+ autoware::ndt_scan_matcher::NdtBackend & ndt_ref,
const std::vector & poses_to_search,
const pcl::shared_ptr> & source, const double temperature)
{
@@ -82,7 +82,7 @@ ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt_score(
pcl::PointCloud trans_cloud;
// multiple searches
- std::vector ndt_results;
+ std::vector ndt_results;
for (const Eigen::Matrix4f & curr_pose : poses_to_search) {
const Eigen::Vector2d sub_ndt_pose_2d = curr_pose.topRightCorner<2, 1>().cast();
ndt_pose_2d_vec.emplace_back(sub_ndt_pose_2d);
@@ -91,7 +91,7 @@ ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt_score(
const double nvtl = ndt_ref.calculate_nearest_voxel_transformation_likelihood(trans_cloud);
score_vec.emplace_back(nvtl);
- NdtResult sub_ndt_result{};
+ autoware::ndt_scan_matcher::NdtResult sub_ndt_result{};
sub_ndt_result.pose = curr_pose;
sub_ndt_result.iteration_num = 0;
sub_ndt_result.nearest_voxel_transformation_likelihood = static_cast(nvtl);
@@ -107,7 +107,7 @@ ResultOfMultiNdtCovarianceEstimation estimate_xy_covariance_by_multi_ndt_score(
}
std::vector propose_poses_to_search(
- const NdtResult & ndt_result, const std::vector & offset_x,
+ const autoware::ndt_scan_matcher::NdtResult & ndt_result, const std::vector & offset_x,
const std::vector & offset_y)
{
assert(offset_x.size() == offset_y.size());
diff --git a/localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp b/localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp
index 3e71dba6f8..62f3dfc0ca 100644
--- a/localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp
+++ b/localization/autoware_ndt_scan_matcher/src/ndt_scan_matcher_core.cpp
@@ -17,6 +17,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -44,6 +45,7 @@
#include
#include
#include