From 5cb11daa068f4e39112ea819492e2b1d6c05b3ca Mon Sep 17 00:00:00 2001 From: Nicolas Mellado Date: Wed, 18 Sep 2024 21:21:58 +0200 Subject: [PATCH] [SpatialPartitioning] Prevent change of min_cell_size after construction Changing the min_cell_size affects the construction, but cannot be set only after processing. Changes: - remove setter, and add a paramter when building the tree --- Ponca/src/SpatialPartitioning/KdTree/kdTree.h | 39 +++++++++---------- .../src/SpatialPartitioning/KdTree/kdTree.hpp | 10 +++-- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Ponca/src/SpatialPartitioning/KdTree/kdTree.h b/Ponca/src/SpatialPartitioning/KdTree/kdTree.h index f7b17ffd..df9aaf24 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/kdTree.h +++ b/Ponca/src/SpatialPartitioning/KdTree/kdTree.h @@ -145,7 +145,7 @@ class KdTreeBase /// \param points Input points /// \param c Cast/Convert input point type to DataType template - inline void build(PointUserContainer&& points, Converter c); + inline void build(PointUserContainer&& points, Converter c, LeafSizeType min_cell_size = DEFAULT_LEAF_SIZE ); /// Convert a custom point container to the KdTree \ref PointContainer using \ref DataPoint default constructor struct DefaultConverter @@ -166,9 +166,9 @@ class KdTreeBase /// \tparam PointUserContainer Input point container, transformed to PointContainer /// \param points Input points template - inline void build(PointUserContainer&& points) + inline void build(PointUserContainer&& points, LeafSizeType min_cell_size = DEFAULT_LEAF_SIZE) { - build(std::forward(points), DefaultConverter()); + build(std::forward(points), DefaultConverter(), min_cell_size); } /// Clear tree data @@ -224,13 +224,6 @@ class KdTreeBase return m_min_cell_size; } - /// Write leaf min size - inline void set_min_cell_size(LeafSizeType min_cell_size) - { - PONCA_DEBUG_ASSERT(min_cell_size > 0); - m_min_cell_size = min_cell_size; - } - // Index mapping ----------------------------------------------------------- public: /// Return the point index associated with the specified sample index @@ -298,7 +291,8 @@ public : NodeContainer m_nodes; IndexContainer m_indices; - LeafSizeType m_min_cell_size {64}; ///< Minimal number of points per leaf + static const LeafSizeType DEFAULT_LEAF_SIZE = 64; + LeafSizeType m_min_cell_size {DEFAULT_LEAF_SIZE}; ///< Minimal number of points per leaf NodeIndexType m_leaf_count {0}; ///< Number of leaves in the Kdtree (computed during construction) // Internal ---------------------------------------------------------------- @@ -315,7 +309,8 @@ public : template inline void buildWithSampling(PointUserContainer&& points, IndexUserContainer sampling, - Converter c); + Converter c, + LeafSizeType min_cell_size = DEFAULT_LEAF_SIZE); /// Generate a tree sampled from a custom contained type converted using a \ref KdTreeBase::DefaultConverter /// \tparam PointUserContainer Input points, transformed to PointContainer @@ -324,9 +319,10 @@ public : /// \param sampling Samples used in the tree template inline void buildWithSampling(PointUserContainer&& points, - IndexUserContainer sampling) + IndexUserContainer sampling, + LeafSizeType min_cell_size = DEFAULT_LEAF_SIZE) { - buildWithSampling(std::forward(points), std::move(sampling), DefaultConverter()); + buildWithSampling(std::forward(points), std::move(sampling), DefaultConverter(), min_cell_size); } private: @@ -361,10 +357,11 @@ class KdTreeDenseBase : public KdTreeBase /// Constructor generating a tree from a custom contained type converted using a \ref KdTreeBase::DefaultConverter template - inline explicit KdTreeDenseBase(PointUserContainer&& points) + inline explicit KdTreeDenseBase(PointUserContainer&& points, + typename Base::LeafSizeType min_cell_size = Base::DEFAULT_LEAF_SIZE) : Base() { - this->build(std::forward(points)); + this->build(std::forward(points), min_cell_size); } }; @@ -397,10 +394,11 @@ class KdTreeSparseBase : public KdTreeBase /// Constructor generating a tree from a custom contained type converted using a \ref KdTreeBase::DefaultConverter template - inline explicit KdTreeSparseBase(PointUserContainer&& points) + inline explicit KdTreeSparseBase(PointUserContainer&& points, + typename Base::LeafSizeType min_cell_size = Base::DEFAULT_LEAF_SIZE) : Base() { - this->build(std::forward(points)); + this->build(std::forward(points), min_cell_size); } /// Constructor generating a tree sampled from a custom contained type converted using a \ref KdTreeBase::DefaultConverter @@ -409,10 +407,11 @@ class KdTreeSparseBase : public KdTreeBase /// \param point Input points /// \param sampling Samples used in the tree template - inline KdTreeSparseBase(PointUserContainer&& points, IndexUserContainer sampling) + inline KdTreeSparseBase(PointUserContainer&& points, IndexUserContainer sampling, + typename Base::LeafSizeType min_cell_size = Base::DEFAULT_LEAF_SIZE) : Base() { - this->buildWithSampling(std::forward(points), std::move(sampling)); + this->buildWithSampling(std::forward(points), std::move(sampling), min_cell_size); } using Base::buildWithSampling; diff --git a/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp b/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp index 4d5783a1..255d6f07 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp +++ b/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp @@ -8,11 +8,11 @@ template template -inline void KdTreeBase::build(PointUserContainer&& points, Converter c) +inline void KdTreeBase::build(PointUserContainer&& points, Converter c, LeafSizeType min_cell_size) { IndexContainer ids(points.size()); std::iota(ids.begin(), ids.end(), 0); - this->buildWithSampling(std::forward(points), std::move(ids), std::move(c)); + this->buildWithSampling(std::forward(points), std::move(ids), std::move(c), min_cell_size); } template @@ -120,11 +120,15 @@ template template inline void KdTreeBase::buildWithSampling(PointUserContainer&& points, IndexUserContainer sampling, - Converter c) + Converter c, + LeafSizeType min_cell_size) { PONCA_DEBUG_ASSERT(points.size() <= MAX_POINT_COUNT); this->clear(); + // Set cell size + this->m_min_cell_size = min_cell_size; + // Move, copy or convert input samples c(std::forward(points), m_points);