Ponca  3415d6bf4b5de0468067f7a5953b71ec2d1f6564
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 inline QueryInput(InputType input) : m_input(input) {}
100
102 inline const InputType &input() const { return m_input; }
103 protected:
112 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 inline QueryInputIsIndex(const InputType &point = -1)
135 : Base(point) {}
136
138 inline void operator()(const InputType &point = InputType::Zero()){
139 Base::setInput(point);
140 }
141 protected:
143 template <typename IndexType>
144 inline bool skipIndexFunctor(IndexType idx) const {return Base::input() == idx;};
146 template <typename Container>
147 inline auto getInputPosition(const Container &c) -> const typename Container::value_type::VectorType
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 inline QueryInputIsPosition(const InputType &point = InputType::Zero())
166 : Base(point) {}
167
169 inline void operator()(const InputType &point = InputType::Zero()){
170 Base::setInput( point );
171 }
172 protected:
174 template <typename IndexType>
175 inline bool skipIndexFunctor(IndexType idx) const {return false;};
177 template <typename Container>
178 inline auto getInputPosition(const Container &) -> const typename Container::value_type::VectorType
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
199 setRadius( radius );
200 }
201
212 inline Scalar radius() const {
213 PONCA_MULTIARCH_STD_MATH(sqrt);
214 return sqrt(m_squared_radius);
215 }
216
218 inline Scalar squaredRadius() const { return m_squared_radius; }
219
225 inline void setRadius(Scalar radius) {
227 }
228
231
232 protected:
234 inline void reset() { }
236 inline Scalar descentDistanceThreshold() const { return m_squared_radius; }
239 };
240
247 template<typename Index, typename Scalar>
251
254
256 inline void operator() (){ }
257
259 Index get() const { return m_nearest; }
260
261 protected:
263 void reset() {
264 PONCA_MULTIARCH_STD_MATH(numeric_limits);
265 m_nearest = -1;
266 m_squared_distance = numeric_limits<Scalar>::max();
267 }
269 inline Scalar descentDistanceThreshold() const { return m_squared_distance; }
270
272 Index m_nearest {-1};
274 Scalar m_squared_distance {PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits)<Scalar>::max()};
275 };
276
282 template<typename Index, typename Scalar>
285 using OutputParameter = Index;
286
289
292
295
296 protected:
298 void reset() {
299 PONCA_MULTIARCH_STD_MATH(numeric_limits);
300 m_queue.clear();
301 m_queue.push({-1, numeric_limits<Scalar>::max()});
302 }
304 inline Scalar descentDistanceThreshold() const { return m_queue.bottom().squared_distance; }
307 };
308
315 template<typename Input_, typename Output_>
316 struct Query : public Input_, public Output_ {
318 using QueryInType = Input_;
320 using QueryOutType = Output_;
321
322 static_assert(std::is_base_of<QueryInputBase, QueryInType>::value,
323 "QueryInType must inherit Ponca::QueryInputBase");
324 static_assert(std::is_base_of<QueryOutputBase, QueryOutType>::value,
325 "QueryInType must inherit Ponca::QueryInputBase");
326
328 inline Query(const typename QueryInType::InputType &in)
329 : QueryInType(in), QueryOutType() {}
330
332 inline Query(const typename QueryOutType::OutputParameter &outParam,
333 const typename QueryInType::InputType &in)
334 : QueryOutType(outParam), QueryInType(in) {}
335
337 template<typename Base, typename... outputType>
338 inline Base& operator()(const typename QueryInType::InputType &in, outputType&&... out){
339 QueryInType:: operator()(in);
340 QueryOutType::operator()(std::forward<outputType>(out)...);
341 return *((Base*)(this));
342 }
343
345 template<typename Base>
346 inline Base& operator()(const typename QueryInType::InputType &in){
347 QueryInType:: operator()(in);
348 return *((Base*)(this));
349 }
350 };
351
352DECLARE_INDEX_QUERY_CLASS(KNearest) //KNearestIndexQuery
353DECLARE_INDEX_QUERY_CLASS(Nearest) //NearestIndexQuery
354DECLARE_INDEX_QUERY_CLASS(Range) //RangeIndexQuery
355DECLARE_POINT_QUERY_CLASS(KNearest) //KNearestPointQuery
356DECLARE_POINT_QUERY_CLASS(Nearest) //NearestPointQuery
357DECLARE_POINT_QUERY_CLASS(Range) //RangePointQuery
358
360
361#undef DECLARE_INDEX_QUERY_CLASS
362#undef DECLARE_POINT_QUERY_CLASS
363}
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:346
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:269
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:147
QueryOutputIsKNearest(OutputParameter k=0)
Default constructor that initialize the output parameter value.
Definition query.h:288
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:285
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:306
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
Index m_nearest
Index of the nearest neighbor.
Definition query.h:272
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:178
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:332
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:298
Base & operator()(const typename QueryInType::InputType &in, outputType &&... out)
Access operator that resets the input and output parameters.
Definition query.h:338
Input_ QueryInType
Alias to the input type.
Definition query.h:318
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:291
Output_ QueryOutType
Alias to the output type.
Definition query.h:320
Scalar m_squared_distance
Distance to the nearest neighbor.
Definition query.h:274
limited_priority_queue< IndexSquaredDistance< Index, Scalar > > & queue()
Access to the priority queue storing the neighbors.
Definition query.h:294
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:304
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:328
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:316
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:283
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.