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