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/cmake/config.hpp.in b/cmake/config.hpp.in index ab4f840..96d74b4 100644 --- a/cmake/config.hpp.in +++ b/cmake/config.hpp.in @@ -48,15 +48,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..c1403f5 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,38 @@ }, "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, + "lambda_distance": 1.0, + "lambda_penalty": 1.0, + "max_neighbors": 11 } }, "sim": 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/accumulator_map.hpp b/include/modules/accumulator_map.hpp new file mode 100644 index 0000000..06256eb --- /dev/null +++ b/include/modules/accumulator_map.hpp @@ -0,0 +1,80 @@ +// /******************************************************************************* +// * 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 +// { + +// template +// class AccumulatorMap +// { +// using PointCloudT = pcl::PointCloud; + +// using Vec3f = Eigen::Vector3f; + +// public: +// AccumulatorMap(double voxel_size = 0.1); +// ~AccumulatorMap() = default; + +// public: +// void clear(); +// void append( +// const PointCloudT& pts, +// const Vec3f& bound_min, +// const Vec3f& bound_max); + +// protected: +// MapT map_octree; +// }; + +// }; // namespace perception +// }; // namespace csm diff --git a/include/modules/impl/kfc_map_impl.hpp b/include/modules/impl/kfc_map_impl.hpp index 978b795..e68a9fa 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, @@ -72,8 +72,8 @@ void KFCMap::applyParams( } } -template -void KFCMap::setBounds( +template +void KFCMap::setBounds( const Vec3f& min, const Vec3f& max) { @@ -85,10 +85,10 @@ void KFCMap::setBounds( this->map_octree.crop(min, max, true); } -template +template template -typename KFCMap::UpdateResult - KFCMap::updateMap( +typename KFCMap::UpdateResult + KFCMap::updateMap( const Vec3f& origin, const pcl::PointCloud& pts, const std::vector* inf_rays, 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 31ffb16..4a0a534 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,9 +51,9 @@ #include -#include - -#include +#include +#include +#include using namespace util::geom::cvt::ops; @@ -453,7 +454,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) { @@ -518,13 +519,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., @@ -532,7 +533,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); diff --git a/include/modules/impl/map_octree_impl.hpp b/include/modules/impl/map_octree_impl.hpp index 43f9844..8170d3d 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)); } @@ -380,11 +380,11 @@ void MapOctree::optimizeStorage() if (this->hole_indices.size() >= this->cloud_buff->size()) { this->cloud_buff->clear(); - if constexpr(HAS_POINT_STAMPS) + if constexpr (HAS_POINT_STAMPS) { this->pt_stamps.clear(); } - if constexpr(HAS_POINT_NORMALS) + if constexpr (HAS_POINT_NORMALS) { this->pt_normals.clear(); } @@ -431,11 +431,11 @@ void MapOctree::optimizeStorage() { { (*this->cloud_buff)[idx] = (*this->cloud_buff)[end_idx]; - if constexpr(HAS_POINT_STAMPS) + 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]; } @@ -455,11 +455,11 @@ void MapOctree::optimizeStorage() // std::cout << "exhibit h" << std::endl; this->cloud_buff->resize(end_idx + 1); - if constexpr(HAS_POINT_STAMPS) + 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); } @@ -480,7 +480,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); @@ -512,7 +512,7 @@ bool MapOctree::mergePointFields( 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 +526,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; } diff --git a/include/modules/impl/path_planner_impl.hpp b/include/modules/impl/path_planner_impl.hpp index 32cb785..82f7c7d 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,30 +58,32 @@ #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) : +template +PathPlanner

