Ponca  eb7239a9c07e156fb1edc6faac7fe89e2b256bda
Point Cloud Analysis library
Loading...
Searching...
No Matches
basketUnit.h
1/*
2 Copyright (C) 2014 Nicolas Mellado <nmellado0@gmail.com>
3
4 This Source Code Form is subject to the terms of the Mozilla Public
5 License, v. 2.0. If a copy of the MPL was not distributed with this
6 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7*/
8
9#pragma once
10#include "../defines.h"
11#include "concepts.h"
12
13#include <Eigen/Dense>
14
15namespace Ponca
16{
17
25 template <class DataPoint, class _NFilter, typename T = void>
27 {
28 public:
29 using Scalar = typename DataPoint::Scalar;
30 using VectorType = typename DataPoint::VectorType;
31 using NeighborFilter = _NFilter;
32 using NeighborFrame = typename NeighborFilter::NeighborhoodFrame;
33
34 private:
36 int m_nbNeighbors{0};
37
39 Scalar m_sumW{0};
40
41 protected:
44
48
49 public:
50 /**************************************************************************/
51 /* Initialization */
52 /**************************************************************************/
53 PONCA_FITTING_APIDOC_SETWFUNC
54 PONCA_MULTIARCH inline bool setNeighborFilter(const NeighborFilter& _nFilter)
55 {
56 m_nFilter = _nFilter;
57 return true;
58 }
59
60 PONCA_FITTING_APIDOC_INIT
61 PONCA_MULTIARCH inline void init()
62 {
65 }
66
71 PONCA_MULTIARCH [[nodiscard]] inline bool isReady() const
72 {
74 }
75
77 PONCA_MULTIARCH [[nodiscard]] inline bool isStable() const { return m_eCurrentState == STABLE; }
78
81 PONCA_MULTIARCH [[nodiscard]] inline bool needAnotherPass() const { return m_eCurrentState == NEED_OTHER_PASS; }
82
84 PONCA_MULTIARCH [[nodiscard]] inline int getNumNeighbors() const { return m_nbNeighbors; }
85
87 PONCA_MULTIARCH [[nodiscard]] inline Scalar getWeightSum() const { return m_sumW; }
88
90 PONCA_MULTIARCH inline void startNewPass()
91 {
92 m_nbNeighbors = 0;
93 m_sumW = Scalar(0);
94 }
95
97 PONCA_MULTIARCH inline const NeighborFilter& getNeighborFilter() const { return m_nFilter; }
98
99 PONCA_MULTIARCH inline NeighborFrame& getNeighborFrame() { return m_nFilter.frame(); }
100 PONCA_MULTIARCH inline const NeighborFrame& getNeighborFrame() const { return m_nFilter.frame(); }
101
102 public:
104 PONCA_MULTIARCH [[nodiscard]] inline FIT_RESULT getCurrentState() const { return m_eCurrentState; }
105
106 PONCA_FITTING_APIDOC_ADDNEIGHBOR
107 PONCA_MULTIARCH inline void addLocalNeighbor(Scalar w, const VectorType&, const DataPoint&)
108 {
109 m_sumW += w;
110 ++(m_nbNeighbors);
111 }
112
113 PONCA_FITTING_APIDOC_FINALIZE
114 PONCA_MULTIARCH inline FIT_RESULT finalize()
115 {
116 // handle specific configurations
117 if (m_sumW == Scalar(0.) || m_nbNeighbors < 1)
118 {
119 return m_eCurrentState = UNDEFINED;
120 }
121 return m_eCurrentState = STABLE;
122 }
123
124 }; // class BasketUnit
125
144 template <class DataPoint, class _NFilter, int Type, typename T>
146 class BasketDiffUnitBase : public T
147 {
148 PONCA_FITTING_DECLARE_DEFAULT_TYPES
149 PONCA_FITTING_DECLARE_MATRIX_TYPE
150 protected:
151 static constexpr int NbDerivatives =
152 ((Type & FitScaleDer) ? 1 : 0) + ((Type & FitSpaceDer) ? DataPoint::Dim : 0);
153 static constexpr int DerStorageOrder = (Type & FitSpaceDer) ? Eigen::RowMajor : Eigen::ColMajor;
154
155 public:
157 using VectorArray = Eigen::Matrix<Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder>;
158
160 using ScalarArray = Eigen::Matrix<Scalar, 1, NbDerivatives>;
161
162 protected:
163 // computation data
165
166 public:
167 /************************************************************************/
168 /* Initialization */
169 /************************************************************************/
171 PONCA_MULTIARCH inline void init()
172 {
173 Base::init();
174 m_dSumW.setZero();
175 }
176
177 /************************************************************************/
178 /* Processing */
179 /************************************************************************/
182 PONCA_MULTIARCH inline void addLocalNeighbor(Scalar w, const VectorType& localQ, const DataPoint& attributes,
183 ScalarArray& dw)
184 {
185 Base::addLocalNeighbor(w, localQ, attributes);
186 // call the Primitive Fit (without dw)
187 int spaceId = (Type & FitScaleDer) ? 1 : 0;
188 // compute weight
189 if (Type & FitScaleDer)
190 dw[0] = Base::getNeighborFilter().scaledw(attributes.pos(), attributes);
191
192 if (Type & FitSpaceDer)
193 dw.template segment<int(DataPoint::Dim)>(spaceId) =
194 -Base::getNeighborFilter().spacedw(attributes.pos(), attributes).transpose();
195
196 m_dSumW += dw;
197 }
198
199 /**************************************************************************/
200 /* Use results */
201 /**************************************************************************/
203 PONCA_MULTIARCH [[nodiscard]] static inline constexpr bool isScaleDer() { return bool(Type & FitScaleDer); }
205 PONCA_MULTIARCH [[nodiscard]] static inline constexpr bool isSpaceDer() { return bool(Type & FitSpaceDer); }
207 PONCA_MULTIARCH [[nodiscard]] static inline constexpr unsigned int derDimension() { return NbDerivatives; }
208 };
209
210} // namespace Ponca
Base class of any computation unit of a BasketDiff.
Definition basketUnit.h:147
Eigen::Matrix< Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder > VectorArray
Static array of scalars with a size adapted to the differentiation type.
Definition basketUnit.h:157
void addLocalNeighbor(Scalar w, const VectorType &localQ, const DataPoint &attributes, ScalarArray &dw)
Definition basketUnit.h:182
static constexpr bool isSpaceDer()
State specified at compilation time to compute derivatives in space.
Definition basketUnit.h:205
static constexpr bool isScaleDer()
State specified at compilation time to compute derivatives in scale.
Definition basketUnit.h:203
typename DataPoint::Scalar Scalar
Alias to scalar type.
Definition basketUnit.h:148
typename Base::VectorType VectorType
Alias to vector type.
Definition basketUnit.h:148
Eigen::Matrix< Scalar, 1, NbDerivatives > ScalarArray
Static array of scalars with a size adapted to the differentiation type.
Definition basketUnit.h:160
ScalarArray m_dSumW
Sum of weight derivatives.
Definition basketUnit.h:164
static constexpr unsigned int derDimension()
Number of dimensions used for the differentiation.
Definition basketUnit.h:207
Base class of any computation unit of a Basket.
Definition basketUnit.h:27
Scalar getWeightSum() const
Get the sum of the weights.
Definition basketUnit.h:87
int getNumNeighbors() const
Get number of points added in the neighborhood (with positive weight).
Definition basketUnit.h:84
bool isStable() const
Is the fitted primitive ready to use (finalize has been called and the result is stable).
Definition basketUnit.h:77
void addLocalNeighbor(Scalar w, const VectorType &, const DataPoint &)
Add a neighbor to perform the fit.
Definition basketUnit.h:107
const NeighborFilter & getNeighborFilter() const
Read access to the NeighborFilter.
Definition basketUnit.h:97
void init()
Set the evaluation position and reset the internal states.
Definition basketUnit.h:61
bool needAnotherPass() const
Is another pass required for fitting (finalize has been called and the result is NEED_OTHER_PASS).
Definition basketUnit.h:81
typename DataPoint::VectorType VectorType
Inherited vector type.
Definition basketUnit.h:30
NeighborFilter m_nFilter
Neighborhood filter.
Definition basketUnit.h:43
FIT_RESULT getCurrentState() const
Definition basketUnit.h:104
void startNewPass()
To be called when starting a new processing pass, ie.
Definition basketUnit.h:90
bool isReady() const
Is the primitive well-fitted and ready to use (finalize has been called) ?
Definition basketUnit.h:71
typename DataPoint::Scalar Scalar
Inherited scalar type.
Definition basketUnit.h:29
_NFilter NeighborFilter
Filter applied on each neighbor.
Definition basketUnit.h:31
FIT_RESULT m_eCurrentState
Represent the current state of the fit (finalize function update the state).
Definition basketUnit.h:47
This concept ensures that the default types and accessors in a Basket are well-formed.
Definition concepts.h:43
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11
@ FitSpaceDer
Flag indicating a space differentiation.
Definition enums.h:36
@ FitScaleDer
Flag indicating a scale differentiation.
Definition enums.h:35
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
@ NEED_OTHER_PASS
The fitting procedure needs to analyse the neighborhood another time.
Definition enums.h:25
@ STABLE
The fitting is stable and ready to use.
Definition enums.h:17
@ UNSTABLE
The fitting is ready to use but it is considered as unstable (if the number of neighbors is low for e...
Definition enums.h:20