Ponca  f14040391a1f2ce7ea3cc77346b41db82171c6db
Point Cloud Analysis library
Loading...
Searching...
No Matches
basket.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 "enums.h"
11#include "primitive.h"
12
13#include PONCA_MULTIARCH_INCLUDE_STD(iterator)
14
15namespace Ponca
16{
17
18#define BSKW typename BasketType::WeightFunction
19#define BSKP typename BasketType::DataPoint
20
21#ifndef PARSED_WITH_DOXYGEN
23namespace internal
24{
25 template <class P, class W,
26 typename Aggregate,
27 template <class, class, typename> class Ext,
28 template <class, class, typename> class... Exts>
29 struct BasketAggregateImpl
30 {
31 using type = typename BasketAggregateImpl<P, W, Ext<P, W, Aggregate>, Exts...>::type;
32 };
33
34 template <class P, class W,
35 typename Aggregate,
36 template <class, class, typename> class Ext>
37 struct BasketAggregateImpl<P, W, Aggregate, Ext>
38 {
39 using type = Ext<P, W, Aggregate>;
40 };
41
43 template <class P, class W,
44 template <class, class, typename> class... Exts>
45 struct BasketAggregate : BasketAggregateImpl<P, W, PrimitiveBase<P, W>, Exts...>
46 {
47 };
48
49 template <typename BasketType, int Type,
50 typename Aggregate,
51 template <class, class, int, typename> class Ext,
52 template <class, class, int, typename> class... Exts>
53 struct BasketDiffAggregateImpl
54 {
55 using type = typename BasketDiffAggregateImpl<BasketType, Type, Ext<BSKP, BSKW, Type, Aggregate>, Exts...>::type;
56 };
57
58 template <typename BasketType, int Type,
59 typename Aggregate,
60 template <class, class, int, typename> class Ext>
61 struct BasketDiffAggregateImpl<BasketType, Type, Aggregate, Ext>
62 {
63 using type = Ext<BSKP, BSKW, Type, Aggregate>;
64 };
65
67 template <typename BasketType, int Type,
68 template <class, class, int, typename> class... Exts>
69 struct BasketDiffAggregate : BasketDiffAggregateImpl<BasketType, Type, BasketType, PrimitiveDer, Exts...>
70 {
71 };
72}
73#endif
74
81 template <typename Derived>
83 protected:
86 Derived& derived() { return static_cast<Derived&>(*this); }
87 public:
88
89#ifdef PONCA_CPU_ARCH
94 template <typename Container>
95 FIT_RESULT compute(const Container& c) {
96 return derived().compute(std::begin(c), std::end(c));
97 }
98#endif
99
104 template <typename IteratorBegin, typename IteratorEnd>
105 PONCA_MULTIARCH inline FIT_RESULT compute(const IteratorBegin& begin, const IteratorEnd& end) { return UNDEFINED; };
106
112 template <typename IndexRange, typename PointContainer>
113 PONCA_MULTIARCH inline FIT_RESULT computeWithIds(IndexRange /*ids*/, const PointContainer& /*points*/) { return UNDEFINED; };
114 }; // struct ComputeObject
115
116#define WRITE_COMPUTE_FUNCTIONS \
117 \
118 \
119 \
120 \
121 template <typename IteratorBegin, typename IteratorEnd> \
122 PONCA_MULTIARCH inline \
123 FIT_RESULT compute(const IteratorBegin& begin, const IteratorEnd& end){ \
124 Base::init(); \
125 FIT_RESULT res = UNDEFINED; \
126 do { \
127 Self::startNewPass(); \
128 for (auto it = begin; it != end; ++it){ \
129 Self::addNeighbor(*it); \
130 } \
131 res = Base::finalize(); \
132 } while ( res == NEED_OTHER_PASS ); \
133 return res; \
134 } \
135 \
136 \
137 \
138 \
139 \
140 template <typename IndexRange, typename PointContainer> \
141 PONCA_MULTIARCH inline \
142 FIT_RESULT computeWithIds(IndexRange ids, const PointContainer& points){ \
143 this->init(); \
144 FIT_RESULT res = UNDEFINED; \
145 do { \
146 Self::startNewPass(); \
147 for (const auto& i : ids){ \
148 this->addNeighbor(points[i]); \
149 } \
150 res = this->finalize(); \
151 } while ( res == NEED_OTHER_PASS ); \
152 return res; \
153 }
154
180 template <typename BasketType, int Type,
181 template <class, class, int, typename> class Ext0,
182 template <class, class, int, typename> class... Exts>
183 class BasketDiff : public ComputeObject<BasketDiff<BasketType, Type, Ext0, Exts...>>,
184 public internal::BasketDiffAggregate<BasketType, Type, Ext0, Exts...>::type {
185 private:
186 using Self = BasketDiff;
187 public:
189 using Base = typename internal::BasketDiffAggregate<BasketType, Type, Ext0, Exts...>::type;
191 using WeightFunction = BSKW;
193 using DataPoint = BSKP;
195 using Scalar = typename DataPoint::Scalar;
196
197 using ComputeObject<Self>::compute; // Make the default compute accessible
198 WRITE_COMPUTE_FUNCTIONS
199
201 PONCA_MULTIARCH inline bool addNeighbor(const DataPoint &_nei) {
202 // compute weight
203 auto wres = Base::m_w.w(_nei.pos(), _nei);
204 typename Base::ScalarArray dw;
205
206 if (wres.first > Scalar(0.)) {
207 Base::addLocalNeighbor(wres.first, wres.second, _nei, dw);
208 return true;
209 }
210 return false;
211 }
212};
213
239 template <class P, class W,
240 template <class, class, typename> class Ext0,
241 template <class, class, typename> class... Exts>
242 class Basket : public ComputeObject<Basket<P, W, Ext0, Exts...>>,
243 public internal::BasketAggregate<P, W, Ext0, Exts...>::type
244 {
245 private:
246 using Self = Basket;
247 public:
249 using Base = typename internal::BasketAggregate<P, W, Ext0, Exts...>::type;
251 using Scalar = typename P::Scalar;
253 using DataPoint = P;
255 using WeightFunction = W;
256
257 using ComputeObject<Self>::compute; // Make the default compute accessible
258 WRITE_COMPUTE_FUNCTIONS;
259
266 PONCA_MULTIARCH inline bool addNeighbor(const DataPoint &_nei) {
267 // compute weight
268 auto wres = Base::m_w.w(_nei.pos(), _nei);
269
270 if (wres.first > Scalar(0.)) {
271 Base::addLocalNeighbor(wres.first, wres.second, _nei);
272 return true;
273 }
274 return false;
275 }
276 }; // class Basket
277
278} //namespace Ponca
279
Aggregator class used to declare specialized structures with derivatives computations,...
Definition basket.h:184
typename BasketType::DataPoint DataPoint
Point type used for computation.
Definition basket.h:193
typename internal::BasketDiffAggregate< BasketType, Type, Ext0, Exts... >::type Base
Base type, which aggregates all the computational objects using the CRTP.
Definition basket.h:189
FIT_RESULT compute(const IteratorBegin &begin, const IteratorEnd &end)
Convenience function for STL-like iterators
Definition basket.h:198
bool addNeighbor(const DataPoint &_nei)
Add a neighbor to perform the fit.
Definition basket.h:201
typename DataPoint::Scalar Scalar
Scalar type used for computation, as defined from Basket.
Definition basket.h:195
typename BasketType::WeightFunction WeightFunction
Weighting function.
Definition basket.h:191
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:244
W WeightFunction
Weighting function.
Definition basket.h:255
FIT_RESULT compute(const IteratorBegin &begin, const IteratorEnd &end)
Convenience function for STL-like iterators
Definition basket.h:258
P DataPoint
Point type used for computation.
Definition basket.h:253
typename P::Scalar Scalar
Scalar type used for computation, as defined from template parameter P
Definition basket.h:251
bool addNeighbor(const DataPoint &_nei)
Add a neighbor to perform the fit.
Definition basket.h:266
typename internal::BasketAggregate< P, W, Ext0, Exts... >::type Base
Base type, which aggregates all the computational objects using the CRTP.
Definition basket.h:249
This Source Code Form is subject to the terms of the Mozilla Public License, v.
FIT_RESULT
Enum corresponding to the state of a fitting method (and what the finalize function returns)
Definition enums.h:15
@ UNDEFINED
The fitting is undefined, you can't use it for valid results.
Definition enums.h:22
ComputeObject is a virtual object that represents an algorithm which can be used with the compute fun...
Definition basket.h:82
FIT_RESULT computeWithIds(IndexRange, const PointContainer &)
Convenience function to iterate over a subset of samples in a PointContainer.
Definition basket.h:113
FIT_RESULT compute(const IteratorBegin &begin, const IteratorEnd &end)
Convenience function for STL-like iterators.
Definition basket.h:105
FIT_RESULT compute(const Container &c)
Convenience function for STL-like container
Definition basket.h:95
Derived & derived()
Retrieve the top layer object Returns a reference to the derived class so that we can use its overwri...
Definition basket.h:86