Ponca  93eea5457c48839cb5d16642765afa89fc7cfe66
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
18
22#define DECLARE_INDEX_QUERY_CLASS(OUT_TYPE) \
23 \
24 \
25 \
26 \
27 \
28 \
29template <typename Index, typename Scalar> \
30struct OUT_TYPE##IndexQuery : Query<QueryInputIsIndex<Index>, QueryOutputIs##OUT_TYPE<Index, Scalar>> \
31{ \
32 \
33 using Base = Query<QueryInputIsIndex<Index>, QueryOutputIs##OUT_TYPE<Index, Scalar>>; \
34 \
35 using Base::Base; \
36};
37
38
42#define DECLARE_POINT_QUERY_CLASS(OUT_TYPE) \
43 \
44 \
45 \
46 \
47 \
48 \
49template <typename Index, typename DataPoint> \
50struct OUT_TYPE##PointQuery : Query<QueryInputIsPosition<DataPoint>, \
51 QueryOutputIs##OUT_TYPE<Index, typename DataPoint::Scalar>> \
52{ \
53 \
54 using Base = Query<QueryInputIsPosition<DataPoint>, QueryOutputIs##OUT_TYPE<Index, typename DataPoint::Scalar>>; \
55 \
56 using Base::Base; \
57};
58
61
63// Base classes
65
68 };
69
81 };
82 };
83
93 template<typename InputType_>
94 struct QueryInput : public QueryInputBase {
96 using InputType = InputType_;
97
99 PONCA_MULTIARCH inline QueryInput(InputType input) : m_input(input) {}
100
102 PONCA_MULTIARCH inline const InputType &input() const { return m_input; }
103 protected:
112 PONCA_MULTIARCH inline void setInput(const InputType& input) { m_input = input; }
113
114 private:
116 InputType m_input;
117 };
118
119
126 template <typename Index>
127 struct QueryInputIsIndex : public QueryInput<Index> {
131 using InputType = typename Base::InputType;
132
134 PONCA_MULTIARCH inline QueryInputIsIndex(const InputType &point = -1)
135 : Base(point) {}
136
138 PONCA_MULTIARCH inline void operator()(const InputType &point = InputType::Zero()){
139 Base::setInput(point);
140 }
141 protected:
143 template <typename IndexType>
144 PONCA_MULTIARCH inline bool skipIndexFunctor(IndexType idx) const {return Base::input() == idx;};
146 template <typename VectorType, typename Container>
147 PONCA_MULTIARCH inline const VectorType getInputPosition(const Container &c)
148 { return c[Base::input()].pos(); }
149 };
150
157 template<typename DataPoint>
158 struct QueryInputIsPosition : public QueryInput<typename DataPoint::VectorType> {
162 using InputType = typename Base::InputType;
163
165 PONCA_MULTIARCH inline QueryInputIsPosition(const InputType &point = InputType::Zero())
166 : Base(point) {}
167
169 PONCA_MULTIARCH inline void operator()(const InputType &point = InputType::Zero()){
170 Base::setInput( point );
171 }
172 protected:
174 template <typename IndexType>
175 PONCA_MULTIARCH inline bool skipIndexFunctor(IndexType idx) const {return false;};
177 template <typename VectorType=typename DataPoint::VectorType, typename Container>
178 PONCA_MULTIARCH inline const VectorType getInputPosition(const Container &)
179 { return Base::input(); }
180 };
181
188 template<typename Index, typename Scalar>
191 using OutputParameter = Scalar;
192
195 : m_squared_radius(PONCA_MULTIARCH_STD_MATH_NAMESPACE(pow)(radius, OutputParameter(2))) {}
196
198 PONCA_MULTIARCH inline void operator() (OutputParameter radius){
199 setRadius( radius );
200 }
201
212 PONCA_MULTIARCH inline Scalar radius() const {
213 PONCA_MULTIARCH_STD_MATH(sqrt);
214 return sqrt(m_squared_radius);
215 }
216
218 PONCA_MULTIARCH inline Scalar squaredRadius() const { return m_squared_radius; }
219
225 PONCA_MULTIARCH inline void setRadius(Scalar radius) {
227 }
228
230 PONCA_MULTIARCH inline void setSquaredRadius(Scalar radius) { m_squared_radius = radius; }
231
232 protected:
234 PONCA_MULTIARCH inline void reset() { }
236 PONCA_MULTIARCH inline Scalar descentDistanceThreshold() const { return m_squared_radius; }
239 };
240
247 template<typename Index, typename Scalar>
251
253 PONCA_MULTIARCH QueryOutputIsNearest() {}
254
256 PONCA_MULTIARCH inline void operator() (){ }
257
259 PONCA_MULTIARCH Index get() const { return m_nearest; }
260
261 protected:
263 PONCA_MULTIARCH void reset() {
264 m_nearest = -1;
265 m_squared_distance = PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits)<Scalar>::max();
266 }
268 PONCA_MULTIARCH inline Scalar descentDistanceThreshold() const { return m_squared_distance; }
269
271 Index m_nearest {-1};
273 Scalar m_squared_distance {PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits)<Scalar>::max()};
274 };
275
281 template<typename Index, typename Scalar>
284 using OutputParameter = Index;
285
287 PONCA_MULTIARCH inline QueryOutputIsKNearest(OutputParameter k = 0) : m_queue(k) {}
288
291
294
295 protected:
297 PONCA_MULTIARCH void reset() {
298 m_queue.clear();
299 m_queue.push({-1, PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits)<Scalar>::max()});
300 }
302 PONCA_MULTIARCH inline Scalar descentDistanceThreshold() const { return m_queue.bottom().squared_distance; }
305 };
306
313 template<typename Input_, typename Output_>
314 struct Query : public Input_, public Output_ {
316 using QueryInType = Input_;
318 using QueryOutType = Output_;
319
320 static_assert(std::is_base_of<QueryInputBase, QueryInType>::value,
321 "QueryInType must inherit Ponca::QueryInputBase");
322 static_assert(std::is_base_of<QueryOutputBase, QueryOutType>::value,
323 "QueryInType must inherit Ponca::QueryInputBase");
324
326 PONCA_MULTIARCH inline Query(const typename QueryInType::InputType &in)
327 : QueryInType(in), QueryOutType() {}
328
330 PONCA_MULTIARCH inline Query(const typename QueryOutType::OutputParameter &outParam,
331 const typename QueryInType::InputType &in)
332 : QueryOutType(outParam), QueryInType(in) {}
333
335 template<typename Base, typename... outputType>
336 PONCA_MULTIARCH inline Base& operator()(const typename QueryInType::InputType &in, outputType&&... out){
337 QueryInType:: operator()(in);
338 QueryOutType::operator()(std::forward<outputType>(out)...);
339 return *((Base*)(this));
340 }
341
343 template<typename Base>
344 PONCA_MULTIARCH inline Base& operator()(const typename QueryInType::InputType &in){
345 QueryInType:: operator()(in);
346 return *((Base*)(this));
347 }
348 };
349
350DECLARE_INDEX_QUERY_CLASS(KNearest) //KNearestIndexQuery
351DECLARE_INDEX_QUERY_CLASS(Nearest) //NearestIndexQuery
352DECLARE_INDEX_QUERY_CLASS(Range) //RangeIndexQuery
353DECLARE_POINT_QUERY_CLASS(KNearest) //KNearestPointQuery
354DECLARE_POINT_QUERY_CLASS(Nearest) //NearestPointQuery
355DECLARE_POINT_QUERY_CLASS(Range) //RangePointQuery
356
358
359#undef DECLARE_INDEX_QUERY_CLASS
360#undef DECLARE_POINT_QUERY_CLASS
361}
The limited_priority_queue class is similar to std::priority_queue but has a limited capacity and han...
QueryInput(InputType input)
Default constructor that initialize the input field.
Definition query.h:99
Index get() const
Get the closest points.
Definition query.h:259
Scalar squaredRadius() const
Generic method to access the radius squared.
Definition query.h:218
void operator()(const InputType &point=InputType::Zero())
Access operator that resets the input point.
Definition query.h:169
Base & operator()(const typename QueryInType::InputType &in)
Access operator that resets the input parameter only.
Definition query.h:344
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:268
QueryOutputIsKNearest(OutputParameter k=0)
Default constructor that initialize the output parameter value.
Definition query.h:287
const VectorType getInputPosition(const Container &)
Generic method to access input position. The VectorType needs to be passed as a template argument.
Definition query.h:178
InputType_ InputType
Alias to the templated input type.
Definition query.h:96
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:236
QueryOutputIsRange(OutputParameter radius=OutputParameter(0))
Default constructor that initialize the output parameter value.
Definition query.h:194
Index OutputParameter
Alias to Output type.
Definition query.h:284
QueryInputIsPosition(const InputType &point=InputType::Zero())
Default constructor that initialize the input position.
Definition query.h:165
limited_priority_queue< IndexSquaredDistance< Index, Scalar > > m_queue
Queue storing the neighbors.
Definition query.h:304
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition query.h:144
typename Base::InputType InputType
Alias to the templated input type (Index)
Definition query.h:162
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:147
Index m_nearest
Index of the nearest neighbor.
Definition query.h:271
void operator()()
Access operator.
Definition query.h:256
Query(const typename QueryOutType::OutputParameter &outParam, const typename QueryInType::InputType &in)
Default constructor that initialize the input and output parameters.
Definition query.h:330
Scalar radius() const
Generic method to access the radius.
Definition query.h:212
Scalar OutputParameter
Alias to Output type.
Definition query.h:191
void reset()
Reset Query for a new search.
Definition query.h:234
typename QueryOutputBase::DummyOutputParameter OutputParameter
Alias to Output type.
Definition query.h:250
Scalar m_squared_radius
Radius used for the search.
Definition query.h:238
void setSquaredRadius(Scalar radius)
Set the squared radius distance of the query.
Definition query.h:230
typename Base::InputType InputType
Alias to the templated input type (Index)
Definition query.h:131
void reset()
Reset Query for a new search.
Definition query.h:297
Base & operator()(const typename QueryInType::InputType &in, outputType &&... out)
Access operator that resets the input and output parameters.
Definition query.h:336
Input_ QueryInType
Alias to the input type.
Definition query.h:316
void setRadius(Scalar radius)
Set the radius distance of the query.
Definition query.h:225
QueryOutputIsNearest()
Default constructor.
Definition query.h:253
void reset()
Reset Query for a new search.
Definition query.h:263
void operator()(OutputParameter k)
Access operator that resets the output parameter.
Definition query.h:290
Output_ QueryOutType
Alias to the output type.
Definition query.h:318
Scalar m_squared_distance
Distance to the nearest neighbor.
Definition query.h:273
limited_priority_queue< IndexSquaredDistance< Index, Scalar > > & queue()
Access to the priority queue storing the neighbors.
Definition query.h:293
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:302
void operator()(OutputParameter radius)
Access operator that resets the output parameter.
Definition query.h:198
const InputType & input() const
Read access to the input.
Definition query.h:102
Query(const typename QueryInType::InputType &in)
Default constructor that initialize the input parameter only.
Definition query.h:326
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition query.h:175
QueryInputIsIndex(const InputType &point=-1)
Default constructor that initialize the input index.
Definition query.h:134
void setInput(const InputType &input)
Edit the input.
Definition query.h:112
void operator()(const InputType &point=InputType::Zero())
Access operator that resets the input index.
Definition query.h:138
Composes the Query object depending on an input type and output type.
Definition query.h:314
Base class for Query input type.
Definition query.h:94
Base class for queries input type.
Definition query.h:67
Extension of QueryInput that handles an index based search, in a partitioning structure.
Definition query.h:127
Extension of QueryInput that handles a position based search, in a partitioning structure.
Definition query.h:158
Base class for queries output types.
Definition query.h:79
Class to construct the knearest queries.
Definition query.h:282
Class to construct the nearest query output.
Definition query.h:248
Class to construct the range query output.
Definition query.h:189
This Source Code Form is subject to the terms of the Mozilla Public License, v.