Ponca  f5b8b13495108d95baa74f687c24d962f21272fc
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
12#include <vector>
13#include <stack>
14#include <set>
15
16namespace Ponca {
17template <typename Traits> class KnnGraphBase;
18
26template <typename Traits>
27class KnnGraphRangeQuery : public RangeIndexQuery<typename Traits::IndexType, typename Traits::DataPoint::Scalar>
28{
29protected:
31 friend class KnnGraphRangeIterator<Traits>; // This type must be equal to KnnGraphRangeQuery::Iterator
32
33public:
34 using DataPoint = typename Traits::DataPoint;
35 using IndexType = typename Traits::IndexType;
36 using Scalar = typename DataPoint::Scalar;
37 using VectorType = typename DataPoint::VectorType;
40
41public:
42 inline KnnGraphRangeQuery(const KnnGraphBase<Traits>* graph, Scalar radius, int index):
43 QueryType(radius, index),
44 m_graph(graph),
45 m_flag(),
46 m_stack() {}
47
49 inline Self& operator()(int index, Scalar radius) {
50 return QueryType::template operator()<Self>(index, radius);
51 }
52
54 inline Self& operator()(int index) {
55 return QueryType::template operator()<Self>(index);
56 }
57
59 inline Iterator begin(){
61 Iterator it(this);
62 this->initialize(it);
63 this->advance(it);
64 return it;
65 }
66
68 inline Iterator end(){
69 return Iterator(this, static_cast<int>(m_graph->size()));
70 }
71
72protected:
73 inline void initialize(Iterator& iterator){
74 m_flag.clear();
75 m_flag.insert(QueryType::input());
76
77 PONCA_DEBUG_ASSERT(m_stack.empty());
79
80 iterator.m_index = -1;
81 }
82
83 inline void advance(Iterator& iterator){
84 const auto& points = m_graph->m_kdTreePoints;
85 const auto& point = points[QueryType::input()].pos();
86
87 if(! (iterator != end())) return;
88
89 if(m_stack.empty())
90 {
91 iterator = end();
92 }
93 else
94 {
95 int idx_current = m_stack.top();
96 m_stack.pop();
97
98 PONCA_DEBUG_ASSERT((point - points[idx_current].pos()).squaredNorm() < QueryType::squaredRadius());
99
100 iterator.m_index = idx_current;
101
102 for(int idx_nei : m_graph->kNearestNeighbors(idx_current))
103 {
104 PONCA_DEBUG_ASSERT(idx_nei>=0);
105 Scalar d = (point - points[idx_nei].pos()).squaredNorm();
107 if((point - points[idx_nei].pos()).squaredNorm() < QueryType::descentDistanceThreshold() && m_flag.insert(idx_nei).second)
108 {
109 m_stack.push(idx_nei);
110 }
111 }
112 if (iterator.m_index == QueryType::input()) advance(iterator); // query is not included in returned set
113 }
114 }
115
116protected:
117 const KnnGraphBase<Traits>* m_graph {nullptr};
118 std::set<int> m_flag;
119 std::stack<int> m_stack;
120};
121
122} // namespace Ponca
Customizable base class for KnnGraph datastructure.
Definition knnGraph.h:45
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.
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.
std::stack< int > m_stack
hold ids (ids range from 0 to point cloud size)
std::set< int > m_flag
store visited ids
Iterator end()
Returns an iterator to the end of the range neighbors query.
Scalar squaredRadius() const
Generic method to access the radius squared.
Definition query.h:218
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:236
void reset()
Reset Query for a new search.
Definition query.h:234
const InputType & input() const
Read access to the input.
Definition query.h:102
Base Query class combining QueryInputIsIndex and QueryOutputIsRange.
Definition query.h:354
This Source Code Form is subject to the terms of the Mozilla Public License, v.