From 0b98833fc18af2dcdef4340a28961481963bb207 Mon Sep 17 00:00:00 2001 From: Amael Marquez Date: Mon, 11 Dec 2023 16:41:36 +0100 Subject: [PATCH 1/3] [SpatialPartitioning] Change part of the kdtree API --- .../KdTree/Query/kdTreeKNearestQueries.h | 2 +- .../KdTree/Query/kdTreeNearestQueries.h | 2 +- .../KdTree/Query/kdTreeQuery.h | 6 +-- .../KdTree/Query/kdTreeRangeQueries.h | 4 +- Ponca/src/SpatialPartitioning/KdTree/kdTree.h | 20 +++++--- .../src/SpatialPartitioning/KdTree/kdTree.hpp | 50 +++++++++---------- .../SpatialPartitioning/KnnGraph/knnGraph.h | 6 +-- examples/cpp/ponca_basic_cpu.cpp | 2 +- tests/src/basket.cpp | 4 +- 9 files changed, 50 insertions(+), 46 deletions(-) diff --git a/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeKNearestQueries.h b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeKNearestQueries.h index 043b371e5..dfb03a7c4 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeKNearestQueries.h +++ b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeKNearestQueries.h @@ -41,7 +41,7 @@ class KdTreeKNearestQueryBase : public KdTreeQuery, public QueryType protected: inline void search(){ - KdTreeQuery::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->point_data()), + KdTreeQuery::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->points()), [](IndexType, IndexType){}, [this](){return QueryType::descentDistanceThreshold();}, [this](IndexType idx){return QueryType::skipIndexFunctor(idx);}, diff --git a/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeNearestQueries.h b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeNearestQueries.h index 629b2f7e0..e6f6c3d53 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeNearestQueries.h +++ b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeNearestQueries.h @@ -41,7 +41,7 @@ class KdTreeNearestQueryBase : public KdTreeQuery, public QueryType protected: inline void search(){ - KdTreeQuery::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->point_data()), + KdTreeQuery::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->points()), [](IndexType, IndexType){}, [this](){return QueryType::descentDistanceThreshold();}, [this](IndexType idx){return QueryType::skipIndexFunctor(idx);}, diff --git a/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeQuery.h b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeQuery.h index f51b29912..8580c409d 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeQuery.h +++ b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeQuery.h @@ -44,9 +44,9 @@ class KdTreeQuery ProcessNeighborFunctor processNeighborFunctor ) { - const auto& nodes = m_kdtree->node_data(); - const auto& points = m_kdtree->point_data(); - const auto& indices = m_kdtree->index_data(); + const auto& nodes = m_kdtree->nodes(); + const auto& points = m_kdtree->points(); + const auto& indices = m_kdtree->samples(); if (nodes.empty() || points.empty() || indices.empty()) throw std::invalid_argument("Empty KdTree"); diff --git a/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeRangeQueries.h b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeRangeQueries.h index e6102486d..127077ab0 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeRangeQueries.h +++ b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeRangeQueries.h @@ -47,8 +47,8 @@ class KdTreeRangeQueryBase : public KdTreeQuery, public QueryType protected: inline void advance(Iterator& it){ - const auto& points = QueryAccelType::m_kdtree->point_data(); - const auto& indices = QueryAccelType::m_kdtree->index_data(); + const auto& points = QueryAccelType::m_kdtree->points(); + const auto& indices = QueryAccelType::m_kdtree->samples(); const auto& point = QueryType::getInputPosition(points); auto descentDistanceThreshold = [this](){return QueryType::descentDistanceThreshold();}; diff --git a/Ponca/src/SpatialPartitioning/KdTree/kdTree.h b/Ponca/src/SpatialPartitioning/KdTree/kdTree.h index 572cebd84..a91f7cf40 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/kdTree.h +++ b/Ponca/src/SpatialPartitioning/KdTree/kdTree.h @@ -8,6 +8,7 @@ #include "./kdTreeTraits.h" +#include #include #include #include @@ -186,7 +187,7 @@ class KdTreeBase Converter c); inline bool valid() const; - inline std::string to_string(bool verbose = false) const; + inline void print(std::ostream& os, bool verbose = false) const; // Accessors --------------------------------------------------------------- public: @@ -195,7 +196,7 @@ class KdTreeBase return m_nodes.size(); } - inline IndexType index_count() const + inline IndexType sample_count() const { return (IndexType)m_indices.size(); } @@ -210,22 +211,22 @@ class KdTreeBase return m_leaf_count; } - inline PointContainer& point_data() + inline PointContainer& points() { return m_points; }; - inline const PointContainer& point_data() const + inline const PointContainer& points() const { return m_points; }; - inline const NodeContainer& node_data() const + inline const NodeContainer& nodes() const { return m_nodes; } - inline const IndexContainer& index_data() const + inline const IndexContainer& samples() const { return m_indices; } @@ -295,3 +296,10 @@ public : #include "./kdTree.hpp" } // namespace Ponca + +template +std::ostream& operator<<(std::ostream& os, Ponca::KdTreeBase& kdtree) +{ + kdtree.print(os); + return os; +} diff --git a/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp b/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp index e0f746f46..d39f54175 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp +++ b/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp @@ -42,7 +42,7 @@ inline void KdTreeBase::buildWithSampling(PointUserContainer&& points, m_indices = std::move(sampling); - this->build_rec(0, 0, index_count(), 1); + this->build_rec(0, 0, sample_count(), 1); PONCA_DEBUG_ASSERT(this->valid()); } @@ -73,7 +73,7 @@ bool KdTreeBase::valid() const const NodeType& node = m_nodes[n]; if(node.is_leaf()) { - if(index_count() <= node.leaf_start() || node.leaf_start()+node.leaf_size() > index_count()) + if(sample_count() <= node.leaf_start() || node.leaf_start()+node.leaf_size() > sample_count()) { return false; } @@ -95,52 +95,48 @@ bool KdTreeBase::valid() const } template -std::string KdTreeBase::to_string(bool verbose) const +void KdTreeBase::print(std::ostream& os, bool verbose) const { - std::stringstream str; - - str << "KdTree:"; - str << "\n MaxNodes: " << MAX_NODE_COUNT; - str << "\n MaxPoints: " << MAX_POINT_COUNT; - str << "\n MaxDepth: " << Traits::MAX_DEPTH; - str << "\n PointCount: " << point_count(); - str << "\n SampleCount: " << index_count(); - str << "\n NodeCount: " << node_count(); + os << "KdTree:"; + os << "\n MaxNodes: " << MAX_NODE_COUNT; + os << "\n MaxPoints: " << MAX_POINT_COUNT; + os << "\n MaxDepth: " << Traits::MAX_DEPTH; + os << "\n PointCount: " << point_count(); + os << "\n SampleCount: " << index_count(); + os << "\n NodeCount: " << node_count(); if (!verbose) { - return str.str(); + return; } - str << "\n Samples: ["; + os << "\n Samples: ["; static constexpr IndexType SAMPLES_PER_LINE = 10; for (IndexType i = 0; i < index_count(); ++i) { - str << (i == 0 ? "" : ","); - str << (i % SAMPLES_PER_LINE == 0 ? "\n " : " "); - str << m_indices[i]; + os << (i == 0 ? "" : ","); + os << (i % SAMPLES_PER_LINE == 0 ? "\n " : " "); + os << m_indices[i]; } - str << "]\n Nodes:"; + os << "]\n Nodes:"; for (NodeIndexType n = 0; n < node_count(); ++n) { const NodeType& node = m_nodes[n]; if (node.is_leaf()) { - str << "\n - Type: Leaf"; - str << "\n Start: " << node.leaf_start(); - str << "\n Size: " << node.leaf_size(); + os << "\n - Type: Leaf"; + os << "\n Start: " << node.leaf_start(); + os << "\n Size: " << node.leaf_size(); } else { - str << "\n - Type: Inner"; - str << "\n SplitDim: " << node.inner_split_dim(); - str << "\n SplitValue: " << node.inner_split_value(); - str << "\n FirstChild: " << node.inner_first_child_id(); + os << "\n - Type: Inner"; + os << "\n SplitDim: " << node.inner_split_dim(); + os << "\n SplitValue: " << node.inner_split_value(); + os << "\n FirstChild: " << node.inner_first_child_id(); } } - - return str.str(); } template diff --git a/Ponca/src/SpatialPartitioning/KnnGraph/knnGraph.h b/Ponca/src/SpatialPartitioning/KnnGraph/knnGraph.h index 4433dd36e..a10066283 100644 --- a/Ponca/src/SpatialPartitioning/KnnGraph/knnGraph.h +++ b/Ponca/src/SpatialPartitioning/KnnGraph/knnGraph.h @@ -70,8 +70,8 @@ template class KnnGraphBase /// \warning KdTreeTraits compatibility is checked with static assertion template inline KnnGraphBase(const KdTreeBase& kdtree, int k = 6) - : m_k(std::min(k,kdtree.index_count()-1)), - m_kdTreePoints(kdtree.point_data()) + : m_k(std::min(k,kdtree.sample_count()-1)), + m_kdTreePoints(kdtree.points()) { static_assert( std::is_same::value, "KdTreeTraits::DataPoint is not equal to Traits::DataPoint" ); @@ -85,7 +85,7 @@ template class KnnGraphBase // \fixme Update API to properly handle kdtree subsampling const int cloudSize = kdtree.point_count(); { - const int samplesSize = kdtree.index_count(); + const int samplesSize = kdtree.sample_count(); eigen_assert(cloudSize == samplesSize); } diff --git a/examples/cpp/ponca_basic_cpu.cpp b/examples/cpp/ponca_basic_cpu.cpp index bba27b89e..76ade052c 100644 --- a/examples/cpp/ponca_basic_cpu.cpp +++ b/examples/cpp/ponca_basic_cpu.cpp @@ -89,7 +89,7 @@ void test_fit(Fit& _fit, const KdTree& tree, const VectorType& _p) // Iterate over samples and _fit the primitive for(int i : tree.range_neighbors(_p, tmax) ) { - _fit.addNeighbor( tree.point_data()[i] ); + _fit.addNeighbor( tree.points()[i] ); } //finalize fitting diff --git a/tests/src/basket.cpp b/tests/src/basket.cpp index f89d87b04..9f56e79c6 100644 --- a/tests/src/basket.cpp +++ b/tests/src/basket.cpp @@ -70,7 +70,7 @@ void testBasicFunctionalities(const KdTree& tree, typen typedef typename DataPoint::VectorType VectorType; typedef typename Fit::WFunctor WeightFunc; - const auto& vectorPoints = tree.point_data(); + const auto& vectorPoints = tree.points(); // Test for each point if the fitted sphere correspond to the theoretical sphere #ifdef NDEBUG @@ -135,7 +135,7 @@ void testIsSame(const KdTree& tree, typedef typename Fit1::Scalar Scalar; typedef typename Fit1::VectorType VectorType; typedef typename Fit1::WFunctor WeightFunc; - const auto& vectorPoints = tree.point_data(); + const auto& vectorPoints = tree.points(); // Test for each point if the fitted sphere correspond to the theoretical sphere #ifdef NDEBUG From 53b1fbaf0bb9fe2553472aba0f7a3f4dab912bcd Mon Sep 17 00:00:00 2001 From: Amael Marquez Date: Mon, 11 Dec 2023 16:48:13 +0100 Subject: [PATCH 2/3] Update CHANGELOG --- CHANGELOG | 1 + Ponca/src/SpatialPartitioning/KdTree/kdTree.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index f0352cbe0..076939814 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -16,6 +16,7 @@ This release introduces several bug fixes and minor improvements. - [spatialPartitioning] Simplify node customization (#128) - [fitting] Mark `Base` type as protected instead of private in CRTP classes (#119) - [fitting] Improve KdTreeNodes API by hiding internal memory layout, improve methods naming (#120) + - [SpatialPartitioning] Change part of the kdtree API (#123) - Examples - Fix example cuda/ponca_ssgls (#109) diff --git a/Ponca/src/SpatialPartitioning/KdTree/kdTree.h b/Ponca/src/SpatialPartitioning/KdTree/kdTree.h index a91f7cf40..c791bc999 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/kdTree.h +++ b/Ponca/src/SpatialPartitioning/KdTree/kdTree.h @@ -298,7 +298,7 @@ public : } // namespace Ponca template -std::ostream& operator<<(std::ostream& os, Ponca::KdTreeBase& kdtree) +std::ostream& operator<<(std::ostream& os, const Ponca::KdTreeBase& kdtree) { kdtree.print(os); return os; From bf61fde1132563754b43876b60507150214cf942 Mon Sep 17 00:00:00 2001 From: Amael Marquez Date: Fri, 15 Dec 2023 16:44:04 +0100 Subject: [PATCH 3/3] [SpatialPartitioning] Fix kdtree print using old method names --- CHANGELOG | 3 ++- Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp | 4 ++-- examples/cpp/ponca_customize_kdtree.cpp | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 076939814..5994db921 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,8 @@ Ponca changelog -------------------------------------------------------------------------------- Current head (v.1.3 RC) +- API + - [SpatialPartitioning] Change part of the kdtree API (#123) -------------------------------------------------------------------------------- v.1.2 @@ -16,7 +18,6 @@ This release introduces several bug fixes and minor improvements. - [spatialPartitioning] Simplify node customization (#128) - [fitting] Mark `Base` type as protected instead of private in CRTP classes (#119) - [fitting] Improve KdTreeNodes API by hiding internal memory layout, improve methods naming (#120) - - [SpatialPartitioning] Change part of the kdtree API (#123) - Examples - Fix example cuda/ponca_ssgls (#109) diff --git a/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp b/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp index d39f54175..bf6706764 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp +++ b/Ponca/src/SpatialPartitioning/KdTree/kdTree.hpp @@ -102,7 +102,7 @@ void KdTreeBase::print(std::ostream& os, bool verbose) const os << "\n MaxPoints: " << MAX_POINT_COUNT; os << "\n MaxDepth: " << Traits::MAX_DEPTH; os << "\n PointCount: " << point_count(); - os << "\n SampleCount: " << index_count(); + os << "\n SampleCount: " << sample_count(); os << "\n NodeCount: " << node_count(); if (!verbose) @@ -112,7 +112,7 @@ void KdTreeBase::print(std::ostream& os, bool verbose) const os << "\n Samples: ["; static constexpr IndexType SAMPLES_PER_LINE = 10; - for (IndexType i = 0; i < index_count(); ++i) + for (IndexType i = 0; i < sample_count(); ++i) { os << (i == 0 ? "" : ","); os << (i % SAMPLES_PER_LINE == 0 ? "\n " : " "); diff --git a/examples/cpp/ponca_customize_kdtree.cpp b/examples/cpp/ponca_customize_kdtree.cpp index 5619391d7..e8f430d8d 100644 --- a/examples/cpp/ponca_customize_kdtree.cpp +++ b/examples/cpp/ponca_customize_kdtree.cpp @@ -80,7 +80,7 @@ int main() << *kdtree.nearest_neighbor(query_pt).begin() << std::endl; //! [ReadCustomProperties] - auto bbox = kdtree.node_data()[0].getAabb(); + auto bbox = kdtree.nodes()[0].getAabb(); if (bbox) { std::cout << "Root bounding box is as follows: \n" << " Center: " << bbox->center()