diff --git a/CMakeLists.txt b/CMakeLists.txt index 39461f0..9e7d51b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 3.14) -project(cardinal_perception VERSION 0.6.0) +project(cardinal_perception VERSION 0.7.0) # --- Dependencies ------------------------------------------------------------- find_package(rosidl_default_generators REQUIRED) find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) +find_package(std_srvs REQUIRED) find_package(sensor_msgs REQUIRED) find_package(geometry_msgs REQUIRED) find_package(nav_msgs REQUIRED) @@ -36,7 +37,7 @@ endif() # Default to C++17 if(NOT CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 17) + set(CMAKE_CXX_STANDARD 20) endif() if(NOT MSVC) @@ -46,7 +47,7 @@ if(NOT MSVC) endif() if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") - add_compile_options(-Wall -Wextra -Wpedantic) + add_compile_options(-Wall -Wextra -Wpedantic -Wno-psabi) endif() # --- Misc. Options ------------------------------------------------------------ @@ -123,7 +124,12 @@ ament_target_dependencies(core_modules add_executable(perception_node "src/perception_node.cpp" "src/core/perception_core.cpp" - "src/core/perception_threads.cpp" ) + "src/core/threads/imu_worker.cpp" + "src/core/threads/localization_worker.cpp" + "src/core/threads/mapping_worker.cpp" + "src/core/threads/mining_eval_worker.cpp" + "src/core/threads/path_planning_worker.cpp" + "src/core/threads/traversibility_worker.cpp" ) target_include_directories(perception_node PUBLIC ${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}/generated ) @@ -149,6 +155,7 @@ ament_target_dependencies(perception_node "rclcpp" "pcl_ros" "pcl_conversions" + "std_srvs" "sensor_msgs" "geometry_msgs" "nav_msgs" @@ -202,6 +209,7 @@ ament_target_dependencies(pplan_client_node ament_export_dependencies( rclcpp std_msgs + std_srvs sensor_msgs geometry_msgs nav_msgs diff --git a/LICENSE b/LICENSE index e1bf1b4..18c9cf7 100644 --- a/LICENSE +++ b/LICENSE @@ -1,9 +1,21 @@ MIT License -Copyright (c) 2024-2025 Cardinal Space Mining Club +Copyright (c) 2024-2026 Cardinal Space Mining Club -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index c9f93f7..e5ba1e3 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,35 @@ ![Cardinal Perception](doc/cardinal-perception.png) -Cardinal Perception is CSM's perception package used as a base for all advanced robot autonomy. It is structured as a multi-stage pipeline, consisting of **odometry**, **fiducial-based localization**, **mapping**, **traversiblity estimation**, and **path-planning** components. +Cardinal Perception is CSM's ROS2 perception package which comprises all the necessary prerequisites for advanced autonomy; most notably localization, path-planning and terrain analysis. It has been designed around using the SICK MultiScan136 3D-LiDAR as its only input, although is highly configrable and likely to support other LiDAR/IMU setups. -## Overview -![architecture overview](doc/cardinal-perception-v050-overview.svg) +# Setup +> [!IMPORTANT] +> **CSM team members:** Cardinal Perception is usually included as a submodule in larger robot code projects, where build/install procedures are integrated into a larger system/script. **YOU SHOULD NOT NEED TO CLONE/USE THIS REPO ON ITS OWN!** -*This diagram is slightly out of date!* +> [!NOTE] +> Cardinal Perception has been verified to build and function on **ROS2 Humble (Ubuntu 22.04)**, **Jazzy (Ubuntu 24.04)** and **Kilted (Ubuntu 24.04)**, on both **x86-64** and **aarch64** architectures, as well as **WSL**. -See the [architecture documentation](doc/architecture.md) for more information on individual pipeline stages. - -## Build 1. Install [ROS2](https://docs.ros.org/en/jazzy/Installation.html) if necessary -2. Use rosdep to install ROS package dependencies +2. Setup your workspace and clone required repos + - Create directories: + ```bash + mkdir ros-ws && cd ros-ws + mkdir src && cd src + ``` + - Clone repos: + ```bash + git clone https://github.com/Cardinal-Space-Mining/Cardinal-Perception -b main cardinal-perception + git clone https://github.com/Cardinal-Space-Mining/launch-utils -b main launch-utils + git clone https://github.com/Cardinal-Space-Mining/csm-metrics -b main csm-metrics + ``` + - Navigate back to your workspace directory for the following steps: + ```bash + cd .. + ``` + +3. Use rosdep to install ROS package dependencies - Initialize rosdep if necessary: ```bash sudo rosdep init @@ -21,53 +37,63 @@ See the [architecture documentation](doc/architecture.md) for more information o - Update and install: ```bash rosdep update - rosdep install --ignore-src --from-paths . -r -y + rosdep install --ignore-src --from-paths ./src -r -y ``` -3. Install apt dependencies (should have already been resolved by rosdep) +4. Install apt dependencies (should have already been resolved by rosdep) ```bash sudo apt update sudo apt-get install libpcl-dev libopencv-dev ``` -4. Build with colcon +5. Build with colcon ```bash - colcon build --symlink-install <--executor parallel> <--event-handlers console_direct+> <--cmake-args=-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON> + colcon build \ + --symlink-install \ + --event-handlers console_direct+ \ + --cmake-args=-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON source install/setup.bash ``` - Additionally, there are various compile-time configurations which are exposed as CMake options. These are all listed in the [config generator template](cmake/config.hpp.in). +> [!TIP] +> There are various compile-time configurations which are exposed as CMake options. These are all listed in the [config generator template](cmake/config.hpp.in). -*The project has been verified to build and function on ROS2 Humble (Ubuntu 22.04), Jazzy (Ubuntu 24.04) and Kilted (Ubuntu 24.04), on both x86-64 and aarch64 architectures, as well as WSL.* +# Usage +The best way to run Cardinal Perception is by using the included launchfile, which utilizes [launch-utils](https://github.com/Cardinal-Perception/launch-utils) to setup everything using the included [JSON config](config/perception.json): +```bash +ros2 launch cardinal_perception perception.launch.py +``` +> [!IMPORTANT] +> Review the following sections to ensure ROS2 and your config file are setup properly! -## Usage -### Prerequisites -**To run Cardinal Perception, you will need (REQUIRED):** -- A `sensor_msgs::msg::PointCloud2` topic providing a 3D LiDAR scan. -- A correctly configured `config/perception.yaml` file (or equivalent) - see the [related documentation](doc/config.md) for information on parameters. +## ROS Prerequisites +Cardinal Perception requires the following in order to run: +1. A `sensor_msgs::msg::PointCloud2` topic providing a 3D LiDAR scan. This can be live or from a bag. +2. A proper transform definition, usually published on `/tf` and `/tf_static` by `robot_state_publisher`. The launch-utils package provides a way to bundle the config for this and automatically launch this node - see the [relevant docs](https://github.com/Cardinal-Space-Mining/launch-utils?tab=readme-ov-file#usage) for more information. -**Optionally, you may also need:** -- A `sensor_msgs::msg::Imu` topic providing IMU samples. This can help stabilize the odometry system, especially when an orientation estimate is directly usable as apart of each sample. -- A set of `/tf` of `/tf_static` transforms provided by `robot_state_publisher`. This is necessary when the coordinate frame of the LiDAR scan is different from the coordinate frame of the IMU, or if you want to compute odometry for a frame different than that of the LiDAR scan. -- A customized launch configuration to support your specific robot setup. +The following are not required but are useful in some situations: +- A `sensor_msgs::msg::Imu` topic providing IMU samples. This is used by the LIO system to pre-align scans and can drastically improve localization quality when in featureless environments or if scan message reliability is decreased. Some LiDARs contain integrated IMUs in which case this is a freebe. +- A set of `sensor_msgs::msg::Image` and accompanying `sensor_msgs::msg::CameraInfo` topic for any number of cameras. These can be used to enable the optional AprilTag alignment pipeline. -**Finally, to use the AprilTag detector for global estimates, you will need:** -- A set of `sensor_msgs::msg::Image` and accompanying `sensor_msgs::msg::CameraInfo` topic for each camera to be used. -- A launch configuration file describing the behavior of the fiducial-tag system. See the provided [config file](config/perception.json) for an example. +## Configuration +As already alluded, Cardinal Perception uses the [launch-utils](https://github.com/Cardinal-Perception/launch-utils) package to handle configuration and launching. This is due to the fact that many other ROS2 nodes must be run alongside Cardinal Perception in order to accomplish anything useful. By default, the included launch system uses [this file](config/perception.json) to configure and launch everything. This is merely a starting point which includes defaults for all the main parameters that can be used by Cardinal Perception, as well a barebones robot setup for the various boilerplate ROS2 nodes. -### Running -The following nodes are built, which can all be run individually or in a launch system: -- `perception_node` - The core perception package -- `tag_detection_node` - The apriltag detector -- `pplan_client_node` - A service caller that can interface with foxglove studio cursor clicks +The primary config sections used by Cardinal Perception are: +- `perception`: Configures the main perception node. +- `tag_detection`: Configures the AprilTag detector node. +- `pplan_client`: Configures (enables/disables) the path planning client node. -However, to fully configure the perception system, you will need to build and source the [launch_utils](https://github.com/Cardinal-Space-Mining/launch-utils) package. All three packages can then be configured an run using the [JSON config](config/perception.json) and following launch command: -```bash -ros2 launch cardinal_perception perception.launch.py -``` -**_For an advanced usage example, see [this repo](https://github.com/Cardinal-Space-Mining/lance-2025)._** +Due to the large number of paramters, up-to-date descriptions on what each one does have not been documented here. Old documentation can be referenced [here](doc/config.md) for some parameters that haven't changed in a while, and more specific docs about this may be included in the future. Fortunately, many configs can be left unchanged and the most crucial ones are quite straightforward in what they do. + +> [!TIP] +> To better understand the config file layout, as well as boilerplate config sections, check out the [relevant docs](https://github.com/Cardinal-Space-Mining/launch-utils?tab=readme-ov-file#how-it-works). + +> [!TIP] +> The example config is a cut down version of the primarly config used by CSM's LANCE robot(s). The full config can be found [here](https://github.com/Cardinal-Space-Mining/lance-2025/blob/main/lance/config/lance.json). + +# Development ## VSCode -The build script exports compile commands which can help VSCode's C/C++ extension resolve correct syntax highlighting. To ensure this is working, paste the following code into the `c_cpp_properties.json` file (under .vscode directory in a workspace): +The provided build command exports compile commands which can help VSCode's C/C++ extension resolve correct syntax highlighting. To ensure this is working, paste the following code into the `c_cpp_properties.json` file (under .vscode directory in a workspace): ```json { "configurations": [ @@ -89,5 +115,6 @@ The build script exports compile commands which can help VSCode's C/C++ extensio "version": 4 } ``` -__*Last updated: 9/13/25*__ + +__*Last updated on 1/19/26*__ diff --git a/cmake/Config.cmake b/cmake/Config.cmake index 47b22c8..bf353e7 100644 --- a/cmake/Config.cmake +++ b/cmake/Config.cmake @@ -1,19 +1,14 @@ # Config.cmake # Central place to declare all perception-related CMake options -# --- MODULES CONFIGURATION ------------- -option(KFC_MAP_STORE_INSTANCE_BUFFERS "Enable map store instance buffers" ON) -option(LFD_USE_ORTHO_PLANE_INTERSECTION "Use ortho plane intersection" ON) -option(LFD_PRINT_DEBUG "Print LFD debug logs" OFF) -option(PPLAN_PRINT_DEBUG "Print Path Planning debug logs" OFF) -option(PATH_PLANNING_PEDANTIC "Enable pedantic path planning" OFF) - # --- PROFILING CONFIGURATION ----------- set(PROFILING_MODE "PROFILING_MODE_LIMITED" CACHE STRING "Profiling mode") # full profiling use 'PROFILING_MODE_ALL' set(PROFILING_DEFAULT_BUFFER_SIZE 1 CACHE STRING "Profiling default buffer size") # full profiling use 20 # --- PRINTING ENABLE/DISABLE ----------- option(PERCEPTION_PRINT_STARTUP_CONFIGS "Print startup configs" ON) +option(LFD_PRINT_DEBUG "Print LFD debug logs" OFF) +option(PPLAN_PRINT_DEBUG "Print Path Planning debug logs" OFF) option(TRANSFORM_SYNC_PRINT_DEBUG "Enable transform sync debug logs" OFF) option(TRAJECTORY_FILTER_PRINT_DEBUG "Enable trajectory filter debug logs" OFF) @@ -30,9 +25,6 @@ option(PERCEPTION_USE_SCAN_DESKEW "Use scan deskew" OFF) option(PERCEPTION_USE_NULL_RAY_DELETION "Use null ray deletion" OFF) # --- PIPELINE STAGES ENABLE/DISABLE ---- -option(PERCEPTION_ENABLE_MAPPING "Enable mapping" ON) -option(PERCEPTION_ENABLE_TRAVERSIBILITY "Enable traversibility" ON) -option(PERCEPTION_ENABLE_PATH_PLANNING "Enable path planning" ON) option(PERCEPTION_USE_TAG_DETECTION_PIPELINE "Use tag detection pipeline" OFF) option(PERCEPTION_USE_LFD_PIPELINE "Use LFD pipeline" ON) @@ -42,12 +34,9 @@ set(PERCEPTION_PUBSUB_QOS "rclcpp::SensorDataQoS()" CACHE STRING "PubSub QoS con # Boolean options foreach(opt - KFC_MAP_STORE_INSTANCE_BUFFERS - LFD_USE_ORTHO_PLANE_INTERSECTION + PERCEPTION_PRINT_STARTUP_CONFIGS LFD_PRINT_DEBUG PPLAN_PRINT_DEBUG - PATH_PLANNING_PEDANTIC - PERCEPTION_PRINT_STARTUP_CONFIGS TRANSFORM_SYNC_PRINT_DEBUG TRAJECTORY_FILTER_PRINT_DEBUG PERCEPTION_PUBLISH_GRAV_ESTIMATION @@ -58,9 +47,6 @@ foreach(opt PERCEPTION_PUBLISH_FULL_MAP PERCEPTION_USE_SCAN_DESKEW PERCEPTION_USE_NULL_RAY_DELETION - PERCEPTION_ENABLE_MAPPING - PERCEPTION_ENABLE_TRAVERSIBILITY - PERCEPTION_ENABLE_PATH_PLANNING PERCEPTION_USE_TAG_DETECTION_PIPELINE PERCEPTION_USE_LFD_PIPELINE ) diff --git a/cmake/config.hpp.in b/cmake/config.hpp.in index ab4f840..6dcd7df 100644 --- a/cmake/config.hpp.in +++ b/cmake/config.hpp.in @@ -3,19 +3,14 @@ #include #include -// --- MODULES CONFIGURATION ------------- -#define KFC_MAP_STORE_INSTANCE_BUFFERS @KFC_MAP_STORE_INSTANCE_BUFFERS_VALUE@ -#define LFD_USE_ORTHO_PLANE_INTERSECTION @LFD_USE_ORTHO_PLANE_INTERSECTION_VALUE@ -#define LFD_PRINT_DEBUG @LFD_PRINT_DEBUG_VALUE@ -#define PPLAN_PRINT_DEBUG @PPLAN_PRINT_DEBUG_VALUE@ -#define PATH_PLANNING_PEDANTIC @PATH_PLANNING_PEDANTIC_VALUE@ - // --- PROFILING CONFIGURATION ----------- #define PROFILING_MODE @PROFILING_MODE@ #define PROFILING_DEFAULT_BUFFER_SIZE @PROFILING_DEFAULT_BUFFER_SIZE@ // --- PRINTING ENABLE/DISABLE ----------- #define PERCEPTION_PRINT_STARTUP_CONFIGS @PERCEPTION_PRINT_STARTUP_CONFIGS_VALUE@ +#define LFD_PRINT_DEBUG @LFD_PRINT_DEBUG_VALUE@ +#define PPLAN_PRINT_DEBUG @PPLAN_PRINT_DEBUG_VALUE@ #define TRANSFORM_SYNC_PRINT_DEBUG @TRANSFORM_SYNC_PRINT_DEBUG_VALUE@ #define TRAJECTORY_FILTER_PRINT_DEBUG @TRAJECTORY_FILTER_PRINT_DEBUG_VALUE@ @@ -32,9 +27,6 @@ #define PERCEPTION_USE_NULL_RAY_DELETION @PERCEPTION_USE_NULL_RAY_DELETION_VALUE@ // --- PIPELINE STAGES ENABLE/DISABLE ---- -#define PERCEPTION_ENABLE_MAPPING @PERCEPTION_ENABLE_MAPPING_VALUE@ -#define PERCEPTION_ENABLE_TRAVERSIBILITY @PERCEPTION_ENABLE_TRAVERSIBILITY_VALUE@ -#define PERCEPTION_ENABLE_PATH_PLANNING @PERCEPTION_ENABLE_PATH_PLANNING_VALUE@ #define PERCEPTION_USE_TAG_DETECTION_PIPELINE @PERCEPTION_USE_TAG_DETECTION_PIPELINE_VALUE@ #define PERCEPTION_USE_LFD_PIPELINE @PERCEPTION_USE_LFD_PIPELINE_VALUE@ @@ -48,15 +40,16 @@ namespace csm namespace perception { -using OdomPointType = pcl::PointXYZ; -using MappingPointType = pcl::PointXYZ; +using MinimalPointType = pcl::PointXYZ; + +using OdomPointType = MinimalPointType; +using MappingPointType = MinimalPointType; using FiducialPointType = csm::perception::PointXYZR; -using CollisionPointType = pcl::PointXYZLNormal; +using TraversibilityPointType = pcl::PointXYZI; + using RayDirectionType = pcl::Axis; using SphericalDirectionPointType = csm::perception::PointSDir; using TimestampPointType = csm::perception::PointT_32HL; -using TraversibilityPointType = pcl::PointXYZ; -using TraversibilityMetaType = csm::perception::NormalTraversal; }; // namespace perception }; // namespace csm diff --git a/config/perception.json b/config/perception.json index bc70893..80967ec 100644 --- a/config/perception.json +++ b/config/perception.json @@ -105,10 +105,10 @@ "s2s": { "k_correspondences": 10, - "max_correspondence_distance": 0.4, + "max_correspondence_distance": 0.5, "max_iterations": 32, - "transformation_epsilon": 0.01, - "euclidean_fitness_epsilon": 0.01, + "transformation_epsilon": 0.001, + "euclidean_fitness_epsilon": 0.001, "ransac": { "iterations": 5, @@ -120,8 +120,8 @@ "k_correspondences": 15, "max_correspondence_distance": 0.5, "max_iterations": 32, - "transformation_epsilon": 0.0009, - "euclidean_fitness_epsilon": 0.005, + "transformation_epsilon": 0.0005, + "euclidean_fitness_epsilon": 0.001, "ransac": { "iterations": 6, @@ -144,17 +144,44 @@ }, "mapping": { - "frustum_search_radius": 0.04, - "radial_distance_thresh": 0.05, - "delete_delta_coeff": 0.08, - "delete_max_range": 3.5, - "add_max_range": 3.5, - "voxel_size": 0.1 + "crop_horizontal_range": 5.0, + "crop_vertical_range": 1.0, + "frustum_search_radius": 0.015, + "radial_distance_thresh": 0.008, + "surface_width": 0.02, + "delete_max_range": 2.5, + "add_max_range": 2.5, + "voxel_size": 0.02 }, "traversibility": { - "chunk_horizontal_range": 3.5, - "chunk_vertical_range": 1.0 + "chunk_horizontal_range": 2.5, + "chunk_vertical_range": 1.0, + "normal_estimation_radius": 0.2, + "output_res": 0.1, + "grad_search_radius": 0.3, + "min_grad_diff": 0.15, + "non_trav_grad_angle": 45.0, + "avoidance_radius": 0.35, + "trav_score_curvature_weight": 5.0, + "trav_score_grad_weight": 1.0, + "min_vox_cell_points": 3, + "interp_point_samples": 7 + }, + "pplan": + { + "boundary_radius": 0.15, + "goal_threshold": 0.1, + "search_radius": 0.5, + "distance_coeff": 1.0, + "straightness_coeff": 2.0, + "traversibility_coeff": 1.0, + "verification_range": 1.5, + "verification_degree": 2, + "max_neighbors": 11, + "map_obstacle_merge_window": 0.5, + "map_passive_crop_horizontal_range": 10.0, + "map_passive_crop_vertical_range": 5.0 } }, "sim": @@ -468,7 +495,7 @@ }, "robot_tf": { - "pragma:default": "lance", + "pragma:default": "lance2", "lance": { "robot_description": @@ -478,8 +505,6 @@ "links": [ "base_link", "frame_link", - "left_track_link", - "right_track_link", "collection_link", "lidar_link", "lidar_link_inv_rotated" @@ -491,18 +516,6 @@ "child": "frame_link", "origin": { "xyz": [0, 0, 0.13103567] } }, - "left_track_joint": { - "type": "fixed", - "parent": "frame_link", - "child": "left_track_link", - "origin": { "xyz": [0.20318559, 0.289888, 0.00242022] } - }, - "right_track_joint": { - "type": "fixed", - "parent": "frame_link", - "child": "right_track_link", - "origin": { "xyz": [0.20318559, -0.289888, 0.00242022] } - }, "dump_joint": { "type": "revolute", "parent": "frame_link", @@ -540,6 +553,54 @@ } } } + }, + "lance2": + { + "robot_description": + { + "name": "lance2", + "structure": + { + "links": [ + "base_link", + "hopper_link", + "lidar_link" + ], + "joints": + { + "hopper_joint": + { + "type": "revolute", + "parent": "base_link", + "child": "hopper_link", + "origin": + { + "xyz": [-0.4572, 0, 0.3488], + "rpy": [0, 0.175, 0] + }, + "axis": [0, 1, 0], + "limit": + { + "upper": 0.175, + "lower": -0.175, + "effort": 100, + "velocity": 100 + } + }, + "lidar_joint": + { + "type": "fixed", + "parent": "base_link", + "child": "lidar_link", + "origin": + { + "xyz": [0.5156, 0.0, 0.5691], + "rpy": [3.14159, 0.349066, 0.0] + } + } + } + } + } } }, "bag_play": diff --git a/doc/architecture.md b/doc/architecture.md index c453989..a93da18 100644 --- a/doc/architecture.md +++ b/doc/architecture.md @@ -1,4 +1,11 @@ ## Cardinal Perception System Architecture + +> [!WARNING] +> This doc is out of date and currently under construction! + +### V0.5.0 System Overview +![architecture overview](cardinal-perception-v050-overview.svg) + ### Localization The localization solution consists of two components: LiDAR odometry and fiducial detection for global alignment. The LiDAR odometry is based off of [direct_lidar_odometry](https://github.com/vectr-ucla/direct_lidar_odometry) (DLO) and utilizes a scan-to-scan and scan-to-map based odometry solution, with optional IMU input to seed registration. Fiducial detection is not required but can come in one of two forms - AprilTag detections or LiDAR-reflector detection (this is custom). Fusion between local and global measurements is currently only done on a simple transform-offset basis, utilizing the `map` and `odom` transform-tree frames (meaning that without fiducial detection, odometry is implicitly treated as the full localization solution). @@ -30,4 +37,4 @@ Terrain mapping attempts to model the real world utilizing a set of cartesian po ### Trajectory Generation (Not implemented yet!) -__*Last updated: 2/19/25*__ +__*Last updated: 1/19/26*__ diff --git a/doc/config.md b/doc/config.md index ec968cd..f284b66 100644 --- a/doc/config.md +++ b/doc/config.md @@ -1,11 +1,11 @@ ## Node Parameters -# THIS SPEC IS OUT OF DATE FOR V0.6.0+! -New documentation to come soon... hopefully :) +> [!CAUTION] +> This doc is out of date for V0.6.0+. Some parameters listed here have been removed, and others have been added. Use only as a guide for the parameters that are still relevant and listed in the example config file. --- ### Perception Node -_See file: [config/perception.yaml](../config/perception.yaml)_ +_Config block:_ `perception` * `map_frame_id` (String) : **The global frame id** - Default: `"map"` @@ -206,7 +206,7 @@ _See file: [config/perception.yaml](../config/perception.yaml)_ --- ### Tag Detection Node -_See file: [config/tag_detection.yaml](../config/tag_detection.yaml)_ +_Config block:_ `tag_detection` * `image_transport` (String) : **Transport type for image stream** - Default: `"raw"` @@ -310,4 +310,6 @@ _See file: [config/tag_detection.yaml](../config/tag_detection.yaml)_ * `tag5_corners` (Float Array) - Default: `[0.303, 0.360, 0.476, 0.364, 0.280, 0.477, 0.362, 0.280, 0.577, 0.301, 0.360, 0.576]` -__*Last updated: 2/19/25*__ +__*Last updated on 1/19/26*__ + +__*Parameters relevant as of 2/19/25*__ diff --git a/include/d_ary_heap.hpp b/include/d_ary_heap.hpp deleted file mode 100644 index db6c431..0000000 --- a/include/d_ary_heap.hpp +++ /dev/null @@ -1,282 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -template< - class Key, // priority key type (float, int, etc.) - class Index = int, // ID type (int32_t, etc.) - class Comparator = std::less, // Min-heap by default - int D = 4> // arity: default 4 -class DaryHeap -{ - static_assert(D >= 2, "D-ary heap requires D >= 2"); - static_assert(std::is_integral::value, "Index must be integral"); - -public: - using key_type = Key; - using index_type = Index; - using size_type = std::size_t; - - explicit DaryHeap(Index max_id = 0, const Comparator& comp = Comparator()) : - _comp(comp) - { - if (max_id > 0) - { - reserve_ids(max_id + 1); - } - } - - // Pre-allocate space for IDs in [0, max_id] - void reserve_ids(Index max_id_plus_one) - { - const size_type n = static_cast(max_id_plus_one); - _pos.assign(n, kAbsent); - _keys.assign(n, Key{}); - } - - // Reserve heap capacity - void reserve_heap(size_type cap) { _heap.reserve(cap); } - - bool empty() const noexcept { return _heap.empty(); } - size_type size() const noexcept { return _heap.size(); } - - // Is id currently in the heap? - bool contains(Index id) const noexcept - { - return id_in_range(id) && _pos[static_cast(id)] != kAbsent; - } - - // Return the id with best key (minimum for Comparator=less) - Index top() const - { - assert(!empty()); - return _heap[0]; - } - - const Key& top_key() const - { - assert(!empty()); - return _keys[_heap[0]]; - } - - // Insert a new (id, key) - void push(Index id, const Key& key) - { - ensure_id(id); - assert(!contains(id)); - _keys[static_cast(id)] = key; - _heap.push_back(id); - _pos[static_cast(id)] = static_cast(_heap.size() - 1); - sift_up(_heap.size() - 1); - } - - // Extract-best (min for Comparator=less) - Index pop() - { - assert(!empty()); - Index root = _heap[0]; - remove_at_root(); - return root; - } - - // Remove id if present; returns true if removed. - bool erase(Index id) - { - if (!contains(id)) - { - return false; - } - size_type i = static_cast(_pos[id]); - remove_at(i); - return true; - } - - // Update the key and restore heap order in the right direction. - // If you know it only decreased/increased, call the specialized version. - void update_key(Index id, const Key& new_key) - { - assert(contains(id)); - size_type i = static_cast(_pos[id]); - const Key& old = _keys[id]; - _keys[id] = new_key; - if (is_better(new_key, old)) - { - sift_up(i); - } - else if (is_better(old, new_key)) - { - sift_down(i); - } - // equal: no-op - } - - // Strict decrease (if new_key is known to be better) - void decrease_key(Index id, const Key& new_key) - { - assert(contains(id)); - size_type i = static_cast(_pos[id]); - // For min-heap, new_key must be strictly better than old - assert(is_better(new_key, _keys[id])); - _keys[id] = new_key; - sift_up(i); - } - - // Strict increase (if new_key is known to be worse) - void increase_key(Index id, const Key& new_key) - { - assert(contains(id)); - size_type i = static_cast(_pos[id]); - assert(is_better(_keys[id], new_key)); - _keys[id] = new_key; - sift_down(i); - } - - // Access or set key of an id (valid even if not contained). - const Key& key(Index id) const - { - ensure_id(id); - return _keys[static_cast(id)]; - } - void set_key(Index id, const Key& k) - { - ensure_id(id); - _keys[static_cast(id)] = k; - } - -private: - // Storage: - // heap: array of ids in heap order - // pos[id]: index in heap[] or kAbsent if not present - // keys[id]: current key associated with id - std::vector _heap; - std::vector _pos; - std::vector _keys; - Comparator _comp; - - static constexpr Index kAbsent = static_cast(-1); - - // Helpers - static constexpr size_type parent(size_type i) noexcept - { - return (i - 1) / D; - } - static constexpr size_type child0(size_type i) noexcept - { - return D * i + 1; - } - - bool id_in_range(Index id) const noexcept - { - return static_cast(id) < _pos.size(); - } - - void ensure_id(Index id) - { - if (!id_in_range(id)) - { - // Grow pos/keys to accommodate id - size_type new_size = static_cast(id) + 1; - _pos.resize(new_size, kAbsent); - _keys.resize(new_size); - } - } - - inline bool better(Index a, Index b) const noexcept - { - // a has higher priority than b if comp(key[a], key[b]) is true - return _comp(_keys[a], _keys[b]); - } - inline bool is_better(const Key& a, const Key& b) const noexcept - { - return _comp(a, b); - } - - void swap_nodes(size_type i, size_type j) noexcept - { - Index ai = _heap[i], aj = _heap[j]; - std::swap(_heap[i], _heap[j]); - _pos[ai] = static_cast(j); - _pos[aj] = static_cast(i); - } - - void sift_up(size_type i) noexcept - { - while (i > 0) - { - size_type p = parent(i); - if (!better(_heap[i], _heap[p])) - { - break; - } - swap_nodes(i, p); - i = p; - } - } - - void sift_down(size_type i) noexcept - { - const size_type n = _heap.size(); - for (;;) - { - size_type best = i; - size_type c = child0(i); - // Check up to D children - for (int k = 0; k < D; ++k, ++c) - { - if (c < n && better(_heap[c], _heap[best])) - { - best = c; - } - } - if (best == i) - { - break; - } - swap_nodes(i, best); - i = best; - } - } - - void remove_at_root() noexcept - { - const size_type last = _heap.size() - 1; - Index root_id = _heap[0]; - _pos[root_id] = kAbsent; - if (last == 0) - { - _heap.pop_back(); - return; - } - _heap[0] = _heap[last]; - _pos[_heap[0]] = 0; - _heap.pop_back(); - sift_down(0); - } - - void remove_at(size_type i) noexcept - { - const size_type last = _heap.size() - 1; - Index id = _heap[i]; - _pos[id] = kAbsent; - if (i == last) - { - _heap.pop_back(); - return; - } - _heap[i] = _heap[last]; - _pos[_heap[i]] = static_cast(i); - _heap.pop_back(); - // Reorder both directions conservatively - if (i > 0 && better(_heap[i], _heap[parent(i)])) - { - sift_up(i); - } - else - { - sift_down(i); - } - } -}; diff --git a/include/modules/impl/kfc_map_impl.hpp b/include/modules/impl/kfc_map_impl.hpp index 978b795..1bc5df8 100644 --- a/include/modules/impl/kfc_map_impl.hpp +++ b/include/modules/impl/kfc_map_impl.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -49,8 +49,8 @@ namespace csm namespace perception { -template -void KFCMap::applyParams( +template +void KFCMap::applyParams( double frustum_search_radius, double radial_dist_thresh, double surface_width, @@ -68,14 +68,12 @@ void KFCMap::applyParams( if (voxel_res > 0.) { - this->map_octree.setResolution(voxel_res); + this->map_octree.reconfigure(voxel_res); } } -template -void KFCMap::setBounds( - const Vec3f& min, - const Vec3f& max) +template +void KFCMap::setBounds(const Vec3f& min, const Vec3f& max) { std::unique_lock lock{this->mtx}; @@ -85,23 +83,28 @@ void KFCMap::setBounds( this->map_octree.crop(min, max, true); } -template +template template -typename KFCMap::UpdateResult - KFCMap::updateMap( - const Vec3f& origin, - const pcl::PointCloud& pts, - const std::vector* inf_rays, - const pcl::Indices* pt_indices) +typename KFCMap::UpdateResult KFCMap::updateMap( + const Vec3f& origin, + const PointCloudT& pts, + const std::vector* inf_rays, + const pcl::Indices* pt_indices) { + // 0. Init buffers --------------------------------------------------------- UpdateResult results{}; if (pt_indices && pt_indices->size() <= 0) { return results; } + pcl::Indices tmp_indices, points_to_add; + std::vector tmp_dists; + std::set submap_remove_indices; + std::unique_lock lock{this->mtx}; + // 1. Use raw if empty ----------------------------------------------------- auto map_cloud_ptr = this->map_octree.getInputCloud(); if (map_cloud_ptr->empty()) { @@ -110,44 +113,30 @@ typename KFCMap::UpdateResult return results; } -#if KFC_MAP_STORE_INSTANCE_BUFFERS <= 0 - thread_local struct - { - pcl::Indices search_indices, points_to_add; - std::vector dists; - std::set submap_remove_indices; - } buff; -#endif - - // reset search buffers - buff.search_indices.clear(); - buff.dists.clear(); - - // collect indices for points within range from scanner origin + // 2. Collect indices for points within range from scanner origin ---------- PointT lp; lp.getVector3fMap() = origin; results.points_searched = this->map_octree.radiusSearch( lp, this->delete_max_range, - buff.search_indices, - buff.dists); + tmp_indices, + tmp_dists); - // prepare submap range buffer + // 3. Compute range, direction for each point in submap -------------------- if (!this->submap_ranges) { - this->submap_ranges = - std::make_shared>(); + this->submap_ranges = std::make_shared(); } this->submap_ranges->clear(); - this->submap_ranges->reserve(buff.search_indices.size()); + this->submap_ranges->reserve(tmp_indices.size()); // IMPROVE: OMP parallelize - for (size_t i = 0; i < buff.search_indices.size(); i++) + for (size_t i = 0; i < tmp_indices.size(); i++) { // store distance and original index for each submap point auto& v = this->submap_ranges->points.emplace_back(); - v.curvature = std::sqrt(buff.dists[i]); - v.label = buff.search_indices[i]; + v.curvature = std::sqrt(tmp_dists[i]); + v.label = tmp_indices[i]; // store unit direction to each submap point from scanner const auto& p = map_cloud_ptr->points[v.label]; @@ -157,20 +146,18 @@ typename KFCMap::UpdateResult this->submap_ranges->width = this->submap_ranges->points.size(); this->submap_ranges->height = 1; - // build kdtree using unit directions of submap points - this->collision_kdtree.setInputCloud( - this->submap_ranges); // TODO: handle no map points + // 4. Build kdtree using unit directions of submap points ------------------ + // TODO: handle no map points + this->collision_kdtree.setInputCloud(this->submap_ranges); auto& scan_vec = pts.points; - buff.submap_remove_indices.clear(); - buff.points_to_add.clear(); - buff.points_to_add.reserve( - pt_indices ? pt_indices->size() : scan_vec.size()); + points_to_add.reserve(pt_indices ? pt_indices->size() : scan_vec.size()); - // loop all input points + // 5. Analyze collisions for all input points ------------------------------ // IMPROVE: could possibly parallelize if the outputs are synchronized for (size_t idx = 0;; idx++) { + // 5-A. Handle selection size_t i = idx; if (pt_indices) { @@ -185,24 +172,26 @@ typename KFCMap::UpdateResult break; } - // collision direction buffer + // 5-B. Compute direction and range for input point const auto& src_p = scan_vec[i]; CollisionPointT p; p.getNormalVector3fMap() = (src_p.getVector3fMap() - origin); p.curvature = p.getNormalVector3fMap().norm(); p.getVector3fMap() = p.getNormalVector3fMap().normalized(); - // search for points with directions in angular range + // 5-C. Search for points with directions in angular range using kdtree + tmp_indices.clear(); + tmp_dists.clear(); this->collision_kdtree.radiusSearch( p, this->frustum_search_radius, - buff.search_indices, - buff.dists); + tmp_indices, + tmp_dists); - // loop points in angular range - for (size_t j = 0; j < buff.search_indices.size(); j++) + // 5-D. Compare input point to all points in search set to analyze collisions + for (size_t j = 0; j < tmp_indices.size(); j++) { - pcl::index_t k = buff.search_indices[j]; + pcl::index_t k = tmp_indices[j]; // only consider keeping the point if it has a valid normal if (!(CollisionV & KF_COLLISION_MODEL_REMOVE_INVALID_NORMALS) || @@ -212,13 +201,14 @@ typename KFCMap::UpdateResult { // keep the point if it's outside the radial zone if ((this->submap_ranges->points[k].curvature * - std::sqrt(buff.dists[j])) > this->radial_dist_thresh) + std::sqrt(tmp_dists[j])) > this->radial_dist_thresh) { continue; } } - // find local plane intersection, and keep the point if the new point is "in range" of this sample + // find local plane intersection, and keep the point if the new + // point is "in range" of this sample auto n = this->map_octree.pointNormals()[k].template head<3>(); const float inv_a = 1.f / p.getVector3fMap().dot(n); const float b = @@ -233,20 +223,19 @@ typename KFCMap::UpdateResult } } - buff.submap_remove_indices.insert( - this->submap_ranges->points[k].label); + submap_remove_indices.insert(this->submap_ranges->points[k].label); } - // add point if it's in the currently configured bounds + // 5-E. Add point if it's in the currently configured bounds if ((src_p.getArray3fMap() > this->bounds_min).all() && (src_p.getArray3fMap() < this->bounds_max).all() && (p.curvature <= this->add_max_range)) { - buff.points_to_add.push_back(i); + points_to_add.push_back(i); } } - // if infinite range dirs provided, remove all points in respective collision zones + // 6. Repeat previous process with infinite ray directions, if provided ---- if (inf_rays) { for (const RayDirT& r : *inf_rays) @@ -254,45 +243,49 @@ typename KFCMap::UpdateResult CollisionPointT p; p.getVector3fMap() = r.getNormalVector3fMap(); + tmp_indices.clear(); + tmp_dists.clear(); this->collision_kdtree.radiusSearch( p, this->frustum_search_radius, - buff.search_indices, - buff.dists); + tmp_indices, + tmp_dists); - for (size_t i = 0; i < buff.search_indices.size(); i++) + for (size_t i = 0; i < tmp_indices.size(); i++) { - pcl::index_t k = buff.search_indices[i]; + pcl::index_t k = tmp_indices[i]; if constexpr (CollisionV & KF_COLLISION_MODEL_USE_RADIAL) { if ((this->submap_ranges->points[k].curvature * - std::sqrt(buff.dists[i])) > this->radial_dist_thresh) + std::sqrt(tmp_dists[i])) > this->radial_dist_thresh) { continue; } } - buff.submap_remove_indices.insert( + submap_remove_indices.insert( this->submap_ranges->points[k].label); } } } - // delete all points which were "collided with" - for (auto itr = buff.submap_remove_indices.begin(); - itr != buff.submap_remove_indices.end(); + // 7. Delete all points which were "collided with" from the map ------------ + for (auto itr = submap_remove_indices.begin(); + itr != submap_remove_indices.end(); itr++) { - this->map_octree.deletePoint(*itr); + this->map_octree.deletePoint(*itr, true); } - // add source points - this->map_octree.addPoints(pts, &buff.points_to_add); - // ensure point buffers are dense + + // 8. Add inputs points to map --------------------------------------------- + this->map_octree.addPoints(pts, &points_to_add); + + // 9. Ensure map point buffer is dense ------------------------------------- this->map_octree.optimizeStorage(); results.points_deleted = - static_cast(buff.submap_remove_indices.size()); + static_cast(submap_remove_indices.size()); return results; } diff --git a/include/modules/impl/lf_detector_impl.hpp b/include/modules/impl/lf_detector_impl.hpp index d3cbb91..2f5ca1a 100644 --- a/include/modules/impl/lf_detector_impl.hpp +++ b/include/modules/impl/lf_detector_impl.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -46,8 +46,8 @@ #include #include -#include "cloud_ops.hpp" -#include "util.hpp" +#include +#include #if LFD_PRINT_DEBUG @@ -73,7 +73,7 @@ LidarFiducialDetector

::LidarFiducialDetector() for (size_t i = 0; i < this->seg_clouds.size(); i++) { - this->seg_cloud_ptrs[i] = util::wrap_unmanaged(this->seg_clouds[i]); + this->seg_cloud_ptrs[i] = util::wrapUnmanaged(this->seg_clouds[i]); } } @@ -198,7 +198,7 @@ typename LidarFiducialDetector

::DetectionStatus } // extract reflective points and voxelize them into the first seg buffer - util::voxel_filter( + util::voxelFilter( this->in_cloud, this->seg_clouds[0], Vec3f::Constant(this->param.vox_filter_res), @@ -301,7 +301,7 @@ typename LidarFiducialDetector

::DetectionStatus "LFD: combined unused reflective points - using " << this->redetect_cloud.size() << " total points for redetection"); - util::voxel_filter( + util::voxelFilter( this->redetect_cloud, this->seg_clouds[status.iterations], Vec3f::Constant(this->param.vox_filter_res)); @@ -454,7 +454,7 @@ size_t LidarFiducialDetector

::segmentPlanes( << ", d: " << this->plane_coeffs.values[3] << "]"); // copy the rest of the points to the next seg cloud - util::pc_copy_inverse_selection( + util::copyInverseSelection( this->seg_clouds[iter], this->seg_indices.indices, this->seg_clouds[iter + 1]); @@ -464,7 +464,7 @@ size_t LidarFiducialDetector

::segmentPlanes( << " points to next seg buffer"); // each cloud ends up with only the segmented points - util::pc_normalize_selection( + util::trimToSelection( this->seg_clouds[iter], this->seg_indices.indices); diff --git a/include/modules/impl/lidar_odom_impl.hpp b/include/modules/impl/lidar_odom_impl.hpp index fc8003e..2d8a35c 100644 --- a/include/modules/impl/lidar_odom_impl.hpp +++ b/include/modules/impl/lidar_odom_impl.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -43,6 +43,7 @@ #include #include +#include #include #include @@ -50,7 +51,9 @@ #include -#include +#include +#include +#include using namespace util::geom::cvt::ops; @@ -318,6 +321,9 @@ void LidarOdometry::initState() this->gicp.setSearchMethodSource(temp, true); this->gicp.setSearchMethodTarget(temp, true); + // this->gicp_s2s.setDebugPrint(true); + // this->gicp.setDebugPrint(true); + this->vf_scan.setLeafSize( this->param.vf_scan_res_, this->param.vf_scan_res_, @@ -339,14 +345,14 @@ bool LidarOdometry::setInitial(const util::geom::Pose3f& pose) // set known position this->state.T.template block<3, 1>(0, 3) = this->state.T_s2s.template block<3, 1>(0, 3) = - this->state.T_s2s_prev.template block<3, 1>(0, 3) = + this->state.T_prev.template block<3, 1>(0, 3) = this->state.translation = pose.vec; // set known orientation this->state.last_rotq = this->state.rotq = pose.quat; this->state.T.template block<3, 3>(0, 0) = this->state.T_s2s.template block<3, 3>(0, 0) = - this->state.T_s2s_prev.template block<3, 3>(0, 0) = + this->state.T_prev.template block<3, 3>(0, 0) = this->state.rotq.toRotationMatrix(); return true; @@ -366,7 +372,8 @@ typename LidarOdometry::IterationStatus LidarOdometry::processScan( if (stamp <= this->state.prev_frame_stamp) { std::cout << "[ODOMETRY]: Scan timestamp is older than the previous by " - << (this->state.prev_frame_stamp - stamp) << "s!" << std::endl; + << (this->state.prev_frame_stamp - stamp) << "s!" + << std::endl; return {}; } @@ -411,7 +418,10 @@ typename LidarOdometry::IterationStatus LidarOdometry::processScan( this->setInputSources(); // Get the next pose via IMU + S2S + S2M - this->getNextPose(align_estimate); + if(!this->getNextPose(align_estimate)) + { + return {}; + } // Transform point cloud this->transformCurrentScan(); @@ -451,7 +461,7 @@ void LidarOdometry::publishDebugScans( std::unique_lock scan_lock{this->state.mtx}; PointCloudMsg output; - output.header.stamp = util::toTimeStamp(this->state.curr_frame_stamp); + output.header.stamp = util::toTimeMsg(this->state.curr_frame_stamp); if (this->current_scan_t) { @@ -505,6 +515,15 @@ bool LidarOdometry::preprocessPoints(const PointCloudT& scan) return false; } + // for(const auto& pt : scan.points) + // { + // if(pt.getArray3fMap().isNaN().any()) + // { + // std::cout << "[ODOMETRY]: Input cloud had NaN input!" << std::endl; + // return false; + // } + // } + // Find new voxel size before applying filter if (this->param.adaptive_params_use_) { @@ -516,13 +535,13 @@ bool LidarOdometry::preprocessPoints(const PointCloudT& scan) this->current_scan = std::make_shared(); } - PointCloudConstPtrT cloud = util::wrap_unmanaged(&scan); + PointCloudConstPtrT cloud = util::wrapUnmanaged(&scan); #if 0 if (this->param.immediate_filter_use_) { pcl::Indices in_range; - util::pc_filter_distance( + util::filterDistance( scan, in_range, 0., @@ -530,7 +549,7 @@ bool LidarOdometry::preprocessPoints(const PointCloudT& scan) if (in_range.size() > scan.points.size() * this->param.immediate_filter_thresh_) { - util::pc_copy_inverse_selection( + util::copyInverseSelection( scan, in_range, *this->current_scan); @@ -707,7 +726,7 @@ void LidarOdometry::setInputSources() } template -void LidarOdometry::getNextPose(const std::optional& align_estimate) +bool LidarOdometry::getNextPose(const std::optional& align_estimate) { // // FRAME-TO-FRAME PROCEDURE @@ -729,16 +748,20 @@ void LidarOdometry::getNextPose(const std::optional& align_estimate) } // Get the local S2S transform - Mat4f T_S2S = this->gicp_s2s.getFinalTransformation(); + const Mat4f T_s2s_local = this->gicp_s2s.getFinalTransformation(); + + if (T_s2s_local.array().isNaN().any()) + { + std::cout << "[ODOMETRY]: S2S transform contained NaN values!" + << std::endl; + return false; + } // Get the global S2S transform - this->propagateS2S(T_S2S); + this->propagateS2S(T_s2s_local); // reuse covariances from s2s for s2m - this->gicp.source_covs_ = this->gicp_s2s.source_covs_; - - // Swap source and target (which also swaps KdTrees internally) for next S2S - this->gicp_s2s.swapSourceAndTarget(); + this->gicp.source_covs_ = this->gicp_s2s.target_covs_; // // FRAME-TO-SUBMAP @@ -765,10 +788,23 @@ void LidarOdometry::getNextPose(const std::optional& align_estimate) } // Get final transformation in global frame - this->state.T = this->gicp.getFinalTransformation(); + const Mat4f T = this->gicp.getFinalTransformation(); + + if (T.array().isNaN().any()) + { + std::cout << "[ODOMETRY]: S2M transform contained NaN values!" + << std::endl; + return false; + } + + this->state.T = std::move(T); + + // Swap source and target (which also swaps KdTrees internally) for next S2S + // (don't do this until we know it is safe to do so) + this->gicp_s2s.swapSourceAndTarget(); // Update the S2S transform for next propagation - this->state.T_s2s_prev = this->state.T; + this->state.T_prev = this->state.T; // Update next global pose // Both source and target clouds are in the global frame now, so tranformation is global @@ -776,13 +812,15 @@ void LidarOdometry::getNextPose(const std::optional& align_estimate) // Set next target cloud as current source cloud *this->target_cloud = *this->current_scan; + + return true; } template -void LidarOdometry::propagateS2S(const Mat4f& T) +void LidarOdometry::propagateS2S(const Mat4f& T_rel) { - this->state.T_s2s = this->state.T_s2s_prev * T; - this->state.T_s2s_prev = this->state.T_s2s; + this->state.T_s2s = this->state.T_prev * T_rel; + // this->state.T_prev = this->state.T_s2s; } template @@ -1042,7 +1080,7 @@ void LidarOdometry::updateKeyframes() // calculate difference in orientation double theta_rad = this->state.rotq.angularDistance( this->keyframes[closest_idx].first.second); - double theta_deg = theta_rad * (180.0 / M_PI); + double theta_deg = theta_rad * (180.0 / std::numbers::pi); // update keyframe const bool keyframe_close = diff --git a/include/modules/impl/map_octree_impl.hpp b/include/modules/impl/map_octree_impl.hpp index 43f9844..f3fc660 100644 --- a/include/modules/impl/map_octree_impl.hpp +++ b/include/modules/impl/map_octree_impl.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -85,8 +85,8 @@ void OctreeContainerPointIndex_Patched::reset() data_ = static_cast(-1); } -OctreeContainerPointIndex_Patched* - OctreeContainerPointIndex_Patched::deepCopy() const +OctreeContainerPointIndex_Patched* OctreeContainerPointIndex_Patched::deepCopy() + const { return (new OctreeContainerPointIndex_Patched(*this)); } @@ -123,18 +123,64 @@ const std::vector>& -template -MapOctree::MapOctree(const double vox_res) : - Super_T(vox_res), - cloud_buff{std::make_shared()} +template +MapOctree::MapOctree(double vox_res) : + SuperT(vox_res), + cloud_buff{std::make_shared()} { this->input_ = this->cloud_buff; this->cloud_buff->is_dense = false; } -template -size_t MapOctree::addPoint( +template +void MapOctree::clear() +{ + this->deleteTree(); + this->cloud_buff->clear(); + this->hole_indices.clear(); + if constexpr (HAS_POINT_STAMPS) + { + this->pt_stamps.clear(); + } + if constexpr (HAS_POINT_NORMALS) + { + this->pt_normals.clear(); + } +} + +template +void MapOctree::reconfigure(double vox_res) +{ + this->clear(); + this->setResolution(vox_res); +} + +template +const typename MapOctree::PointCloudT& MapOctree::points() + const +{ + return *this->cloud_buff; +} + +template +typename MapOctree::PointT& MapOctree::pointAt( + pcl::index_t pt_idx) +{ + const size_t pt_idx_ = static_cast(pt_idx); + assert(pt_idx_ < this->cloud_buff->size()); + + return (*this->cloud_buff)[pt_idx_]; +} + +template +size_t MapOctree::octreeSize() const +{ + return this->leaf_count_ + this->branch_count_; +} + +template +size_t MapOctree::addPoint( const PointT& pt, uint64_t stamp, bool compute_normal) @@ -194,7 +240,7 @@ size_t MapOctree::addPoint( const size_t idx_ = pt_idx->getPointIndex(); auto& map_point = (*this->cloud_buff)[idx_]; - if (Derived_T::mergePointFields(map_point, pt)) + if (DerivedT::mergePointFields(map_point, pt)) { this->hole_indices.push_back(idx_); pt_idx->reset(); @@ -226,9 +272,9 @@ size_t MapOctree::addPoint( } } -template -void MapOctree::addPoints( - const pcl::PointCloud& pts, +template +void MapOctree::addPoints( + const PointCloudT& pts, const pcl::Indices* indices) { std::vector map_indices; @@ -283,10 +329,8 @@ void MapOctree::addPoints( } } -template -void MapOctree::deletePoint( - const pcl::index_t pt_idx, - bool trim_nodes) +template +void MapOctree::deletePoint(const pcl::index_t pt_idx, bool trim_nodes) { const size_t pt_idx_ = static_cast(pt_idx); assert(pt_idx_ < this->cloud_buff->size()); @@ -317,8 +361,8 @@ void MapOctree::deletePoint( } } -template -void MapOctree::deletePoints( +template +void MapOctree::deletePoints( const pcl::Indices& indices, bool trim_nodes) { @@ -329,8 +373,8 @@ void MapOctree::deletePoints( } -template -void MapOctree::crop( +template +void MapOctree::crop( const Vec3f& min, const Vec3f& max, bool trim_nodes) @@ -372,19 +416,25 @@ void MapOctree::crop( } } -template -void MapOctree::optimizeStorage() +template +void MapOctree::optimizeStorage() { + // std::cout << "MapOctree::optimizeStorage() => start pts : " + // << this->cloud_buff->points.size() + // << ", holes : " << this->hole_indices.size() << std::endl; + // std::cout << "exhibit a" << std::endl; - if (this->hole_indices.size() >= this->cloud_buff->size()) + auto& points = this->cloud_buff->points; + + if (this->hole_indices.size() >= points.size()) { - this->cloud_buff->clear(); - if constexpr(HAS_POINT_STAMPS) + points.clear(); + if constexpr (HAS_POINT_STAMPS) { this->pt_stamps.clear(); } - if constexpr(HAS_POINT_NORMALS) + if constexpr (HAS_POINT_NORMALS) { this->pt_normals.clear(); } @@ -394,9 +444,8 @@ void MapOctree::optimizeStorage() // std::cout << "exhibit b" << std::endl; - const size_t target_len = - this->cloud_buff->size() - this->hole_indices.size(); - int64_t end_idx = static_cast(this->cloud_buff->size()) - 1; + const size_t target_len = points.size() - this->hole_indices.size(); + int64_t end_idx = static_cast(points.size()) - 1; // std::cout << "exhibit c" << std::endl; @@ -405,11 +454,10 @@ void MapOctree::optimizeStorage() { // std::cout << "exhibit d" << std::endl; - LeafContainer_T* pt_idx = nullptr; + LeafContainerT* pt_idx = nullptr; while (end_idx >= 0 && - (!pcl::isFinite((*this->cloud_buff)[end_idx]) || - !(pt_idx = - this->getOctreePoint((*this->cloud_buff)[end_idx], key)))) + (!pcl::isFinite(points[end_idx]) || + !(pt_idx = this->getOctreePoint(points[end_idx], key)))) { end_idx--; } @@ -430,12 +478,12 @@ void MapOctree::optimizeStorage() if (pt_idx->getSize() > 0 && pt_idx->getPointIndex() == end_idx) { { - (*this->cloud_buff)[idx] = (*this->cloud_buff)[end_idx]; - if constexpr(HAS_POINT_STAMPS) + points[idx] = points[end_idx]; + if constexpr (HAS_POINT_STAMPS) { this->pt_stamps[idx] = this->pt_stamps[end_idx]; } - if constexpr(HAS_POINT_NORMALS) + if constexpr (HAS_POINT_NORMALS) { this->pt_normals[idx] = this->pt_normals[end_idx]; } @@ -454,24 +502,34 @@ void MapOctree::optimizeStorage() // std::cout << "exhibit h" << std::endl; - this->cloud_buff->resize(end_idx + 1); - if constexpr(HAS_POINT_STAMPS) + points.resize(end_idx + 1); + this->cloud_buff->width = points.size(); + // if(points.size() <= points.capacity() / 2) + // { + // points.shrink_to_fit(); + // } + + if constexpr (HAS_POINT_STAMPS) { this->pt_stamps.resize(end_idx + 1); } - if constexpr(HAS_POINT_NORMALS) + if constexpr (HAS_POINT_NORMALS) { this->pt_normals.resize(end_idx + 1); } this->hole_indices.clear(); // std::cout << "exhibit i" << std::endl; + + // std::cout << "MapOctree::optimizeStorage() => end pts : " + // << this->cloud_buff->points.size() + // << ", holes : " << this->hole_indices.size() << std::endl; } -template -bool MapOctree::mergePointFields( +template +bool MapOctree::mergePointFields( PointT& map_point, const PointT& new_point) { @@ -480,7 +538,7 @@ bool MapOctree::mergePointFields( (map_point.getVector3fMap() *= POINT_MERGE_LPF_FACTOR) += (new_point.getVector3fMap() * INV_LPF_FACTOR); - if constexpr (util::traits::has_intensity::value) + if constexpr (pcl::traits::has_intensity::value) { (map_point.intensity *= POINT_MERGE_LPF_FACTOR) += (new_point.intensity * INV_LPF_FACTOR); @@ -509,10 +567,10 @@ bool MapOctree::mergePointFields( return false; } -template -void MapOctree::computePointNormal(size_t idx) +template +void MapOctree::computePointNormal(size_t idx) { - if constexpr(HAS_POINT_NORMALS) + if constexpr (HAS_POINT_NORMALS) { pcl::Indices neighbors; std::vector dists; @@ -526,7 +584,11 @@ void MapOctree::computePointNormal(size_t idx) Vec4f plane; float curvature; - pcl::computePointNormal(*this->cloud_buff, neighbors, plane, curvature); + pcl::computePointNormal( + *this->cloud_buff, + neighbors, + plane, + curvature); this->pt_normals[idx].template head<4>() = plane; this->pt_normals[idx][4] = curvature; } @@ -541,19 +603,18 @@ void MapOctree::computePointNormal(size_t idx) } } -template -typename MapOctree::LeafContainer_T* - MapOctree::getOctreePoint( - const PointT& pt, - pcl::octree::OctreeKey& key) +template +typename MapOctree::LeafContainerT* MapOctree::getOctreePoint( + const PointT& pt, + pcl::octree::OctreeKey& key) { this->genOctreeKeyforPoint(pt, key); return this->findLeaf(key); } -template -typename MapOctree::LeafContainer_T* - MapOctree::getOrCreateOctreePoint( +template +typename MapOctree::LeafContainerT* + MapOctree::getOrCreateOctreePoint( const PointT& pt, pcl::octree::OctreeKey& key) { @@ -563,8 +624,8 @@ typename MapOctree::LeafContainer_T* // generate key this->genOctreeKeyforPoint(pt, key); - typename Super_T::LeafNode* leaf_node; - typename Super_T::BranchNode* parent_branch_of_leaf_node; + typename SuperT::LeafNode* leaf_node; + typename SuperT::BranchNode* parent_branch_of_leaf_node; auto depth_mask = this->createLeafRecursive( key, this->depth_mask_, diff --git a/include/modules/impl/path_planner_impl.hpp b/include/modules/impl/path_planner_impl.hpp index aa83b23..af20a59 100644 --- a/include/modules/impl/path_planner_impl.hpp +++ b/include/modules/impl/path_planner_impl.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -48,7 +48,8 @@ #include -#include "d_ary_heap.hpp" +#include +#include #if PPLAN_PRINT_DEBUG #include @@ -57,166 +58,351 @@ #define DEBUG_COUT(...) #endif +using namespace csm::perception::traversibility; + namespace csm { namespace perception { -template -PathPlanner::Node::Node(const P& point, const M& meta, float h, Node* p) : - trav_point(point), - cost(meta.trav_weight()), - g(std::numeric_limits::infinity()), - h(h), - parent(p) +template +PathPlanner

::Node::Node(const P& point, float h, Node* p) : + point{point}, + dir{}, + g{std::numeric_limits::infinity()}, + h{h}, + parent{p} +{ +} + +template +PathPlanner

::Node::Node( + const PointT& point, + const Vec3f& dir, + float g, + float h, + Node* p) : + point{point}, + dir{dir}, + g{g}, + h{h}, + parent{p} { } -template -PathPlanner::PathPlanner() +template +PathPlanner

::PathPlanner() { this->kdtree.setSortedResults(true); } -template -void PathPlanner::setParameters( +template +void PathPlanner

::setParameters( float boundary_radius, float goal_threshold, float search_radius, - float lambda_dist, - float lambda_penalty, + float distance_coeff, + float straightness_coeff, + float traversibility_coeff, + float verification_range, + size_t verification_degree, size_t max_neighbors) { this->boundary_radius = boundary_radius; this->goal_threshold = goal_threshold; this->search_radius = search_radius; - this->lambda_dist = lambda_dist; - this->lambda_penalty = lambda_penalty; + this->distance_coeff = distance_coeff; + this->straightness_coeff = straightness_coeff; + this->traversibility_coeff = traversibility_coeff; + this->verification_range = verification_range; + this->verification_degree = verification_degree; this->max_neighbors = max_neighbors; } -template -bool PathPlanner::solvePath( +// template +// bool PathPlanner

::solvePath( +// std::vector& path, +// const Vec3f& start, +// const Vec3f& goal, +// const Vec3f& bound_min, +// const Vec3f& bound_max, +// const PointCloudT& points, +// const WeightT max_weight) +// { +// return false; +// } + +template +bool PathPlanner

::solvePath( + std::vector& path, const Vec3f& start, const Vec3f& goal, - const Vec3f& local_bound_min, - const Vec3f& local_bound_max, - const PointCloudT& loc_cloud, - const MetaCloudT& meta_cloud, - std::vector& path) + const PathPlanMapT& map, + const WeightT max_weight) { -#if PATH_PLANNING_PEDANTIC - if (start.x() < local_bound_min.x() || start.y() < local_bound_min.y() || - start.x() > local_bound_max.x() || start.y() > local_bound_max.y()) + // 0. Init state + const typename PathPlanMapT::UEOctreeT& ue_space = map.getUESpace(); + const PointCloudT& map_points = map.getPoints(); + + pcl::Indices tmp_indices; + std::vector tmp_dists; + std::vector path_prefix; + PointT extra_pt_buff; + int start_idx = -1; + + this->nodes.clear(); + + // 1. Filter out non-traversible points and build KDTree + this->points.points.clear(); + this->points.points.reserve(map_points.size()); + for (const PointT& pt : map_points) { - throw std::out_of_range("Start point is out of bounds"); + if (isWeighted(pt) && weight(pt) <= max_weight /*|| isFrontier(pt)*/) + { + this->points.points.emplace_back(pt); + } } - if (loc_cloud.points.size() != meta_cloud.points.size()) + this->points.width = this->points.points.size(); + this->points.height = 1; + this->points.is_dense = true; + + const auto points_ptr = util::wrapUnmanaged(this->points); + this->kdtree.setInputCloud(points_ptr); + + // 2. Snap goal pt to pointset if inside explored region but outside of + // discovery range. Since we removed all obstacle points, this changes + // the goal to be the nearest non-obstacle point in the map as long as it + // is in the explored region (in the case which the goal was + // non-traversible). + PointT goal_pt{goal.x(), goal.y(), goal.z()}; + const bool goal_is_explored = ue_space.isExplored(goal); + if (goal_is_explored && !this->kdtree.radiusSearch( + goal_pt, + this->goal_threshold, + tmp_indices, + tmp_dists, + 1)) { - throw std::invalid_argument( - "Location and meta clouds must have the same size"); + if (this->kdtree.nearestKSearch(goal_pt, 1, tmp_indices, tmp_dists)) + { + goal_pt = this->points[tmp_indices[0]]; + if (std::sqrt(tmp_dists[0]) > this->search_radius) + { + DEBUG_COUT( + "Warning - path planning goal node snapped to available" + " node with distance greater than search radius!"); + } + } + else + { + // no nearest point --> no points at all! + return false; + } } - if (loc_cloud.points.empty() || meta_cloud.points.empty()) + + // 3. Attempt to reuse previous path if provided + if (!path.empty()) { - throw std::invalid_argument("Point clouds are empty"); - } -#endif + // 3-A. Ignore initial segments that are no longer relevant + size_t start_i = 0; + for (; start_i + 1 < path.size(); start_i++) + { + const auto& prev = path[start_i]; + const auto& curr = path[start_i + 1]; - // temp buffers reused by radiusSearch - pcl::Indices tmp_indices; - std::vector tmp_dists; + Vec3f diff = curr - prev; + float proj = (diff.dot(start - prev)) / diff.squaredNorm(); - auto shared_loc_cloud = util::wrap_unmanaged(loc_cloud); - this->kdtree.setInputCloud(shared_loc_cloud); + if (proj < 1.f) + { + break; + } + } - PointT goal_pt; - goal_pt.getVector3fMap() = goal; - // handle goal point isn't reachable given goal thresh - if ((goal.array() >= local_bound_min.array()).all() && - (goal.array() < local_bound_max.array()).all()) - { - if (!this->kdtree.radiusSearch( - goal_pt, - this->goal_threshold, - tmp_indices, - tmp_dists, - 1)) + // 3-B. Verify remaining segments + pcl::Indices prev_nearest; + float path_len = 0.f; + float min_goal_dist = std::numeric_limits::infinity(); + size_t i = start_i; + size_t prev_i = start_i; + size_t checkpt_i = start_i; + size_t goal_trim_i = start_i; + while (i < path.size()) { - if (this->kdtree.nearestKSearch(goal_pt, 1, tmp_indices, tmp_dists)) + const auto& pt = path[i]; + + const float d_to_goal = (goal_pt.getVector3fMap() - pt).norm(); + if (d_to_goal < min_goal_dist) + { + goal_trim_i = i; + min_goal_dist = d_to_goal; + } + + tmp_indices.clear(); + if (!this->kdtree.nearestKSearch( + PointT{pt.x(), pt.y(), pt.z()}, + static_cast(this->verification_degree), + tmp_indices, + tmp_dists)) + { + // no nearest pts - bad keypoint + goto BREAK_L; + } + + for (size_t j = 0; j < tmp_indices.size(); j++) { - goal_pt = loc_cloud.points[tmp_indices[0]]; - if (std::sqrt(tmp_dists[0]) > this->search_radius) + // test if the current keypoint is still valid + if (std::sqrt(tmp_dists[j]) > this->search_radius) { - DEBUG_COUT( - "Warning - path planning goal node snapped to available" - " node with distance greater than search radius!"); + // bad keypoint + goto BREAK_L; + } + + // test if the prev-to-curr segment is still valid + // if prev is empty (init), this doesn't run (as required) + const auto& pt_a = this->points[tmp_indices[j]]; + for (const pcl::index_t k : prev_nearest) + { + const auto& pt_b = this->points[k]; + const float d = + (pt_a.getVector3fMap() - pt_b.getVector3fMap()).norm(); + if (d > this->search_radius) + { + // bad segment + goto BREAK_L; + } } } + + prev_nearest.swap(tmp_indices); + path_len += (pt - path[prev_i]).norm(); + if (checkpt_i == start_i && path_len >= this->verification_range) + { + checkpt_i = i; + } + prev_i = i; + i++; + continue; + + BREAK_L: + break; + } + + // 3-C. Analyze results + if (i >= path.size()) + { + // Verified entire path: extend if needed, otherwise exit + if ((goal_is_explored && min_goal_dist > this->goal_threshold) || + (!goal_is_explored && + ue_space.distToUnexplored(path.back(), this->boundary_radius) > + this->boundary_radius)) + { + // extend from path end + path_prefix.insert( + path_prefix.begin(), + path.begin() + start_i, + path.end() - 1); + + extra_pt_buff.getVector3fMap() = path.back(); + const Vec3f dir = + path.size() > 1 + ? (path.back() - path[path.size() - 2]).normalized() + : Vec3f::Zero(); + this->nodes.emplace_back( + extra_pt_buff, + dir, + 0.f, + this->distance_coeff * + (goal_pt.getVector3fMap() - path.back()).norm()); + start_idx = 0; + } else { - return false; + // trim unneeded keypoints + if (min_goal_dist <= this->goal_threshold) + { + path.erase(path.begin() + goal_trim_i + 1, path.end()); + } + path.erase(path.begin(), path.begin() + start_i); + return true; + } + } + else + { + // Error occurred somewhere: replan from checkpoint or from scratch + if (checkpt_i != start_i) + { + // replan from checkpt + path_prefix.insert( + path_prefix.begin(), + path.begin() + start_i, + path.begin() + checkpt_i); + + extra_pt_buff.getVector3fMap() = path[checkpt_i]; + const Vec3f dir = + checkpt_i > 0 + ? (path[checkpt_i] - path[checkpt_i - 1]).normalized() + : Vec3f::Zero(); + this->nodes.emplace_back( + extra_pt_buff, + dir, + 0.f, + this->distance_coeff * + (goal_pt.getVector3fMap() - path[checkpt_i]).norm()); + start_idx = 0; + } + else + { + // discard path and replan (continue as normal) } } } - this->nodes.clear(); - this->nodes.reserve(loc_cloud.points.size()); - - // Heuristic = lambda_d * Euclidean distance - for (size_t i = 0; i < loc_cloud.points.size(); ++i) + // 4. Construct nodes + const size_t num_prefix_nodes = this->nodes.size(); + this->nodes.reserve(num_prefix_nodes + this->points.size()); + for (const auto& pt : this->points) { - const auto& point = loc_cloud.points[i]; - const auto& meta = meta_cloud.points[i]; - float h = lambda_dist * - (goal_pt.getVector3fMap() - point.getVector3fMap()).norm(); - this->nodes.emplace_back(point, meta, h); + // compute h as proportional to straight-line goal distance + this->nodes.emplace_back( + pt, + this->distance_coeff * + (goal_pt.getVector3fMap() - pt.getVector3fMap()).norm()); } - // find start node - int start_idx; - if (this->kdtree.nearestKSearch( - PointT{start.x(), start.y(), start.z()}, - 1, - tmp_indices, - tmp_dists)) + // 5. Init start node if not already done (snap to pointset) + if (start_idx < 0) { - start_idx = tmp_indices[0]; - this->nodes[start_idx].g = 0.f; - } - else - { - return false; + if (this->kdtree.nearestKSearch( + PointT{start.x(), start.y(), start.z()}, + 1, + tmp_indices, + tmp_dists)) + { + start_idx = static_cast(num_prefix_nodes) + tmp_indices[0]; + this->nodes[start_idx].g = 0.f; + this->nodes[start_idx].dir.setZero(); + } + else + { + DEBUG_COUT( + "Error - could not snap start node to available points!"); + return false; + } } - // create open heap over f = g + h - DaryHeap open(static_cast(this->nodes.size())); + // 6. Setup + util::DAryHeap open(static_cast(this->nodes.size())); open.reserve_heap(this->nodes.size()); open.push(start_idx, this->nodes[start_idx].f()); - // closed set std::vector closed(this->nodes.size(), false); - // keep track of closest visited node to goal - int closest_idx = start_idx; - // h proportional to distance to goal - float closest_dist = this->nodes[start_idx].h; - bool found_boundary = false; - - const Box3f outside_boundary{ - local_bound_min + Vec3f::Constant(boundary_radius), - local_bound_max - Vec3f::Constant(boundary_radius)}; - - auto create_path = [&](Node& path_end) - { - path.clear(); - for (Node* n = &path_end; n != nullptr; n = n->parent) - { - path.push_back(n->position()); - } - std::reverse(path.begin(), path.end()); - }; + int closest_idx = -1; + float closest_dist = std::numeric_limits::infinity(); + // 7. A* body while (!open.empty()) { const int idx = open.pop(); @@ -224,104 +410,350 @@ bool PathPlanner::solvePath( if (closed[idx]) { - continue; // skip if already closed + continue; + } + else + { + closed[idx] = true; } - closed[idx] = true; - // Goal check (by geometry) - if ((current.position() - goal_pt.getVector3fMap()).norm() < - goal_threshold) + // if ((current.position() - goal_pt.getVector3fMap()).norm() < + // this->goal_threshold) + if (current.h < this->goal_threshold * this->distance_coeff) { - create_path(current); - return true; + closest_idx = idx; + break; } - // Lazily compute neighbors on first expansion if (current.neighbors.empty()) { - current.neighbors.clear(); tmp_dists.clear(); this->kdtree.radiusSearch( - current.trav_point, - search_radius, + current.point, + this->search_radius, current.neighbors, tmp_dists, - max_neighbors); + this->max_neighbors); } - // Relax neighbors - for (const int nb_idx : current.neighbors) + for (const pcl::index_t nb_idx_ : current.neighbors) { + const size_t nb_idx = + num_prefix_nodes + static_cast(nb_idx_); + Node& nb = this->nodes[nb_idx]; + if (closed[nb_idx]) { continue; } - else if (this->nodes[nb_idx].cost > 1.f) - { - closed[nb_idx] = true; - continue; - } - Node& nb = this->nodes[nb_idx]; - // geometric edge length - const float geom = (nb.position() - current.position()).norm(); + const Vec3f diff = (nb.position() - current.position()); + const float dist = diff.norm(); + const float inv_dot = (1.f - diff.dot(current.dir) / dist); + const float edge_cost = (this->distance_coeff * dist) + + (this->straightness_coeff * inv_dot) + + (this->traversibility_coeff * nb.cost()); + const float tentative_g = current.g + edge_cost; - const float edge = lambda_dist * geom + lambda_penalty * nb.cost; - - const float tentative_g = current.g + edge; if (tentative_g < nb.g) { + nb.dir = (diff / dist); nb.g = tentative_g; nb.parent = ¤t; - bool in_boundary = !outside_boundary.contains(nb.position()); - - if (!found_boundary && in_boundary) + if (!open.contains(nb_idx)) { - closest_dist = nb.h; - closest_idx = nb_idx; - found_boundary = true; + open.push(nb_idx, nb.f()); } - else if (nb.h < closest_dist) + else { - if (!found_boundary || in_boundary) - { - closest_dist = nb.h; - closest_idx = nb_idx; - } + open.decrease_key(nb_idx, nb.f()); } - const float new_f = nb.f(); - if (!open.contains(nb_idx)) + const bool is_frontier = + ue_space.distToUnexplored( + nb.position(), + this->boundary_radius) <= this->boundary_radius; + + // We already snapped the goal to the nearest point in the set + // as long as it was in explored range, thus if the goal is + // reachable, it will trigger the exit condition above + // (proximity). This implies that we should only track the + // closest frontier node - either the goal was outside explored + // range or there is an obstacle blocking it; either way we need + // to keep exploring (ie. traverse to a frontier point). If we + // can't reach any frontier nodes [and goal is not reached], + // then the function should return false, meaning there is no + // meaningful path to follow. Thus, the return state is only + // correct if we limit closest_idx to being a frontier node here! + if (is_frontier && nb.h < closest_dist) { - open.push(nb_idx, new_f); - } - else - { - open.decrease_key(nb_idx, new_f); // strictly better + closest_idx = nb_idx; + closest_dist = nb.h; } } } } - // No path to goal: pick closest visited node to the goal + // 8. Export if (closest_idx >= 0) { - create_path(this->nodes[closest_idx]); - - if (!found_boundary) + path.clear(); + for (Node* n = &this->nodes[closest_idx]; n != nullptr; n = n->parent) { - DEBUG_COUT( - "No boundary nodes reached, returning closest node to goal"); + path.push_back(n->position()); + } + if (!path_prefix.empty()) + { + path.insert(path.end(), path_prefix.rbegin(), path_prefix.rend()); } - return found_boundary; + std::reverse(path.begin(), path.end()); + + return true; } - // Nothing reachable at all - DEBUG_COUT("No reachable nodes; start may be isolated"); return false; } +// template +// bool PathPlanner

::solvePath( +// const Vec3f& start, +// const Vec3f& goal, +// const Vec3f& local_bound_min, +// const Vec3f& local_bound_max, +// const PointCloudT& trav_points, +// std::vector& path) +// { +// #if PATH_PLANNING_PEDANTIC +// if (start.x() < local_bound_min.x() || start.y() < local_bound_min.y() || +// start.x() > local_bound_max.x() || start.y() > local_bound_max.y()) +// { +// throw std::out_of_range("Start point is out of bounds"); +// } +// if (trav_points.points.empty()) +// { +// throw std::invalid_argument("Input point cloud are empty"); +// } +// #endif + +// // temp buffers reused by radiusSearch +// pcl::Indices tmp_indices; +// std::vector tmp_dists; + +// auto shared_trav_points = util::wrapUnmanaged(trav_points); +// this->kdtree.setInputCloud(shared_trav_points); + +// PointT goal_pt; +// goal_pt.getVector3fMap() = goal; +// // handle goal point isn't reachable given goal thresh +// if ((goal.array() >= local_bound_min.array()).all() && +// (goal.array() < local_bound_max.array()).all()) +// { +// if (!this->kdtree.radiusSearch( +// goal_pt, +// this->goal_threshold, +// tmp_indices, +// tmp_dists, +// 1)) +// { +// if (this->kdtree.nearestKSearch(goal_pt, 1, tmp_indices, tmp_dists)) +// { +// goal_pt = trav_points.points[tmp_indices[0]]; +// if (std::sqrt(tmp_dists[0]) > this->search_radius) +// { +// DEBUG_COUT( +// "Warning - path planning goal node snapped to available" +// " node with distance greater than search radius!"); +// } +// } +// else +// { +// return false; +// } +// } +// } + +// // check for unreachable destination or move goal to closest traversible point +// this->kdtree +// .radiusSearch(goal_pt, this->search_radius, tmp_indices, tmp_dists); +// bool all_invalid = true; +// for (pcl::index_t i : tmp_indices) +// { +// if (isNominal(trav_points.points[i])) +// { +// goal_pt = trav_points.points[i]; +// all_invalid = false; +// break; +// } +// } +// if (all_invalid) +// { +// DEBUG_COUT("Error - goal is unreachable!"); +// return false; +// } + +// this->nodes.clear(); +// this->nodes.reserve(trav_points.points.size()); + +// // Heuristic = lambda_d * Euclidean distance +// for (size_t i = 0; i < trav_points.points.size(); ++i) +// { +// const auto& point = trav_points.points[i]; +// float h = distance_coeff * +// (goal_pt.getVector3fMap() - point.getVector3fMap()).norm(); +// this->nodes.emplace_back(point, h); +// } + +// // find start node +// int start_i; +// if (this->kdtree.nearestKSearch( +// PointT{start.x(), start.y(), start.z()}, +// 1, +// tmp_indices, +// tmp_dists)) +// { +// start_i = tmp_indices[0]; +// this->nodes[start_i].g = 0.f; +// } +// else +// { +// DEBUG_COUT("Error - could not initialize starting location!"); +// return false; +// } + +// // create open heap over f = g + h +// util::DAryHeap open(static_cast(this->nodes.size())); +// open.reserve_heap(this->nodes.size()); +// open.push(start_i, this->nodes[start_i].f()); + +// // closed set +// std::vector closed(this->nodes.size(), false); + +// // keep track of closest visited node to goal +// int closest_idx = start_i; +// // h proportional to distance to goal +// float closest_dist = this->nodes[start_i].h; +// bool found_boundary = false; + +// const Box3f outside_boundary{ +// local_bound_min + Vec3f::Constant(boundary_radius), +// local_bound_max - Vec3f::Constant(boundary_radius)}; + +// auto create_path = [&](Node& path_end) +// { +// path.clear(); +// for (Node* n = &path_end; n != nullptr; n = n->parent) +// { +// path.push_back(n->position()); +// } +// std::reverse(path.begin(), path.end()); +// }; + +// while (!open.empty()) +// { +// const int idx = open.pop(); +// Node& current = this->nodes[idx]; + +// if (closed[idx]) +// { +// continue; // skip if already closed +// } +// closed[idx] = true; + +// // Goal check (by geometry) +// if ((current.position() - goal_pt.getVector3fMap()).norm() < +// goal_threshold) +// { +// create_path(current); +// return true; +// } + +// // Lazily compute neighbors on first expansion +// if (current.neighbors.empty()) +// { +// current.neighbors.clear(); +// tmp_dists.clear(); +// this->kdtree.radiusSearch( +// current.point, +// search_radius, +// current.neighbors, +// tmp_dists, +// max_neighbors); +// } + +// // Relax neighbors +// for (const int nb_idx : current.neighbors) +// { +// if (closed[nb_idx]) +// { +// continue; +// } +// else if (this->nodes[nb_idx].cost() > 1.f) +// { +// closed[nb_idx] = true; +// continue; +// } +// Node& nb = this->nodes[nb_idx]; + +// // geometric edge length +// const float geom = (nb.position() - current.position()).norm(); + +// const float edge = distance_coeff * geom + traversibility_coeff * nb.cost(); + +// const float tentative_g = current.g + edge; +// if (tentative_g < nb.g) +// { +// nb.g = tentative_g; +// nb.parent = ¤t; + +// bool in_boundary = !outside_boundary.contains(nb.position()); + +// if (!found_boundary && in_boundary) +// { +// closest_dist = nb.h; +// closest_idx = nb_idx; +// found_boundary = true; +// } +// else if (nb.h < closest_dist) +// { +// if (!found_boundary || in_boundary) +// { +// closest_dist = nb.h; +// closest_idx = nb_idx; +// } +// } + +// const float new_f = nb.f(); +// if (!open.contains(nb_idx)) +// { +// open.push(nb_idx, new_f); +// } +// else +// { +// open.decrease_key(nb_idx, new_f); // strictly better +// } +// } +// } +// } + +// // No path to goal: pick closest visited node to the goal +// if (closest_idx >= 0) +// { +// create_path(this->nodes[closest_idx]); + +// if (!found_boundary) +// { +// DEBUG_COUT( +// "No boundary nodes reached, returning closest node to goal"); +// } +// return found_boundary; +// } + +// // Nothing reachable at all +// DEBUG_COUT("No reachable nodes; start may be isolated"); +// return false; +// } + }; // namespace perception }; // namespace csm diff --git a/include/modules/impl/traversibility_gen_impl.hpp b/include/modules/impl/traversibility_gen_impl.hpp index c82b8b3..300a66c 100644 --- a/include/modules/impl/traversibility_gen_impl.hpp +++ b/include/modules/impl/traversibility_gen_impl.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -42,9 +42,13 @@ #include "../traversibility_gen.hpp" #include +#include #include +#include +#include + namespace csm { @@ -52,8 +56,8 @@ namespace perception { -template -void TraversibilityGenerator::configure( +template +void TraversibilityGenerator::configure( float normal_estimation_radius, float output_res, float grad_search_radius, @@ -62,6 +66,7 @@ void TraversibilityGenerator::configure( float avoidance_radius, float trav_score_curvature_weight, float trav_score_grad_weight, + int min_vox_cell_points, int interp_sample_count) { this->normal_estimation_radius = normal_estimation_radius; @@ -69,17 +74,18 @@ void TraversibilityGenerator::configure( this->grad_search_radius = grad_search_radius; this->min_grad_diff = min_grad_diff; this->non_trav_grad_thresh = - 1.f - std::cos(non_traversible_grad_angle * (M_PI / 180.)); + 1.f - std::cos(non_traversible_grad_angle * (std::numbers::pi / 180.)); this->avoidance_radius = avoidance_radius; this->avoid_radius_sqrd = avoidance_radius * avoidance_radius; this->trav_score_curvature_weight = trav_score_curvature_weight; this->trav_score_grad_weight = trav_score_grad_weight; + this->min_vox_cell_points = min_vox_cell_points; this->interp_sample_count = interp_sample_count; } -template -void TraversibilityGenerator::processMapPoints( - const TravPointCloud& map_points, +template +void TraversibilityGenerator::processMapPoints( + const MapPointCloud& map_points, const Vec3f& map_min_bound, const Vec3f& map_max_bound, const Vec3f& map_grav_vec, @@ -95,9 +101,9 @@ void TraversibilityGenerator::processMapPoints( } -template -typename TraversibilityGenerator::TravPointCloud& - TraversibilityGenerator::copyPoints(TravPointCloud& copy_cloud) const +template +typename TraversibilityGenerator::TravPointCloud& + TraversibilityGenerator::copyPoints(TravPointCloud& copy_cloud) const { this->mtx.lock(); copy_cloud = this->points; @@ -105,21 +111,10 @@ typename TraversibilityGenerator::TravPointCloud& return copy_cloud; } -template -typename TraversibilityGenerator::MetaDataList& - TraversibilityGenerator::copyMetaDataList( - MetaDataList& copy_list) const -{ - this->mtx.lock(); - copy_list = this->points_meta.points; - this->mtx.unlock(); - return copy_list; -} - -template -typename TraversibilityGenerator::TravPointCloud& - TraversibilityGenerator::swapPoints(TravPointCloud& swap_cloud) +template +typename TraversibilityGenerator::TravPointCloud& + TraversibilityGenerator::swapPoints(TravPointCloud& swap_cloud) { this->mtx.lock(); std::swap(this->points.points, swap_cloud.points); @@ -130,74 +125,45 @@ typename TraversibilityGenerator::TravPointCloud& return swap_cloud; } -template -typename TraversibilityGenerator::MetaDataList& - TraversibilityGenerator::swapMetaDataList(MetaDataList& swap_list) -{ - this->mtx.lock(); - std::swap(this->points_meta.points, swap_list); - this->mtx.unlock(); - return swap_list; -} - -template -void TraversibilityGenerator::extractTravElements( - TravPointCloud& trav_points, - MetaDataList& trav_meta_data) const +template +void TraversibilityGenerator::extractTravPoints( + TravPointCloud& trav_points) const { trav_points.clear(); - trav_meta_data.clear(); this->mtx.lock(); - util::pc_copy_selection(this->points, this->trav_selection, trav_points); - util::pc_copy_selection( - this->points_meta.points, - this->trav_selection, - trav_meta_data); + util::copySelection(this->points, this->trav_selection, trav_points); this->mtx.unlock(); } -template -void TraversibilityGenerator::extractExtTravElements( - TravPointCloud& ext_trav_points, - MetaDataList& ext_trav_meta_data) const + +template +void TraversibilityGenerator::extractExtTravPoints( + TravPointCloud& ext_trav_points) const { ext_trav_points.clear(); - ext_trav_meta_data.clear(); this->mtx.lock(); - util::pc_copy_selection( + util::copySelection( this->points, this->ext_trav_selection, ext_trav_points); - util::pc_copy_selection( - this->points_meta.points, - this->ext_trav_selection, - ext_trav_meta_data); this->mtx.unlock(); } -template -void TraversibilityGenerator::extractNonTravElements( - TravPointCloud& non_trav_points, - MetaDataList& non_trav_meta_data) const + +template +void TraversibilityGenerator::extractNonTravPoints( + TravPointCloud& non_trav_points) const { non_trav_points.clear(); - non_trav_meta_data.clear(); this->mtx.lock(); - util::pc_copy_selection( - this->points, - this->avoid_selection, - non_trav_points); - util::pc_copy_selection( - this->points_meta.points, - this->avoid_selection, - non_trav_meta_data); + util::copySelection(this->points, this->avoid_selection, non_trav_points); this->mtx.unlock(); } -template -pcl::Indices& TraversibilityGenerator::swapTravIndices( +template +pcl::Indices& TraversibilityGenerator::swapTravIndices( pcl::Indices& swap_indices) { this->mtx.lock(); @@ -206,8 +172,8 @@ pcl::Indices& TraversibilityGenerator::swapTravIndices( return swap_indices; } -template -pcl::Indices& TraversibilityGenerator::swapExtTravIndices( +template +pcl::Indices& TraversibilityGenerator::swapExtTravIndices( pcl::Indices& swap_indices) { this->mtx.lock(); @@ -216,8 +182,8 @@ pcl::Indices& TraversibilityGenerator::swapExtTravIndices( return swap_indices; } -template -pcl::Indices& TraversibilityGenerator::swapNonTravIndices( +template +pcl::Indices& TraversibilityGenerator::swapNonTravIndices( pcl::Indices& swap_indices) { this->mtx.lock(); @@ -229,19 +195,20 @@ pcl::Indices& TraversibilityGenerator::swapNonTravIndices( -template -void TraversibilityGenerator::process( - const TravPointCloud& map_points, +template +void TraversibilityGenerator::process( + const MapPointCloud& map_points, const Vec3f& map_min_bound, const Vec3f& map_max_bound, const Vec3f& map_grav_vec, const Vec3f& source_pos) { + using namespace csm::perception::traversibility; + pcl::Indices cell_indices_buff, nearest_indices_buff; std::vector dists_sqrd_buff; this->points.clear(); - this->points_meta.clear(); this->trav_selection.clear(); this->ext_trav_selection.clear(); this->avoid_selection.clear(); @@ -291,29 +258,27 @@ void TraversibilityGenerator::process( } Vec4f centroid; - if (cell_indices_buff.size() >= 3 && + if (cell_indices_buff.size() >= + static_cast(this->min_vox_cell_points) && pcl::compute3DCentroid(map_points, cell_indices_buff, centroid)) { this->points.points.emplace_back().getVector3fMap() = centroid.template head<3>(); + weight(this->points.points.back()) = NOMINAL_MIN_WEIGHT; } } this->points.height = this->points.points.size(); this->points.width = 1; - this->points_meta.points.resize( - this->points.points.size(), - TRAV_MIN_WEIGHT); // 4. REBUILD KDTREE - this->points_ptr = util::wrap_unmanaged(this->points); + this->points_ptr = util::wrapUnmanaged(this->points); this->interp_search_tree.setInputCloud(this->points_ptr); // 5. FILTER NON-TRAV POINTS for (size_t i = 0; i < this->points.size(); i++) { auto& pt = this->points.points[i]; - auto& meta = this->points_meta.points[i]; this->interp_search_tree.radiusSearch( pt, @@ -340,7 +305,7 @@ void TraversibilityGenerator::process( std::abs((max - min).normalized().dot(map_grav_vec)) > this->non_trav_grad_thresh) { - trav_weight(meta) = NON_TRAV_WEIGHT; + weight(pt) = OBSTACLE_MARKER_VAL; this->avoid_selection.push_back(i); } else @@ -371,7 +336,7 @@ void TraversibilityGenerator::process( const Vec2f origin_cell_center = this->vox_grid.getCellCenter2(Vec2i::Constant(0)); size_t cell_i = 0; - PointT center_pt{0.f, 0.f, source_pos.z()}; + TravPointT center_pt{0.f, 0.f, source_pos.z()}; for (center_pt.y = origin_cell_center.y(); // center_pt.y < map_max_bound.y(); center_pt.y += this->output_res) @@ -391,8 +356,7 @@ void TraversibilityGenerator::process( size_t samples = 0; for (pcl::index_t idx : nearest_indices_buff) { - if (trav_weight(this->points_meta.points[idx]) > - TRAV_MIN_WEIGHT) + if (isObstacle(this->points.points[idx])) { continue; } @@ -405,12 +369,11 @@ void TraversibilityGenerator::process( { this->trav_selection.push_back(this->points.points.size()); auto& interp_pt = this->points.points.emplace_back(); - auto& interp_meta = this->points_meta.points.emplace_back(); interp_pt.x = center_pt.x; interp_pt.y = center_pt.y; interp_pt.z = sum_z / samples; - trav_weight(interp_meta) = TRAV_MIN_WEIGHT; + weight(interp_pt) = NOMINAL_MIN_WEIGHT; } } cell_i++; @@ -419,13 +382,11 @@ void TraversibilityGenerator::process( this->points.height = this->points.points.size(); this->points.width = 1; - this->points_meta.height = this->points_meta.points.size(); - this->points_meta.width = 1; // 8. REBUILD TRAV KDTREE this->trav_search_tree.setInputCloud( this->points_ptr, - util::wrap_unmanaged(this->trav_selection)); + util::wrapUnmanaged(this->trav_selection)); // 9. LOOP AVOID POINTS, APPLY SPREAD RADIUS for (pcl::index_t avoid_i : this->avoid_selection) @@ -439,13 +400,11 @@ void TraversibilityGenerator::process( for (size_t i = 0; i < nearest_indices_buff.size(); i++) { const auto idx = nearest_indices_buff[i]; - const float weight = - TRAV_MAX_WEIGHT + - (EXT_TRAV_MAX_WEIGHT - TRAV_MAX_WEIGHT) * - (1.f - (dists_sqrd_buff[i] / this->avoid_radius_sqrd)); - if (weight > trav_weight(this->points_meta.points[idx])) + const auto w = extendedWeight( + 1.f - (dists_sqrd_buff[i] / this->avoid_radius_sqrd)); + if (w > weight(this->points.points[idx])) { - trav_weight(this->points_meta.points[idx]) = weight; + weight(this->points.points[idx]) = w; } } } @@ -454,10 +413,9 @@ void TraversibilityGenerator::process( for (size_t i = 0; i < this->trav_selection.size(); i++) { const auto& pt_idx = this->trav_selection[i]; - const auto& pt = this->points.points[pt_idx]; - auto& pt_meta = this->points_meta.points[pt_idx]; + auto& pt = this->points.points[pt_idx]; - if (trav_weight(pt_meta) > TRAV_MAX_WEIGHT) + if (isExtended(pt)) { this->ext_trav_selection.push_back(pt_idx); this->trav_selection[i] = this->trav_selection.back(); @@ -473,17 +431,18 @@ void TraversibilityGenerator::process( dists_sqrd_buff); Vec4f plane; + float curvature; pcl::computePointNormal( this->points, nearest_indices_buff, plane, - pt_meta.curvature); + curvature); + + const float w = (curvature * this->trav_score_curvature_weight) + + (1.f - map_grav_vec.dot(plane.template head<3>())) * + this->trav_score_grad_weight; - pt_meta.getNormalVector3fMap() = plane.template head<3>(); - trav_weight(pt_meta) = - (pt_meta.curvature * this->trav_score_curvature_weight) + - (1.f - map_grav_vec.dot(pt_meta.getNormalVector3fMap())) * - this->trav_score_grad_weight; + weight(pt) = nominalWeight(std::min(1.f, w)); } } } diff --git a/include/imu_integrator.hpp b/include/modules/imu_integrator.hpp similarity index 78% rename from include/imu_integrator.hpp rename to include/modules/imu_integrator.hpp index e5a05df..189dd16 100644 --- a/include/imu_integrator.hpp +++ b/include/modules/imu_integrator.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -47,8 +47,9 @@ #include -#include "tsq.hpp" -#include "geometry.hpp" +#include +#include +#include namespace csm @@ -56,8 +57,9 @@ namespace csm namespace perception { -/* Manages IMU sensor samples, providing convenience functions for looking up the delta - * rotation between timestamps, applying gyro/acceleration biases, and computing the gravity vector. */ +/* Manages IMU sensor samples, providing convenience functions for looking + * up the delta rotation between timestamps, applying gyro/acceleration biases, + * and computing the gravity vector. */ template class ImuIntegrator { @@ -82,6 +84,7 @@ class ImuIntegrator public: void addSample(const ImuMsg& imu); void trimSamples(TimeFloatT trim_ts); + void setAutoTrimWindow(TimeFloatT window_s = 0.f); bool recalibrate(TimeFloatT dt, bool force = false); Vec3 estimateGravity( @@ -119,6 +122,7 @@ class ImuIntegrator std::atomic use_orientation; const double calib_time; + double trim_window{600.0}; }; @@ -154,10 +158,13 @@ void ImuIntegrator::addSample(const ImuMsg& imu) stamp, q); - // 10 min max, orientation is likely still valid but at this point we probably have worse problems - util::tsq::trimToStamp( - this->orient_buffer, - (util::tsq::newestStamp(this->orient_buffer) - 600.)); + if (this->trim_window > 0) + { + util::tsq::trimToStamp( + this->orient_buffer, + (util::tsq::newestStamp(this->orient_buffer) - + this->trim_window)); + } } { @@ -177,22 +184,32 @@ void ImuIntegrator::addSample(const ImuMsg& imu) this->recalibrateRange(0, this->raw_buffer.size()); } - // 5 min max, integration is definitely deviated after this for most imus - util::tsq::trimToStamp( - this->raw_buffer, - (util::tsq::newestStamp(this->raw_buffer) - 300.)); + if (this->trim_window > 0) + { + util::tsq::trimToStamp( + this->raw_buffer, + (util::tsq::newestStamp(this->raw_buffer) - this->trim_window)); + } } } template void ImuIntegrator::trimSamples(TimeFloatT trim_ts) { - std::unique_lock{this->mtx}; + std::unique_lock imu_lock{this->mtx}; util::tsq::trimToStamp(this->orient_buffer, trim_ts); util::tsq::trimToStamp(this->raw_buffer, trim_ts); } +template +void ImuIntegrator::setAutoTrimWindow(TimeFloatT window_s) +{ + std::unique_lock imu_lock{this->mtx}; + + this->trim_window = window_s; +} + template bool ImuIntegrator::recalibrate(TimeFloatT dt, bool force) { @@ -263,6 +280,8 @@ typename ImuIntegrator::Quat ImuIntegrator::getDelta( TimeFloatT start, TimeFloatT end) const { + using namespace util::geom::cvt::ops; + Quat q = Quat::Identity(); std::unique_lock imu_lock{this->mtx}; @@ -280,17 +299,17 @@ typename ImuIntegrator::Quat ImuIntegrator::getDelta( newest--; } - if (newest != oldest) + if (newest < oldest) { const auto& a = this->orient_buffer[oldest]; const auto& b = this->orient_buffer[oldest - 1]; const auto& c = this->orient_buffer[newest + 1]; const auto& d = this->orient_buffer[newest]; - Quat prev = a.second.slerp( + const Quat prev = a.second.slerp( (start - a.first) / (b.first - a.first), b.second); - Quat curr = + const Quat curr = c.second.slerp((end - c.first) / (d.first - c.first), d.second); q = prev.inverse() * curr; @@ -298,54 +317,71 @@ typename ImuIntegrator::Quat ImuIntegrator::getDelta( } else { - assert(!"IMU angular velocity integration is not implemented!"); -#if 0 // TODO - const size_t - init_idx = util::tsq::binarySearchIdx(this->imu_buffer, start), - end_idx = util::tsq::binarySearchIdx(this->imu_buffer, end); - - // Relative IMU integration of gyro and accelerometer - double curr_imu_stamp = 0.; - double prev_imu_stamp = 0.; - double dt; - - for(size_t i = init_idx; i >= end_idx; i--) + size_t oldest = util::tsq::binarySearchIdx(this->raw_buffer, start); + size_t newest = util::tsq::binarySearchIdx(this->raw_buffer, end); + + if (oldest == this->raw_buffer.size() && oldest > 0) + { + oldest--; + } + if (newest > 0) { - const auto& imu_sample = this->raw_buffer[i]; + newest--; + } - if(prev_imu_stamp == 0.) + if (newest < oldest) + { + const auto& a = this->raw_buffer[oldest]; + const auto& b = this->raw_buffer[oldest - 1]; + const auto& c = this->raw_buffer[newest + 1]; + const auto& d = this->raw_buffer[newest]; + + const Vec3 head = + a.second.ang_vel + (b.second.ang_vel - a.second.ang_vel) * + (start - a.first) / (b.first - a.first); + const Vec3 tail = + c.second.ang_vel + (d.second.ang_vel - c.second.ang_vel) * + (end - c.first) / (d.first - c.first); + + for (size_t i = oldest; i > newest; i--) { - prev_imu_stamp = imu_sample.first; - continue; + const Vec3 *from, *to; + double from_t, to_t; + if (i == oldest) + { + from = &head; + from_t = start; + } + else + { + from = &(this->raw_buffer[i].second.ang_vel); + from_t = this->raw_buffer[i].first; + } + + if (i - 1 == newest) + { + to = &tail; + to_t = end; + } + else + { + to = &(this->raw_buffer[i - 1].second.ang_vel); + to_t = this->raw_buffer[i - 1].first; + } + + const Vec3 avg_w = (*from + *to) * 0.5f; + const FloatT dt = static_cast(to_t - from_t); + const Vec3 theta = avg_w * dt; + const FloatT angle = theta.norm(); + + if (angle > 1e-8) + { + const Vec3 axis = theta / angle; + q = (q * Quat{Eigen::AngleAxis(angle, axis)}) + .normalized(); + } } - - // Calculate difference in imu measurement times IN SECONDS - curr_imu_stamp = imu_sample.first; - dt = curr_imu_stamp - prev_imu_stamp; - prev_imu_stamp = curr_imu_stamp; - - // Relative gyro propagation quaternion dynamics - Quatd qq = q; - q.w() -= 0.5 * dt * - (qq.x() * imu_sample.second.ang_vel.x() - + qq.y() * imu_sample.second.ang_vel.y() - + qq.z() * imu_sample.second.ang_vel.z() ); - q.x() += 0.5 * dt * - (qq.w() * imu_sample.second.ang_vel.x() - - qq.z() * imu_sample.second.ang_vel.y() - + qq.y() * imu_sample.second.ang_vel.z() ); - q.y() += 0.5 * dt * - (qq.z() * imu_sample.second.ang_vel.x() - + qq.w() * imu_sample.second.ang_vel.y() - - qq.x() * imu_sample.second.ang_vel.z() ); - q.z() += 0.5 * dt * - (qq.x() * imu_sample.second.ang_vel.y() - - qq.y() * imu_sample.second.ang_vel.x() - + qq.w() * imu_sample.second.ang_vel.z() ); } - - q.normalize(); -#endif } return q; diff --git a/include/modules/kfc_map.hpp b/include/modules/kfc_map.hpp index ca24f2e..67c88d5 100644 --- a/include/modules/kfc_map.hpp +++ b/include/modules/kfc_map.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -49,13 +49,8 @@ #include #include -#include "point_def.hpp" #include "map_octree.hpp" -#ifndef KFC_MAP_STORE_INSTANCE_BUFFERS - #define KFC_MAP_STORE_INSTANCE_BUFFERS 1 -#endif - namespace csm { @@ -90,21 +85,26 @@ enum /** KDTree Frustum Collision (KFC) mapping implementation */ template< - typename PointT, - typename MapT = MapOctree, - typename CollisionPointT = pcl::PointXYZLNormal> + typename Point_T, + typename Map_T = MapOctree> class KFCMap { - static_assert(MapT::HAS_POINT_NORMALS); - static_assert( - pcl::traits::has_normal::value && - pcl::traits::has_curvature::value && - pcl::traits::has_label::value); + static_assert(pcl::traits::has_xyz::value); + static_assert(Map_T::HAS_POINT_NORMALS); + + using PointT = Point_T; + using MapT = Map_T; + using PointCloudT = pcl::PointCloud; + + using CollisionPointT = pcl::PointXYZLNormal; + using CollisionPointCloudT = pcl::PointCloud; using Arr3f = Eigen::Array3f; using Vec3f = Eigen::Vector3f; public: +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" struct UpdateResult { union @@ -116,6 +116,7 @@ class KFCMap uint32_t points_deleted; }; }; +#pragma GCC diagnostic pop inline UpdateResult(uint64_t v = 0) : data{v} {} inline operator uint64_t() const { return this->data; } @@ -146,7 +147,8 @@ class KFCMap * from the map. * @param add_max_range The maximum range for points that can be added to * the map. - * @param voxel_res The octree resolution. Values <= 0 result in no change. + * @param voxel_res The octree resolution. Values <= 0 result in no change. + * Note that if this is updated, the internal map gets cleared. */ void applyParams( double frustum_search_radius, @@ -162,7 +164,7 @@ class KFCMap template inline UpdateResult updateMap( const Vec3f& origin, - const pcl::PointCloud& pts, + const PointCloudT& pts, const pcl::Indices* indices = nullptr) { return this @@ -175,7 +177,7 @@ class KFCMap typename RayDirT = pcl::Axis> inline UpdateResult updateMap( const Vec3f& origin, - const pcl::PointCloud& pts, + const PointCloudT& pts, const std::vector& inf_rays, const pcl::Indices* pt_indices = nullptr) { @@ -188,29 +190,27 @@ class KFCMap pt_indices); } - inline typename pcl::PointCloud::ConstPtr getPoints() const + inline const PointCloudT& getPoints() const { - return this->map_octree.getInputCloud(); + return this->map_octree.points(); } - - inline const MapT& getMap() const { return this->map_octree; } - inline size_t numPoints() const { - return this->map_octree.getInputCloud()->size(); + return this->map_octree.points().size(); } + inline const MapT& getMap() const { return this->map_octree; } protected: template UpdateResult updateMap( const Vec3f& origin, - const pcl::PointCloud& pts, + const PointCloudT& pts, const std::vector* inf_rays, const pcl::Indices* pt_indices); protected: pcl::KdTreeFLANN collision_kdtree; - typename pcl::PointCloud::Ptr submap_ranges{nullptr}; + typename CollisionPointCloudT::Ptr submap_ranges{nullptr}; MapT map_octree; Arr3f bounds_min = Arr3f::Constant(-std::numeric_limits::infinity()); @@ -218,15 +218,6 @@ class KFCMap std::mutex mtx; -#if KFC_MAP_STORE_INSTANCE_BUFFERS - struct - { - pcl::Indices search_indices, points_to_add; - std::vector dists; - std::set submap_remove_indices; - } buff; -#endif - double frustum_search_radius{0.01}; double radial_dist_thresh{0.01}; double half_surface_width{0.01}; @@ -247,32 +238,21 @@ class KFCMap #include "impl/kfc_map_impl.hpp" // clang-format off -#define KFC_MAP_INSTANTIATE_CLASS_TEMPLATE( \ - POINT_TYPE, \ - MAP_TYPE, \ - COLL_TYPE) \ - template class csm::perception::KFCMap; - -#define KFC_MAP_INSTANTIATE_UPDATE_FUNC_TEMPLATE( \ - POINT_TYPE, \ - MAP_TYPE, \ - COLL_TYPE, \ - COLL_PARAMS, \ - RAY_TYPE) \ - template csm::perception::KFCMap:: \ - UpdateResult \ - csm::perception::KFCMap:: \ - updateMap( \ - const Eigen::Vector3f&, \ - const pcl::PointCloud&, \ - const std::vector*, \ +#define KFC_MAP_INSTANTIATE_CLASS_TEMPLATE(POINT_TYPE, MAP_TYPE) \ + template class csm::perception::KFCMap; + +#define KFC_MAP_INSTANTIATE_UPDATE_FUNC_TEMPLATE( \ + POINT_TYPE, \ + MAP_TYPE, \ + COLL_PARAMS, \ + RAY_TYPE) \ + template csm::perception::KFCMap::UpdateResult \ + csm::perception::KFCMap:: \ + updateMap( \ + const Eigen::Vector3f&, \ + const pcl::PointCloud&, \ + const std::vector*, \ const pcl::Indices*); - -#define KFC_MAP_INSTANTIATE_PCL_DEPENDENCIES( \ - POINT_TYPE, \ - MAP_TYPE, \ - COLL_TYPE) \ - template class pcl::KdTreeFLANN; // clang-format on #endif diff --git a/include/modules/lf_detector.hpp b/include/modules/lf_detector.hpp index 6f71e4d..ec13e6f 100644 --- a/include/modules/lf_detector.hpp +++ b/include/modules/lf_detector.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -39,8 +39,6 @@ #pragma once -#include "point_def.hpp" - #include #include #include @@ -56,7 +54,8 @@ #include #include -#include "geometry.hpp" +#include +#include #ifndef LFD_USE_ORTHO_PLANE_INTERSECTION #define LFD_USE_ORTHO_PLANE_INTERSECTION 1 @@ -102,6 +101,8 @@ class LidarFiducialDetector using Quatf = Eigen::Quaternionf; public: +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" struct DetectionStatus { union @@ -130,6 +131,8 @@ class LidarFiducialDetector } }; +#pragma GCC diagnostic pop + public: LidarFiducialDetector(); ~LidarFiducialDetector() = default; diff --git a/include/modules/lidar_odom.hpp b/include/modules/lidar_odom.hpp index df22922..5abe304 100644 --- a/include/modules/lidar_odom.hpp +++ b/include/modules/lidar_odom.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -39,8 +39,6 @@ #pragma once -#include - #include #include #include @@ -62,9 +60,9 @@ #include -#include -#include -#include +#include +#include +#include namespace csm @@ -76,7 +74,7 @@ namespace perception * optional IMU initialization. The core algorithm is formally known as * Direct Lidar Odometry - aka 'DLO' (see lisence) but has been * heavily modified. */ -template +template class LidarOdometry { using PointT = Point_T; @@ -153,7 +151,7 @@ class LidarOdometry void initializeInputTarget(); void setInputSources(); - void getNextPose(const std::optional& align_estimate = std::nullopt); + bool getNextPose(const std::optional& align_estimate = std::nullopt); void propagateS2S(const Mat4f& T); void propagateS2M(); @@ -213,7 +211,7 @@ class LidarOdometry Mat4f T{Mat4f::Identity()}; Mat4f T_s2s{Mat4f::Identity()}; - Mat4f T_s2s_prev{Mat4f::Identity()}; + Mat4f T_prev{Mat4f::Identity()}; std::mutex mtx; } state; diff --git a/include/modules/map_octree.hpp b/include/modules/map_octree.hpp index 387d6c6..99b54f3 100644 --- a/include/modules/map_octree.hpp +++ b/include/modules/map_octree.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -53,7 +53,7 @@ #include #include -#include "point_def.hpp" +#include namespace csm @@ -99,6 +99,10 @@ enum class MapOctreeBase { +private: + class Empty1 {}; + class Empty2 {}; + private: class StampStorageBase { @@ -106,8 +110,8 @@ class MapOctreeBase using StampT = uint64_t; public: - const std::vector& pointStamps() const; - uint64_t& pointStamp(pcl::index_t pt_idx); + const std::vector& pointStamps() const; + StampT& pointStamp(pcl::index_t pt_idx); protected: std::vector pt_stamps; @@ -133,35 +137,38 @@ class MapOctreeBase template< - typename PointT, + typename Point_T, int ConfigV = MAP_OCTREE_DEFAULT, - typename ChildT = void> + typename Child_T = void> class MapOctree : - public pcl::octree::OctreePointCloudSearch, + public pcl::octree::OctreePointCloudSearch, public std::conditional< (ConfigV & MAP_OCTREE_STORE_STAMPS), MapOctreeBase::StampStorageBase, - MapOctreeBase>::type, + MapOctreeBase::Empty1>::type, public std::conditional< (ConfigV & MAP_OCTREE_STORE_NORMALS), MapOctreeBase::NormalStorageBase, - MapOctreeBase>::type + MapOctreeBase::Empty2>::type { - static_assert(pcl::traits::has_xyz::value); + static_assert(pcl::traits::has_xyz::value); + + using PointT = Point_T; + using ChildT = Child_T; - using Super_T = pcl::octree::OctreePointCloudSearch; - using LeafContainer_T = typename Super_T::OctreeT::Base::LeafContainer; - using Derived_T = typename std::conditional< + using SuperT = pcl::octree::OctreePointCloudSearch; + using LeafContainerT = typename SuperT::OctreeT::Base::LeafContainer; + using DerivedT = typename std::conditional< std::is_same::value, MapOctree, ChildT>::type; - using typename Super_T::IndicesPtr; - using typename Super_T::IndicesConstPtr; + using PointCloudT = typename SuperT::PointCloud; - using typename Super_T::PointCloud; - using typename Super_T::PointCloudPtr; - using typename Super_T::PointCloudConstPtr; + using typename SuperT::IndicesPtr; + using typename SuperT::IndicesConstPtr; + using typename SuperT::PointCloudPtr; + using typename SuperT::PointCloudConstPtr; using Vec3f = Eigen::Vector3f; using Vec4f = Eigen::Vector4f; @@ -175,7 +182,7 @@ class MapOctree : (ConfigV & MAP_OCTREE_STORE_NORMALS); public: - MapOctree(const double voxel_res); + MapOctree(double voxel_res); MapOctree(const MapOctree&) = delete; ~MapOctree() = default; @@ -188,12 +195,21 @@ class MapOctree : } // TODO: invalidate other pcl::octree::OctreePointCloud point insertion methods + void clear(); + /* Note that this also clears the map */ + void reconfigure(double voxel_res); + + const PointCloudT& points() const; + PointT& pointAt(pcl::index_t i); + + size_t octreeSize() const; + size_t addPoint( const PointT& pt, uint64_t stamp = 0, bool compute_normal = true); void addPoints( - const pcl::PointCloud& pts, + const PointCloudT& pts, const pcl::Indices* indices = nullptr); void deletePoint(const pcl::index_t pt_idx, bool trim_nodes = false); void deletePoints(const pcl::Indices& indices, bool trim_nodes = false); @@ -207,14 +223,14 @@ class MapOctree : void computePointNormal(size_t idx); - LeafContainer_T* getOctreePoint( + LeafContainerT* getOctreePoint( const PointT& pt, pcl::octree::OctreeKey& key); - LeafContainer_T* getOrCreateOctreePoint( + LeafContainerT* getOrCreateOctreePoint( const PointT& pt, pcl::octree::OctreeKey& key); - typename Super_T::PointCloudPtr cloud_buff; + PointCloudPtr cloud_buff; std::vector hole_indices; }; diff --git a/include/modules/path_plan_map.hpp b/include/modules/path_plan_map.hpp new file mode 100644 index 0000000..8b7bf29 --- /dev/null +++ b/include/modules/path_plan_map.hpp @@ -0,0 +1,237 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include + +#include +#include + +#include +#include + +#include +#include + +#include "ue_octree.hpp" +#include "map_octree.hpp" + + +namespace csm +{ +namespace perception +{ + +template +class PathPlanMap +{ + static_assert(util::traits::supports_traversibility::value); + +public: + using PointT = Point_T; + using PointCloudT = pcl::PointCloud; + + using UEOctreeT = UEOctree; + using MapOctreeT = MapOctree; + + using Vec3f = Eigen::Vector3f; + using Arr3f = Eigen::Array3f; + +public: + PathPlanMap(float voxel_size = 0.1f); + PathPlanMap(const PathPlanMap&) = delete; + ~PathPlanMap() = default; + +public: + void clear(); + void reconfigure( + float obstacle_merge_window, + float voxel_size = -1.f); + + void append( + const PointCloudT& pts, + const Vec3f& bound_min, + const Vec3f& bound_max); + + void crop(const Vec3f& min, const Vec3f& max); + + const PointCloudT& getPoints() const; + const UEOctreeT& getUESpace() const; + const MapOctreeT& getMap() const; + +protected: + UEOctreeT ue_octree; + MapOctreeT map_octree; + + float obstacle_merge_window{0.5f}; +}; + + + +// --- Implementation ---------------------------------------------------------- + +template +PathPlanMap

::PathPlanMap(float vox_sz) : + ue_octree{vox_sz}, + map_octree{vox_sz} +{ +} + +template +void PathPlanMap

::clear() +{ + this->ue_octree.clear(); + this->map_octree.clear(); +} + +template +void PathPlanMap

::reconfigure( + float obstacle_merge_window, + float voxel_size) +{ + this->obstacle_merge_window = obstacle_merge_window; + + if (voxel_size > 0.f) + { + this->ue_octree.reconfigure(voxel_size); + this->map_octree.reconfigure(voxel_size); + } +} + +template +void PathPlanMap

::append( + const PointCloudT& pts, + const Vec3f& bound_min, + const Vec3f& bound_max) +{ + // TODO: frame-to-frame "bounds alignment" for more condistent map + + using namespace csm::perception::traversibility; + + const float res = static_cast(this->map_octree.getResolution()); + + pcl::Indices tmp_indices; + std::vector tmp_dists; + typename PointCloudT::VectorType merge_pts; + + const auto& map_pts_vec = this->map_octree.points().points; + + // 1. If the update region is large enough to have space that doesn't + // intersect the merge window, then remove it first. + if (((bound_max.array() - bound_min.array()) > + Arr3f::Constant(this->obstacle_merge_window * 2.f)) + .all()) + { + Vec3f inner_min = + bound_min + Vec3f::Constant(this->obstacle_merge_window); + Vec3f inner_max = + bound_max - Vec3f::Constant(this->obstacle_merge_window); + + this->map_octree.boxSearch(inner_min, inner_max, tmp_indices); + this->map_octree.deletePoints(tmp_indices, false); + tmp_indices.clear(); + } + + // 2. Copy out merge-window points, then remove from map + this->map_octree.boxSearch( + bound_min + Vec3f::Constant(res), + bound_max - Vec3f::Constant(res), + tmp_indices); + merge_pts.reserve(tmp_indices.size()); + util::copySelection(map_pts_vec, tmp_indices, merge_pts); + this->map_octree.deletePoints(tmp_indices, false); + tmp_indices.clear(); + + // 3. Insert new points + this->map_octree.addPoints(pts); + + // TODO: fix weird artifacts when doing this: + // 4. Update trav scores for old-new intersecting voxels (in window) + // for (const auto& pt : merge_pts) + // { + // if ((isWeighted(pt) || isObstacle(pt)) && + // this->map_octree.radiusSearch(pt, res * 0.5f, tmp_indices, tmp_dists)) + // { + // for(const pcl::index_t i : tmp_indices) + // { + // PointT& new_pt = this->map_octree.pointAt(i); + // if (weight(pt) > weight(new_pt)) + // { + // weight(new_pt) = weight(pt); + // } + // } + // } + // tmp_indices.clear(); + // } + + // 5. Update u-e octree, insert new frontier points + this->ue_octree.addExploredSpace(bound_min, bound_max); + + // 6. Optimize point layout + this->map_octree.optimizeStorage(); +} + +template +void PathPlanMap

::crop(const Vec3f& min, const Vec3f& max) +{ + this->ue_octree.crop(min, max); + this->map_octree.crop(min, max); +} + +template +const typename PathPlanMap

::PointCloudT& PathPlanMap

::getPoints() const +{ + return this->map_octree.points(); +} + +template +const typename PathPlanMap

::UEOctreeT& PathPlanMap

::getUESpace() const +{ + return this->ue_octree; +} + +template +const typename PathPlanMap

::MapOctreeT& PathPlanMap

::getMap() const +{ + return this->map_octree; +} + +}; // namespace perception +}; // namespace csm diff --git a/include/modules/path_planner.hpp b/include/modules/path_planner.hpp index 6a3fceb..8a46627 100644 --- a/include/modules/path_planner.hpp +++ b/include/modules/path_planner.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -47,12 +47,11 @@ #include #include -#include "util.hpp" -#include "point_def.hpp" +#include + +#include "path_plan_map.hpp" + -#ifndef PATH_PLANNING_PEDANTIC - #define PATH_PLANNING_PEDANTIC 0 -#endif #ifndef PPLAN_PRINT_DEBUG #define PPLAN_PRINT_DEBUG 0 #endif @@ -62,42 +61,23 @@ namespace csm namespace perception { -template< - typename Point_T = pcl::PointXYZ, - typename MetaPoint_T = csm::perception::NormalTraversal> +template class PathPlanner { - static_assert(util::traits::has_trav_weight::value); + static_assert( + pcl::traits::has_xyz::value && + util::traits::supports_traversibility::value); public: using PointT = Point_T; - using MetaPointT = MetaPoint_T; using PointCloudT = pcl::PointCloud; - using MetaCloudT = pcl::PointCloud; + using PathPlanMapT = PathPlanMap; + + using WeightT = traversibility::weight_t; using Vec3f = Eigen::Vector3f; using Box3f = Eigen::AlignedBox3f; -private: - struct Node - { - const PointT& trav_point; - float cost; // traversal cost of this node only - float g; // cost from start to this node - float h; // heuristic cost to goal - Node* parent = nullptr; - pcl::Indices neighbors; - - Node( - const PointT& point, - const MetaPointT& meta, - float h = 0.0f, - Node* p = nullptr); - - inline float f() const { return g + h; } // total cost - inline auto position() const { return trav_point.getVector3fMap(); } - }; - public: PathPlanner(); ~PathPlanner() = default; @@ -106,20 +86,60 @@ class PathPlanner float boundary_radius, float goal_threshold, float search_radius, - float lambda_dist, - float lambda_penalty, + float distance_coeff, + float straightness_coeff, + float traversibility_coeff, + float verification_range, + size_t verification_degree, size_t max_neighbors = 10); + // bool solvePath( + // std::vector& path, + // const Vec3f& start, + // const Vec3f& goal, + // const Vec3f& local_bound_min, + // const Vec3f& local_bound_max, + // const PointCloudT& trav_points, + // const WeightT max_weight = + // traversibility::NOMINAL_MAX_WEIGHT); + bool solvePath( + std::vector& path, const Vec3f& start, const Vec3f& goal, - const Vec3f& local_bound_min, - const Vec3f& local_bound_max, - const PointCloudT& loc_cloud, - const MetaCloudT& meta_cloud, - std::vector& path); + const PathPlanMapT& map, + const WeightT max_weight = traversibility::NOMINAL_MAX_WEIGHT); private: + struct Node + { + const PointT& point; + Vec3f dir; // previous direction vec + float g; // cost from start to this node + float h; // heuristic cost to goal + Node* parent = nullptr; + pcl::Indices neighbors; + + Node(const PointT& point, float h = 0.f, Node* p = nullptr); + Node( + const PointT& point, + const Vec3f& dir, + float g = 0.f, + float h = 0.f, + Node* p = nullptr); + + // total cost + inline float f() const { return this->g + this->h; } + // cost of this node + inline WeightT cost() const + { + return traversibility::weight(this->point); + } + inline auto position() const { return this->point.getVector3fMap(); } + }; + +private: + PointCloudT points; pcl::search::KdTree kdtree; std::vector nodes; // all nodes in the search space @@ -128,10 +148,16 @@ class PathPlanner // threshold for considering goal reached float goal_threshold = 0.1f; // radius for neighbor search - float search_radius = 1.0f; - // weights for cost model: edge_cost = lambda_d * dist + lambda_p * penalty - float lambda_dist = 1.f; - float lambda_penalty = 1.f; + float search_radius = 0.5f; + // cost model : + // distance_coeff * (curr.pos - prev.pos).norm() + + // straightness_coeff * (1 - prev.dir.dot(curr.pos - prev.pos).normalized()) + + // traversibility_coeff * trav_weight + float distance_coeff = 1.f; + float straightness_coeff = 1.f; + float traversibility_coeff = 1.f; + float verification_range = 1.5f; + size_t verification_degree = 2; // maximum number of neighbors to consider size_t max_neighbors = 10; }; @@ -148,15 +174,10 @@ class PathPlanner #include "impl/path_planner_impl.hpp" // clang-format off -#define PATH_PLANNER_INSTANTIATE_CLASS_TEMPLATE( \ - POINT_TYPE, \ - META_TYPE) \ - template class csm::perception:: \ - PathPlanner; - -#define PATH_PLANNER_INSTANTIATE_PCL_DEPENDENCIES( \ - POINT_TYPE, \ - META_TYPE) \ +#define PATH_PLANNER_INSTANTIATE_CLASS_TEMPLATE(POINT_TYPE) \ + template class csm::perception::PathPlanner; + +#define PATH_PLANNER_INSTANTIATE_PCL_DEPENDENCIES(POINT_TYPE) \ template class pcl::search::KdTree; // clang-format on diff --git a/include/scan_preprocessor.hpp b/include/modules/scan_preprocessor.hpp similarity index 95% rename from include/scan_preprocessor.hpp rename to include/modules/scan_preprocessor.hpp index 337a927..31fe15e 100644 --- a/include/scan_preprocessor.hpp +++ b/include/modules/scan_preprocessor.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -62,11 +62,14 @@ #include #include -#include "geometry.hpp" -#include "cloud_ops.hpp" -#include "point_def.hpp" +#include +#include +#include +#include +#include +#include + #include "imu_integrator.hpp" -#include "tsq.hpp" #define DESKEW_ROT_THRESH_RAD 1e-3 @@ -175,29 +178,12 @@ class ScanPreprocessor } protected: - struct TransparentStringHash - { - using is_transparent = void; - size_t operator()(std::string_view s) const noexcept - { - return std::hash{}(s); - } - }; - struct TransparentStringEq - { - using is_transparent = void; - bool operator()(std::string_view a, std::string_view b) const noexcept - { - return a == b; - } - }; - template using UoStrMultiMap = std::unordered_multimap< std::string, T, - TransparentStringHash, - TransparentStringEq>; + util::TransparentStringHash, + util::TransparentStringEq>; using TfZoneList = std::pair>; @@ -402,11 +388,20 @@ int ScanPreprocessor::filterPoints(const PointCloudMsg& scan) continue; } + bool out_was_nan = false; // loop through each zone tf for (const auto& zones_tf : this->computed_tf_zones) { + out_was_nan = false; // transform point to output out_pt_v3m = zones_tf.first * in_pt.getVector3fMap(); + if (out_pt_v3m.array().isNaN().any()) + { + // for some reason some points pass the input NaN test + // but blow up when being transformed??? + out_was_nan = true; + continue; + } bool br = false; // check all bounding zones in this frame @@ -428,12 +423,19 @@ int ScanPreprocessor::filterPoints(const PointCloudMsg& scan) break; } } + + if (out_was_nan) + { + out_pt_v3m.setZero(); + this->null_indices.push_back(i); + this->remove_indices.push_back(i); + } } if constexpr (!(Config_Value & PREPROC_PERFORM_DESKEW)) { // condense output points now since no deskew operation - util::pc_remove_selection(this->tf_point_buff, this->remove_indices); + util::removeSelection(this->tf_point_buff, this->remove_indices); } return 0; @@ -562,7 +564,7 @@ int ScanPreprocessor::deskewPoints( out_v = rot * in_v; } - util::pc_remove_selection(this->tf_point_buff, this->remove_indices); + util::removeSelection(this->tf_point_buff, this->remove_indices); pcl::transformPointCloud( this->tf_point_buff, this->tf_point_buff, @@ -571,7 +573,7 @@ int ScanPreprocessor::deskewPoints( else { // tf_point_buff already in output frame from export - util::pc_remove_selection(this->tf_point_buff, this->remove_indices); + util::removeSelection(this->tf_point_buff, this->remove_indices); } return 0; diff --git a/include/selection_octree.hpp b/include/modules/selection_octree.hpp similarity index 89% rename from include/selection_octree.hpp rename to include/modules/selection_octree.hpp index 8f68001..e9fe92f 100644 --- a/include/selection_octree.hpp +++ b/include/modules/selection_octree.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * @@ -61,20 +61,20 @@ class SelectionOctree : { static_assert(pcl::traits::has_xyz::value); - using Super_T = pcl::octree::OctreePointCloudSearch< + using SuperT = pcl::octree::OctreePointCloudSearch< PointT, pcl::octree::OctreeContainerPointIndices>; - using LeafContainer_T = typename Super_T::OctreeT::Base::LeafContainer; + using LeafContainerT = typename SuperT::OctreeT::Base::LeafContainer; - using typename Super_T::IndicesPtr; - using typename Super_T::IndicesConstPtr; + using typename SuperT::IndicesPtr; + using typename SuperT::IndicesConstPtr; - using typename Super_T::PointCloud; - using typename Super_T::PointCloudPtr; - using typename Super_T::PointCloudConstPtr; + using typename SuperT::PointCloud; + using typename SuperT::PointCloudPtr; + using typename SuperT::PointCloudConstPtr; public: - inline SelectionOctree(const double voxel_res) : Super_T(voxel_res) {} + inline SelectionOctree(const double voxel_res) : SuperT(voxel_res) {} void initPoints( const PointCloudConstPtr& cloud, @@ -94,13 +94,13 @@ void SelectionOctree::initPoints( { if (this->input_) { - Super_T:: + SuperT:: deleteTree(); // remove old tree // indices get reset when they are copied } - Super_T::setInputCloud(cloud, indices); - Super_T::addPointsFromInputCloud(); + SuperT::setInputCloud(cloud, indices); + SuperT::addPointsFromInputCloud(); } template diff --git a/include/trajectory_filter.hpp b/include/modules/trajectory_filter.hpp similarity index 98% rename from include/trajectory_filter.hpp rename to include/modules/trajectory_filter.hpp index 8641e51..1cd9323 100644 --- a/include/trajectory_filter.hpp +++ b/include/modules/trajectory_filter.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -40,20 +40,19 @@ #pragma once #include -#include #include +#include +#include +#include #include #include -#include #include -#include -#include #include +#include -#include "tsq.hpp" -#include "util.hpp" -#include "geometry.hpp" +#include +#include #ifndef TRAJECTORY_FILTER_PRINT_DEBUG #define TRAJECTORY_FILTER_PRINT_DEBUG 0 @@ -380,7 +379,7 @@ void TrajectoryFilter::processQueue() Pose3 manifold, interp_off; // get the relative transform (pose form) - util::geom::relative_diff(manifold, before.second, after.second); + util::geom::relativeDiff(manifold, before.second, after.second); // TODO: shortcut if close enough to either endpoint // interpolate assuming constant curvature within the manifold util::geom::lerpSimple(interp_off, manifold, interp_ts); @@ -620,8 +619,8 @@ template void TrajectoryFilter::KeyPose::computeError(const KeyPose& prev) { Pose3 odom_diff, meas_diff; - util::geom::relative_diff(odom_diff, prev.odometry, this->odometry); - util::geom::relative_diff( + util::geom::relativeDiff(odom_diff, prev.odometry, this->odometry); + util::geom::relativeDiff( meas_diff, static_cast(*prev.measurement), static_cast(*this->measurement)); diff --git a/include/transform_sync.hpp b/include/modules/transform_sync.hpp similarity index 93% rename from include/transform_sync.hpp rename to include/modules/transform_sync.hpp index 1da886e..a8652b9 100644 --- a/include/transform_sync.hpp +++ b/include/modules/transform_sync.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -46,10 +46,12 @@ #include +#include #include -#include "util.hpp" -#include "geometry.hpp" +#include +#include + #include "trajectory_filter.hpp" #ifndef TRANSFORM_SYNC_PRINT_DEBUG @@ -82,10 +84,12 @@ class TransformSynchronizer public: inline TransformSynchronizer( tf2_ros::TransformBroadcaster& tf_broadcaster, + tf2_ros::Buffer* tf_buffer = nullptr, std::string_view map_frame_id = "map", std::string_view odom_frame_id = "odom", std::string_view base_frame_id = "base_link") : tf_broadcaster{tf_broadcaster}, + tf_buffer{tf_buffer}, map_frame{map_frame_id}, odom_frame{odom_frame_id}, base_frame{base_frame_id} @@ -134,6 +138,7 @@ class TransformSynchronizer protected: tf2_ros::TransformBroadcaster& tf_broadcaster; + tf2_ros::Buffer* tf_buffer{nullptr}; TrajectoryFilterT trajectory_filter; std::string map_frame; @@ -454,14 +459,18 @@ void TransformSynchronizer::publishMap() { using namespace util::geom::cvt::ops; - geometry_msgs::msg::TransformStamped _tf; + geometry_msgs::msg::TransformStamped tf_; - _tf.header.stamp = util::toTimeStamp(this->map_stamp); - _tf.header.frame_id = this->map_frame; - _tf.child_frame_id = this->odom_frame; - _tf.transform << this->map_tf.pose; + tf_.header.stamp = util::toTimeMsg(this->map_stamp); + tf_.header.frame_id = this->map_frame; + tf_.child_frame_id = this->odom_frame; + tf_.transform << this->map_tf.pose; - this->tf_broadcaster.sendTransform(_tf); + this->tf_broadcaster.sendTransform(tf_); + if(this->tf_buffer) + { + this->tf_buffer->setTransform(tf_, "cardinal_perception"); + } } template @@ -469,14 +478,18 @@ void TransformSynchronizer::publishOdom() { using namespace util::geom::cvt::ops; - geometry_msgs::msg::TransformStamped _tf; + geometry_msgs::msg::TransformStamped tf_; - _tf.header.stamp = util::toTimeStamp(this->odom_stamp); - _tf.header.frame_id = this->odom_frame; - _tf.child_frame_id = this->base_frame; - _tf.transform << this->odom_tf.pose; + tf_.header.stamp = util::toTimeMsg(this->odom_stamp); + tf_.header.frame_id = this->odom_frame; + tf_.child_frame_id = this->base_frame; + tf_.transform << this->odom_tf.pose; - this->tf_broadcaster.sendTransform(_tf); + this->tf_broadcaster.sendTransform(tf_); + if(this->tf_buffer) + { + this->tf_buffer->setTransform(tf_, "cardinal_perception"); + } } }; // namespace perception diff --git a/include/modules/traversibility_gen.hpp b/include/modules/traversibility_gen.hpp index 37e421b..60e6fe2 100644 --- a/include/modules/traversibility_gen.hpp +++ b/include/modules/traversibility_gen.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -52,10 +52,10 @@ #include #include -#include "util.hpp" -#include "cloud_ops.hpp" -#include "meta_grid.hpp" -#include "point_def.hpp" + +#include +#include +#include namespace csm @@ -64,15 +64,14 @@ namespace perception { template< - typename Point_T = pcl::PointXYZ, - typename Meta_T = csm::perception::NormalTraversal> + typename MapPoint_T = pcl::PointXYZ, + typename TravPoint_T = pcl::PointXYZI> class TraversibilityGenerator { - static_assert(pcl::traits::has_xyz::value); - static_assert(pcl::traits::has_normal::value); + static_assert(pcl::traits::has_xyz::value); static_assert( - util::traits::has_trav_weight::value || - pcl::traits::has_curvature::value); + pcl::traits::has_xyz::value && + util::traits::supports_traversibility::value); private: using Vec2f = Eigen::Vector2f; @@ -83,22 +82,11 @@ class TraversibilityGenerator using IndicesPair = std::pair; public: - using PointT = Point_T; - using MetaT = Meta_T; - using NormalT = MetaT; - - using TravPointCloud = pcl::PointCloud; - using MetaDataCloud = pcl::PointCloud; - using MetaDataList = typename MetaDataCloud::VectorType; - - static constexpr float TRAV_MIN_WEIGHT = 0.f; - static constexpr float TRAV_MAX_WEIGHT = 1.f; - static constexpr float EXT_TRAV_MAX_WEIGHT = 10.f; - static constexpr float NON_TRAV_MIN_WEIGHT = 10.f; - static constexpr float UNKNOWN_WEIGHT = -1.f; - static constexpr float FRONTIER_WEIGHT = -2.f; - static constexpr float NON_TRAV_WEIGHT = - std::numeric_limits::infinity(); + using MapPointT = MapPoint_T; + using TravPointT = TravPoint_T; + + using MapPointCloud = pcl::PointCloud; + using TravPointCloud = pcl::PointCloud; public: TraversibilityGenerator() = default; @@ -114,10 +102,11 @@ class TraversibilityGenerator float avoidance_radius, float trav_score_curvature_weight, float trav_score_grad_weight, + int min_vox_cell_points, int interp_sample_count); void processMapPoints( - const TravPointCloud& map_points, + const MapPointCloud& map_points, const Vec3f& map_min_bound, const Vec3f& map_max_bound, const Vec3f& map_grav_vec, @@ -125,24 +114,14 @@ class TraversibilityGenerator public: TravPointCloud& copyPoints(TravPointCloud& copy_cloud) const; - MetaDataList& copyMetaDataList(MetaDataList& copy_list) const; /* WARNING : This can only be done once per iteration to extract the * processed result! */ TravPointCloud& swapPoints(TravPointCloud& swap_cloud); - /* WARNING : This can only be done once per iteration to extract the - * processed result! */ - MetaDataList& swapMetaDataList(MetaDataList& swap_list); - - void extractTravElements( - TravPointCloud& trav_points, - MetaDataList& trav_meta_data) const; - void extractExtTravElements( - TravPointCloud& ext_trav_points, - MetaDataList& ext_trav_meta_data) const; - void extractNonTravElements( - TravPointCloud& non_trav_points, - MetaDataList& non_trav_meta_data) const; + + void extractTravPoints(TravPointCloud& trav_points) const; + void extractExtTravPoints(TravPointCloud& ext_trav_points) const; + void extractNonTravPoints(TravPointCloud& non_trav_points) const; /* WARNING : This can only be done once per iteration to extract the * processed result! */ @@ -155,52 +134,27 @@ class TraversibilityGenerator pcl::Indices& swapNonTravIndices(pcl::Indices& swap_indices); protected: - static inline float& trav_weight(MetaT& m) - { - if constexpr (util::traits::has_trav_weight::value) - { - return m.trav_weight(); - } - else - { - return m.curvature; - } - } - static inline float trav_weight(const MetaT& m) - { - if constexpr (util::traits::has_trav_weight::value) - { - return m.trav_weight(); - } - else - { - return m.curvature; - } - } - void process( - const TravPointCloud& map_points, + const MapPointCloud& map_points, const Vec3f& map_min_bound, const Vec3f& map_max_bound, const Vec3f& map_grav_vec, const Vec3f& source_pos); protected: - pcl::search::KdTree interp_search_tree; - pcl::search::KdTree trav_search_tree; + pcl::search::KdTree interp_search_tree; + pcl::search::KdTree trav_search_tree; util::GridMeta vox_grid; std::vector cell_pt_indices; std::vector interp_index_map; TravPointCloud points; - MetaDataCloud points_meta; - typename TravPointCloud::Ptr points_ptr; pcl::Indices - trav_selection, // set of points that are traversible (excluding spread) - ext_trav_selection, // extended set of points that may be traversible - avoid_selection; // set of points that are not traversible ever + trav_selection, // set of points that are traversible (excluding spread) + ext_trav_selection, // extended set of points that may be traversible + avoid_selection; // set of points that are not traversible ever mutable std::mutex mtx; @@ -213,6 +167,7 @@ class TraversibilityGenerator float avoid_radius_sqrd{0.25f}; float trav_score_curvature_weight{5.f}; float trav_score_grad_weight{1.f}; + int min_vox_cell_points{3}; int interp_sample_count{7}; // }; @@ -228,12 +183,12 @@ class TraversibilityGenerator #include "impl/traversibility_gen_impl.hpp" // clang-format off -#define TRAVERSIBILITY_GEN_INSTANTIATE_CLASS_TEMPLATE(POINT_TYPE, META_TYPE) \ - template class csm::perception:: \ - TraversibilityGenerator; +#define TRAVERSIBILITY_GEN_INSTANTIATE_CLASS_TEMPLATE(MAP_POINT_TYPE, TRAV_POINT_TYPE) \ + template class csm::perception:: \ + TraversibilityGenerator; -#define TRAVERSIBILITY_GEN_INSTANTIATE_PCL_DEPENDENCIES(POINT_TYPE, META_TYPE) - // template class pcl::NormalEstimationOMP; +#define TRAVERSIBILITY_GEN_INSTANTIATE_PCL_DEPENDENCIES(MAP_POINT_TYPE, TRAV_POINT_TYPE) \ + template class pcl::search::KdTree; // clang-format on #endif diff --git a/include/modules/ue_octree.hpp b/include/modules/ue_octree.hpp new file mode 100644 index 0000000..efcffbb --- /dev/null +++ b/include/modules/ue_octree.hpp @@ -0,0 +1,660 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include +#include +#include +#include +#include + +#include +#include + + +namespace csm +{ +namespace perception +{ + +template +class UEOctree +{ +public: + using FloatT = Float_T; + using IndexT = std::make_unsigned::type; + + using Vec3f = Eigen::Vector; + using Vec3i = Eigen::Vector; + using Arr3f = Eigen::Array; + using Arr3i = Eigen::Array; + using Box3f = Eigen::AlignedBox; + + using NodeDescriptor = Eigen::Vector; + + static constexpr size_t MAX_DEPTH = (sizeof(IndexT) * 8 - 1); + +public: + UEOctree(FloatT max_res); + ~UEOctree(); + +public: + void clear(); + void reconfigure(FloatT max_res); + + void addExploredSpace(const Vec3f& min, const Vec3f& max); + void crop(const Vec3f& min, const Vec3f& max); + + bool isExplored(const Vec3f& pt) const; + FloatT distToUnexplored(const Vec3f& pt, FloatT max_dist) const; + + inline FloatT resolution() const { return this->vox_res; } + inline size_t treeDepth() const { return this->root_height; } + inline size_t allocEstimate() const { return this->alloc_estimate; } + +protected: + struct Node + { + Node* children{nullptr}; + bool explored{false}; + + Node() = default; + inline ~Node() = default; + + Node& operator[](size_t i); + const Node& operator[](size_t i) const; + + inline bool isNull() const { return !(this->children); } + inline bool fullyExplored() const { return this->explored; } + inline bool anyExplored() const + { + return !this->isNull() || this->fullyExplored(); + } + inline bool isUnexplored() const + { + return !this->explored && this->isNull(); + } + }; + +protected: + Node* allocChildren(); + void initChildren(Node&); + void recursiveClearChildren(Node&); + void collapseAsExplored(Node&); + void collapseAsUnexplored(Node&); + + static bool isLeaf(const NodeDescriptor& n); + static Vec3i getDescriptorKey(const NodeDescriptor& n); + static Vec3f getDescriptorKeyF(const NodeDescriptor& n); + static Vec3i getDescriptorSpan3(const NodeDescriptor& n); + static Vec3f getDescriptorSpan3f(const NodeDescriptor& n); + + NodeDescriptor getRootDescriptor() const; + NodeDescriptor getChildDescriptor(const NodeDescriptor& n, size_t i) const; + Box3f getDescriptorBox(const NodeDescriptor& n) const; + + static FloatT distSquaredToBox(const Vec3f& p, const Box3f& b); + static FloatT distToBoxInv(const Vec3f& p, const Box3f& b); + +protected: + bool adjustBounds(const Vec3f& min, const Vec3f& max); + void recursiveExplore( + Node& node, + const NodeDescriptor& descriptor, + const Box3f& zone); + void recursiveCrop( + Node& node, + const NodeDescriptor& descriptor, + const Box3f& zone); + +protected: + Vec3f origin{Vec3f::Zero()}; + IndexT vox_span{0}; + FloatT vox_res{0}; + + Node root; + size_t root_height{0}; + + size_t alloc_estimate{0}; +}; + + + +// --- Implementation ---------------------------------------------------------- + +template +UEOctree::UEOctree(FloatT res) : vox_res{res} +{ +} + +template +UEOctree::~UEOctree() +{ + this->recursiveClearChildren(this->root); +} + +template +void UEOctree::clear() +{ + this->collapseAsUnexplored(this->root); + this->root_height = 0; + this->vox_span = 0; +} + +template +void UEOctree::reconfigure(FloatT res) +{ + this->clear(); + this->vox_res = res; +} + +template +void UEOctree::addExploredSpace(const Vec3f& min, const Vec3f& max) +{ + if (this->adjustBounds(min, max)) + { + if (this->root_height < 1) + { + this->root.explored = true; + } + else + { + this->recursiveExplore( + this->root, + this->getRootDescriptor(), + Box3f{min, max}); + } + } +} + +template +void UEOctree::crop(const Vec3f& min, const Vec3f& max) +{ + this->recursiveCrop(this->root, this->getRootDescriptor(), Box3f{min, max}); +} + +template +bool UEOctree::isExplored(const Vec3f& pt) const +{ + const Arr3f aligned_pt = ((pt - this->origin) / this->vox_res).array(); + if ((aligned_pt >= Arr3f::Zero()).all() && + (aligned_pt < Arr3f::Constant(this->vox_span)).all()) + { + if (this->root.fullyExplored()) + { + return true; + } + + const Arr3i vox_idx = aligned_pt.floor().template cast(); + + const Node* node = &(this->root); + // iterates through [root_height - 1 ... 0], while active node still has children + for (size_t height = this->root_height; + height-- > 0 && !node->isNull();) + { + const IndexT i = + (((vox_idx.x() >> height) & 0x1) << 2 | + ((vox_idx.y() >> height) & 0x1) << 1 | + ((vox_idx.z() >> height) & 0x1)); + + node = node->children + i; + if (node->fullyExplored()) + { + return true; + } + } + } + return false; +} + +template +typename UEOctree::FloatT UEOctree::distToUnexplored( + const Vec3f& pt, + FloatT max_dist) const +{ + const FloatT max_dist2 = max_dist * max_dist; + + const NodeDescriptor root_desc = this->getRootDescriptor(); + const Box3f root_box = this->getDescriptorBox(root_desc); + + if (!root_box.contains(pt)) + { + return 0; + } + if (this->root.fullyExplored()) + { + const FloatT d = distToBoxInv(pt, root_box); + return d <= max_dist ? d : std::numeric_limits::infinity(); + } + + struct QNode + { + const Node* node; + NodeDescriptor desc; + FloatT dist2; + + bool operator>(const QNode& other) const + { + return this->dist2 > other.dist2; + } + }; + + std::priority_queue, std::greater> queue; + queue.push({&this->root, root_desc, 0}); + + while (!queue.empty()) + { + const QNode cur = queue.top(); + queue.pop(); + + if (cur.dist2 > max_dist2) + { + continue; + } + + const Node* node = cur.node; + const NodeDescriptor& desc = cur.desc; + + if (node->isUnexplored()) + { + return std::sqrt(cur.dist2); + } + + if (isLeaf(desc)) + { + if (!node->explored) + { + return std::sqrt(cur.dist2); + } + continue; + } + + if (!node->isNull()) + { + for (size_t i = 0; i < 8; ++i) + { + const Node& child = (*node)[i]; + if (child.fullyExplored()) + { + continue; + } + + const NodeDescriptor child_desc = + this->getChildDescriptor(desc, i); + + const FloatT d2 = + distSquaredToBox(pt, this->getDescriptorBox(child_desc)); + if (d2 <= max_dist2) + { + queue.push({&child, child_desc, d2}); + } + } + } + else + { + return std::sqrt(cur.dist2); + } + } + + return std::numeric_limits::infinity(); +} + + +template +typename UEOctree::Node& UEOctree::Node::operator[](size_t i) +{ + // assert(i < 8 && this->children); + return this->children[i]; +} +template +const typename UEOctree::Node& UEOctree::Node::operator[]( + size_t i) const +{ + // assert(i < 8 && this->children); + return this->children[i]; +} + + +template +typename UEOctree::Node* UEOctree::allocChildren() +{ + Node* children = new Node[8]; + this->alloc_estimate += sizeof(Node) * 8; + return children; +} +template +void UEOctree::initChildren(Node& node) +{ + if(node.isNull()) + { + node.children = this->allocChildren(); + } +} +template +void UEOctree::recursiveClearChildren(Node& node) +{ + if(node.children) + { + for(size_t i = 0; i < 8; i++) + { + this->recursiveClearChildren(node[i]); + } + + delete[] node.children; + node.children = nullptr; + this->alloc_estimate -= sizeof(Node) * 8; + } +} +template +void UEOctree::collapseAsExplored(Node& node) +{ + this->recursiveClearChildren(node); + node.explored = true; +} +template +void UEOctree::collapseAsUnexplored(Node& node) +{ + this->recursiveClearChildren(node); + node.explored = false; +} + + +template +bool UEOctree::isLeaf(const NodeDescriptor& n) +{ + return n[3] <= 1; +} +template +typename UEOctree::Vec3i UEOctree::getDescriptorKey( + const NodeDescriptor& n) +{ + return n.template head<3>(); +} +template +typename UEOctree::Vec3f UEOctree::getDescriptorKeyF( + const NodeDescriptor& n) +{ + return n.template head<3>().template cast(); +} +template +typename UEOctree::Vec3i UEOctree::getDescriptorSpan3( + const NodeDescriptor& n) +{ + return Vec3i::Constant(n[3]); +} +template +typename UEOctree::Vec3f UEOctree::getDescriptorSpan3f( + const NodeDescriptor& n) +{ + return Vec3f::Constant(static_cast(n[3])); +} + +template +typename UEOctree::NodeDescriptor UEOctree::getRootDescriptor() + const +{ + return NodeDescriptor{0, 0, 0, (I)0x1 << this->root_height}; +} + +template +typename UEOctree::NodeDescriptor UEOctree::getChildDescriptor( + const NodeDescriptor& n, + size_t i) const +{ + // assert(i < 8); + return NodeDescriptor{ + n[0] + (n[3] / 2) * static_cast(i >> 2 & 0x1), + n[1] + (n[3] / 2) * static_cast(i >> 1 & 0x1), + n[2] + (n[3] / 2) * static_cast(i >> 0 & 0x1), + n[3] / 2}; +} + +template +typename UEOctree::Box3f UEOctree::getDescriptorBox( + const NodeDescriptor& n) const +{ + const Vec3f min_corner = + this->origin + getDescriptorKeyF(n) * this->vox_res; + return Box3f{ + min_corner, + min_corner + getDescriptorSpan3f(n) * this->vox_res}; +} + +template +typename UEOctree::FloatT UEOctree::distSquaredToBox( + const Vec3f& p, + const Box3f& b) +{ + FloatT d2 = 0; + for (size_t i = 0; i < 3; i++) + { + if (p[i] < b.min()[i]) + { + FloatT d = b.min()[i] - p[i]; + d2 += d * d; + } + else if (p[i] > b.max()[i]) + { + FloatT d = p[i] - b.max()[i]; + d2 += d * d; + } + } + return d2; +} + +template +typename UEOctree::FloatT UEOctree::distToBoxInv( + const Vec3f& p, + const Box3f& b) +{ + if (!b.contains(p)) + { + return 0; + } + + FloatT dx = std::min(p.x() - b.min().x(), b.max().x() - p.x()); + FloatT dy = std::min(p.y() - b.min().y(), b.max().y() - p.y()); + FloatT dz = std::min(p.z() - b.min().z(), b.max().z() - p.z()); + + return std::min({dx, dy, dz}); +} + + +template +bool UEOctree::adjustBounds(const Vec3f& min, const Vec3f& max) +{ + if (!this->root.anyExplored()) + { + this->origin = min; + const FloatT max_diff = ((max - min) / this->vox_res).maxCoeff(); + this->root_height = + static_cast(std::ceil(std::log2(std::max(max_diff, 1.f)))); + this->vox_span = (0x1 << std::min(this->root_height, MAX_DEPTH)); + + if (this->root_height > MAX_DEPTH) + { + this->root_height = MAX_DEPTH; + return false; + } + } + else + { + while ((min.array() < this->origin.array()).any()) + { + if (this->root_height >= MAX_DEPTH) + { + return false; + } + + const size_t x_bit = min.x() < this->origin.x() ? 0x1 : 0x0; + const size_t y_bit = min.y() < this->origin.y() ? 0x1 : 0x0; + const size_t z_bit = min.z() < this->origin.z() ? 0x1 : 0x0; + const Vec3f coeffs{ + static_cast(x_bit), + static_cast(y_bit), + static_cast(z_bit)}; + this->origin -= coeffs * this->vox_res * (0x1 << this->root_height); + this->vox_span *= 2; + this->root_height++; + if (this->root.anyExplored()) + { + const size_t octant = (x_bit << 2) | (y_bit << 1) | (z_bit << 0); + Node* tmp = this->allocChildren(); + tmp[octant].children = root.children; + tmp[octant].explored = root.explored; + root.children = tmp; + root.explored = false; + } + } + while (((max - this->origin).array() >= + Arr3f::Constant(this->vox_span * this->vox_res)) + .any()) + { + if (this->root_height >= MAX_DEPTH) + { + return false; + } + + this->vox_span *= 2; + this->root_height++; + if (this->root.anyExplored()) + { + Node* tmp = this->allocChildren(); + tmp[0].children = root.children; + tmp[0].explored = root.explored; + root.children = tmp; + root.explored = false; + } + } + } + + return true; +} + +template +void UEOctree::recursiveExplore( + Node& node, + const NodeDescriptor& descriptor, + const Box3f& zone) +{ + this->initChildren(node); + bool all_explored = true; + for (size_t i = 0; i < 8; i++) + { + Node& child = node[i]; + NodeDescriptor child_desc = this->getChildDescriptor(descriptor, i); + Box3f child_span = this->getDescriptorBox(child_desc); + + if (zone.contains(child_span)) + { + this->collapseAsExplored(child); + } + else if (zone.intersects(child_span)) + { + if (isLeaf(child_desc)) + { + this->collapseAsExplored(child); + } + else + { + this->recursiveExplore(child, child_desc, zone); + } + } + + all_explored &= child.fullyExplored(); + } + + if (all_explored) + { + this->collapseAsExplored(node); + } +} + +template +void UEOctree::recursiveCrop( + Node& node, + const NodeDescriptor& descriptor, + const Box3f& zone) +{ + Box3f span = this->getDescriptorBox(descriptor); + if (!zone.contains(span)) + { + if (zone.intersects(span)) + { + // not contained but intersected + if (!isLeaf(descriptor)) + { + // not leaf --> expand children and recurse + const bool was_explored = node.explored; + this->initChildren(node); + node.explored = false; + + bool all_explored = true; + for (size_t i = 0; i < 8; i++) + { + Node& child = node[i]; + // need to propegate down in the case that the parent was split up, + // but leave untouched otherwise + child.explored |= was_explored; + this->recursiveCrop( + child, + this->getChildDescriptor(descriptor, i), + zone); + + all_explored &= child.fullyExplored(); + } + + if (all_explored) + { + this->collapseAsExplored(node); + } + } + // leaf --> leave alone + } + else + { + // not contained and not intersected --> delete + this->collapseAsUnexplored(node); + } + } +} + +}; // namespace perception +}; // namespace csm diff --git a/include/nano_gicp/impl/nanoflann_impl.hpp b/include/nano_gicp/impl/nanoflann_impl.hpp index 54102a0..5070cd3 100644 --- a/include/nano_gicp/impl/nanoflann_impl.hpp +++ b/include/nano_gicp/impl/nanoflann_impl.hpp @@ -1,18 +1,9 @@ -/************************************************************ - * - * Copyright (c) 2022, University of California, Los Angeles - * - * Authors: Kenny J. Chen, Brett T. Lopez - * Contact: kennyjchen@ucla.edu, btlopez@ucla.edu - * - ***********************************************************/ - /*********************************************************************** * Software License Agreement (BSD License) * * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved. * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved. - * Copyright 2011-2021 Jose Luis Blanco (joseluisblancoc@gmail.com). + * Copyright 2011-2025 Jose Luis Blanco (joseluisblancoc@gmail.com). * All rights reserved. * * THE BSD LICENSE @@ -46,2111 +37,3248 @@ * nanoflann does not require compiling or installing, just an * #include in your code. * + * Macros that are observed in this file: + * - NANOFLANN_NO_THREADS: If defined, single thread will be enforced. + * - NANOFLANN_FIRST_MATCH: If defined, in case of a tie in distances the item with the smallest + * index will be returned. + * - NANOFLANN_NODE_ALIGNMENT: The memory alignment, in bytes, for kd-tree nodes. Default: 16 + * * See: - * - C++ API organized by modules - * - Online README - * - Doxygen - * documentation + * - [Online README](https://github.com/jlblancoc/nanoflann) + * - [C++ API documentation](https://jlblancoc.github.io/nanoflann/) */ -// clang-format off - -#ifndef NANOFLANN_HPP_ -#define NANOFLANN_HPP_ +#pragma once #include #include +#include #include -#include // for abs() -#include // for fwrite() -#include // for abs() -#include -#include // std::reference_wrapper +#include // for abs() +#include +#include // for abs() +#include // std::reference_wrapper +#include +#include +#include // std::numeric_limits +#include +#include #include +#include #include /** Library version: 0xMmP (M=Major,m=minor,P=patch) */ -#define NANOFLANN_VERSION 0x132 - -// Avoid conflicting declaration of min/max macros in windows headers -#if !defined(NOMINMAX) && (defined(_WIN32) || defined(_WIN32_) || defined(WIN32) || defined(_WIN64)) -#define NOMINMAX -#ifdef max -#undef max -#undef min +#define NANOFLANN_VERSION 0x190 + +// Avoid conflicting declaration of min/max macros in Windows headers +#if !defined(NOMINMAX) && \ + (defined(_WIN32) || defined(_WIN32_) || defined(WIN32) || defined(_WIN64)) + #define NOMINMAX + #ifdef max + #undef max + #undef min + #endif +#endif +// Avoid conflicts with X11 headers +#ifdef None + #undef None +#endif + +// Handle restricted pointers +#if defined(__GNUC__) || defined(__clang__) + #define NANOFLANN_RESTRICT __restrict__ +#elif defined(_MSC_VER) + #define NANOFLANN_RESTRICT __restrict +#else + #define NANOFLANN_RESTRICT #endif + +// Memory alignment of KD-tree nodes: +#ifndef NANOFLANN_NODE_ALIGNMENT + #define NANOFLANN_NODE_ALIGNMENT 16 #endif namespace nanoflann { - /** @addtogroup nanoflann_grp nanoflann C++ library for ANN - * @{ */ +/** @addtogroup nanoflann_grp nanoflann C++ library for KD-trees + * @{ */ + +/** the PI constant (required to avoid MSVC missing symbols) */ +template +constexpr T pi_const() +{ + return static_cast(3.14159265358979323846); +} + +/** + * Traits if object is resizable and assignable (typically has a resize | assign + * method) + */ +template +struct has_resize : std::false_type +{ +}; - /** the PI constant (required to avoid MSVC missing symbols) */ - template - T pi_const() +template +struct has_resize().resize(1), 0)> : + std::true_type +{ +}; + +template +struct has_assign : std::false_type +{ +}; + +template +struct has_assign().assign(1, 0), 0)> : + std::true_type +{ +}; + +/** + * Free function to resize a resizable object + */ +template +inline typename std::enable_if::value, void>::type resize( + Container& c, + const size_t nElements) +{ + c.resize(nElements); +} + +/** + * Free function that has no effects on non resizable containers (e.g. + * std::array) It raises an exception if the expected size does not match + */ +template +inline typename std::enable_if::value, void>::type + resize(Container& c, const size_t nElements) +{ + if (nElements != c.size()) { - return static_cast(3.14159265358979323846); + throw std::logic_error("Attempt to resize a fixed size container."); } +} - /** - * Traits if object is resizable and assignable (typically has a resize | assign - * method) - */ - template - struct has_resize : std::false_type - { - }; +/** + * Free function to assign to a container + */ +template +inline typename std::enable_if::value, void>::type + assign(Container& c, const size_t nElements, const T& value) +{ + c.assign(nElements, value); +} - template - struct has_resize().resize(1), 0)> : std::true_type +/** + * Free function to assign to a std::array + */ +template +inline typename std::enable_if::value, void>::type + assign(Container& c, const size_t nElements, const T& value) +{ + for (size_t i = 0; i < nElements; i++) { - }; + c[i] = value; + } +} - template - struct has_assign : std::false_type +/** operator "<" for std::sort() */ +struct IndexDist_Sorter +{ + /** PairType will be typically: ResultItem */ + template + bool operator()(const PairType& p1, const PairType& p2) const { - }; - - template - struct has_assign().assign(1, 0), 0)> : std::true_type + return p1.second < p2.second; + } +}; + +/** + * Each result element in RadiusResultSet. Note that distances and indices + * are named `first` and `second` to keep backward-compatibility with the + * `std::pair<>` type used in the past. In contrast, this structure is ensured + * to be `std::is_standard_layout` so it can be used in wrappers to other + * languages. + * See: https://github.com/jlblancoc/nanoflann/issues/166 + */ +template +struct ResultItem +{ + ResultItem() = default; + ResultItem(const IndexType index, const DistanceType distance) : + first(index), + second(distance) { - }; + } - /** - * Free function to resize a resizable object - */ - template - inline typename std::enable_if::value, void>::type resize(Container & c, - const size_t nElements) + IndexType first; //!< Index of the sample in the dataset + DistanceType second; //!< Distance from sample to query point +}; + +/** @addtogroup result_sets_grp Result set classes + * @{ */ + +/** Result set for KNN searches (N-closest neighbors) */ +template< + typename _DistanceType, + typename _IndexType = size_t, + typename _CountType = size_t> +class KNNResultSet +{ +public: + using DistanceType = _DistanceType; + using IndexType = _IndexType; + using CountType = _CountType; + +private: + IndexType* indices; + DistanceType* dists; + CountType capacity; + CountType count; + +public: + explicit KNNResultSet(CountType capacity_) : + indices(nullptr), + dists(nullptr), + capacity(capacity_), + count(0) { - c.resize(nElements); } - /** - * Free function that has no effects on non resizable containers (e.g. - * std::array) It raises an exception if the expected size does not match - */ - template - inline typename std::enable_if::value, void>::type resize(Container & c, - const size_t nElements) + void init(IndexType* indices_, DistanceType* dists_) { - if(nElements != c.size()) - throw std::logic_error("Try to change the size of a std::array."); + indices = indices_; + dists = dists_; + count = 0; } + CountType size() const noexcept { return count; } + bool empty() const noexcept { return count == 0; } + bool full() const noexcept { return count == capacity; } + /** - * Free function to assign to a container + * Called during search to add an element matching the criteria. + * @return true if the search should be continued, false if the results are + * sufficient */ - template - inline typename std::enable_if::value, void>::type - assign(Container & c, const size_t nElements, const T & value) + bool addPoint(DistanceType dist, IndexType index) { - c.assign(nElements, value); + CountType i; + for (i = count; i > 0; --i) + { + /** If defined and two points have the same distance, the one with + * the lowest-index will be returned first. */ +#ifdef NANOFLANN_FIRST_MATCH + if ((dists[i - 1] > dist) || + ((dist == dists[i - 1]) && (indices[i - 1] > index))) + { +#else + if (dists[i - 1] > dist) + { +#endif + if (i < capacity) + { + dists[i] = dists[i - 1]; + indices[i] = indices[i - 1]; + } + } + else + { + break; + } + } + if (i < capacity) + { + dists[i] = dist; + indices[i] = index; + } + if (count < capacity) + { + count++; + } + + // tell caller that the search shall continue + return true; } - /** - * Free function to assign to a std::array - */ - template - inline typename std::enable_if::value, void>::type - assign(Container & c, const size_t nElements, const T & value) + //! Returns the worst distance among found solutions if the search result is + //! full, or the maximum possible distance, if not full yet. + DistanceType worstDist() const noexcept { - for(size_t i = 0; i < nElements; i++) c[i] = value; + return (count < capacity || !count) + ? std::numeric_limits::max() + : dists[count - 1]; } - /** @addtogroup result_sets_grp Result set classes - * @{ */ - template - class KNNResultSet + void sort() { - public: - typedef _DistanceType DistanceType; - typedef _IndexType IndexType; - typedef _CountType CountType; - - private: - IndexType * indices; - DistanceType * dists; - CountType capacity; - CountType count; - - public: - inline KNNResultSet(CountType capacity_) : indices(0), dists(0), capacity(capacity_), count(0) {} + // already sorted + } +}; + +/** Result set for RKNN searches (N-closest neighbors with a maximum radius) */ +template< + typename _DistanceType, + typename _IndexType = size_t, + typename _CountType = size_t> +class RKNNResultSet +{ +public: + using DistanceType = _DistanceType; + using IndexType = _IndexType; + using CountType = _CountType; + +private: + IndexType* indices; + DistanceType* dists; + CountType capacity; + CountType count; + DistanceType maximumSearchDistanceSquared; + +public: + explicit RKNNResultSet( + CountType capacity_, + DistanceType maximumSearchDistanceSquared_) : + indices(nullptr), + dists(nullptr), + capacity(capacity_), + count(0), + maximumSearchDistanceSquared(maximumSearchDistanceSquared_) + { + } - inline void init(IndexType * indices_, DistanceType * dists_) + void init(IndexType* indices_, DistanceType* dists_) + { + indices = indices_; + dists = dists_; + count = 0; + if (capacity) { - indices = indices_; - dists = dists_; - count = 0; - if(capacity) - dists[capacity - 1] = (std::numeric_limits::max)(); + dists[capacity - 1] = maximumSearchDistanceSquared; } + } - inline CountType size() const { return count; } - - inline bool full() const { return count == capacity; } + CountType size() const noexcept { return count; } + bool empty() const noexcept { return count == 0; } + bool full() const noexcept { return count == capacity; } - /** - * Called during search to add an element matching the criteria. - * @return true if the search should be continued, false if the results are - * sufficient - */ - inline bool addPoint(DistanceType dist, IndexType index) - { - CountType i; - for(i = count; i > 0; --i) + /** + * Called during search to add an element matching the criteria. + * @return true if the search should be continued, false if the results are + * sufficient + */ + bool addPoint(DistanceType dist, IndexType index) + { + CountType i; + for (i = count; i > 0; --i) + { + /** If defined and two points have the same distance, the one with + * the lowest-index will be returned first. */ +#ifdef NANOFLANN_FIRST_MATCH + if ((dists[i - 1] > dist) || + ((dist == dists[i - 1]) && (indices[i - 1] > index))) { -#ifdef NANOFLANN_FIRST_MATCH // If defined and two points have the same - // distance, the one with the lowest-index will be - // returned first. - if((dists[i - 1] > dist) || ((dist == dists[i - 1]) && (indices[i - 1] > index))) - { #else - if(dists[i - 1] > dist) - { + if (dists[i - 1] > dist) + { #endif - if(i < capacity) - { - dists[i] = dists[i - 1]; - indices[i] = indices[i - 1]; - } + if (i < capacity) + { + dists[i] = dists[i - 1]; + indices[i] = indices[i - 1]; } - else - break; } - if(i < capacity) + else { - dists[i] = dist; - indices[i] = index; + break; } - if(count < capacity) - count++; - - // tell caller that the search shall continue - return true; } - - inline DistanceType worstDist() const { return dists[capacity - 1]; } - }; - - /** operator "<" for std::sort() */ - struct IndexDist_Sorter - { - /** PairType will be typically: std::pair */ - template - inline bool operator()(const PairType & p1, const PairType & p2) const + if (i < capacity) { - return p1.second < p2.second; + dists[i] = dist; + indices[i] = index; } - }; - - /** - * A result-set class used when performing a radius based search. - */ - template - class RadiusResultSet - { - public: - typedef _DistanceType DistanceType; - typedef _IndexType IndexType; - - public: - const DistanceType radius; - - std::vector> & m_indices_dists; - - inline RadiusResultSet(DistanceType radius_, std::vector> & indices_dists) : - radius(radius_), m_indices_dists(indices_dists) + if (count < capacity) { - init(); + count++; } - inline void init() { clear(); } - inline void clear() { m_indices_dists.clear(); } - - inline size_t size() const { return m_indices_dists.size(); } + // tell caller that the search shall continue + return true; + } - inline bool full() const { return true; } + //! Returns the worst distance among found solutions if the search result is + //! full, or the maximum possible distance, if not full yet. + DistanceType worstDist() const noexcept + { + return (count < capacity || !count) ? maximumSearchDistanceSquared + : dists[count - 1]; + } - /** - * Called during search to add an element matching the criteria. - * @return true if the search should be continued, false if the results are - * sufficient - */ - inline bool addPoint(DistanceType dist, IndexType index) - { - if(dist < radius) - m_indices_dists.push_back(std::make_pair(index, dist)); - return true; - } + void sort() + { + // already sorted + } +}; - inline DistanceType worstDist() const { return radius; } +/** + * A result-set class used when performing a radius based search. + */ +template +class RadiusResultSet +{ +public: + using DistanceType = _DistanceType; + using IndexType = _IndexType; - /** - * Find the worst result (furtherest neighbor) without copying or sorting - * Pre-conditions: size() > 0 - */ - std::pair worst_item() const - { - if(m_indices_dists.empty()) - throw std::runtime_error("Cannot invoke RadiusResultSet::worst_item() on " - "an empty list of results."); - typedef typename std::vector>::const_iterator DistIt; - DistIt it = std::max_element(m_indices_dists.begin(), m_indices_dists.end(), IndexDist_Sorter()); - return *it; - } - }; +public: + const DistanceType radius; - /** @} */ + std::vector>& m_indices_dists; - /** @addtogroup loadsave_grp Load/save auxiliary functions - * @{ */ - template - void save_value(FILE * stream, const T & value, size_t count = 1) + explicit RadiusResultSet( + DistanceType radius_, + std::vector>& indices_dists) : + radius(radius_), + m_indices_dists(indices_dists) { - fwrite(&value, sizeof(value), count, stream); + init(); } - template - void save_value(FILE * stream, const std::vector & value) - { - size_t size = value.size(); - fwrite(&size, sizeof(size_t), 1, stream); - fwrite(&value[0], sizeof(T), size, stream); - } + void init() { clear(); } + void clear() { m_indices_dists.clear(); } - template - void load_value(FILE * stream, T & value, size_t count = 1) + size_t size() const noexcept { return m_indices_dists.size(); } + bool empty() const noexcept { return m_indices_dists.empty(); } + bool full() const noexcept { return true; } + + /** + * Called during search to add an element matching the criteria. + * @return true if the search should be continued, false if the results are + * sufficient + */ + bool addPoint(DistanceType dist, IndexType index) { - size_t read_cnt = fread(&value, sizeof(value), count, stream); - if(read_cnt != count) + if (dist < radius) { - throw std::runtime_error("Cannot read from file"); + m_indices_dists.emplace_back(index, dist); } + return true; } - template - void load_value(FILE * stream, std::vector & value) + DistanceType worstDist() const noexcept { return radius; } + + /** + * Find the worst result (farthest neighbor) without copying or sorting + * Pre-conditions: size() > 0 + */ + ResultItem worst_item() const { - size_t size; - size_t read_cnt = fread(&size, sizeof(size_t), 1, stream); - if(read_cnt != 1) + if (m_indices_dists.empty()) { - throw std::runtime_error("Cannot read from file"); - } - value.resize(size); - read_cnt = fread(&value[0], sizeof(T), size, stream); - if(read_cnt != size) - { - throw std::runtime_error("Cannot read from file"); + throw std::runtime_error( + "Cannot invoke RadiusResultSet::worst_item() on " + "an empty list of results."); } + auto it = std::max_element( + m_indices_dists.begin(), + m_indices_dists.end(), + IndexDist_Sorter()); + return *it; } - /** @} */ - - /** @addtogroup metric_grp Metric (distance) classes - * @{ */ - struct Metric + void sort() { - }; + std::sort( + m_indices_dists.begin(), + m_indices_dists.end(), + IndexDist_Sorter()); + } +}; - /** Manhattan distance functor (generic version, optimized for - * high-dimensionality data sets). Corresponding distance traits: - * nanoflann::metric_L1 \tparam T Type of the elements (e.g. double, float, - * uint8_t) \tparam _DistanceType Type of distance variables (must be signed) - * (e.g. float, double, int64_t) - */ - template - struct L1_Adaptor - { - typedef T ElementType; - typedef _DistanceType DistanceType; +/** @} */ - const DataSource & data_source; +/** @addtogroup loadsave_grp Load/save auxiliary functions + * @{ */ +template +void save_value(std::ostream& stream, const T& value) +{ + stream.write(reinterpret_cast(&value), sizeof(T)); +} - L1_Adaptor(const DataSource & _data_source) : data_source(_data_source) {} +template +void save_value(std::ostream& stream, const std::vector& value) +{ + size_t size = value.size(); + stream.write(reinterpret_cast(&size), sizeof(size_t)); + stream.write(reinterpret_cast(value.data()), sizeof(T) * size); +} - inline DistanceType evalMetric(const T * a, const size_t b_idx, size_t size, DistanceType worst_dist = -1) const - { - DistanceType result = DistanceType(); - const T * last = a + size; - const T * lastgroup = last - 3; - size_t d = 0; +template +void load_value(std::istream& stream, T& value) +{ + stream.read(reinterpret_cast(&value), sizeof(T)); +} + +template +void load_value(std::istream& stream, std::vector& value) +{ + size_t size; + stream.read(reinterpret_cast(&size), sizeof(size_t)); + value.resize(size); + stream.read(reinterpret_cast(value.data()), sizeof(T) * size); +} +/** @} */ + +/** @addtogroup metric_grp Metric (distance) classes + * @{ */ + +struct Metric +{ +}; + +/** Manhattan distance functor (generic version, optimized for + * high-dimensionality data sets). Corresponding distance traits: + * nanoflann::metric_L1 + * + * \tparam T Type of the elements (e.g. double, float, uint8_t) + * \tparam DataSource Source of the data, i.e. where the vectors are stored + * \tparam _DistanceType Type of distance variables (must be signed) + * \tparam IndexType Type of the arguments with which the data can be + * accessed (e.g. float, double, int64_t, T*) + */ +template< + class T, + class DataSource, + typename _DistanceType = T, + typename IndexType = size_t> +struct L1_Adaptor +{ + using ElementType = T; + using DistanceType = _DistanceType; + + const DataSource& data_source; + + L1_Adaptor(const DataSource& _data_source) : data_source(_data_source) {} - /* Process 4 items with each loop for efficiency. */ - while(a < lastgroup) + inline DistanceType evalMetric( + const T* NANOFLANN_RESTRICT a, + const IndexType b_idx, + size_t size, + DistanceType worst_dist = -1) const + { + DistanceType result = DistanceType(); + const size_t multof4 = (size >> 2) + << 2; // largest multiple of 4, i.e. 1 << 2 + size_t d; + + /* Process 4 items with each loop for efficiency. */ + if (worst_dist <= 0) + { + /* No checks with worst_dist. */ + for (d = 0; d < multof4; d += 4) + { + const DistanceType diff0 = std::abs( + a[d + 0] - data_source.kdtree_get_pt(b_idx, d + 0)); + const DistanceType diff1 = std::abs( + a[d + 1] - data_source.kdtree_get_pt(b_idx, d + 1)); + const DistanceType diff2 = std::abs( + a[d + 2] - data_source.kdtree_get_pt(b_idx, d + 2)); + const DistanceType diff3 = std::abs( + a[d + 3] - data_source.kdtree_get_pt(b_idx, d + 3)); + /* Parentheses break dependency chain: */ + result += (diff0 + diff1) + (diff2 + diff3); + } + } + else + { + /* Check with worst_dist. */ + for (d = 0; d < multof4; d += 4) { - const DistanceType diff0 = std::abs(a[0] - data_source.kdtree_get_pt(b_idx, d++)); - const DistanceType diff1 = std::abs(a[1] - data_source.kdtree_get_pt(b_idx, d++)); - const DistanceType diff2 = std::abs(a[2] - data_source.kdtree_get_pt(b_idx, d++)); - const DistanceType diff3 = std::abs(a[3] - data_source.kdtree_get_pt(b_idx, d++)); - result += diff0 + diff1 + diff2 + diff3; - a += 4; - if((worst_dist > 0) && (result > worst_dist)) + const DistanceType diff0 = std::abs( + a[d + 0] - data_source.kdtree_get_pt(b_idx, d + 0)); + const DistanceType diff1 = std::abs( + a[d + 1] - data_source.kdtree_get_pt(b_idx, d + 1)); + const DistanceType diff2 = std::abs( + a[d + 2] - data_source.kdtree_get_pt(b_idx, d + 2)); + const DistanceType diff3 = std::abs( + a[d + 3] - data_source.kdtree_get_pt(b_idx, d + 3)); + /* Parentheses break dependency chain: */ + result += (diff0 + diff1) + (diff2 + diff3); + if (result > worst_dist) { return result; } } - /* Process last 0-3 components. Not needed for standard vector lengths. */ - while(a < last) { result += std::abs(*a++ - data_source.kdtree_get_pt(b_idx, d++)); } - return result; - } - - template - inline DistanceType accum_dist(const U a, const V b, const size_t) const - { - return std::abs(a - b); } - }; + /* Process last 0-3 components. Unrolled loop with fall-through switch. + */ + switch (size - multof4) + { + case 3: + result += std::abs( + a[d + 2] - data_source.kdtree_get_pt(b_idx, d + 2)); + case 2: + result += std::abs( + a[d + 1] - data_source.kdtree_get_pt(b_idx, d + 1)); + case 1: + result += std::abs( + a[d + 0] - data_source.kdtree_get_pt(b_idx, d + 0)); + case 0: + break; + } + return result; + } - /** Squared Euclidean distance functor (generic version, optimized for - * high-dimensionality data sets). Corresponding distance traits: - * nanoflann::metric_L2 \tparam T Type of the elements (e.g. double, float, - * uint8_t) \tparam _DistanceType Type of distance variables (must be signed) - * (e.g. float, double, int64_t) - */ - template - struct L2_Adaptor + template + inline DistanceType accum_dist(const U a, const V b, const size_t) const { - typedef T ElementType; - typedef _DistanceType DistanceType; + return std::abs(a - b); + } +}; + +/** **Squared** Euclidean distance functor (generic version, optimized for + * high-dimensionality data sets). Corresponding distance traits: + * nanoflann::metric_L2 + * + * \tparam T Type of the elements (e.g. double, float, uint8_t) + * \tparam DataSource Source of the data, i.e. where the vectors are stored + * \tparam _DistanceType Type of distance variables (must be signed) + * \tparam IndexType Type of the arguments with which the data can be + * accessed (e.g. float, double, int64_t, T*) + */ +template< + class T, + class DataSource, + typename _DistanceType = T, + typename IndexType = size_t> +struct L2_Adaptor +{ + using ElementType = T; + using DistanceType = _DistanceType; - const DataSource & data_source; + const DataSource& data_source; - L2_Adaptor(const DataSource & _data_source) : data_source(_data_source) {} + L2_Adaptor(const DataSource& _data_source) : data_source(_data_source) {} - inline DistanceType evalMetric(const T * a, const size_t b_idx, size_t size, DistanceType worst_dist = -1) const - { - DistanceType result = DistanceType(); - const T * last = a + size; - const T * lastgroup = last - 3; - size_t d = 0; + inline DistanceType evalMetric( + const T* NANOFLANN_RESTRICT a, + const IndexType b_idx, + size_t size, + DistanceType worst_dist = -1) const + { + DistanceType result = DistanceType(); + const size_t multof4 = (size >> 2) + << 2; // largest multiple of 4, i.e. 1 << 2 + size_t d; - /* Process 4 items with each loop for efficiency. */ - while(a < lastgroup) + /* Process 4 items with each loop for efficiency. */ + if (worst_dist <= 0) + { + /* No checks with worst_dist. */ + for (d = 0; d < multof4; d += 4) + { + const DistanceType diff0 = + a[d + 0] - data_source.kdtree_get_pt(b_idx, d + 0); + const DistanceType diff1 = + a[d + 1] - data_source.kdtree_get_pt(b_idx, d + 1); + const DistanceType diff2 = + a[d + 2] - data_source.kdtree_get_pt(b_idx, d + 2); + const DistanceType diff3 = + a[d + 3] - data_source.kdtree_get_pt(b_idx, d + 3); + /* Parentheses break dependency chain: */ + result += (diff0 * diff0 + diff1 * diff1) + + (diff2 * diff2 + diff3 * diff3); + } + } + else + { + /* Check with worst_dist. */ + for (d = 0; d < multof4; d += 4) { - const DistanceType diff0 = a[0] - data_source.kdtree_get_pt(b_idx, d++); - const DistanceType diff1 = a[1] - data_source.kdtree_get_pt(b_idx, d++); - const DistanceType diff2 = a[2] - data_source.kdtree_get_pt(b_idx, d++); - const DistanceType diff3 = a[3] - data_source.kdtree_get_pt(b_idx, d++); - result += diff0 * diff0 + diff1 * diff1 + diff2 * diff2 + diff3 * diff3; - a += 4; - if((worst_dist > 0) && (result > worst_dist)) + const DistanceType diff0 = + a[d + 0] - data_source.kdtree_get_pt(b_idx, d + 0); + const DistanceType diff1 = + a[d + 1] - data_source.kdtree_get_pt(b_idx, d + 1); + const DistanceType diff2 = + a[d + 2] - data_source.kdtree_get_pt(b_idx, d + 2); + const DistanceType diff3 = + a[d + 3] - data_source.kdtree_get_pt(b_idx, d + 3); + /* Parentheses break dependency chain: */ + result += (diff0 * diff0 + diff1 * diff1) + + (diff2 * diff2 + diff3 * diff3); + if (result > worst_dist) { return result; } } - /* Process last 0-3 components. Not needed for standard vector lengths. */ - while(a < last) - { - const DistanceType diff0 = *a++ - data_source.kdtree_get_pt(b_idx, d++); - result += diff0 * diff0; - } - return result; } - - template - inline DistanceType accum_dist(const U a, const V b, const size_t) const + /* Process last 0-3 components. Unrolled loop with fall-through switch. + */ + DistanceType diff; + switch (size - multof4) { - return (a - b) * (a - b); + case 3: + diff = a[d + 2] - data_source.kdtree_get_pt(b_idx, d + 2); + result += diff * diff; + case 2: + diff = a[d + 1] - data_source.kdtree_get_pt(b_idx, d + 1); + result += diff * diff; + case 1: + diff = a[d + 0] - data_source.kdtree_get_pt(b_idx, d + 0); + result += diff * diff; + case 0: + break; } - }; + return result; + } - /** Squared Euclidean (L2) distance functor (suitable for low-dimensionality - * datasets, like 2D or 3D point clouds) Corresponding distance traits: - * nanoflann::metric_L2_Simple \tparam T Type of the elements (e.g. double, - * float, uint8_t) \tparam _DistanceType Type of distance variables (must be - * signed) (e.g. float, double, int64_t) - */ - template - struct L2_Simple_Adaptor + template + inline DistanceType accum_dist(const U a, const V b, const size_t) const { - typedef T ElementType; - typedef _DistanceType DistanceType; + auto diff = a - b; + return diff * diff; + } +}; - const DataSource & data_source; +/** **Squared** Euclidean (L2) distance functor (suitable for low-dimensionality + * datasets, like 2D or 3D point clouds) Corresponding distance traits: + * nanoflann::metric_L2_Simple + * + * \tparam T Type of the elements (e.g. double, float, uint8_t) + * \tparam DataSource Source of the data, i.e. where the vectors are stored + * \tparam _DistanceType Type of distance variables (must be signed) + * \tparam IndexType Type of the arguments with which the data can be + * accessed (e.g. float, double, int64_t, T*) + */ +template< + class T, + class DataSource, + typename _DistanceType = T, + typename IndexType = size_t> +struct L2_Simple_Adaptor +{ + using ElementType = T; + using DistanceType = _DistanceType; - L2_Simple_Adaptor(const DataSource & _data_source) : data_source(_data_source) {} + const DataSource& data_source; - inline DistanceType evalMetric(const T * a, const size_t b_idx, size_t size) const - { - DistanceType result = DistanceType(); - for(size_t i = 0; i < size; ++i) - { - const DistanceType diff = a[i] - data_source.kdtree_get_pt(b_idx, i); - result += diff * diff; - } - return result; - } + L2_Simple_Adaptor(const DataSource& _data_source) : + data_source(_data_source) + { + } - template - inline DistanceType accum_dist(const U a, const V b, const size_t) const + inline DistanceType + evalMetric(const T* a, const IndexType b_idx, size_t size) const + { + DistanceType result = DistanceType(); + for (size_t i = 0; i < size; ++i) { - return (a - b) * (a - b); + const DistanceType diff = + a[i] - data_source.kdtree_get_pt(b_idx, i); + result += diff * diff; } - }; + return result; + } - /** SO2 distance functor - * Corresponding distance traits: nanoflann::metric_SO2 - * \tparam T Type of the elements (e.g. double, float) - * \tparam _DistanceType Type of distance variables (must be signed) (e.g. - * float, double) orientation is constrained to be in [-pi, pi] - */ - template - struct SO2_Adaptor + template + inline DistanceType accum_dist(const U a, const V b, const size_t) const { - typedef T ElementType; - typedef _DistanceType DistanceType; + auto diff = a - b; + return diff * diff; + } +}; - const DataSource & data_source; +/** SO2 distance functor + * Corresponding distance traits: nanoflann::metric_SO2 + * + * \tparam T Type of the elements (e.g. double, float, uint8_t) + * \tparam DataSource Source of the data, i.e. where the vectors are stored + * \tparam _DistanceType Type of distance variables (must be signed) (e.g. + * float, double) orientation is constrained to be in [-pi, pi] + * \tparam IndexType Type of the arguments with which the data can be + * accessed (e.g. float, double, int64_t, T*) + */ +template< + class T, + class DataSource, + typename _DistanceType = T, + typename IndexType = size_t> +struct SO2_Adaptor +{ + using ElementType = T; + using DistanceType = _DistanceType; + + const DataSource& data_source; - SO2_Adaptor(const DataSource & _data_source) : data_source(_data_source) {} + SO2_Adaptor(const DataSource& _data_source) : data_source(_data_source) {} - inline DistanceType evalMetric(const T * a, const size_t b_idx, size_t size) const + inline DistanceType + evalMetric(const T* a, const IndexType b_idx, size_t size) const + { + return accum_dist( + a[size - 1], + data_source.kdtree_get_pt(b_idx, size - 1), + size - 1); + } + + /** Note: this assumes that input angles are already in the range [-pi,pi] + */ + template + inline DistanceType accum_dist(const U a, const V b, const size_t) const + { + DistanceType result = DistanceType(); + DistanceType PI = pi_const(); + result = b - a; + if (result > PI) { - return accum_dist(a[size - 1], data_source.kdtree_get_pt(b_idx, size - 1), size - 1); + result -= 2 * PI; } - - /** Note: this assumes that input angles are already in the range [-pi,pi] */ - template - inline DistanceType accum_dist(const U a, const V b, const size_t) const + else if (result < -PI) { - DistanceType result = DistanceType(); - DistanceType PI = pi_const(); - result = b - a; - if(result > PI) - result -= 2 * PI; - else if(result < -PI) - result += 2 * PI; - return result; + result += 2 * PI; } - }; + return result; + } +}; - /** SO3 distance functor (Uses L2_Simple) - * Corresponding distance traits: nanoflann::metric_SO3 - * \tparam T Type of the elements (e.g. double, float) - * \tparam _DistanceType Type of distance variables (must be signed) (e.g. - * float, double) - */ - template - struct SO3_Adaptor - { - typedef T ElementType; - typedef _DistanceType DistanceType; +/** SO3 distance functor (Uses L2_Simple) + * Corresponding distance traits: nanoflann::metric_SO3 + * + * \tparam T Type of the elements (e.g. double, float, uint8_t) + * \tparam DataSource Source of the data, i.e. where the vectors are stored + * \tparam _DistanceType Type of distance variables (must be signed) (e.g. + * float, double) + * \tparam IndexType Type of the arguments with which the data can be + * accessed (e.g. float, double, int64_t, T*) + */ +template< + class T, + class DataSource, + typename _DistanceType = T, + typename IndexType = size_t> +struct SO3_Adaptor +{ + using ElementType = T; + using DistanceType = _DistanceType; - L2_Simple_Adaptor distance_L2_Simple; + L2_Simple_Adaptor + distance_L2_Simple; - SO3_Adaptor(const DataSource & _data_source) : distance_L2_Simple(_data_source) {} + SO3_Adaptor(const DataSource& _data_source) : + distance_L2_Simple(_data_source) + { + } - inline DistanceType evalMetric(const T * a, const size_t b_idx, size_t size) const - { - return distance_L2_Simple.evalMetric(a, b_idx, size); - } + inline DistanceType + evalMetric(const T* a, const IndexType b_idx, size_t size) const + { + return distance_L2_Simple.evalMetric(a, b_idx, size); + } - template - inline DistanceType accum_dist(const U a, const V b, const size_t idx) const - { - return distance_L2_Simple.accum_dist(a, b, idx); - } - }; + template + inline DistanceType accum_dist(const U a, const V b, const size_t idx) const + { + return distance_L2_Simple.accum_dist(a, b, idx); + } +}; - /** Metaprogramming helper traits class for the L1 (Manhattan) metric */ - struct metric_L1 : public Metric +/** Metaprogramming helper traits class for the L1 (Manhattan) metric */ +struct metric_L1 : public Metric +{ + template + struct traits { - template - struct traits - { - typedef L1_Adaptor distance_t; - }; + using distance_t = L1_Adaptor; }; - /** Metaprogramming helper traits class for the L2 (Euclidean) metric */ - struct metric_L2 : public Metric +}; +/** Metaprogramming helper traits class for the L2 (Euclidean) **squared** + * distance metric */ +struct metric_L2 : public Metric +{ + template + struct traits { - template - struct traits - { - typedef L2_Adaptor distance_t; - }; + using distance_t = L2_Adaptor; }; - /** Metaprogramming helper traits class for the L2_simple (Euclidean) metric */ - struct metric_L2_Simple : public Metric +}; +/** Metaprogramming helper traits class for the L2_simple (Euclidean) + * **squared** distance metric */ +struct metric_L2_Simple : public Metric +{ + template + struct traits { - template - struct traits - { - typedef L2_Simple_Adaptor distance_t; - }; + using distance_t = L2_Simple_Adaptor; }; - /** Metaprogramming helper traits class for the SO3_InnerProdQuat metric */ - struct metric_SO2 : public Metric +}; +/** Metaprogramming helper traits class for the SO3_InnerProdQuat metric */ +struct metric_SO2 : public Metric +{ + template + struct traits { - template - struct traits - { - typedef SO2_Adaptor distance_t; - }; + using distance_t = SO2_Adaptor; }; - /** Metaprogramming helper traits class for the SO3_InnerProdQuat metric */ - struct metric_SO3 : public Metric +}; +/** Metaprogramming helper traits class for the SO3_InnerProdQuat metric */ +struct metric_SO3 : public Metric +{ + template + struct traits { - template - struct traits - { - typedef SO3_Adaptor distance_t; - }; + using distance_t = SO3_Adaptor; }; +}; - /** @} */ +/** @} */ - /** @addtogroup param_grp Parameter structs - * @{ */ +/** @addtogroup param_grp Parameter structs + * @{ */ - /** Parameters (see README.md) */ - struct KDTreeSingleIndexAdaptorParams - { - KDTreeSingleIndexAdaptorParams(size_t _leaf_max_size = 10) : leaf_max_size(_leaf_max_size) {} +enum class KDTreeSingleIndexAdaptorFlags +{ + None = 0, + SkipInitialBuildIndex = 1 +}; - size_t leaf_max_size; - }; +inline std::underlying_type::type operator&( + KDTreeSingleIndexAdaptorFlags lhs, + KDTreeSingleIndexAdaptorFlags rhs) +{ + using underlying = + typename std::underlying_type::type; + return static_cast(lhs) & static_cast(rhs); +} - /** Search options for KDTreeSingleIndexAdaptor::findNeighbors() */ - struct SearchParams +/** Parameters (see README.md) */ +struct KDTreeSingleIndexAdaptorParams +{ + KDTreeSingleIndexAdaptorParams( + size_t _leaf_max_size = 10, + KDTreeSingleIndexAdaptorFlags _flags = + KDTreeSingleIndexAdaptorFlags::None, + unsigned int _n_thread_build = 1) : + leaf_max_size(_leaf_max_size), + flags(_flags), + n_thread_build(_n_thread_build) { - /** Note: The first argument (checks_IGNORED_) is ignored, but kept for - * compatibility with the FLANN interface */ - SearchParams(int checks_IGNORED_ = 32, float eps_ = 0, bool sorted_ = true) : - checks(checks_IGNORED_), eps(eps_), sorted(sorted_) - { - } + } - int checks; //!< Ignored parameter (Kept for compatibility with the FLANN - //!< interface). - float eps; //!< search for eps-approximate neighbours (default: 0) - bool sorted; //!< only for radius search, require neighbours sorted by - //!< distance (default: true) - }; - /** @} */ + size_t leaf_max_size; + KDTreeSingleIndexAdaptorFlags flags; + unsigned int n_thread_build; +}; - /** @addtogroup memalloc_grp Memory allocation - * @{ */ +/** Search options for KDTreeSingleIndexAdaptor::findNeighbors() */ +struct SearchParameters +{ + SearchParameters(float eps_ = 0, bool sorted_ = true) : + eps(eps_), + sorted(sorted_) + { + } - /** - * Allocates (using C's malloc) a generic type T. - * - * Params: - * count = number of instances to allocate. - * Returns: pointer (of type T*) to memory buffer - */ - template - inline T * allocate(size_t count = 1) + float eps; //!< search for eps-approximate neighbours (default: 0) + bool sorted; //!< only for radius search, require neighbours sorted by + //!< distance (default: true) +}; +/** @} */ + +/** @addtogroup memalloc_grp Memory allocation + * @{ */ + +/** + * Pooled storage allocator + * + * The following routines allow for the efficient allocation of storage in + * small chunks from a specified pool. Rather than allowing each structure + * to be freed individually, an entire pool of storage is freed at once. + * This method has two advantages over just using malloc() and free(). First, + * it is far more efficient for allocating small objects, as there is + * no overhead for remembering all the information needed to free each + * object or consolidating fragmented memory. Second, the decision about + * how long to keep an object is made at the time of allocation, and there + * is no need to track down all the objects to free them. + * + */ +class PooledAllocator +{ + static constexpr size_t WORDSIZE = 16; // WORDSIZE must >= 8 + static constexpr size_t BLOCKSIZE = 8192; + + /* We maintain memory alignment to word boundaries by requiring that all + allocations be in multiples of the machine wordsize. */ + /* Size of machine word in bytes. Must be power of 2. */ + /* Minimum number of bytes requested at a time from the system. Must be + * multiple of WORDSIZE. */ + + using Size = size_t; + + Size remaining_ = 0; //!< Number of bytes left in current block of storage + void* base_ = nullptr; //!< Pointer to base of current block of storage + void* loc_ = nullptr; //!< Current location in block to next allocate + + void internal_init() { - T * mem = static_cast(::malloc(sizeof(T) * count)); - return mem; + remaining_ = 0; + base_ = nullptr; + usedMemory = 0; + wastedMemory = 0; } +public: + Size usedMemory = 0; + Size wastedMemory = 0; + /** - * Pooled storage allocator - * - * The following routines allow for the efficient allocation of storage in - * small chunks from a specified pool. Rather than allowing each structure - * to be freed individually, an entire pool of storage is freed at once. - * This method has two advantages over just using malloc() and free(). First, - * it is far more efficient for allocating small objects, as there is - * no overhead for remembering all the information needed to free each - * object or consolidating fragmented memory. Second, the decision about - * how long to keep an object is made at the time of allocation, and there - * is no need to track down all the objects to free them. - * + Default constructor. Initializes a new pool. */ + PooledAllocator() { internal_init(); } - const size_t WORDSIZE = 16; - const size_t BLOCKSIZE = 8192; + /** + * Destructor. Frees all the memory allocated in this pool. + */ + ~PooledAllocator() { free_all(); } - class PooledAllocator + /** Frees all allocated memory chunks */ + void free_all() { - /* We maintain memory alignment to word boundaries by requiring that all - allocations be in multiples of the machine wordsize. */ - /* Size of machine word in bytes. Must be power of 2. */ - /* Minimum number of bytes requested at a time from the system. Must be - * multiple of WORDSIZE. */ - - size_t remaining; /* Number of bytes left in current block of storage. */ - void * base; /* Pointer to base of current block of storage. */ - void * loc; /* Current location in block to next allocate memory. */ - - void internal_init() + while (base_ != nullptr) { - remaining = 0; - base = NULL; - usedMemory = 0; - wastedMemory = 0; + // Get pointer to prev block + void* prev = *(static_cast(base_)); + ::free(base_); + base_ = prev; } + internal_init(); + } - public: - size_t usedMemory; - size_t wastedMemory; - - /** - Default constructor. Initializes a new pool. + /** + * Returns a pointer to a piece of new memory of the given size in bytes + * allocated from the pool. + */ + void* malloc(const size_t req_size) + { + /* Round size up to a multiple of wordsize. The following expression + only works for WORDSIZE that is a power of 2, by masking last bits + of incremented size to zero. */ - PooledAllocator() { internal_init(); } + const Size size = (req_size + (WORDSIZE - 1)) & ~(WORDSIZE - 1); - /** - * Destructor. Frees all the memory allocated in this pool. + /* Check whether a new block must be allocated. Note that the first + word of a block is reserved for a pointer to the previous block. */ - ~PooledAllocator() { free_all(); } - - /** Frees all allocated memory chunks */ - void free_all() + if (size > remaining_) { - while(base != NULL) + wastedMemory += remaining_; + + /* Allocate new storage. */ + const Size blocksize = + size > BLOCKSIZE ? size + WORDSIZE : BLOCKSIZE + WORDSIZE; + + // use the standard C malloc to allocate memory + void* m = ::malloc(blocksize); + if (!m) { - void * prev = *(static_cast(base)); /* Get pointer to prev block. */ - ::free(base); - base = prev; + throw std::bad_alloc(); } - internal_init(); - } - /** - * Returns a pointer to a piece of new memory of the given size in bytes - * allocated from the pool. - */ - void * malloc(const size_t req_size) - { - /* Round size up to a multiple of wordsize. The following expression - only works for WORDSIZE that is a power of 2, by masking last bits of - incremented size to zero. - */ - const size_t size = (req_size + (WORDSIZE - 1)) & ~(WORDSIZE - 1); - - /* Check whether a new block must be allocated. Note that the first word - of a block is reserved for a pointer to the previous block. - */ - if(size > remaining) - { - wastedMemory += remaining; + /* Fill first word of new block with pointer to previous block. */ + static_cast(m)[0] = base_; + base_ = m; - /* Allocate new storage. */ - const size_t blocksize = (size + sizeof(void *) + (WORDSIZE - 1) > BLOCKSIZE) - ? size + sizeof(void *) + (WORDSIZE - 1) - : BLOCKSIZE; + remaining_ = blocksize - WORDSIZE; + loc_ = static_cast(m) + WORDSIZE; + } + void* rloc = loc_; + loc_ = static_cast(loc_) + size; + remaining_ -= size; - // use the standard C malloc to allocate memory - void * m = ::malloc(blocksize); - if(!m) - { - fprintf(stderr, "Failed to allocate memory.\n"); - throw std::bad_alloc(); - } + usedMemory += size; - /* Fill first word of new block with pointer to previous block. */ - static_cast(m)[0] = base; - base = m; + return rloc; + } - size_t shift = 0; - // int size_t = (WORDSIZE - ( (((size_t)m) + sizeof(void*)) & - // (WORDSIZE-1))) & (WORDSIZE-1); + /** + * Allocates (using this pool) a generic type T. + * + * Params: + * count = number of instances to allocate. + * Returns: pointer (of type T*) to memory buffer + */ + template + T* allocate(const size_t count = 1) + { + T* mem = static_cast(this->malloc(sizeof(T) * count)); + return mem; + } +}; +/** @} */ - remaining = blocksize - sizeof(void *) - shift; - loc = (static_cast(m) + sizeof(void *) + shift); - } - void * rloc = loc; - loc = static_cast(loc) + size; - remaining -= size; +/** @addtogroup nanoflann_metaprog_grp Auxiliary metaprogramming stuff + * @{ */ - usedMemory += size; +/** Used to declare fixed-size arrays when DIM>0, dynamically-allocated vectors + * when DIM=-1. Fixed size version for a generic DIM: + */ +template +struct array_or_vector +{ + using type = std::array; +}; +/** Dynamic size version */ +template +struct array_or_vector<-1, T> +{ + using type = std::vector; +}; - return rloc; - } +/** @} */ - /** - * Allocates (using this pool) a generic type T. - * - * Params: - * count = number of instances to allocate. - * Returns: pointer (of type T*) to memory buffer - */ - template - T * allocate(const size_t count = 1) - { - T * mem = static_cast(this->malloc(sizeof(T) * count)); - return mem; - } - }; - /** @} */ +/** kd-tree base-class + * + * Contains the member functions common to the classes KDTreeSingleIndexAdaptor + * and KDTreeSingleIndexDynamicAdaptor_. + * + * \tparam Derived The name of the class which inherits this class. + * \tparam DatasetAdaptor The user-provided adaptor, which must be ensured to + * have a lifetime equal or longer than the instance of this class. + * \tparam Distance The distance metric to use, these are all classes derived + * from nanoflann::Metric + * \tparam DIM Dimensionality of data points (e.g. 3 for 3D points) + * \tparam IndexType Type of the arguments with which the data can be + * accessed (e.g. float, double, int64_t, T*) + */ +template< + class Derived, + typename Distance, + class DatasetAdaptor, + int32_t DIM = -1, + typename index_t = uint32_t> +class KDTreeBaseClass +{ +public: + /** Frees the previously-built index. Automatically called within + * buildIndex(). */ + void freeIndex(Derived& obj) + { + obj.pool_.free_all(); + obj.root_node_ = nullptr; + obj.size_at_index_build_ = 0; + } - /** @addtogroup nanoflann_metaprog_grp Auxiliary metaprogramming stuff - * @{ */ + using ElementType = typename Distance::ElementType; + using DistanceType = typename Distance::DistanceType; + using IndexType = index_t; - /** Used to declare fixed-size arrays when DIM>0, dynamically-allocated vectors - * when DIM=-1. Fixed size version for a generic DIM: + /** + * Array of indices to vectors in the dataset_. */ - template - struct array_or_vector_selector - { - typedef std::array container_t; - }; - /** Dynamic size version */ - template - struct array_or_vector_selector<-1, T> - { - typedef std::vector container_t; - }; + std::vector vAcc_; - /** @} */ + using Offset = typename decltype(vAcc_)::size_type; + using Size = typename decltype(vAcc_)::size_type; + using Dimension = int32_t; - /** kd-tree base-class + /*------------------------------------------------------------------- + * Internal Data Structures * - * Contains the member functions common to the classes KDTreeSingleIndexAdaptor - * and KDTreeSingleIndexDynamicAdaptor_. + * "Node" below can be declared with alignas(N) to improve + * cache friendliness and SIMD load/store performance. * - * \tparam Derived The name of the class which inherits this class. - * \tparam DatasetAdaptor The user-provided adaptor (see comments above). - * \tparam Distance The distance metric to use, these are all classes derived - * from nanoflann::Metric \tparam DIM Dimensionality of data points (e.g. 3 for - * 3D points) \tparam IndexType Will be typically size_t or int - */ - - template - class KDTreeBaseClass + * The optimal N depends on the underlying hardware: + * + Intel x86-64: 16 for SSE, 32 for AVX/AVX2 and 64 for AVX-512 + * + NVIDIA Jetson: 16 for ARM + NEON and CUDA float4/ + * To avoid unnecessary padding, the smallest alignment + * compatible with a platform's vector width should be chosen. + * ------------------------------------------------------------------*/ + struct alignas(NANOFLANN_NODE_ALIGNMENT) Node { - public: - /** Frees the previously-built index. Automatically called within - * buildIndex(). */ - void freeIndex(Derived & obj) - { - obj.pool.free_all(); - obj.root_node = NULL; - obj.m_size_at_index_build = 0; - } - - typedef typename Distance::ElementType ElementType; - typedef typename Distance::DistanceType DistanceType; - - /*--------------------- Internal Data Structures --------------------------*/ - struct Node + /** Union used because a node can be either a LEAF node or a non-leaf + * node, so both data fields are never used simultaneously */ + union { - /** Union used because a node can be either a LEAF node or a non-leaf node, - * so both data fields are never used simultaneously */ - union + struct leaf { - struct leaf - { - IndexType left, right; //!< Indices of points in leaf node - } lr; - struct nonleaf - { - int divfeat; //!< Dimension used for subdivision. - DistanceType divlow, divhigh; //!< The values used for subdivision. - } sub; - } node_type; - Node *child1, *child2; //!< Child nodes (both=NULL mean its a leaf node) - }; - - typedef Node * NodePtr; - - struct Interval - { - ElementType low, high; - }; + Offset left, right; //!< Indices of points in leaf node + } lr; + struct nonleaf + { + Dimension divfeat; //!< Dimension used for subdivision. + /// The values used for subdivision. + DistanceType divlow, divhigh; + } sub; + } node_type; + + /** Child nodes (both=nullptr mean its a leaf node) */ + Node *child1 = nullptr, *child2 = nullptr; + }; - /** - * Array of indices to vectors in the dataset. - */ - std::vector vind; + using NodePtr = Node*; + using NodeConstPtr = const Node*; - NodePtr root_node; + struct Interval + { + ElementType low, high; + }; - size_t m_leaf_max_size; + NodePtr root_node_ = nullptr; - size_t m_size; //!< Number of current points in the dataset - size_t m_size_at_index_build; //!< Number of points in the dataset when the - //!< index was built - int dim; //!< Dimensionality of each data point + Size leaf_max_size_ = 0; - /** Define "BoundingBox" as a fixed-size or variable-size container depending - * on "DIM" */ - typedef typename array_or_vector_selector::container_t BoundingBox; + /// Number of thread for concurrent tree build + Size n_thread_build_ = 1; + /// Number of current points in the dataset + Size size_ = 0; + /// Number of points in the dataset when the index was built + Size size_at_index_build_ = 0; + Dimension dim_ = 0; //!< Dimensionality of each data point - /** Define "distance_vector_t" as a fixed-size or variable-size container - * depending on "DIM" */ - typedef typename array_or_vector_selector::container_t distance_vector_t; + /** Define "BoundingBox" as a fixed-size or variable-size container + * depending on "DIM" */ + using BoundingBox = typename array_or_vector::type; - /** The KD-tree used to find neighbours */ + /** Define "distance_vector_t" as a fixed-size or variable-size container + * depending on "DIM" */ + using distance_vector_t = typename array_or_vector::type; - BoundingBox root_bbox; + /** The KD-tree used to find neighbours */ + BoundingBox root_bbox_; - /** - * Pooled memory allocator. - * - * Using a pooled memory allocator is more efficient - * than allocating memory directly when there is a large - * number small of memory allocations. - */ - PooledAllocator pool; + /** + * Pooled memory allocator. + * + * Using a pooled memory allocator is more efficient + * than allocating memory directly when there is a large + * number small of memory allocations. + */ + PooledAllocator pool_; - /** Returns number of points in dataset */ - size_t size(const Derived & obj) const { return obj.m_size; } + /** Returns number of points in dataset */ + Size size(const Derived& obj) const { return obj.size_; } - /** Returns the length of each point in the dataset */ - size_t veclen(const Derived & obj) { return static_cast(DIM > 0 ? DIM : obj.dim); } + /** Returns the length of each point in the dataset */ + Size veclen(const Derived& obj) const { return DIM > 0 ? DIM : obj.dim_; } - /// Helper accessor to the dataset points: - inline ElementType dataset_get(const Derived & obj, size_t idx, int component) const - { - return obj.dataset.kdtree_get_pt(idx, component); - } + /// Helper accessor to the dataset points: + ElementType dataset_get( + const Derived& obj, + IndexType element, + Dimension component) const + { + return obj.dataset_.kdtree_get_pt(element, component); + } - /** - * Computes the inde memory usage - * Returns: memory used by the index - */ - size_t usedMemory(Derived & obj) - { - return obj.pool.usedMemory + obj.pool.wastedMemory + - obj.dataset.kdtree_get_point_count() * sizeof(IndexType); // pool memory and vind array memory - } + /** + * Computes the index memory usage + * Returns: memory used by the index + */ + Size usedMemory(const Derived& obj) const + { + return obj.pool_.usedMemory + obj.pool_.wastedMemory + + obj.dataset_.kdtree_get_point_count() * + sizeof(IndexType); // pool memory and vind array memory + } - void computeMinMax(const Derived & obj, IndexType * ind, IndexType count, int element, ElementType & min_elem, - ElementType & max_elem) + /** + * Compute the minimum and maximum element values in the specified dimension + */ + void computeMinMax( + const Derived& obj, + Offset ind, + Size count, + Dimension element, + ElementType& min_elem, + ElementType& max_elem) const + { + min_elem = dataset_get(obj, vAcc_[ind], element); + max_elem = min_elem; + for (Offset i = 1; i < count; ++i) { - min_elem = dataset_get(obj, ind[0], element); - max_elem = dataset_get(obj, ind[0], element); - for(IndexType i = 1; i < count; ++i) + ElementType val = dataset_get(obj, vAcc_[ind + i], element); + if (val < min_elem) + { + min_elem = val; + } + if (val > max_elem) { - ElementType val = dataset_get(obj, ind[i], element); - if(val < min_elem) - min_elem = val; - if(val > max_elem) - max_elem = val; + max_elem = val; } } + } - /** - * Create a tree node that subdivides the list of vecs from vind[first] - * to vind[last]. The routine is called recursively on each sublist. - * - * @param left index of the first vector - * @param right index of the last vector - */ - NodePtr divideTree(Derived & obj, const IndexType left, const IndexType right, BoundingBox & bbox) + /** + * Create a tree node that subdivides the list of vecs from vind[first] + * to vind[last]. The routine is called recursively on each sublist. + * + * @param left index of the first vector + * @param right index of the last vector + * @param bbox bounding box used as input for splitting and output for + * parent node + */ + NodePtr divideTree( + Derived& obj, + const Offset left, + const Offset right, + BoundingBox& bbox) + { + assert(left < obj.dataset_.kdtree_get_point_count()); + + NodePtr node = obj.pool_.template allocate(); // allocate memory + const auto dims = (DIM > 0 ? DIM : obj.dim_); + + /* If too few exemplars remain, then make this a leaf node. */ + if ((right - left) <= static_cast(obj.leaf_max_size_)) { - NodePtr node = obj.pool.template allocate(); // allocate memory + node->child1 = node->child2 = nullptr; /* Mark as leaf node. */ + node->node_type.lr.left = left; + node->node_type.lr.right = right; - /* If too few exemplars remain, then make this a leaf node. */ - if((right - left) <= static_cast(obj.m_leaf_max_size)) + // compute bounding-box of leaf points + for (Dimension i = 0; i < dims; ++i) { - node->child1 = node->child2 = NULL; /* Mark as leaf node. */ - node->node_type.lr.left = left; - node->node_type.lr.right = right; - - // compute bounding-box of leaf points - for(int i = 0; i < (DIM > 0 ? DIM : obj.dim); ++i) - { - bbox[i].low = dataset_get(obj, obj.vind[left], i); - bbox[i].high = dataset_get(obj, obj.vind[left], i); - } - for(IndexType k = left + 1; k < right; ++k) + bbox[i].low = dataset_get(obj, obj.vAcc_[left], i); + bbox[i].high = dataset_get(obj, obj.vAcc_[left], i); + } + for (Offset k = left + 1; k < right; ++k) + { + for (Dimension i = 0; i < dims; ++i) { - for(int i = 0; i < (DIM > 0 ? DIM : obj.dim); ++i) + const auto val = dataset_get(obj, obj.vAcc_[k], i); + if (bbox[i].low > val) + { + bbox[i].low = val; + } + if (bbox[i].high < val) { - if(bbox[i].low > dataset_get(obj, obj.vind[k], i)) - bbox[i].low = dataset_get(obj, obj.vind[k], i); - if(bbox[i].high < dataset_get(obj, obj.vind[k], i)) - bbox[i].high = dataset_get(obj, obj.vind[k], i); + bbox[i].high = val; } } } - else - { - IndexType idx; - int cutfeat; - DistanceType cutval; - middleSplit_(obj, &obj.vind[0] + left, right - left, idx, cutfeat, cutval, bbox); + } + else + { + /* Determine the index, dimension and value for split plane */ + Offset idx; + Dimension cutfeat; + DistanceType cutval; + middleSplit_(obj, left, right - left, idx, cutfeat, cutval, bbox); - node->node_type.sub.divfeat = cutfeat; + node->node_type.sub.divfeat = cutfeat; - BoundingBox left_bbox(bbox); - left_bbox[cutfeat].high = cutval; - node->child1 = divideTree(obj, left, left + idx, left_bbox); + /* Recurse on left */ + BoundingBox left_bbox(bbox); + left_bbox[cutfeat].high = cutval; + node->child1 = this->divideTree(obj, left, left + idx, left_bbox); - BoundingBox right_bbox(bbox); - right_bbox[cutfeat].low = cutval; - node->child2 = divideTree(obj, left + idx, right, right_bbox); + /* Recurse on right */ + BoundingBox right_bbox(bbox); + right_bbox[cutfeat].low = cutval; + node->child2 = this->divideTree(obj, left + idx, right, right_bbox); - node->node_type.sub.divlow = left_bbox[cutfeat].high; - node->node_type.sub.divhigh = right_bbox[cutfeat].low; + node->node_type.sub.divlow = left_bbox[cutfeat].high; + node->node_type.sub.divhigh = right_bbox[cutfeat].low; - for(int i = 0; i < (DIM > 0 ? DIM : obj.dim); ++i) - { - bbox[i].low = std::min(left_bbox[i].low, right_bbox[i].low); - bbox[i].high = std::max(left_bbox[i].high, right_bbox[i].high); - } + for (Dimension i = 0; i < dims; ++i) + { + bbox[i].low = std::min(left_bbox[i].low, right_bbox[i].low); + bbox[i].high = std::max(left_bbox[i].high, right_bbox[i].high); } - - return node; } - void middleSplit_(Derived & obj, IndexType * ind, IndexType count, IndexType & index, int & cutfeat, - DistanceType & cutval, const BoundingBox & bbox) + return node; + } + + /** + * Create a tree node that subdivides the list of vecs from vind[first] to + * vind[last] concurrently. The routine is called recursively on each + * sublist. + * + * @param left index of the first vector + * @param right index of the last vector + * @param bbox bounding box used as input for splitting and output for + * parent node + * @param thread_count count of std::async threads + * @param mutex mutex for mempool allocation + */ + NodePtr divideTreeConcurrent( + Derived& obj, + const Offset left, + const Offset right, + BoundingBox& bbox, + std::atomic& thread_count, + std::mutex& mutex) + { + std::unique_lock lock(mutex); + NodePtr node = obj.pool_.template allocate(); // allocate memory + lock.unlock(); + + const auto dims = (DIM > 0 ? DIM : obj.dim_); + + /* If too few exemplars remain, then make this a leaf node. */ + if ((right - left) <= static_cast(obj.leaf_max_size_)) { - const DistanceType EPS = static_cast(0.00001); - ElementType max_span = bbox[0].high - bbox[0].low; - for(int i = 1; i < (DIM > 0 ? DIM : obj.dim); ++i) + node->child1 = node->child2 = nullptr; /* Mark as leaf node. */ + node->node_type.lr.left = left; + node->node_type.lr.right = right; + + // compute bounding-box of leaf points + for (Dimension i = 0; i < dims; ++i) { - ElementType span = bbox[i].high - bbox[i].low; - if(span > max_span) - { - max_span = span; - } + bbox[i].low = dataset_get(obj, obj.vAcc_[left], i); + bbox[i].high = dataset_get(obj, obj.vAcc_[left], i); } - ElementType max_spread = -1; - cutfeat = 0; - for(int i = 0; i < (DIM > 0 ? DIM : obj.dim); ++i) + for (Offset k = left + 1; k < right; ++k) { - ElementType span = bbox[i].high - bbox[i].low; - if(span > (1 - EPS) * max_span) + for (Dimension i = 0; i < dims; ++i) { - ElementType min_elem, max_elem; - computeMinMax(obj, ind, count, i, min_elem, max_elem); - ElementType spread = max_elem - min_elem; - if(spread > max_spread) + const auto val = dataset_get(obj, obj.vAcc_[k], i); + if (bbox[i].low > val) + { + bbox[i].low = val; + } + if (bbox[i].high < val) { - cutfeat = i; - max_spread = spread; + bbox[i].high = val; } } } - // split in the middle - DistanceType split_val = (bbox[cutfeat].low + bbox[cutfeat].high) / 2; - ElementType min_elem, max_elem; - computeMinMax(obj, ind, count, cutfeat, min_elem, max_elem); - - if(split_val < min_elem) - cutval = min_elem; - else if(split_val > max_elem) - cutval = max_elem; - else - cutval = split_val; + } + else + { + /* Determine the index, dimension and value for split plane */ + Offset idx; + Dimension cutfeat; + DistanceType cutval; + middleSplit_(obj, left, right - left, idx, cutfeat, cutval, bbox); - IndexType lim1, lim2; - planeSplit(obj, ind, count, cutfeat, cutval, lim1, lim2); + node->node_type.sub.divfeat = cutfeat; - if(lim1 > count / 2) - index = lim1; - else if(lim2 < count / 2) - index = lim2; - else - index = count / 2; - } + std::future right_future; - /** - * Subdivide the list of points by a plane perpendicular on axe corresponding - * to the 'cutfeat' dimension at 'cutval' position. - * - * On return: - * dataset[ind[0..lim1-1]][cutfeat]cutval - */ - void planeSplit(Derived & obj, IndexType * ind, const IndexType count, int cutfeat, DistanceType & cutval, - IndexType & lim1, IndexType & lim2) - { - /* Move vector indices for left subtree to front of list. */ - IndexType left = 0; - IndexType right = count - 1; - for(;;) + /* Recurse on right concurrently, if possible */ + + BoundingBox right_bbox(bbox); + right_bbox[cutfeat].low = cutval; + if (++thread_count < n_thread_build_) { - while(left <= right && dataset_get(obj, ind[left], cutfeat) < cutval) ++left; - while(right && left <= right && dataset_get(obj, ind[right], cutfeat) >= cutval) --right; - if(left > right || !right) - break; // "!right" was added to support unsigned Index types - std::swap(ind[left], ind[right]); - ++left; - --right; + /* Concurrent thread for right recursion */ + + right_future = std::async( + std::launch::async, + &KDTreeBaseClass::divideTreeConcurrent, + this, + std::ref(obj), + left + idx, + right, + std::ref(right_bbox), + std::ref(thread_count), + std::ref(mutex)); } - /* If either list is empty, it means that all remaining features - * are identical. Split in the middle to maintain a balanced tree. - */ - lim1 = left; - right = count - 1; - for(;;) + else { - while(left <= right && dataset_get(obj, ind[left], cutfeat) <= cutval) ++left; - while(right && left <= right && dataset_get(obj, ind[right], cutfeat) > cutval) --right; - if(left > right || !right) - break; // "!right" was added to support unsigned Index types - std::swap(ind[left], ind[right]); - ++left; - --right; + --thread_count; } - lim2 = left; - } - DistanceType computeInitialDistances(const Derived & obj, const ElementType * vec, - distance_vector_t & dists) const - { - assert(vec); - DistanceType distsq = DistanceType(); + /* Recurse on left in this thread */ - for(int i = 0; i < (DIM > 0 ? DIM : obj.dim); ++i) + BoundingBox left_bbox(bbox); + left_bbox[cutfeat].high = cutval; + node->child1 = this->divideTreeConcurrent( + obj, + left, + left + idx, + left_bbox, + thread_count, + mutex); + + if (right_future.valid()) { - if(vec[i] < obj.root_bbox[i].low) - { - dists[i] = obj.distance.accum_dist(vec[i], obj.root_bbox[i].low, i); - distsq += dists[i]; - } - if(vec[i] > obj.root_bbox[i].high) - { - dists[i] = obj.distance.accum_dist(vec[i], obj.root_bbox[i].high, i); - distsq += dists[i]; - } - } - return distsq; - } + /* Block and wait for concurrent right from above */ - void save_tree(Derived & obj, FILE * stream, NodePtr tree) - { - save_value(stream, *tree); - if(tree->child1 != NULL) + node->child2 = right_future.get(); + --thread_count; + } + else { - save_tree(obj, stream, tree->child1); + /* Otherwise, recurse on right in this thread */ + + node->child2 = this->divideTreeConcurrent( + obj, + left + idx, + right, + right_bbox, + thread_count, + mutex); } - if(tree->child2 != NULL) + + node->node_type.sub.divlow = left_bbox[cutfeat].high; + node->node_type.sub.divhigh = right_bbox[cutfeat].low; + + for (Dimension i = 0; i < dims; ++i) { - save_tree(obj, stream, tree->child2); + bbox[i].low = std::min(left_bbox[i].low, right_bbox[i].low); + bbox[i].high = std::max(left_bbox[i].high, right_bbox[i].high); } } - void load_tree(Derived & obj, FILE * stream, NodePtr & tree) + return node; + } + + void middleSplit_( + const Derived& obj, + const Offset ind, + const Size count, + Offset& index, + Dimension& cutfeat, + DistanceType& cutval, + const BoundingBox& bbox) + { + const auto dims = (DIM > 0 ? DIM : obj.dim_); + const auto EPS = static_cast(0.00001); + + // Pre-compute max_span once + ElementType max_span = bbox[0].high - bbox[0].low; + for (Dimension i = 1; i < dims; ++i) { - tree = obj.pool.template allocate(); - load_value(stream, *tree); - if(tree->child1 != NULL) + ElementType span = bbox[i].high - bbox[i].low; + if (span > max_span) { - load_tree(obj, stream, tree->child1); - } - if(tree->child2 != NULL) - { - load_tree(obj, stream, tree->child2); + max_span = span; } } - /** Stores the index in a binary file. - * IMPORTANT NOTE: The set of data points is NOT stored in the file, so when - * loading the index object it must be constructed associated to the same - * source of data points used while building it. See the example: - * examples/saveload_example.cpp \sa loadIndex */ - void saveIndex_(Derived & obj, FILE * stream) + // Single-pass min/max computation for candidate dimensions + cutfeat = 0; + ElementType max_spread = -1; + ElementType min_elem = 0, max_elem = 0; + + // Only check dimensions within (1-EPS) of max_span + std::vector candidates; + candidates.reserve(dims); + for (Dimension i = 0; i < dims; ++i) { - save_value(stream, obj.m_size); - save_value(stream, obj.dim); - save_value(stream, obj.root_bbox); - save_value(stream, obj.m_leaf_max_size); - save_value(stream, obj.vind); - save_tree(obj, stream, obj.root_node); + if (bbox[i].high - bbox[i].low >= (1 - EPS) * max_span) + { + candidates.push_back(i); + } } - /** Loads a previous index from a binary file. - * IMPORTANT NOTE: The set of data points is NOT stored in the file, so the - * index object must be constructed associated to the same source of data - * points used while building the index. See the example: - * examples/saveload_example.cpp \sa loadIndex */ - void loadIndex_(Derived & obj, FILE * stream) + // Vectorized min/max for candidates + for (Dimension dim : candidates) { - load_value(stream, obj.m_size); - load_value(stream, obj.dim); - load_value(stream, obj.root_bbox); - load_value(stream, obj.m_leaf_max_size); - load_value(stream, obj.vind); - load_tree(obj, stream, obj.root_node); - } - }; + ElementType local_min = dataset_get(obj, vAcc_[ind], dim); + ElementType local_max = local_min; - /** @addtogroup kdtrees_grp KD-tree classes and adaptors - * @{ */ + // Unrolled loop for better performance + constexpr size_t UNROLL = 4; + Offset k = 1; + for (; k + UNROLL <= count; k += UNROLL) + { + ElementType v0 = dataset_get(obj, vAcc_[ind + k], dim); + ElementType v1 = dataset_get(obj, vAcc_[ind + k + 1], dim); + ElementType v2 = dataset_get(obj, vAcc_[ind + k + 2], dim); + ElementType v3 = dataset_get(obj, vAcc_[ind + k + 3], dim); - /** kd-tree static index - * - * Contains the k-d trees and other information for indexing a set of points - * for nearest-neighbor matching. - * - * The class "DatasetAdaptor" must provide the following interface (can be - * non-virtual, inlined methods): - * - * \code - * // Must return the number of data poins - * inline size_t kdtree_get_point_count() const { ... } - * - * - * // Must return the dim'th component of the idx'th point in the class: - * inline T kdtree_get_pt(const size_t idx, const size_t dim) const { ... } - * - * // Optional bounding-box computation: return false to default to a standard - * bbox computation loop. - * // Return true if the BBOX was already computed by the class and returned - * in "bb" so it can be avoided to redo it again. - * // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 - * for point clouds) template bool kdtree_get_bbox(BBOX &bb) const - * { - * bb[0].low = ...; bb[0].high = ...; // 0th dimension limits - * bb[1].low = ...; bb[1].high = ...; // 1st dimension limits - * ... - * return true; - * } - * - * \endcode - * - * \tparam DatasetAdaptor The user-provided adaptor (see comments above). - * \tparam Distance The distance metric to use: nanoflann::metric_L1, - * nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. \tparam DIM - * Dimensionality of data points (e.g. 3 for 3D points) \tparam IndexType Will - * be typically size_t or int - */ - template - class KDTreeSingleIndexAdaptor : - public KDTreeBaseClass, Distance, - DatasetAdaptor, DIM, IndexType> - { - public: - /** Deleted copy constructor*/ - KDTreeSingleIndexAdaptor(const KDTreeSingleIndexAdaptor &) = delete; + local_min = std::min({local_min, v0, v1, v2, v3}); + local_max = std::max({local_max, v0, v1, v2, v3}); + } - /** - * The dataset used by this index - */ - const DatasetAdaptor & dataset; //!< The source of our data - - const KDTreeSingleIndexAdaptorParams index_params; - - Distance distance; - - typedef typename nanoflann::KDTreeBaseClass< - nanoflann::KDTreeSingleIndexAdaptor, Distance, DatasetAdaptor, - DIM, IndexType> - BaseClassRef; - - typedef typename BaseClassRef::ElementType ElementType; - typedef typename BaseClassRef::DistanceType DistanceType; - - typedef typename BaseClassRef::Node Node; - typedef Node * NodePtr; - - typedef typename BaseClassRef::Interval Interval; - /** Define "BoundingBox" as a fixed-size or variable-size container depending - * on "DIM" */ - typedef typename BaseClassRef::BoundingBox BoundingBox; - - /** Define "distance_vector_t" as a fixed-size or variable-size container - * depending on "DIM" */ - typedef typename BaseClassRef::distance_vector_t distance_vector_t; - - /** - * KDTree constructor - * - * Refer to docs in README.md or online in - * https://github.com/jlblancoc/nanoflann - * - * The KD-Tree point dimension (the length of each point in the datase, e.g. 3 - * for 3D points) is determined by means of: - * - The \a DIM template parameter if >0 (highest priority) - * - Otherwise, the \a dimensionality parameter of this constructor. - * - * @param inputData Dataset with the input features - * @param params Basically, the maximum leaf node size - */ - KDTreeSingleIndexAdaptor(const int dimensionality, const DatasetAdaptor & inputData, - const KDTreeSingleIndexAdaptorParams & params = KDTreeSingleIndexAdaptorParams()) : - dataset(inputData), index_params(params), distance(inputData) - { - BaseClassRef::root_node = NULL; - BaseClassRef::m_size = dataset.kdtree_get_point_count(); - BaseClassRef::m_size_at_index_build = BaseClassRef::m_size; - BaseClassRef::dim = dimensionality; - if(DIM > 0) - BaseClassRef::dim = DIM; - BaseClassRef::m_leaf_max_size = params.leaf_max_size; + // Handle remainder + for (; k < count; ++k) + { + ElementType val = dataset_get(obj, vAcc_[ind + k], dim); + local_min = std::min(local_min, val); + local_max = std::max(local_max, val); + } - // Create a permutable array of indices to the input vectors. - init_vind(); + ElementType spread = local_max - local_min; + if (spread > max_spread) + { + cutfeat = dim; + max_spread = spread; + min_elem = local_min; + max_elem = local_max; + } } - /** - * Builds the index - */ - void buildIndex() - { - BaseClassRef::m_size = dataset.kdtree_get_point_count(); - BaseClassRef::m_size_at_index_build = BaseClassRef::m_size; - init_vind(); - this->freeIndex(*this); - BaseClassRef::m_size_at_index_build = BaseClassRef::m_size; - if(BaseClassRef::m_size == 0) - return; - computeBoundingBox(BaseClassRef::root_bbox); - BaseClassRef::root_node = this->divideTree(*this, 0, BaseClassRef::m_size, - BaseClassRef::root_bbox); // construct the tree - } - - /** \name Query methods - * @{ */ - - /** - * Find set of nearest neighbors to vec[0:dim-1]. Their indices are stored - * inside the result object. - * - * Params: - * result = the result object in which the indices of the - * nearest-neighbors are stored vec = the vector for which to search the - * nearest neighbors - * - * \tparam RESULTSET Should be any ResultSet - * \return True if the requested neighbors could be found. - * \sa knnSearch, radiusSearch - */ - template - bool findNeighbors(RESULTSET & result, const ElementType * vec, const SearchParams & searchParams) const - { - assert(vec); - if(this->size(*this) == 0) - return false; - if(!BaseClassRef::root_node) - throw std::runtime_error("[nanoflann] findNeighbors() called before building the index."); - float epsError = 1 + searchParams.eps; - - distance_vector_t dists; // fixed or variable-sized container (depending on DIM) - auto zero = static_cast(0); - assign(dists, (DIM > 0 ? DIM : BaseClassRef::dim), - zero); // Fill it with zeros. - DistanceType distsq = this->computeInitialDistances(*this, vec, dists); - searchLevel(result, vec, BaseClassRef::root_node, distsq, dists, - epsError); // "count_leaf" parameter removed since was neither - // used nor returned to the user. - return result.full(); - } - - /** - * Find the "num_closest" nearest neighbors to the \a query_point[0:dim-1]. - * Their indices are stored inside the result object. \sa radiusSearch, - * findNeighbors \note nChecks_IGNORED is ignored but kept for compatibility - * with the original FLANN interface. \return Number `N` of valid points in - * the result set. Only the first `N` entries in `out_indices` and - * `out_distances_sq` will be valid. Return may be less than `num_closest` - * only if the number of elements in the tree is less than `num_closest`. - */ - size_t knnSearch(const ElementType * query_point, const size_t num_closest, IndexType * out_indices, - DistanceType * out_distances_sq, const int /* nChecks_IGNORED */ = 10) const - { - nanoflann::KNNResultSet resultSet(num_closest); - resultSet.init(out_indices, out_distances_sq); - this->findNeighbors(resultSet, query_point, nanoflann::SearchParams()); - return resultSet.size(); - } - - /** - * Find all the neighbors to \a query_point[0:dim-1] within a maximum radius. - * The output is given as a vector of pairs, of which the first element is a - * point index and the second the corresponding distance. Previous contents of - * \a IndicesDists are cleared. - * - * If searchParams.sorted==true, the output list is sorted by ascending - * distances. - * - * For a better performance, it is advisable to do a .reserve() on the vector - * if you have any wild guess about the number of expected matches. - * - * \sa knnSearch, findNeighbors, radiusSearchCustomCallback - * \return The number of points within the given radius (i.e. indices.size() - * or dists.size() ) - */ - size_t radiusSearch(const ElementType * query_point, const DistanceType & radius, - std::vector> & IndicesDists, - const SearchParams & searchParams) const + // Median-of-three for better balance + DistanceType split_val = (bbox[cutfeat].low + bbox[cutfeat].high) / 2; + if (split_val < min_elem) { - RadiusResultSet resultSet(radius, IndicesDists); - const size_t nFound = radiusSearchCustomCallback(query_point, resultSet, searchParams); - if(searchParams.sorted) - std::sort(IndicesDists.begin(), IndicesDists.end(), IndexDist_Sorter()); - return nFound; + split_val = min_elem; } - - /** - * Just like radiusSearch() but with a custom callback class for each point - * found in the radius of the query. See the source of RadiusResultSet<> as a - * start point for your own classes. \sa radiusSearch - */ - template - size_t radiusSearchCustomCallback(const ElementType * query_point, SEARCH_CALLBACK & resultSet, - const SearchParams & searchParams = SearchParams()) const + if (split_val > max_elem) { - this->findNeighbors(resultSet, query_point, searchParams); - return resultSet.size(); + split_val = max_elem; } - /** @} */ + cutval = split_val; - public: - /** Make sure the auxiliary list \a vind has the same size than the current - * dataset, and re-generate if size has changed. */ - void init_vind() - { - // Create a permutable array of indices to the input vectors. - BaseClassRef::m_size = dataset.kdtree_get_point_count(); - if(BaseClassRef::vind.size() != BaseClassRef::m_size) - BaseClassRef::vind.resize(BaseClassRef::m_size); - for(size_t i = 0; i < BaseClassRef::m_size; i++) BaseClassRef::vind[i] = i; - } + // Optimized partitioning + Offset lim1, lim2; + planeSplit(obj, ind, count, cutfeat, cutval, lim1, lim2); - void computeBoundingBox(BoundingBox & bbox) - { - resize(bbox, (DIM > 0 ? DIM : BaseClassRef::dim)); - if(dataset.kdtree_get_bbox(bbox)) - { - // Done! It was implemented in derived class - } - else - { - const size_t N = dataset.kdtree_get_point_count(); - if(!N) - throw std::runtime_error("[nanoflann] computeBoundingBox() called but " - "no data points found."); - for(int i = 0; i < (DIM > 0 ? DIM : BaseClassRef::dim); ++i) - { - bbox[i].low = bbox[i].high = this->dataset_get(*this, 0, i); - } - for(size_t k = 1; k < N; ++k) - { - for(int i = 0; i < (DIM > 0 ? DIM : BaseClassRef::dim); ++i) - { - if(this->dataset_get(*this, k, i) < bbox[i].low) - bbox[i].low = this->dataset_get(*this, k, i); - if(this->dataset_get(*this, k, i) > bbox[i].high) - bbox[i].high = this->dataset_get(*this, k, i); - } - } - } - } + index = (lim1 > count / 2) ? lim1 + : (lim2 < count / 2) ? lim2 + : count / 2; + } - /** - * Performs an exact search in the tree starting from a node. - * \tparam RESULTSET Should be any ResultSet - * \return true if the search should be continued, false if the results are - * sufficient - */ - template - bool searchLevel(RESULTSET & result_set, const ElementType * vec, const NodePtr node, DistanceType mindistsq, - distance_vector_t & dists, const float epsError) const + /** + * Subdivide the list of points by a plane perpendicular on the axis + * corresponding to the 'cutfeat' dimension at 'cutval' position. + * + * On return: + * dataset[ind[0..lim1-1]][cutfeat] < cutval + * dataset[ind[lim1..lim2-1]][cutfeat] == cutval + * dataset[ind[lim2..count]][cutfeat] > cutval + */ + void planeSplit( + const Derived& obj, + const Offset ind, + const Size count, + const Dimension cutfeat, + const DistanceType& cutval, + Offset& lim1, + Offset& lim2) + { + // Dutch National Flag algorithm for three-way partitioning + Offset left = 0; + Offset mid = 0; + Offset right = count - 1; + + while (mid <= right) { - /* If this is a leaf node, then do check and return. */ - if((node->child1 == NULL) && (node->child2 == NULL)) + ElementType val = dataset_get(obj, vAcc_[ind + mid], cutfeat); + + if (val < cutval) { - // count_leaf += (node->lr.right-node->lr.left); // Removed since was - // neither used nor returned to the user. - DistanceType worst_dist = result_set.worstDist(); - for(IndexType i = node->node_type.lr.left; i < node->node_type.lr.right; ++i) - { - const IndexType index = BaseClassRef::vind[i]; // reorder... : i; - DistanceType dist = distance.evalMetric(vec, index, (DIM > 0 ? DIM : BaseClassRef::dim)); - if(dist < worst_dist) - { - if(!result_set.addPoint(dist, BaseClassRef::vind[i])) - { - // the resultset doesn't want to receive any more points, we're done - // searching! - return false; - } - } - } - return true; + std::swap(vAcc_[ind + left], vAcc_[ind + mid]); + left++; + mid++; } - - /* Which child branch should be taken first? */ - int idx = node->node_type.sub.divfeat; - ElementType val = vec[idx]; - DistanceType diff1 = val - node->node_type.sub.divlow; - DistanceType diff2 = val - node->node_type.sub.divhigh; - - NodePtr bestChild; - NodePtr otherChild; - DistanceType cut_dist; - if((diff1 + diff2) < 0) + else if (val > cutval) { - bestChild = node->child1; - otherChild = node->child2; - cut_dist = distance.accum_dist(val, node->node_type.sub.divhigh, idx); + std::swap(vAcc_[ind + mid], vAcc_[ind + right]); + right--; } else { - bestChild = node->child2; - otherChild = node->child1; - cut_dist = distance.accum_dist(val, node->node_type.sub.divlow, idx); + mid++; } + } + + lim1 = left; + lim2 = mid; + } + + DistanceType computeInitialDistances( + const Derived& obj, + const ElementType* vec, + distance_vector_t& dists) const + { + assert(vec); + DistanceType dist = DistanceType(); - /* Call recursively to search next level down. */ - if(!searchLevel(result_set, vec, bestChild, mindistsq, dists, epsError)) + for (Dimension i = 0; i < (DIM > 0 ? DIM : obj.dim_); ++i) + { + if (vec[i] < obj.root_bbox_[i].low) { - // the resultset doesn't want to receive any more points, we're done - // searching! - return false; + dists[i] = + obj.distance_.accum_dist(vec[i], obj.root_bbox_[i].low, i); + dist += dists[i]; } - - DistanceType dst = dists[idx]; - mindistsq = mindistsq + cut_dist - dst; - dists[idx] = cut_dist; - if(mindistsq * epsError <= result_set.worstDist()) + if (vec[i] > obj.root_bbox_[i].high) { - if(!searchLevel(result_set, vec, otherChild, mindistsq, dists, epsError)) - { - // the resultset doesn't want to receive any more points, we're done - // searching! - return false; - } + dists[i] = + obj.distance_.accum_dist(vec[i], obj.root_bbox_[i].high, i); + dist += dists[i]; } - dists[idx] = dst; - return true; } + return dist; + } - public: - /** Stores the index in a binary file. - * IMPORTANT NOTE: The set of data points is NOT stored in the file, so when - * loading the index object it must be constructed associated to the same - * source of data points used while building it. See the example: - * examples/saveload_example.cpp \sa loadIndex */ - void saveIndex(FILE * stream) { this->saveIndex_(*this, stream); } - - /** Loads a previous index from a binary file. - * IMPORTANT NOTE: The set of data points is NOT stored in the file, so the - * index object must be constructed associated to the same source of data - * points used while building the index. See the example: - * examples/saveload_example.cpp \sa loadIndex */ - void loadIndex(FILE * stream) { this->loadIndex_(*this, stream); } - - }; // class KDTree + static void save_tree( + const Derived& obj, + std::ostream& stream, + const NodeConstPtr tree) + { + save_value(stream, *tree); + if (tree->child1 != nullptr) + { + save_tree(obj, stream, tree->child1); + } + if (tree->child2 != nullptr) + { + save_tree(obj, stream, tree->child2); + } + } + + static void load_tree(Derived& obj, std::istream& stream, NodePtr& tree) + { + tree = obj.pool_.template allocate(); + load_value(stream, *tree); + if (tree->child1 != nullptr) + { + load_tree(obj, stream, tree->child1); + } + if (tree->child2 != nullptr) + { + load_tree(obj, stream, tree->child2); + } + } - /** kd-tree dynamic index + /** Stores the index in a binary file. + * IMPORTANT NOTE: The set of data points is NOT stored in the file, so + * when loading the index object it must be constructed associated to the + * same source of data points used while building it. See the example: + * examples/saveload_example.cpp \sa loadIndex */ + void saveIndex(const Derived& obj, std::ostream& stream) const + { + save_value(stream, obj.size_); + save_value(stream, obj.dim_); + save_value(stream, obj.root_bbox_); + save_value(stream, obj.leaf_max_size_); + save_value(stream, obj.vAcc_); + if (obj.root_node_) + { + save_tree(obj, stream, obj.root_node_); + } + } + + /** Loads a previous index from a binary file. + * IMPORTANT NOTE: The set of data points is NOT stored in the file, so + * the index object must be constructed associated to the same source of + * data points used while building the index. See the example: + * examples/saveload_example.cpp \sa loadIndex */ + void loadIndex(Derived& obj, std::istream& stream) + { + load_value(stream, obj.size_); + load_value(stream, obj.dim_); + load_value(stream, obj.root_bbox_); + load_value(stream, obj.leaf_max_size_); + load_value(stream, obj.vAcc_); + load_tree(obj, stream, obj.root_node_); + } +}; + +/** @addtogroup kdtrees_grp KD-tree classes and adaptors + * @{ */ + +/** kd-tree static index + * + * Contains the k-d trees and other information for indexing a set of points + * for nearest-neighbor matching. + * + * The class "DatasetAdaptor" must provide the following interface (can be + * non-virtual, inlined methods): + * + * \code + * // Must return the number of data poins + * size_t kdtree_get_point_count() const { ... } + * + * + * // Must return the dim'th component of the idx'th point in the class: + * T kdtree_get_pt(const size_t idx, const size_t dim) const { ... } + * + * // Optional bounding-box computation: return false to default to a standard + * bbox computation loop. + * // Return true if the BBOX was already computed by the class and returned + * in "bb" so it can be avoided to redo it again. + * // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 + * for point clouds) template bool kdtree_get_bbox(BBOX &bb) const + * { + * bb[0].low = ...; bb[0].high = ...; // 0th dimension limits + * bb[1].low = ...; bb[1].high = ...; // 1st dimension limits + * ... + * return true; + * } + * + * \endcode + * + * \tparam DatasetAdaptor The user-provided adaptor, which must be ensured to + * have a lifetime equal or longer than the instance of this class. + * \tparam Distance The distance metric to use: nanoflann::metric_L1, + * nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. \tparam DIM + * Dimensionality of data points (e.g. 3 for 3D points) \tparam IndexType Will + * be typically size_t or int + */ +template< + typename Distance, + class DatasetAdaptor, + int32_t DIM = -1, + typename index_t = uint32_t> +class KDTreeSingleIndexAdaptor : + public KDTreeBaseClass< + KDTreeSingleIndexAdaptor, + Distance, + DatasetAdaptor, + DIM, + index_t> +{ +public: + /** Deleted copy constructor*/ + explicit KDTreeSingleIndexAdaptor( + const KDTreeSingleIndexAdaptor< + Distance, + DatasetAdaptor, + DIM, + index_t>&) = delete; + + /** The data source used by this index */ + const DatasetAdaptor& dataset_; + + const KDTreeSingleIndexAdaptorParams indexParams; + + Distance distance_; + + using Base = typename nanoflann::KDTreeBaseClass< + nanoflann:: + KDTreeSingleIndexAdaptor, + Distance, + DatasetAdaptor, + DIM, + index_t>; + + using Offset = typename Base::Offset; + using Size = typename Base::Size; + using Dimension = typename Base::Dimension; + + using ElementType = typename Base::ElementType; + using DistanceType = typename Base::DistanceType; + using IndexType = typename Base::IndexType; + + using Node = typename Base::Node; + using NodePtr = Node*; + + using Interval = typename Base::Interval; + + /** Define "BoundingBox" as a fixed-size or variable-size container + * depending on "DIM" */ + using BoundingBox = typename Base::BoundingBox; + + /** Define "distance_vector_t" as a fixed-size or variable-size container + * depending on "DIM" */ + using distance_vector_t = typename Base::distance_vector_t; + + /** + * KDTree constructor * - * Contains the k-d trees and other information for indexing a set of points - * for nearest-neighbor matching. + * Refer to docs in README.md or online in + * https://github.com/jlblancoc/nanoflann * - * The class "DatasetAdaptor" must provide the following interface (can be - * non-virtual, inlined methods): + * The KD-Tree point dimension (the length of each point in the datase, e.g. + * 3 for 3D points) is determined by means of: + * - The \a DIM template parameter if >0 (highest priority) + * - Otherwise, the \a dimensionality parameter of this constructor. * - * \code - * // Must return the number of data poins - * inline size_t kdtree_get_point_count() const { ... } + * @param inputData Dataset with the input features. Its lifetime must be + * equal or longer than that of the instance of this class. + * @param params Basically, the maximum leaf node size * - * // Must return the dim'th component of the idx'th point in the class: - * inline T kdtree_get_pt(const size_t idx, const size_t dim) const { ... } + * Note that there is a variable number of optional additional parameters + * which will be forwarded to the metric class constructor. Refer to example + * `examples/pointcloud_custom_metric.cpp` for a use case. * - * // Optional bounding-box computation: return false to default to a standard - * bbox computation loop. - * // Return true if the BBOX was already computed by the class and returned - * in "bb" so it can be avoided to redo it again. - * // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 - * for point clouds) template bool kdtree_get_bbox(BBOX &bb) const - * { - * bb[0].low = ...; bb[0].high = ...; // 0th dimension limits - * bb[1].low = ...; bb[1].high = ...; // 1st dimension limits - * ... - * return true; - * } + */ + template + explicit KDTreeSingleIndexAdaptor( + const Dimension dimensionality, + const DatasetAdaptor& inputData, + const KDTreeSingleIndexAdaptorParams& params, + Args&&... args) : + dataset_(inputData), + indexParams(params), + distance_(inputData, std::forward(args)...) + { + init(dimensionality, params); + } + + explicit KDTreeSingleIndexAdaptor( + const Dimension dimensionality, + const DatasetAdaptor& inputData, + const KDTreeSingleIndexAdaptorParams& params = {}) : + dataset_(inputData), + indexParams(params), + distance_(inputData) + { + init(dimensionality, params); + } + +private: + void init( + const Dimension dimensionality, + const KDTreeSingleIndexAdaptorParams& params) + { + Base::size_ = dataset_.kdtree_get_point_count(); + Base::size_at_index_build_ = Base::size_; + Base::dim_ = dimensionality; + if (DIM > 0) + { + Base::dim_ = DIM; + } + Base::leaf_max_size_ = params.leaf_max_size; + if (params.n_thread_build > 0) + { + Base::n_thread_build_ = params.n_thread_build; + } + else + { + Base::n_thread_build_ = + std::max(std::thread::hardware_concurrency(), 1u); + } + + if (!(params.flags & + KDTreeSingleIndexAdaptorFlags::SkipInitialBuildIndex)) + { + // Build KD-tree: + buildIndex(); + } + } + +public: + /** + * Builds the index + */ + void buildIndex() + { + Base::size_ = dataset_.kdtree_get_point_count(); + Base::size_at_index_build_ = Base::size_; + init_vind(); + this->freeIndex(*this); + Base::size_at_index_build_ = Base::size_; + if (Base::size_ == 0) + { + return; + } + computeBoundingBox(Base::root_bbox_); + // construct the tree + if (Base::n_thread_build_ == 1) + { + Base::root_node_ = + this->divideTree(*this, 0, Base::size_, Base::root_bbox_); + } + else + { +#ifndef NANOFLANN_NO_THREADS + std::atomic thread_count(0u); + std::mutex mutex; + Base::root_node_ = this->divideTreeConcurrent( + *this, + 0, + Base::size_, + Base::root_bbox_, + thread_count, + mutex); +#else /* NANOFLANN_NO_THREADS */ + throw std::runtime_error("Multithreading is disabled"); +#endif /* NANOFLANN_NO_THREADS */ + } + } + + /** \name Query methods + * @{ */ + + /** + * Find set of nearest neighbors to vec[0:dim-1]. Their indices are stored + * inside the result object. + * + * Params: + * result = the result object in which the indices of the + * nearest-neighbors are stored vec = the vector for which to search the + * nearest neighbors * - * \endcode + * \tparam RESULTSET Should be any ResultSet + * \return True if the requested neighbors could be found. + * \sa knnSearch, radiusSearch * - * \tparam DatasetAdaptor The user-provided adaptor (see comments above). - * \tparam Distance The distance metric to use: nanoflann::metric_L1, - * nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. \tparam DIM - * Dimensionality of data points (e.g. 3 for 3D points) \tparam IndexType Will - * be typically size_t or int + * \note If L2 norms are used, all returned distances are actually squared + * distances. */ - template - class KDTreeSingleIndexDynamicAdaptor_ : - public KDTreeBaseClass, Distance, - DatasetAdaptor, DIM, IndexType> - { - public: - /** - * The dataset used by this index - */ - const DatasetAdaptor & dataset; //!< The source of our data - - KDTreeSingleIndexAdaptorParams index_params; - - std::vector & treeIndex; - - Distance distance; - - typedef typename nanoflann::KDTreeBaseClass< - nanoflann::KDTreeSingleIndexDynamicAdaptor_, Distance, - DatasetAdaptor, DIM, IndexType> - BaseClassRef; - - typedef typename BaseClassRef::ElementType ElementType; - typedef typename BaseClassRef::DistanceType DistanceType; - - typedef typename BaseClassRef::Node Node; - typedef Node * NodePtr; - - typedef typename BaseClassRef::Interval Interval; - /** Define "BoundingBox" as a fixed-size or variable-size container depending - * on "DIM" */ - typedef typename BaseClassRef::BoundingBox BoundingBox; - - /** Define "distance_vector_t" as a fixed-size or variable-size container - * depending on "DIM" */ - typedef typename BaseClassRef::distance_vector_t distance_vector_t; - - /** - * KDTree constructor - * - * Refer to docs in README.md or online in - * https://github.com/jlblancoc/nanoflann - * - * The KD-Tree point dimension (the length of each point in the datase, e.g. 3 - * for 3D points) is determined by means of: - * - The \a DIM template parameter if >0 (highest priority) - * - Otherwise, the \a dimensionality parameter of this constructor. - * - * @param inputData Dataset with the input features - * @param params Basically, the maximum leaf node size - */ - KDTreeSingleIndexDynamicAdaptor_( - const int dimensionality, const DatasetAdaptor & inputData, std::vector & treeIndex_, - const KDTreeSingleIndexAdaptorParams & params = KDTreeSingleIndexAdaptorParams()) : - dataset(inputData), index_params(params), treeIndex(treeIndex_), distance(inputData) - { - BaseClassRef::root_node = NULL; - BaseClassRef::m_size = 0; - BaseClassRef::m_size_at_index_build = 0; - BaseClassRef::dim = dimensionality; - if(DIM > 0) - BaseClassRef::dim = DIM; - BaseClassRef::m_leaf_max_size = params.leaf_max_size; - } - - /** Assignment operator definiton */ - KDTreeSingleIndexDynamicAdaptor_ operator=(const KDTreeSingleIndexDynamicAdaptor_ & rhs) - { - KDTreeSingleIndexDynamicAdaptor_ tmp(rhs); - std::swap(BaseClassRef::vind, tmp.BaseClassRef::vind); - std::swap(BaseClassRef::m_leaf_max_size, tmp.BaseClassRef::m_leaf_max_size); - std::swap(index_params, tmp.index_params); - std::swap(treeIndex, tmp.treeIndex); - std::swap(BaseClassRef::m_size, tmp.BaseClassRef::m_size); - std::swap(BaseClassRef::m_size_at_index_build, tmp.BaseClassRef::m_size_at_index_build); - std::swap(BaseClassRef::root_node, tmp.BaseClassRef::root_node); - std::swap(BaseClassRef::root_bbox, tmp.BaseClassRef::root_bbox); - std::swap(BaseClassRef::pool, tmp.BaseClassRef::pool); - return *this; - } - - /** - * Builds the index - */ - void buildIndex() - { - BaseClassRef::m_size = BaseClassRef::vind.size(); - this->freeIndex(*this); - BaseClassRef::m_size_at_index_build = BaseClassRef::m_size; - if(BaseClassRef::m_size == 0) - return; - computeBoundingBox(BaseClassRef::root_bbox); - BaseClassRef::root_node = this->divideTree(*this, 0, BaseClassRef::m_size, - BaseClassRef::root_bbox); // construct the tree - } - - /** \name Query methods - * @{ */ - - /** - * Find set of nearest neighbors to vec[0:dim-1]. Their indices are stored - * inside the result object. - * - * Params: - * result = the result object in which the indices of the - * nearest-neighbors are stored vec = the vector for which to search the - * nearest neighbors - * - * \tparam RESULTSET Should be any ResultSet - * \return True if the requested neighbors could be found. - * \sa knnSearch, radiusSearch - */ - template - bool findNeighbors(RESULTSET & result, const ElementType * vec, const SearchParams & searchParams) const + template + bool findNeighbors( + RESULTSET& result, + const ElementType* vec, + const SearchParameters& searchParams = {}) const + { + assert(vec); + if (this->size(*this) == 0) { - assert(vec); - if(this->size(*this) == 0) - return false; - if(!BaseClassRef::root_node) - return false; - float epsError = 1 + searchParams.eps; - - // fixed or variable-sized container (depending on DIM) - distance_vector_t dists; - // Fill it with zeros. - assign(dists, (DIM > 0 ? DIM : BaseClassRef::dim), static_cast(0)); - DistanceType distsq = this->computeInitialDistances(*this, vec, dists); - searchLevel(result, vec, BaseClassRef::root_node, distsq, dists, - epsError); // "count_leaf" parameter removed since was neither - // used nor returned to the user. - return result.full(); - } - - /** - * Find the "num_closest" nearest neighbors to the \a query_point[0:dim-1]. - * Their indices are stored inside the result object. \sa radiusSearch, - * findNeighbors \note nChecks_IGNORED is ignored but kept for compatibility - * with the original FLANN interface. \return Number `N` of valid points in - * the result set. Only the first `N` entries in `out_indices` and - * `out_distances_sq` will be valid. Return may be less than `num_closest` - * only if the number of elements in the tree is less than `num_closest`. - */ - size_t knnSearch(const ElementType * query_point, const size_t num_closest, IndexType * out_indices, - DistanceType * out_distances_sq, const int /* nChecks_IGNORED */ = 10) const - { - nanoflann::KNNResultSet resultSet(num_closest); - resultSet.init(out_indices, out_distances_sq); - this->findNeighbors(resultSet, query_point, nanoflann::SearchParams()); - return resultSet.size(); - } - - /** - * Find all the neighbors to \a query_point[0:dim-1] within a maximum radius. - * The output is given as a vector of pairs, of which the first element is a - * point index and the second the corresponding distance. Previous contents of - * \a IndicesDists are cleared. - * - * If searchParams.sorted==true, the output list is sorted by ascending - * distances. - * - * For a better performance, it is advisable to do a .reserve() on the vector - * if you have any wild guess about the number of expected matches. - * - * \sa knnSearch, findNeighbors, radiusSearchCustomCallback - * \return The number of points within the given radius (i.e. indices.size() - * or dists.size() ) - */ - size_t radiusSearch(const ElementType * query_point, const DistanceType & radius, - std::vector> & IndicesDists, - const SearchParams & searchParams) const + return false; + } + if (!Base::root_node_) { - RadiusResultSet resultSet(radius, IndicesDists); - const size_t nFound = radiusSearchCustomCallback(query_point, resultSet, searchParams); - if(searchParams.sorted) - std::sort(IndicesDists.begin(), IndicesDists.end(), IndexDist_Sorter()); - return nFound; + throw std::runtime_error( + "[nanoflann] findNeighbors() called before building the " + "index."); } + float epsError = 1 + searchParams.eps; - /** - * Just like radiusSearch() but with a custom callback class for each point - * found in the radius of the query. See the source of RadiusResultSet<> as a - * start point for your own classes. \sa radiusSearch - */ - template - size_t radiusSearchCustomCallback(const ElementType * query_point, SEARCH_CALLBACK & resultSet, - const SearchParams & searchParams = SearchParams()) const + // fixed or variable-sized container (depending on DIM) + distance_vector_t dists; + // Fill it with zeros. + auto zero = static_cast(0); + assign(dists, (DIM > 0 ? DIM : Base::dim_), zero); + DistanceType dist = this->computeInitialDistances(*this, vec, dists); + searchLevel(result, vec, Base::root_node_, dist, dists, epsError); + + if (searchParams.sorted) + { + result.sort(); + } + + return result.full(); + } + + /** + * Find all points contained within the specified bounding box. Their + * indices are stored inside the result object. + * + * Params: + * result = the result object in which the indices of the points + * within the bounding box are stored + * bbox = the bounding box defining the search region + * + * \tparam RESULTSET Should be any ResultSet + * \return Number of points found within the bounding box. + * \sa findNeighbors, knnSearch, radiusSearch + * + * \note The search is inclusive - points on the boundary are included. + */ + template + Size findWithinBox(RESULTSET& result, const BoundingBox& bbox) const + { + if (this->size(*this) == 0) + { + return 0; + } + if (!Base::root_node_) { - this->findNeighbors(resultSet, query_point, searchParams); - return resultSet.size(); + throw std::runtime_error( + "[nanoflann] findWithinBox() called before building the " + "index."); } - /** @} */ + std::stack stack; + stack.push(Base::root_node_); - public: - void computeBoundingBox(BoundingBox & bbox) + while (!stack.empty()) { - resize(bbox, (DIM > 0 ? DIM : BaseClassRef::dim)); + const NodePtr node = stack.top(); + stack.pop(); - if(dataset.kdtree_get_bbox(bbox)) + // If this is a leaf node, then do check and return. + if (!node->child1) // (if one node is nullptr, both are) { - // Done! It was implemented in derived class + for (Offset i = node->node_type.lr.left; + i < node->node_type.lr.right; + ++i) + { + if (contains(bbox, Base::vAcc_[i])) + { + if (!result.addPoint(0, Base::vAcc_[i])) + { + // the resultset doesn't want to receive any more + // points, we're done searching! + return result.size(); + } + } + } } else { - const size_t N = BaseClassRef::m_size; - if(!N) - throw std::runtime_error("[nanoflann] computeBoundingBox() called but " - "no data points found."); - for(int i = 0; i < (DIM > 0 ? DIM : BaseClassRef::dim); ++i) + const int idx = node->node_type.sub.divfeat; + const auto low_bound = node->node_type.sub.divlow; + const auto high_bound = node->node_type.sub.divhigh; + + if (bbox[idx].low <= low_bound) { - bbox[i].low = bbox[i].high = this->dataset_get(*this, BaseClassRef::vind[0], i); + stack.push(node->child1); } - for(size_t k = 1; k < N; ++k) + if (bbox[idx].high >= high_bound) { - for(int i = 0; i < (DIM > 0 ? DIM : BaseClassRef::dim); ++i) - { - if(this->dataset_get(*this, BaseClassRef::vind[k], i) < bbox[i].low) - bbox[i].low = this->dataset_get(*this, BaseClassRef::vind[k], i); - if(this->dataset_get(*this, BaseClassRef::vind[k], i) > bbox[i].high) - bbox[i].high = this->dataset_get(*this, BaseClassRef::vind[k], i); - } + stack.push(node->child2); } } } - /** - * Performs an exact search in the tree starting from a node. - * \tparam RESULTSET Should be any ResultSet - */ - template - void searchLevel(RESULTSET & result_set, const ElementType * vec, const NodePtr node, DistanceType mindistsq, - distance_vector_t & dists, const float epsError) const + return result.size(); + } + + /** + * Find the "num_closest" nearest neighbors to the \a query_point[0:dim-1]. + * Their indices and distances are stored in the provided pointers to + * array/vector. + * + * \sa radiusSearch, findNeighbors + * \return Number `N` of valid points in the result set. + * + * \note If L2 norms are used, all returned distances are actually squared + * distances. + * + * \note Only the first `N` entries in `out_indices` and `out_distances` + * will be valid. Return is less than `num_closest` only if the + * number of elements in the tree is less than `num_closest`. + */ + Size knnSearch( + const ElementType* query_point, + const Size num_closest, + IndexType* out_indices, + DistanceType* out_distances) const + { + nanoflann::KNNResultSet resultSet(num_closest); + resultSet.init(out_indices, out_distances); + findNeighbors(resultSet, query_point); + return resultSet.size(); + } + + /** + * Find all the neighbors to \a query_point[0:dim-1] within a maximum + * radius. The output is given as a vector of pairs, of which the first + * element is a point index and the second the corresponding distance. + * Previous contents of \a IndicesDists are cleared. + * + * If searchParams.sorted==true, the output list is sorted by ascending + * distances. + * + * For a better performance, it is advisable to do a .reserve() on the + * vector if you have any wild guess about the number of expected matches. + * + * \sa knnSearch, findNeighbors, radiusSearchCustomCallback + * \return The number of points within the given radius (i.e. indices.size() + * or dists.size() ) + * + * \note If L2 norms are used, search radius and all returned distances + * are actually squared distances. + */ + Size radiusSearch( + const ElementType* query_point, + const DistanceType& radius, + std::vector>& IndicesDists, + const SearchParameters& searchParams = {}) const + { + RadiusResultSet resultSet( + radius, + IndicesDists); + const Size nFound = + radiusSearchCustomCallback(query_point, resultSet, searchParams); + return nFound; + } + + /** + * Just like radiusSearch() but with a custom callback class for each point + * found in the radius of the query. See the source of RadiusResultSet<> as + * a start point for your own classes. \sa radiusSearch + */ + template + Size radiusSearchCustomCallback( + const ElementType* query_point, + SEARCH_CALLBACK& resultSet, + const SearchParameters& searchParams = {}) const + { + findNeighbors(resultSet, query_point, searchParams); + return resultSet.size(); + } + + /** + * Find the first N neighbors to \a query_point[0:dim-1] within a maximum + * radius. The output is given as a vector of pairs, of which the first + * element is a point index and the second the corresponding distance. + * Previous contents of \a IndicesDists are cleared. + * + * \sa radiusSearch, findNeighbors + * \return Number `N` of valid points in the result set. + * + * \note If L2 norms are used, all returned distances are actually squared + * distances. + * + * \note Only the first `N` entries in `out_indices` and `out_distances` + * will be valid. Return is less than `num_closest` only if the + * number of elements in the tree is less than `num_closest`. + */ + Size rknnSearch( + const ElementType* query_point, + const Size num_closest, + IndexType* out_indices, + DistanceType* out_distances, + const DistanceType& radius) const + { + nanoflann::RKNNResultSet resultSet( + num_closest, + radius); + resultSet.init(out_indices, out_distances); + findNeighbors(resultSet, query_point); + return resultSet.size(); + } + + /** @} */ + +public: + /** Make sure the auxiliary list \a vind has the same size as the + * current dataset, and re-generate if size has changed. */ + void init_vind() + { + // Create a permutable array of indices to the input vectors. + Base::size_ = dataset_.kdtree_get_point_count(); + if (Base::vAcc_.size() != Base::size_) { - /* If this is a leaf node, then do check and return. */ - if((node->child1 == NULL) && (node->child2 == NULL)) + Base::vAcc_.resize(Base::size_); + } + for (IndexType i = 0; i < static_cast(Base::size_); i++) + { + Base::vAcc_[i] = i; + } + } + + void computeBoundingBox(BoundingBox& bbox) + { + const auto dims = (DIM > 0 ? DIM : Base::dim_); + resize(bbox, dims); + if (dataset_.kdtree_get_bbox(bbox)) + { + // Done! It was implemented in derived class + } + else + { + const Size N = dataset_.kdtree_get_point_count(); + if (!N) + { + throw std::runtime_error( + "[nanoflann] computeBoundingBox() called but " + "no data points found."); + } + for (Dimension i = 0; i < dims; ++i) + { + bbox[i].low = bbox[i].high = + this->dataset_get(*this, Base::vAcc_[0], i); + } + for (Offset k = 1; k < N; ++k) { - // count_leaf += (node->lr.right-node->lr.left); // Removed since was - // neither used nor returned to the user. - DistanceType worst_dist = result_set.worstDist(); - for(IndexType i = node->node_type.lr.left; i < node->node_type.lr.right; ++i) + for (Dimension i = 0; i < dims; ++i) { - const IndexType index = BaseClassRef::vind[i]; // reorder... : i; - if(treeIndex[index] == -1) - continue; - DistanceType dist = distance.evalMetric(vec, index, (DIM > 0 ? DIM : BaseClassRef::dim)); - if(dist < worst_dist) + const auto val = + this->dataset_get(*this, Base::vAcc_[k], i); + if (val < bbox[i].low) { - if(!result_set.addPoint(static_cast(dist), - static_cast(BaseClassRef::vind[i]))) - { - // the resultset doesn't want to receive any more points, we're done - // searching! - return; // false; - } + bbox[i].low = val; + } + if (val > bbox[i].high) + { + bbox[i].high = val; } } - return; } + } + } - /* Which child branch should be taken first? */ - int idx = node->node_type.sub.divfeat; - ElementType val = vec[idx]; - DistanceType diff1 = val - node->node_type.sub.divlow; - DistanceType diff2 = val - node->node_type.sub.divhigh; - - NodePtr bestChild; - NodePtr otherChild; - DistanceType cut_dist; - if((diff1 + diff2) < 0) + bool contains(const BoundingBox& bbox, IndexType idx) const + { + const auto dims = (DIM > 0 ? DIM : Base::dim_); + for (Dimension i = 0; i < dims; ++i) + { + const auto point = this->dataset_.kdtree_get_pt(idx, i); + if (point < bbox[i].low || point > bbox[i].high) { - bestChild = node->child1; - otherChild = node->child2; - cut_dist = distance.accum_dist(val, node->node_type.sub.divhigh, idx); + return false; } - else + } + return true; + } + + /** + * Performs an exact search in the tree starting from a node. + * \tparam RESULTSET Should be any ResultSet + * \return true if the search should be continued, false if the results are + * sufficient + */ + template + bool searchLevel( + RESULTSET& result_set, + const ElementType* vec, + const NodePtr node, + DistanceType mindist, + distance_vector_t& dists, + const float epsError) const + { + // If this is a leaf node, then do check and return. + if (!node->child1) // (if one node is nullptr, both are) + { + for (Offset i = node->node_type.lr.left; + i < node->node_type.lr.right; + ++i) { - bestChild = node->child2; - otherChild = node->child1; - cut_dist = distance.accum_dist(val, node->node_type.sub.divlow, idx); + const IndexType accessor = Base::vAcc_[i]; // reorder... : i; + DistanceType dist = distance_.evalMetric( + vec, + accessor, + (DIM > 0 ? DIM : Base::dim_)); + if (dist < result_set.worstDist()) + { + if (!result_set.addPoint(dist, Base::vAcc_[i])) + { + // the resultset doesn't want to receive any more + // points, we're done searching! + return false; + } + } } + return true; + } - /* Call recursively to search next level down. */ - searchLevel(result_set, vec, bestChild, mindistsq, dists, epsError); + /* Which child branch should be taken first? */ + Dimension idx = node->node_type.sub.divfeat; + ElementType val = vec[idx]; + DistanceType diff1 = val - node->node_type.sub.divlow; + DistanceType diff2 = val - node->node_type.sub.divhigh; - DistanceType dst = dists[idx]; - mindistsq = mindistsq + cut_dist - dst; - dists[idx] = cut_dist; - if(mindistsq * epsError <= result_set.worstDist()) + NodePtr bestChild; + NodePtr otherChild; + DistanceType cut_dist; + if ((diff1 + diff2) < 0) + { + bestChild = node->child1; + otherChild = node->child2; + cut_dist = + distance_.accum_dist(val, node->node_type.sub.divhigh, idx); + } + else + { + bestChild = node->child2; + otherChild = node->child1; + cut_dist = + distance_.accum_dist(val, node->node_type.sub.divlow, idx); + } + + /* Call recursively to search next level down. */ + if (!searchLevel(result_set, vec, bestChild, mindist, dists, epsError)) + { + // the resultset doesn't want to receive any more points, we're done + // searching! + return false; + } + + DistanceType dst = dists[idx]; + mindist = mindist + cut_dist - dst; + dists[idx] = cut_dist; + if (mindist * epsError <= result_set.worstDist()) + { + if (!searchLevel( + result_set, + vec, + otherChild, + mindist, + dists, + epsError)) { - searchLevel(result_set, vec, otherChild, mindistsq, dists, epsError); + // the resultset doesn't want to receive any more points, we're + // done searching! + return false; } - dists[idx] = dst; - } - - public: - /** Stores the index in a binary file. - * IMPORTANT NOTE: The set of data points is NOT stored in the file, so when - * loading the index object it must be constructed associated to the same - * source of data points used while building it. See the example: - * examples/saveload_example.cpp \sa loadIndex */ - void saveIndex(FILE * stream) { this->saveIndex_(*this, stream); } - - /** Loads a previous index from a binary file. - * IMPORTANT NOTE: The set of data points is NOT stored in the file, so the - * index object must be constructed associated to the same source of data - * points used while building the index. See the example: - * examples/saveload_example.cpp \sa loadIndex */ - void loadIndex(FILE * stream) { this->loadIndex_(*this, stream); } - }; + } + dists[idx] = dst; + return true; + } + +public: + /** Stores the index in a binary file. + * IMPORTANT NOTE: The set of data points is NOT stored in the file, so + * when loading the index object it must be constructed associated to the + * same source of data points used while building it. See the example: + * examples/saveload_example.cpp \sa loadIndex */ + void saveIndex(std::ostream& stream) const + { + Base::saveIndex(*this, stream); + } + + /** Loads a previous index from a binary file. + * IMPORTANT NOTE: The set of data points is NOT stored in the file, so + * the index object must be constructed associated to the same source of + * data points used while building the index. See the example: + * examples/saveload_example.cpp \sa loadIndex */ + void loadIndex(std::istream& stream) { Base::loadIndex(*this, stream); } + +}; // class KDTree - /** kd-tree dynaimic index +/** kd-tree dynamic index + * + * Contains the k-d trees and other information for indexing a set of points + * for nearest-neighbor matching. + * + * The class "DatasetAdaptor" must provide the following interface (can be + * non-virtual, inlined methods): + * + * \code + * // Must return the number of data poins + * size_t kdtree_get_point_count() const { ... } + * + * // Must return the dim'th component of the idx'th point in the class: + * T kdtree_get_pt(const size_t idx, const size_t dim) const { ... } + * + * // Optional bounding-box computation: return false to default to a standard + * bbox computation loop. + * // Return true if the BBOX was already computed by the class and returned + * in "bb" so it can be avoided to redo it again. + * // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 + * for point clouds) template bool kdtree_get_bbox(BBOX &bb) const + * { + * bb[0].low = ...; bb[0].high = ...; // 0th dimension limits + * bb[1].low = ...; bb[1].high = ...; // 1st dimension limits + * ... + * return true; + * } + * + * \endcode + * + * \tparam DatasetAdaptor The user-provided adaptor (see comments above). + * \tparam Distance The distance metric to use: nanoflann::metric_L1, + * nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. + * \tparam DIM Dimensionality of data points (e.g. 3 for 3D points) + * \tparam IndexType Type of the arguments with which the data can be + * accessed (e.g. float, double, int64_t, T*) + */ +template< + typename Distance, + class DatasetAdaptor, + int32_t DIM = -1, + typename IndexType = uint32_t> +class KDTreeSingleIndexDynamicAdaptor_ : + public KDTreeBaseClass< + KDTreeSingleIndexDynamicAdaptor_< + Distance, + DatasetAdaptor, + DIM, + IndexType>, + Distance, + DatasetAdaptor, + DIM, + IndexType> +{ +public: + /** + * The dataset used by this index + */ + const DatasetAdaptor& dataset_; //!< The source of our data + + KDTreeSingleIndexAdaptorParams index_params_; + + std::vector& treeIndex_; + + Distance distance_; + + using Base = typename nanoflann::KDTreeBaseClass< + nanoflann::KDTreeSingleIndexDynamicAdaptor_< + Distance, + DatasetAdaptor, + DIM, + IndexType>, + Distance, + DatasetAdaptor, + DIM, + IndexType>; + + using ElementType = typename Base::ElementType; + using DistanceType = typename Base::DistanceType; + + using Offset = typename Base::Offset; + using Size = typename Base::Size; + using Dimension = typename Base::Dimension; + + using Node = typename Base::Node; + using NodePtr = Node*; + + using Interval = typename Base::Interval; + /** Define "BoundingBox" as a fixed-size or variable-size container + * depending on "DIM" */ + using BoundingBox = typename Base::BoundingBox; + + /** Define "distance_vector_t" as a fixed-size or variable-size container + * depending on "DIM" */ + using distance_vector_t = typename Base::distance_vector_t; + + /** + * KDTree constructor * - * class to create multiple static index and merge their results to behave as - * single dynamic index as proposed in Logarithmic Approach. + * Refer to docs in README.md or online in + * https://github.com/jlblancoc/nanoflann * - * Example of usage: - * examples/dynamic_pointcloud_example.cpp + * The KD-Tree point dimension (the length of each point in the datase, e.g. + * 3 for 3D points) is determined by means of: + * - The \a DIM template parameter if >0 (highest priority) + * - Otherwise, the \a dimensionality parameter of this constructor. * - * \tparam DatasetAdaptor The user-provided adaptor (see comments above). - * \tparam Distance The distance metric to use: nanoflann::metric_L1, - * nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. \tparam DIM - * Dimensionality of data points (e.g. 3 for 3D points) \tparam IndexType Will - * be typically size_t or int + * @param inputData Dataset with the input features. Its lifetime must be + * equal or longer than that of the instance of this class. + * @param params Basically, the maximum leaf node size */ - template - class KDTreeSingleIndexDynamicAdaptor + KDTreeSingleIndexDynamicAdaptor_( + const Dimension dimensionality, + const DatasetAdaptor& inputData, + std::vector& treeIndex, + const KDTreeSingleIndexAdaptorParams& params = + KDTreeSingleIndexAdaptorParams()) : + dataset_(inputData), + index_params_(params), + treeIndex_(treeIndex), + distance_(inputData) { - public: - typedef typename Distance::ElementType ElementType; - typedef typename Distance::DistanceType DistanceType; + Base::size_ = 0; + Base::size_at_index_build_ = 0; + for (auto& v : Base::root_bbox_) + { + v = {}; + } + Base::dim_ = dimensionality; + if (DIM > 0) + { + Base::dim_ = DIM; + } + Base::leaf_max_size_ = params.leaf_max_size; + if (params.n_thread_build > 0) + { + Base::n_thread_build_ = params.n_thread_build; + } + else + { + Base::n_thread_build_ = + std::max(std::thread::hardware_concurrency(), 1u); + } + } - protected: - size_t m_leaf_max_size; - size_t treeCount; - size_t pointCount; + /** Explicitly default the copy constructor */ + KDTreeSingleIndexDynamicAdaptor_( + const KDTreeSingleIndexDynamicAdaptor_& rhs) = default; - /** - * The dataset used by this index - */ - const DatasetAdaptor & dataset; //!< The source of our data + /** Assignment operator definiton */ + KDTreeSingleIndexDynamicAdaptor_ operator=( + const KDTreeSingleIndexDynamicAdaptor_& rhs) + { + KDTreeSingleIndexDynamicAdaptor_ tmp(rhs); + std::swap(Base::vAcc_, tmp.Base::vAcc_); + std::swap(Base::leaf_max_size_, tmp.Base::leaf_max_size_); + std::swap(index_params_, tmp.index_params_); + std::swap(treeIndex_, tmp.treeIndex_); + std::swap(Base::size_, tmp.Base::size_); + std::swap(Base::size_at_index_build_, tmp.Base::size_at_index_build_); + std::swap(Base::root_node_, tmp.Base::root_node_); + std::swap(Base::root_bbox_, tmp.Base::root_bbox_); + std::swap(Base::pool_, tmp.Base::pool_); + return *this; + } - std::vector treeIndex; //!< treeIndex[idx] is the index of tree in which - //!< point at idx is stored. treeIndex[idx]=-1 - //!< means that point has been removed. + /** + * Builds the index + */ + void buildIndex() + { + Base::size_ = Base::vAcc_.size(); + this->freeIndex(*this); + Base::size_at_index_build_ = Base::size_; + if (Base::size_ == 0) + { + return; + } + computeBoundingBox(Base::root_bbox_); + // construct the tree + if (Base::n_thread_build_ == 1) + { + Base::root_node_ = + this->divideTree(*this, 0, Base::size_, Base::root_bbox_); + } + else + { +#ifndef NANOFLANN_NO_THREADS + std::atomic thread_count(0u); + std::mutex mutex; + Base::root_node_ = this->divideTreeConcurrent( + *this, + 0, + Base::size_, + Base::root_bbox_, + thread_count, + mutex); +#else /* NANOFLANN_NO_THREADS */ + throw std::runtime_error("Multithreading is disabled"); +#endif /* NANOFLANN_NO_THREADS */ + } + } - KDTreeSingleIndexAdaptorParams index_params; + /** \name Query methods + * @{ */ - int dim; //!< Dimensionality of each data point + /** + * Find set of nearest neighbors to vec[0:dim-1]. Their indices are stored + * inside the result object. + * This is the core search function, all others are wrappers around this + * one. + * + * \param result The result object in which the indices of the + * nearest-neighbors are stored. + * \param vec The vector of the query point for which to search the + * nearest neighbors. + * \param searchParams Optional parameters for the search. + * + * \tparam RESULTSET Should be any ResultSet + * \return True if the requested neighbors could be found. + * + * \sa knnSearch(), radiusSearch(), radiusSearchCustomCallback() + * + * \note If L2 norms are used, all returned distances are actually squared + * distances. + */ + template + bool findNeighbors( + RESULTSET& result, + const ElementType* vec, + const SearchParameters& searchParams = {}) const + { + assert(vec); + if (this->size(*this) == 0) + { + return false; + } + if (!Base::root_node_) + { + return false; + } + float epsError = 1 + searchParams.eps; + + // fixed or variable-sized container (depending on DIM) + distance_vector_t dists; + // Fill it with zeros. + assign( + dists, + (DIM > 0 ? DIM : Base::dim_), + static_cast(0)); + DistanceType dist = this->computeInitialDistances(*this, vec, dists); + searchLevel(result, vec, Base::root_node_, dist, dists, epsError); + + if (searchParams.sorted) + { + result.sort(); + } + + return result.full(); + } + + /** + * Find the "num_closest" nearest neighbors to the \a query_point[0:dim-1]. + * Their indices are stored inside the result object. \sa radiusSearch, + * findNeighbors + * \return Number `N` of valid points in + * the result set. + * + * \note If L2 norms are used, all returned distances are actually squared + * distances. + * + * \note Only the first `N` entries in `out_indices` and `out_distances` + * will be valid. Return may be less than `num_closest` only if the + * number of elements in the tree is less than `num_closest`. + */ + Size knnSearch( + const ElementType* query_point, + const Size num_closest, + IndexType* out_indices, + DistanceType* out_distances, + const SearchParameters& searchParams = {}) const + { + nanoflann::KNNResultSet resultSet(num_closest); + resultSet.init(out_indices, out_distances); + findNeighbors(resultSet, query_point, searchParams); + return resultSet.size(); + } + + /** + * Find all the neighbors to \a query_point[0:dim-1] within a maximum + * radius. The output is given as a vector of pairs, of which the first + * element is a point index and the second the corresponding distance. + * Previous contents of \a IndicesDists are cleared. + * + * If searchParams.sorted==true, the output list is sorted by ascending + * distances. + * + * For a better performance, it is advisable to do a .reserve() on the + * vector if you have any wild guess about the number of expected matches. + * + * \sa knnSearch, findNeighbors, radiusSearchCustomCallback + * \return The number of points within the given radius (i.e. indices.size() + * or dists.size() ) + * + * \note If L2 norms are used, search radius and all returned distances + * are actually squared distances. + */ + Size radiusSearch( + const ElementType* query_point, + const DistanceType& radius, + std::vector>& IndicesDists, + const SearchParameters& searchParams = {}) const + { + RadiusResultSet resultSet( + radius, + IndicesDists); + const size_t nFound = + radiusSearchCustomCallback(query_point, resultSet, searchParams); + return nFound; + } + + /** + * Just like radiusSearch() but with a custom callback class for each point + * found in the radius of the query. See the source of RadiusResultSet<> as + * a start point for your own classes. \sa radiusSearch + */ + template + Size radiusSearchCustomCallback( + const ElementType* query_point, + SEARCH_CALLBACK& resultSet, + const SearchParameters& searchParams = {}) const + { + findNeighbors(resultSet, query_point, searchParams); + return resultSet.size(); + } - typedef KDTreeSingleIndexDynamicAdaptor_ index_container_t; - std::vector index; + /** @} */ - public: - /** Get a const ref to the internal list of indices; the number of indices is - * adapted dynamically as the dataset grows in size. */ - const std::vector & getAllIndices() const { return index; } +public: + void computeBoundingBox(BoundingBox& bbox) + { + const auto dims = (DIM > 0 ? DIM : Base::dim_); + resize(bbox, dims); - private: - /** finds position of least significant unset bit */ - int First0Bit(IndexType num) + if (dataset_.kdtree_get_bbox(bbox)) + { + // Done! It was implemented in derived class + } + else + { + const Size N = Base::size_; + if (!N) + { + throw std::runtime_error( + "[nanoflann] computeBoundingBox() called but " + "no data points found."); + } + for (Dimension i = 0; i < dims; ++i) + { + bbox[i].low = bbox[i].high = + this->dataset_get(*this, Base::vAcc_[0], i); + } + for (Offset k = 1; k < N; ++k) + { + for (Dimension i = 0; i < dims; ++i) + { + const auto val = + this->dataset_get(*this, Base::vAcc_[k], i); + if (val < bbox[i].low) + { + bbox[i].low = val; + } + if (val > bbox[i].high) + { + bbox[i].high = val; + } + } + } + } + } + + /** + * Performs an exact search in the tree starting from a node. + * \tparam RESULTSET Should be any ResultSet + */ + template + void searchLevel( + RESULTSET& result_set, + const ElementType* vec, + const NodePtr node, + DistanceType mindist, + distance_vector_t& dists, + const float epsError) const + { + // If this is a leaf node, then do check and return. + if (!node->child1) // (if one node is nullptr, both are) { - int pos = 0; - while(num & 1) + for (Offset i = node->node_type.lr.left; + i < node->node_type.lr.right; + ++i) { - num = num >> 1; - pos++; + const IndexType index = Base::vAcc_[i]; // reorder... : i; + if (treeIndex_[index] == -1) + { + continue; + } + DistanceType dist = distance_.evalMetric( + vec, + index, + (DIM > 0 ? DIM : Base::dim_)); + if (dist < result_set.worstDist()) + { + if (!result_set.addPoint( + static_cast(dist), + static_cast( + Base::vAcc_[i]))) + { + // the resultset doesn't want to receive any more + // points, we're done searching! + return; // false; + } + } } - return pos; + return; } - /** Creates multiple empty trees to handle dynamic support */ - void init() + /* Which child branch should be taken first? */ + Dimension idx = node->node_type.sub.divfeat; + ElementType val = vec[idx]; + DistanceType diff1 = val - node->node_type.sub.divlow; + DistanceType diff2 = val - node->node_type.sub.divhigh; + + NodePtr bestChild; + NodePtr otherChild; + DistanceType cut_dist; + if ((diff1 + diff2) < 0) + { + bestChild = node->child1; + otherChild = node->child2; + cut_dist = + distance_.accum_dist(val, node->node_type.sub.divhigh, idx); + } + else { - typedef KDTreeSingleIndexDynamicAdaptor_ my_kd_tree_t; - std::vector index_(treeCount, my_kd_tree_t(dim /*dim*/, dataset, treeIndex, index_params)); - index = index_; + bestChild = node->child2; + otherChild = node->child1; + cut_dist = + distance_.accum_dist(val, node->node_type.sub.divlow, idx); } - public: - Distance distance; + /* Call recursively to search next level down. */ + searchLevel(result_set, vec, bestChild, mindist, dists, epsError); - /** - * KDTree constructor - * - * Refer to docs in README.md or online in - * https://github.com/jlblancoc/nanoflann - * - * The KD-Tree point dimension (the length of each point in the datase, e.g. 3 - * for 3D points) is determined by means of: - * - The \a DIM template parameter if >0 (highest priority) - * - Otherwise, the \a dimensionality parameter of this constructor. - * - * @param inputData Dataset with the input features - * @param params Basically, the maximum leaf node size - */ - KDTreeSingleIndexDynamicAdaptor( - const int dimensionality, const DatasetAdaptor & inputData, - const KDTreeSingleIndexAdaptorParams & params = KDTreeSingleIndexAdaptorParams(), - const size_t maximumPointCount = 1000000000U) : - dataset(inputData), index_params(params), distance(inputData) - { - treeCount = static_cast(std::log2(maximumPointCount)); - pointCount = 0U; - dim = dimensionality; - treeIndex.clear(); - if(DIM > 0) - dim = DIM; - m_leaf_max_size = params.leaf_max_size; - init(); - const size_t num_initial_points = dataset.kdtree_get_point_count(); - if(num_initial_points > 0) - { - addPoints(0, num_initial_points - 1); - } + DistanceType dst = dists[idx]; + mindist = mindist + cut_dist - dst; + dists[idx] = cut_dist; + if (mindist * epsError <= result_set.worstDist()) + { + searchLevel(result_set, vec, otherChild, mindist, dists, epsError); } + dists[idx] = dst; + } + +public: + /** Stores the index in a binary file. + * IMPORTANT NOTE: The set of data points is NOT stored in the file, so + * when loading the index object it must be constructed associated to the + * same source of data points used while building it. See the example: + * examples/saveload_example.cpp \sa loadIndex */ + void saveIndex(std::ostream& stream) { saveIndex(*this, stream); } + + /** Loads a previous index from a binary file. + * IMPORTANT NOTE: The set of data points is NOT stored in the file, so + * the index object must be constructed associated to the same source of + * data points used while building the index. See the example: + * examples/saveload_example.cpp \sa loadIndex */ + void loadIndex(std::istream& stream) { loadIndex(*this, stream); } +}; + +/** kd-tree dynaimic index + * + * class to create multiple static index and merge their results to behave as + * single dynamic index as proposed in Logarithmic Approach. + * + * Example of usage: + * examples/dynamic_pointcloud_example.cpp + * + * \tparam DatasetAdaptor The user-provided adaptor (see comments above). + * \tparam Distance The distance metric to use: nanoflann::metric_L1, + * nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. \tparam DIM + * Dimensionality of data points (e.g. 3 for 3D points) \tparam IndexType + * Will be typically size_t or int + */ +template< + typename Distance, + class DatasetAdaptor, + int32_t DIM = -1, + typename IndexType = uint32_t> +class KDTreeSingleIndexDynamicAdaptor +{ +public: + using ElementType = typename Distance::ElementType; + using DistanceType = typename Distance::DistanceType; + + using Offset = typename KDTreeSingleIndexDynamicAdaptor_< + Distance, + DatasetAdaptor, + DIM>::Offset; + using Size = typename KDTreeSingleIndexDynamicAdaptor_< + Distance, + DatasetAdaptor, + DIM>::Size; + using Dimension = typename KDTreeSingleIndexDynamicAdaptor_< + Distance, + DatasetAdaptor, + DIM>::Dimension; + +protected: + Size leaf_max_size_; + Size treeCount_; + Size pointCount_; + + /** + * The dataset used by this index + */ + const DatasetAdaptor& dataset_; //!< The source of our data + + /** treeIndex[idx] is the index of tree in which point at idx is stored. + * treeIndex[idx]=-1 means that point has been removed. */ + std::vector treeIndex_; + std::unordered_set removedPoints_; - /** Deleted copy constructor*/ - KDTreeSingleIndexDynamicAdaptor( - const KDTreeSingleIndexDynamicAdaptor &) = delete; + KDTreeSingleIndexAdaptorParams index_params_; - /** Add points to the set, Inserts all points from [start, end] */ - void addPoints(IndexType start, IndexType end) + Dimension dim_; //!< Dimensionality of each data point + + using index_container_t = KDTreeSingleIndexDynamicAdaptor_< + Distance, + DatasetAdaptor, + DIM, + IndexType>; + std::vector index_; + +public: + /** Get a const ref to the internal list of indices; the number of indices + * is adapted dynamically as the dataset grows in size. */ + const std::vector& getAllIndices() const + { + return index_; + } + +private: + /** finds position of least significant unset bit */ + int First0Bit(IndexType num) + { + int pos = 0; + while (num & 1) { - size_t count = end - start + 1; - treeIndex.resize(treeIndex.size() + count); - for(IndexType idx = start; idx <= end; idx++) + num = num >> 1; + pos++; + } + return pos; + } + + /** Creates multiple empty trees to handle dynamic support */ + void init() + { + using my_kd_tree_t = KDTreeSingleIndexDynamicAdaptor_< + Distance, + DatasetAdaptor, + DIM, + IndexType>; + std::vector index( + treeCount_, + my_kd_tree_t(dim_ /*dim*/, dataset_, treeIndex_, index_params_)); + index_ = index; + } + +public: + Distance distance_; + + /** + * KDTree constructor + * + * Refer to docs in README.md or online in + * https://github.com/jlblancoc/nanoflann + * + * The KD-Tree point dimension (the length of each point in the datase, e.g. + * 3 for 3D points) is determined by means of: + * - The \a DIM template parameter if >0 (highest priority) + * - Otherwise, the \a dimensionality parameter of this constructor. + * + * @param inputData Dataset with the input features. Its lifetime must be + * equal or longer than that of the instance of this class. + * @param params Basically, the maximum leaf node size + */ + explicit KDTreeSingleIndexDynamicAdaptor( + const int dimensionality, + const DatasetAdaptor& inputData, + const KDTreeSingleIndexAdaptorParams& params = + KDTreeSingleIndexAdaptorParams(), + const size_t maximumPointCount = 1000000000U) : + dataset_(inputData), + index_params_(params), + distance_(inputData) + { + treeCount_ = static_cast(std::log2(maximumPointCount)) + 1; + pointCount_ = 0U; + dim_ = dimensionality; + treeIndex_.clear(); + if (DIM > 0) + { + dim_ = DIM; + } + leaf_max_size_ = params.leaf_max_size; + init(); + const size_t num_initial_points = dataset_.kdtree_get_point_count(); + if (num_initial_points > 0) + { + addPoints(0, num_initial_points - 1); + } + } + + /** Deleted copy constructor*/ + explicit KDTreeSingleIndexDynamicAdaptor( + const KDTreeSingleIndexDynamicAdaptor< + Distance, + DatasetAdaptor, + DIM, + IndexType>&) = delete; + + /** Add points to the set, Inserts all points from [start, end] */ + void addPoints(IndexType start, IndexType end) + { + const Size count = end - start + 1; + int maxIndex = 0; + treeIndex_.resize(treeIndex_.size() + count); + for (IndexType idx = start; idx <= end; idx++) + { + const int pos = First0Bit(pointCount_); + maxIndex = std::max(pos, maxIndex); + treeIndex_[pointCount_] = pos; + + const auto it = removedPoints_.find(idx); + if (it != removedPoints_.end()) + { + removedPoints_.erase(it); + treeIndex_[idx] = pos; + } + + for (int i = 0; i < pos; i++) { - int pos = First0Bit(pointCount); - index[pos].vind.clear(); - treeIndex[pointCount] = pos; - for(int i = 0; i < pos; i++) + for (int j = 0; j < static_cast(index_[i].vAcc_.size()); + j++) { - for(int j = 0; j < static_cast(index[i].vind.size()); j++) + index_[pos].vAcc_.push_back(index_[i].vAcc_[j]); + if (treeIndex_[index_[i].vAcc_[j]] != -1) { - index[pos].vind.push_back(index[i].vind[j]); - if(treeIndex[index[i].vind[j]] != -1) - treeIndex[index[i].vind[j]] = pos; + treeIndex_[index_[i].vAcc_[j]] = pos; } - index[i].vind.clear(); - index[i].freeIndex(index[i]); } - index[pos].vind.push_back(idx); - index[pos].buildIndex(); - pointCount++; + index_[i].vAcc_.clear(); } + index_[pos].vAcc_.push_back(idx); + pointCount_++; } - /** Remove a point from the set (Lazy Deletion) */ - void removePoint(size_t idx) + for (int i = 0; i <= maxIndex; ++i) { - if(idx >= pointCount) - return; - treeIndex[idx] = -1; + index_[i].freeIndex(index_[i]); + if (!index_[i].vAcc_.empty()) + { + index_[i].buildIndex(); + } } + } - /** - * Find set of nearest neighbors to vec[0:dim-1]. Their indices are stored - * inside the result object. - * - * Params: - * result = the result object in which the indices of the - * nearest-neighbors are stored vec = the vector for which to search the - * nearest neighbors - * - * \tparam RESULTSET Should be any ResultSet - * \return True if the requested neighbors could be found. - * \sa knnSearch, radiusSearch - */ - template - bool findNeighbors(RESULTSET & result, const ElementType * vec, const SearchParams & searchParams) const + /** Remove a point from the set (Lazy Deletion) */ + void removePoint(size_t idx) + { + if (idx >= pointCount_) { - for(size_t i = 0; i < treeCount; i++) { index[i].findNeighbors(result, &vec[0], searchParams); } - return result.full(); + return; } - }; + removedPoints_.insert(idx); + treeIndex_[idx] = -1; + } - /** An L2-metric KD-tree adaptor for working with data directly stored in an - * Eigen Matrix, without duplicating the data storage. You can select whether a - * row or column in the matrix represents a point in the state space. + /** + * Find set of nearest neighbors to vec[0:dim-1]. Their indices are stored + * inside the result object. * - * Example of usage: - * \code - * Eigen::Matrix mat; - * // Fill out "mat"... + * Params: + * result = the result object in which the indices of the + * nearest-neighbors are stored vec = the vector for which to search the + * nearest neighbors * - * typedef KDTreeEigenMatrixAdaptor< Eigen::Matrix > - * my_kd_tree_t; const int max_leaf = 10; my_kd_tree_t mat_index(mat, max_leaf - * ); mat_index.index->buildIndex(); mat_index.index->... \endcode + * \tparam RESULTSET Should be any ResultSet + * \return True if the requested neighbors could be found. + * \sa knnSearch, radiusSearch * - * \tparam DIM If set to >0, it specifies a compile-time fixed dimensionality - * for the points in the data set, allowing more compiler optimizations. \tparam - * Distance The distance metric to use: nanoflann::metric_L1, - * nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. \tparam row_major - * If set to true the rows of the matrix are used as the points, if set to false - * the columns of the matrix are used as the points. + * \note If L2 norms are used, all returned distances are actually squared + * distances. */ - template - struct KDTreeEigenMatrixAdaptor - { - typedef KDTreeEigenMatrixAdaptor self_t; - typedef typename MatrixType::Scalar num_t; - typedef typename MatrixType::Index IndexType; - typedef typename Distance::template traits::distance_t metric_t; - typedef KDTreeSingleIndexAdaptor index_t; - - index_t * index; //! The kd-tree index for the user to call its methods as - //! usual with any other FLANN index. - - /// Constructor: takes a const ref to the matrix object with the data points - KDTreeEigenMatrixAdaptor(const size_t dimensionality, const std::reference_wrapper & mat, - const int leaf_max_size = 10) : - m_data_matrix(mat) - { - const auto dims = row_major ? mat.get().cols() : mat.get().rows(); - if(size_t(dims) != dimensionality) - throw std::runtime_error("Error: 'dimensionality' must match column count in data matrix"); - if(DIM > 0 && int(dims) != DIM) - throw std::runtime_error("Data set dimensionality does not match the 'DIM' template argument"); - index = new index_t(static_cast(dims), *this /* adaptor */, - nanoflann::KDTreeSingleIndexAdaptorParams(leaf_max_size)); - index->buildIndex(); - } - - public: - /** Deleted copy constructor */ - KDTreeEigenMatrixAdaptor(const self_t &) = delete; - - ~KDTreeEigenMatrixAdaptor() { delete index; } - - const std::reference_wrapper m_data_matrix; - - /** Query for the \a num_closest closest points to a given point (entered as - * query_point[0:dim-1]). Note that this is a short-cut method for - * index->findNeighbors(). The user can also call index->... methods as - * desired. \note nChecks_IGNORED is ignored but kept for compatibility with - * the original FLANN interface. - */ - inline void query(const num_t * query_point, const size_t num_closest, IndexType * out_indices, - num_t * out_distances_sq, const int /* nChecks_IGNORED */ = 10) const + template + bool findNeighbors( + RESULTSET& result, + const ElementType* vec, + const SearchParameters& searchParams = {}) const + { + for (size_t i = 0; i < treeCount_; i++) { - nanoflann::KNNResultSet resultSet(num_closest); - resultSet.init(out_indices, out_distances_sq); - index->findNeighbors(resultSet, query_point, nanoflann::SearchParams()); + index_[i].findNeighbors(result, &vec[0], searchParams); } + return result.full(); + } +}; + +/** An L2-metric KD-tree adaptor for working with data directly stored in an + * Eigen Matrix, without duplicating the data storage. You can select whether a + * row or column in the matrix represents a point in the state space. + * + * Example of usage: + * \code + * Eigen::Matrix mat; + * + * // Fill out "mat"... + * using my_kd_tree_t = nanoflann::KDTreeEigenMatrixAdaptor< + * Eigen::Matrix>; + * + * const int max_leaf = 10; + * my_kd_tree_t mat_index(mat, max_leaf); + * mat_index.index->... + * \endcode + * + * \tparam DIM If set to >0, it specifies a compile-time fixed dimensionality + * for the points in the data set, allowing more compiler optimizations. + * \tparam Distance The distance metric to use: nanoflann::metric_L1, + * nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. + * \tparam row_major If set to true the rows of the matrix are used as the + * points, if set to false the columns of the matrix are used as the + * points. + */ +template< + class MatrixType, + int32_t DIM = -1, + class Distance = nanoflann::metric_L2, + bool row_major = true> +struct KDTreeEigenMatrixAdaptor +{ + using self_t = + KDTreeEigenMatrixAdaptor; + using num_t = typename MatrixType::Scalar; + using IndexType = typename MatrixType::Index; + using metric_t = typename Distance:: + template traits::distance_t; + + using index_t = KDTreeSingleIndexAdaptor< + metric_t, + self_t, + row_major ? MatrixType::ColsAtCompileTime + : MatrixType::RowsAtCompileTime, + IndexType>; + + index_t* index_; //! The kd-tree index for the user to call its methods as + //! usual with any other FLANN index. + + using Offset = typename index_t::Offset; + using Size = typename index_t::Size; + using Dimension = typename index_t::Dimension; + + /// Constructor: takes a const ref to the matrix object with the data points + explicit KDTreeEigenMatrixAdaptor( + const Dimension dimensionality, + const std::reference_wrapper& mat, + const int leaf_max_size = 10, + const unsigned int n_thread_build = 1) : + m_data_matrix(mat) + { + const auto dims = row_major ? mat.get().cols() : mat.get().rows(); + if (static_cast(dims) != dimensionality) + { + throw std::runtime_error( + "Error: 'dimensionality' must match column count in data " + "matrix"); + } + if (DIM > 0 && static_cast(dims) != DIM) + { + throw std::runtime_error( + "Data set dimensionality does not match the 'DIM' template " + "argument"); + } + index_ = new index_t( + dims, + *this /* adaptor */, + nanoflann::KDTreeSingleIndexAdaptorParams( + leaf_max_size, + nanoflann::KDTreeSingleIndexAdaptorFlags::None, + n_thread_build)); + } + +public: + /** Deleted copy constructor */ + KDTreeEigenMatrixAdaptor(const self_t&) = delete; - /** @name Interface expected by KDTreeSingleIndexAdaptor - * @{ */ + ~KDTreeEigenMatrixAdaptor() { delete index_; } - const self_t & derived() const { return *this; } - self_t & derived() { return *this; } + const std::reference_wrapper m_data_matrix; - // Must return the number of data points - inline size_t kdtree_get_point_count() const + /** Query for the \a num_closest closest points to a given point (entered as + * query_point[0:dim-1]). Note that this is a short-cut method for + * index->findNeighbors(). The user can also call index->... methods as + * desired. + * + * \note If L2 norms are used, all returned distances are actually squared + * distances. + */ + void query( + const num_t* query_point, + const Size num_closest, + IndexType* out_indices, + num_t* out_distances) const + { + nanoflann::KNNResultSet resultSet(num_closest); + resultSet.init(out_indices, out_distances); + index_->findNeighbors(resultSet, query_point); + } + + /** @name Interface expected by KDTreeSingleIndexAdaptor + * @{ */ + + inline const self_t& derived() const noexcept { return *this; } + inline self_t& derived() noexcept { return *this; } + + // Must return the number of data points + inline Size kdtree_get_point_count() const + { + if (row_major) { - if(row_major) - return m_data_matrix.get().rows(); - else - return m_data_matrix.get().cols(); + return m_data_matrix.get().rows(); } - - // Returns the dim'th component of the idx'th point in the class: - inline num_t kdtree_get_pt(const IndexType idx, size_t dim) const + else { - if(row_major) - return m_data_matrix.get().coeff(idx, IndexType(dim)); - else - return m_data_matrix.get().coeff(IndexType(dim), idx); + return m_data_matrix.get().cols(); } + } - // Optional bounding-box computation: return false to default to a standard - // bbox computation loop. - // Return true if the BBOX was already computed by the class and returned in - // "bb" so it can be avoided to redo it again. Look at bb.size() to find out - // the expected dimensionality (e.g. 2 or 3 for point clouds) - template - bool kdtree_get_bbox(BBOX & /*bb*/) const + // Returns the dim'th component of the idx'th point in the class: + inline num_t kdtree_get_pt(const IndexType idx, size_t dim) const + { + if (row_major) { - return false; + return m_data_matrix.get().coeff(idx, IndexType(dim)); } + else + { + return m_data_matrix.get().coeff(IndexType(dim), idx); + } + } - /** @} */ + // Optional bounding-box computation: return false to default to a standard + // bbox computation loop. + // Return true if the BBOX was already computed by the class and returned + // in "bb" so it can be avoided to redo it again. Look at bb.size() to + // find out the expected dimensionality (e.g. 2 or 3 for point clouds) + template + inline bool kdtree_get_bbox(BBOX& /*bb*/) const + { + return false; + } - }; // end of KDTreeEigenMatrixAdaptor /** @} */ - /** @} */ // end of grouping -} // namespace nanoflann +}; // end of KDTreeEigenMatrixAdaptor +/** @} */ + +/** @} */ // end of grouping +} // namespace nanoflann -#endif /* NANOFLANN_HPP_ */ +#undef NANOFLANN_RESTRICT diff --git a/include/nano_gicp/nanoflann.hpp b/include/nano_gicp/nanoflann.hpp index 31fcc35..7909561 100644 --- a/include/nano_gicp/nanoflann.hpp +++ b/include/nano_gicp/nanoflann.hpp @@ -96,7 +96,7 @@ class KdTreeFLANN std::vector& k_sqr_distances) const; protected: - nanoflann::SearchParams _params; + nanoflann::SearchParameters _params; struct PointCloud_Adaptor { @@ -166,7 +166,7 @@ inline int KdTreeFLANN::nearestKSearch( nanoflann::KNNResultSet resultSet(num_closest); resultSet.init(k_indices.data(), k_sqr_distances.data()); - _kdtree.findNeighbors(resultSet, point.data, nanoflann::SearchParams()); + _kdtree.findNeighbors(resultSet, point.data, nanoflann::SearchParameters()); return resultSet.size(); } @@ -177,7 +177,7 @@ inline int KdTreeFLANN::radiusSearch( std::vector& k_indices, std::vector& k_sqr_distances) const { - static std::vector> indices_dist; + static std::vector> indices_dist; indices_dist.reserve(128); RadiusResultSet resultSet(radius, indices_dist); diff --git a/include/point_def.hpp b/include/point_def.hpp index 7cac314..4ba4126 100644 --- a/include/point_def.hpp +++ b/include/point_def.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -43,7 +43,11 @@ #define PCL_NO_PRECOMPILE #endif #include + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #include +#pragma GCC diagnostic pop namespace csm @@ -87,137 +91,31 @@ struct EIGEN_ALIGN16 PointXYZR PCL_MAKE_ALIGNED_OPERATOR_NEW }; -struct EIGEN_ALIGN16 PointXYZIR -{ - PCL_ADD_POINT4D; - float intensity; - float reflective; - - inline constexpr PointXYZIR(const PointXYZIR& p) : - PointXYZIR(p.x, p.y, p.z, p.intensity, p.reflective) - { - } - inline constexpr PointXYZIR() : PointXYZIR(0.f, 0.f, 0.f, 0.f, 0.f) {} - inline constexpr PointXYZIR(float _x, float _y, float _z) : - PointXYZIR(_x, _y, _z, 0.f, 0.f) - { - } - inline constexpr PointXYZIR( - float _x, - float _y, - float _z, - float _intensity, - float _reflective) : - data{_x, _y, _z, 1.f}, - intensity{_intensity}, - reflective{_reflective} - { - } - - inline constexpr PointXYZIR& operator=(const PointXYZIR& p) - { - x = p.x; - y = p.y; - z = p.z; - intensity = p.intensity; - reflective = p.reflective; - return *this; - } - - PCL_MAKE_ALIGNED_OPERATOR_NEW -}; - -struct EIGEN_ALIGN16 PointXYZRT -{ - PCL_ADD_POINT4D; - float reflective; - union - { - struct - { - uint32_t tl, th; - }; - uint64_t t; - }; - - inline uint64_t integer_time() const - { - return this->t; - } - static inline uint64_t time_base() - { - return 1000000; - } -}; - struct EIGEN_ALIGN8 PointSDir { float azimuth; float elevation; - inline float theta() const - { - return this->azimuth; - } - inline float phi() const - { - return (3.1415926f / 2.f) - this->elevation; - } + inline float theta() const { return this->azimuth; } + inline float phi() const { return (3.1415926f / 2.f) - this->elevation; } }; struct EIGEN_ALIGN8 PointT_32HL { union { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" struct { uint32_t tl, th; }; +#pragma GCC diagnostic pop uint64_t t; }; - inline uint64_t integer_time() const - { - return this->t; - } - static inline uint64_t time_base() - { - return 1000000; - } -}; - -struct NormalTraversal : public pcl::_Normal -{ - inline constexpr NormalTraversal(const _Normal& p) : - NormalTraversal{ - p.normal_x, - p.normal_y, - p.normal_z, - p.curvature, - p.data_c[1]} - { - } - inline constexpr NormalTraversal( - float _curvature = 0.f, - float _trav_weight = 0.f) : - NormalTraversal{0.f, 0.f, 0.f, _curvature, _trav_weight} - { - } - inline constexpr NormalTraversal( - float n_x, - float n_y, - float n_z, - float _curvature = 0.f, - float _trav_weight = 0.f) : - _Normal{{{n_x, n_y, n_z, 0.f}}, {{_curvature}}} - { - this->data_c[1] = _trav_weight; - } - - inline float& trav_weight() { return this->data_c[1]; } - inline float trav_weight() const { return this->data_c[1]; } - - PCL_MAKE_ALIGNED_OPERATOR_NEW + inline uint64_t integer_time() const { return this->t; } + static inline uint64_t time_base() { return 1000000; } }; }; // namespace perception @@ -231,22 +129,6 @@ POINT_CLOUD_REGISTER_POINT_STRUCT( (float, z, z) (float, reflective, reflective)) -POINT_CLOUD_REGISTER_POINT_STRUCT( - csm::perception::PointXYZIR, - (float, x, x) - (float, y, y) - (float, z, z)(float, intensity, intensity) - (float, reflective, reflective)) - -POINT_CLOUD_REGISTER_POINT_STRUCT( - csm::perception::PointXYZRT, - (float, x, x) - (float, y, y) - (float, z, z) - (float, reflective, reflective) - (uint32_t, tl, tl) - (uint32_t, th, th)) - POINT_CLOUD_REGISTER_POINT_STRUCT( csm::perception::PointSDir, (float, azimuth, azimuth) @@ -257,9 +139,6 @@ POINT_CLOUD_REGISTER_POINT_STRUCT( (uint32_t, tl, tl) (uint32_t, th, th)) -POINT_CLOUD_REGISTER_POINT_WRAPPER( - csm::perception::NormalTraversal, - pcl::_Normal) // clang-format on @@ -272,17 +151,7 @@ namespace traits template struct has_reflective : public std::bool_constant< - std::is_same::value || - std::is_same::value || - std::is_same::value> -{ -}; - -template -struct has_intensity : - public std::bool_constant< - std::is_same::value || - pcl::traits::has_intensity::value> + std::is_same::value> { }; @@ -296,17 +165,9 @@ struct has_spherical : template struct has_integer_time : public std::bool_constant< - std::is_same::value || std::is_same::value> { }; -template -struct has_trav_weight : - public std::bool_constant< - std::is_same::value> -{ -}; - }; // namespace traits }; // namespace util diff --git a/include/traversibility_def.hpp b/include/traversibility_def.hpp new file mode 100644 index 0000000..a2c8915 --- /dev/null +++ b/include/traversibility_def.hpp @@ -0,0 +1,358 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include +#include +#include +#include + +#include + + +namespace csm +{ +namespace perception +{ + +namespace traversibility +{ + +/* WeightEval does nothing for non-specialized types. */ +template +struct WeightEval; + +/* Currently specialized evaluators are: + * float + * double + * pcl::PointXYZI (intensity field) + * pcl::PointXYZINormal (intensity field) */ + +/* WeightEval specialization for float. Just a wrapper. */ +template<> +struct WeightEval +{ + using EvalT = float; + using WeightT = float; + + inline static WeightT& eval(EvalT& v) { return v; } + inline static const WeightT& eval(const EvalT& v) { return v; } +}; +/* WeightEval specialization for float. Just a wrapper. */ +template<> +struct WeightEval +{ + using EvalT = double; + using WeightT = double; + + inline static WeightT& eval(EvalT& v) { return v; } + inline static const WeightT& eval(const EvalT& v) { return v; } +}; +/* WeightEval specialization for pcl::PointXYZI. Exposes the intensity member. */ +template<> +struct WeightEval +{ + using EvalT = pcl::PointXYZI; + using WeightT = float; + + inline static WeightT& eval(EvalT& v) { return v.intensity; } + inline static const WeightT& eval(const EvalT& v) { return v.intensity; } +}; +/* WeightEval specialization for pcl::PointXYZINormal. Exposes the intensity member. */ +template<> +struct WeightEval +{ + using EvalT = pcl::PointXYZINormal; + using WeightT = float; + + inline static WeightT& eval(EvalT& v) { return v.intensity; } + inline static const WeightT& eval(const EvalT& v) { return v.intensity; } +}; + + +/* Traits struct to test if WeightEval is specialized for a given type and + * the eval() function is defined correctly. */ +template +struct has_weight_eval : std::false_type +{ +}; +template +struct has_weight_eval< + T, + std::void_t< + typename WeightEval::WeightT, + decltype(WeightEval::eval(std::declval()))>> : std::true_type +{ +}; + +/* Weighting-type mapping shortcut for valid WeightEval<> specializations. */ +template +using weight_t = typename WeightEval::WeightT; + +/* Checks if a weight evaluator is defined, the mapped weight-type is the same as the + * input type, and if this type is trivial (can be constexpr evaluated) */ +template +inline constexpr bool can_constexpr_eval_weight = + has_weight_eval::value && std::is_same_v> && + std::is_trivially_constructible_v && std::is_trivially_copyable_v; + + +/* Shortcut to unwrap the weight of any type for which an evaluator is defined. */ +template +inline weight_t& weight(T& x) +{ + static_assert( + has_weight_eval::value, + "Type does not support traversibility weight evaluation"); + + return WeightEval::eval(x); +} +/* Shortcut to unwrap the weight of any type for which an evaluator is defined. */ +template +inline const weight_t& weight(const T& x) +{ + static_assert( + has_weight_eval::value, + "Type does not support traversibility weight evaluation"); + + return WeightEval::eval(x); +} + +/* Shortcuts wrapper-like evaluators so that constexpr evaluation can be preserved. + * (Assumes wrapper-like eval API's are indeed wrappers, which is currently the + * case. Be careful - this assumption may not always be true!) */ +template +inline constexpr weight_t constexprWeight(const T& x) +{ + if constexpr (can_constexpr_eval_weight) + { + return x; + } + else + { + return weight(x); + } +} + + + +/* Traversibility/Pathplanning definition: + * Nominal range is the always traversible - lower is more traversible, higher is less + * Extended range is possibly traversible - higher is less + * Anything higher than extended range bound is a definite obstacle + * Anything lower than nominal range min signals a frontier node + * NaN signals unknown traversibility */ + +/* Traversibility weight nominal range minimum bound (inclusive). + * Value type is that which is mapped by the weight eval specialization + * given the template param. */ +template +inline constexpr weight_t NOMINAL_MIN_WEIGHT = [] +{ + static_assert(has_weight_eval::value); + return weight_t(0); +}(); +/* Traversibility weight nominal range maximum bound (inclusive). + * [This is the same value as the extended range minimum bound (exclusive)] + * Value type is that which is mapped by the weight eval specialization + * given the template param. */ +template +inline constexpr weight_t NOMINAL_MAX_WEIGHT = [] +{ + static_assert(has_weight_eval::value); + return weight_t(1); +}(); +/* Traversibility weight extended range maximum bound (inclusive). + * Value type is that which is mapped by the weight eval specialization + * given the template param. */ +template +inline constexpr weight_t EXTENDED_MAX_WEIGHT = [] +{ + static_assert(has_weight_eval::value); + return weight_t(10); +}(); + + +/* Marker value which denotes the traversibility weight of a "definite obstacle" + * Value type is that which is mapped by the weight eval specialization + * given the template param. */ +template +inline constexpr weight_t OBSTACLE_MARKER_VAL = [] +{ + static_assert( + has_weight_eval::value && + std::numeric_limits>::has_infinity); + return std::numeric_limits>::infinity(); +}(); +/* Marker value which denotes the traversibility weight of a frontier node (path-planning only) + * Value type is that which is mapped by the weight eval specialization + * given the template param. */ +template +inline constexpr weight_t FRONTIER_MARKER_VAL = [] +{ + static_assert( + has_weight_eval::value && + std::numeric_limits>::has_infinity); + return -std::numeric_limits>::infinity(); +}(); +/* Marker value which denotes an unknown traversibility weight + * Value type is that which is mapped by the weight eval specialization + * given the template param. */ +template +inline constexpr weight_t UNKNOWN_MARKER_VAL = [] +{ + static_assert( + has_weight_eval::value && + std::numeric_limits>::has_quiet_NaN); + return std::numeric_limits>::quiet_NaN(); +}(); + + +/* Denormalize the input value (range [0, 1]) to the equivalent weight + * in the range [NOMINAL_MIN_WEIGHT, NOMINAL_MAX_WEIGHT]. + * Input value must be floating point, and return value is of the type + * mapped by the equivalent weight evaluator given the template parameter T. + * (Set T to the container type, not the weight type!) */ +template +inline constexpr weight_t nominalWeight(Fp normalized_val) +{ + static_assert(std::is_floating_point::value); + + constexpr weight_t NOMINAL_RANGE = + (NOMINAL_MAX_WEIGHT - NOMINAL_MIN_WEIGHT); + + return static_cast>(normalized_val) * NOMINAL_RANGE + + NOMINAL_MIN_WEIGHT; +} +/* Denormalize the input value (range [0, 1]) to the equivalent weight + * in the range [NOMINAL_MAX_WEIGHT, EXTENDED_MAX_WEIGHT]. + * Input value must be floating point, and return value is of the type + * mapped by the equivalent weight evaluator given the template parameter T. + * (Set T to the container type, not the weight type!)*/ +template +inline constexpr weight_t extendedWeight(Fp normalized_val) +{ + static_assert(std::is_floating_point::value); + + constexpr weight_t EXTENDED_RANGE = + (EXTENDED_MAX_WEIGHT - NOMINAL_MAX_WEIGHT); + + return static_cast>(normalized_val) * EXTENDED_RANGE + + NOMINAL_MAX_WEIGHT; +} + + +/* Test if a traversibility weight is in the nominal range - that is - + * [NOMINAL_MIN_WEIGHT, NOMINAL_MAX_WEIGHT]. + * Weight evaluators are automatically applied to "unbox" the weight value. */ +template +inline constexpr bool isNominal(const T& val) +{ + weight_t w = constexprWeight(val); + return w >= NOMINAL_MIN_WEIGHT && w <= NOMINAL_MAX_WEIGHT; +} +/* Test if a traversibility weight is in the extended range - that is - + * (NOMINAL_MAX_WEIGHT, EXTENDED_MAX_WEIGHT]. + * Weight evaluators are automatically applied to "unbox" the weight value. */ +template +inline constexpr bool isExtended(const T& val) +{ + weight_t w = constexprWeight(val); + return w > NOMINAL_MAX_WEIGHT && w <= EXTENDED_MAX_WEIGHT; +} +/* Test if a traversibility weight is in the nominal or extended range - ie. + * it't weight value is meaningful and not just a marker. + * Weight evaluators are automatically applied to "unbox" the weight value. */ +template +inline constexpr bool isWeighted(const T& val) +{ + weight_t w = constexprWeight(val); + return w >= NOMINAL_MIN_WEIGHT && w <= EXTENDED_MAX_WEIGHT; +} +/* Test if a traversibility weight denotes a definite obstacle. + * Weight evaluators are automatically applied to "unbox" the weight value. */ +template +inline constexpr bool isObstacle(const T& val) +{ + return constexprWeight(val) > EXTENDED_MAX_WEIGHT; +} +/* Test if a traversibility weight denotes a frontier node. + * Weight evaluators are automatically applied to "unbox" the weight value. */ +template +inline constexpr bool isFrontier(const T& val) +{ + return constexprWeight(val) < NOMINAL_MIN_WEIGHT; +} +/* Test if a traversibility weight is unknown. + * Weight evaluators are automatically applied to "unbox" the weight value. */ +template +inline constexpr bool isUnknown(const T& val) +{ + return std::isnan(constexprWeight(val)); +} +/* Test if a traversibility weight should be read as a marker - ie. it's weight + * value is not meaningful as a weight. + * Weight evaluators are automatically applied to "unbox" the weight value. */ +template +inline constexpr bool isMarker(const T& val) +{ + return !isWeighted(val); +} + +}; // namespace traversibility + +}; // namespace perception +}; // namespace csm + + +namespace util +{ +namespace traits +{ + +template +using supports_traversibility = + csm::perception::traversibility::has_weight_eval; + +template +using traversibility_weight_t = csm::perception::traversibility::weight_t; + +}; // namespace traits +}; // namespace util diff --git a/include/cloud_ops.hpp b/include/util/cloud_ops.hpp similarity index 78% rename from include/cloud_ops.hpp rename to include/util/cloud_ops.hpp index a36b68e..c190109 100644 --- a/include/cloud_ops.hpp +++ b/include/util/cloud_ops.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -42,16 +42,16 @@ #include #include #include -#include #include +#include #include #include #include -#include -#include #include +#include +#include #include // ^ includes and // which we use @@ -59,8 +59,7 @@ // ^ includes #include -#include -#include +#include "std_utils.hpp" namespace util @@ -71,60 +70,6 @@ namespace util static_assert(std::is_floating_point::value); -#if 0 -/** Get the min/max of first N dimensions for a selection of points. Does not - * properly handle non-dense clouds. */ -template< - int Ndims = 3, - typename PointT = pcl::PointXYZ, - typename IntT = pcl::index_t, - typename FloatT = float> -void minMaxND( - const pcl::PointCloud& cloud, - const std::vector& selection, - Eigen::Vector& min, - Eigen::Vector& max -) { - static_assert(Ndims > 0 && Ndims <= 4, ""); - using VecT = Eigen::Vector; - - min.setConstant( std::numeric_limits::max() ); - max.setConstant( std::numeric_limits::min() ); - - if(selection.empty()) { - for(const PointT& pt : cloud.points) { - const VecT* pt2 = reinterpret_cast(&pt); - min = min.cwiseMin(*pt2); - max = max.cwiseMax(*pt2); - } - } else { - for(const IntT i : selection) { - const VecT* pt2 = reinterpret_cast(&cloud.points[i]); - min = min.cwiseMin(*pt2); - max = max.cwiseMax(*pt2); - } - } -} - -/** minMaxND<>() alias for getting min/max for x and y */ -template< - typename PointT = pcl::PointXYZ, - typename IntT = pcl::index_t, - typename FloatT = float> -inline void minMaxXY( - const pcl::PointCloud& cloud, - const std::vector& selection, - Eigen::Vector2& min, - Eigen::Vector2& max -) { - return minMaxND<2, PointT, IntT, FloatT>( - cloud, - selection, - min, - max - ); -} -#endif /** minMaxND<>() alias for getting min/max for x, y, and z */ template @@ -171,7 +116,7 @@ template< typename PointT = pcl::PointXYZ, typename IntT = pcl::index_t, bool DownSampleAllData = false> -void voxel_filter( +void voxelFilter( const pcl::PointCloud& cloud, pcl::PointCloud& voxelized, Eigen::Vector3f leaf_size_, @@ -191,7 +136,7 @@ void voxel_filter( voxelized.is_dense = true; // we filter out invalid points Eigen::Vector3f min_p, max_p; - minMaxXYZ(cloud, min_p, max_p, selection); + util::minMaxXYZ(cloud, min_p, max_p, selection); // Check that the leaf size is not too small, given the size of the data int64_t dx = @@ -356,76 +301,14 @@ template< typename PointT = pcl::PointXYZ, typename IntT = pcl::index_t, bool UseNegative = false> -void cropbox_filter( - const pcl::PointCloud& cloud, - std::vector& filtered, - const Eigen::Vector3f min_pt_ = Eigen::Vector3f::Constant(-1.f), - const Eigen::Vector3f max_pt_ = Eigen::Vector3f::Constant(1.f), - const std::vector* selection = nullptr) -{ - ASSERT_POINT_HAS_XYZ(PointT) - - filtered.clear(); - // reserve maximum size - filtered.reserve(selection ? selection->size() : cloud.size()); - - for (size_t idx = 0;; idx++) - { - size_t i = idx; - if (selection) - { - if (idx >= selection->size()) - { - break; - } - i = static_cast((*selection)[idx]); - } - else if (idx >= cloud.points.size()) - { - break; - } - - const PointT& pt = cloud[i]; - if (!cloud.is_dense && !pcl::isFinite(pt)) - { - continue; - } - - if ((pt.x < min_pt_[0] || pt.y < min_pt_[1] || pt.z < min_pt_[2]) || - (pt.x > max_pt_[0] || pt.y > max_pt_[1] || pt.z > max_pt_[2])) - { - if constexpr (UseNegative) - { - // outside the cropbox --> push on negative - filtered.push_back(i); - } - } - else if constexpr (!UseNegative) - { - // inside the cropbox and not negative - filtered.push_back(i); - } - } -} - - - -/** cropbox_filter<>() specialization for sorting exclusively using the - * z-coordinate */ -template< - typename PointT = pcl::PointXYZ, - typename IntT = pcl::index_t, - typename FloatT = float, - bool UseNegative = false> -void carteZ_filter( +void cropFilter( const pcl::PointCloud& cloud, std::vector& filtered, - const FloatT min_z = -1.f, - const FloatT max_z = 1.f, + const Eigen::Vector3f min_pt, + const Eigen::Vector3f max_pt, const std::vector* selection = nullptr) { ASSERT_POINT_HAS_XYZ(PointT) - ASSERT_FLOATING_POINT(FloatT) filtered.clear(); // reserve maximum size @@ -447,13 +330,14 @@ void carteZ_filter( break; } - const PointT& pt = cloud[i]; + const PointT& pt = cloud.points[i]; if (!cloud.is_dense && !pcl::isFinite(pt)) { continue; } - if (pt.z < min_z || pt.z > max_z) + if ((pt.getArray3fMap() < min_pt.array()).any() || + (pt.getArray3fMap() > max_pt.array()).any()) { if constexpr (UseNegative) { @@ -551,7 +435,7 @@ template< // bool mirror_z = false, typename PointT = pcl::PointXYZ, typename IntT = pcl::index_t> -void progressive_morph_filter( +void progressiveMorphFilter( const pcl::PointCloud& cloud_, pcl::Indices& ground, const float base_, @@ -624,9 +508,9 @@ void progressive_morph_filter( const std::shared_ptr > cloud_shared_ref = - wrap_unmanaged(cloud_); + util::wrapUnmanaged(cloud_); const std::shared_ptr ground_shared_ref = - wrap_unmanaged(ground); + util::wrapUnmanaged(ground); pcl::octree::OctreePointCloudSearch tree{1.f}; // csm::perception::SelectionOctree tree{ window_sizes[0] }; @@ -657,21 +541,21 @@ void progressive_morph_filter( const float half_res = window_sizes[i] / 2.0f; // calculate points within each window (for each point in the selection) - for (size_t _idx = 0; _idx < ground.size(); _idx++) + for (size_t idx = 0; idx < ground.size(); idx++) { - const PointT& _pt = - cloud_[ground[_idx]]; // retrieve source (x, y) for each pt in - // selection + const PointT& pt = + cloud_[ground[idx]]; // retrieve source (x, y) for each pt in + // selection tree.boxSearch( Eigen::Vector3f{ - _pt.x - half_res, - _pt.y - half_res, + pt.x - half_res, + pt.y - half_res, std::numeric_limits::lowest()}, Eigen::Vector3f{ - _pt.x + half_res, - _pt.y + half_res, + pt.x + half_res, + pt.y + half_res, std::numeric_limits::max()}, - pt_window_indices[_idx] // output into the cache + pt_window_indices[idx] // output into the cache ); } @@ -679,21 +563,21 @@ void progressive_morph_filter( for (size_t p_idx = 0; p_idx < ground.size(); p_idx++) { const pcl::Indices& pt_indices = pt_window_indices[p_idx]; - float& _zp_temp = zp_temp[ground[p_idx]]; - float& _zn_temp = zn_temp[ground[p_idx]]; - _zp_temp = _zn_temp = cloud_[ground[p_idx]].z; + float& zp_temp_i = zp_temp[ground[p_idx]]; + float& zn_temp_i = zn_temp[ground[p_idx]]; + zp_temp_i = zn_temp_i = cloud_[ground[p_idx]].z; for (const pcl::index_t window_idx : pt_indices) { - const float _z = cloud_[window_idx].z; + const float z = cloud_[window_idx].z; - if (_z < _zp_temp) + if (z < zp_temp_i) { - _zp_temp = _z; + zp_temp_i = z; } - if (_z > _zn_temp) + if (z > zn_temp_i) { - _zn_temp = _z; + zn_temp_i = z; } } } @@ -702,30 +586,29 @@ void progressive_morph_filter( for (size_t p_idx = 0; p_idx < ground.size(); p_idx++) { const pcl::Indices& pt_indices = pt_window_indices[p_idx]; - float& _zp_final = zp_final[ground[p_idx]]; - float& _zn_final = zn_final[ground[p_idx]]; - _zp_final = zp_temp[ground[p_idx]]; - _zn_final = zn_temp[ground[p_idx]]; + float& zp_final_i = zp_final[ground[p_idx]]; + float& zn_final_i = zn_final[ground[p_idx]]; + zp_final_i = zp_temp[ground[p_idx]]; + zn_final_i = zn_temp[ground[p_idx]]; for (const pcl::index_t window_idx : pt_indices) { - const float _zp = zp_temp[window_idx], - _zn = zn_temp[window_idx]; + const float zp = zp_temp[window_idx], zn = zn_temp[window_idx]; - if (_zp > _zp_final) + if (zp > zp_final_i) { - _zp_final = _zp; + zp_final_i = zp; } - if (_zn < _zn_final) + if (zn < zn_final_i) { - _zn_final = _zn; + zn_final_i = zn; } } } // Find indices of the points whose difference between the source and // filtered point clouds is less than the current height threshold. - size_t _slot = 0; + size_t slot = 0; for (size_t p_idx = 0; p_idx < ground.size(); p_idx++) { const float diff_p = @@ -736,15 +619,15 @@ void progressive_morph_filter( // pt is part of ground if (diff_p < height_thresholds[i] && diff_n < height_thresholds[i]) { - if (_slot != p_idx) + if (slot != p_idx) { // tree.removeIndex(ground[_slot], true); - ground[_slot] = ground[p_idx]; + ground[slot] = ground[p_idx]; } - _slot++; + slot++; } } - ground.resize(_slot); + ground.resize(slot); } } @@ -759,7 +642,7 @@ template< typename pcl::PointCloud::VectorType::allocator_type, typename IntT = pcl::index_t, typename FloatT = float> -void pc_generate_ranges( +void generateRanges( const std::vector& points, std::vector& out_ranges, const Eigen::Vector3 origin = Eigen::Vector3::Zero(), @@ -796,12 +679,12 @@ template< typename PointT = pcl::PointXYZ, typename IntT = pcl::index_t, typename FloatT = float> -inline void pc_generate_ranges( +inline void generateRanges( const pcl::PointCloud& points, std::vector& out_ranges, const std::vector* selection = nullptr) { - pc_generate_ranges( + util::generateRanges( points.points, out_ranges, Eigen::Vector3{ @@ -811,7 +694,7 @@ inline void pc_generate_ranges( /** Filter a set of ranges to an inclusive set of indices */ template -void pc_filter_ranges( +void filterRanges( const std::vector& ranges, std::vector& filtered, const FloatT min, @@ -855,7 +738,7 @@ template< typename pcl::PointCloud::VectorType::allocator_type, typename IntT = pcl::index_t, typename FloatT = float> -void pc_filter_distance( +void filterDistance( const std::vector& points, std::vector& filtered, const FloatT min, @@ -898,14 +781,14 @@ template< typename PointT = pcl::PointXYZ, typename IntT = pcl::index_t, typename FloatT = float> -inline void pc_filter_distance( +inline void filterDistance( const pcl::PointCloud& points, std::vector& filtered, const FloatT min, const FloatT max, const std::vector* selection = nullptr) { - pc_filter_distance( + filterDistance( points.points, filtered, min, @@ -919,13 +802,13 @@ inline void pc_filter_distance( template -inline void sort_indices(std::vector& indices) +inline void sortIndices(std::vector& indices) { std::sort(indices.begin(), indices.end()); } template -inline void sort_indices_reverse(std::vector& indices) +inline void reverseSortIndices(std::vector& indices) { std::sort(indices.begin(), indices.end(), std::greater{}); } @@ -934,28 +817,28 @@ inline void sort_indices_reverse(std::vector& indices) /** Given a base set of indices A and a subset of indices B, get (A - B). * prereq: selection indices must be in ascending order */ template -void pc_negate_selection( - const std::vector& base, +void negateSelection( + const std::vector& input, const std::vector& selection, std::vector& negated) { - if (base.size() <= selection.size()) + if (input.size() <= selection.size()) { return; } - negated.resize(base.size() - selection.size()); - size_t _base = 0, _select = 0, _negate = 0; - for (; _base < base.size() && _negate < negated.size(); _base++) + negated.resize(input.size() - selection.size()); + size_t base = 0, select = 0, negate = 0; + for (; base < input.size() && negate < negated.size(); base++) { - if (_select < selection.size() && base[_base] == selection[_select]) + if (select < selection.size() && input[base] == selection[select]) { - _select++; + select++; } else { - negated[_negate] = base[_base]; - _negate++; + negated[negate] = input[base]; + negate++; } } } @@ -963,28 +846,28 @@ void pc_negate_selection( /** Given a base set of indices A and a subset of indices B, get (A - B). * prereq: selection indices must be in ascending order */ template -void pc_negate_selection( - const IntT base_range, +void negateSelection( + const IntT input_range, const std::vector& selection, std::vector& negated) { - if (base_range <= selection.size()) + if (input_range <= selection.size()) { return; } - negated.resize(base_range - selection.size()); - size_t _base = 0, _select = 0, _negate = 0; - for (; _base < base_range && _negate < negated.size(); _base++) + negated.resize(input_range - selection.size()); + size_t base = 0, select = 0, negate = 0; + for (; base < input_range && negate < negated.size(); base++) { - if (_select < selection.size() && _base == selection[_select]) + if (select < selection.size() && base == selection[select]) { - _select++; + select++; } - else // if (_base < selection[_select]) + else // if (base < selection[select]) { - negated[_negate] = _base; - _negate++; + negated[negate] = base; + negate++; } } } @@ -994,7 +877,7 @@ void pc_negate_selection( * (non-descending) * prereq: both selections must be sorted in non-descending order */ template -void pc_combine_sorted( +void combineSortedSelections( const std::vector& sel1, const std::vector& sel2, std::vector& out) @@ -1030,33 +913,31 @@ void pc_combine_sorted( } /** Remove the points at the each index in the provided set. - * Prereq: selection indices must be sorted in non-descending order! */ + * Prereq: selection must be valid for the point set and in increasing order! */ template< typename PointT = pcl::PointXYZ, typename AllocT = typename pcl::PointCloud::VectorType::allocator_type, typename IntT = pcl::index_t> -void pc_remove_selection( +void removeSelection( std::vector& points, const std::vector& selection) { - ASSERT_POINT_HAS_XYZ(PointT) - // assert sizes size_t last = points.size() - 1; - for (int64_t i = static_cast(selection.size()) - 1; i >= 0; i--) + for (size_t i = selection.size(); i-- > 0;) { - points[selection[i]] = points[last]; + points[selection[i]] = std::move(points[last]); last--; } points.resize(last + 1); } template -inline void pc_remove_selection( +inline void removeSelection( pcl::PointCloud& points, const std::vector& selection) { - pc_remove_selection(points.points, selection); + removeSelection(points.points, selection); points.width = points.points.size(); points.height = 1; } @@ -1069,25 +950,23 @@ template< typename AllocT = typename pcl::PointCloud::VectorType::allocator_type, typename IntT = pcl::index_t> -inline void pc_normalize_selection( +inline void trimToSelection( std::vector& points, const std::vector& selection) { - ASSERT_POINT_HAS_XYZ(PointT) - for (size_t i = 0; i < selection.size(); i++) { - points[i] = points[selection[i]]; + points[i] = std::move(points[selection[i]]); } points.resize(selection.size()); } template -inline void pc_normalize_selection( +inline void trimToSelection( pcl::PointCloud& points, const std::vector& selection) { - pc_normalize_selection(points.points, selection); + trimToSelection(points.points, selection); points.width = points.points.size(); points.height = 1; } @@ -1099,13 +978,11 @@ template< typename AllocT = typename pcl::PointCloud::VectorType::allocator_type, typename IntT = pcl::index_t> -inline void pc_copy_selection( +inline void copySelection( const std::vector& points, const std::vector& selection, std::vector& buffer) { - // ASSERT_POINT_HAS_XYZ(PointT) - buffer.resize(selection.size()); for (size_t i = 0; i < selection.size(); i++) { @@ -1114,12 +991,12 @@ inline void pc_copy_selection( } template -inline void pc_copy_selection( +inline void copySelection( const pcl::PointCloud& points, const std::vector& selection, pcl::PointCloud& buffer) { - pc_copy_selection(points.points, selection, buffer.points); + copySelection(points.points, selection, buffer.points); buffer.width = buffer.points.size(); buffer.height = 1; } @@ -1131,37 +1008,35 @@ template< typename AllocT = typename pcl::PointCloud::VectorType::allocator_type, typename IntT = pcl::index_t> -void pc_copy_inverse_selection( +void copyInverseSelection( const std::vector& points, const std::vector& selection, std::vector& buffer) { - ASSERT_POINT_HAS_XYZ(PointT) - buffer.resize(points.size() - selection.size()); - size_t _base = 0, _select = 0, _negate = 0; - for (; _base < points.size() && _negate < buffer.size(); _base++) + size_t base = 0, select = 0, negate = 0; + for (; base < points.size() && negate < buffer.size(); base++) { - if (_select < selection.size() && - _base == static_cast(selection[_select])) + if (select < selection.size() && + base == static_cast(selection[select])) { - _select++; + select++; } - else // if (_base < selection[_select]) + else // if (base < selection[select]) { - buffer[_negate] = points[_base]; - _negate++; + buffer[negate] = points[base]; + negate++; } } } template -inline void pc_copy_inverse_selection( +inline void copyInverseSelection( const pcl::PointCloud& points, const std::vector& selection, pcl::PointCloud& buffer) { - pc_copy_inverse_selection(points.points, selection, buffer.points); + copyInverseSelection(points.points, selection, buffer.points); buffer.width = buffer.points.size(); buffer.height = 1; } diff --git a/include/util/d_ary_heap.hpp b/include/util/d_ary_heap.hpp new file mode 100644 index 0000000..267b533 --- /dev/null +++ b/include/util/d_ary_heap.hpp @@ -0,0 +1,329 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include +#include +#include +#include + + +namespace util +{ + +template< + class Key, // priority key type (float, int, etc.) + class Index = int, // ID type (int32_t, etc.) + class Comparator = std::less, // Min-heap by default + int D = 4> // arity: default 4 +class DAryHeap +{ + static_assert(D >= 2, "D-ary heap requires D >= 2"); + static_assert(std::is_integral::value, "Index must be integral"); + +public: + using key_type = Key; + using index_type = Index; + using size_type = std::size_t; + + explicit DAryHeap(Index max_id = 0, const Comparator& comp = Comparator()) : + comp(comp) + { + if (max_id > 0) + { + reserve_ids(max_id + 1); + } + } + + // Pre-allocate space for IDs in [0, max_id] + void reserve_ids(Index max_id_plus_one) + { + const size_type n = static_cast(max_id_plus_one); + this->pos.assign(n, kAbsent); + this->keys.assign(n, Key{}); + } + + // Reserve heap capacity + void reserve_heap(size_type cap) { this->heap.reserve(cap); } + + bool empty() const noexcept { return this->heap.empty(); } + size_type size() const noexcept { return this->heap.size(); } + + // Is id currently in the heap? + bool contains(Index id) const noexcept + { + return id_in_range(id) && + this->pos[static_cast(id)] != kAbsent; + } + + // Return the id with best key (minimum for Comparator=less) + Index top() const + { + assert(!empty()); + return this->heap[0]; + } + + const Key& top_key() const + { + assert(!empty()); + return this->keys[this->heap[0]]; + } + + // Insert a new (id, key) + void push(Index id, const Key& key) + { + ensure_id(id); + assert(!contains(id)); + this->keys[static_cast(id)] = key; + this->heap.push_back(id); + this->pos[static_cast(id)] = + static_cast(this->heap.size() - 1); + sift_up(this->heap.size() - 1); + } + + // Extract-best (min for Comparator=less) + Index pop() + { + assert(!empty()); + Index root = this->heap[0]; + remove_at_root(); + return root; + } + + // Remove id if present; returns true if removed. + bool erase(Index id) + { + if (!contains(id)) + { + return false; + } + size_type i = static_cast(this->pos[id]); + remove_at(i); + return true; + } + + // Update the key and restore heap order in the right direction. + // If you know it only decreased/increased, call the specialized version. + void update_key(Index id, const Key& new_key) + { + assert(contains(id)); + size_type i = static_cast(this->pos[id]); + const Key& old = this->keys[id]; + this->keys[id] = new_key; + if (is_better(new_key, old)) + { + sift_up(i); + } + else if (is_better(old, new_key)) + { + sift_down(i); + } + // equal: no-op + } + + // Strict decrease (if new_key is known to be better) + void decrease_key(Index id, const Key& new_key) + { + assert(contains(id)); + size_type i = static_cast(this->pos[id]); + // For min-heap, new_key must be strictly better than old + assert(is_better(new_key, this->keys[id])); + this->keys[id] = new_key; + sift_up(i); + } + + // Strict increase (if new_key is known to be worse) + void increase_key(Index id, const Key& new_key) + { + assert(contains(id)); + size_type i = static_cast(this->pos[id]); + assert(is_better(this->keys[id], new_key)); + this->keys[id] = new_key; + sift_down(i); + } + + // Access or set key of an id (valid even if not contained). + const Key& key(Index id) const + { + ensure_id(id); + return this->keys[static_cast(id)]; + } + void set_key(Index id, const Key& k) + { + ensure_id(id); + this->keys[static_cast(id)] = k; + } + +private: + // Storage: + // heap: array of ids in heap order + // pos[id]: index in heap[] or kAbsent if not present + // keys[id]: current key associated with id + std::vector heap; + std::vector pos; + std::vector keys; + Comparator comp; + + static constexpr Index kAbsent = static_cast(-1); + + // Helpers + static constexpr size_type parent(size_type i) noexcept + { + return (i - 1) / D; + } + static constexpr size_type child0(size_type i) noexcept + { + return D * i + 1; + } + + bool id_in_range(Index id) const noexcept + { + return static_cast(id) < this->pos.size(); + } + + void ensure_id(Index id) + { + if (!id_in_range(id)) + { + // Grow pos/keys to accommodate id + size_type new_size = static_cast(id) + 1; + this->pos.resize(new_size, kAbsent); + this->keys.resize(new_size); + } + } + + inline bool better(Index a, Index b) const noexcept + { + // a has higher priority than b if comp(key[a], key[b]) is true + return this->comp(this->keys[a], this->keys[b]); + } + inline bool is_better(const Key& a, const Key& b) const noexcept + { + return this->comp(a, b); + } + + void swap_nodes(size_type i, size_type j) noexcept + { + Index ai = this->heap[i], aj = this->heap[j]; + std::swap(this->heap[i], this->heap[j]); + this->pos[ai] = static_cast(j); + this->pos[aj] = static_cast(i); + } + + void sift_up(size_type i) noexcept + { + while (i > 0) + { + size_type p = parent(i); + if (!better(this->heap[i], this->heap[p])) + { + break; + } + swap_nodes(i, p); + i = p; + } + } + + void sift_down(size_type i) noexcept + { + const size_type n = this->heap.size(); + for (;;) + { + size_type best = i; + size_type c = child0(i); + // Check up to D children + for (int k = 0; k < D; ++k, ++c) + { + if (c < n && better(this->heap[c], this->heap[best])) + { + best = c; + } + } + if (best == i) + { + break; + } + swap_nodes(i, best); + i = best; + } + } + + void remove_at_root() noexcept + { + const size_type last = this->heap.size() - 1; + Index root_id = this->heap[0]; + this->pos[root_id] = kAbsent; + if (last == 0) + { + this->heap.pop_back(); + return; + } + this->heap[0] = this->heap[last]; + this->pos[this->heap[0]] = 0; + this->heap.pop_back(); + sift_down(0); + } + + void remove_at(size_type i) noexcept + { + const size_type last = this->heap.size() - 1; + Index id = this->heap[i]; + this->pos[id] = kAbsent; + if (i == last) + { + this->heap.pop_back(); + return; + } + this->heap[i] = this->heap[last]; + this->pos[this->heap[i]] = static_cast(i); + this->heap.pop_back(); + // Reorder both directions conservatively + if (i > 0 && better(this->heap[i], this->heap[parent(i)])) + { + sift_up(i); + } + else + { + sift_down(i); + } + } +}; + +}; diff --git a/include/geometry.hpp b/include/util/geometry.hpp similarity index 96% rename from include/geometry.hpp rename to include/util/geometry.hpp index 836129d..b4e559d 100644 --- a/include/geometry.hpp +++ b/include/util/geometry.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -176,8 +176,8 @@ namespace geom template inline static auto& fname(param& v) { return v.opr; } \ template inline static auto fname(const param& v) { return v.opr; } - template inline - void cast_assign(T& x, const U& y) + template + inline void castOrAssign(T& x, const U& y) { static_assert(std::is_convertible::value); @@ -231,9 +231,9 @@ namespace geom inline A& cvt(A& a, const B& b) { using namespace util::geom::cvt::vec3::traits; - cvt::cast_assign(x(a), x(b)); - cvt::cast_assign(y(a), y(b)); - cvt::cast_assign(z(a), z(b)); + cvt::castOrAssign(x(a), x(b)); + cvt::castOrAssign(y(a), y(b)); + cvt::castOrAssign(z(a), z(b)); return a; } }; @@ -276,10 +276,10 @@ namespace geom inline A& cvt(A& a, const B& b) { using namespace util::geom::cvt::quat::traits; - cvt::cast_assign(w(a), w(b)); - cvt::cast_assign(x(a), x(b)); - cvt::cast_assign(y(a), y(b)); - cvt::cast_assign(z(a), z(b)); + cvt::castOrAssign(w(a), w(b)); + cvt::castOrAssign(x(a), x(b)); + cvt::castOrAssign(y(a), y(b)); + cvt::castOrAssign(z(a), z(b)); return a; } }; @@ -290,19 +290,19 @@ namespace geom template inline // EIGEN to CV cv_vec4& cvt(cv_vec4& a, const eigen_vec4& b) { - cvt::cast_assign(a[0], b[0]); - cvt::cast_assign(a[1], b[1]); - cvt::cast_assign(a[2], b[2]); - cvt::cast_assign(a[3], b[3]); + cvt::castOrAssign(a[0], b[0]); + cvt::castOrAssign(a[1], b[1]); + cvt::castOrAssign(a[2], b[2]); + cvt::castOrAssign(a[3], b[3]); return a; } template inline // CV to EIGEN eigen_vec4& cvt(eigen_vec4& a, const cv_vec4& b) { - cvt::cast_assign(a[0], b[0]); - cvt::cast_assign(a[1], b[1]); - cvt::cast_assign(a[2], b[2]); - cvt::cast_assign(a[3], b[3]); + cvt::castOrAssign(a[0], b[0]); + cvt::castOrAssign(a[1], b[1]); + cvt::castOrAssign(a[2], b[2]); + cvt::castOrAssign(a[3], b[3]); return a; } #endif @@ -316,7 +316,7 @@ namespace geom { for(std::size_t i = 0; i < N; i++) { - cvt::cast_assign(a[i], b[i]); + cvt::castOrAssign(a[i], b[i]); } return a; } @@ -325,7 +325,7 @@ namespace geom { for(std::size_t i = 0; i < N; i++) { - cvt::cast_assign(a[i], b[i]); + cvt::castOrAssign(a[i], b[i]); } return a; } @@ -618,9 +618,9 @@ namespace geom // pose component ops - /** Simple difference between pose components. THIS IS NOT THE RELATIVE DIFFERENCE (see relative_diff()) */ + /** Simple difference between pose components. THIS IS NOT THE RELATIVE DIFFERENCE (see relativeDiff()) */ template inline - void component_diff( + void componentDiff( geom::Pose3& diff, const geom::Pose3& from, const geom::Pose3& to) @@ -631,12 +631,12 @@ namespace geom /** Get the difference in poses relative to the "from" coordinate frame (manifold difference) */ template inline - void relative_diff( + void relativeDiff( geom::Pose3& diff, const geom::Pose3& from, const geom::Pose3& to) { - component_diff(diff, from, to); + componentDiff(diff, from, to); diff.vec = from.quat.inverse()._transformVector(diff.vec); } /** Append poses that are relative to each other (resulting pose is in the external frame of reference) */ diff --git a/include/meta_grid.hpp b/include/util/meta_grid.hpp similarity index 94% rename from include/meta_grid.hpp rename to include/util/meta_grid.hpp index 445a6ff..2eec990 100644 --- a/include/meta_grid.hpp +++ b/include/util/meta_grid.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -234,6 +234,21 @@ class GridMeta this->origin.z() + (this->dim.z() * this->cell_res.z()))); } + inline Vec2f minBound2() const { return this->origin.template head<2>(); } + inline Vec2f maxBound2() const + { + return this->origin.template head<2>() + + this->dim.template head<2>() + .template cast() + .cwiseProduct(this->cell_res.template head<2>()); + } + inline Vec3f minBound3() const { return this->origin; } + inline Vec3f maxBound3() const + { + return this->origin + + this->dim.template cast().cwiseProduct(this->cell_res); + } + inline const Vec2f cellRes2() const { return this->cell_res.template head<2>(); diff --git a/include/pub_map.hpp b/include/util/pub_map.hpp similarity index 61% rename from include/pub_map.hpp rename to include/util/pub_map.hpp index 84ee1ff..fc8d2f9 100644 --- a/include/pub_map.hpp +++ b/include/util/pub_map.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -62,29 +62,11 @@ #include #include +#include "std_utils.hpp" -namespace util -{ - -template -inline ros_T& to_ros(primitive_T& v) -{ - static_assert( - std::is_same::value && - sizeof(ros_T) == sizeof(primitive_T) && - alignof(ros_T) == alignof(primitive_T)); - - return reinterpret_cast(v); -} -template -inline ros_T to_ros_val(primitive_T v) +namespace util { - static_assert(std::is_same::value); - - return ros_T{}.set__data(v); -} - template struct is_convertible_to_std_msg : std::false_type @@ -110,28 +92,72 @@ struct is_convertible_to_std_msg< std::is_same, std::is_same, std::is_same, - std::is_same >, - void> > : std::is_convertible + std::is_same>, + void>> : std::is_convertible { }; +class PubMapBase +{ +public: + using RclNode = rclcpp::Node; + using RclQoS = rclcpp::QoS; + + template + using Pub = rclcpp::Publisher; + template + using SharedPub = typename Pub::SharedPtr; + + template + using StringMap = std::unordered_map< + std::string, + T, + util::TransparentStringHash, + util::TransparentStringEq>; + +protected: + inline PubMapBase(RclNode& n, std::string_view prefix, const RclQoS& qos) : + node{n}, + default_qos{qos}, + prefix{ + // empty prefix should be left alone because in some cases it is nice to exploit namespace-local topics + prefix.empty() || prefix.back() == '/' ? prefix + : std::string(prefix) + "/"} + { + } + +protected: + inline std::string formatFullTopic(std::string_view topic) + { + // ASSERT TOPIC IS NOT EMPTY!? + return this->prefix + + std::string(topic.front() == '/' ? topic.substr(1) : topic); + } + +protected: + RclNode& node; + RclQoS default_qos; + std::string prefix; +}; + + template -class PubMap +class PubMap : public PubMapBase { public: using MsgT = Msg_T; - using SharedPubT = typename rclcpp::Publisher::SharedPtr; + using SharedPubT = SharedPub; public: + /* Construct a publisher map. + * Prefix may be relative or absolute and gets appended to topics in all method calls. */ inline PubMap( - rclcpp::Node* n, + RclNode& n, std::string_view prefix = "", - const rclcpp::QoS& qos = rclcpp::SensorDataQoS{}) : - node{n}, - default_qos{qos}, - prefix{prefix}, + const RclQoS& qos = rclcpp::SensorDataQoS{}) : + PubMapBase{n, prefix, qos}, publishers{} { } @@ -139,57 +165,44 @@ class PubMap ~PubMap() = default; public: - // wraps the other addPub using the default QoS + /* Add a publisher on the provided topic, using the default QoS */ inline SharedPubT addPub(std::string_view topic) { return this->addPub(topic, this->default_qos); } - // create a publisher and add it to the map - SharedPubT addPub(std::string_view topic, const rclcpp::QoS& qos) + /* Add a publisher on the provided topic, using the provided QoS. + * Does not recreate publisher if the topic is already used. */ + SharedPubT addPub(std::string_view topic, const RclQoS& qos) { - if (!this->node) - { - return nullptr; - } + std::lock_guard lock{this->mtx}; - std::string full; - if (this->prefix.empty()) + auto it = this->publishers.find(topic); + if (it != this->publishers.end()) { - full = topic; - } - else - { -#if __cpp_lib_string_view >= 202403 // from cppreference - full = this->prefix + topic; -#else - full = this->prefix + std::string{topic}; -#endif + return it->second; } - std::lock_guard _lock{this->mtx}; - auto ptr = this->publishers.insert( - {std::string{topic}, - this->node->template create_publisher(full, qos)}); + auto ptr = this->publishers.emplace( + topic, + this->node.template create_publisher( + this->formatFullTopic(topic), + qos)); if (ptr.second && ptr.first->second) { return ptr.first->second; } + return nullptr; } - // extract a publisher from its topic + /* Find and return a publisher if it exists for the provided topic, + * otherwise returns nullptr. */ SharedPubT findPub(std::string_view topic) { - if (!this->node) - { - return nullptr; - } - - std::shared_lock _lock{this->mtx}; - auto search = this->publishers.find(std::string{topic}); - _lock.unlock(); + std::shared_lock lock{this->mtx}; + auto search = this->publishers.find(topic); if (search == this->publishers.end()) { return nullptr; @@ -200,7 +213,8 @@ class PubMap } } - // extract or add if not already present + /* Get the publisher for the provided topic if it exists, + * otherwise creates a new one with the default QoS. */ SharedPubT getPub(std::string_view topic) { SharedPubT p = this->findPub(topic); @@ -211,13 +225,13 @@ class PubMap return p; } - // wraps getPub() + /* Operator wrapper for getPub() */ inline SharedPubT operator[](std::string_view topic) { return this->getPub(topic); } - // publish a value to a topic + /* Publish a value on the provided topic, which is created if not already present. */ template void publish(std::string_view topic, T val) { @@ -227,11 +241,11 @@ class PubMap return; } - if constexpr (std::is_same::value) + if constexpr (std::is_same_v) { p->publish(val); } - else if constexpr (std::is_convertible::value) + else if constexpr (std::is_convertible_v) { p->publish(static_cast(val)); } @@ -240,106 +254,95 @@ class PubMap p->publish( MsgT{}.set__data(static_cast(val))); } + else + { + static_assert( + std::is_same_v || + std::is_convertible_v || + is_convertible_to_std_msg::value, "Error: incompatible types!"); + } } protected: - rclcpp::Node* node = nullptr; - rclcpp::QoS default_qos = 1; - std::string prefix = ""; - - std::unordered_map publishers{}; + StringMap publishers{}; std::shared_mutex mtx; - // }; template<> -class PubMap +class PubMap : public PubMapBase { - template - using Pub = rclcpp::Publisher; - template - using SharedPub = typename Pub::SharedPtr; - - using SharedVoidPtr = std::shared_ptr; + using SharedBasePtr = std::shared_ptr; public: + /* Construct a publisher map. + * Prefix may be relative or absolute and gets appended to topics in all method calls. */ inline PubMap( - rclcpp::Node* n, + RclNode& n, std::string_view prefix = "", - const rclcpp::QoS& qos = 1) : - node{n}, - default_qos{qos}, - prefix{prefix}, - pubs{} + const RclQoS& qos = 1) : + PubMapBase{n, prefix, qos}, + publishers{} { } PubMap(const PubMap&) = delete; ~PubMap() = default; public: + /* Add a publisher on the provided topic, using the default QoS */ template inline SharedPub addPub(std::string_view topic) { return this->addPub(topic, this->default_qos); } + + /* Add a publisher on the provided topic, using the provided QoS. + * Does not recreate publisher if the topic is already used. */ template - inline SharedPub addPub( - std::string_view topic, - const rclcpp::QoS& qos) + inline SharedPub addPub(std::string_view topic, const RclQoS& qos) { - if (!this->node) - { - return nullptr; - } + std::lock_guard lock{this->mtx}; - std::string full; - if (this->prefix.empty()) + // Avoid additional calls to create_publisher + auto it = this->publishers.find(topic); + if (it != this->publishers.end()) { - full = topic; - } - else - { -#if __cpp_lib_string_view >= 202403 // from cppreference - full = this->prefix + topic; -#else - full = this->prefix + std::string{topic}; -#endif + return std::dynamic_pointer_cast>(it->second); } - std::lock_guard lock_{this->mtx}; - auto iter = this->pubs.insert( - {std::string(topic), - this->node->template create_publisher(full, qos)}); + auto iter = this->publishers.emplace( + topic, + this->node.template create_publisher( + this->formatFullTopic(topic), + qos)); if (iter.second && iter.first->second) { - return std::static_pointer_cast>(iter.first->second); + return std::dynamic_pointer_cast>(iter.first->second); } + return nullptr; } + /* Find and return a publisher if it exists for the provided topic, + * otherwise returns nullptr. */ template SharedPub findPub(std::string_view topic) { - if (!this->node) - { - return nullptr; - } + std::shared_lock lock{this->mtx}; - std::shared_lock lock_{this->mtx}; - auto iter = this->pubs.find(std::string(topic)); - lock_.unlock(); - - if (iter == this->pubs.end()) + auto iter = this->publishers.find(topic); + if (iter == this->publishers.end()) { return nullptr; } else { - return std::static_pointer_cast>(iter->second); + return std::dynamic_pointer_cast>(iter->second); } } + /* Get the publisher for the provided topic if it exists, + * otherwise creates a new one with the default QoS. */ template SharedPub getPub(std::string_view topic) { @@ -351,12 +354,14 @@ class PubMap return ptr; } + /* Publish a value on the provided topic, which is created if not already present. */ template inline void publish(std::string_view topic, const MsgT& val) { this->publish(topic, val); } + /* Publish a value on the provided topic, which is created if not already present. */ template void publish(std::string_view topic, const T& val) { @@ -366,32 +371,34 @@ class PubMap return; } - if constexpr (std::is_same::value) + if constexpr (std::is_same_v) { pub->publish(val); return; } - if constexpr (std::is_convertible::value) + else if constexpr (std::is_convertible_v) { pub->publish(static_cast(val)); return; } - if constexpr (is_convertible_to_std_msg::value) + else if constexpr (is_convertible_to_std_msg::value) { pub->publish( MsgT{}.set__data(static_cast(val))); return; } + else + { + static_assert( + std::is_same_v || + std::is_convertible_v || + is_convertible_to_std_msg::value, "Error: incompatible types!"); + } } protected: - rclcpp::Node* node = nullptr; - rclcpp::QoS default_qos = 1; - std::string prefix = ""; - - std::unordered_map pubs; + StringMap publishers; std::shared_mutex mtx; - // }; using GenericPubMap = PubMap; diff --git a/include/util/ros_utils.hpp b/include/util/ros_utils.hpp new file mode 100644 index 0000000..763dcac --- /dev/null +++ b/include/util/ros_utils.hpp @@ -0,0 +1,119 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include +#include + +#include + + +namespace util +{ + +namespace ros_aliases +{ + +using RclNode = rclcpp::Node; +using RclTimer = rclcpp::TimerBase::SharedPtr; + +template +using SharedPub = typename rclcpp::Publisher::SharedPtr; +template +using SharedSub = typename rclcpp::Subscription::SharedPtr; +template +using SharedSrv = typename rclcpp::Service::SharedPtr; + +#define BUILD_MSG_ALIAS(pkg, name) using name##Msg = pkg::msg::name; +#define BUILD_SRV_ALIAS(pkg, name) using name##Srv = pkg::srv::name; +#define BUILD_STD_MSG_ALIAS(name) BUILD_MSG_ALIAS(std_msgs, name) +#define BUILD_SENSORS_MSG_ALIAS(name) BUILD_MSG_ALIAS(sensor_msgs, name) +#define BUILD_GEOM_MSG_ALIAS(name) BUILD_MSG_ALIAS(geometry_msgs, name) +#define BUILD_BUILTIN_MSG_ALIAS(name) BUILD_MSG_ALIAS(builtin_interfaces, name) + +}; // namespace ros_aliases + + +template +struct identity +{ + typedef T type; +}; + +template +inline void declare_param( + rclcpp::Node* node, + const std::string param_name, + T& param, + const typename identity::type& default_value) +{ + node->declare_parameter(param_name, default_value); + node->get_parameter(param_name, param); +} +template +inline void declare_param( + rclcpp::Node& node, + const std::string param_name, + T& param, + const typename identity::type& default_value) +{ + node.declare_parameter(param_name, default_value); + node.get_parameter(param_name, param); +} +template +inline T declare_and_get_param( + rclcpp::Node& node, + const std::string param_name, + const T& default_value) +{ + node.declare_parameter(param_name, default_value); + return node.get_parameter_or(param_name, default_value); +} + + +template +inline ros_T to_ros_val(primitive_T v) +{ + static_assert(std::is_same::value); + + return ros_T{}.set__data(v); +} + +}; // namespace util diff --git a/include/util/std_utils.hpp b/include/util/std_utils.hpp new file mode 100644 index 0000000..f1582e4 --- /dev/null +++ b/include/util/std_utils.hpp @@ -0,0 +1,89 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include +#include +#include + + +namespace util +{ + +/* Create a shared pointer from a stack-allocated variable pointer. Make sure +* the object will outlast the shared pointer scope! */ +template +inline std::shared_ptr wrapUnmanaged(T* x) +{ + return std::shared_ptr(x, [](T*) {}); +} +/* Create a shared pointer from a stack-allocated variable reference. Make sure + * the object will outlast the shared pointer scope! */ +template +inline std::shared_ptr wrapUnmanaged(T& x) +{ + return std::shared_ptr(&x, [](T*) {}); +} + + +struct TransparentStringHash +{ + using is_transparent = void; + size_t operator()(std::string_view s) const noexcept + { + return std::hash{}(s); + } +}; +struct TransparentStringEq +{ + using is_transparent = void; + bool operator()(std::string_view a, std::string_view b) const noexcept + { + return a == b; + } +}; + +}; // namespace util + +#define DECLARE_IMMOVABLE(Typename) \ + Typename(const Typename&) = delete; \ + Typename(Typename&&) = delete; \ + Typename& operator=(const Typename&) = delete; \ + Typename& operator=(Typename&&) = delete; diff --git a/include/synchronization.hpp b/include/util/synchronization.hpp similarity index 97% rename from include/synchronization.hpp rename to include/util/synchronization.hpp index 7829184..a36ea94 100644 --- a/include/synchronization.hpp +++ b/include/util/synchronization.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -45,6 +45,9 @@ #include +namespace util +{ + /** * A.K.A. an "SPSC" * @@ -77,7 +80,7 @@ class ResourcePipeline public: /* Aquire a reference to the current input buffer. Internally locks a control * mutex, so unlockInput() or unlockInputAndNotify() must be called when - * ruffer modification is complete on the current thread to unlock it! */ + * buffer modification is complete on the current thread to unlock it! */ T& lockInput() { this->swap_mtx.lock(); @@ -168,3 +171,5 @@ class ResourcePipeline std::condition_variable resource_notifier; std::atomic resource_available{false}, do_exit{false}; }; + +}; diff --git a/include/util.hpp b/include/util/time_cvt.hpp similarity index 68% rename from include/util.hpp rename to include/util/time_cvt.hpp index 1056dcc..25386fd 100644 --- a/include/util.hpp +++ b/include/util/time_cvt.hpp @@ -1,162 +1,109 @@ -/******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * -* * -* ;xxxxxxx: * -* ;$$$$$$$$$ ...::.. * -* $$$$$$$$$$x .:::::::::::.. * -* x$$$$$$$$$$$$$$::::::::::::::::. * -* :$$$$$&X; .xX:::::::::::::.::... * -* .$$Xx++$$$$+ :::. :;: .::::::. .... : * -* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * -* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * -* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * -* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * -* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * -* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * -* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * -* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * -* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * -* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * -* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * -* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * -* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * -* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * -* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * -* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * -* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * -* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * -* :;;;;;;;;;;;;. :$$$$$$$$$$X * -* .;;;;;;;;:;; +$$$$$$$$$ * -* .;;;;;;. X$$$$$$$: * -* * -* 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. * -* * -*******************************************************************************/ - -#pragma once - -#include -#include -#include -#include - -#include -#include - - -namespace util -{ - -template -struct identity -{ - typedef T type; -}; - -template -inline void declare_param( - rclcpp::Node* node, - const std::string param_name, - T& param, - const typename identity::type& default_value) -{ - node->declare_parameter(param_name, default_value); - node->get_parameter(param_name, param); -} -template -inline void declare_param( - rclcpp::Node& node, - const std::string param_name, - T& param, - const typename identity::type& default_value) -{ - node.declare_parameter(param_name, default_value); - node.get_parameter(param_name, param); -} - - - -inline tf2::TimePoint toTf2TimePoint(const builtin_interfaces::msg::Time& t) -{ - return tf2::TimePoint{ std::chrono::seconds{ t.sec } + - std::chrono::nanoseconds{ t.nanosec } }; -} -inline tf2::TimePoint toTf2TimePoint(const rclcpp::Time& t) -{ - return tf2::TimePoint{ std::chrono::nanoseconds{ t.nanoseconds() } }; -} -inline tf2::TimePoint toTf2TimePoint(double t_secs) -{ - return tf2::timeFromSec(t_secs); -} - -inline double toFloatSeconds(const builtin_interfaces::msg::Time& t) -{ - return static_cast(t.sec) + static_cast(t.nanosec) * 1e-9; -} -inline double toFloatSeconds(const rclcpp::Time& t) -{ - return static_cast(t.nanoseconds()) * 1e-9; -} -inline double toFloatSeconds(const tf2::TimePoint& t) -{ - return tf2::timeToSec(t); -} -template -inline double toFloatSeconds(const std::chrono::duration& dur) -{ - return std::chrono::duration_cast>(dur) - .count(); -} - -template -inline builtin_interfaces::msg::Time toTimeStamp( - const std::chrono::time_point& t) -{ - auto _t = std::chrono::duration_cast( - t.time_since_epoch()) - .count(); - return builtin_interfaces::msg::Time{} - .set__sec(_t / 1000000000) - .set__nanosec(_t % 1000000000); -} -inline builtin_interfaces::msg::Time toTimeStamp(const rclcpp::Time& t) -{ - return static_cast(t); -} -inline builtin_interfaces::msg::Time toTimeStamp(double t_secs) -{ - return builtin_interfaces::msg::Time{} - .set__sec(static_cast(t_secs)) - .set__nanosec( - static_cast( - fmod(t_secs, 1.) * 1e9)); -} - - -/* Create a shared pointer from a stack-allocated variable pointer. Make sure -* the object will outlast the shared pointer scope! */ -template -inline std::shared_ptr wrap_unmanaged(T* x) -{ - return std::shared_ptr(x, [](T*) {}); -} -/* Create a shared pointer from a stack-allocated variable reference. Make sure - * the object will outlast the shared pointer scope! */ -template -inline std::shared_ptr wrap_unmanaged(T& x) -{ - return std::shared_ptr(&x, [](T*) {}); -} - -}; // namespace util - -#define DECLARE_IMMOVABLE(Typename) \ - Typename(const Typename&) = delete; \ - Typename(Typename&&) = delete; \ - Typename& operator=(const Typename&) = delete; \ - Typename& operator=(Typename&&) = delete; +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include +#include + +#include +#include + + +namespace util +{ + +inline tf2::TimePoint toTf2TimePoint(const builtin_interfaces::msg::Time& t) +{ + return tf2::TimePoint{ + std::chrono::seconds{t.sec} + std::chrono::nanoseconds{t.nanosec}}; +} +inline tf2::TimePoint toTf2TimePoint(const rclcpp::Time& t) +{ + return tf2::TimePoint{std::chrono::nanoseconds{t.nanoseconds()}}; +} +inline tf2::TimePoint toTf2TimePoint(double t_secs) +{ + return tf2::timeFromSec(t_secs); +} + +inline double toFloatSeconds(const builtin_interfaces::msg::Time& t) +{ + return static_cast(t.sec) + static_cast(t.nanosec) * 1e-9; +} +inline double toFloatSeconds(const rclcpp::Time& t) +{ + return static_cast(t.nanoseconds()) * 1e-9; +} +inline double toFloatSeconds(const tf2::TimePoint& t) +{ + return tf2::timeToSec(t); +} +template +inline double toFloatSeconds(const std::chrono::duration& dur) +{ + return std::chrono::duration_cast>(dur) + .count(); +} + +template +inline builtin_interfaces::msg::Time toTimeMsg( + const std::chrono::time_point& t) +{ + auto _t = std::chrono::duration_cast( + t.time_since_epoch()) + .count(); + return builtin_interfaces::msg::Time{} + .set__sec(_t / 1000000000) + .set__nanosec(_t % 1000000000); +} +inline builtin_interfaces::msg::Time toTimeMsg(const rclcpp::Time& t) +{ + return static_cast(t); +} +inline builtin_interfaces::msg::Time toTimeMsg(double t_secs) +{ + return builtin_interfaces::msg::Time{} + .set__sec(static_cast(t_secs)) + .set__nanosec( + static_cast( + std::fmod(t_secs, 1.) * 1e9)); +} + +}; // namespace util diff --git a/include/tsq.hpp b/include/util/time_search.hpp similarity index 93% rename from include/tsq.hpp rename to include/util/time_search.hpp index 88f1ad5..a1e653e 100644 --- a/include/tsq.hpp +++ b/include/util/time_search.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -48,6 +48,21 @@ namespace util { + +// template +// struct TSBase +// { +// static_assert(std::is_numeric::value); + +// using StampT = Stamp_T; + +// template +// using StampedElem = std::pair; +// template>> +// using TSDeque = std::deque, AllocT>; +// }; + + namespace tsq // TimeStamp "Q" (queue) { diff --git a/launch/perception.launch.py b/launch/perception.launch.py index e154755..d403ba1 100644 --- a/launch/perception.launch.py +++ b/launch/perception.launch.py @@ -15,12 +15,13 @@ except Exception as e: HAVE_LAUNCH_UTILS = False -from perception_launch_utils import get_perception_actions - - PKG_PATH = get_package_share_directory('cardinal_perception') DEFAULT_JSON_PATH = os.path.join(PKG_PATH, 'config', 'perception.json') +sys.path.append(os.path.join(PKG_PATH, 'launch')) +from perception_launch_utils import get_perception_actions + + def launch(context, *args, **kwargs): actions = [] diff --git a/launch/perception_launch_utils.py b/launch/perception_launch_utils.py index 50e723c..3a713ac 100644 --- a/launch/perception_launch_utils.py +++ b/launch/perception_launch_utils.py @@ -65,7 +65,7 @@ def get_perception_actions(config): percept_config = config['perception'] preproc_perception_config(percept_config) node_action = NodeAction(percept_config) - node_action.remappings['/trace_notifications'] = '/cardinal_perception/trace_notifications' + # node_action.remappings['/trace_notifications'] = '/cardinal_perception/trace_notifications' actions.append( node_action.format_node( package = 'cardinal_perception', @@ -79,7 +79,7 @@ def get_perception_actions(config): package = 'csm_metrics', executable = 'profiling_manager.py', output = 'screen', - parameters = [{'notification_topic': '/cardinal_perception/trace_notifications'}] + # parameters = [{'notification_topic': '/cardinal_perception/trace_notifications'}] ) ) if 'tag_detection' in config: tag_det_config = config['tag_detection'] diff --git a/msg/MiningEvalResults.msg b/msg/MiningEvalResults.msg index f915b97..5cb7136 100644 --- a/msg/MiningEvalResults.msg +++ b/msg/MiningEvalResults.msg @@ -1,2 +1,2 @@ float32[] ranges -int32 query_id \ No newline at end of file +int32 query_id diff --git a/package.xml b/package.xml index ea8decf..f6da6a0 100644 --- a/package.xml +++ b/package.xml @@ -11,6 +11,7 @@ rclcpp std_msgs + std_srvs sensor_msgs geometry_msgs nav_msgs diff --git a/src/core/modules/kfc_map.cpp b/src/core/modules/kfc_map.cpp index f34b461..9170c51 100644 --- a/src/core/modules/kfc_map.cpp +++ b/src/core/modules/kfc_map.cpp @@ -14,23 +14,18 @@ using MapOctreeType = MapOctree; KFC_MAP_INSTANTIATE_CLASS_TEMPLATE( MappingPointType, - MapOctreeType, - CollisionPointType) - -// KFC_MAP_INSTANTIATE_PCL_DEPENDENCIES(...) // <-- use if template types are non-pcl (ie. core classes need to be compiled) + MapOctreeType) #if PERCEPTION_USE_NULL_RAY_DELETION KFC_MAP_INSTANTIATE_UPDATE_FUNC_TEMPLATE( MappingPointType, - MapOctree, - CollisionPointType, + MapOctreeType, KF_COLLISION_DEFAULT_PARAMS, RayDirectionType) #else KFC_MAP_INSTANTIATE_UPDATE_FUNC_TEMPLATE( MappingPointType, MapOctreeType, - CollisionPointType, KF_COLLISION_DEFAULT_PARAMS, pcl::Axis) #endif diff --git a/src/core/modules/map_octree.cpp b/src/core/modules/map_octree.cpp index 846f263..15860f0 100644 --- a/src/core/modules/map_octree.cpp +++ b/src/core/modules/map_octree.cpp @@ -12,6 +12,7 @@ using namespace csm::perception; MAP_OCTREE_INSTANTIATE_CLASS_TEMPLATE(MappingPointType, MAP_OCTREE_STORE_NORMALS) +MAP_OCTREE_INSTANTIATE_PCL_DEPENDENCIES(MappingPointType) -// MAP_OCTREE_INSTANTIATE_PCL_DEPENDENCIES(...) // <-- use if template types are non-pcl (ie. core classes need to be compiled) - +MAP_OCTREE_INSTANTIATE_CLASS_TEMPLATE(TraversibilityPointType) +MAP_OCTREE_INSTANTIATE_PCL_DEPENDENCIES(TraversibilityPointType) diff --git a/src/core/modules/path_planner.cpp b/src/core/modules/path_planner.cpp index 0e99d1c..29ef6e9 100644 --- a/src/core/modules/path_planner.cpp +++ b/src/core/modules/path_planner.cpp @@ -11,8 +11,6 @@ using namespace csm::perception; -PATH_PLANNER_INSTANTIATE_CLASS_TEMPLATE( - TraversibilityPointType, - TraversibilityMetaType) +PATH_PLANNER_INSTANTIATE_CLASS_TEMPLATE(TraversibilityPointType) // PATH_PLANNER_INSTANTIATE_PCL_DEPENDENCIES(...) // <-- use if template types are non-pcl (ie. core classes need to be compiled) diff --git a/src/core/modules/traversibility_gen.cpp b/src/core/modules/traversibility_gen.cpp index e5ad739..498b605 100644 --- a/src/core/modules/traversibility_gen.cpp +++ b/src/core/modules/traversibility_gen.cpp @@ -12,9 +12,9 @@ using namespace csm::perception; TRAVERSIBILITY_GEN_INSTANTIATE_CLASS_TEMPLATE( - TraversibilityPointType, - TraversibilityMetaType) + MappingPointType, + TraversibilityPointType) TRAVERSIBILITY_GEN_INSTANTIATE_PCL_DEPENDENCIES( - TraversibilityPointType, - TraversibilityMetaType) + MappingPointType, + TraversibilityPointType) diff --git a/src/core/perception.hpp b/src/core/perception.hpp index 89d0c13..8bf31e5 100644 --- a/src/core/perception.hpp +++ b/src/core/perception.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -41,49 +41,31 @@ #include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - #include #include #include -#include #include #include -#include +#include #include #include -#include #include #include -#include -#include -#include -#include -#include -#include +#include +#include +#include -#include -#include -#include -#include -#include -#include -#include +#include "threads/imu_worker.hpp" +#include "threads/mapping_worker.hpp" +#include "threads/mining_eval_worker.hpp" +#include "threads/localization_worker.hpp" +#include "threads/path_planning_worker.hpp" +#include "threads/traversibility_worker.hpp" #include "perception_presets.hpp" @@ -93,108 +75,24 @@ namespace csm namespace perception { -template -using SparseMap = KFCMap< - PointT, - MapOctree, - CollisionPointT>; +using namespace util::ros_aliases; +struct PerceptionConfig; + class PerceptionNode : public rclcpp::Node { protected: - using Float64Msg = std_msgs::msg::Float64; using ImuMsg = sensor_msgs::msg::Imu; using PointCloudMsg = sensor_msgs::msg::PointCloud2; - using PoseStampedMsg = geometry_msgs::msg::PoseStamped; - using TagsTransformMsg = cardinal_perception::msg::TagsTransform; - using TrajectoryFilterDebugMsg = - cardinal_perception::msg::TrajectoryFilterDebug; + using SetBoolSrv = std_srvs::srv::SetBool; using UpdatePathPlanSrv = cardinal_perception::srv::UpdatePathPlanningMode; using UpdateMiningEvalSrv = cardinal_perception::srv::UpdateMiningEvalMode; using ProcessStatsCtx = csm::metrics::ProcessStats; -public: - using OdomPointType = csm::perception::OdomPointType; - using MappingPointType = csm::perception::MappingPointType; - using FiducialPointType = csm::perception::FiducialPointType; - using CollisionPointType = csm::perception::CollisionPointType; - using RayDirectionType = csm::perception::RayDirectionType; - using SphericalDirectionPointType = - csm::perception::SphericalDirectionPointType; - using TimestampPointType = csm::perception::TimestampPointType; - using TraversibilityPointType = csm::perception::TraversibilityPointType; - using TraversibilityMetaType = csm::perception::TraversibilityMetaType; - - using OdomPointCloudType = pcl::PointCloud; - using MappingPointCloudType = pcl::PointCloud; - using TraversibilityPointCloudType = - pcl::PointCloud; - using TraversibilityMetaCloudType = pcl::PointCloud; - - using ClockType = std::chrono::system_clock; - -protected: -#if TAG_DETECTION_ENABLED - struct TagDetection - { - using Ptr = std::shared_ptr; - using ConstPtr = std::shared_ptr; - - util::geom::Pose3d pose; - - double time_point, pix_area, avg_range, rms; - size_t num_tags; - - inline operator util::geom::Pose3d&() { return this->pose; } - }; -#endif - -#if LFD_ENABLED - struct FiducialResources - { - util::geom::PoseTf3f lidar_to_base; - PointCloudMsg::ConstSharedPtr raw_scan; - std::shared_ptr nan_indices, remove_indices; - uint32_t iteration_count; - }; -#endif -#if MAPPING_ENABLED - struct MappingResources - { - util::geom::PoseTf3f lidar_to_base, base_to_odom; - PointCloudMsg::ConstSharedPtr raw_scan; - OdomPointCloudType lo_buff; - #if PERCEPTION_USE_NULL_RAY_DELETION - std::vector null_vecs; - #endif - std::shared_ptr nan_indices, remove_indices; - }; -#endif -#if TRAVERSIBILITY_ENABLED - struct TraversibilityResources - { - double stamp; - Eigen::Vector3f search_min, search_max; - util::geom::PoseTf3f lidar_to_base, base_to_odom; - MappingPointCloudType points; - }; -#endif -#if PATH_PLANNING_ENABLED - struct PathPlanningResources - { - double stamp; - Eigen::Vector3f local_bound_min, local_bound_max; - util::geom::PoseTf3f base_to_odom; - PoseStampedMsg target; - TraversibilityPointCloudType trav_points; - TraversibilityMetaCloudType trav_meta; - }; -#endif - public: PerceptionNode(); ~PerceptionNode(); @@ -203,115 +101,35 @@ class PerceptionNode : public rclcpp::Node void shutdown(); protected: - void getParams(void* = nullptr); - void initPubSubs(void* = nullptr); - void printStartup(void* = nullptr); - - void imu_worker(const ImuMsg::SharedPtr& imu); - IF_TAG_DETECTION_ENABLED( - void detection_worker(const TagsTransformMsg::ConstSharedPtr& det);) - void odometry_worker(); - IF_LFD_ENABLED(void fiducial_worker();) - IF_MAPPING_ENABLED(void mapping_worker();) - IF_TRAVERSIBILITY_ENABLED(void traversibility_worker();) - IF_PATH_PLANNING_ENABLED(void path_planning_worker();) - -private: - void scan_callback_internal(const PointCloudMsg::ConstSharedPtr& scan); - IF_LFD_ENABLED(void fiducial_callback_internal(FiducialResources& buff);) - IF_MAPPING_ENABLED(void mapping_callback_internal(MappingResources& buff);) - IF_TRAVERSIBILITY_ENABLED( - void traversibility_callback_internal(TraversibilityResources& buff);) - IF_PATH_PLANNING_ENABLED( - void path_planning_callback_internal(PathPlanningResources& buffer);) + void getParams(PerceptionConfig& cfg); + void initPubSubs(PerceptionConfig& cfg); + void printStartup(PerceptionConfig& cfg); private: // --- TRANSFORM UTILITEIS ------------------------------------------------- tf2_ros::Buffer tf_buffer; tf2_ros::TransformListener tf_listener; - tf2_ros::TransformBroadcaster tf_broadcaster; // --- CORE COMPONENTS ----------------------------------------------------- - ImuIntegrator<> imu_samples; - ScanPreprocessor< - OdomPointType, - RayDirectionType, - SphericalDirectionPointType, - TimestampPointType> - scan_preproc; - LidarOdometry lidar_odom; - IF_LFD_ENABLED(LidarFiducialDetector fiducial_detector;) - IF_MAPPING_ENABLED( - SparseMap sparse_map;) -#if TAG_DETECTION_ENABLED - TransformSynchronizer transform_sync; -#else - TransformSynchronizer transform_sync; -#endif - IF_TRAVERSIBILITY_ENABLED( - TraversibilityGenerator - trav_gen;) - IF_PATH_PLANNING_ENABLED( - PathPlanner - path_planner;) + ImuWorker imu_worker; + LocalizationWorker localization_worker; + MappingWorker mapping_worker; + TraversibilityWorker traversibility_worker; + PathPlanningWorker path_planning_worker; + MiningEvalWorker mining_eval_worker; // --- SUBSCRIPTIONS/SERVICES/PUBLISHERS ----------------------------------- - rclcpp::Subscription::SharedPtr imu_sub; - rclcpp::Subscription::SharedPtr scan_sub; - IF_TAG_DETECTION_ENABLED( - rclcpp::Subscription::SharedPtr detections_sub;) + SharedSub imu_sub; + SharedSub scan_sub; + IF_TAG_DETECTION_ENABLED(SharedSub detections_sub;) - rclcpp::Service::SharedPtr path_plan_service; + SharedSrv alignment_state_service; + SharedSrv path_plan_service; + SharedSrv mining_eval_service; - rclcpp::TimerBase::SharedPtr proc_stats_timer; + RclTimer proc_stats_timer; util::GenericPubMap generic_pub; - util::PubMap metrics_pub; - util::PubMap scan_pub; - util::PubMap pose_pub; - - // --- FRAME IDS ----------------------------------------------------------- - std::string map_frame; - std::string odom_frame; - std::string base_frame; - - // --- STATE VARS ---------------------------------------------------------- - struct - { - // std::atomic has_rebiased{ false }; - std::atomic pplan_enabled{false}; - std::atomic threads_running{true}; - } // - state; - - // --- PARAMETERIZED CONFIGS ----------------------------------------------- - struct - { - IF_TAG_DETECTION_ENABLED(int tag_usage_mode;) - - double map_crop_horizontal_range; - double map_crop_vertical_range; - double map_export_horizontal_range; - double map_export_vertical_range; - } // - param; - - // --- MULTITHREADING RESOURCES -------------------------------------------- - struct - { - ResourcePipeline odometry_resources; - IF_LFD_ENABLED(ResourcePipeline fiducial_resources;) - IF_MAPPING_ENABLED( - ResourcePipeline mapping_resources;) - IF_TRAVERSIBILITY_ENABLED( - ResourcePipeline traversibility_resources;) - IF_PATH_PLANNING_ENABLED( - ResourcePipeline pplan_target_notifier; - ResourcePipeline path_planning_resources;) - - std::vector threads; - } // - mt; // --- METRICS ------------------------------------------------------------- ProcessStatsCtx process_stats; diff --git a/src/core/perception_core.cpp b/src/core/perception_core.cpp index 6840a9b..1f86961 100644 --- a/src/core/perception_core.cpp +++ b/src/core/perception_core.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -45,6 +45,9 @@ #include +#include +#include + using namespace util::geom::cvt::ops; @@ -84,40 +87,20 @@ static constexpr char const* STARTUP_SPLASH = " .;;;;;;;;:;; +$$$$$$$$$ \n" " .;;;;;;. X$$$$$$$: \n" "\n" - " Copyright (C) 2024-2025 Cardinal Space Mining Club\n"; + " Copyright (C) 2024-2026 Cardinal Space Mining Club\n"; struct PerceptionConfig { public: explicit PerceptionConfig(PerceptionNode& node) : node{node} {} - inline static PerceptionConfig& castOrAllocate( - void* buff, - PerceptionNode& node) - { - PerceptionConfig* config_ptr; - if (!buff) - { - config_ptr = new PerceptionConfig(node); - } - else - { - config_ptr = static_cast(buff); - } - return *config_ptr; - } - inline static void handleDeallocate(void* buff, PerceptionConfig& config) - { - if (!buff) - { - delete &config; - } - } - public: PerceptionNode& node; // core + std::string map_frame; + std::string odom_frame; + std::string base_frame; std::string scan_topic; std::string imu_topic; float metrics_pub_freq; @@ -155,6 +138,11 @@ struct PerceptionConfig double kfc_add_max_range; double kfc_voxel_size; + float map_crop_horizontal_range; + float map_crop_vertical_range; + float map_export_horizontal_range; + float map_export_vertical_range; + // traversibility float trav_norm_estimation_radius; float trav_output_res; @@ -164,14 +152,21 @@ struct PerceptionConfig float trav_avoid_radius; float trav_score_curvature_weight; float trav_score_grad_weight; + int trav_min_vox_cell_points; int trav_interp_point_samples; // path planning float pplan_boundary_radius; float pplan_goal_thresh; float pplan_search_radius; - float pplan_lambda_dist; - float pplan_lambda_penalty; + float pplan_dist_coeff; + float pplan_dir_coeff; + float pplan_trav_coeff; + float pplan_verification_range; + float pplan_map_obstacle_merge_window; + float pplan_map_passive_crop_horizontal_range; + float pplan_map_passive_crop_vertical_range; + int pplan_verification_degree; int pplan_max_neighbors; public: @@ -180,7 +175,7 @@ struct PerceptionConfig #if PERCEPTION_USE_TAG_DETECTION_PIPELINE return "AprilTag"; #elif PERCEPTION_USE_LFD_PIPELINE - return "Lidar fiducial"; + return "Lidar Fiducial"; #else return "Disabled"; #endif @@ -189,49 +184,205 @@ struct PerceptionConfig { return val ? "Enabled" : "Disabled"; } + + friend std::ostream& operator<<( + std::ostream& os, + const PerceptionConfig& config); }; +std::ostream& operator<<(std::ostream& os, const PerceptionConfig& config) +{ + // alignment, feat. ChatGPT lol + constexpr int CONFIG_ALIGN_WIDTH = 25; + auto align = [](const char* label) + { + std::ostringstream oss; + oss << " | " << std::left << std::setw(CONFIG_ALIGN_WIDTH) << label + << ": "; + return oss.str(); + }; + + + + os << std::setprecision(3); + os << "\n" + " +-- CONFIGURATION ---------------------------------+\n" + " |\n" + " +- FEATURES\n" + << align("Fiducial Mode") << PerceptionConfig::getFiducialModeStr() + << "\n" + << align("Scan Deskew") + << PerceptionConfig::getEnableDisableStr(PERCEPTION_USE_SCAN_DESKEW) + << "\n" + << align("Null Ray Mapping") + << PerceptionConfig::getEnableDisableStr( + PERCEPTION_USE_NULL_RAY_DELETION) + << "\n" + " |\n" + " +- CORE\n" + << align("Scan Topic") << config.scan_topic << "\n" + << align("IMU Topic") << config.imu_topic << "\n" + << align("Map Frame ID") << config.map_frame << "\n" + << align("Odom Frame ID") << config.odom_frame << "\n" + << align("Robot Frame ID") << config.base_frame << "\n" + << align("Statistics Frequency") << config.metrics_pub_freq + << " hz\n" + " |\n" + " +- EXCLUSION ZONES\n"; + if (config.num_excl_zones) + { + for (const auto& zone : config.excl_zones) + { + os << " | +- Frame \"" << std::get<0>(zone) << "\"\n" + << align("| Min") << "[" << std::get<1>(zone)[0] << ", " + << std::get<1>(zone)[1] << ", " << std::get<1>(zone)[2] + << "] (m)\n" + << align("| Max") << "[" << std::get<2>(zone)[0] << ", " + << std::get<2>(zone)[1] << ", " << std::get<2>(zone)[2] + << "] (m)\n"; + } + } + else + { + os << " | (none)\n"; + } + os << " |\n" + << " +- TRAJECTORY FILTER\n" + << align("Sample Window") << config.trjf_sample_window_s << " seconds\n" + << align("Filter Window") << config.trjf_filter_window_s << " seconds\n" + << align("Linear Error Thresh") << config.trjf_avg_linear_err_thresh + << " meters\n" + << align("Angular Error Thresh") << config.trjf_avg_angular_err_thresh + << " radians\n" + << align("Linear Dev Thresh") << config.trjf_max_linear_dev_thresh + << "\n" + << align("Angular Dev Thresh") << config.trjf_max_angular_dev_thresh + << "\n" + << " |\n" + " +- ODOMETRY\n" + " | (not implemented)\n"; + +#if LFD_ENABLED + os << " |\n" + " +- LIDAR FIDUCIAL DETECTOR\n" + << align("Detection Range") << config.lfd_detection_radius << " meters\n" + << align("Plane Seg Thickness") << config.lfd_plane_seg_thickness + << " meters\n" + << align("Ground Seg Thickness") << config.lfd_ground_seg_thickness + << " meters\n" + << align("Up Vec Max Angular Dev") << config.lfd_up_vec_max_angular_dev + << " radians\n" + << align("Planes Max Angular Dev") << config.lfd_planes_max_angular_dev + << " radians\n" + << align("Voxel Resolution") << config.lfd_vox_res << " meters\n" + << align("Max Percentage Leftover") + << (config.lfd_max_proportion_leftover * 100) << "%\n" + << align("Min Num Input Points") << config.lfd_min_num_input_points + << "\n" + << align("Min Num Seg Points") << config.lfd_min_plane_seg_points + << "\n"; +#endif + + // #if MAPPING_ENABLED + os << " |\n" + " +- MAPPING\n" + << align("Horizontal Crop Range") << config.map_crop_horizontal_range + << " meters\n" + << align("Vertical Crop Range") << config.map_crop_vertical_range + << " meters\n" + << align("Frustum Radius") << config.kfc_frustum_search_radius + << " radians\n" + << align("Radial Dist Thresh") << config.kfc_radial_dist_thresh + << " meters\n" + << align("Surface Width") << config.kfc_surface_width << " meters\n" + << align("Delete Max Range") << config.kfc_delete_max_range + << " meters\n" + << align("Add Max Range") << config.kfc_add_max_range << " meters\n" + << align("Voxel Size") << config.kfc_voxel_size << " meters\n"; + // #endif + + // #if TRAVERSIBILITY_ENABLED + os << " |\n" + " +- TRAVERSIBILITY\n" + << align("Horizontal Export Range") << config.map_export_horizontal_range + << " meters\n" + << align("Vertical Export Range") << config.map_export_vertical_range + << " meters\n" + << align("Normal Est Radius") << config.trav_norm_estimation_radius + << " meters\n" + << align("Output Grid Res") << config.trav_output_res << " meters\n" + << align("Grad Search Radius") << config.trav_grad_search_radius + << " meters\n" + << align("Min Grad Diff") << config.trav_min_grad_diff << " meters\n" + << align("Avoid Grad Angle") << config.trav_avoid_grad_angle + << " degrees\n" + << align("Avoid Radius") << config.trav_avoid_radius << " meters\n" + << align("Curvature Trav Weight") << config.trav_score_curvature_weight + << "\n" + << align("Gradient Trav Weight") << config.trav_score_grad_weight << "\n" + << align("Vox Cell Points Thresh") << config.trav_min_vox_cell_points + << "\n" + << align("Point Samples") << config.trav_interp_point_samples << "\n"; + // #endif + + // #if PATH_PLANNING_ENABLED + os << " |\n" + " +- PATH PLANNING\n" + << align("Boundary Radius") << config.pplan_boundary_radius + << " meters\n" + << align("Goal Threshold") << config.pplan_goal_thresh << " meters\n" + << align("Search Radius") << config.pplan_search_radius << " meters\n" + << align("Distance Coeff") << config.pplan_dist_coeff << "\n" + << align("Straightness Coeff") << config.pplan_dir_coeff << "\n" + << align("Traversibility Coeff") << config.pplan_trav_coeff << "\n" + << align("Verification Range") << config.pplan_verification_range + << " meters\n" + << align("Verification Degree") << config.pplan_verification_degree + << " points\n" + << align("Max Num Neighbors") << config.pplan_max_neighbors + << " points\n" + << align("Map Merge Window") << config.pplan_map_obstacle_merge_window + << " meters\n" + << align("Map Hrz. Crop Range") + << config.pplan_map_passive_crop_horizontal_range << " meters\n" + << align("Map Vrt. Crop Range") + << config.pplan_map_passive_crop_vertical_range << " meters\n"; + // #endif + + os << " +\n"; + return os; +} PerceptionNode::PerceptionNode() : Node("cardinal_perception"), tf_buffer{std::make_shared(RCL_ROS_TIME)}, - tf_listener{tf_buffer}, - tf_broadcaster{*this}, - lidar_odom{*this}, - transform_sync{this->tf_broadcaster}, - generic_pub{this, PERCEPTION_TOPIC(""), PERCEPTION_PUBSUB_QOS}, - metrics_pub{this, PERCEPTION_TOPIC("metrics/"), PERCEPTION_PUBSUB_QOS}, - scan_pub{this, PERCEPTION_TOPIC(""), PERCEPTION_PUBSUB_QOS}, - pose_pub{this, PERCEPTION_TOPIC("poses/"), PERCEPTION_PUBSUB_QOS} + tf_listener{tf_buffer, this}, + imu_worker{*this, tf_buffer}, + localization_worker{*this, tf_buffer, imu_worker.getSampler()}, + mapping_worker{*this}, + traversibility_worker{*this, imu_worker.getSampler()}, + path_planning_worker{*this, tf_buffer}, + mining_eval_worker{*this, tf_buffer}, + generic_pub{*this, PERCEPTION_TOPIC(""), PERCEPTION_PUBSUB_QOS} { PerceptionConfig config{*this}; - this->getParams(&config); - this->initPubSubs(&config); - this->transform_sync.setFrameIds( - this->map_frame, - this->odom_frame, - this->base_frame); - - this->printStartup(&config); - -#define PERCEPTION_THREADS \ - ((PERCEPTION_USE_LFD_PIPELINE > 0) + (PERCEPTION_ENABLE_MAPPING > 0) + \ - (PERCEPTION_ENABLE_TRAVERSIBILITY > 0) + \ - (PERCEPTION_ENABLE_PATH_PLANNING)) - - this->mt.threads.reserve(PERCEPTION_THREADS); - this->mt.threads.emplace_back(&PerceptionNode::odometry_worker, this); - IF_LFD_ENABLED( - this->mt.threads.emplace_back(&PerceptionNode::fiducial_worker, this);) - IF_MAPPING_ENABLED( - this->mt.threads.emplace_back(&PerceptionNode::mapping_worker, this);) - IF_TRAVERSIBILITY_ENABLED(this->mt.threads.emplace_back( - &PerceptionNode::traversibility_worker, - this);) - IF_PATH_PLANNING_ENABLED(this->mt.threads.emplace_back( - &PerceptionNode::path_planning_worker, - this);) + this->getParams(config); + this->initPubSubs(config); + this->printStartup(config); + + this->localization_worker.connectOutput(this->mapping_worker.getInput()); + this->mapping_worker.connectOutput(this->traversibility_worker.getInput()); + this->traversibility_worker.connectOutput( + this->path_planning_worker.getInput()); + this->traversibility_worker.connectOutput( + this->mining_eval_worker.getInput()); + + this->localization_worker.startThreads(); + this->mapping_worker.startThreads(); + this->traversibility_worker.startThreads(); + this->path_planning_worker.startThreads(); + this->mining_eval_worker.startThreads(); } PerceptionNode::~PerceptionNode() { this->shutdown(); } @@ -239,41 +390,20 @@ PerceptionNode::~PerceptionNode() { this->shutdown(); } void PerceptionNode::shutdown() { - this->state.threads_running = false; - - this->mt.odometry_resources.notifyExit(); - IF_LFD_ENABLED(this->mt.fiducial_resources.notifyExit();) - IF_MAPPING_ENABLED(this->mt.mapping_resources.notifyExit();) - IF_TRAVERSIBILITY_ENABLED(this->mt.traversibility_resources.notifyExit();) - IF_PATH_PLANNING_ENABLED(this->mt.pplan_target_notifier.notifyExit(); - this->mt.path_planning_resources.notifyExit();) - - for (auto& x : this->mt.threads) - { - if (x.joinable()) - { - x.join(); - } - } + this->localization_worker.stopThreads(); + this->mapping_worker.stopThreads(); + this->traversibility_worker.stopThreads(); + this->path_planning_worker.stopThreads(); + this->mining_eval_worker.stopThreads(); } -void PerceptionNode::getParams(void* buff) +void PerceptionNode::getParams(PerceptionConfig& config) { - PerceptionConfig& config = PerceptionConfig::castOrAllocate(buff, *this); - - util::declare_param(this, "map_frame_id", this->map_frame, "map"); - util::declare_param(this, "odom_frame_id", this->odom_frame, "odom"); - util::declare_param(this, "base_frame_id", this->base_frame, "base_link"); - - // --- TAG DETECTION PARAMS ------------------------------------------------ - IF_TAG_DETECTION_ENABLED( - util::declare_param( - this, - "tag_usage_mode", - this->param.tag_usage_mode, - -1);) + util::declare_param(this, "map_frame_id", config.map_frame, "map"); + util::declare_param(this, "odom_frame_id", config.odom_frame, "odom"); + util::declare_param(this, "base_frame_id", config.base_frame, "base_link"); util::declare_param(this, "metrics_pub_freq", config.metrics_pub_freq, 10.); // --- CROP BOUNDS --------------------------------------------------------- @@ -310,7 +440,7 @@ void PerceptionNode::getParams(void* buff) if (min_config.size() >= 3 && max_config.size() >= 3) { - this->scan_preproc.addExclusionZone( + this->localization_worker.scan_preproc.addExclusionZone( frame_config, Eigen::AlignedBox3f( Eigen::Vector3f( @@ -359,7 +489,8 @@ void PerceptionNode::getParams(void* buff) "trajectory_filter.thresh.max_angular_deviation", config.trjf_max_angular_dev_thresh, 4e-2); - this->transform_sync.trajectoryFilter().applyParams( + + this->localization_worker.transform_sync.trajectoryFilter().applyParams( config.trjf_sample_window_s, config.trjf_filter_window_s, config.trjf_avg_linear_err_thresh, @@ -414,9 +545,10 @@ void PerceptionNode::getParams(void* buff) "fiducial_detection.min_plane_seg_points", config.lfd_min_plane_seg_points, 15); - this->fiducial_detector.configDetector( + + this->localization_worker.fiducial_detector.configDetector( LFD_ESTIMATE_GROUND_PLANE | LFD_PREFER_USE_GROUND_SAMPLE); - this->fiducial_detector.applyParams( + this->localization_worker.fiducial_detector.applyParams( config.lfd_detection_radius, config.lfd_plane_seg_thickness, config.lfd_ground_seg_thickness, @@ -429,16 +561,16 @@ void PerceptionNode::getParams(void* buff) #endif // --- MAPPING ------------------------------------------------------------- -#if MAPPING_ENABLED + // #if MAPPING_ENABLED util::declare_param( this, "mapping.crop_horizontal_range", - this->param.map_crop_horizontal_range, + config.map_crop_horizontal_range, 0.); util::declare_param( this, "mapping.crop_vertical_range", - this->param.map_crop_vertical_range, + config.map_crop_vertical_range, 0.); util::declare_param( this, @@ -470,26 +602,27 @@ void PerceptionNode::getParams(void* buff) "mapping.voxel_size", config.kfc_voxel_size, 0.05); - this->sparse_map.applyParams( + + this->mapping_worker.sparse_map.applyParams( config.kfc_frustum_search_radius, config.kfc_radial_dist_thresh, config.kfc_surface_width, config.kfc_delete_max_range, config.kfc_add_max_range, config.kfc_voxel_size); -#endif + // #endif // --- TRAVERSIBILITY ------------------------------------------------------ -#if TRAVERSIBILITY_ENABLED + // #if TRAVERSIBILITY_ENABLED util::declare_param( this, "traversibility.chunk_horizontal_range", - this->param.map_export_horizontal_range, + config.map_export_horizontal_range, 4.); util::declare_param( this, "traversibility.chunk_vertical_range", - this->param.map_export_vertical_range, + config.map_export_vertical_range, 1.); util::declare_param( this, @@ -531,11 +664,17 @@ void PerceptionNode::getParams(void* buff) "traversibility.trav_score_grad_weight", config.trav_score_grad_weight, 1.f); + util::declare_param( + this, + "traversibility.min_vox_cell_points", + config.trav_min_vox_cell_points, + 3); util::declare_param( this, "traversibility.interp_point_samples", config.trav_interp_point_samples, 7); + if (config.trav_norm_estimation_radius <= 0.f) { config.trav_norm_estimation_radius = config.kfc_voxel_size * 2; @@ -544,7 +683,8 @@ void PerceptionNode::getParams(void* buff) { config.trav_output_res = config.kfc_voxel_size; } - this->trav_gen.configure( + + this->traversibility_worker.trav_gen.configure( config.trav_norm_estimation_radius, config.trav_output_res, config.trav_grad_search_radius, @@ -553,11 +693,12 @@ void PerceptionNode::getParams(void* buff) config.trav_avoid_radius, config.trav_score_curvature_weight, config.trav_score_grad_weight, + config.trav_min_vox_cell_points, config.trav_interp_point_samples); -#endif + // #endif // --- PATH PLANNING ------------------------------------------------------- -#if PATH_PLANNING_ENABLED + // #if PATH_PLANNING_ENABLED util::declare_param( this, "pplan.boundary_radius", @@ -575,78 +716,129 @@ void PerceptionNode::getParams(void* buff) 1.f); util::declare_param( this, - "pplan.lambda_distance", - config.pplan_lambda_dist, + "pplan.distance_coeff", + config.pplan_dist_coeff, 1.f); util::declare_param( this, - "pplan.lambda_penalty", - config.pplan_lambda_penalty, + "pplan.straightness_coeff", + config.pplan_dir_coeff, 1.f); + util::declare_param( + this, + "pplan.traversibility_coeff", + config.pplan_trav_coeff, + 1.f); + util::declare_param( + this, + "pplan.verification_range", + config.pplan_verification_range, + 1.5f); + util::declare_param( + this, + "pplan.verification_degree", + config.pplan_verification_degree, + 2); util::declare_param( this, "pplan.max_neighbors", config.pplan_max_neighbors, 10); + util::declare_param( + this, + "pplan.map_obstacle_merge_window", + config.pplan_map_obstacle_merge_window, + 0.5f); + util::declare_param( + this, + "pplan.map_passive_crop_horizontal_range", + config.pplan_map_passive_crop_horizontal_range, + 10.f); + util::declare_param( + this, + "pplan.map_passive_crop_vertical_range", + config.pplan_map_passive_crop_vertical_range, + 5.f); - this->path_planner.setParameters( + this->path_planning_worker.path_planner.setParameters( config.pplan_boundary_radius, config.pplan_goal_thresh, config.pplan_search_radius, - config.pplan_lambda_dist, - config.pplan_lambda_penalty, + config.pplan_dist_coeff, + config.pplan_dir_coeff, + config.pplan_trav_coeff, + config.pplan_verification_range, + config.pplan_verification_degree, config.pplan_max_neighbors); -#endif - - PerceptionConfig::handleDeallocate(buff, config); + // #endif + + this->imu_worker.configure(config.base_frame); + this->localization_worker.configure( + config.map_frame, + config.odom_frame, + config.base_frame); + this->mapping_worker.configure( + config.odom_frame, + config.map_crop_horizontal_range, + config.map_crop_vertical_range, + config.map_export_horizontal_range, + config.map_export_vertical_range); + this->traversibility_worker.configure(config.odom_frame); + this->path_planning_worker.configure( + config.odom_frame, + config.pplan_map_obstacle_merge_window, + config.pplan_map_passive_crop_horizontal_range, + config.pplan_map_passive_crop_vertical_range); + this->mining_eval_worker.configure(config.odom_frame); } -void PerceptionNode::initPubSubs(void* buff) +void PerceptionNode::initPubSubs(PerceptionConfig& config) { - PerceptionConfig& config = PerceptionConfig::castOrAllocate(buff, *this); - util::declare_param(this, "scan_topic", config.scan_topic, "scan"); util::declare_param(this, "imu_topic", config.imu_topic, "imu"); this->imu_sub = this->create_subscription( config.imu_topic, PERCEPTION_PUBSUB_QOS, - [this](ImuMsg::SharedPtr imu) { this->imu_worker(imu); }); + [this](ImuMsg::SharedPtr msg) { this->imu_worker.accept(*msg); }); this->scan_sub = this->create_subscription( config.scan_topic, PERCEPTION_PUBSUB_QOS, - [this](const PointCloudMsg::ConstSharedPtr& scan) - { this->mt.odometry_resources.updateAndNotify(scan); }); + [this](const PointCloudMsg::ConstSharedPtr& msg) + { this->localization_worker.accept(msg); }); #if TAG_DETECTION_ENABLED this->detections_sub = this->create_subscription( PERCEPTION_TOPIC("tags_detections"), PERCEPTION_PUBSUB_QOS, - [this](const TagsTransformMsg::ConstSharedPtr& det) - { this->detection_worker(det); }); + [this](const TagsTransformMsg::ConstSharedPtr& msg) + { this->localization_worker.accept(msg); }); #endif -#if PATH_PLANNING_ENABLED + this->alignment_state_service = this->create_service( + PERCEPTION_TOPIC("set_global_alignment"), + [this]( + SetBoolSrv::Request::SharedPtr req, + SetBoolSrv::Response::SharedPtr resp) + { + resp->success = + this->localization_worker.setGlobalAlignmentEnabled(req->data); + }); + // #if PATH_PLANNING_ENABLED this->path_plan_service = this->create_service( PERCEPTION_TOPIC("update_path_planning"), [this]( UpdatePathPlanSrv::Request::SharedPtr req, UpdatePathPlanSrv::Response::SharedPtr resp) - { - if (req->completed) - { - this->state.pplan_enabled = false; - } - else - { - this->state.pplan_enabled = true; - this->mt.pplan_target_notifier.updateAndNotify(req->target); - } - - resp->running = this->state.pplan_enabled; - }); -#endif + { this->path_planning_worker.accept(req, resp); }); + // #endif + this->mining_eval_service = this->create_service( + PERCEPTION_TOPIC("query_mining_eval"), + [this]( + UpdateMiningEvalSrv::Request::SharedPtr req, + UpdateMiningEvalSrv::Response::SharedPtr resp) + { this->mining_eval_worker.accept(req, resp); }); this->proc_stats_timer = this->create_wall_timer( std::chrono::milliseconds( @@ -658,197 +850,19 @@ void PerceptionNode::initPubSubs(void* buff) "process_stats", this->process_stats.toMsg()); }); - - PerceptionConfig::handleDeallocate(buff, config); } -void PerceptionNode::printStartup(void* buff) +void PerceptionNode::printStartup(PerceptionConfig& config) { std::ostringstream msg; msg << "\n" << STARTUP_SPLASH; #if PERCEPTION_PRINT_STARTUP_CONFIGS - // alignment, feat. ChatGPT lol - constexpr int CONFIG_ALIGN_WIDTH = 25; - auto align = [](const char* label) - { - std::ostringstream oss; - oss << " | " << std::left << std::setw(CONFIG_ALIGN_WIDTH) << label - << ": "; - return oss.str(); - }; - // auto align_numbered = [](int i, const char* label) - // { - // std::ostringstream n_label, oss; - // n_label << i << ". " << label; - // oss << " | " << std::left << std::setw(CONFIG_ALIGN_WIDTH) - // << n_label.str() << ": "; - // return oss.str(); - // }; - - if (buff) - { - PerceptionConfig& config = *static_cast(buff); - - msg << std::setprecision(3); - msg << "\n" - " +-- CONFIGURATION ---------------------------------+\n" - " |\n" - " +- PIPELINE STAGES\n" - << align("Fiducial mode") << PerceptionConfig::getFiducialModeStr() - << "\n" - << align("Mapping") - << PerceptionConfig::getEnableDisableStr(PERCEPTION_ENABLE_MAPPING) - << "\n" - << align("Traversibility") - << PerceptionConfig::getEnableDisableStr( - PERCEPTION_ENABLE_TRAVERSIBILITY) - << "\n" - << align("Path Planning") - << PerceptionConfig::getEnableDisableStr( - PERCEPTION_ENABLE_PATH_PLANNING) - << "\n" - " |\n" - " +- FEATURES\n" - << align("Scan Deskew") - << PerceptionConfig::getEnableDisableStr(PERCEPTION_USE_SCAN_DESKEW) - << "\n" - << align("Null Ray Mapping") - << PerceptionConfig::getEnableDisableStr( - PERCEPTION_USE_NULL_RAY_DELETION) - << "\n" - " |\n" - " +- CORE\n" - << align("Scan Topic") << config.scan_topic << "\n" - << align("IMU Topic") << config.imu_topic << "\n" - << align("Map Frame ID") << this->map_frame << "\n" - << align("Odom Frame ID") << this->odom_frame << "\n" - << align("Robot Frame ID") << this->base_frame << "\n" - << align("Statistics Frequency") << config.metrics_pub_freq - << " hz\n" - " |\n" - " +- EXCLUSION ZONES\n"; - if (config.num_excl_zones) - { - for (const auto& zone : config.excl_zones) - { - msg << " | +- Frame \"" << std::get<0>(zone) << "\"\n" - << align("| Min") << "[" << std::get<1>(zone)[0] << ", " - << std::get<1>(zone)[1] << ", " << std::get<1>(zone)[2] - << "] (m)\n" - << align("| Max") << "[" << std::get<2>(zone)[0] << ", " - << std::get<2>(zone)[1] << ", " << std::get<2>(zone)[2] - << "] (m)\n"; - } - } - else - { - msg << " | (none)\n"; - } - msg << " |\n" - << " +- TRAJECTORY FILTER\n" - << align("Sample Window") << config.trjf_sample_window_s - << " seconds\n" - << align("Filter Window") << config.trjf_filter_window_s - << " seconds\n" - << align("Linear Error Thresh") << config.trjf_avg_linear_err_thresh - << " meters\n" - << align("Angular Error Thresh") - << config.trjf_avg_angular_err_thresh << " radians\n" - << align("Linear Dev Thresh") << config.trjf_max_linear_dev_thresh - << "\n" - << align("Angular Dev Thresh") << config.trjf_max_angular_dev_thresh - << "\n" - << " |\n" - " +- ODOMETRY\n" - " | (not implemented)\n"; - - #if LFD_ENABLED - msg << " |\n" - " +- LIDAR FIDUCIAL DETECTOR\n" - << align("Detection Range") << config.lfd_detection_radius - << " meters\n" - << align("Plane Seg Thickness") << config.lfd_plane_seg_thickness - << " meters\n" - << align("Ground Seg Thickness") << config.lfd_ground_seg_thickness - << " meters\n" - << align("Up Vec Max Angular Dev") - << config.lfd_up_vec_max_angular_dev << " radians\n" - << align("Planes Max Angular Dev") - << config.lfd_planes_max_angular_dev << " radians\n" - << align("Voxel Resolution") << config.lfd_vox_res << " meters\n" - << align("Max Percentage Leftover") - << (config.lfd_max_proportion_leftover * 100) << "%\n" - << align("Min Num Input Points") << config.lfd_min_num_input_points - << "\n" - << align("Min Num Seg Points") << config.lfd_min_plane_seg_points - << "\n"; - #endif - - #if MAPPING_ENABLED - msg << " |\n" - " +- MAPPING\n" - << align("Horizontal Crop Range") - << this->param.map_crop_horizontal_range << " meters\n" - << align("Vertical Crop Range") - << this->param.map_crop_vertical_range << " meters\n" - << align("Frustum Radius") << config.kfc_frustum_search_radius - << " radians\n" - << align("Radial Dist Thresh") << config.kfc_radial_dist_thresh - << " meters\n" - << align("Surface Width") << config.kfc_surface_width << " meters\n" - << align("Delete Max Range") << config.kfc_delete_max_range - << " meters\n" - << align("Add Max Range") << config.kfc_add_max_range << " meters\n" - << align("Voxel Size") << config.kfc_voxel_size << " meters\n"; - #endif - - #if TRAVERSIBILITY_ENABLED - msg << " |\n" - " +- TRAVERSIBILITY\n" - << align("Horizontal Export Range") - << this->param.map_export_horizontal_range << " meters\n" - << align("Vertical Export Range") - << this->param.map_export_vertical_range << " meters\n" - << align("Normal Est Radius") << config.trav_norm_estimation_radius - << " meters\n" - << align("Output Grid Res") << config.trav_output_res << " meters\n" - << align("Grad Search Radius") << config.trav_grad_search_radius - << " meters\n" - << align("Min Grad Diff") << config.trav_min_grad_diff - << " meters\n" - << align("Avoid Grad Angle") << config.trav_avoid_grad_angle - << " degrees\n" - << align("Avoid Radius") << config.trav_avoid_radius << " meters\n" - << align("Curvature Trav Weight") - << config.trav_score_curvature_weight << "\n" - << align("Gradient Trav Weight") << config.trav_score_grad_weight - << "\n" - << align("Point Samples") << config.trav_interp_point_samples - << "\n"; - #endif - - #if PATH_PLANNING_ENABLED - msg << " |\n" - " +- PATH PLANNING\n" - << align("Boundary Radius") << config.pplan_boundary_radius - << " meters\n" - << align("Goal Threshold") << config.pplan_goal_thresh - << " meters\n" - << align("Search Radius") << config.pplan_search_radius - << " meters\n" - << align("Lambda Distance") << config.pplan_lambda_dist - << " meters\n" - << align("Lambda Penalty") << config.pplan_lambda_penalty << "\n" - << align("Max Num Neighbors") << config.pplan_max_neighbors << "\n"; - #endif - - msg << " +\n"; - } + msg << config; #else - (void)buff; + (void)config; #endif RCLCPP_INFO( @@ -857,162 +871,5 @@ void PerceptionNode::printStartup(void* buff) msg.str().c_str()); } - - - - -// --- THREAD LOOPS ----------------------------------------------------------- - -void PerceptionNode::odometry_worker() -{ - RCLCPP_INFO( - this->get_logger(), - "[CORE]: Odometry thread started successfully."); - - do - { - auto& scan = this->mt.odometry_resources.waitNewestResource(); - if (!this->state.threads_running.load()) - { - return; - } - - PROFILING_SYNC(); - PROFILING_NOTIFY_ALWAYS(odometry); - this->scan_callback_internal(scan); - PROFILING_NOTIFY_ALWAYS(odometry); - } // - while (this->state.threads_running.load()); - - RCLCPP_INFO(this->get_logger(), "[CORE]: Odometry thread exited cleanly"); -} - - - -#if LFD_ENABLED -void PerceptionNode::fiducial_worker() -{ - RCLCPP_INFO( - this->get_logger(), - "[CORE]: Lidar fiducial detection thread started successfully."); - - do - { - auto& buff = this->mt.fiducial_resources.waitNewestResource(); - if (!this->state.threads_running.load()) - { - return; - } - - PROFILING_SYNC(); - PROFILING_NOTIFY_ALWAYS(lidar_fiducial); - this->fiducial_callback_internal(buff); - PROFILING_NOTIFY_ALWAYS(lidar_fiducial); - } // - while (this->state.threads_running.load()); - - RCLCPP_INFO( - this->get_logger(), - "[CORE]: Lidar fiducial detection thread exited cleanly."); -} -#endif - - - -#if MAPPING_ENABLED -void PerceptionNode::mapping_worker() -{ - RCLCPP_INFO( - this->get_logger(), - "[CORE]: Mapping thread started successfully."); - - do - { - auto& buff = this->mt.mapping_resources.waitNewestResource(); - if (!this->state.threads_running.load()) - { - return; - } - - PROFILING_SYNC(); - PROFILING_NOTIFY_ALWAYS(mapping); - this->mapping_callback_internal(buff); - PROFILING_NOTIFY_ALWAYS(mapping); - } // - while (this->state.threads_running.load()); - - RCLCPP_INFO(this->get_logger(), "[CORE]: Mapping thread exited cleanly."); -} -#endif - - - -#if TRAVERSIBILITY_ENABLED -void PerceptionNode::traversibility_worker() -{ - RCLCPP_INFO( - this->get_logger(), - "[CORE]: Traversibility thread started successfully."); - - do - { - auto& buff = this->mt.traversibility_resources.waitNewestResource(); - if (!this->state.threads_running.load()) - { - return; - } - - PROFILING_SYNC(); - PROFILING_NOTIFY_ALWAYS(traversibility); - this->traversibility_callback_internal(buff); - PROFILING_NOTIFY_ALWAYS(traversibility); - } // - while (this->state.threads_running.load()); - - RCLCPP_INFO( - this->get_logger(), - "[CORE]: Traversibility thread exited cleanly."); -} -#endif - - - -#if PATH_PLANNING_ENABLED -void PerceptionNode::path_planning_worker() -{ - RCLCPP_INFO( - this->get_logger(), - "[CORE]: Path planning thread started successfully."); - - do - { - if (this->state.pplan_enabled) - { - auto& buff = this->mt.path_planning_resources.waitNewestResource(); - if (!this->state.threads_running.load()) - { - return; - } - - buff.target = this->mt.pplan_target_notifier.aquireNewestOutput(); - - PROFILING_SYNC(); - PROFILING_NOTIFY_ALWAYS(path_planning); - this->path_planning_callback_internal(buff); - PROFILING_NOTIFY_ALWAYS(path_planning); - } - else - { - this->mt.pplan_target_notifier.waitNewestResource(); - } - } // - while (this->state.threads_running.load()); - - RCLCPP_INFO( - this->get_logger(), - "[CORE]: Path planning thread exited cleanly."); -} -#endif - }; // namespace perception }; // namespace csm diff --git a/src/core/perception_presets.hpp b/src/core/perception_presets.hpp index ec4b244..116e676 100644 --- a/src/core/perception_presets.hpp +++ b/src/core/perception_presets.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -79,18 +79,6 @@ #endif // --- PIPELINE STAGES ENABLE/DISABLE ----------------------------------------- -#ifndef PERCEPTION_ENABLE_MAPPING - #define PERCEPTION_ENABLE_MAPPING 1 -#endif - -#ifndef PERCEPTION_ENABLE_TRAVERSIBILITY - #define PERCEPTION_ENABLE_TRAVERSIBILITY (PERCEPTION_ENABLE_MAPPING) -#endif - -#ifndef PERCEPTION_ENABLE_PATH_PLANNING - #define PERCEPTION_ENABLE_PATH_PLANNING (PERCEPTION_ENABLE_TRAVERSIBILITY) -#endif - #ifndef PERCEPTION_USE_TAG_DETECTION_PIPELINE #define PERCEPTION_USE_TAG_DETECTION_PIPELINE 0 #endif @@ -125,39 +113,9 @@ static_assert( "Tag detection and lidar fiducial pipelines are mutually exclusive." "You may only enable one at a time."); #endif -#if ((PERCEPTION_ENABLE_TRAVERSIBILITY) && !(PERCEPTION_ENABLE_MAPPING)) - #undef PERCEPTION_ENABLE_TRAVERSIBILITY - #define PERCEPTION_ENABLE_TRAVERSIBILITY 0 -#endif -#if ((PERCEPTION_ENABLE_PATH_PLANNING) && !(PERCEPTION_ENABLE_TRAVERSIBILITY)) - #undef PERCEPTION_ENABLE_PATH_PLANNING - #define PERCEPTION_ENABLE_PATH_PLANNING 0 -#endif // --- HELPER MACROS ---------------------------------------------------------- -#if PERCEPTION_ENABLE_MAPPING > 0 - #define IF_MAPPING_ENABLED(...) __VA_ARGS__ - #define MAPPING_ENABLED 1 -#else - #define IF_MAPPING_ENABLED(...) - #define MAPPING_ENABLED 0 -#endif -#if PERCEPTION_ENABLE_TRAVERSIBILITY > 0 - #define IF_TRAVERSIBILITY_ENABLED(...) __VA_ARGS__ - #define TRAVERSIBILITY_ENABLED 1 -#else - #define IF_TRAVERSIBILITY_ENABLED(...) - #define TRAVERSIBILITY_ENABLED 0 -#endif -#if PERCEPTION_ENABLE_PATH_PLANNING > 0 - #define IF_PATH_PLANNING_ENABLED(...) __VA_ARGS__ - #define PATH_PLANNING_ENABLED 1 -#else - #define IF_PATH_PLANNING_ENABLED(...) - #define PATH_PLANNING_ENABLED 0 -#endif - #if PERCEPTION_USE_TAG_DETECTION_PIPELINE > 0 #define IF_TAG_DETECTION_ENABLED(...) __VA_ARGS__ #define TAG_DETECTION_ENABLED 1 diff --git a/src/core/perception_threads.cpp b/src/core/perception_threads.cpp deleted file mode 100644 index c49b2f1..0000000 --- a/src/core/perception_threads.cpp +++ /dev/null @@ -1,865 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * -* * -* ;xxxxxxx: * -* ;$$$$$$$$$ ...::.. * -* $$$$$$$$$$x .:::::::::::.. * -* x$$$$$$$$$$$$$$::::::::::::::::. * -* :$$$$$&X; .xX:::::::::::::.::... * -* .$$Xx++$$$$+ :::. :;: .::::::. .... : * -* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * -* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * -* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * -* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * -* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * -* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * -* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * -* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * -* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * -* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * -* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * -* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * -* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * -* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * -* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * -* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * -* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * -* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * -* :;;;;;;;;;;;;. :$$$$$$$$$$X * -* .;;;;;;;;:;; +$$$$$$$$$ * -* .;;;;;;. X$$$$$$$: * -* * -* 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 "perception.hpp" - -#include -#include -#include - -#include - -#include -#include -#include - -#include -#include - -#include - -#include -#include - -#include -#include - - -using namespace util::geom::cvt::ops; - -using Vec3f = Eigen::Vector3f; -using Vec3d = Eigen::Vector3d; -using Mat4f = Eigen::Matrix4f; -using Iso3f = Eigen::Isometry3f; -using Quatf = Eigen::Quaternionf; -using Quatd = Eigen::Quaterniond; - -using PathMsg = nav_msgs::msg::Path; -using TwistStampedMsg = geometry_msgs::msg::TwistStamped; - -using ReflectorHintMsg = cardinal_perception::msg::ReflectorHint; -using MiningEvalResultsMsg = cardinal_perception::msg::MiningEvalResults; - - -namespace csm -{ -namespace perception -{ - -// --- DIRECT ROS CALLBACK WORKERS -------------------------------------------- - -void PerceptionNode::imu_worker(const ImuMsg::SharedPtr& imu) -{ - PROFILING_SYNC(); - PROFILING_NOTIFY_ALWAYS(imu); - - try - { - auto tf = this->tf_buffer.lookupTransform( - this->base_frame, - imu->header.frame_id, - util::toTf2TimePoint(imu->header.stamp)); - - tf2::doTransform(*imu, *imu, tf); - - this->imu_samples.addSample(*imu); - } - catch (const std::exception& e) - { - RCLCPP_INFO( - this->get_logger(), - "[IMU CALLBACK]: Failed to process imu measurment.\n\twhat(): %s", - e.what()); - } - -#if PERCEPTION_PUBLISH_GRAV_ESTIMATION > 0 - double stddev, delta_r; - Vec3d grav_vec = this->imu_samples.estimateGravity(1., &stddev, &delta_r); - - PoseStampedMsg grav_pub; - grav_pub.header.stamp = imu->header.stamp; - grav_pub.header.frame_id = this->base_frame; - grav_pub.pose.orientation - << Quatd::FromTwoVectors(Vec3d{1., 0., 0.}, grav_vec); - this->pose_pub.publish("gravity_estimation", grav_pub); - - this->metrics_pub.publish("gravity_estimation/acc_stddev", stddev); - this->metrics_pub.publish("gravity_estimation/delta_rotation", delta_r); -#endif - - PROFILING_NOTIFY_ALWAYS(imu); -} - - - -#if TAG_DETECTION_ENABLED -void PerceptionNode::detection_worker( - const TagsTransformMsg::ConstSharedPtr& detection_group) -{ - PROFILING_SYNC(); - PROFILING_NOTIFY_ALWAYS(tags_detection); - - const geometry_msgs::msg::TransformStamped& tf = - detection_group->estimated_tf; - if (tf.header.frame_id == this->map_frame && - tf.child_frame_id == this->base_frame && - (this->param.tag_usage_mode > 0 || - (this->param.tag_usage_mode < 0 && - detection_group->filter_mask >= 31))) - { - TagDetection::Ptr td = std::make_shared(); - td->pose << tf.transform; - td->time_point = util::toFloatSeconds(tf.header.stamp); - td->pix_area = detection_group->pix_area; - td->avg_range = detection_group->avg_range; - td->rms = detection_group->rms; - td->num_tags = detection_group->num_tags; - this->transform_sync.endMeasurementIterationSuccess(td, td->time_point); - - // RCLCPP_INFO(this->get_logger(), "[DETECTION CB]: Recv - Base delta: %f", util::toFloatSeconds(this->get_clock()->now()) - td->time_point); - } - - PROFILING_NOTIFY_ALWAYS(tags_detection); -} -#endif - - - - - -// --- INTERNAL CALLBACK WORKERS ---------------------------------------------- - -void PerceptionNode::scan_callback_internal( - const PointCloudMsg::ConstSharedPtr& scan) -{ - PROFILING_NOTIFY(odometry_init); - - OdomPointCloudType lo_cloud; - util::geom::PoseTf3f lidar_to_base_tf, base_to_odom_tf; - const auto scan_stamp = scan->header.stamp; - - PROFILING_NOTIFY2(odometry_init, odometry_preprocess); - - // 1. Preprocess Scan ------------------------------------------------------ - constexpr int Preproc_Config = - (decltype(this->scan_preproc)::PREPROC_EXCLUDE_ZONES | - (decltype(this->scan_preproc)::PREPROC_PERFORM_DESKEW * - (PERCEPTION_USE_SCAN_DESKEW != 0)) | - (decltype(this->scan_preproc)::PREPROC_EXPORT_NULL_RAYS * - (PERCEPTION_USE_NULL_RAY_DELETION != 0))); - this->scan_preproc.process( - *scan, - this->base_frame, - this->tf_buffer, - this->imu_samples); - - this->scan_preproc.swapOutputPoints(lo_cloud); - lidar_to_base_tf.pose - << (lidar_to_base_tf.tf = this->scan_preproc.getTargetTf()); - - PROFILING_NOTIFY2(odometry_preprocess, odometry_lfd_export); - - const uint32_t iteration_token = - this->transform_sync.beginOdometryIteration(); - -#if LFD_ENABLED - // 2. Export LFD Resources (if enabled) ------------------------------------ - FiducialResources& f = this->mt.fiducial_resources.lockInput(); - f.lidar_to_base = lidar_to_base_tf; - f.raw_scan = scan; - if (!f.nan_indices || f.nan_indices.use_count() > 1) - { - f.nan_indices = std::make_shared(); - } - if (!f.remove_indices || f.remove_indices.use_count() > 1) - { - f.remove_indices = std::make_shared(); - } - this->scan_preproc.swapNullIndices( - const_cast(*f.nan_indices)); - this->scan_preproc.swapRemovalIndices( - const_cast(*f.remove_indices)); - const auto& nan_indices_ptr = f.nan_indices; - const auto& remove_indices_ptr = f.remove_indices; - f.iteration_count = iteration_token; - this->mt.fiducial_resources.unlockInputAndNotify(f); -#else - (void)iteration_token; -#endif - - PROFILING_NOTIFY2(odometry_lfd_export, odometry_lio); - - // apply sensor origin - lo_cloud.sensor_origin_ << lidar_to_base_tf.pose.vec, 1.f; - const double new_odom_stamp = util::toFloatSeconds(scan_stamp); - - // get imu estimated rotation - Quatd imu_rot_q = Quatd::Identity(); - Mat4f imu_rot = Mat4f::Identity(); - if (this->imu_samples.hasSamples()) - { - imu_rot_q = this->imu_samples.getDelta( - this->lidar_odom.prevStamp(), - new_odom_stamp); - imu_rot.block<3, 3>(0, 0) = - imu_rot_q.template cast().toRotationMatrix(); - } - - // 3. Iterate LIO ---------------------------------------------------------- - auto lo_status = this->lidar_odom.processScan( - lo_cloud, - new_odom_stamp, - base_to_odom_tf, - imu_rot); - - PROFILING_NOTIFY(odometry_lio); - - if (!lo_status) - { - this->transform_sync.endOdometryIterationFailure(); - return; - } - - PROFILING_NOTIFY(odometry_map_export); - -#if MAPPING_ENABLED - // 4. Export Mapping Resources (if enabled) -------------------------------- - MappingResources& m = this->mt.mapping_resources.lockInput(); - m.lidar_to_base = lidar_to_base_tf; - m.base_to_odom = base_to_odom_tf; - m.raw_scan = scan; - m.lo_buff.swap(lo_cloud); - #if PERCEPTION_USE_NULL_RAY_DELETION - this->scan_preproc.swapNullRays(m.null_vecs); - #endif - #if LFD_ENABLED // LFD doesnt share removal indices - m.nan_indices = nan_indices_ptr; - m.remove_indices = remove_indices_ptr; - #else - if (!m.nan_indices) - { - m.nan_indices = std::make_shared(); - } - if (!m.remove_indices) - { - m.remove_indices = std::make_shared(); - } - this->scan_preproc.swapNullIndices( - const_cast(*m.nan_indices)); - this->scan_preproc.swapRemovalIndices( - const_cast(*m.remove_indices)); - #endif - this->mt.mapping_resources.unlockInputAndNotify(m); -#endif - - PROFILING_NOTIFY2(odometry_map_export, odometry_debpub); - - // 5. Update Transforms ---------------------------------------------------- - util::geom::PoseTf3d prev_odom_tf, new_odom_tf; - const double prev_odom_stamp = this->transform_sync.getOdomTf(prev_odom_tf); - - // Update odom tf - this->transform_sync.endOdometryIterationSuccess( - base_to_odom_tf, - new_odom_stamp); - - // 6. Publish Debug Data --------------------------------------------------- - // Publish velocity - const double t_diff = - this->transform_sync.getOdomTf(new_odom_tf) - prev_odom_stamp; - Quatd odom_rot_q = prev_odom_tf.pose.quat.inverse() * new_odom_tf.pose.quat; - Vec3d l_vel = (new_odom_tf.pose.vec - prev_odom_tf.pose.vec) / t_diff; - Vec3d r_vel = (odom_rot_q.toRotationMatrix().eulerAngles(0, 1, 2) / t_diff); - - TwistStampedMsg odom_vel; - odom_vel.twist.linear << l_vel; - odom_vel.twist.angular << r_vel; - odom_vel.header.frame_id = this->odom_frame; - odom_vel.header.stamp = scan_stamp; - this->generic_pub.publish("odom_velocity", odom_vel); - - // this->generic_pub.publish( - // "odom_rot_error", - // Float64Msg{}.set__data(imu_rot_q.angularDistance(odom_rot_q))); - - // Publish LO debug -#if PERCEPTION_PUBLISH_LIO_DEBUG > 0 - this->lidar_odom.publishDebugScans( - this->generic_pub, - lo_status, - this->odom_frame); - this->lidar_odom.publishDebugMetrics(this->generic_pub); -#endif - - // Publish filtering debug -#if PERCEPTION_PUBLISH_TRJF_DEBUG > 0 - const auto& trjf = this->transform_sync.trajectoryFilter(); - - TrajectoryFilterDebugMsg dbg; - dbg.is_stable = trjf.lastFilterStatus(); - dbg.filter_mask = trjf.lastFilterMask(); - dbg.odom_queue_size = trjf.odomQueueSize(); - dbg.meas_queue_size = trjf.measurementQueueSize(); - dbg.trajectory_length = trjf.trajectoryQueueSize(); - dbg.filter_dt = trjf.lastFilterWindow(); - dbg.linear_error = trjf.lastLinearDelta(); - dbg.angular_error = trjf.lastAngularDelta(); - dbg.linear_deviation = trjf.lastLinearDeviation(); - dbg.angular_deviation = trjf.lastAngularDeviation(); - dbg.avg_linear_error = trjf.lastAvgLinearError(); - dbg.avg_angular_error = trjf.lastAvgAngularError(); - this->generic_pub.publish("metrics/trajectory_filter_stats", dbg); -#endif - - PROFILING_NOTIFY(odometry_debpub); -} - - - - - -#if LFD_ENABLED -void PerceptionNode::fiducial_callback_internal(FiducialResources& buff) -{ - PROFILING_NOTIFY(lfd_preprocess); - - this->transform_sync.beginMeasurementIteration(buff.iteration_count); - - pcl::PointCloud reflector_points; - pcl::fromROSMsg(*buff.raw_scan, reflector_points); - - util::pc_remove_selection(reflector_points, *buff.remove_indices); - // signals to odom thread that these buffers can be reused - buff.nan_indices.reset(); - buff.remove_indices.reset(); - - double stddev, delta_r; - const Vec3d grav_vec = - this->imu_samples.estimateGravity(0.5, &stddev, &delta_r); - - Vec3f up_vec{0, 0, 1}; - // if (stddev < 1. && delta_r < 0.01) - { - up_vec = grav_vec.template cast(); - } - Iso3f base_to_lidar_tf = buff.lidar_to_base.tf.inverse(); - up_vec = (base_to_lidar_tf * up_vec); - Vec3f base_pt = base_to_lidar_tf.translation(); - - util::geom::PoseTf3f fiducial_pose; - ReflectorHintMsg hint_msg; - Vec3f centroid; - PROFILING_NOTIFY2(lfd_preprocess, lfd_calculate); - auto result = this->fiducial_detector.calculatePose( - fiducial_pose.pose, - reflector_points, - &up_vec, - &base_pt); - hint_msg.samples = this->fiducial_detector.calculateReflectiveCentroid( - centroid, - hint_msg.variance); - PROFILING_NOTIFY2(lfd_calculate, lfd_export); - - if (result) - { - util::geom::Pose3d fiducial_to_base, base_to_fiducial; - fiducial_to_base - << (buff.lidar_to_base.tf * - (fiducial_pose.tf << fiducial_pose.pose)); - util::geom::inverse(base_to_fiducial, fiducial_to_base); - - this->transform_sync.endMeasurementIterationSuccess( - base_to_fiducial, - util::toFloatSeconds(buff.raw_scan->header.stamp)); - - PoseStampedMsg p; - p.pose << fiducial_to_base; - p.header.stamp = buff.raw_scan->header.stamp; - p.header.frame_id = this->base_frame; - - this->pose_pub.publish("fiducial_tag_pose", p); - } - else - { - this->transform_sync.endMeasurementIterationFailure(); - } - - hint_msg.centroid.point << (buff.lidar_to_base.tf * centroid); - hint_msg.centroid.header.stamp = buff.raw_scan->header.stamp; - hint_msg.centroid.header.frame_id = this->base_frame; - this->generic_pub.publish("reflector_hint", hint_msg); - - PROFILING_NOTIFY2(lfd_export, lfd_debpub); - - #if PERCEPTION_PUBLISH_LFD_DEBUG > 0 - try - { - PoseStampedMsg _p; - PointCloudMsg _pc; - - const auto& input_cloud = this->fiducial_detector.getInputPoints(); - const auto& redetect_cloud = - this->fiducial_detector.getRedetectPoints(); - - pcl::toROSMsg(input_cloud, _pc); - _pc.header = buff.raw_scan->header; - this->scan_pub.publish("fiducial_input_points", _pc); - - pcl::toROSMsg(redetect_cloud, _pc); - _pc.header = buff.raw_scan->header; - this->scan_pub.publish("fiducial_redetect_points", _pc); - - // if (result.has_point_num) - // { - const auto& seg_clouds = this->fiducial_detector.getSegClouds(); - const auto& seg_planes = this->fiducial_detector.getSegPlanes(); - const auto& seg_plane_centers = - this->fiducial_detector.getPlaneCenters(); - const auto& remaining_points = - this->fiducial_detector.getRemainingPoints(); - - for (uint32_t i = 0; i < 3; i++) - { - _p.header = buff.raw_scan->header; - _p.pose.position << seg_plane_centers[i]; - _p.pose.orientation << Quatf::FromTwoVectors( - Vec3f{1.f, 0.f, 0.f}, - seg_planes[i].head<3>()); - - std::string topic = - (std::ostringstream{} << "fiducial_plane_" << i << "/pose") - .str(); - this->pose_pub.publish(topic, _p); - - if (i < result.iterations) - { - pcl::toROSMsg(seg_clouds[i], _pc); - } - else - { - pcl::PointCloud empty_cloud; - pcl::toROSMsg(empty_cloud, _pc); - } - _pc.header = buff.raw_scan->header; - - topic = - ((std::ostringstream{} << "fiducial_plane_" << i << "/points") - .str()); - this->scan_pub.publish(topic, _pc); - } - - if (result.iterations == 3 && !remaining_points.empty()) - { - pcl::toROSMsg(remaining_points, _pc); - _pc.header = buff.raw_scan->header; - - this->scan_pub.publish("fiducial_unmodeled_points", _pc); - } - // } - } - catch (const std::exception& e) - { - RCLCPP_INFO( - this->get_logger(), - "[FIDUCIAL DETECTION]: Failed to publish debug data -- what():\n\t%s", - e.what()); - } - #endif - - PROFILING_NOTIFY(lfd_debpub); -} -#endif - - - - - -#if MAPPING_ENABLED -void PerceptionNode::mapping_callback_internal(MappingResources& buff) -{ - PROFILING_NOTIFY(mapping_preproc); - - // RCLCPP_INFO(this->get_logger(), "MAPPING CALLBACK INTERNAL"); - - util::geom::PoseTf3f lidar_to_odom_tf; - lidar_to_odom_tf.pose - << (lidar_to_odom_tf.tf = buff.base_to_odom.tf * buff.lidar_to_base.tf); - - pcl::PointCloud* filtered_scan_t = nullptr; - if constexpr (std::is_same::value) - { - pcl_conversions::toPCL(buff.raw_scan->header, buff.lo_buff.header); - pcl::transformPointCloud( - buff.lo_buff, - buff.lo_buff, - buff.base_to_odom.tf, - true); - filtered_scan_t = &buff.lo_buff; - } - else - { - thread_local pcl::PointCloud map_input_cloud; - pcl::fromROSMsg(*buff.raw_scan, map_input_cloud); - - util::pc_remove_selection(map_input_cloud, *buff.remove_indices); - pcl::transformPointCloud( - map_input_cloud, - map_input_cloud, - lidar_to_odom_tf.tf, - true); - filtered_scan_t = &map_input_cloud; - } - - if (this->param.map_crop_horizontal_range > 0. && - this->param.map_crop_vertical_range > 0.) - { - const Vec3f crop_range{ - static_cast(this->param.map_crop_horizontal_range), - static_cast(this->param.map_crop_horizontal_range), - static_cast(this->param.map_crop_vertical_range)}; - this->sparse_map.setBounds( - buff.base_to_odom.pose.vec - crop_range, - buff.base_to_odom.pose.vec + crop_range); - } - - #if PERCEPTION_USE_NULL_RAY_DELETION - for (RayDirectionType& r : buff.null_vecs) - { - r.getNormalVector4fMap() = - lidar_to_odom_tf.tf * r.getNormalVector4fMap(); - } - - PROFILING_NOTIFY2(mapping_preproc, mapping_kfc_update); - - auto results = this->sparse_map.updateMap( - lidar_to_odom_tf.pose.vec, - *filtered_scan_t, - buff.null_vecs); - #else - - PROFILING_NOTIFY2(mapping_preproc, mapping_kfc_update); - - auto results = - this->sparse_map.updateMap(lidar_to_odom_tf.pose.vec, *filtered_scan_t); - #endif - - if (this->param.map_export_horizontal_range > 0. && - this->param.map_export_vertical_range > 0.) - { - PROFILING_NOTIFY2(mapping_kfc_update, mapping_search_local); - - pcl::Indices export_points; - const Vec3f search_range{ - static_cast(this->param.map_export_horizontal_range), - static_cast(this->param.map_export_horizontal_range), - static_cast(this->param.map_export_vertical_range)}; - const Vec3f search_min{buff.base_to_odom.pose.vec - search_range}; - const Vec3f search_max{buff.base_to_odom.pose.vec + search_range}; - - this->sparse_map.getMap().boxSearch( - search_min, - search_max, - export_points); - - PROFILING_NOTIFY2(mapping_search_local, mapping_export_trav); - - #if TRAVERSIBILITY_ENABLED - { - auto& x = this->mt.traversibility_resources.lockInput(); - x.search_min = search_min; - x.search_max = search_max; - x.lidar_to_base = buff.lidar_to_base; - x.base_to_odom = buff.base_to_odom; - util::pc_copy_selection( - *this->sparse_map.getPoints(), - export_points, - x.points); - x.stamp = util::toFloatSeconds(buff.raw_scan->header.stamp); - this->mt.traversibility_resources.unlockInputAndNotify(x); - } - #else - try - { - thread_local pcl::PointCloud trav_points; - util::pc_copy_selection( - *this->sparse_map.getPoints(), - export_points, - trav_points); - - PointCloudMsg trav_points_output; - pcl::toROSMsg(trav_points, trav_points_output); - trav_points_output.header.stamp = - util::toTimeStamp(buff.raw_scan->header.stamp); - trav_points_output.header.frame_id = this->odom_frame; - this->scan_pub.publish("traversibility_points", trav_points_output); - } - catch (const std::exception& e) - { - RCLCPP_INFO( - this->get_logger(), - "[MAPPING]: Failed to publish traversibility subcloud -- what():\n\t%s", - e.what()); - } - #endif - - PROFILING_NOTIFY2(mapping_export_trav, mapping_debpub); - } - else - { - PROFILING_NOTIFY2(mapping_kfc_update, mapping_debpub); - } - - try - { - pcl::PointCloud output_buff; - const auto& map_pts = *this->sparse_map.getPoints(); - const auto& map_norms = this->sparse_map.getMap().pointNormals(); - - output_buff.points.resize(map_pts.size()); - for (size_t i = 0; i < output_buff.size(); i++) - { - output_buff.points[i].getVector3fMap() = - map_pts.points[i].getVector3fMap(); - output_buff.points[i].getNormalVector3fMap() = - map_norms[i].template head<3>(); - output_buff.points[i].curvature = map_norms[i][4]; - } - output_buff.height = map_pts.height; - output_buff.width = map_pts.width; - output_buff.is_dense = map_pts.is_dense; - - PointCloudMsg output; - #if PERCEPTION_PUBLISH_FULL_MAP > 0 - pcl::toROSMsg(output_buff, output); - output.header.stamp = buff.raw_scan->header.stamp; - output.header.frame_id = this->odom_frame; - this->scan_pub.publish("map_cloud", output); - #endif - - pcl::toROSMsg(*filtered_scan_t, output); - output.header.stamp = buff.raw_scan->header.stamp; - output.header.frame_id = this->odom_frame; - this->scan_pub.publish("filtered_scan", output); - } - catch (const std::exception& e) - { - RCLCPP_INFO( - this->get_logger(), - "[MAPPING]: Failed to publish mapping debug scans -- what():\n\t%s", - e.what()); - } - - this->metrics_pub.publish( - "mapping/search_pointset", - static_cast(results.points_searched)); - this->metrics_pub.publish( - "mapping/points_deleted", - static_cast(results.points_deleted)); - - PROFILING_NOTIFY(mapping_debpub); -} -#endif - - - - - -#if TRAVERSIBILITY_ENABLED -void PerceptionNode::traversibility_callback_internal( - TraversibilityResources& buff) -{ - PROFILING_NOTIFY(traversibility_preproc); - - thread_local Vec3f env_grav_vec{0.f, 0.f, 1.f}; - if (this->imu_samples.hasSamples()) - { - double stddev, delta_r; - const Vec3d grav_vec = - this->imu_samples.estimateGravity(0.5, &stddev, &delta_r); - if (stddev < 1. && delta_r < 0.01) - { - env_grav_vec = - (buff.base_to_odom.tf * grav_vec.template cast()) - .normalized(); - } - } - - PROFILING_NOTIFY2(traversibility_preproc, traversibility_gen_proc); - - this->trav_gen.processMapPoints( - buff.points, - buff.search_min, - buff.search_max, - env_grav_vec, - buff.base_to_odom.pose.vec); - - PROFILING_NOTIFY2(traversibility_gen_proc, traversibility_export); - - TraversibilityPointCloudType trav_points; - TraversibilityMetaCloudType trav_meta; - pcl::PointCloud trav_debug_cloud; - - this->trav_gen.swapPoints(trav_points); - this->trav_gen.swapMetaDataList(trav_meta.points); - - trav_debug_cloud.points.resize(trav_points.size()); - for (size_t i = 0; i < trav_points.size(); i++) - { - auto& out = trav_debug_cloud.points[i]; - out.getVector3fMap() = trav_points.points[i].getVector3fMap(); - out.getNormalVector3fMap() = trav_meta.points[i].getNormalVector3fMap(); - out.curvature = trav_meta.points[i].curvature; - out.intensity = trav_meta.points[i].trav_weight(); - } - trav_debug_cloud.height = 1; - trav_debug_cloud.width = trav_debug_cloud.points.size(); - trav_debug_cloud.is_dense = true; - - #if PATH_PLANNING_ENABLED - { - auto& x = this->mt.path_planning_resources.lockInput(); - x.stamp = buff.stamp; - x.local_bound_min = buff.search_min; - x.local_bound_max = buff.search_max; - x.base_to_odom = buff.base_to_odom; - x.trav_points.swap(trav_points); - x.trav_meta.swap(trav_meta); - this->mt.path_planning_resources.unlockInputAndNotify(x); - } - #endif - - PROFILING_NOTIFY2(traversibility_export, traversibility_debpub); - - try - { - PointCloudMsg output; - pcl::toROSMsg(trav_debug_cloud, output); - output.header.stamp = util::toTimeStamp(buff.stamp); - output.header.frame_id = this->odom_frame; - this->scan_pub.publish("traversibility_points", output); - } - catch (const std::exception& e) - { - RCLCPP_INFO( - this->get_logger(), - "[TRAVERSIBILITY]: Failed to publish debug cloud -- what():\n\t%s", - e.what()); - } - - PROFILING_NOTIFY(traversibility_debpub); -} -#endif - - - - - -#if PATH_PLANNING_ENABLED -void PerceptionNode::path_planning_callback_internal( - PathPlanningResources& buff) -{ - using namespace util::geom::cvt::ops; - - if (buff.target.header.frame_id != this->odom_frame) - { - try - { - auto tf = this->tf_buffer.lookupTransform( - this->odom_frame, - buff.target.header.frame_id, - // util::toTf2TimePoint(buff.stamp)); - tf2::timeFromSec(0)); - - // ensure tf stamp is not wildly out of date - - tf2::doTransform(buff.target, buff.target, tf); - } - catch (const std::exception& e) - { - RCLCPP_INFO( - this->get_logger(), - "[PATH PLANNING CALLBACK]: Failed to transform target pose from '%s' to '%s'\n\twhat(): %s", - buff.target.header.frame_id.c_str(), - this->odom_frame.c_str(), - e.what()); - return; - } - } - - Vec3f odom_target; - odom_target << buff.target.pose.position; - - std::vector path; - - if (!this->path_planner.solvePath( - buff.base_to_odom.pose.vec, - odom_target, - buff.local_bound_min, - buff.local_bound_max, - buff.trav_points, - buff.trav_meta, - path)) - { - return; - } - - PathMsg path_msg; - path_msg.header.frame_id = this->odom_frame; - path_msg.header.stamp = util::toTimeStamp(buff.stamp); - - path_msg.poses.reserve(path.size()); - for (const Vec3f& kp : path) - { - PoseStampedMsg& pose = path_msg.poses.emplace_back(); - pose.pose.position << kp; - pose.header.frame_id = this->odom_frame; - } - - this->generic_pub.publish("planned_path", path_msg); - this->generic_pub.publish("pplan_target", buff.target); - - // RCLCPP_INFO( - // this->get_logger(), - // "[PATH PLANNING CALLBACK]: Published path with %lu keypoints.", - // path_msg.poses.size()); -} -#endif - -}; // namespace perception -}; // namespace csm diff --git a/src/core/tag_detection.cpp b/src/core/tag_detection.cpp index 5ecd614..a912cef 100644 --- a/src/core/tag_detection.cpp +++ b/src/core/tag_detection.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -53,6 +53,9 @@ #include #endif +#include +#include + using namespace util::geom::cvt::ops; @@ -134,7 +137,7 @@ TagDetector::TagDetector() : img_transport{std::shared_ptr(this, [](auto*) {})}, mt_callback_group{ this->create_callback_group(rclcpp::CallbackGroupType::Reentrant)}, - generic_pub{this, "", rclcpp::SensorDataQoS{}}, + generic_pub{*this, "", rclcpp::SensorDataQoS{}}, aruco_params{cv::aruco::DetectorParameters::create()} { this->getParams(); diff --git a/src/core/tag_detection.hpp b/src/core/tag_detection.hpp index 19eab9b..8457af8 100644 --- a/src/core/tag_detection.hpp +++ b/src/core/tag_detection.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -70,9 +70,8 @@ #include -#include -#include -#include +#include +#include diff --git a/src/core/threads/imu_worker.cpp b/src/core/threads/imu_worker.cpp new file mode 100644 index 0000000..1130516 --- /dev/null +++ b/src/core/threads/imu_worker.cpp @@ -0,0 +1,125 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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 "imu_worker.hpp" + +#include + +#include + +#include + +#include + +#include +#include + + +using namespace util::geom::cvt::ops; + +using Vec3d = Eigen::Vector3d; +using Quatd = Eigen::Quaterniond; +using PoseStampedMsg = geometry_msgs::msg::PoseStamped; + + +namespace csm +{ +namespace perception +{ + +ImuWorker::ImuWorker(RclNode& node, const Tf2Buffer& tf_buffer) : + node{node}, + tf_buffer{tf_buffer}, + pub_map{node, PERCEPTION_TOPIC(""), PERCEPTION_PUBSUB_QOS} +{ +} + +void ImuWorker::configure(const std::string& base_frame) +{ + this->base_frame = base_frame; +} + +void ImuWorker::accept(const ImuMsg& msg) +{ + PROFILING_SYNC(); + PROFILING_NOTIFY_ALWAYS(imu); + + try + { + auto tf = this->tf_buffer.lookupTransform( + this->base_frame, + msg.header.frame_id, + util::toTf2TimePoint(msg.header.stamp)); + + ImuMsg tf_imu; + tf2::doTransform(msg, tf_imu, tf); + + this->imu_sampler.addSample(tf_imu); + } + catch (const std::exception& e) + { + RCLCPP_INFO( + this->node.get_logger(), + "[IMU CALLBACK]: Failed to process imu measurment.\n\twhat(): %s", + e.what()); + } + +#if PERCEPTION_PUBLISH_GRAV_ESTIMATION > 0 + double stddev, delta_r; + Vec3d grav_vec = this->imu_sampler.estimateGravity(1., &stddev, &delta_r); + + PoseStampedMsg grav_pub; + grav_pub.header.stamp = msg.header.stamp; + grav_pub.header.frame_id = this->base_frame; + grav_pub.pose.orientation + << Quatd::FromTwoVectors(Vec3d{1., 0., 0.}, grav_vec); + this->pub_map.publish("poses/gravity_estimation", grav_pub); + this->pub_map.publish( + "metrics/gravity_estimation/acc_stddev", + stddev); + this->pub_map.publish( + "metrics/gravity_estimation/delta_rotation", + delta_r); +#endif + + PROFILING_NOTIFY_ALWAYS(imu); +} + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/imu_worker.hpp b/src/core/threads/imu_worker.hpp new file mode 100644 index 0000000..41838c1 --- /dev/null +++ b/src/core/threads/imu_worker.hpp @@ -0,0 +1,98 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include "../perception_presets.hpp" + + +namespace csm +{ +namespace perception +{ + +class ImuWorker +{ + friend class PerceptionNode; + + using RclNode = rclcpp::Node; + using Tf2Buffer = tf2_ros::Buffer; + + using ImuMsg = sensor_msgs::msg::Imu; + +public: + ImuWorker(RclNode& node, const Tf2Buffer& tf_buffer); + ~ImuWorker() = default; + +public: + void configure(const std::string& base_frame); + + void accept(const ImuMsg& msg); + inline ImuIntegrator<>& getSampler() { return this->imu_sampler; } + inline const ImuIntegrator<>& getSampler() const + { + return this->imu_sampler; + } + +protected: + RclNode& node; + const Tf2Buffer& tf_buffer; + util::GenericPubMap pub_map; + + ImuIntegrator<> imu_sampler; + + std::string base_frame; +}; + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/localization_worker.cpp b/src/core/threads/localization_worker.cpp new file mode 100644 index 0000000..fb2166d --- /dev/null +++ b/src/core/threads/localization_worker.cpp @@ -0,0 +1,590 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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 "localization_worker.hpp" + +#include +#include + +#include + +#include + +#include + +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include + + +using namespace util::geom::cvt::ops; + +using Vec3f = Eigen::Vector3f; +using Vec3d = Eigen::Vector3d; +using Mat4f = Eigen::Matrix4f; +using Iso3f = Eigen::Isometry3f; +using Quatf = Eigen::Quaternionf; +using Quatd = Eigen::Quaterniond; + +using PoseStampedMsg = geometry_msgs::msg::PoseStamped; +using TwistStampedMsg = geometry_msgs::msg::TwistStamped; +using TransformStampedMsg = geometry_msgs::msg::TransformStamped; + +using ReflectorHintMsg = cardinal_perception::msg::ReflectorHint; +using TrajectoryFilterDebugMsg = + cardinal_perception::msg::TrajectoryFilterDebug; + + +namespace csm +{ +namespace perception +{ + +LocalizationWorker::LocalizationWorker( + RclNode& node, + Tf2Buffer& tf_buffer, + const ImuIntegrator<>& imu_sampler) : + node{node}, + tf_buffer{tf_buffer}, + imu_sampler{imu_sampler}, + tf_broadcaster{node}, + pub_map{node, PERCEPTION_TOPIC(""), PERCEPTION_PUBSUB_QOS}, + lidar_odom{node}, + transform_sync{tf_broadcaster, &tf_buffer} +{ +} + +LocalizationWorker::~LocalizationWorker() { this->stopThreads(); } + +void LocalizationWorker::configure( + const std::string& map_frame, + const std::string& odom_frame, + const std::string& base_frame) +{ + this->map_frame = map_frame; + this->odom_frame = odom_frame; + this->base_frame = base_frame; + this->transform_sync.setFrameIds(map_frame, odom_frame, base_frame); +} + +void LocalizationWorker::accept(const PointCloudMsg::ConstSharedPtr& msg) +{ + this->odometry_resources.updateAndNotify(msg); +} + +#if TAG_DETECTION_ENABLED +void LocalizationWorker::accept(const TagsTransformMsg::ConstSharedPtr& msg) +{ + if (!this->global_alignment_enabled) + { + return; + } + + PROFILING_SYNC(); + PROFILING_NOTIFY_ALWAYS(tags_detection); + + constexpr int tag_usage_mode = + 1; // TODO: parameter (if we ever use this again) + + const TransformStampedMsg& tf = msg->estimated_tf; + if (tf.header.frame_id == this->map_frame && + tf.child_frame_id == this->base_frame && + (tag_usage_mode > 0 || (tag_usage_mode < 0 && msg->filter_mask >= 31))) + { + TagDetection::Ptr td = std::make_shared(); + td->pose << tf.transform; + td->time_point = util::toFloatSeconds(tf.header.stamp); + td->pix_area = msg->pix_area; + td->avg_range = msg->avg_range; + td->rms = msg->rms; + td->num_tags = msg->num_tags; + this->transform_sync.endMeasurementIterationSuccess(td, td->time_point); + } + + PROFILING_NOTIFY_ALWAYS(tags_detection); +} +#endif + +bool LocalizationWorker::setGlobalAlignmentEnabled(bool enable) +{ + return (this->global_alignment_enabled = enable); +} + +void LocalizationWorker::connectOutput( + util::ResourcePipeline& mapping_resources) +{ + this->mapping_resources = &mapping_resources; +} + +void LocalizationWorker::startThreads() +{ + if (!this->threads_running) + { + this->threads_running = true; + this->odometry_thread = + std::thread{&LocalizationWorker::odom_thread_worker, this}; +#if LFD_ENABLED + this->fiducial_thread = + std::thread{&LocalizationWorker::fiducial_thread_worker, this}; +#endif + } +} + +void LocalizationWorker::stopThreads() +{ + this->threads_running = false; + + this->odometry_resources.notifyExit(); + if (this->odometry_thread.joinable()) + { + this->odometry_thread.join(); + } + +#if LFD_ENABLED + this->fiducial_resources.notifyExit(); + if (this->fiducial_thread.joinable()) + { + this->fiducial_thread.join(); + } +#endif +} + + +void LocalizationWorker::odom_thread_worker() +{ + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Odometry thread started successfully."); + + do + { + auto& scan = this->odometry_resources.waitNewestResource(); + if (!this->threads_running.load()) + { + break; + } + + PROFILING_SYNC(); + PROFILING_NOTIFY_ALWAYS(odometry); + this->scan_callback(scan); + PROFILING_NOTIFY_ALWAYS(odometry); + } // + while (this->threads_running.load()); + + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Odometry thread exited cleanly."); +} + +void LocalizationWorker::scan_callback( + const PointCloudMsg::ConstSharedPtr& scan) +{ + PROFILING_NOTIFY(odometry_init); + + pcl::PointCloud lo_cloud; + util::geom::PoseTf3f lidar_to_base_tf, base_to_odom_tf; + + PROFILING_NOTIFY2(odometry_init, odometry_preprocess); + + // 1. Preprocess Scan ------------------------------------------------------ + constexpr int Preproc_Config = + (decltype(this->scan_preproc)::PREPROC_EXCLUDE_ZONES | + (decltype(this->scan_preproc)::PREPROC_PERFORM_DESKEW * + (PERCEPTION_USE_SCAN_DESKEW != 0)) | + (decltype(this->scan_preproc)::PREPROC_EXPORT_NULL_RAYS * + (PERCEPTION_USE_NULL_RAY_DELETION != 0))); + this->scan_preproc.process( + *scan, + this->base_frame, + this->tf_buffer, + this->imu_sampler); + + this->scan_preproc.swapOutputPoints(lo_cloud); + lidar_to_base_tf.pose + << (lidar_to_base_tf.tf = this->scan_preproc.getTargetTf()); + + PROFILING_NOTIFY2(odometry_preprocess, odometry_lfd_export); + + const uint32_t iteration_token = + this->transform_sync.beginOdometryIteration(); + +#if LFD_ENABLED + // 2. Export LFD Resources (if enabled) ------------------------------------ + FiducialResources& f = this->fiducial_resources.lockInput(); + f.lidar_to_base = lidar_to_base_tf; + f.raw_scan = scan; + if (!f.nan_indices || f.nan_indices.use_count() > 1) + { + f.nan_indices = std::make_shared(); + } + if (!f.remove_indices || f.remove_indices.use_count() > 1) + { + f.remove_indices = std::make_shared(); + } + this->scan_preproc.swapNullIndices( + const_cast(*f.nan_indices)); + this->scan_preproc.swapRemovalIndices( + const_cast(*f.remove_indices)); + const auto& nan_indices_ptr = f.nan_indices; + const auto& remove_indices_ptr = f.remove_indices; + f.iteration_count = iteration_token; + this->fiducial_resources.unlockInputAndNotify(f); +#else + (void)iteration_token; +#endif + + PROFILING_NOTIFY2(odometry_lfd_export, odometry_lio); + + // apply sensor origin + lo_cloud.sensor_origin_ << lidar_to_base_tf.pose.vec, 1.f; + const double new_odom_stamp = util::toFloatSeconds(scan->header.stamp); + + // get imu estimated rotation + Quatd imu_rot_q = Quatd::Identity(); + Mat4f imu_rot = Mat4f::Identity(); + if (this->imu_sampler.hasSamples()) + { + imu_rot_q = this->imu_sampler.getDelta( + this->lidar_odom.prevStamp(), + new_odom_stamp); + imu_rot.block<3, 3>(0, 0) = + imu_rot_q.template cast().toRotationMatrix(); + } + + // 3. Iterate LIO ---------------------------------------------------------- + auto lo_status = this->lidar_odom.processScan( + lo_cloud, + new_odom_stamp, + base_to_odom_tf, + imu_rot); + + PROFILING_NOTIFY(odometry_lio); + + if (!lo_status) + { + this->transform_sync.endOdometryIterationFailure(); + return; + } + + PROFILING_NOTIFY(odometry_map_export); + + // 4. Export Mapping Resources (if connected) ------------------------------ + if (this->mapping_resources) + { + MappingResources& m = this->mapping_resources->lockInput(); + m.lidar_to_base = lidar_to_base_tf; + m.base_to_odom = base_to_odom_tf; + m.raw_scan = scan; + m.lidar_odom_buff.swap(lo_cloud); +#if PERCEPTION_USE_NULL_RAY_DELETION + this->scan_preproc.swapNullRays(m.null_vecs); +#endif +#if LFD_ENABLED // LFD doesnt share removal indices + m.nan_indices = nan_indices_ptr; + m.remove_indices = remove_indices_ptr; +#else + if (!m.nan_indices) + { + m.nan_indices = std::make_shared(); + } + if (!m.remove_indices) + { + m.remove_indices = std::make_shared(); + } + this->scan_preproc.swapNullIndices( + const_cast(*m.nan_indices)); + this->scan_preproc.swapRemovalIndices( + const_cast(*m.remove_indices)); +#endif + this->mapping_resources->unlockInputAndNotify(m); + } + + PROFILING_NOTIFY2(odometry_map_export, odometry_debpub); + + // 5. Update Transforms ---------------------------------------------------- + util::geom::PoseTf3d prev_odom_tf, new_odom_tf; + const double prev_odom_stamp = this->transform_sync.getOdomTf(prev_odom_tf); + + // Update odom tf + this->transform_sync.endOdometryIterationSuccess( + base_to_odom_tf, + new_odom_stamp); + + // 6. Publish Debug Data --------------------------------------------------- + // Publish velocity + const double t_diff = + this->transform_sync.getOdomTf(new_odom_tf) - prev_odom_stamp; + Quatd odom_rot_q = prev_odom_tf.pose.quat.inverse() * new_odom_tf.pose.quat; + Vec3d l_vel = (new_odom_tf.pose.vec - prev_odom_tf.pose.vec) / t_diff; + Vec3d r_vel = (odom_rot_q.toRotationMatrix().eulerAngles(0, 1, 2) / t_diff); + + TwistStampedMsg odom_vel; + odom_vel.twist.linear << l_vel; + odom_vel.twist.angular << r_vel; + odom_vel.header.frame_id = this->odom_frame; + odom_vel.header.stamp = scan->header.stamp; + this->pub_map.publish("odom_velocity", odom_vel); + + // Publish LO debug +#if PERCEPTION_PUBLISH_LIO_DEBUG > 0 + this->lidar_odom.publishDebugScans( + this->pub_map, + lo_status, + this->odom_frame); + this->lidar_odom.publishDebugMetrics(this->pub_map); +#endif + + // Publish filtering debug +#if PERCEPTION_PUBLISH_TRJF_DEBUG > 0 + const auto& trjf = this->transform_sync.trajectoryFilter(); + + TrajectoryFilterDebugMsg dbg; + dbg.is_stable = trjf.lastFilterStatus(); + dbg.filter_mask = trjf.lastFilterMask(); + dbg.odom_queue_size = trjf.odomQueueSize(); + dbg.meas_queue_size = trjf.measurementQueueSize(); + dbg.trajectory_length = trjf.trajectoryQueueSize(); + dbg.filter_dt = trjf.lastFilterWindow(); + dbg.linear_error = trjf.lastLinearDelta(); + dbg.angular_error = trjf.lastAngularDelta(); + dbg.linear_deviation = trjf.lastLinearDeviation(); + dbg.angular_deviation = trjf.lastAngularDeviation(); + dbg.avg_linear_error = trjf.lastAvgLinearError(); + dbg.avg_angular_error = trjf.lastAvgAngularError(); + this->pub_map.publish("metrics/trajectory_filter_stats", dbg); +#endif + + PROFILING_NOTIFY(odometry_debpub); +} + + +#if LFD_ENABLED +void LocalizationWorker::fiducial_thread_worker() +{ + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Lidar fiducial detection thread started successfully."); + + do + { + auto& buff = this->fiducial_resources.waitNewestResource(); + if (!this->threads_running.load()) + { + break; + } + + if (!this->global_alignment_enabled) + { + continue; + } + + PROFILING_SYNC(); + PROFILING_NOTIFY_ALWAYS(lidar_fiducial); + this->fiducial_callback(buff); + PROFILING_NOTIFY_ALWAYS(lidar_fiducial); + } // + while (this->threads_running.load()); + + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Lidar fiducial detection thread exited cleanly."); +} + +void LocalizationWorker::fiducial_callback(FiducialResources& buff) +{ + PROFILING_NOTIFY(lfd_preprocess); + + this->transform_sync.beginMeasurementIteration(buff.iteration_count); + + pcl::PointCloud reflector_points; + pcl::fromROSMsg(*buff.raw_scan, reflector_points); + + util::removeSelection(reflector_points, *buff.remove_indices); + // signals to odom thread that these buffers can be reused + buff.nan_indices.reset(); + buff.remove_indices.reset(); + + double stddev, delta_r; + const Vec3d grav_vec = + this->imu_sampler.estimateGravity(0.5, &stddev, &delta_r); + + Vec3f up_vec{0, 0, 1}; + // if (stddev < 1. && delta_r < 0.01) + { + up_vec = grav_vec.template cast(); + } + Iso3f base_to_lidar_tf = buff.lidar_to_base.tf.inverse(); + up_vec = (base_to_lidar_tf * up_vec); + Vec3f base_pt = base_to_lidar_tf.translation(); + + util::geom::PoseTf3f fiducial_pose; + ReflectorHintMsg hint_msg; + Vec3f centroid; + PROFILING_NOTIFY2(lfd_preprocess, lfd_calculate); + auto result = this->fiducial_detector.calculatePose( + fiducial_pose.pose, + reflector_points, + &up_vec, + &base_pt); + hint_msg.samples = this->fiducial_detector.calculateReflectiveCentroid( + centroid, + hint_msg.variance); + PROFILING_NOTIFY2(lfd_calculate, lfd_export); + + if (result) + { + util::geom::Pose3d fiducial_to_base, base_to_fiducial; + fiducial_to_base + << (buff.lidar_to_base.tf * + (fiducial_pose.tf << fiducial_pose.pose)); + util::geom::inverse(base_to_fiducial, fiducial_to_base); + + this->transform_sync.endMeasurementIterationSuccess( + base_to_fiducial, + util::toFloatSeconds(buff.raw_scan->header.stamp)); + + PoseStampedMsg p; + p.pose << fiducial_to_base; + p.header.stamp = buff.raw_scan->header.stamp; + p.header.frame_id = this->base_frame; + + this->pub_map.publish("poses/fiducial_tag_pose", p); + } + else + { + this->transform_sync.endMeasurementIterationFailure(); + } + + hint_msg.centroid.point << (buff.lidar_to_base.tf * centroid); + hint_msg.centroid.header.stamp = buff.raw_scan->header.stamp; + hint_msg.centroid.header.frame_id = this->base_frame; + this->pub_map.publish("reflector_hint", hint_msg); + + PROFILING_NOTIFY2(lfd_export, lfd_debpub); + + #if PERCEPTION_PUBLISH_LFD_DEBUG > 0 + try + { + PoseStampedMsg p_; + PointCloudMsg pc_; + + const auto& input_cloud = this->fiducial_detector.getInputPoints(); + const auto& redetect_cloud = + this->fiducial_detector.getRedetectPoints(); + + pcl::toROSMsg(input_cloud, pc_); + pc_.header = buff.raw_scan->header; + this->pub_map.publish("fiducial_input_points", pc_); + + pcl::toROSMsg(redetect_cloud, pc_); + pc_.header = buff.raw_scan->header; + this->pub_map.publish("fiducial_redetect_points", pc_); + + // if (result.has_point_num) + // { + const auto& seg_clouds = this->fiducial_detector.getSegClouds(); + const auto& seg_planes = this->fiducial_detector.getSegPlanes(); + const auto& seg_plane_centers = + this->fiducial_detector.getPlaneCenters(); + const auto& remaining_points = + this->fiducial_detector.getRemainingPoints(); + + for (uint32_t i = 0; i < 3; i++) + { + p_.header = buff.raw_scan->header; + p_.pose.position << seg_plane_centers[i]; + p_.pose.orientation << Quatf::FromTwoVectors( + Vec3f{1.f, 0.f, 0.f}, + seg_planes[i].head<3>()); + + std::string topic = + (std::ostringstream{} << "fiducial_plane_" << i << "/pose") + .str(); + this->pub_map.publish(topic, p_); + + if (i < result.iterations) + { + pcl::toROSMsg(seg_clouds[i], pc_); + } + else + { + pcl::PointCloud empty_cloud; + pcl::toROSMsg(empty_cloud, pc_); + } + pc_.header = buff.raw_scan->header; + + topic = + ((std::ostringstream{} << "fiducial_plane_" << i << "/points") + .str()); + this->pub_map.publish(topic, pc_); + } + + if (result.iterations == 3 && !remaining_points.empty()) + { + pcl::toROSMsg(remaining_points, pc_); + pc_.header = buff.raw_scan->header; + + this->pub_map.publish("fiducial_unmodeled_points", pc_); + } + // } + } + catch (const std::exception& e) + { + RCLCPP_INFO( + this->node.get_logger(), + "[FIDUCIAL DETECTION]: Failed to publish debug data -- what():\n\t%s", + e.what()); + } + #endif + + PROFILING_NOTIFY(lfd_debpub); +} +#endif + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/localization_worker.hpp b/src/core/threads/localization_worker.hpp new file mode 100644 index 0000000..0759a4a --- /dev/null +++ b/src/core/threads/localization_worker.hpp @@ -0,0 +1,184 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include + +#include +#include +#include + +#include + +#include +#include + +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include "shared_resources.hpp" +#include "../perception_presets.hpp" + + +namespace csm +{ +namespace perception +{ + +class LocalizationWorker +{ + friend class PerceptionNode; + + using RclNode = rclcpp::Node; + using Tf2Buffer = tf2_ros::Buffer; + using Tf2Broadcaster = tf2_ros::TransformBroadcaster; + + using PointCloudMsg = sensor_msgs::msg::PointCloud2; + using TagsTransformMsg = cardinal_perception::msg::TagsTransform; + +public: + LocalizationWorker( + RclNode& node, + Tf2Buffer& tf_buffer, + const ImuIntegrator<>& imu_sampler); + ~LocalizationWorker(); + +public: + void configure( + const std::string& map_frame, + const std::string& odom_frame, + const std::string& base_frame); + + void accept(const PointCloudMsg::ConstSharedPtr& msg); +#if TAG_DETECTION_ENABLED + void accept(const TagsTransformMsg::ConstSharedPtr& msg); +#endif + bool setGlobalAlignmentEnabled(bool enable); + + void connectOutput( + util::ResourcePipeline& mapping_resources); + + void startThreads(); + void stopThreads(); + +protected: +#if TAG_DETECTION_ENABLED + struct TagDetection + { + using Ptr = std::shared_ptr; + using ConstPtr = std::shared_ptr; + + util::geom::Pose3d pose; + + double time_point, pix_area, avg_range, rms; + size_t num_tags; + + inline operator util::geom::Pose3d&() { return this->pose; } + }; +#endif +#if LFD_ENABLED + struct FiducialResources + { + util::geom::PoseTf3f lidar_to_base; + PointCloudMsg::ConstSharedPtr raw_scan; + std::shared_ptr nan_indices, remove_indices; + uint32_t iteration_count; + }; +#endif + +protected: + void odom_thread_worker(); + void scan_callback(const PointCloudMsg::ConstSharedPtr& scan); +#if LFD_ENABLED + void fiducial_thread_worker(); + void fiducial_callback(FiducialResources& buff); +#endif + +protected: + RclNode& node; + const Tf2Buffer& tf_buffer; + const ImuIntegrator<>& imu_sampler; + Tf2Broadcaster tf_broadcaster; + util::GenericPubMap pub_map; + + std::string map_frame; + std::string odom_frame; + std::string base_frame; + + std::atomic threads_running{false}; + std::atomic global_alignment_enabled{true}; + + ScanPreprocessor< + OdomPointType, + RayDirectionType, + SphericalDirectionPointType, + TimestampPointType> + scan_preproc; + LidarOdometry lidar_odom; +#if LFD_ENABLED + LidarFiducialDetector fiducial_detector; +#endif +#if TAG_DETECTION_ENABLED + TransformSynchronizer transform_sync; +#else + TransformSynchronizer transform_sync; +#endif + + util::ResourcePipeline odometry_resources; + std::thread odometry_thread; +#if LFD_ENABLED + util::ResourcePipeline fiducial_resources; + std::thread fiducial_thread; +#endif + util::ResourcePipeline* mapping_resources{nullptr}; +}; + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/mapping_worker.cpp b/src/core/threads/mapping_worker.cpp new file mode 100644 index 0000000..9f7eb13 --- /dev/null +++ b/src/core/threads/mapping_worker.cpp @@ -0,0 +1,343 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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 "mapping_worker.hpp" + +#include + +#include +#include + +#include + +#include + +#include + +#include +#include +#include + + +using namespace util::geom::cvt::ops; + +using Vec3f = Eigen::Vector3f; + +using PointCloudMsg = sensor_msgs::msg::PointCloud2; + + +namespace csm +{ +namespace perception +{ + +MappingWorker::MappingWorker(RclNode& node) : + node{node}, + pub_map{node, PERCEPTION_TOPIC(""), PERCEPTION_PUBSUB_QOS} +{ +} + +MappingWorker::~MappingWorker() { this->stopThreads(); } + +void MappingWorker::configure( + const std::string& odom_frame, + float map_crop_horizontal_range, + float map_crop_vertical_range, + float map_export_horizontal_range, + float map_export_vertical_range) +{ + this->odom_frame = odom_frame; + this->map_crop_horizontal_range = map_crop_horizontal_range; + this->map_crop_vertical_range = map_crop_vertical_range; + this->map_export_horizontal_range = map_export_horizontal_range; + this->map_export_vertical_range = map_export_vertical_range; +} + +util::ResourcePipeline& MappingWorker::getInput() +{ + return this->mapping_resources; +} +void MappingWorker::connectOutput( + util::ResourcePipeline& traversibility_resources) +{ + this->traversibility_resources = &traversibility_resources; +} + +void MappingWorker::startThreads() +{ + if (!this->threads_running) + { + this->threads_running = true; + this->mapping_thread = + std::thread{&MappingWorker::mapping_thread_worker, this}; + } +} + +void MappingWorker::stopThreads() +{ + this->threads_running = false; + + this->mapping_resources.notifyExit(); + if (this->mapping_thread.joinable()) + { + this->mapping_thread.join(); + } +} + +void MappingWorker::mapping_thread_worker() +{ + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Mapping thread started successfully."); + + do + { + auto& buff = this->mapping_resources.waitNewestResource(); + if (!this->threads_running.load()) + { + break; + } + + PROFILING_SYNC(); + PROFILING_NOTIFY_ALWAYS(mapping); + this->mapping_callback(buff); + PROFILING_NOTIFY_ALWAYS(mapping); + } // + while (this->threads_running.load()); + + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Mapping thread exited cleanly."); +} + + +void MappingWorker::mapping_callback(MappingResources& buff) +{ + PROFILING_NOTIFY(mapping_preproc); + + // RCLCPP_INFO(this->get_logger(), "MAPPING CALLBACK INTERNAL"); + + util::geom::PoseTf3f lidar_to_odom_tf; + lidar_to_odom_tf.pose + << (lidar_to_odom_tf.tf = buff.base_to_odom.tf * buff.lidar_to_base.tf); + + pcl::PointCloud* filtered_scan_t = nullptr; + if constexpr (std::is_same::value) + { + pcl_conversions::toPCL( + buff.raw_scan->header, + buff.lidar_odom_buff.header); + pcl::transformPointCloud( + buff.lidar_odom_buff, + buff.lidar_odom_buff, + buff.base_to_odom.tf, + true); + filtered_scan_t = &buff.lidar_odom_buff; + } + else + { + thread_local pcl::PointCloud map_input_cloud; + pcl::fromROSMsg(*buff.raw_scan, map_input_cloud); + + util::removeSelection(map_input_cloud, *buff.remove_indices); + pcl::transformPointCloud( + map_input_cloud, + map_input_cloud, + lidar_to_odom_tf.tf, + true); + filtered_scan_t = &map_input_cloud; + } + + if (this->map_crop_horizontal_range > 0. && + this->map_crop_vertical_range > 0.) + { + const Vec3f crop_range{ + this->map_crop_horizontal_range, + this->map_crop_horizontal_range, + this->map_crop_vertical_range}; + this->sparse_map.setBounds( + buff.base_to_odom.pose.vec - crop_range, + buff.base_to_odom.pose.vec + crop_range); + } + +#if PERCEPTION_USE_NULL_RAY_DELETION + for (RayDirectionType& r : buff.null_vecs) + { + r.getNormalVector4fMap() = + lidar_to_odom_tf.tf * r.getNormalVector4fMap(); + } + + PROFILING_NOTIFY2(mapping_preproc, mapping_kfc_update); + + auto results = this->sparse_map.updateMap( + lidar_to_odom_tf.pose.vec, + *filtered_scan_t, + buff.null_vecs); +#else + + PROFILING_NOTIFY2(mapping_preproc, mapping_kfc_update); + + auto results = + this->sparse_map.updateMap(lidar_to_odom_tf.pose.vec, *filtered_scan_t); +#endif + + if (this->map_export_horizontal_range > 0. && + this->map_export_vertical_range > 0.) + { + PROFILING_NOTIFY2(mapping_kfc_update, mapping_search_local); + + pcl::Indices export_points; + const Vec3f search_range{ + this->map_export_horizontal_range, + this->map_export_horizontal_range, + this->map_export_vertical_range}; + const Vec3f search_min{buff.base_to_odom.pose.vec - search_range}; + const Vec3f search_max{buff.base_to_odom.pose.vec + search_range}; + + this->sparse_map.getMap().boxSearch( + search_min, + search_max, + export_points); + + PROFILING_NOTIFY2(mapping_search_local, mapping_export_trav); + + if (this->traversibility_resources) + { + auto& x = this->traversibility_resources->lockInput(); + x.bounds_min = search_min; + x.bounds_max = search_max; + x.lidar_to_base = buff.lidar_to_base; + x.base_to_odom = buff.base_to_odom; + util::copySelection( + this->sparse_map.getPoints(), + export_points, + x.points); + x.stamp = util::toFloatSeconds(buff.raw_scan->header.stamp); + this->traversibility_resources->unlockInputAndNotify(x); + } + else + { + try + { + thread_local pcl::PointCloud trav_points; + util::copySelection( + this->sparse_map.getPoints(), + export_points, + trav_points); + + PointCloudMsg trav_points_output; + pcl::toROSMsg(trav_points, trav_points_output); + trav_points_output.header.stamp = + util::toTimeMsg(buff.raw_scan->header.stamp); + trav_points_output.header.frame_id = this->odom_frame; + this->pub_map.publish( + "traversibility_points", + trav_points_output); + } + catch (const std::exception& e) + { + RCLCPP_INFO( + this->node.get_logger(), + "[MAPPING]: Failed to publish traversibility subcloud -- what():\n\t%s", + e.what()); + } + } + + PROFILING_NOTIFY2(mapping_export_trav, mapping_debpub); + } + else + { + PROFILING_NOTIFY2(mapping_kfc_update, mapping_debpub); + } + + try + { + pcl::PointCloud output_buff; + const auto& map_pts = this->sparse_map.getPoints(); + const auto& map_norms = this->sparse_map.getMap().pointNormals(); + + output_buff.points.resize(map_pts.size()); + for (size_t i = 0; i < output_buff.size(); i++) + { + output_buff.points[i].getVector3fMap() = + map_pts.points[i].getVector3fMap(); + output_buff.points[i].getNormalVector3fMap() = + map_norms[i].template head<3>(); + output_buff.points[i].curvature = map_norms[i][4]; + } + output_buff.height = map_pts.height; + output_buff.width = map_pts.width; + output_buff.is_dense = map_pts.is_dense; + + PointCloudMsg output; +#if PERCEPTION_PUBLISH_FULL_MAP > 0 + pcl::toROSMsg(output_buff, output); + output.header.stamp = buff.raw_scan->header.stamp; + output.header.frame_id = this->odom_frame; + this->pub_map.publish("map_cloud", output); +#endif + + pcl::toROSMsg(*filtered_scan_t, output); + output.header.stamp = buff.raw_scan->header.stamp; + output.header.frame_id = this->odom_frame; + this->pub_map.publish("filtered_scan", output); + } + catch (const std::exception& e) + { + RCLCPP_INFO( + this->node.get_logger(), + "[MAPPING]: Failed to publish mapping debug scans -- what():\n\t%s", + e.what()); + } + + this->pub_map.publish( + "metrics/mapping/search_pointset", + static_cast(results.points_searched)); + this->pub_map.publish( + "metrics/mapping/points_deleted", + static_cast(results.points_deleted)); + this->pub_map.publish( + "metrics/mapping/octree_size", + this->sparse_map.getMap().octreeSize()); + + PROFILING_NOTIFY(mapping_debpub); +} + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/mapping_worker.hpp b/src/core/threads/mapping_worker.hpp new file mode 100644 index 0000000..2185d18 --- /dev/null +++ b/src/core/threads/mapping_worker.hpp @@ -0,0 +1,119 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include + +#include +#include +#include + +#include + +#include +#include + +#include +#include + +#include "shared_resources.hpp" +#include "../perception_presets.hpp" + + +namespace csm +{ +namespace perception +{ + +class MappingWorker +{ + friend class PerceptionNode; + + using RclNode = rclcpp::Node; + + template + using SparseMap = + KFCMap>; + +public: + MappingWorker(RclNode& node); + ~MappingWorker(); + +public: + void configure( + const std::string& odom_frame, + float map_crop_horizontal_range, + float map_crop_vertical_range, + float map_export_horizontal_range, + float map_export_vertical_range); + + util::ResourcePipeline& getInput(); + void connectOutput( + util::ResourcePipeline& + traversibility_resources); + + void startThreads(); + void stopThreads(); + +protected: + void mapping_thread_worker(); + void mapping_callback(MappingResources& buff); + +protected: + RclNode& node; + util::GenericPubMap pub_map; + + std::string odom_frame; + float map_crop_horizontal_range{0.f}; + float map_crop_vertical_range{0.f}; + float map_export_horizontal_range{0.f}; + float map_export_vertical_range{0.f}; + + std::atomic threads_running{false}; + + SparseMap sparse_map; + util::ResourcePipeline mapping_resources; + util::ResourcePipeline* traversibility_resources{ + nullptr}; + std::thread mapping_thread; +}; + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/mining_eval_worker.cpp b/src/core/threads/mining_eval_worker.cpp new file mode 100644 index 0000000..2a08495 --- /dev/null +++ b/src/core/threads/mining_eval_worker.cpp @@ -0,0 +1,231 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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 "mining_eval_worker.hpp" + +#include + +#include + +#include + +#include + +#include +#include + + +using Vec3f = Eigen::Vector3f; +using Iso3f = Eigen::Isometry3f; +using Box3f = Eigen::AlignedBox3f; + +using MiningEvalResultsMsg = cardinal_perception::msg::MiningEvalResults; + +using namespace util::geom::cvt::ops; + + +namespace csm +{ +namespace perception +{ + +MiningEvalWorker::MiningEvalWorker(RclNode& node, const Tf2Buffer& tf_buffer) : + node{node}, + tf_buffer{tf_buffer}, + pub_map{node, PERCEPTION_TOPIC(""), PERCEPTION_PUBSUB_QOS} +{ +} + +MiningEvalWorker::~MiningEvalWorker() { this->stopThreads(); } + +void MiningEvalWorker::configure(const std::string& odom_frame) +{ + this->odom_frame = odom_frame; +} + +void MiningEvalWorker::accept( + const UpdateMiningEvalSrv::Request::SharedPtr& req, + const UpdateMiningEvalSrv::Response::SharedPtr& resp) +{ + if (req->completed) + { + this->srv_enable_state = false; + resp->query_id = -1; + } + else + { + this->srv_enable_state = true; + { + auto& buff = this->query_notifier.lockInput(); + if (!buff) + { + buff = std::make_shared(); + } + buff->poses = req->queries; + buff->eval_width = req->eval_width; + buff->eval_height = req->eval_height; + resp->query_id = buff->id = this->query_count.load(); + this->query_notifier.unlockInputAndNotify(buff); + } + this->query_count++; + } +} + +util::ResourcePipeline& MiningEvalWorker::getInput() +{ + return this->mining_eval_resources; +} + +void MiningEvalWorker::startThreads() +{ + if (!this->threads_running) + { + this->threads_running = true; + this->mining_eval_thread = + std::thread{&MiningEvalWorker::mining_eval_thread_worker, this}; + } +} + +void MiningEvalWorker::stopThreads() +{ + this->threads_running = false; + + this->query_notifier.notifyExit(); + this->mining_eval_resources.notifyExit(); + if (this->mining_eval_thread.joinable()) + { + this->mining_eval_thread.join(); + } +} + +void MiningEvalWorker::mining_eval_thread_worker() +{ + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Mining evaluation thread started successfully."); + + do + { + if (this->srv_enable_state.load()) + { + auto& buff = this->mining_eval_resources.waitNewestResource(); + if (!this->threads_running.load()) + { + break; + } + + buff.query = this->query_notifier.aquireNewestOutput(); + + PROFILING_SYNC(); + PROFILING_NOTIFY_ALWAYS(mining_eval); + this->mining_eval_callback(buff); + PROFILING_NOTIFY_ALWAYS(mining_eval); + } + else + { + this->query_notifier.waitNewestResource(); + } + } // + while (this->threads_running.load()); + + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Mining evaluation thread exited cleanly."); +} + + +void MiningEvalWorker::mining_eval_callback(MiningEvalResources& buff) +{ + auto query_ptr = std::static_pointer_cast(buff.query); + + MiningEvalResultsMsg msg; + msg.query_id = query_ptr->id; + msg.ranges.resize(query_ptr->poses.poses.size(), 0.f); + + Iso3f odom_to_ref_tf = Iso3f::Identity(); + const auto& header = query_ptr->poses.header; + if (header.frame_id != this->odom_frame) + { + try + { + odom_to_ref_tf << this->tf_buffer + .lookupTransform( + header.frame_id, + this->odom_frame, + util::toTf2TimePoint(header.stamp)) + .transform; + } + catch (const std::exception& e) + { + return; + } + } + + Box3f eval_bound; + eval_bound.min().x() = 0.f; + eval_bound.min().y() = query_ptr->eval_width * -0.5f; + eval_bound.min().z() = query_ptr->eval_height * -0.5f; + eval_bound.max().x() = std::numeric_limits::infinity(); + eval_bound.max().y() = query_ptr->eval_width * 0.5f; + eval_bound.max().z() = query_ptr->eval_height * 0.5f; + + for (size_t i = 0; i < query_ptr->poses.poses.size(); i++) + { + Iso3f pose_tf; + // (ref to pose tf)[4x4] * (odom to ref tf)[4x4] --> (odom to pose tf)[4x4] + pose_tf = + (pose_tf << query_ptr->poses.poses[i]).inverse() * odom_to_ref_tf; + + msg.ranges[i] = std::numeric_limits::infinity(); + for (const auto& pt : buff.avoid_points) + { + Vec3f pt_ = (pose_tf * pt.getVector4fMap()).template head<3>(); + + if(eval_bound.contains(pt_) && pt_.x() < msg.ranges[i]) + { + msg.ranges[i] = pt_.x(); + } + } + } + + this->pub_map.publish("mining_evaluation", msg); +} + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/mining_eval_worker.hpp b/src/core/threads/mining_eval_worker.hpp new file mode 100644 index 0000000..4f1f091 --- /dev/null +++ b/src/core/threads/mining_eval_worker.hpp @@ -0,0 +1,127 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include + +#include +#include +#include + +#include + +#include + +#include + +#include + +#include +#include + +#include "shared_resources.hpp" +#include "../perception_presets.hpp" + + +namespace csm +{ +namespace perception +{ + +class MiningEvalWorker +{ + friend class PerceptionNode; + + using RclNode = rclcpp::Node; + using Tf2Buffer = tf2_ros::Buffer; + + using PoseArrayMsg = geometry_msgs::msg::PoseArray; + + using UpdateMiningEvalSrv = cardinal_perception::srv::UpdateMiningEvalMode; + +public: + MiningEvalWorker(RclNode& node, const Tf2Buffer& tf_buffer); + ~MiningEvalWorker(); + +public: + void configure(const std::string& odom_frame); + + void accept( + const UpdateMiningEvalSrv::Request::SharedPtr& req, + const UpdateMiningEvalSrv::Response::SharedPtr& resp); + + util::ResourcePipeline& getInput(); + + void startThreads(); + void stopThreads(); + +protected: + void mining_eval_thread_worker(); + void mining_eval_callback(MiningEvalResources& buff); + +protected: + struct Query + { + using SharedPtr = std::shared_ptr; + + PoseArrayMsg poses; + float eval_width; + float eval_height; + uint32_t id; + }; + +protected: + RclNode& node; + const Tf2Buffer& tf_buffer; + util::GenericPubMap pub_map; + + std::string odom_frame; + + std::atomic threads_running{false}; + std::atomic srv_enable_state{false}; + std::atomic query_count{0}; + + util::ResourcePipeline query_notifier; + util::ResourcePipeline mining_eval_resources; + std::thread mining_eval_thread; +}; + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/path_planning_worker.cpp b/src/core/threads/path_planning_worker.cpp new file mode 100644 index 0000000..04a7396 --- /dev/null +++ b/src/core/threads/path_planning_worker.cpp @@ -0,0 +1,289 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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 "path_planning_worker.hpp" + +#include + +#include +#include + +#include + +#include +#include + +#include + +#include + +#include +#include +#include + + +using namespace util::geom::cvt::ops; + +using PathMsg = nav_msgs::msg::Path; +using PointCloudMsg = sensor_msgs::msg::PointCloud2; + + +namespace csm +{ +namespace perception +{ + +PathPlanningWorker::PathPlanningWorker( + RclNode& node, + const Tf2Buffer& tf_buffer) : + node{node}, + tf_buffer{tf_buffer}, + pub_map{node, PERCEPTION_TOPIC(""), PERCEPTION_PUBSUB_QOS} +{ +} + +PathPlanningWorker::~PathPlanningWorker() { this->stopThreads(); } + +void PathPlanningWorker::configure( + const std::string& odom_frame, + float map_obstacle_merge_window, + float passive_crop_horizontal_range, + float passive_crop_vertical_range) +{ + this->odom_frame = odom_frame; + this->passive_crop_horizontal_range = passive_crop_horizontal_range; + this->passive_crop_vertical_range = passive_crop_vertical_range; + + this->pplan_map.reconfigure(map_obstacle_merge_window); +} + +void PathPlanningWorker::accept( + const UpdatePathPlanSrv::Request::SharedPtr& req, + const UpdatePathPlanSrv::Response::SharedPtr& resp) +{ + if (req->completed) + { + this->srv_enable_state = false; + } + else + { + this->pplan_target_notifier.updateAndNotify(req->target); + this->need_clear_buffered = true; + this->srv_enable_state = true; + } + + resp->running = this->srv_enable_state; +} + +util::ResourcePipeline& PathPlanningWorker::getInput() +{ + return this->path_planning_resources; +} + +void PathPlanningWorker::startThreads() +{ + if (!this->threads_running) + { + this->threads_running = true; + this->path_planning_thread = + std::thread{&PathPlanningWorker::path_planning_thread_worker, this}; + } +} + +void PathPlanningWorker::stopThreads() +{ + this->threads_running = false; + + this->pplan_target_notifier.notifyExit(); + this->path_planning_resources.notifyExit(); + if (this->path_planning_thread.joinable()) + { + this->path_planning_thread.join(); + } +} + +void PathPlanningWorker::path_planning_thread_worker() +{ + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Path planning thread started successfully."); + + do + { + const PathPlanningResources& buff = + this->path_planning_resources.waitNewestResource(); + + if (!this->threads_running.load()) + { + break; + } + + PROFILING_SYNC(); + PROFILING_NOTIFY_ALWAYS(path_planning); + this->updateMap(buff); + + if (this->srv_enable_state.load() && this->threads_running.load()) + { + this->planPath( + buff, + this->pplan_target_notifier.aquireNewestOutput()); + } + + PROFILING_NOTIFY_ALWAYS(path_planning); + PROFILING_FLUSH_LOCAL(); + } // + while (this->threads_running.load()); + + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Path planning thread exited cleanly."); +} + + +void PathPlanningWorker::updateMap(const PathPlanningResources& buff) +{ + this->pplan_map.append(buff.points, buff.bounds_min, buff.bounds_max); + + if (!this->srv_enable_state.load() && + (this->passive_crop_horizontal_range * + this->passive_crop_vertical_range) > 0.f) + { + const Vec3f crop_range{ + this->passive_crop_horizontal_range, + this->passive_crop_horizontal_range, + this->passive_crop_vertical_range}; + this->pplan_map.crop( + buff.base_to_odom.pose.vec - crop_range, + buff.base_to_odom.pose.vec + crop_range); + } + + PointCloudMsg msg; + pcl::toROSMsg(this->pplan_map.getPoints(), msg); + msg.header.frame_id = this->odom_frame; + msg.header.stamp = util::toTimeMsg(buff.stamp); + this->pub_map.publish("pplan_map", msg); + + this->pub_map.publish( + "metrics/pplan/map_octree_size", + this->pplan_map.getMap().octreeSize()); + this->pub_map.publish( + "metrics/pplan/ue_octree_depth", + this->pplan_map.getUESpace().treeDepth()); + this->pub_map.publish( + "metrics/pplan/ue_octree_alloc", + this->pplan_map.getUESpace().allocEstimate()); +} + +void PathPlanningWorker::planPath( + const PathPlanningResources& buff, + PoseStampedMsg& target) +{ + using namespace csm::perception::traversibility; + + if (target.header.frame_id != this->odom_frame) + { + try + { + auto tf = this->tf_buffer.lookupTransform( + this->odom_frame, + target.header.frame_id, + // util::toTf2TimePoint(buff.stamp)); + tf2::timeFromSec(0)); + + // TODO: ensure tf stamp is not wildly out of date + + tf2::doTransform(target, target, tf); + } + catch (const std::exception& e) + { + RCLCPP_INFO( + this->node.get_logger(), + "[PATH PLANNING CALLBACK]: Failed to transform target pose from '%s' to '%s'\n\twhat(): %s", + target.header.frame_id.c_str(), + this->odom_frame.c_str(), + e.what()); + return; + } + } + + Vec3f odom_target; + odom_target << target.pose.position; + + if (this->need_clear_buffered) + { + this->path.clear(); + this->need_clear_buffered = false; + } + + std::vector prev_path = this->path; + for (auto max_weight = NOMINAL_MAX_WEIGHT; + isWeighted(max_weight) && !this->path_planner.solvePath( + this->path, + buff.base_to_odom.pose.vec, + odom_target, + this->pplan_map, + max_weight); + max_weight *= 2) + { + this->path = prev_path; + } + if (this->path.empty()) + { + // Fail + return; + } + + PathMsg path_msg; + path_msg.header.frame_id = this->odom_frame; + path_msg.header.stamp = util::toTimeMsg(buff.stamp); + + path_msg.poses.reserve(this->path.size()); + for (const Vec3f& kp : this->path) + { + PoseStampedMsg& pose = path_msg.poses.emplace_back(); + pose.pose.position << kp; + pose.header.frame_id = this->odom_frame; + } + + this->pub_map.publish("planned_path", path_msg); + this->pub_map.publish("pplan_target", target); +} + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/path_planning_worker.hpp b/src/core/threads/path_planning_worker.hpp new file mode 100644 index 0000000..8182c59 --- /dev/null +++ b/src/core/threads/path_planning_worker.hpp @@ -0,0 +1,132 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include + +#include +#include +#include + +#include + +#include + +#include + +#include + +#include +#include + +#include +#include + +#include "shared_resources.hpp" +#include "../perception_presets.hpp" + + +namespace csm +{ +namespace perception +{ + +class PathPlanningWorker +{ + friend class PerceptionNode; + + using RclNode = rclcpp::Node; + using Tf2Buffer = tf2_ros::Buffer; + + using PoseStampedMsg = geometry_msgs::msg::PoseStamped; + + using UpdatePathPlanSrv = cardinal_perception::srv::UpdatePathPlanningMode; + + using Vec3f = Eigen::Vector3f; + +public: + PathPlanningWorker(RclNode& node, const Tf2Buffer& tf_buffer); + ~PathPlanningWorker(); + +public: + void configure( + const std::string& odom_frame, + float map_obstacle_merge_window, + float passive_crop_horizontal_range, + float passive_crop_vertical_range); + + void accept( + const UpdatePathPlanSrv::Request::SharedPtr& req, + const UpdatePathPlanSrv::Response::SharedPtr& resp); + + util::ResourcePipeline& getInput(); + + void startThreads(); + void stopThreads(); + +protected: + void path_planning_thread_worker(); + void updateMap(const PathPlanningResources& buff); + void planPath(const PathPlanningResources& buff, PoseStampedMsg& target); + +protected: + RclNode& node; + const Tf2Buffer& tf_buffer; + util::GenericPubMap pub_map; + + std::string odom_frame; + float passive_crop_horizontal_range{0.f}; + float passive_crop_vertical_range{0.f}; + + std::atomic threads_running{false}; + std::atomic srv_enable_state{false}; + std::atomic need_clear_buffered{false}; + + std::thread path_planning_thread; + + std::vector path; + PathPlanMap pplan_map; + PathPlanner path_planner; + util::ResourcePipeline pplan_target_notifier; + util::ResourcePipeline path_planning_resources; +}; + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/shared_resources.hpp b/src/core/threads/shared_resources.hpp new file mode 100644 index 0000000..597aed0 --- /dev/null +++ b/src/core/threads/shared_resources.hpp @@ -0,0 +1,103 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include +#include + +#include + +#include + +#include +#include + +#include +#include + + +namespace csm +{ +namespace perception +{ + +struct MappingResources +{ + util::geom::PoseTf3f lidar_to_base; + util::geom::PoseTf3f base_to_odom; + sensor_msgs::msg::PointCloud2::ConstSharedPtr raw_scan; + pcl::PointCloud lidar_odom_buff; +#if PERCEPTION_USE_NULL_RAY_DELETION + std::vector null_vecs; +#endif + std::shared_ptr nan_indices; + std::shared_ptr remove_indices; +}; + +struct TraversibilityResources +{ + double stamp; + Eigen::Vector3f bounds_min; + Eigen::Vector3f bounds_max; + util::geom::PoseTf3f lidar_to_base; + util::geom::PoseTf3f base_to_odom; + pcl::PointCloud points; +}; + +struct PathPlanningResources +{ + double stamp; + Eigen::Vector3f bounds_min; + Eigen::Vector3f bounds_max; + util::geom::PoseTf3f base_to_odom; + pcl::PointCloud points; +}; + +struct MiningEvalResources +{ + double stamp; + Eigen::Vector3f bounds_min; + Eigen::Vector3f bounds_max; + pcl::PointCloud avoid_points; + std::shared_ptr query; +}; + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/traversibility_worker.cpp b/src/core/threads/traversibility_worker.cpp new file mode 100644 index 0000000..068fe96 --- /dev/null +++ b/src/core/threads/traversibility_worker.cpp @@ -0,0 +1,229 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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 "traversibility_worker.hpp" + +#include + +#include +#include + +#include + +#include + +#include + +#include +#include + + +using namespace util::geom::cvt::ops; + +using Vec3f = Eigen::Vector3f; +using Vec3d = Eigen::Vector3d; + +using PointCloudMsg = sensor_msgs::msg::PointCloud2; + + +namespace csm +{ +namespace perception +{ + +TraversibilityWorker::TraversibilityWorker( + RclNode& node, + const ImuIntegrator<>& imu_sampler) : + node{node}, + imu_sampler{imu_sampler}, + pub_map{node, PERCEPTION_TOPIC(""), PERCEPTION_PUBSUB_QOS} +{ +} + +TraversibilityWorker::~TraversibilityWorker() { this->stopThreads(); } + +void TraversibilityWorker::configure(const std::string& odom_frame) +{ + this->odom_frame = odom_frame; +} + +util::ResourcePipeline& TraversibilityWorker::getInput() +{ + return this->traversibility_resources; +} +void TraversibilityWorker::connectOutput( + util::ResourcePipeline& path_planning_resources) +{ + this->path_planning_resources = &path_planning_resources; +} +void TraversibilityWorker::connectOutput( + util::ResourcePipeline& mining_eval_resources) +{ + this->mining_eval_resources = &mining_eval_resources; +} + +void TraversibilityWorker::startThreads() +{ + if (!this->threads_running) + { + this->threads_running = true; + this->traversibility_thread = std::thread{ + &TraversibilityWorker::traversibility_thread_worker, + this}; + } +} + +void TraversibilityWorker::stopThreads() +{ + this->threads_running = false; + + this->traversibility_resources.notifyExit(); + if (this->traversibility_thread.joinable()) + { + this->traversibility_thread.join(); + } +} + +void TraversibilityWorker::traversibility_thread_worker() +{ + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Traversibility thread started successfully."); + + do + { + auto& buff = this->traversibility_resources.waitNewestResource(); + if (!this->threads_running.load()) + { + break; + } + + PROFILING_SYNC(); + PROFILING_NOTIFY_ALWAYS(traversibility); + this->traversibility_callback(buff); + PROFILING_NOTIFY_ALWAYS(traversibility); + } // + while (this->threads_running.load()); + + RCLCPP_INFO( + this->node.get_logger(), + "[CORE]: Traversibility thread exited cleanly."); +} + + +void TraversibilityWorker::traversibility_callback( + TraversibilityResources& buff) +{ + PROFILING_NOTIFY(traversibility_preproc); + + thread_local Vec3f env_grav_vec{0.f, 0.f, 1.f}; + if (this->imu_sampler.hasSamples()) + { + double stddev, delta_r; + const Vec3d grav_vec = + this->imu_sampler.estimateGravity(0.5, &stddev, &delta_r); + if (stddev < 1. && delta_r < 0.01) + { + env_grav_vec = + (buff.base_to_odom.tf * grav_vec.template cast()) + .normalized(); + } + } + + PROFILING_NOTIFY2(traversibility_preproc, traversibility_gen_proc); + + this->trav_gen.processMapPoints( + buff.points, + buff.bounds_min, + buff.bounds_max, + env_grav_vec, + buff.base_to_odom.pose.vec); + + PROFILING_NOTIFY2(traversibility_gen_proc, traversibility_export); + + if (this->mining_eval_resources) + { + auto& x = this->mining_eval_resources->lockInput(); + x.stamp = buff.stamp; + x.bounds_min = buff.bounds_min; + x.bounds_max = buff.bounds_max; + this->trav_gen.extractNonTravPoints(x.avoid_points); + this->mining_eval_resources->unlockInputAndNotify(x); + } + + pcl::PointCloud trav_points, trav_debug_cloud; + + this->trav_gen.swapPoints(trav_points); + trav_debug_cloud = trav_points; // need to copy since trav_gen gets swapped later + + if (this->path_planning_resources) + { + auto& x = this->path_planning_resources->lockInput(); + x.stamp = buff.stamp; + x.bounds_min = buff.bounds_min; + x.bounds_max = buff.bounds_max; + x.base_to_odom = buff.base_to_odom; + x.points.swap(trav_points); + // x.points_meta.points.swap(trav_meta.points); + this->path_planning_resources->unlockInputAndNotify(x); + } + + PROFILING_NOTIFY2(traversibility_export, traversibility_debpub); + + try + { + PointCloudMsg output; + pcl::toROSMsg(trav_debug_cloud, output); + output.header.stamp = util::toTimeMsg(buff.stamp); + output.header.frame_id = this->odom_frame; + this->pub_map.publish("traversibility_points", output); + } + catch (const std::exception& e) + { + RCLCPP_INFO( + this->node.get_logger(), + "[TRAVERSIBILITY]: Failed to publish debug cloud -- what():\n\t%s", + e.what()); + } + + PROFILING_NOTIFY(traversibility_debpub); +} + +}; // namespace perception +}; // namespace csm diff --git a/src/core/threads/traversibility_worker.hpp b/src/core/threads/traversibility_worker.hpp new file mode 100644 index 0000000..ae308fe --- /dev/null +++ b/src/core/threads/traversibility_worker.hpp @@ -0,0 +1,111 @@ +/******************************************************************************* +* Copyright (C) 2024-2026 Cardinal Space Mining Club * +* * +* ;xxxxxxx: * +* ;$$$$$$$$$ ...::.. * +* $$$$$$$$$$x .:::::::::::.. * +* x$$$$$$$$$$$$$$::::::::::::::::. * +* :$$$$$&X; .xX:::::::::::::.::... * +* .$$Xx++$$$$+ :::. :;: .::::::. .... : * +* :$$$$$$$$$ ;: ;xXXXXXXXx .::. .::::. .:. * +* :$$$$$$$$: ; ;xXXXXXXXXXXXXx: ..:::::: .::. * +* ;$$$$$$$$ :: :;XXXXXXXXXXXXXXXXXX+ .::::. .::: * +* X$$$$$X : +XXXXXXXXXXXXXXXXXXXXXXXX; .:: .::::. * +* .$$$$ :xXXXXXXXXXXXXXXXXXXXXXXXXXXX. .:::::. * +* X$$X XXXXXXXXXXXXXXXXXXXXXXXXXXXXx: .::::. * +* $$$:.XXXXXXXXXXXXXXXXXXXXXXXXXXX ;; ..:. * +* $$& :XXXXXXXXXXXXXXXXXXXXXXXX; +XX; X$$; * +* $$$: XXXXXXXXXXXXXXXXXXXXXX; :XXXXX; X$$; * +* X$$X XXXXXXXXXXXXXXXXXXX; .+XXXXXXX; $$$ * +* $$$$ ;XXXXXXXXXXXXXXX+ +XXXXXXXXx+ X$$$+ * +* x$$$$$X ;XXXXXXXXXXX+ :xXXXXXXXX+ .;$$$$$$ * +* +$$$$$$$$ ;XXXXXXx;;+XXXXXXXXX+ : +$$$$$$$$ * +* +$$$$$$$$: xXXXXXXXXXXXXXX+ ; X$$$$$$$$ * +* :$$$$$$$$$. +XXXXXXXXX; ;: x$$$$$$$$$ * +* ;x$$$$XX$$$$+ .;+X+ :;: :$$$$$xX$$$X * +* ;;;;;;;;;;X$$$$$$$+ :X$$$$$$&. * +* ;;;;;;;:;;;;;x$$$$$$$$$$$$$$$$x. * +* :;;;;;;;;;;;;. :$$$$$$$$$$X * +* .;;;;;;;;:;; +$$$$$$$$$ * +* .;;;;;;. X$$$$$$$: * +* * +* 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. * +* * +*******************************************************************************/ + +#pragma once + +#include + +#include +#include +#include + +#include + +#include + +#include +#include + +#include + +#include "shared_resources.hpp" +#include "../perception_presets.hpp" + + +namespace csm +{ +namespace perception +{ + +class TraversibilityWorker +{ + friend class PerceptionNode; + + using RclNode = rclcpp::Node; + +public: + TraversibilityWorker(RclNode& node, const ImuIntegrator<>& imu_sampler); + ~TraversibilityWorker(); + +public: + void configure(const std::string& odom_frame); + + util::ResourcePipeline& getInput(); + void connectOutput( + util::ResourcePipeline& path_planning_resources); + void connectOutput( + util::ResourcePipeline& mining_eval_resources); + + void startThreads(); + void stopThreads(); + +protected: + void traversibility_thread_worker(); + void traversibility_callback(TraversibilityResources& buff); + +protected: + RclNode& node; + const ImuIntegrator<>& imu_sampler; + util::GenericPubMap pub_map; + + std::string odom_frame; + + std::atomic threads_running{false}; + + TraversibilityGenerator + trav_gen; + util::ResourcePipeline traversibility_resources; + util::ResourcePipeline* path_planning_resources{ + nullptr}; + util::ResourcePipeline* mining_eval_resources{nullptr}; + std::thread traversibility_thread; +}; + +}; // namespace perception +}; // namespace csm diff --git a/src/perception_node.cpp b/src/perception_node.cpp index f394845..c1e76a4 100644 --- a/src/perception_node.cpp +++ b/src/perception_node.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * diff --git a/src/pplan_client_node.cpp b/src/pplan_client_node.cpp index 8ab18d9..25d2453 100644 --- a/src/pplan_client_node.cpp +++ b/src/pplan_client_node.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * diff --git a/src/tag_detection_node.cpp b/src/tag_detection_node.cpp index b51881c..7bb9495 100644 --- a/src/tag_detection_node.cpp +++ b/src/tag_detection_node.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * diff --git a/src/todo.txt b/src/todo.txt index d29c7ca..d1cc3fd 100644 --- a/src/todo.txt +++ b/src/todo.txt @@ -12,8 +12,6 @@ ODOMETRY: KFC MAP: 1. Frustum radius/radial distance scale inversely proportionally to base_link velocity |-> A. Sparse point removal of ghost points if this doesn't work -2. More parallelization -3. Range limits (octree leaf reassignment?) TESTING: 1. Find optimal threading for nanogicp diff --git a/srv/UpdateMiningEvalMode.srv b/srv/UpdateMiningEvalMode.srv index 977bd9f..fd45ba4 100644 --- a/srv/UpdateMiningEvalMode.srv +++ b/srv/UpdateMiningEvalMode.srv @@ -1,4 +1,6 @@ geometry_msgs/PoseArray queries +float32 eval_width +float32 eval_height bool completed --- int32 query_id