Ponca  7d8ac87a7de01d881c9fde3c42e397b44bffb901
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{
20
21 template <typename Traits>
22 class KnnGraphBase;
23
32 template <typename DataPoint>
34
46 template <typename Traits>
48 {
49 public:
50 using DataPoint = typename Traits::DataPoint;
51 using Scalar = typename DataPoint::Scalar;
52 using VectorType = typename DataPoint::VectorType;
53
54 using IndexType = typename Traits::IndexType;
55 using PointContainer = typename Traits::PointContainer;
56 using IndexContainer = typename Traits::IndexContainer;
57
60
61 friend class KnnGraphKNearestQuery<Traits>; // This type must be equal to KnnGraphBase::KNearestIndexQuery
62 friend class KnnGraphRangeQuery<Traits>; // This type must be equal to KnnGraphBase::RangeIndexQuery
63
64 // knnGraph ----------------------------------------------------------------
65 public:
73 template <typename KdTreeTraits>
75 : m_k(std::min(k, kdtree.sampleCount() - 1)), m_kdTreePoints(kdtree.points())
76 {
77 static_assert(std::is_same<typename Traits::DataPoint, typename KdTreeTraits::DataPoint>::value,
78 "KdTreeTraits::DataPoint is not equal to Traits::DataPoint");
79 static_assert(std::is_same<typename Traits::PointContainer, typename KdTreeTraits::PointContainer>::value,
80 "KdTreeTraits::PointContainer is not equal to Traits::PointContainer");
81 static_assert(std::is_same<typename Traits::IndexContainer, typename KdTreeTraits::IndexContainer>::value,
82 "KdTreeTraits::IndexContainer is not equal to Traits::IndexContainer");
83
84 // We need to account for the entire point set, irrespectively of the sampling. This is because the kdtree
85 // (kNearestNeighbors) return ids of the entire point set, not it sub-sampled list of ids.
86 // \fixme Update API to properly handle kdtree subsampling
87 const int cloudSize = kdtree.pointCount();
88 {
89 const int samplesSize = kdtree.sampleCount();
91 }
92
93 m_indices.resize(cloudSize * m_k, -1);
94
95#pragma omp parallel for shared(kdtree, cloudSize) default(none)
96 for (int i = 0; i < cloudSize; ++i)
97 {
98 int j = 0;
99 for (auto n : kdtree.kNearestNeighbors(typename KdTreeTraits::IndexType(i),
100 typename KdTreeTraits::IndexType(m_k)))
101 {
102 m_indices[i * m_k + j] = n;
103 ++j;
104 }
105 }
106 }
107
108 // Query -------------------------------------------------------------------
109 public:
119 inline KNearestIndexQuery kNearestNeighbors(int index) const { return KNearestIndexQuery(this, index); }
120
129 inline RangeIndexQuery rangeNeighbors(int index, Scalar r) const { return RangeIndexQuery(this, r, index); }
130
144
154 inline RangeIndexQuery rangeNeighborsIndexQuery() const { return RangeIndexQuery(this, 0, 0); }
155
156 // Accessors ---------------------------------------------------------------
157 public:
159 [[nodiscard]] inline int k() const { return m_k; }
161 [[nodiscard]] inline size_t size() const { return m_indices.size() / static_cast<size_t>(m_k); }
162
163 // Data --------------------------------------------------------------------
164 private:
165 const int m_k;
166 IndexContainer m_indices;
167
168 protected: // for friends relations
169 const PointContainer& m_kdTreePoints;
170 inline const IndexContainer& index_data() const { return m_indices; };
171 };
172
173} // namespace Ponca
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:318
Customizable base class for KnnGraph datastructure.
Definition knnGraph.h:48
typename Traits::PointContainer PointContainer
Container for DataPoint used inside the KdTree.
Definition knnGraph.h:55
KNearestIndexQuery kNearestNeighborsIndexQuery() const
Convenience function that provides a k-nearest neighbors Query object.
Definition knnGraph.h:143
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:50
KnnGraphBase(const KdTreeBase< KdTreeTraits > &kdtree, int k=6)
Build a KnnGraph from a KdTreeDense.
Definition knnGraph.h:74
RangeIndexQuery rangeNeighborsIndexQuery() const
Convenience function that provides an empty range neighbors Query object.
Definition knnGraph.h:154
typename DataPoint::Scalar Scalar
Scalar given by user via DataPoint.
Definition knnGraph.h:51
int k() const
Number of neighbor per vertex.
Definition knnGraph.h:159
size_t size() const
Number of vertices in the neighborhood graph.
Definition knnGraph.h:161
typename Traits::IndexContainer IndexContainer
Container for indices used inside the KdTree.
Definition knnGraph.h:56
typename DataPoint::VectorType VectorType
VectorType given by user via DataPoint.
Definition knnGraph.h:52
KNearestIndexQuery kNearestNeighbors(int index) const
Computes a Query object to iterate over the k-nearest neighbors of a point.
Definition knnGraph.h:119
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.