Ponca  7df32c91629c89b89840c4d7917cb272433f2d2b
Point Cloud Analysis library
Loading...
Searching...
No Matches
knnGraphRangeQuery.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 "../../query.h"
10#include "../Iterator/knnGraphRangeIterator.h"
11
12namespace Ponca
13{
14 template <typename Traits>
15 class StaticKnnGraphBase;
16
55 template <typename Traits>
56 class KnnGraphRangeQuery : public RangeIndexQuery<typename Traits::IndexType, typename Traits::DataPoint::Scalar>
57 {
58 protected:
60 friend class KnnGraphRangeIterator<Traits>; // This type must be equal to KnnGraphRangeQuery::Iterator
61
62 public:
63 using DataPoint = typename Traits::DataPoint;
64 using IndexType = typename Traits::IndexType;
65 using Scalar = typename DataPoint::Scalar;
66 using VectorType = typename DataPoint::VectorType;
69
70 public:
71 PONCA_MULTIARCH inline KnnGraphRangeQuery(const StaticKnnGraphBase<Traits>* graph, Scalar radius, int index)
72 : QueryType(radius, index), m_graph(graph), m_flag(), m_stack()
73 {
74 }
75
77 PONCA_MULTIARCH inline Self& operator()(int index, Scalar radius)
78 {
79 return QueryType::template operator()<Self>(index, radius);
80 }
81
83 PONCA_MULTIARCH inline Self& operator()(int index) { return QueryType::template operator()<Self>(index); }
84
86 PONCA_MULTIARCH inline Iterator begin()
87 {
88 QueryType::reset();
89 Iterator it(this);
90 this->initialize(it);
91 this->advance(it);
92 return it;
93 }
94
96 PONCA_MULTIARCH inline Iterator end() { return Iterator(this, static_cast<int>(m_graph->size())); }
97
98 protected:
99 PONCA_MULTIARCH inline void initialize(Iterator& iterator)
100 {
101 m_flag.clear();
102 m_flag.insert(QueryType::input());
103
104 PONCA_DEBUG_ASSERT(m_stack.empty());
105 m_stack.push(QueryType::input());
106
107 iterator.m_index = -1;
108 }
109
116 PONCA_MULTIARCH inline void advance(Iterator& iterator)
117 {
118 const auto& points = m_graph->points();
119 const auto& point = points[QueryType::input()].pos();
120
121 if (iterator == end())
122 return;
123
124 // Search the next neighbor in range
125 while (!m_stack.empty())
126 {
127 int idx_current = m_stack.top();
128 m_stack.pop();
129
130 PONCA_DEBUG_ASSERT((point - points[idx_current].pos()).squaredNorm() < QueryType::squaredRadius());
131
132 iterator.m_index = idx_current;
133
134 for (int idx_nei : m_graph->kNearestNeighbors(idx_current))
135 {
136 PONCA_DEBUG_ASSERT(idx_nei >= 0);
137 // Add into the search stack only if :
138 // A - The point is within range
139 Scalar d = (point - points[idx_nei].pos()).squaredNorm();
140 Scalar th = QueryType::descentDistanceThreshold();
141 if (d >= th)
142 continue;
143
144 // B - The point is not already visited
146 if (!wasInserted)
147 {
148 // Check that the Set isn't full (in case if it's a limited capacity set)
149 PONCA_ASSERT(iteratorToInsertedValue != m_flag.end());
150 continue;
151 }
152
153 m_stack.push(idx_nei);
154 }
155
156 // Query is not included in returned set
157 if (iterator.m_index != QueryType::input())
158 return; // Next neighbor was found
159 }
160 iterator = end(); // If no neighbor was found, we've reached the end
161 }
162
163 protected:
164 const StaticKnnGraphBase<Traits>* m_graph{nullptr};
165 typename Traits::KnnGraphRangeSet m_flag;
166 typename Traits::KnnGraphRangeStack m_stack;
167 };
168
169} // namespace Ponca
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:257
Input iterator to read the KnnGraphRangeQuery object.
Extension of the Query class that allows to read the result of a range neighbor search on the KnnGrap...
Self & operator()(int index, Scalar radius)
Call the range neighbors query with new input and radius parameters.
void advance(Iterator &iterator)
Helper function for the KnnGraphRangeIterator that advances the range neighbors search using the k-ne...
Iterator begin()
Returns an iterator to the beginning of the range neighbors query.
Traits::KnnGraphRangeStack m_stack
Holds the next ids the Query should visit.
Self & operator()(int index)
Call the range neighbors query with new input parameter.
Traits::KnnGraphRangeSet m_flag
Stores every visited neighbor ids.
Iterator end()
Returns an iterator to the end of the range neighbors query.
Composes the Query object depending on an input type and output type.
Definition query.h:294
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition bitset.h:17