Skip to content

Commit

Permalink
[SpatialPartitioning] Prevent change of min_cell_size after construction
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nmellado committed Sep 18, 2024
1 parent 48cea2d commit 5cb11da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
39 changes: 19 additions & 20 deletions Ponca/src/SpatialPartitioning/KdTree/kdTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class KdTreeBase
/// \param points Input points
/// \param c Cast/Convert input point type to DataType
template<typename PointUserContainer, typename Converter>
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
Expand All @@ -166,9 +166,9 @@ class KdTreeBase
/// \tparam PointUserContainer Input point container, transformed to PointContainer
/// \param points Input points
template<typename PointUserContainer>
inline void build(PointUserContainer&& points)
inline void build(PointUserContainer&& points, LeafSizeType min_cell_size = DEFAULT_LEAF_SIZE)
{
build(std::forward<PointUserContainer>(points), DefaultConverter());
build(std::forward<PointUserContainer>(points), DefaultConverter(), min_cell_size);
}

/// Clear tree data
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ----------------------------------------------------------------
Expand All @@ -315,7 +309,8 @@ public :
template<typename PointUserContainer, typename IndexUserContainer, typename Converter>
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
Expand All @@ -324,9 +319,10 @@ public :
/// \param sampling Samples used in the tree
template<typename PointUserContainer, typename IndexUserContainer>
inline void buildWithSampling(PointUserContainer&& points,
IndexUserContainer sampling)
IndexUserContainer sampling,
LeafSizeType min_cell_size = DEFAULT_LEAF_SIZE)
{
buildWithSampling(std::forward<PointUserContainer>(points), std::move(sampling), DefaultConverter());
buildWithSampling(std::forward<PointUserContainer>(points), std::move(sampling), DefaultConverter(), min_cell_size);
}

private:
Expand Down Expand Up @@ -361,10 +357,11 @@ class KdTreeDenseBase : public KdTreeBase<Traits>

/// Constructor generating a tree from a custom contained type converted using a \ref KdTreeBase::DefaultConverter
template<typename PointUserContainer>
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<PointUserContainer>(points));
this->build(std::forward<PointUserContainer>(points), min_cell_size);
}
};

Expand Down Expand Up @@ -397,10 +394,11 @@ class KdTreeSparseBase : public KdTreeBase<Traits>

/// Constructor generating a tree from a custom contained type converted using a \ref KdTreeBase::DefaultConverter
template<typename PointUserContainer>
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<PointUserContainer>(points));
this->build(std::forward<PointUserContainer>(points), min_cell_size);
}

/// Constructor generating a tree sampled from a custom contained type converted using a \ref KdTreeBase::DefaultConverter
Expand All @@ -409,10 +407,11 @@ class KdTreeSparseBase : public KdTreeBase<Traits>
/// \param point Input points
/// \param sampling Samples used in the tree
template<typename PointUserContainer, typename IndexUserContainer>
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<PointUserContainer>(points), std::move(sampling));
this->buildWithSampling(std::forward<PointUserContainer>(points), std::move(sampling), min_cell_size);
}

using Base::buildWithSampling;
Expand Down
10 changes: 7 additions & 3 deletions Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

template<typename Traits>
template<typename PointUserContainer, typename Converter>
inline void KdTreeBase<Traits>::build(PointUserContainer&& points, Converter c)
inline void KdTreeBase<Traits>::build(PointUserContainer&& points, Converter c, LeafSizeType min_cell_size)
{
IndexContainer ids(points.size());
std::iota(ids.begin(), ids.end(), 0);
this->buildWithSampling(std::forward<PointUserContainer>(points), std::move(ids), std::move(c));
this->buildWithSampling(std::forward<PointUserContainer>(points), std::move(ids), std::move(c), min_cell_size);
}

template<typename Traits>
Expand Down Expand Up @@ -120,11 +120,15 @@ template<typename Traits>
template<typename PointUserContainer, typename IndexUserContainer, typename Converter>
inline void KdTreeBase<Traits>::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<PointUserContainer>(points), m_points);

Expand Down

0 comments on commit 5cb11da

Please sign in to comment.