Ponca  7d8ac87a7de01d881c9fde3c42e397b44bffb901
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 \
29 \
30 template <typename Index, typename Scalar> \
31 struct OUT_TYPE##IndexQuery : Query<QueryInputIsIndex<Index>, QueryOutputIs##OUT_TYPE<Index, Scalar>> \
32 { \
33 \
34 using Base = Query<QueryInputIsIndex<Index>, QueryOutputIs##OUT_TYPE<Index, Scalar>>; \
35 \
36 using Base::Base; \
37 };
38
42#define DECLARE_POINT_QUERY_CLASS(OUT_TYPE) \
43 \
44 \
45 \
46 \
47 \
49 \
50 template <typename Index, typename DataPoint> \
51 struct OUT_TYPE##PointQuery \
52 : Query<QueryInputIsPosition<DataPoint>, QueryOutputIs##OUT_TYPE<Index, typename DataPoint::Scalar>> \
53 { \
54 \
55 using Base = \
56 Query<QueryInputIsPosition<DataPoint>, QueryOutputIs##OUT_TYPE<Index, typename DataPoint::Scalar>>; \
57 \
58 using Base::Base; \
59 };
60
63
65 // Base classes
69 struct QueryInputBase
70 {
71 };
72
85 {
86 };
87 };
88
98 template <typename InputType_>
99 struct QueryInput : public QueryInputBase
102 using InputType = InputType_;
105 PONCA_MULTIARCH inline QueryInput(InputType input) : m_input(input) {}
108 PONCA_MULTIARCH inline const InputType& input() const { return m_input; }
109
110 protected:
119 PONCA_MULTIARCH inline void setInput(const InputType& input) { m_input = input; }
120
121 private:
123 InputType m_input;
124 };
125
132 template <typename Index>
133 struct QueryInputIsIndex : public QueryInput<Index>
138 using InputType = typename Base::InputType;
141 PONCA_MULTIARCH inline QueryInputIsIndex(const InputType& point = -1) : Base(point) {}
144 PONCA_MULTIARCH inline void operator()(const InputType& point = InputType::Zero()) { Base::setInput(point); }
145
146 protected:
148 template <typename IndexType>
149 PONCA_MULTIARCH inline bool skipIndexFunctor(IndexType idx) const
150 {
151 return Base::input() == idx;
152 };
154 template <typename VectorType, typename Container>
155 PONCA_MULTIARCH inline const VectorType getInputPosition(const Container& c)
156 {
157 return c[Base::input()].pos();
158 }
159 };
160
167 template <typename DataPoint>
168 struct QueryInputIsPosition : public QueryInput<typename DataPoint::VectorType>
173 using InputType = typename Base::InputType;
176 PONCA_MULTIARCH inline QueryInputIsPosition(const InputType& point = InputType::Zero()) : Base(point) {}
179 PONCA_MULTIARCH inline void operator()(const InputType& point = InputType::Zero()) { Base::setInput(point); }
180
181 protected:
183 template <typename IndexType>
184 PONCA_MULTIARCH inline bool skipIndexFunctor(IndexType idx) const
185 {
186 return false;
187 };
189 template <typename VectorType = typename DataPoint::VectorType, typename Container>
190 PONCA_MULTIARCH inline const VectorType getInputPosition(const Container&)
191 {
192 return Base::input();
193 }
194 };
195
202 template <typename Index, typename Scalar>
203 struct QueryOutputIsRange : public QueryOutputBase
206 using OutputParameter = Scalar;
209 PONCA_MULTIARCH inline QueryOutputIsRange(OutputParameter radius = OutputParameter(0))
210 : m_squared_radius(PONCA_MULTIARCH_STD_MATH_NAMESPACE(pow)(radius, OutputParameter(2)))
211 {
212 }
215 PONCA_MULTIARCH inline void operator()(OutputParameter radius) { setRadius(radius); }
216
227 PONCA_MULTIARCH inline Scalar radius() const
228 {
229 PONCA_MULTIARCH_STD_MATH(sqrt);
230 return sqrt(m_squared_radius);
231 }
234 PONCA_MULTIARCH inline Scalar squaredRadius() const { return m_squared_radius; }
235
241 PONCA_MULTIARCH inline void setRadius(Scalar radius) { setSquaredRadius(radius * radius); }
244 PONCA_MULTIARCH inline void setSquaredRadius(Scalar radius) { m_squared_radius = radius; }
245
246 protected:
248 PONCA_MULTIARCH inline void reset() {}
250 PONCA_MULTIARCH inline Scalar descentDistanceThreshold() const { return m_squared_radius; }
252 Scalar m_squared_radius{0};
253 };
254
261 template <typename Index, typename Scalar>
262 struct QueryOutputIsNearest : public QueryOutputBase
268 PONCA_MULTIARCH QueryOutputIsNearest() {}
271 PONCA_MULTIARCH inline void operator()() {}
274 PONCA_MULTIARCH Index get() const { return m_nearest; }
275
276 protected:
278 PONCA_MULTIARCH void reset()
279 {
280 m_nearest = -1;
281 m_squared_distance = PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits)<Scalar>::max();
284 PONCA_MULTIARCH inline Scalar descentDistanceThreshold() const { return m_squared_distance; }
287 Index m_nearest{-1};
289 Scalar m_squared_distance{PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits) < Scalar > ::max()};
290 };
291
297 template <typename Index, typename Scalar>
298 struct QueryOutputIsKNearest : public QueryOutputBase
301 using OutputParameter = Index;
304 PONCA_MULTIARCH inline QueryOutputIsKNearest(OutputParameter k = 0) : m_queue(k) {}
307 PONCA_MULTIARCH inline void operator()(OutputParameter k)
308 {
310 }
314
315 protected:
317 PONCA_MULTIARCH void reset()
318 {
319 m_queue.clear();
320 m_queue.push({-1, PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits) < Scalar > ::max()});
323 PONCA_MULTIARCH inline Scalar descentDistanceThreshold() const { return m_queue.bottom().squared_distance; }
326 };
327
334 template <typename Input_, typename Output_>
335 struct Query : public Input_, public Output_
338 using QueryInType = Input_;
340 using QueryOutType = Output_;
341
342 static_assert(std::is_base_of<QueryInputBase, QueryInType>::value,
343 "QueryInType must inherit Ponca::QueryInputBase");
344 static_assert(std::is_base_of<QueryOutputBase, QueryOutType>::value,
345 "QueryInType must inherit Ponca::QueryInputBase");
348 PONCA_MULTIARCH inline Query(const typename QueryInType::InputType& in) : QueryInType(in), QueryOutType() {}
351 PONCA_MULTIARCH inline Query(const typename QueryOutType::OutputParameter& outParam,
352 const typename QueryInType::InputType& in)
354 {
355 }
356
358 template <typename Base, typename... outputType>
359 PONCA_MULTIARCH inline Base& operator()(const typename QueryInType::InputType& in, outputType&&... out)
360 {
361 QueryInType::operator()(in);
362 QueryOutType::operator()(std::forward<outputType>(out)...);
363 return *((Base*)(this));
364 }
365
367 template <typename Base>
368 PONCA_MULTIARCH inline Base& operator()(const typename QueryInType::InputType& in)
369 {
370 QueryInType::operator()(in);
371 return *((Base*)(this));
372 }
373 };
375 DECLARE_INDEX_QUERY_CLASS(KNearest) // KNearestIndexQuery
376 DECLARE_INDEX_QUERY_CLASS(Nearest) // NearestIndexQuery
377 DECLARE_INDEX_QUERY_CLASS(Range) // RangeIndexQuery
378 DECLARE_POINT_QUERY_CLASS(KNearest) // KNearestPointQuery
379 DECLARE_POINT_QUERY_CLASS(Nearest) // NearestPointQuery
380 DECLARE_POINT_QUERY_CLASS(Range) // RangePointQuery
381
383
384#undef DECLARE_INDEX_QUERY_CLASS
385#undef DECLARE_POINT_QUERY_CLASS
386} // namespace Ponca
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:318
Index get() const
Get the closest points.
Definition query.h:272
Scalar squaredRadius() const
Generic method to access the radius squared.
Definition query.h:232
void operator()(const InputType &point=InputType::Zero())
Access operator that resets the input point.
Definition query.h:177
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:282
const VectorType getInputPosition(const Container &)
Generic method to access input position. The VectorType needs to be passed as a template argument.
Definition query.h:188
InputType_ InputType
Alias to the templated input type.
Definition query.h:100
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:248
Index OutputParameter
Alias to Output type.
Definition query.h:299
limited_priority_queue< IndexSquaredDistance< Index, Scalar > > m_queue
Queue storing the neighbors.
Definition query.h:323
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition query.h:147
typename Base::InputType InputType
Alias to the templated input type (Index)
Definition query.h:171
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:153
Index m_nearest
Index of the nearest neighbor.
Definition query.h:285
void operator()()
Access operator.
Definition query.h:269
Scalar radius() const
Generic method to access the radius.
Definition query.h:225
Scalar OutputParameter
Alias to Output type.
Definition query.h:204
void reset()
Reset Query for a new search.
Definition query.h:246
typename QueryOutputBase::DummyOutputParameter OutputParameter
Alias to Output type.
Definition query.h:263
Scalar m_squared_radius
Radius used for the search.
Definition query.h:250
void setSquaredRadius(Scalar radius)
Set the squared radius distance of the query.
Definition query.h:242
typename Base::InputType InputType
Alias to the templated input type (Index)
Definition query.h:136
void reset()
Reset Query for a new search.
Definition query.h:315
Base & operator()(const typename QueryInType::InputType &in, outputType &&... out)
Access operator that resets the input and output parameters.
Definition query.h:357
Input_ QueryInType
Alias to the input type.
Definition query.h:336
void setRadius(Scalar radius)
Set the radius distance of the query.
Definition query.h:239
QueryOutputIsNearest()
Default constructor.
Definition query.h:266
void reset()
Reset Query for a new search.
Definition query.h:276
void operator()(OutputParameter k)
Access operator that resets the output parameter.
Definition query.h:305
Output_ QueryOutType
Alias to the output type.
Definition query.h:338
Scalar m_squared_distance
Distance to the nearest neighbor.
Definition query.h:287
limited_priority_queue< IndexSquaredDistance< Index, Scalar > > & queue()
Access to the priority queue storing the neighbors.
Definition query.h:311
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:321
void operator()(OutputParameter radius)
Access operator that resets the output parameter.
Definition query.h:213
const InputType & input() const
Read access to the input.
Definition query.h:106
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition query.h:182
void setInput(const InputType &input)
Edit the input.
Definition query.h:117
void operator()(const InputType &point=InputType::Zero())
Access operator that resets the input index.
Definition query.h:142
Composes the Query object depending on an input type and output type.
Definition query.h:334
Base class for Query input type.
Definition query.h:98
Base class for queries input type.
Definition query.h:68
Extension of QueryInput that handles an index based search, in a partitioning structure.
Definition query.h:132
Extension of QueryInput that handles a position based search, in a partitioning structure.
Definition query.h:167
Base class for queries output types.
Definition query.h:81
Class to construct the knearest queries.
Definition query.h:297
Class to construct the range query output.
Definition query.h:202
This Source Code Form is subject to the terms of the Mozilla Public License, v.