From 5e01cf40ddc6dad07965a82f06ed09c5e87b3b9c Mon Sep 17 00:00:00 2001 From: Amael Marquez Date: Tue, 31 Oct 2023 17:05:52 +0100 Subject: [PATCH] [SpatialPartitioning] Add kd-tree sample index iterator and query --- .../Iterator/kdTreeSampleIndexIterator.h | 42 ++++++++++++++++++ .../KdTree/Query/kdTreeSampleIndexQuery.h | 44 +++++++++++++++++++ Ponca/src/SpatialPartitioning/KdTree/kdTree.h | 9 +++- 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 Ponca/src/SpatialPartitioning/KdTree/Iterator/kdTreeSampleIndexIterator.h create mode 100644 Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeSampleIndexQuery.h diff --git a/Ponca/src/SpatialPartitioning/KdTree/Iterator/kdTreeSampleIndexIterator.h b/Ponca/src/SpatialPartitioning/KdTree/Iterator/kdTreeSampleIndexIterator.h new file mode 100644 index 000000000..55df0aa7a --- /dev/null +++ b/Ponca/src/SpatialPartitioning/KdTree/Iterator/kdTreeSampleIndexIterator.h @@ -0,0 +1,42 @@ +/* + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +namespace Ponca { + +template +class KdTreeSampleIndexQuery; + +template +class KdTreeSampleIndexIterator +{ +public: + using IndexType = typename Traits::IndexType; + + void operator++() { ++m_index; } + + bool operator!=(const KdTreeSampleIndexIterator& other) const { + return m_points != other.m_points || m_samples != other.m_samples || m_index != other.m_index; + } + + IndexType operator*() const { return (*m_samples)[m_index]; } + +private: + friend KdTreeSampleIndexQuery; + + using PointContainer = typename Traits::PointContainer; + using IndexContainer = typename Traits::IndexContainer; + + IndexType m_index; + const PointContainer* m_points; + const IndexContainer* m_samples; + + KdTreeSampleIndexIterator(IndexType index, const PointContainer& points, const IndexContainer& samples) + : m_index(index), m_points(&points), m_samples(&samples) {} +}; + +} // namespace Ponca diff --git a/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeSampleIndexQuery.h b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeSampleIndexQuery.h new file mode 100644 index 000000000..d0cc77a44 --- /dev/null +++ b/Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeSampleIndexQuery.h @@ -0,0 +1,44 @@ +/* + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include "../Iterator/kdTreeSampleIndexIterator.h" + +namespace Ponca { + +template +class KdTreeBase; + +template +class KdTreeSampleIndexQuery +{ +public: + using IndexType = typename Traits::IndexType; + using Iterator = KdTreeSampleIndexIterator; + + Iterator begin() { + return Iterator(0, *m_points, *m_samples); + } + + Iterator end() { + return Iterator((IndexType)m_samples->size(), *m_points, *m_samples); + } + +private: + friend KdTreeBase; + + using PointContainer = typename Traits::PointContainer; + using IndexContainer = typename Traits::IndexContainer; + + const PointContainer* m_points; + const IndexContainer* m_samples; + + KdTreeSampleIndexQuery(const PointContainer& points, const IndexContainer& samples) + : m_points(&points), m_samples(&samples) {} +}; + +} // namespace Ponca diff --git a/Ponca/src/SpatialPartitioning/KdTree/kdTree.h b/Ponca/src/SpatialPartitioning/KdTree/kdTree.h index b25f8759a..c84da5b6a 100644 --- a/Ponca/src/SpatialPartitioning/KdTree/kdTree.h +++ b/Ponca/src/SpatialPartitioning/KdTree/kdTree.h @@ -21,6 +21,7 @@ #include "Query/kdTreeNearestQueries.h" #include "Query/kdTreeKNearestQueries.h" #include "Query/kdTreeRangeQueries.h" +#include "Query/kdTreeSampleIndexQuery.h" namespace Ponca { template class KdTreeBase; @@ -77,7 +78,7 @@ class KdTreeBase static_assert(std::is_same::value, "PointContainer must contain DataPoints"); - + // Queries use a value of -1 for invalid indices static_assert(std::is_signed::value, "Index type must be signed"); static_assert(std::is_same::value, "Index type mismatch"); @@ -289,7 +290,11 @@ public : { return KdTreeRangeIndexQuery(this, r, index); } - + + KdTreeSampleIndexQuery sample_point_indices() const + { + return KdTreeSampleIndexQuery(m_points, m_indices); + } // Data -------------------------------------------------------------------- protected: