Ponca  aa50bfdf187919869239c5b44b748842569114c1
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 // (k_nearest_neighbors) 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.point_count();
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.k_nearest_neighbors(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:
108 inline KNearestIndexQuery k_nearest_neighbors(int index) const{
109 return KNearestIndexQuery(this, index);
110 }
111
112 inline RangeIndexQuery range_neighbors(int index, Scalar r) const{
113 return RangeIndexQuery(this, r, index);
114 }
115
116 // Accessors ---------------------------------------------------------------
117public:
119 inline int k() const { return m_k; }
121 inline int size() const { return m_indices.size()/m_k; }
122
123 // Data --------------------------------------------------------------------
124private:
125 const int m_k;
126 IndexContainer m_indices;
127
128protected: // for friends relations
129 const PointContainer& m_kdTreePoints;
130 inline const IndexContainer& index_data() const { return m_indices; };
131};
132
133} // namespace Ponca
[KdTreeSparse type definition]
Definition: kdTree.h:104
Customizable base class for KnnGraph datastructure.
Definition: knnGraph.h:45
int size() const
Number of vertices in the neighborhood graph.
Definition: knnGraph.h:121
typename Traits::PointContainer PointContainer
Container for DataPoint used inside the KdTree.
Definition: knnGraph.h:52
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
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:119
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
Base Query class combining QueryInputIsIndex and QueryOutputIsKNearest.
Definition: query.h:203
This Source Code Form is subject to the terms of the Mozilla Public License, v.