Ponca  f5b8b13495108d95baa74f687c24d962f21272fc
Point Cloud Analysis library
Loading...
Searching...
No Matches
knnGraph.h
1/*
2 This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6
7#pragma once
8
9#include "./knnGraphTraits.h"
10
11#include "Query/knnGraphKNearestQuery.h"
12#include "Query/knnGraphRangeQuery.h"
13
14#include "../KdTree/kdTree.h"
15
16#include <memory>
17
18namespace Ponca {
19
20template <typename Traits> class KnnGraphBase;
21
30template <typename DataPoint>
32
44template <typename Traits> class KnnGraphBase
45{
46public:
47 using DataPoint = typename Traits::DataPoint;
48 using Scalar = typename DataPoint::Scalar;
49 using VectorType = typename DataPoint::VectorType;
50
51 using IndexType = typename Traits::IndexType;
52 using PointContainer = typename Traits::PointContainer;
53 using IndexContainer = typename Traits::IndexContainer;
54
57
58 friend class KnnGraphKNearestQuery<Traits>; // This type must be equal to KnnGraphBase::KNearestIndexQuery
59 friend class KnnGraphRangeQuery<Traits>; // This type must be equal to KnnGraphBase::RangeIndexQuery
60
61 // knnGraph ----------------------------------------------------------------
62public:
70 template<typename KdTreeTraits>
71 inline KnnGraphBase(const KdTreeBase<KdTreeTraits>& kdtree, int k = 6)
72 : m_k(std::min(k,kdtree.sample_count()-1)),
73 m_kdTreePoints(kdtree.points())
74 {
75 static_assert( std::is_same<typename Traits::DataPoint, typename KdTreeTraits::DataPoint>::value,
76 "KdTreeTraits::DataPoint is not equal to Traits::DataPoint" );
77 static_assert( std::is_same<typename Traits::PointContainer, typename KdTreeTraits::PointContainer>::value,
78 "KdTreeTraits::PointContainer is not equal to Traits::PointContainer" );
79 static_assert( std::is_same<typename Traits::IndexContainer, typename KdTreeTraits::IndexContainer>::value,
80 "KdTreeTraits::IndexContainer is not equal to Traits::IndexContainer" );
81
82 // We need to account for the entire point set, irrespectively of the sampling. This is because the kdtree
83 // (kNearestNeighbors) return ids of the entire point set, not it sub-sampled list of ids.
84 // \fixme Update API to properly handle kdtree subsampling
85 const int cloudSize = kdtree.pointCount();
86 {
87 const int samplesSize = kdtree.sample_count();
88 eigen_assert(cloudSize == samplesSize);
89 }
90
91 m_indices.resize(cloudSize * m_k, -1);
92
93#pragma omp parallel for shared(kdtree, cloudSize) default(none)
94 for(int i=0; i<cloudSize; ++i)
95 {
96 int j = 0;
97 for(auto n : kdtree.kNearestNeighbors(typename KdTreeTraits::IndexType(i),
98 typename KdTreeTraits::IndexType(m_k)))
99 {
100 m_indices[i * m_k + j] = n;
101 ++j;
102 }
103 }
104 }
105
106 // Query -------------------------------------------------------------------
107public:
117 inline KNearestIndexQuery kNearestNeighbors(int index) const{
118 return KNearestIndexQuery(this, index);
119 }
120
129 inline RangeIndexQuery rangeNeighbors(int index, Scalar r) const{
130 return RangeIndexQuery(this, r, index);
131 }
132
146 {
147 return KNearestIndexQuery(this, 0);
148 }
149
160 {
161 return RangeIndexQuery(this, 0, 0);
162 }
163
164 // Accessors ---------------------------------------------------------------
165public:
167 [[nodiscard]] inline int k() const { return m_k; }
169 [[nodiscard]] inline size_t size() const { return m_indices.size()/static_cast<size_t>(m_k); }
170
171 // Data --------------------------------------------------------------------
172private:
173 const int m_k;
174 IndexContainer m_indices;
175
176protected: // for friends relations
177 const PointContainer& m_kdTreePoints;
178 inline const IndexContainer& index_data() const { return m_indices; };
179};
180
181} // namespace Ponca
[KdTreeSparse type definition]
Definition kdTree.h:104
KdTreeKNearestPointQuery< Traits > kNearestNeighbors(const VectorType &point, IndexType k) const
Computes a Query object to iterate over the k-nearest neighbors of a point. The returned object can b...
Definition kdTree.h:268
Customizable base class for KnnGraph datastructure.
Definition knnGraph.h:45
typename Traits::PointContainer PointContainer
Container for DataPoint used inside the KdTree.
Definition knnGraph.h:52
KNearestIndexQuery kNearestNeighborsIndexQuery() const
Convenience function that provides a k-nearest neighbors Query object.
Definition knnGraph.h:145
RangeIndexQuery rangeNeighbors(int index, Scalar r) const
Computes a Query object to iterate over the neighbors that are inside a given radius.
Definition knnGraph.h:129
typename Traits::DataPoint DataPoint
DataPoint given by user via Traits.
Definition knnGraph.h:47
KnnGraphBase(const KdTreeBase< KdTreeTraits > &kdtree, int k=6)
Build a KnnGraph from a KdTreeDense.
Definition knnGraph.h:71
RangeIndexQuery rangeNeighborsIndexQuery() const
Convenience function that provides an empty range neighbors Query object.
Definition knnGraph.h:159
typename DataPoint::Scalar Scalar
Scalar given by user via DataPoint.
Definition knnGraph.h:48
int k() const
Number of neighbor per vertex.
Definition knnGraph.h:167
size_t size() const
Number of vertices in the neighborhood graph.
Definition knnGraph.h:169
typename Traits::IndexContainer IndexContainer
Container for indices used inside the KdTree.
Definition knnGraph.h:53
typename DataPoint::VectorType VectorType
VectorType given by user via DataPoint.
Definition knnGraph.h:49
KNearestIndexQuery kNearestNeighbors(int index) const
Computes a Query object to iterate over the k-nearest neighbors of a point.
Definition knnGraph.h:117
Extension of the Query class that allows to read the result of a k-nearest neighbors search on the Kn...
Extension of the Query class that allows to read the result of a range neighbor search on the KnnGrap...
This Source Code Form is subject to the terms of the Mozilla Public License, v.