::Node::Node(const P& point, float h, Node* p) : trav_point(point), - cost(meta.trav_weight()), + cost(weight(point)), g(std::numeric_limits::infinity()), 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, @@ -96,14 +99,13 @@ void PathPlanner::setParameters( this->max_neighbors = max_neighbors; } -template -bool PathPlanner::solvePath( +template +bool PathPlanner

::solvePath( const Vec3f& start, const Vec3f& goal, const Vec3f& local_bound_min, const Vec3f& local_bound_max, - const PointCloudT& loc_cloud, - const MetaCloudT& meta_cloud, + const PointCloudT& trav_points, std::vector& path) { #if PATH_PLANNING_PEDANTIC @@ -112,14 +114,9 @@ bool PathPlanner::solvePath( { throw std::out_of_range("Start point is out of bounds"); } - if (loc_cloud.points.size() != meta_cloud.points.size()) - { - throw std::invalid_argument( - "Location and meta clouds must have the same size"); - } - if (loc_cloud.points.empty() || meta_cloud.points.empty()) + if (trav_points.points.empty()) { - throw std::invalid_argument("Point clouds are empty"); + throw std::invalid_argument("Input point cloud are empty"); } #endif @@ -127,8 +124,8 @@ bool PathPlanner::solvePath( pcl::Indices tmp_indices; std::vector tmp_dists; - auto shared_loc_cloud = util::wrap_unmanaged(loc_cloud); - this->kdtree.setInputCloud(shared_loc_cloud); + auto shared_trav_points = util::wrapUnmanaged(trav_points); + this->kdtree.setInputCloud(shared_trav_points); PointT goal_pt; goal_pt.getVector3fMap() = goal; @@ -145,7 +142,7 @@ bool PathPlanner::solvePath( { if (this->kdtree.nearestKSearch(goal_pt, 1, tmp_indices, tmp_dists)) { - goal_pt = loc_cloud.points[tmp_indices[0]]; + goal_pt = trav_points.points[tmp_indices[0]]; if (std::sqrt(tmp_dists[0]) > this->search_radius) { DEBUG_COUT( @@ -165,9 +162,9 @@ bool PathPlanner::solvePath( bool all_invalid = true; for(pcl::index_t i : tmp_indices) { - if(meta_cloud.points[i].trav_weight() < 1.f) + if(isNominal(trav_points.points[i])) { - goal_pt = loc_cloud.points[i]; + goal_pt = trav_points.points[i]; all_invalid = false; break; } @@ -179,16 +176,15 @@ bool PathPlanner::solvePath( } this->nodes.clear(); - this->nodes.reserve(loc_cloud.points.size()); + this->nodes.reserve(trav_points.points.size()); // Heuristic = lambda_d * Euclidean distance - for (size_t i = 0; i < loc_cloud.points.size(); ++i) + for (size_t i = 0; i < trav_points.points.size(); ++i) { - const auto& point = loc_cloud.points[i]; - const auto& meta = meta_cloud.points[i]; + const auto& point = trav_points.points[i]; float h = lambda_dist * (goal_pt.getVector3fMap() - point.getVector3fMap()).norm(); - this->nodes.emplace_back(point, meta, h); + this->nodes.emplace_back(point, h); } // find start node @@ -209,7 +205,7 @@ bool PathPlanner::solvePath( } // create open heap over f = g + h - DaryHeap open(static_cast(this->nodes.size())); + util::DAryHeap open(static_cast(this->nodes.size())); open.reserve_heap(this->nodes.size()); open.push(start_idx, this->nodes[start_idx].f()); diff --git a/include/modules/impl/traversibility_gen_impl.hpp b/include/modules/impl/traversibility_gen_impl.hpp index 5404e64..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: * * ;$$$$$$$$$ ...::.. * @@ -46,6 +46,9 @@ #include +#include +#include + namespace csm { @@ -53,8 +56,8 @@ namespace perception { -template -void TraversibilityGenerator::configure( +template +void TraversibilityGenerator::configure( float normal_estimation_radius, float output_res, float grad_search_radius, @@ -63,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; @@ -75,12 +79,13 @@ void TraversibilityGenerator::configure( 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, @@ -96,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; @@ -106,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); @@ -131,112 +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::extractTravPoints( +template +void TraversibilityGenerator::extractTravPoints( TravPointCloud& trav_points) const { trav_points.clear(); this->mtx.lock(); - util::pc_copy_selection(this->points, this->trav_selection, trav_points); + util::copySelection(this->points, this->trav_selection, trav_points); this->mtx.unlock(); } -template -void TraversibilityGenerator::extractTravElements( - TravPointCloud& trav_points, - MetaDataList& trav_meta_data) 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); - this->mtx.unlock(); -} - -template -void TraversibilityGenerator::extractExtTravPoints( +template +void TraversibilityGenerator::extractExtTravPoints( TravPointCloud& ext_trav_points) const { ext_trav_points.clear(); this->mtx.lock(); - util::pc_copy_selection( + util::copySelection( this->points, this->ext_trav_selection, ext_trav_points); this->mtx.unlock(); } -template -void TraversibilityGenerator::extractExtTravElements( - TravPointCloud& ext_trav_points, - MetaDataList& ext_trav_meta_data) const -{ - ext_trav_points.clear(); - ext_trav_meta_data.clear(); - this->mtx.lock(); - util::pc_copy_selection( - 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::extractNonTravPoints( +template +void TraversibilityGenerator::extractNonTravPoints( TravPointCloud& non_trav_points) const { non_trav_points.clear(); this->mtx.lock(); - util::pc_copy_selection( - this->points, - this->avoid_selection, - non_trav_points); - this->mtx.unlock(); -} -template -void TraversibilityGenerator::extractNonTravElements( - TravPointCloud& non_trav_points, - MetaDataList& non_trav_meta_data) 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(); @@ -245,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(); @@ -255,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(); @@ -268,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(); @@ -330,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, @@ -379,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 @@ -410,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) @@ -430,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; } @@ -444,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++; @@ -458,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) @@ -478,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; } } } @@ -493,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(); @@ -512,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 98% rename from include/imu_integrator.hpp rename to include/modules/imu_integrator.hpp index d907c82..4246f8e 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,9 +47,9 @@ #include -#include "tsq.hpp" -#include "util.hpp" -#include "geometry.hpp" +#include +#include +#include namespace csm @@ -57,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 { diff --git a/include/modules/kfc_map.hpp b/include/modules/kfc_map.hpp index edcd095..4bb2f9b 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,7 +49,6 @@ #include #include -#include "point_def.hpp" #include "map_octree.hpp" #ifndef KFC_MAP_STORE_INSTANCE_BUFFERS @@ -91,15 +90,13 @@ enum /** KDTree Frustum Collision (KFC) mapping implementation */ template< typename PointT, - typename MapT = MapOctree, - typename CollisionPointT = pcl::PointXYZLNormal> + typename MapT = MapOctree> class KFCMap { + static_assert(pcl::traits::has_xyz::value); static_assert(MapT::HAS_POINT_NORMALS); - static_assert( - pcl::traits::has_normal::value && - pcl::traits::has_curvature::value && - pcl::traits::has_label::value); + + using CollisionPointT = pcl::PointXYZLNormal; using Arr3f = Eigen::Array3f; using Vec3f = Eigen::Vector3f; @@ -250,32 +247,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 c5e6710..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 diff --git a/include/modules/lidar_odom.hpp b/include/modules/lidar_odom.hpp index df22922..d4597bd 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; diff --git a/include/modules/map_octree.hpp b/include/modules/map_octree.hpp index 387d6c6..945c31b 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 @@ -106,8 +106,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; diff --git a/include/modules/path_planner.hpp b/include/modules/path_planner.hpp index 6a3fceb..d6ff09f 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,8 +47,8 @@ #include #include -#include "util.hpp" -#include "point_def.hpp" +#include + #ifndef PATH_PLANNING_PEDANTIC #define PATH_PLANNING_PEDANTIC 0 @@ -62,18 +62,16 @@ 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 Vec3f = Eigen::Vector3f; using Box3f = Eigen::AlignedBox3f; @@ -90,7 +88,6 @@ class PathPlanner Node( const PointT& point, - const MetaPointT& meta, float h = 0.0f, Node* p = nullptr); @@ -115,8 +112,7 @@ class PathPlanner const Vec3f& goal, const Vec3f& local_bound_min, const Vec3f& local_bound_max, - const PointCloudT& loc_cloud, - const MetaCloudT& meta_cloud, + const PointCloudT& trav_points, std::vector& path); private: @@ -148,15 +144,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 97% rename from include/scan_preprocessor.hpp rename to include/modules/scan_preprocessor.hpp index 414fe43..dbe8633 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,13 @@ #include #include -#include "tsq.hpp" -#include "util.hpp" -#include "geometry.hpp" -#include "cloud_ops.hpp" -#include "point_def.hpp" +#include +#include +#include +#include +#include +#include + #include "imu_integrator.hpp" #define DESKEW_ROT_THRESH_RAD 1e-3 @@ -417,7 +419,7 @@ int ScanPreprocessor::filterPoints(const PointCloudMsg& scan) 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; @@ -546,7 +548,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, @@ -555,7 +557,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 98% rename from include/selection_octree.hpp rename to include/modules/selection_octree.hpp index 8f68001..d17e214 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, * 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 98% rename from include/transform_sync.hpp rename to include/modules/transform_sync.hpp index 7f04b33..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: * * ;$$$$$$$$$ ...::.. * @@ -49,8 +49,9 @@ #include #include -#include "util.hpp" -#include "geometry.hpp" +#include +#include + #include "trajectory_filter.hpp" #ifndef TRANSFORM_SYNC_PRINT_DEBUG @@ -460,7 +461,7 @@ void TransformSynchronizer::publishMap() geometry_msgs::msg::TransformStamped tf_; - tf_.header.stamp = util::toTimeStamp(this->map_stamp); + 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; @@ -479,7 +480,7 @@ void TransformSynchronizer::publishOdom() geometry_msgs::msg::TransformStamped tf_; - tf_.header.stamp = util::toTimeStamp(this->odom_stamp); + 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; diff --git a/include/modules/traversibility_gen.hpp b/include/modules/traversibility_gen.hpp index 0d9c067..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,27 +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 extractTravPoints(TravPointCloud& trav_points) const; - void extractTravElements( - TravPointCloud& trav_points, - MetaDataList& trav_meta_data) const; void extractExtTravPoints(TravPointCloud& ext_trav_points) const; - void extractExtTravElements( - TravPointCloud& ext_trav_points, - MetaDataList& ext_trav_meta_data) const; void extractNonTravPoints(TravPointCloud& non_trav_points) const; - void extractNonTravElements( - TravPointCloud& non_trav_points, - MetaDataList& non_trav_meta_data) const; /* WARNING : This can only be done once per iteration to extract the * processed result! */ @@ -158,46 +134,21 @@ 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 @@ -216,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}; // }; @@ -231,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..c972a34 --- /dev/null +++ b/include/modules/ue_octree.hpp @@ -0,0 +1,397 @@ +/******************************************************************************* +* 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 +{ + +template +class UEOctree +{ + 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() = default; + +public: + void addExploredSpace(const Vec3f& min, const Vec3f& max); + void clear(); + bool isExplored(const Vec3f& pt); + +protected: + struct Node + { + Node* children{nullptr}; + bool explored{false}; + + Node() = default; + inline ~Node() { this->clear(); } + + void init(); + void clear(); + + 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(); + } + }; + +protected: + 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; + +protected: + bool adjustBounds(const Vec3f& min, const Vec3f& max); + void recursiveExplore( + 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}; +}; + + + +// --- Implementation ---------------------------------------------------------- + +template +UEOctree::UEOctree(FloatT res) : 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::clear() +{ + this->root.clear(); + this->root_height = 0; + this->vox_span = 0; +} + +template +bool UEOctree::isExplored(const Vec3f& pt) +{ + 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(); + + 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 +void UEOctree::Node::init() +{ + if (this->isNull()) + { + this->children = new Node[8]; + } +} +template +void UEOctree::Node::clear() +{ + if (this->children) + { + delete[] this->children; + this->children = nullptr; + } +} + +template +UEOctree::Node& UEOctree::Node::operator[](size_t i) +{ + // assert(i < 8 && this->children); + return this->children[i]; +} +template +const UEOctree::Node& UEOctree::Node::operator[](size_t i) const +{ + // assert(i < 8 && this->children); + return this->children[i]; +} + + +template +UEOctree::Vec3i UEOctree::getDescriptorKey(const NodeDescriptor& n) +{ + return n.template head<3>(); +} +template +UEOctree::Vec3f UEOctree::getDescriptorKeyF(const NodeDescriptor& n) +{ + return n.template head<3>().template cast(); +} +template +UEOctree::Vec3i UEOctree::getDescriptorSpan3( + const NodeDescriptor& n) +{ + return Vec3i::Constant(n[3]); +} +template +UEOctree::Vec3f UEOctree::getDescriptorSpan3f( + const NodeDescriptor& n) +{ + return Vec3f::Constant(static_cast(n[3])); +} + +template +UEOctree::NodeDescriptor UEOctree::getRootDescriptor() const +{ + return NodeDescriptor{0, 0, 0, 0x1 << this->root_height}; +} + +template +UEOctree::NodeDescriptor UEOctree::getChildDescriptor( + const NodeDescriptor& n, + size_t i) const +{ + // assert(i < 8); + return NodeDescriptor{ + n[0] + (n[3] / 2) * (i >> 2 & 0x1), + n[1] + (n[3] / 2) * (i >> 1 & 0x1), + n[2] + (n[3] / 2) * (i >> 0 & 0x1), + n[3] / 2}; +} + +template +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 +bool UEOctree::adjustBounds(const Vec3f& min, const Vec3f& max) +{ + if (!this->root.anyExplored()) + { + this->origin = min; + 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; + } + + uint8_t x_bit = min.x() < this->origin.x() ? 0x1 : 0x0; + uint8_t y_bit = min.y() < this->origin.y() ? 0x1 : 0x0; + uint8_t z_bit = min.z() < this->origin.z() ? 0x1 : 0x0; + this->origin -= Vec3f{x_bit, y_bit, z_bit} * this->vox_res * + (0x1 << this->root_height); + this->vox_span *= 2; + this->root_height++; + if (this->root.anyExplored()) + { + size_t octant = (x_bit << 2) | (y_bit << 1) | (z_bit << 0); + Node* tmp = new Node[8]; + 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 = new Node[8]; + 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) +{ + node.init(); + 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)) + { + child.explored = true; + child.clear(); + } + else if (zone.intersects(child_span)) + { + if (child_desc[3] > 1) + { + this->recursiveExplore(child, child_desc, zone); + } + else + { + child.explored = true; + child.clear(); + } + } + + all_explored &= child.fullyExplored(); + } + + if (all_explored) + { + node.explored = true; + node.clear(); + } +} + +}; // 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 596313a..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: * * ;$$$$$$$$$ ...::.. * @@ -91,66 +91,6 @@ 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; -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpedantic" - union - { - struct - { - uint32_t tl, th; - }; - uint64_t t; - }; -#pragma GCC diagnostic pop - - inline uint64_t integer_time() const { return this->t; } - static inline uint64_t time_base() { return 1000000; } -}; - struct EIGEN_ALIGN8 PointSDir { float azimuth; @@ -178,40 +118,6 @@ struct EIGEN_ALIGN8 PointT_32HL 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 -}; - }; // namespace perception }; // namespace csm @@ -223,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) @@ -249,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 @@ -264,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> { }; @@ -288,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..33a3caa --- /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. + * 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 80% rename from include/cloud_ops.hpp rename to include/util/cloud_ops.hpp index a36b68e..395a7e7 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,11 +301,11 @@ template< typename PointT = pcl::PointXYZ, typename IntT = pcl::index_t, bool UseNegative = false> -void cropbox_filter( +void cropFilter( 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 Eigen::Vector3f min_pt, + const Eigen::Vector3f max_pt, const std::vector* selection = nullptr) { ASSERT_POINT_HAS_XYZ(PointT) @@ -385,75 +330,14 @@ void cropbox_filter( 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( - const pcl::PointCloud& cloud, - std::vector& filtered, - const FloatT min_z = -1.f, - const FloatT max_z = 1.f, - const std::vector* selection = nullptr) -{ - ASSERT_POINT_HAS_XYZ(PointT) - ASSERT_FLOATING_POINT(FloatT) - - 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]; + 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) @@ -1036,7 +919,7 @@ template< 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) { @@ -1052,11 +935,11 @@ void pc_remove_selection( } 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,7 +952,7 @@ 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) { @@ -1083,11 +966,11 @@ inline void pc_normalize_selection( } 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,7 +982,7 @@ 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) @@ -1114,12 +997,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,7 +1014,7 @@ 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) @@ -1139,29 +1022,29 @@ void pc_copy_inverse_selection( 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 99% rename from include/pub_map.hpp rename to include/util/pub_map.hpp index 30a0cb4..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,7 +62,7 @@ #include #include -#include "util.hpp" +#include "std_utils.hpp" namespace util 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/accumulator_map.hpp b/include/util/std_utils.hpp similarity index 69% rename from include/accumulator_map.hpp rename to include/util/std_utils.hpp index 08e740b..f1582e4 100644 --- a/include/accumulator_map.hpp +++ b/include/util/std_utils.hpp @@ -1,80 +1,89 @@ -/******************************************************************************* -* 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 - - -namespace csm -{ -namespace perception -{ - -template -class AccumulatorMap -{ - using PointCloudT = pcl::PointCloud; - - using Vec3f = Eigen::Vector3f; - -public: - AccumulatorMap(double voxel_size = 0.1); - ~AccumulatorMap() = default; - -public: - void clear(); - void append( - const PointCloudT& pts, - const Vec3f& bound_min, - const Vec3f& bound_max); - -protected: - MapT map_octree; -}; - -}; // namespace perception -}; // namespace csm +/******************************************************************************* +* 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 98% rename from include/synchronization.hpp rename to include/util/synchronization.hpp index a995397..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" * @@ -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 62% rename from include/util.hpp rename to include/util/time_cvt.hpp index c2b682c..25386fd 100644 --- a/include/util.hpp +++ b/include/util/time_cvt.hpp @@ -1,189 +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 -#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)); -} - - -template -inline ros_T to_ros_val(primitive_T v) -{ - static_assert(std::is_same::value); - - return ros_T{}.set__data(v); -} - - -/* 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*) {}); -} - -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; +/******************************************************************************* +* 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/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..7375185 100644 --- a/src/core/modules/map_octree.cpp +++ b/src/core/modules/map_octree.cpp @@ -12,6 +12,4 @@ using namespace csm::perception; MAP_OCTREE_INSTANTIATE_CLASS_TEMPLATE(MappingPointType, MAP_OCTREE_STORE_NORMALS) - -// MAP_OCTREE_INSTANTIATE_PCL_DEPENDENCIES(...) // <-- use if template types are non-pcl (ie. core classes need to be compiled) - +MAP_OCTREE_INSTANTIATE_PCL_DEPENDENCIES(MappingPointType) 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 be8076e..04df310 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: * * ;$$$$$$$$$ ...::.. * @@ -56,8 +56,8 @@ #include #include -#include -#include +#include +#include #include "threads/imu_worker.hpp" #include "threads/mapping_worker.hpp" @@ -74,6 +74,9 @@ namespace csm namespace perception { +using namespace util::ros_aliases; + + struct PerceptionConfig; class PerceptionNode : public rclcpp::Node @@ -115,16 +118,15 @@ class PerceptionNode : public rclcpp::Node 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 alignment_state_service; - rclcpp::Service::SharedPtr path_plan_service; - rclcpp::Service::SharedPtr mining_eval_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; diff --git a/src/core/perception_core.cpp b/src/core/perception_core.cpp index ed94126..8ba7178 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,7 +87,7 @@ 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 { @@ -149,6 +152,7 @@ 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 @@ -180,8 +184,9 @@ struct PerceptionConfig const PerceptionConfig& config); }; -std::ostream& operator<<(std::ostream& os, const PerceptionConfig& config) { -// alignment, feat. ChatGPT lol +std::ostream& operator<<(std::ostream& os, const PerceptionConfig& config) +{ + // alignment, feat. ChatGPT lol constexpr int CONFIG_ALIGN_WIDTH = 25; auto align = [](const char* label) { @@ -195,53 +200,52 @@ std::ostream& operator<<(std::ostream& os, const PerceptionConfig& config) { os << std::setprecision(3); os << "\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") << 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"; + " +-- 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") << 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"; + << 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 @@ -249,95 +253,95 @@ std::ostream& operator<<(std::ostream& os, const PerceptionConfig& config) { 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 + << " +- 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 + " +- 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 + " +- 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("Point Samples") << config.trav_interp_point_samples << "\n"; - #endif - - #if PATH_PLANNING_ENABLED + " +- 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("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 + " +- 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 os << " +\n"; return os; @@ -654,6 +658,11 @@ void PerceptionNode::getParams(PerceptionConfig& config) "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", @@ -678,6 +687,7 @@ void PerceptionNode::getParams(PerceptionConfig& config) 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 diff --git a/src/core/perception_presets.hpp b/src/core/perception_presets.hpp index ec4b244..3338dfd 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: * * ;$$$$$$$$$ ...::.. * diff --git a/src/core/tag_detection.cpp b/src/core/tag_detection.cpp index 120c264..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; 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 index bad21ea..1130516 100644 --- a/src/core/threads/imu_worker.cpp +++ b/src/core/threads/imu_worker.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -45,10 +45,11 @@ #include -#include -#include #include +#include +#include + using namespace util::geom::cvt::ops; diff --git a/src/core/threads/imu_worker.hpp b/src/core/threads/imu_worker.hpp index 9a722ce..41838c1 100644 --- a/src/core/threads/imu_worker.hpp +++ b/src/core/threads/imu_worker.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,9 @@ #include -#include -#include +#include + +#include #include "../perception_presets.hpp" diff --git a/src/core/threads/localization_worker.cpp b/src/core/threads/localization_worker.cpp index f6b361d..96be4b4 100644 --- a/src/core/threads/localization_worker.cpp +++ b/src/core/threads/localization_worker.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -57,9 +57,9 @@ #include -#include -#include -#include +#include +#include +#include using namespace util::geom::cvt::ops; @@ -158,7 +158,7 @@ bool LocalizationWorker::setGlobalAlignmentEnabled(bool enable) } void LocalizationWorker::connectOutput( - ResourcePipeline& mapping_resources) + util::ResourcePipeline& mapping_resources) { this->mapping_resources = &mapping_resources; } @@ -446,7 +446,7 @@ void LocalizationWorker::fiducial_callback(FiducialResources& buff) pcl::PointCloud reflector_points; pcl::fromROSMsg(*buff.raw_scan, reflector_points); - util::pc_remove_selection(reflector_points, *buff.remove_indices); + 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(); @@ -512,20 +512,20 @@ void LocalizationWorker::fiducial_callback(FiducialResources& buff) #if PERCEPTION_PUBLISH_LFD_DEBUG > 0 try { - PoseStampedMsg _p; - PointCloudMsg _pc; + 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(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); + pcl::toROSMsg(redetect_cloud, pc_); + pc_.header = buff.raw_scan->header; + this->pub_map.publish("fiducial_redetect_points", pc_); // if (result.has_point_num) // { @@ -538,40 +538,40 @@ void LocalizationWorker::fiducial_callback(FiducialResources& buff) 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( + 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); + this->pub_map.publish(topic, p_); if (i < result.iterations) { - pcl::toROSMsg(seg_clouds[i], _pc); + pcl::toROSMsg(seg_clouds[i], pc_); } else { pcl::PointCloud empty_cloud; - pcl::toROSMsg(empty_cloud, _pc); + pcl::toROSMsg(empty_cloud, pc_); } - _pc.header = buff.raw_scan->header; + pc_.header = buff.raw_scan->header; topic = ((std::ostringstream{} << "fiducial_plane_" << i << "/points") .str()); - this->pub_map.publish(topic, _pc); + this->pub_map.publish(topic, pc_); } if (result.iterations == 3 && !remaining_points.empty()) { - pcl::toROSMsg(remaining_points, _pc); - _pc.header = buff.raw_scan->header; + pcl::toROSMsg(remaining_points, pc_); + pc_.header = buff.raw_scan->header; - this->pub_map.publish("fiducial_unmodeled_points", _pc); + this->pub_map.publish("fiducial_unmodeled_points", pc_); } // } } diff --git a/src/core/threads/localization_worker.hpp b/src/core/threads/localization_worker.hpp index 1fd2460..0759a4a 100644 --- a/src/core/threads/localization_worker.hpp +++ b/src/core/threads/localization_worker.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -56,12 +56,12 @@ #include #include +#include +#include +#include -#include -#include -#include -#include -#include +#include +#include #include "shared_resources.hpp" #include "../perception_presets.hpp" @@ -102,7 +102,8 @@ class LocalizationWorker #endif bool setGlobalAlignmentEnabled(bool enable); - void connectOutput(ResourcePipeline& mapping_resources); + void connectOutput( + util::ResourcePipeline& mapping_resources); void startThreads(); void stopThreads(); @@ -170,13 +171,13 @@ class LocalizationWorker TransformSynchronizer transform_sync; #endif - ResourcePipeline odometry_resources; + util::ResourcePipeline odometry_resources; std::thread odometry_thread; #if LFD_ENABLED - ResourcePipeline fiducial_resources; + util::ResourcePipeline fiducial_resources; std::thread fiducial_thread; #endif - ResourcePipeline* mapping_resources{nullptr}; + util::ResourcePipeline* mapping_resources{nullptr}; }; }; // namespace perception diff --git a/src/core/threads/mapping_worker.cpp b/src/core/threads/mapping_worker.cpp index 4cdefc0..e49347d 100644 --- a/src/core/threads/mapping_worker.cpp +++ b/src/core/threads/mapping_worker.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -50,9 +50,9 @@ #include -#include -#include -#include +#include +#include +#include using namespace util::geom::cvt::ops; @@ -89,12 +89,12 @@ void MappingWorker::configure( this->map_export_vertical_range = map_export_vertical_range; } -ResourcePipeline& MappingWorker::getInput() +util::ResourcePipeline& MappingWorker::getInput() { return this->mapping_resources; } void MappingWorker::connectOutput( - ResourcePipeline& traversibility_resources) + util::ResourcePipeline& traversibility_resources) { this->traversibility_resources = &traversibility_resources; } @@ -175,7 +175,7 @@ void MappingWorker::mapping_callback(MappingResources& buff) 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); + util::removeSelection(map_input_cloud, *buff.remove_indices); pcl::transformPointCloud( map_input_cloud, map_input_cloud, @@ -244,7 +244,7 @@ void MappingWorker::mapping_callback(MappingResources& buff) x.bounds_max = search_max; x.lidar_to_base = buff.lidar_to_base; x.base_to_odom = buff.base_to_odom; - util::pc_copy_selection( + util::copySelection( *this->sparse_map.getPoints(), export_points, x.points); @@ -256,7 +256,7 @@ void MappingWorker::mapping_callback(MappingResources& buff) try { thread_local pcl::PointCloud trav_points; - util::pc_copy_selection( + util::copySelection( *this->sparse_map.getPoints(), export_points, trav_points); @@ -264,7 +264,7 @@ void MappingWorker::mapping_callback(MappingResources& buff) PointCloudMsg trav_points_output; pcl::toROSMsg(trav_points, trav_points_output); trav_points_output.header.stamp = - util::toTimeStamp(buff.raw_scan->header.stamp); + util::toTimeMsg(buff.raw_scan->header.stamp); trav_points_output.header.frame_id = this->odom_frame; this->pub_map.publish( "traversibility_points", diff --git a/src/core/threads/mapping_worker.hpp b/src/core/threads/mapping_worker.hpp index 0f073cb..cd7a6d1 100644 --- a/src/core/threads/mapping_worker.hpp +++ b/src/core/threads/mapping_worker.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -50,8 +50,8 @@ #include #include -#include -#include +#include +#include #include "shared_resources.hpp" #include "../perception_presets.hpp" @@ -68,11 +68,9 @@ class MappingWorker using RclNode = rclcpp::Node; - template - using SparseMap = KFCMap< - PointT, - MapOctree, - CollisionPointT>; + template + using SparseMap = + KFCMap>; public: MappingWorker(RclNode& node); @@ -86,9 +84,10 @@ class MappingWorker double map_export_horizontal_range, double map_export_vertical_range); - ResourcePipeline& getInput(); + util::ResourcePipeline& getInput(); void connectOutput( - ResourcePipeline& traversibility_resources); + util::ResourcePipeline& + traversibility_resources); void startThreads(); void stopThreads(); @@ -109,9 +108,9 @@ class MappingWorker std::atomic threads_running{false}; - SparseMap sparse_map; - ResourcePipeline mapping_resources; - ResourcePipeline* traversibility_resources{ + SparseMap sparse_map; + util::ResourcePipeline mapping_resources; + util::ResourcePipeline* traversibility_resources{ nullptr}; std::thread mapping_thread; }; diff --git a/src/core/threads/mining_eval_worker.cpp b/src/core/threads/mining_eval_worker.cpp index 7ad6e25..2a08495 100644 --- a/src/core/threads/mining_eval_worker.cpp +++ b/src/core/threads/mining_eval_worker.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -47,8 +47,8 @@ #include -#include -#include +#include +#include using Vec3f = Eigen::Vector3f; @@ -107,7 +107,7 @@ void MiningEvalWorker::accept( } } -ResourcePipeline& MiningEvalWorker::getInput() +util::ResourcePipeline& MiningEvalWorker::getInput() { return this->mining_eval_resources; } diff --git a/src/core/threads/mining_eval_worker.hpp b/src/core/threads/mining_eval_worker.hpp index 49db3e7..4f1f091 100644 --- a/src/core/threads/mining_eval_worker.hpp +++ b/src/core/threads/mining_eval_worker.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -53,8 +53,8 @@ #include -#include -#include +#include +#include #include "shared_resources.hpp" #include "../perception_presets.hpp" @@ -87,7 +87,7 @@ class MiningEvalWorker const UpdateMiningEvalSrv::Request::SharedPtr& req, const UpdateMiningEvalSrv::Response::SharedPtr& resp); - ResourcePipeline& getInput(); + util::ResourcePipeline& getInput(); void startThreads(); void stopThreads(); @@ -118,8 +118,8 @@ class MiningEvalWorker std::atomic srv_enable_state{false}; std::atomic query_count{0}; - ResourcePipeline query_notifier; - ResourcePipeline mining_eval_resources; + util::ResourcePipeline query_notifier; + util::ResourcePipeline mining_eval_resources; std::thread mining_eval_thread; }; diff --git a/src/core/threads/path_planning_worker.cpp b/src/core/threads/path_planning_worker.cpp index d8f13a7..85eca1c 100644 --- a/src/core/threads/path_planning_worker.cpp +++ b/src/core/threads/path_planning_worker.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -53,9 +53,8 @@ #include -#include -#include -// #include +#include +#include using namespace util::geom::cvt::ops; @@ -66,41 +65,6 @@ using PathMsg = nav_msgs::msg::Path; using PointCloudMsg = sensor_msgs::msg::PointCloud2; -// namespace csm::perception -// { - -// template -// class PlannningMap : -// public MapOctree -// { -// static_assert(pcl::traits::has_xyz::value); -// static_assert(util::traits::has_trav_weight::value); - -// private: -// using PointT = Point_T; -// using MetaT = Meta_T; -// using PointCloudT = pcl::PointCloud; -// using MetaCloudT = pcl::PointCloud; - -// using Vec3f = Eigen::Vector3f; - -// public: -// PlanningMap(double voxel_res); -// ~PlanningMap() = default; - -// public: -// void update( -// const PointCloudT& points, -// const MetaCloudT& points_meta, -// const Vec3f& bound_min, -// const Vec3f& bound_max); - -// protected: -// }; - -// }; // namespace csm::perception - - namespace csm { namespace perception @@ -139,7 +103,7 @@ void PathPlanningWorker::accept( resp->running = this->srv_enable_state; } -ResourcePipeline& PathPlanningWorker::getInput() +util::ResourcePipeline& PathPlanningWorker::getInput() { return this->path_planning_resources; } @@ -241,7 +205,6 @@ void PathPlanningWorker::path_planning_callback(PathPlanningResources& buff) buff.bounds_min, buff.bounds_max, buff.points, - buff.points_meta, path)) { return; @@ -249,7 +212,7 @@ void PathPlanningWorker::path_planning_callback(PathPlanningResources& buff) PathMsg path_msg; path_msg.header.frame_id = this->odom_frame; - path_msg.header.stamp = util::toTimeStamp(buff.stamp); + path_msg.header.stamp = util::toTimeMsg(buff.stamp); path_msg.poses.reserve(path.size()); for (const Vec3f& kp : path) diff --git a/src/core/threads/path_planning_worker.hpp b/src/core/threads/path_planning_worker.hpp index 206ccef..29c6bd5 100644 --- a/src/core/threads/path_planning_worker.hpp +++ b/src/core/threads/path_planning_worker.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -55,8 +55,8 @@ #include -#include -#include +#include +#include #include "shared_resources.hpp" #include "../perception_presets.hpp" @@ -89,7 +89,7 @@ class PathPlanningWorker const UpdatePathPlanSrv::Request::SharedPtr& req, const UpdatePathPlanSrv::Response::SharedPtr& resp); - ResourcePipeline& getInput(); + util::ResourcePipeline& getInput(); void startThreads(); void stopThreads(); @@ -108,9 +108,9 @@ class PathPlanningWorker std::atomic threads_running{false}; std::atomic srv_enable_state{false}; - PathPlanner path_planner; - ResourcePipeline pplan_target_notifier; - ResourcePipeline path_planning_resources; + PathPlanner path_planner; + util::ResourcePipeline pplan_target_notifier; + util::ResourcePipeline path_planning_resources; std::thread path_planning_thread; }; diff --git a/src/core/threads/shared_resources.hpp b/src/core/threads/shared_resources.hpp index dd96b74..369f85b 100644 --- a/src/core/threads/shared_resources.hpp +++ b/src/core/threads/shared_resources.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -51,7 +51,7 @@ #include #include -#include +#include namespace csm @@ -90,7 +90,6 @@ struct PathPlanningResources util::geom::PoseTf3f base_to_odom; geometry_msgs::msg::PoseStamped target; pcl::PointCloud points; - pcl::PointCloud points_meta; }; struct MiningEvalResources diff --git a/src/core/threads/traversibility_worker.cpp b/src/core/threads/traversibility_worker.cpp index 2157a96..068fe96 100644 --- a/src/core/threads/traversibility_worker.cpp +++ b/src/core/threads/traversibility_worker.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -50,8 +50,8 @@ #include -#include -#include +#include +#include using namespace util::geom::cvt::ops; @@ -83,17 +83,17 @@ void TraversibilityWorker::configure(const std::string& odom_frame) this->odom_frame = odom_frame; } -ResourcePipeline& TraversibilityWorker::getInput() +util::ResourcePipeline& TraversibilityWorker::getInput() { return this->traversibility_resources; } void TraversibilityWorker::connectOutput( - ResourcePipeline& path_planning_resources) + util::ResourcePipeline& path_planning_resources) { this->path_planning_resources = &path_planning_resources; } void TraversibilityWorker::connectOutput( - ResourcePipeline& mining_eval_resources) + util::ResourcePipeline& mining_eval_resources) { this->mining_eval_resources = &mining_eval_resources; } @@ -187,26 +187,10 @@ void TraversibilityWorker::traversibility_callback( this->mining_eval_resources->unlockInputAndNotify(x); } - pcl::PointCloud trav_points; - pcl::PointCloud trav_meta; - pcl::PointCloud trav_debug_cloud; + pcl::PointCloud trav_points, 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; + trav_debug_cloud = trav_points; // need to copy since trav_gen gets swapped later if (this->path_planning_resources) { @@ -216,7 +200,7 @@ void TraversibilityWorker::traversibility_callback( 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); + // x.points_meta.points.swap(trav_meta.points); this->path_planning_resources->unlockInputAndNotify(x); } @@ -226,7 +210,7 @@ void TraversibilityWorker::traversibility_callback( { PointCloudMsg output; pcl::toROSMsg(trav_debug_cloud, output); - output.header.stamp = util::toTimeStamp(buff.stamp); + output.header.stamp = util::toTimeMsg(buff.stamp); output.header.frame_id = this->odom_frame; this->pub_map.publish("traversibility_points", output); } diff --git a/src/core/threads/traversibility_worker.hpp b/src/core/threads/traversibility_worker.hpp index e0ded9c..ae308fe 100644 --- a/src/core/threads/traversibility_worker.hpp +++ b/src/core/threads/traversibility_worker.hpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (C) 2024-2025 Cardinal Space Mining Club * +* Copyright (C) 2024-2026 Cardinal Space Mining Club * * * * ;xxxxxxx: * * ;$$$$$$$$$ ...::.. * @@ -49,9 +49,10 @@ #include -#include -#include -#include +#include +#include + +#include #include "shared_resources.hpp" #include "../perception_presets.hpp" @@ -69,19 +70,17 @@ class TraversibilityWorker using RclNode = rclcpp::Node; public: - TraversibilityWorker( - RclNode& node, - const ImuIntegrator<>& imu_sampler); + TraversibilityWorker(RclNode& node, const ImuIntegrator<>& imu_sampler); ~TraversibilityWorker(); public: void configure(const std::string& odom_frame); - ResourcePipeline& getInput(); + util::ResourcePipeline& getInput(); void connectOutput( - ResourcePipeline& path_planning_resources); + util::ResourcePipeline& path_planning_resources); void connectOutput( - ResourcePipeline& mining_eval_resources); + util::ResourcePipeline& mining_eval_resources); void startThreads(); void stopThreads(); @@ -99,11 +98,12 @@ class TraversibilityWorker std::atomic threads_running{false}; - TraversibilityGenerator + TraversibilityGenerator trav_gen; - ResourcePipeline traversibility_resources; - ResourcePipeline* path_planning_resources{nullptr}; - ResourcePipeline* mining_eval_resources{nullptr}; + util::ResourcePipeline traversibility_resources; + util::ResourcePipeline* path_planning_resources{ + nullptr}; + util::ResourcePipeline* mining_eval_resources{nullptr}; std::thread traversibility_thread; }; 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