Ponca  40f245e28b920cbb763a1c6282156c87c626f24c
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 \
24template <typename Index, typename Scalar> \
25struct OUT_TYPE##IndexQuery : Query<QueryInputIsIndex<Index>, QueryOutputIs##OUT_TYPE<Index, Scalar>> \
26{ \
27 using Base = Query<QueryInputIsIndex<Index>, QueryOutputIs##OUT_TYPE<Index, Scalar>>; \
28 using Base::Base; \
29};
30
31
35#define DECLARE_POINT_QUERY_CLASS(OUT_TYPE) \
36 \
37template <typename Index, typename DataPoint> \
38struct OUT_TYPE##PointQuery : Query<QueryInputIsPosition<DataPoint>, \
39 QueryOutputIs##OUT_TYPE<Index, typename DataPoint::Scalar>> \
40{ \
41 using Base = Query<QueryInputIsPosition<DataPoint>, QueryOutputIs##OUT_TYPE<Index, typename DataPoint::Scalar>>; \
42 using Base::Base; \
43};
44
47
49// Base classes
51
54 };
55
59 };
60 };
61
63 template<typename InputType_>
64 struct QueryInput : public QueryInputBase {
65 using InputType = InputType_;
66
67 inline QueryInput(InputType input) : m_input(input) {}
68
69 inline const InputType &input() const { return m_input; }
70 protected:
75 inline void editInput(const InputType& input) { m_input = input; }
76
77 private:
79 const InputType m_input;
80 };
81
82
84 template <typename Index>
85 struct QueryInputIsIndex : public QueryInput<Index> {
86 using Base = QueryInput<Index>;
87 using InputType = typename Base::InputType;
88
89 inline QueryInputIsIndex(const InputType &point = -1)
90 : Base(point) {}
91 protected:
93 template <typename IndexType>
94 inline bool skipIndexFunctor(IndexType idx) const {return Base::input() == idx;};
96 template <typename Container>
97 inline auto getInputPosition(const Container &c) -> const typename Container::value_type::VectorType
98 { return c[Base::input()].pos(); }
99 };
100
102 template<typename DataPoint>
103 struct QueryInputIsPosition : public QueryInput<typename DataPoint::VectorType> {
105 using InputType = typename Base::InputType;
106
107 inline QueryInputIsPosition(const InputType &point = InputType::Zero())
108 : Base(point) {}
109 protected:
111 template <typename IndexType>
112 inline bool skipIndexFunctor(IndexType idx) const {return false;};
114 template <typename Container>
115 inline auto getInputPosition(const Container &) -> const typename Container::value_type::VectorType
116 { return Base::input(); }
117 };
118
120 template<typename Index, typename Scalar>
122 using OutputParameter = Scalar;
123
124 inline QueryOutputIsRange(OutputParameter radius = OutputParameter(0))
125 : m_squared_radius(PONCA_MULTIARCH_CU_STD_NAMESPACE(pow)(radius, OutputParameter(2))) {}
126
127 inline Scalar radius() const {
128 PONCA_MULTIARCH_STD_MATH(sqrt);
129 return sqrt(m_squared_radius);
130 }
131
132 inline Scalar squared_radius() const { return m_squared_radius; }
133
134 inline void set_radius(Scalar radius) {
135 PONCA_MULTIARCH_STD_MATH(pow);
136 m_squared_radius = pow(radius, 2);
137 }
138
139 inline void set_squared_radius(Scalar radius) { m_squared_radius = radius; }
140
141 protected:
143 inline void reset() { }
145 inline Scalar descentDistanceThreshold() const { return m_squared_radius; }
146 Scalar m_squared_radius{0};
147 };
148
150 template<typename Index, typename Scalar>
152 using OutputParameter = typename QueryOutputBase::DummyOutputParameter;
153
155
156 Index get() const { return m_nearest; }
157
158 protected:
160 void reset() {
161 PONCA_MULTIARCH_STD_MATH(numeric_limits);
162 m_nearest = -1;
163 m_squared_distance = numeric_limits<Scalar>::max();
164 }
166 inline Scalar descentDistanceThreshold() const { return m_squared_distance; }
167
168 Index m_nearest {-1};
169 Scalar m_squared_distance {PONCA_MULTIARCH_CU_STD_NAMESPACE(numeric_limits)<Scalar>::max()};
170 };
171
173 template<typename Index, typename Scalar>
175 using OutputParameter = Index;
176
177 inline QueryOutputIsKNearest(OutputParameter k = 0) : m_queue(k) {}
178
179 inline limited_priority_queue<IndexSquaredDistance<Index, Scalar>> &queue() { return m_queue; }
180
181 protected:
183 void reset() {
184 PONCA_MULTIARCH_STD_MATH(numeric_limits);
185 m_queue.clear();
186 m_queue.push({-1, numeric_limits<Scalar>::max()});
187 }
189 inline Scalar descentDistanceThreshold() const { return m_queue.bottom().squared_distance; }
191 };
192
193
194 template<typename Input_, typename Output_>
195 struct Query : public Input_, public Output_ {
196 using QueryInType = Input_;
197 using QueryOutType = Output_;
198
199 static_assert(std::is_base_of<QueryInputBase, QueryInType>::value,
200 "QueryInType must inherit Ponca::QueryInputBase");
201 static_assert(std::is_base_of<QueryOutputBase, QueryOutType>::value,
202 "QueryInType must inherit Ponca::QueryInputBase");
203
204 inline Query(const typename QueryInType::InputType &in)
205 : QueryInType(in), QueryOutType() {}
206
207 inline Query(const typename QueryOutType::OutputParameter &outParam,
208 const typename QueryInType::InputType &in)
209 : QueryInType(in), QueryOutType(outParam) {}
210 };
211
212DECLARE_INDEX_QUERY_CLASS(KNearest) //KNearestIndexQuery
213DECLARE_INDEX_QUERY_CLASS(Nearest) //NearestIndexQuery
214DECLARE_INDEX_QUERY_CLASS(Range) //RangeIndexQuery
215DECLARE_POINT_QUERY_CLASS(KNearest) //KNearestPointQuery
216DECLARE_POINT_QUERY_CLASS(Nearest) //NearestPointQuery
217DECLARE_POINT_QUERY_CLASS(Range) //RangePointQuery
218
220
221#undef DECLARE_INDEX_QUERY_CLASS
222#undef DECLARE_POINT_QUERY_CLASS
223}
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:166
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:97
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:75
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:145
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition query.h:94
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:115
void reset()
Reset Query for a new search.
Definition query.h:143
void reset()
Reset Query for a new search.
Definition query.h:183
void reset()
Reset Query for a new search.
Definition query.h:160
Scalar descentDistanceThreshold() const
Distance threshold used during tree descent to select nodes to explore.
Definition query.h:189
bool skipIndexFunctor(IndexType idx) const
Functor used to check if a given Idx must be skipped.
Definition query.h:112
Base class for typed queries input type.
Definition query.h:64
Base class for queries input type.
Definition query.h:53
Base class for queries storing points.
Definition query.h:85
Base class for queries storing points.
Definition query.h:103
Base class for queries output type.
Definition query.h:57
Base class for knearest queries.
Definition query.h:174
Base class for nearest queries.
Definition query.h:151
Base class for range queries.
Definition query.h:121
This Source Code Form is subject to the terms of the Mozilla Public License, v.