Ponca  045d6c276f3af384cb0ea094d76ed661278a034a
Point Cloud Analysis library
Loading...
Searching...
No Matches
query.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 "./defines.h"
10#include "./indexSquaredDistance.h"
11#include "../Common/Containers/limitedPriorityQueue.h"
12
13#include <cmath>
14
15namespace Ponca {
16
17
21#define DECLARE_INDEX_QUERY_CLASS(OUT_TYPE) \
22 \
23template <typename Index, typename Scalar> \
24struct OUT_TYPE##IndexQuery : Query<QueryInputIsIndex<Index>, QueryOutputIs##OUT_TYPE<Index, Scalar>> \
25{ \
26 using Base = Query<QueryInputIsIndex<Index>, QueryOutputIs##OUT_TYPE<Index, Scalar>>; \
27 using Base::Base; \
28};
29
30
34#define DECLARE_POINT_QUERY_CLASS(OUT_TYPE) \
35 \
36template <typename Index, typename DataPoint> \
37struct OUT_TYPE##PointQuery : Query<QueryInputIsPosition<DataPoint>, \
38 QueryOutputIs##OUT_TYPE<Index, typename DataPoint::Scalar>> \
39{ \
40 using Base = Query<QueryInputIsPosition<DataPoint>, QueryOutputIs##OUT_TYPE<Index, typename DataPoint::Scalar>>; \
41 using Base::Base; \
42};
43
46
48// Base classes
50
53 };
54
58 };
59 };
60
62 template<typename InputType_>
63 struct QueryInput : public QueryInputBase {
64 using InputType = InputType_;
65
66 inline QueryInput(InputType input) : m_input(input) {}
67
68 inline const InputType &input() const { return m_input; }
69 protected:
74 inline void editInput(const InputType& input) { m_input = input; }
75
76 private:
78 const InputType m_input;
79 };
80
81
83 template <typename Index>
84 struct QueryInputIsIndex : public QueryInput<Index> {
85 using Base = QueryInput<Index>;
86 using InputType = typename Base::InputType;
87
88 inline QueryInputIsIndex(const InputType &point = -1)
89 : Base(point) {}
90 protected:
92 template <typename IndexType>
93 inline bool skipIndexFunctor(IndexType idx) const {return Base::input() == idx;};
95 template <typename Container>
96 inline auto getInputPosition(const Container &c) -> const typename Container::value_type::VectorType
97 { return c[Base::input()].pos(); }
98 };
99
101 template<typename DataPoint>
102 struct QueryInputIsPosition : public QueryInput<typename DataPoint::VectorType> {
104 using InputType = typename Base::InputType;
105
106 inline QueryInputIsPosition(const InputType &point = InputType::Zero())
107 : Base(point) {}
108 protected:
110 template <typename IndexType>
111 inline bool skipIndexFunctor(IndexType idx) const {return false;};
113 template <typename Container>
114 inline auto getInputPosition(const Container &) -> const typename Container::value_type::VectorType
115 { return Base::input(); }
116 };
117
119 template<typename Index, typename Scalar>
121 using OutputParameter = Scalar;
122
123 inline QueryOutputIsRange(OutputParameter radius = OutputParameter(0))
124 : m_squared_radius(std::pow(radius, OutputParameter(2))) {}
125
126 inline Scalar radius() const { return std::sqrt(m_squared_radius); }
127
128 inline Scalar squared_radius() const { return m_squared_radius; }
129
130 inline void set_radius(Scalar radius) { m_squared_radius = std::pow(radius, 2); }
131
132 inline void set_squared_radius(Scalar radius) { m_squared_radius = radius; }
133
134 protected:
136 inline void reset() { }
138 inline Scalar descentDistanceThreshold() const { return m_squared_radius; }
139 Scalar m_squared_radius{0};
140 };
141
143 template<typename Index, typename Scalar>
145 using OutputParameter = typename QueryOutputBase::DummyOutputParameter;
146
148
149 Index get() const { return m_nearest; }
150
151 protected:
153 void reset() {
154 m_nearest = -1;
155 m_squared_distance = std::numeric_limits<Scalar>::max();
156 }
158 inline Scalar descentDistanceThreshold() const { return m_squared_distance; }
159
160 Index m_nearest {-1};
161 Scalar m_squared_distance {std::numeric_limits<Scalar>::max()};
162 };
163
165 template<typename Index, typename Scalar>
167 using OutputParameter = Index;
168
169 inline QueryOutputIsKNearest(OutputParameter k = 0) : m_queue(k) {}
170
171 inline limited_priority_queue<IndexSquaredDistance<Index, Scalar>> &queue() { return m_queue; }
172
173 protected:
175 void reset() {
176 m_queue.clear();
177 m_queue.push({-1,std::numeric_limits<Scalar>::max()});
178 }
180 inline Scalar descentDistanceThreshold() const { return m_queue.bottom().squared_distance; }
182 };
183
184
185 template<typename Input_, typename Output_>
186 struct Query : public Input_, public Output_ {
187 using QueryInType = Input_;
188 using QueryOutType = Output_;
189
190 static_assert(std::is_base_of<QueryInputBase, QueryInType>::value,
191 "QueryInType must inherit Ponca::QueryInputBase");
192 static_assert(std::is_base_of<QueryOutputBase, QueryOutType>::value,
193 "QueryInType must inherit Ponca::QueryInputBase");
194
195 inline Query(const typename QueryInType::InputType &in)
196 : QueryInType(in), QueryOutType() {}
197
198 inline Query(const typename QueryOutType::OutputParameter &outParam,
199 const typename QueryInType::InputType &in)
200 : QueryOutType(outParam), QueryInType(in) {}
201 };
202
203DECLARE_INDEX_QUERY_CLASS(KNearest) //KNearestIndexQuery
204DECLARE_INDEX_QUERY_CLASS(Nearest) //NearestIndexQuery
205DECLARE_INDEX_QUERY_CLASS(Range) //RangeIndexQuery
206DECLARE_POINT_QUERY_CLASS(KNearest) //KNearestPointQuery
207DECLARE_POINT_QUERY_CLASS(Nearest) //NearestPointQuery
208DECLARE_POINT_QUERY_CLASS(Range) //RangePointQuery
209
211
212#undef DECLARE_INDEX_QUERY_CLASS
213#undef DECLARE_POINT_QUERY_CLASS
214}
The limited_priority_queue class is similar to std::priority_queue but has a limited capacity and han...
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition: query.h:158
auto getInputPosition(const Container &c) -> const typename Container::value_type::VectorType
Generic method to access input position. Container is expected to hold kdtree positions.
Definition: query.h:96
void editInput(const InputType &input)
Edit the input Need to be used carefully. Modifying a query input while iterating on the query will r...
Definition: query.h:74
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition: query.h:138
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition: query.h:93
auto getInputPosition(const Container &) -> const typename Container::value_type::VectorType
Generic method to access input position. Container is expected to hold kdtree positions.
Definition: query.h:114
void reset()
Reset Query for a new search.
Definition: query.h:136
void reset()
Reset Query for a new search.
Definition: query.h:175
void reset()
Reset Query for a new search.
Definition: query.h:153
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition: query.h:180
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition: query.h:111
Base class for typed queries input type.
Definition: query.h:63
Base class for queries input type.
Definition: query.h:52
Base class for queries storing points.
Definition: query.h:84
Base class for queries storing points.
Definition: query.h:102
Base class for queries output type.
Definition: query.h:56
Base class for knearest queries.
Definition: query.h:166
Base class for nearest queries.
Definition: query.h:144
Base class for range queries.
Definition: query.h:120
This Source Code Form is subject to the terms of the Mozilla Public License, v.