Ponca  7df32c91629c89b89840c4d7917cb272433f2d2b
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 PONCA_MULTIARCH_INCLUDE_STD(cmath)
14#include PONCA_MULTIARCH_INCLUDE_STD(limits)
15
16namespace Ponca
17{
20
22 // Base classes
24
27 {
28 };
29
40 {
42 {
43 };
44 };
45
55 template <typename InputType_>
56 struct QueryInput : public QueryInputBase
57 {
60
62 PONCA_MULTIARCH inline QueryInput(InputType input) : m_input(input) {}
63
65 PONCA_MULTIARCH inline const InputType& input() const { return m_input; }
66
67 protected:
76 PONCA_MULTIARCH inline void setInput(const InputType& input) { m_input = input; }
77
78 private:
80 InputType m_input;
81 };
82
89 template <typename Index>
90 struct QueryInputIsIndex : public QueryInput<Index>
91 {
95 using InputType = typename Base::InputType;
96
98 PONCA_MULTIARCH inline QueryInputIsIndex(const InputType& point = -1) : Base(point) {}
99
101 PONCA_MULTIARCH inline void operator()(const InputType& point = InputType::Zero()) { Base::setInput(point); }
102
103 protected:
105 template <typename IndexType>
106 PONCA_MULTIARCH inline bool skipIndexFunctor(IndexType idx) const
107 {
108 return Base::input() == idx;
109 };
111 template <typename VectorType, typename Container>
112 PONCA_MULTIARCH inline const VectorType getInputPosition(const Container& c)
113 {
114 return c[Base::input()].pos();
115 }
116 };
117
124 template <typename DataPoint>
125 struct QueryInputIsPosition : public QueryInput<typename DataPoint::VectorType>
126 {
130 using InputType = typename Base::InputType;
131
133 PONCA_MULTIARCH inline QueryInputIsPosition(const InputType& point = InputType::Zero()) : Base(point) {}
134
136 PONCA_MULTIARCH inline void operator()(const InputType& point = InputType::Zero()) { Base::setInput(point); }
137
138 protected:
140 template <typename IndexType>
141 PONCA_MULTIARCH inline bool skipIndexFunctor(IndexType idx) const
142 {
143 return false;
144 };
146 template <typename VectorType = typename DataPoint::VectorType, typename Container>
147 PONCA_MULTIARCH inline const VectorType getInputPosition(const Container&)
148 {
149 return Base::input();
150 }
151 };
152
159 template <typename Index, typename Scalar>
161 {
163 using OutputParameter = Scalar;
164
167 : m_squared_radius(PONCA_MULTIARCH_STD_MATH_NAMESPACE(pow)(radius, OutputParameter(2)))
168 {
169 }
170
172 PONCA_MULTIARCH inline void operator()(OutputParameter radius) { setRadius(radius); }
173
184 PONCA_MULTIARCH inline Scalar radius() const
185 {
186 PONCA_MULTIARCH_STD_MATH(sqrt);
187 return sqrt(m_squared_radius);
188 }
189
191 PONCA_MULTIARCH inline Scalar squaredRadius() const { return m_squared_radius; }
192
198 PONCA_MULTIARCH inline void setRadius(Scalar radius) { setSquaredRadius(radius * radius); }
199
201 PONCA_MULTIARCH inline void setSquaredRadius(Scalar radius) { m_squared_radius = radius; }
202
203 protected:
205 PONCA_MULTIARCH inline void reset() {}
207 PONCA_MULTIARCH inline Scalar descentDistanceThreshold() const { return m_squared_radius; }
210 };
211
218 template <typename Index, typename Scalar>
220 {
223
225 PONCA_MULTIARCH QueryOutputIsNearest() {}
226
228 PONCA_MULTIARCH inline void operator()() {}
229
231 PONCA_MULTIARCH Index get() const { return m_nearest; }
232
233 protected:
235 PONCA_MULTIARCH void reset()
236 {
237 m_nearest = -1;
238 m_squared_distance = PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits)<Scalar>::max();
239 }
241 PONCA_MULTIARCH inline Scalar descentDistanceThreshold() const { return m_squared_distance; }
242
244 Index m_nearest{-1};
246 Scalar m_squared_distance{PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits) < Scalar > ::max()};
247 };
248
256 template <typename Index, typename Scalar, int MAX_KNN_SIZE>
258 {
260 using OutputParameter = Index;
263
265 PONCA_MULTIARCH inline QueryOutputIsKNearest(OutputParameter k = 0) : m_queue(k) {}
266
268 PONCA_MULTIARCH inline void operator()(OutputParameter k) { m_queue = Queue(k); }
269
271 PONCA_MULTIARCH inline Queue& queue() { return m_queue; }
272
273 protected:
275 PONCA_MULTIARCH void reset()
276 {
277 m_queue.clear();
278 m_queue.push({-1, PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits) < Scalar > ::max()});
279 }
281 PONCA_MULTIARCH inline Scalar descentDistanceThreshold() const { return m_queue.bottom().squared_distance; }
284 };
285
292 template <typename Input_, typename Output_>
293 struct Query : public Input_, public Output_
294 {
296 using QueryInType = Input_;
298 using QueryOutType = Output_;
299
300 static_assert(std::is_base_of<QueryInputBase, QueryInType>::value,
301 "QueryInType must inherit Ponca::QueryInputBase");
302 static_assert(std::is_base_of<QueryOutputBase, QueryOutType>::value,
303 "QueryInType must inherit Ponca::QueryInputBase");
304
306 PONCA_MULTIARCH inline Query(const typename QueryInType::InputType& in) : QueryInType(in), QueryOutType() {}
307
309 PONCA_MULTIARCH inline Query(const typename QueryOutType::OutputParameter& outParam,
310 const typename QueryInType::InputType& in)
312 {
313 }
314
316 template <typename Base, typename... outputType>
317 PONCA_MULTIARCH inline Base& operator()(const typename QueryInType::InputType& in, outputType&&... out)
318 {
319 QueryInType::operator()(in);
320 QueryOutType::operator()(std::forward<outputType>(out)...);
321 return *((Base*)(this));
322 }
323
325 template <typename Base>
326 PONCA_MULTIARCH inline Base& operator()(const typename QueryInType::InputType& in)
327 {
328 QueryInType::operator()(in);
329 return *((Base*)(this));
330 }
331 };
332
333#define POINT_QUERY_DOC(OUT_TYPE)
\
335 \
336 \
337 \
338
341#define INDEX_QUERY_DOC(OUT_TYPE)
\
343 \
344 \
345 \
346
349 POINT_QUERY_DOC(KNearest)
351 template <typename Index, typename DataPoint, int MAX_KNN_SIZE>
354 POINT_QUERY_DOC(Nearest)
355 template <typename Index, typename DataPoint>
356 using NearestPointQuery =
358 POINT_QUERY_DOC(Range)
359 template <typename Index, typename DataPoint>
360 using RangePointQuery =
362
364 template <typename Index, typename Scalar, int MAX_KNN_SIZE>
367 template <typename Index, typename Scalar>
370 template <typename Index, typename Scalar>
373
374#undef POINT_QUERY_DOC
375#undef INDEX_QUERY_DOC
376} // namespace Ponca
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:257
QueryInput(InputType input)
Default constructor that initialize the input field.
Definition query.h:62
Index get() const
Get the closest points.
Definition query.h:231
Scalar squaredRadius() const
Generic method to access the radius squared.
Definition query.h:191
void operator()(const InputType &point=InputType::Zero())
Access operator that resets the input point.
Definition query.h:136
Base & operator()(const typename QueryInType::InputType &in)
Access operator that resets the input parameter only.
Definition query.h:326
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:241
void operator()(OutputParameter k)
Access operator that resets the output parameter.
Definition query.h:268
const VectorType getInputPosition(const Container &)
Generic method to access input position. The VectorType needs to be passed as a template argument.
Definition query.h:147
InputType_ InputType
Alias to the templated input type.
Definition query.h:59
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:207
QueryOutputIsRange(OutputParameter radius=OutputParameter(0))
Default constructor that initialize the output parameter value.
Definition query.h:166
QueryInputIsPosition(const InputType &point=InputType::Zero())
Default constructor that initialize the input position.
Definition query.h:133
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition query.h:106
typename Base::InputType InputType
Alias to the templated input type (Index)
Definition query.h:130
const VectorType getInputPosition(const Container &c)
Generic method to access input position. The VectorType needs to be passed as a template argument.
Definition query.h:112
LimitedPriorityQueue< IndexSquaredDistance< Index, Scalar >, MAX_KNN_SIZE > Queue
Alias to LimitedPriorityQueue.
Definition query.h:262
Index m_nearest
Index of the nearest neighbor.
Definition query.h:244
void operator()()
Access operator.
Definition query.h:228
Query(const typename QueryOutType::OutputParameter &outParam, const typename QueryInType::InputType &in)
Default constructor that initialize the input and output parameters.
Definition query.h:309
Scalar radius() const
Generic method to access the radius.
Definition query.h:184
Scalar OutputParameter
Alias to Output type.
Definition query.h:163
void reset()
Reset Query for a new search.
Definition query.h:205
typename QueryOutputBase::DummyOutputParameter OutputParameter
Alias to Output type.
Definition query.h:222
Scalar m_squared_radius
Radius used for the search.
Definition query.h:209
void setSquaredRadius(Scalar radius)
Set the squared radius distance of the query.
Definition query.h:201
typename Base::InputType InputType
Alias to the templated input type (Index)
Definition query.h:95
Base & operator()(const typename QueryInType::InputType &in, outputType &&... out)
Access operator that resets the input and output parameters.
Definition query.h:317
Input_ QueryInType
Alias to the input type.
Definition query.h:296
void setRadius(Scalar radius)
Set the radius distance of the query.
Definition query.h:198
QueryOutputIsNearest()
Default constructor.
Definition query.h:225
void reset()
Reset Query for a new search.
Definition query.h:235
Output_ QueryOutType
Alias to the output type.
Definition query.h:298
Index OutputParameter
Alias to Output type.
Definition query.h:260
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:281
Scalar m_squared_distance
Distance to the nearest neighbor.
Definition query.h:246
void operator()(OutputParameter radius)
Access operator that resets the output parameter.
Definition query.h:172
Queue & queue()
Access to the priority queue storing the neighbors.
Definition query.h:271
const InputType & input() const
Read access to the input.
Definition query.h:65
Query(const typename QueryInType::InputType &in)
Default constructor that initialize the input parameter only.
Definition query.h:306
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition query.h:141
QueryOutputIsKNearest(OutputParameter k=0)
Default constructor that initialize the output parameter value.
Definition query.h:265
QueryInputIsIndex(const InputType &point=-1)
Default constructor that initialize the input index.
Definition query.h:98
void setInput(const InputType &input)
Edit the input.
Definition query.h:76
void reset()
Reset Query for a new search.
Definition query.h:275
void operator()(const InputType &point=InputType::Zero())
Access operator that resets the input index.
Definition query.h:101
Queue m_queue
Queue storing the neighbors.
Definition query.h:283
#define INDEX_QUERY_DOC(OUT_TYPE)
Base Query class combining QueryInputIsPosition and QueryOutputIs##OUT_TYPE##.
Definition query.h:341
Composes the Query object depending on an input type and output type.
Definition query.h:294
Base class for Query input type.
Definition query.h:57
Base class for queries input type.
Definition query.h:27
Extension of QueryInput that handles an index based search, in a partitioning structure.
Definition query.h:91
Extension of QueryInput that handles a position based search, in a partitioning structure.
Definition query.h:126
Base class for queries output types.
Definition query.h:40
Class to construct the knearest queries.
Definition query.h:258
Class to construct the nearest query output.
Definition query.h:220
Class to construct the range query output.
Definition query.h:161
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition bitset.h:17