Skip to content

Commit

Permalink
[SpatialPartitioning] Remove code duplication between Index/Point que…
Browse files Browse the repository at this point in the history
…ries
  • Loading branch information
nmellado committed Jul 26, 2023
1 parent 88e73a4 commit a714333
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 255 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,48 @@

namespace Ponca {

template <typename Traits>
class KdTreeKNearestIndexQuery : public KdTreeQuery<Traits>,
public KNearestIndexQuery<typename Traits::IndexType, typename Traits::DataPoint::Scalar>
template <typename Traits,
template <typename, typename> typename IteratorType,
typename QueryType>
class KdTreeKNearestQueryBase : public KdTreeQuery<Traits>, public QueryType
{
public:
using DataPoint = typename Traits::DataPoint;
using IndexType = typename Traits::IndexType;
using Scalar = typename DataPoint::Scalar;
using VectorType = typename DataPoint::VectorType;
using QueryType = KNearestIndexQuery<IndexType, typename DataPoint::Scalar>;
using QueryAccelType = KdTreeQuery<Traits>;
using Iterator = KdTreeKNearestIterator<IndexType, DataPoint>;
using Iterator = IteratorType<typename Traits::IndexType, typename Traits::DataPoint>;

KdTreeKNearestIndexQuery(const KdTreeBase<Traits>* kdtree, IndexType k, IndexType index) :
KdTreeQuery<Traits>(kdtree), KNearestIndexQuery<IndexType, Scalar>(k, index)
{
}
inline KdTreeKNearestQueryBase(const KdTreeBase<Traits>* kdtree, IndexType k, typename QueryType::InputType input) :
KdTreeQuery<Traits>(kdtree), QueryType(k, input) { }

public:
Iterator begin(){
inline Iterator begin(){
QueryAccelType::reset();
QueryType::reset();
this->search();
return Iterator(QueryType::m_queue.begin());
}
Iterator end(){
inline Iterator end(){
return Iterator(QueryType::m_queue.end());
}

protected:
void search(){
KdTreeQuery<Traits>::search_internal(QueryAccelType::m_kdtree->point_data()[QueryType::input()].pos(),
inline void search(){
KdTreeQuery<Traits>::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->point_data()),
[](IndexType, IndexType){},
[this](){return QueryType::descentDistanceThreshold();},
[this](IndexType idx){return QueryType::input() == idx;},
[this](IndexType idx){return QueryType::skipIndexFunctor(idx);},
[this](IndexType idx, IndexType, Scalar d){QueryType::m_queue.push({idx, d}); return false;}
);
}
};

template <typename Traits>
using KdTreeKNearestIndexQuery = KdTreeKNearestQueryBase< Traits, KdTreeKNearestIterator,
KNearestIndexQuery<typename Traits::IndexType, typename Traits::DataPoint::Scalar>>;
template <typename Traits>
using KdTreeKNearestPointQuery = KdTreeKNearestQueryBase< Traits, KdTreeKNearestIterator,
KNearestPointQuery<typename Traits::IndexType, typename Traits::DataPoint>>;
} // namespace ponca

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@

namespace Ponca {

template <typename Traits>
class KdTreeNearestIndexQuery : public KdTreeQuery<Traits>,
public NearestIndexQuery<typename Traits::IndexType, typename Traits::DataPoint::Scalar>
template <typename Traits,
template <typename> typename IteratorType,
typename QueryType>
class KdTreeNearestQueryBase : public KdTreeQuery<Traits>, public QueryType
{
public:
using DataPoint = typename Traits::DataPoint;
using IndexType = typename Traits::IndexType;
using Scalar = typename DataPoint::Scalar;
using VectorType = typename DataPoint::VectorType;
using QueryType = NearestIndexQuery<IndexType, typename DataPoint::Scalar>;
using QueryAccelType = KdTreeQuery<Traits>;
using Iterator = KdTreeNearestIterator<IndexType>;
using Iterator = IteratorType<typename Traits::IndexType>;

KdTreeNearestIndexQuery(const KdTreeBase<Traits>* kdtree, IndexType index) :
KdTreeQuery<Traits>(kdtree), NearestIndexQuery<IndexType, Scalar>(index)
{
}
KdTreeNearestQueryBase(const KdTreeBase<Traits>* kdtree, typename QueryType::InputType input) :
KdTreeQuery<Traits>(kdtree), QueryType(input){}

public:
inline Iterator begin(){
Expand All @@ -37,16 +35,16 @@ class KdTreeNearestIndexQuery : public KdTreeQuery<Traits>,
this->search();
return Iterator(QueryType::m_nearest);
}
Iterator end(){
inline Iterator end(){
return Iterator(QueryType::m_nearest + 1);
}

protected:
void search(){
KdTreeQuery<Traits>::search_internal(QueryAccelType::m_kdtree->point_data()[QueryType::input()].pos(),
inline void search(){
KdTreeQuery<Traits>::search_internal(QueryType::getInputPosition(QueryAccelType::m_kdtree->point_data()),
[](IndexType, IndexType){},
[this](){return QueryType::descentDistanceThreshold();},
[this](IndexType idx){return QueryType::input() == idx;},
[this](IndexType idx){return QueryType::skipIndexFunctor(idx);},
[this](IndexType idx, IndexType, Scalar d)
{
QueryType::m_nearest = idx;
Expand All @@ -56,4 +54,11 @@ class KdTreeNearestIndexQuery : public KdTreeQuery<Traits>,
);
}
};

template <typename Traits>
using KdTreeNearestIndexQuery = KdTreeNearestQueryBase< Traits, KdTreeNearestIterator,
NearestIndexQuery<typename Traits::IndexType, typename Traits::DataPoint::Scalar>>;
template <typename Traits>
using KdTreeNearestPointQuery = KdTreeNearestQueryBase< Traits, KdTreeNearestIterator,
NearestPointQuery<typename Traits::IndexType, typename Traits::DataPoint>>;
} // namespace ponca
91 changes: 0 additions & 91 deletions Ponca/src/SpatialPartitioning/KdTree/Query/kdTreeRangePointQuery.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@

namespace Ponca {

template <typename Traits>
class KdTreeRangeIndexQuery : public KdTreeQuery<Traits>,
public RangeIndexQuery<typename Traits::IndexType, typename Traits::DataPoint::Scalar>
template <typename Traits,
template <typename,typename,typename> typename IteratorType,
typename QueryType>
class KdTreeRangeQueryBase : public KdTreeQuery<Traits>, public QueryType
//public RangeIndexQuery<typename Traits::IndexType, typename Traits::DataPoint::Scalar>
{
public:
using DataPoint = typename Traits::DataPoint;
using IndexType = typename Traits::IndexType;
using Scalar = typename DataPoint::Scalar;
using VectorType = typename DataPoint::VectorType;
using QueryType = RangeIndexQuery<IndexType, typename DataPoint::Scalar>;
using QueryAccelType = KdTreeQuery<Traits>;
using Iterator = KdTreeRangeIterator<IndexType, DataPoint, KdTreeRangeIndexQuery>;
using Iterator = IteratorType<IndexType, DataPoint, KdTreeRangeQueryBase>;

protected:
friend Iterator;

public:

KdTreeRangeIndexQuery(const KdTreeBase<Traits>* kdtree, Scalar radius, IndexType index) :
KdTreeQuery<Traits>(kdtree), RangeIndexQuery<IndexType, Scalar>(radius, index)
{
}
KdTreeRangeQueryBase(const KdTreeBase<Traits>* kdtree, Scalar radius, typename QueryType::InputType input) :
KdTreeQuery<Traits>(kdtree), QueryType(radius, input){}

public:
inline Iterator begin(){
Expand All @@ -51,10 +49,10 @@ class KdTreeRangeIndexQuery : public KdTreeQuery<Traits>,
inline void advance(Iterator& it){
const auto& points = QueryAccelType::m_kdtree->point_data();
const auto& indices = QueryAccelType::m_kdtree->index_data();
const auto& point = QueryAccelType::m_kdtree->point_data()[QueryType::input()].pos();
const auto& point = QueryType::getInputPosition(points);

auto descentDistanceThreshold = [this](){return QueryType::descentDistanceThreshold();};
auto skipFunctor = [this](IndexType idx){return QueryType::input() == idx;};
auto skipFunctor = [this](IndexType idx){return QueryType::skipIndexFunctor(idx);};
auto processNeighborFunctor = [&it](IndexType idx, IndexType i, Scalar)
{
it.m_index = idx;
Expand Down Expand Up @@ -89,4 +87,11 @@ class KdTreeRangeIndexQuery : public KdTreeQuery<Traits>,
it.m_index = static_cast<IndexType>(points.size());
}
};

template <typename Traits>
using KdTreeRangeIndexQuery = KdTreeRangeQueryBase< Traits, KdTreeRangeIterator,
RangeIndexQuery<typename Traits::IndexType, typename Traits::DataPoint::Scalar>>;
template <typename Traits>
using KdTreeRangePointQuery = KdTreeRangeQueryBase< Traits, KdTreeRangeIterator,
RangePointQuery<typename Traits::IndexType, typename Traits::DataPoint>>;
} // namespace ponca
Loading

0 comments on commit a714333

Please sign in to comment.