Ponca  82fc77e8c6294111c6f00670e92ec58a24e2ecf2
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>
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(PointContainer _points, typename Traits::IndexContainerRef _indices,
35 const size_t _points_size, const size_t _indices_size,
36 typename Traits::IndexContainerRef _ranges)
37 : Base(_points, _indices, _points_size, _indices_size), ranges(_ranges)
38 {
39 }
40 };
41
52 template <typename _Traits>
54 : public AbstractNeighborGraph<_Traits, NeighborGraphBuffer,
55 NeighborGraphOneConnectedQuery<StaticNeighborGraphBase<_Traits>>,
56 NeighborGraphRangeQuery<StaticNeighborGraphBase<_Traits>>>
57 {
58 public:
59 WRITE_NEIGHBOR_GRAPH_ALIASES
60
65
69 using Buffers = typename Base::Buffers;
70
71 PONCA_MULTIARCH inline StaticNeighborGraphBase(const Buffers& _bufs) : Base(_bufs) {}
72
75 PONCA_MULTIARCH [[nodiscard]] inline int k(int vId = 0) const { return endId(vId) * beginId(vId); }
77 PONCA_MULTIARCH [[nodiscard]] inline int beginId(int vId) const { return Base::buffers().ranges[vId]; }
79 PONCA_MULTIARCH [[nodiscard]] inline int endId(int vId) const { return Base::buffers().ranges[vId + 1]; }
81 };
82
83 template <typename _Traits>
85 {
86 public:
87 WRITE_NEIGHBOR_GRAPH_ALIASES
88
89 private:
91 using Buffers = typename Base::Buffers;
92
93 public:
105 template <typename KdTreeTraits>
106 PONCA_MULTIARCH_HOST inline NeighborGraphBase(const KdTreeBase<KdTreeTraits>& _kdtree, const Scalar range)
107 : Base(_kdtree.points())
108 {
110#define CHECK_TRAITS_TYPENAME_COMPAT(A, B) \
111 static_assert(std::is_same_v<A, B> || std::is_convertible_v<A, B> || std::is_convertible_v<B, A>, \
112 "KdTreeTraits::DataPoint is not equal to Traits::DataPoint");
113
114 static_assert(std::is_same_v<typename Traits::DataPoint, typename KdTreeTraits::DataPoint>,
115 "KdTreeTraits::DataPoint is not equal to Traits::DataPoint");
116
117 CHECK_TRAITS_TYPENAME_COMPAT(typename Traits::PointContainer, typename KdTreeTraits::PointContainer)
118 CHECK_TRAITS_TYPENAME_COMPAT(typename Traits::IndexContainer, typename KdTreeTraits::IndexContainer)
119
120#undef CHECK_TRAITS_TYPENAME_COMPAT
121
122 // We need to account for the entire point set, irrespectively of the sampling. This is because the kdtree
123 // (kNearestNeighbors) return ids of the entire point set, not it sub-sampled list of ids.
124 // \fixme Update API to properly handle kdtree subsampling
125 const int cloudSize = _kdtree.pointCount();
126 {
127 const int samplesSize = _kdtree.sampleCount();
128 PONCA_ASSERT(cloudSize == samplesSize);
129 }
130
131 Base::m_bufs.ranges.resize(cloudSize + 1); // we need one more index (see StaticNeighborGraphBase::endId)
132 Base::m_bufs.ranges[0] = 0; // first element starts at 0;
133
134 for (int i = 0; i < cloudSize; ++i)
135 {
136 for (auto n : _kdtree.rangeNeighbors(typename KdTreeTraits::IndexType(i), range))
137 {
138 Base::m_bufs.indices.push_back(n);
139 }
140 Base::m_bufs.ranges[i + 1] = IndexType(Base::m_bufs.indices.size());
141 }
143 }
144 };
145
154 template <typename DataPoint>
156
157} // namespace Ponca
158
159#undef WRITE_TRAITS
Base class for neighbor graphs.
const Buffers & buffers() const
Get access to the internal buffer, for instance to prepare GPU binding.
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.
typename DataPoint::Scalar Scalar
Scalar given by user via DataPoint
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
Static generic neighbor graph (does not include build functions)
typename Traits::PointContainer PointContainer
Container for DataPoint used inside the KdTree
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]
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11
Internal structure storing the buffers used by a neighbor graph.
size_t points_size
Number of points in the graph.
size_t indices_size
Number of connections in the graph.
IndexContainer indices
Buffer storing the indices associating the input points to the nodes.
Buffer class for StaticNeighborGraphBase.
typename Traits::IndexContainer IndexContainer
Container for indices used inside the KdTree
typename Traits::PointContainerConstRef PointContainerConstRef
Container for DataPoint used inside the KdTree