Ponca  292d8d7132745e0d4242ff9a01e93697fc96b204
Point Cloud Analysis library
Loading...
Searching...
No Matches
neighborGraph.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 "abstractNeighborGraph.h"
10#include "Query/neighborGraphOneConnectedQuery.h"
11#include "Query/neighborGraphRangeQuery.h"
12
13#include "../KdTree/kdTree.h"
14
15namespace Ponca
16{
24 template <typename _Traits>
25 struct NeighborGraphBuffer : public NeighborGraphBufferBase<_Traits>
26 {
27 WRITE_NEIGHBOR_GRAPH_ALIASES
29
30 IndexContainer ranges;
31
32 PONCA_MULTIARCH inline NeighborGraphBuffer() = default;
33 PONCA_MULTIARCH inline NeighborGraphBuffer(PointContainerConstRef _points) : Base(_points) {}
34 PONCA_MULTIARCH inline NeighborGraphBuffer(PointContainerConstRef _points,
35 typename Traits::IndexContainerRef _indices,
36 const size_t _points_size, const size_t _indices_size,
37 typename Traits::IndexContainerRef _ranges)
38 : Base(_points, _indices, _points_size, _indices_size), ranges(_ranges)
39 {
40 }
41 };
42
53 template <typename _Traits>
54 class StaticNeighborGraphBase
55 : public AbstractNeighborGraph<_Traits, NeighborGraphBuffer,
56 NeighborGraphOneConnectedQuery<StaticNeighborGraphBase<_Traits>>,
57 NeighborGraphRangeQuery<StaticNeighborGraphBase<_Traits>>>
58 {
59 public:
60 WRITE_NEIGHBOR_GRAPH_ALIASES
61
63 friend class NeighborGraphOneConnectedQuery<StaticNeighborGraphBase<Traits>>;
65 friend class NeighborGraphRangeQuery<StaticNeighborGraphBase<Traits>>;
66
70 using Buffers = typename Base::Buffers;
71
72 PONCA_MULTIARCH inline StaticNeighborGraphBase(const Buffers& _bufs) : Base(_bufs) {}
73
76 PONCA_MULTIARCH [[nodiscard]] inline int k(int vId = 0) const { return endId(vId) * beginId(vId); }
78 PONCA_MULTIARCH [[nodiscard]] inline int beginId(int vId) const { return Base::buffers().ranges[vId]; }
80 PONCA_MULTIARCH [[nodiscard]] inline int endId(int vId) const { return Base::buffers().ranges[vId + 1]; }
82 };
83
84 template <typename _Traits>
85 class NeighborGraphBase : public StaticNeighborGraphBase<_Traits>
86 {
87 public:
88 WRITE_NEIGHBOR_GRAPH_ALIASES
89
90 private:
91 using Base = StaticNeighborGraphBase<Traits>;
92 using Buffers = typename Base::Buffers;
93
94 public:
106 template <typename KdTreeTraits>
107 PONCA_MULTIARCH_HOST inline NeighborGraphBase(const KdTreeBase<KdTreeTraits>& _kdtree, const Scalar range)
108 : Base(_kdtree.points())
109 {
110 Base::m_bufs.points_size = _kdtree.pointCount();
111#define CHECK_TRAITS_TYPENAME_COMPAT(A, B) \
112 static_assert(std::is_same_v<A, B> || std::is_convertible_v<A, B> || std::is_convertible_v<B, A>, \
113 "KdTreeTraits::DataPoint is not equal to Traits::DataPoint");
114
115 static_assert(std::is_same_v<typename Traits::DataPoint, typename KdTreeTraits::DataPoint>,
116 "KdTreeTraits::DataPoint is not equal to Traits::DataPoint");
117
118 CHECK_TRAITS_TYPENAME_COMPAT(typename Traits::PointContainerConstRef, typename KdTreeTraits::PointContainer)
119 CHECK_TRAITS_TYPENAME_COMPAT(typename Traits::IndexContainer, typename KdTreeTraits::IndexContainer)
120
121#undef CHECK_TRAITS_TYPENAME_COMPAT
122
123 // We need to account for the entire point set, irrespectively of the sampling. This is because the kdtree
124 // (kNearestNeighbors) return ids of the entire point set, not it sub-sampled list of ids.
125 // \fixme Update API to properly handle kdtree subsampling
126 const int cloudSize = _kdtree.pointCount();
127 {
128 const int samplesSize = _kdtree.sampleCount();
129 PONCA_ASSERT(cloudSize == samplesSize);
130 }
131
132 Base::m_bufs.ranges.resize(cloudSize + 1); // we need one more index (see StaticNeighborGraphBase::endId)
133 Base::m_bufs.ranges[0] = 0; // first element starts at 0;
134
135 for (int i = 0; i < cloudSize; ++i)
136 {
137 for (auto n : _kdtree.rangeNeighbors(typename KdTreeTraits::IndexType(i), range))
138 {
139 Base::m_bufs.indices.push_back(n);
140 }
141 Base::m_bufs.ranges[i + 1] = IndexType(Base::m_bufs.indices.size());
142 }
143 Base::m_bufs.indices_size = Base::m_bufs.indices.size();
144 }
145 };
146
155 template <typename DataPoint>
157
158} // namespace Ponca
159
160#undef WRITE_TRAITS
Constructor that allows the use of prebuilt KdTree containers.
Definition kdTree.h:457
NeighborGraphBase(const KdTreeBase< KdTreeTraits > &_kdtree, const Scalar range)
Build a neighbor graph by connecting all points using Euclidean range queries: two points.
typename Traits::IndexType IndexType
Type used to index points into the PointContainer.
Extension of the Query class that allows to read the neighbors that are directly connected to the que...
Extension of the Query class that allows to read the result of a range neighbor search on the KnnGrap...
IndexType pointCount() const
Get the number of points.
Definition kdTree.h:219
IndexType sampleCount() const
Get the number of indices.
Definition kdTree.h:216
KdTreeRangePointQuery< Traits > rangeNeighbors(const VectorType &point, Scalar r) const
Computes a Query object to iterate over the neighbors that are inside a given radius....
Definition kdTree.h:383
int endId(int vId) const
Index of the end of the neighborhood range (see storing convention in NeighborGraphBuffer).
_Traits Traits
Alias to the Traits type.
int beginId(int vId) const
Index of the beginning of the neighborhood range (see storing convention in NeighborGraphBuffer).
int k(int vId=0) const
[Neighbor Graph Accessors]
typename Traits::PointContainerConstRef PointContainerConstRef
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11
NeighborGraphBase< NeighborGraphDefaultTraits< DataPoint > > NeighborGraph
Public interface for the NeighborGraph datastructure.
NeighborGraphBufferBase()=default
Default constructor, might be deleted depending on the PointContainer type (e.g. const reference).
Buffer class for StaticNeighborGraphBase.
typename Traits::PointContainerConstRef PointContainerConstRef
Container for DataPoint used inside the KdTree.
typename Traits::IndexContainer IndexContainer
Container for indices used inside the KdTree.
std::vector< IndexType > IndexContainer
Type used to store the index container in the AbstractNeighborGraph::Buffers.
const PointContainer & PointContainerConstRef
Convenience alias used to convey the constness and the absence of copy of the container.