Skip to content

Commit

Permalink
[SpatialPartitioning] Add kd-tree sample index iterator and query
Browse files Browse the repository at this point in the history
  • Loading branch information
Amael Marquez committed Oct 31, 2023
1 parent f8e54a5 commit 5e01cf4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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 <typename Traits>
class KdTreeSampleIndexQuery;

template <typename Traits>
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<Traits>;

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
Original file line number Diff line number Diff line change
@@ -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 <typename Traits>
class KdTreeBase;

template <typename Traits>
class KdTreeSampleIndexQuery
{
public:
using IndexType = typename Traits::IndexType;
using Iterator = KdTreeSampleIndexIterator<Traits>;

Iterator begin() {
return Iterator(0, *m_points, *m_samples);
}

Iterator end() {
return Iterator((IndexType)m_samples->size(), *m_points, *m_samples);
}

private:
friend KdTreeBase<Traits>;

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
9 changes: 7 additions & 2 deletions Ponca/src/SpatialPartitioning/KdTree/kdTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Query/kdTreeNearestQueries.h"
#include "Query/kdTreeKNearestQueries.h"
#include "Query/kdTreeRangeQueries.h"
#include "Query/kdTreeSampleIndexQuery.h"

namespace Ponca {
template <typename Traits> class KdTreeBase;
Expand Down Expand Up @@ -77,7 +78,7 @@ class KdTreeBase

static_assert(std::is_same<typename PointContainer::value_type, DataPoint>::value,
"PointContainer must contain DataPoints");

// Queries use a value of -1 for invalid indices
static_assert(std::is_signed<IndexType>::value, "Index type must be signed");
static_assert(std::is_same<typename IndexContainer::value_type, IndexType>::value, "Index type mismatch");
Expand Down Expand Up @@ -289,7 +290,11 @@ public :
{
return KdTreeRangeIndexQuery<Traits>(this, r, index);
}


KdTreeSampleIndexQuery<Traits> sample_point_indices() const
{
return KdTreeSampleIndexQuery<Traits>(m_points, m_indices);
}

// Data --------------------------------------------------------------------
protected:
Expand Down

0 comments on commit 5e01cf4

Please sign in to comment.