Ponca  aa50bfdf187919869239c5b44b748842569114c1
Point Cloud Analysis library
Loading...
Searching...
No Matches
primitive.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
11#include "defines.h"
12#include "enums.h"
13#include <Eigen/Dense>
14
15namespace Ponca
16{
17
27template < class DataPoint, class _WFunctor, typename T = void >
29{
30protected:
31 enum {
33 };
34
35public:
36 using Scalar = typename DataPoint::Scalar;
37 using VectorType = typename DataPoint::VectorType;
38 using WFunctor = _WFunctor;
40private:
42 int m_nbNeighbors {0};
43
45 Scalar m_sumW {0};
46
47protected:
48
52
55
56public:
57 /**************************************************************************/
58 /* Initialization */
59 /**************************************************************************/
60 PONCA_FITTING_APIDOC_SETWFUNC
61 PONCA_MULTIARCH inline void setWeightFunc (const WFunctor& _w) {
62 m_w = _w;
63 }
64
65 PONCA_FITTING_APIDOC_INIT
66 PONCA_MULTIARCH inline void init(const VectorType& _basisCenter = VectorType::Zero())
67 {
70 m_w.init( _basisCenter );
71 }
72
76 PONCA_MULTIARCH inline bool isReady() const
77 {
79 }
80
82 PONCA_MULTIARCH inline bool isStable() const { return m_eCurrentState == STABLE; }
83
86 PONCA_MULTIARCH inline bool needAnotherPass() const { return m_eCurrentState == NEED_OTHER_PASS; }
87
89 PONCA_MULTIARCH inline int getNumNeighbors() const { return m_nbNeighbors; }
90
92 PONCA_MULTIARCH inline Scalar getWeightSum() const { return m_sumW; }
93
95 PONCA_MULTIARCH inline void startNewPass() {
96 m_nbNeighbors = 0;
97 m_sumW = Scalar(0);
98 }
99
101 PONCA_MULTIARCH inline const WFunctor& getWeightFunc() const
102 {
103 return m_w;
104 }
105
106
108 PONCA_MULTIARCH inline FIT_RESULT getCurrentState() const
109 {
110 return m_eCurrentState;
111 }
112
113 PONCA_FITTING_APIDOC_ADDNEIGHBOR
114 PONCA_MULTIARCH inline bool addLocalNeighbor(Scalar w, const VectorType &, const DataPoint &) {
115 m_sumW += w;
116 ++(m_nbNeighbors);
117 return true;
118 }
119
120 PONCA_FITTING_APIDOC_FINALIZE
121 PONCA_MULTIARCH inline FIT_RESULT finalize(){
122 // handle specific configurations
123 if (m_sumW == Scalar(0.) || m_nbNeighbors < 1) {
124 return m_eCurrentState = UNDEFINED;
125 }
126 return m_eCurrentState = STABLE;
127 }
128
129}; //class Primitive
130
131
132
133
134
151template < class DataPoint, class _WFunctor, int Type, typename T>
152class PrimitiveDer : public T
153{
154private:
155 typedef T Base;
157protected:
158 enum {
159 Check = Base::PROVIDES_PRIMITIVE_BASE,
160 PROVIDES_PRIMITIVE_DERIVATIVE
161 };
162
163protected:
164 static constexpr int NbDerivatives = ((Type & FitScaleDer) ? 1 : 0 ) + ((Type & FitSpaceDer) ? DataPoint::Dim : 0);
165 static constexpr int DerStorageOrder = (Type & FitSpaceDer) ? Eigen::RowMajor : Eigen::ColMajor;
166
167public:
168 using Scalar = typename Base::Scalar;
169 using VectorType = typename Base::VectorType;
170 using MatrixType = typename DataPoint::MatrixType;
171 using WFunctor = typename Base::WFunctor;
174 typedef Eigen::Matrix<Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder> VectorArray;
175
177 typedef Eigen::Matrix<Scalar, 1, NbDerivatives> ScalarArray;
178
179protected:
180 // computation data
183public:
184
185 /************************************************************************/
186 /* Initialization */
187 /************************************************************************/
189 PONCA_MULTIARCH inline void init(const VectorType &_evalPos)
190 { Base::init(_evalPos); m_dSumW.setZero(); }
191
192 /************************************************************************/
193 /* Processing */
194 /************************************************************************/
197 PONCA_MULTIARCH inline bool addLocalNeighbor(Scalar w,
198 const VectorType &localQ,
199 const DataPoint &attributes,
200 ScalarArray &dw)
201 {
202 if( Base::addLocalNeighbor(w, localQ, attributes) ) {
203 int spaceId = (Type & FitScaleDer) ? 1 : 0;
204 // compute weight
205 if (Type & FitScaleDer)
206 dw[0] = Base::m_w.scaledw(attributes.pos(), attributes);
207
208 if (Type & FitSpaceDer)
209 dw.template segment<int(DataPoint::Dim)>(spaceId) = -Base::m_w.spacedw(attributes.pos(), attributes).transpose();
210
211 m_dSumW += dw;
212 return true;
213 }
214 return false;
215 }
216
217 /**************************************************************************/
218 /* Use results */
219 /**************************************************************************/
221 PONCA_MULTIARCH inline constexpr bool isScaleDer() const {return bool(Type & FitScaleDer);}
223 PONCA_MULTIARCH inline constexpr bool isSpaceDer() const {return bool(Type & FitSpaceDer);}
225 PONCA_MULTIARCH inline constexpr unsigned int derDimension() const { return NbDerivatives;}
226
227};
228
229
230
231}
Primitive base class.
Definition: primitive.h:29
const WFunctor & getWeightFunc() const
Read access to the WeightFunc.
Definition: primitive.h:101
WFunctor m_w
Weight function (must inherits BaseWeightFunc)
Definition: primitive.h:54
FIT_RESULT getCurrentState() const
Definition: primitive.h:108
void startNewPass()
To be called when starting a new processing pass, ie.
Definition: primitive.h:95
Scalar getWeightSum() const
Get the sum of the weights.
Definition: primitive.h:92
FIT_RESULT finalize()
Finalize the procedure.
Definition: primitive.h:121
void setWeightFunc(const WFunctor &_w)
Init the WeightFunc, without changing the other internal states.
Definition: primitive.h:61
_WFunctor WFunctor
Weight Function.
Definition: primitive.h:38
typename DataPoint::Scalar Scalar
Inherited scalar type.
Definition: primitive.h:36
@ PROVIDES_PRIMITIVE_BASE
Provides base API for primitives.
Definition: primitive.h:32
bool addLocalNeighbor(Scalar w, const VectorType &, const DataPoint &)
Add a neighbor to perform the fit.
Definition: primitive.h:114
bool isReady() const
Is the primitive well fitted an ready to use (finalize has been called)
Definition: primitive.h:76
typename DataPoint::VectorType VectorType
Inherited vector type.
Definition: primitive.h:37
int getNumNeighbors() const
Get number of points added in the neighborhood (with non negative weight)
Definition: primitive.h:89
bool needAnotherPass() const
Is another pass required for fitting (finalize has been called and the result is NEED_OTHER_PASS)
Definition: primitive.h:86
bool isStable() const
Is the fitted primitive ready to use (finalize has been called and the result is stable)
Definition: primitive.h:82
FIT_RESULT m_eCurrentState
Represent the current state of the fit (finalize function update the state)
Definition: primitive.h:51
void init(const VectorType &_basisCenter=VectorType::Zero())
Set the evaluation position and reset the internal states.
Definition: primitive.h:66
Generic class performing the Fit derivation.
Definition: primitive.h:153
typename Base::VectorType VectorType
Inherited vector type.
Definition: primitive.h:169
typename DataPoint::MatrixType MatrixType
Inherited matrix type.
Definition: primitive.h:170
Eigen::Matrix< Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder > VectorArray
Static array of scalars with a size adapted to the differentiation type.
Definition: primitive.h:174
Eigen::Matrix< Scalar, 1, NbDerivatives > ScalarArray
Static array of scalars with a size adapted to the differentiation type.
Definition: primitive.h:177
typename Base::WFunctor WFunctor
Weight Function.
Definition: primitive.h:171
typename Base::Scalar Scalar
Inherited scalar type.
Definition: primitive.h:168
constexpr unsigned int derDimension() const
Number of dimensions used for the differentiation.
Definition: primitive.h:225
constexpr bool isSpaceDer() const
State specified at compilation time to differenciate the fit in space.
Definition: primitive.h:223
@ Check
Provides base API for primitives.
Definition: primitive.h:159
constexpr bool isScaleDer() const
State specified at compilation time to differenciate the fit in scale.
Definition: primitive.h:221
ScalarArray m_dSumW
Sum of weight derivatives.
Definition: primitive.h:181
void init(const VectorType &_evalPos)
Definition: primitive.h:189
bool addLocalNeighbor(Scalar w, const VectorType &localQ, const DataPoint &attributes, ScalarArray &dw)
Definition: primitive.h:197
This Source Code Form is subject to the terms of the Mozilla Public License, v.
@ 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