Ponca  f7f7d7cc113b4e53be6bee25005fbdbe7e016293
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()
67 {
70 }
71
75 PONCA_MULTIARCH inline bool isReady() const
76 {
78 }
79
81 PONCA_MULTIARCH inline bool isStable() const { return m_eCurrentState == STABLE; }
82
85 PONCA_MULTIARCH inline bool needAnotherPass() const { return m_eCurrentState == NEED_OTHER_PASS; }
86
88 PONCA_MULTIARCH inline int getNumNeighbors() const { return m_nbNeighbors; }
89
91 PONCA_MULTIARCH inline Scalar getWeightSum() const { return m_sumW; }
92
94 PONCA_MULTIARCH inline void startNewPass() {
95 m_nbNeighbors = 0;
96 m_sumW = Scalar(0);
97 }
98
100 PONCA_MULTIARCH inline const WFunctor& getWeightFunc() const
101 {
102 return m_w;
103 }
104
105
107 PONCA_MULTIARCH inline FIT_RESULT getCurrentState() const
108 {
109 return m_eCurrentState;
110 }
111
112 PONCA_FITTING_APIDOC_ADDNEIGHBOR
113 PONCA_MULTIARCH inline bool addLocalNeighbor(Scalar w, const VectorType &, const DataPoint &) {
114 m_sumW += w;
115 ++(m_nbNeighbors);
116 return true;
117 }
118
119 PONCA_FITTING_APIDOC_FINALIZE
120 PONCA_MULTIARCH inline FIT_RESULT finalize(){
121 // handle specific configurations
122 if (m_sumW == Scalar(0.) || m_nbNeighbors < 1) {
123 return m_eCurrentState = UNDEFINED;
124 }
125 return m_eCurrentState = STABLE;
126 }
127
128}; //class Primitive
129
130
131
132
133
150template < class DataPoint, class _WFunctor, int Type, typename T>
151class PrimitiveDer : public T
152{
153private:
154 typedef T Base;
156protected:
157 enum {
158 Check = Base::PROVIDES_PRIMITIVE_BASE,
159 PROVIDES_PRIMITIVE_DERIVATIVE
160 };
161
162protected:
163 static constexpr int NbDerivatives = ((Type & FitScaleDer) ? 1 : 0 ) + ((Type & FitSpaceDer) ? DataPoint::Dim : 0);
164 static constexpr int DerStorageOrder = (Type & FitSpaceDer) ? Eigen::RowMajor : Eigen::ColMajor;
165
166public:
167 using Scalar = typename Base::Scalar;
168 using VectorType = typename Base::VectorType;
169 using MatrixType = typename DataPoint::MatrixType;
170 using WFunctor = typename Base::WFunctor;
173 typedef Eigen::Matrix<Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder> VectorArray;
174
176 typedef Eigen::Matrix<Scalar, 1, NbDerivatives> ScalarArray;
177
178protected:
179 // computation data
182public:
183
184 /************************************************************************/
185 /* Initialization */
186 /************************************************************************/
188 PONCA_MULTIARCH inline void init()
189 { Base::init(); m_dSumW.setZero(); }
190
191 /************************************************************************/
192 /* Processing */
193 /************************************************************************/
196 PONCA_MULTIARCH inline bool addLocalNeighbor(Scalar w,
197 const VectorType &localQ,
198 const DataPoint &attributes,
199 ScalarArray &dw)
200 {
201 if( Base::addLocalNeighbor(w, localQ, attributes) ) {
202 int spaceId = (Type & FitScaleDer) ? 1 : 0;
203 // compute weight
204 if (Type & FitScaleDer)
205 dw[0] = Base::m_w.scaledw(attributes.pos(), attributes);
206
207 if (Type & FitSpaceDer)
208 dw.template segment<int(DataPoint::Dim)>(spaceId) = -Base::m_w.spacedw(attributes.pos(), attributes).transpose();
209
210 m_dSumW += dw;
211 return true;
212 }
213 return false;
214 }
215
216 /**************************************************************************/
217 /* Use results */
218 /**************************************************************************/
220 PONCA_MULTIARCH inline constexpr bool isScaleDer() const {return bool(Type & FitScaleDer);}
222 PONCA_MULTIARCH inline constexpr bool isSpaceDer() const {return bool(Type & FitSpaceDer);}
224 PONCA_MULTIARCH inline constexpr unsigned int derDimension() const { return NbDerivatives;}
225
226};
227
228
229
230}
Primitive base class.
Definition primitive.h:29
const WFunctor & getWeightFunc() const
Read access to the WeightFunc.
Definition primitive.h:100
WFunctor m_w
Weight function (must inherits BaseWeightFunc)
Definition primitive.h:54
FIT_RESULT getCurrentState() const
Definition primitive.h:107
void startNewPass()
To be called when starting a new processing pass, ie.
Definition primitive.h:94
Scalar getWeightSum() const
Get the sum of the weights.
Definition primitive.h:91
FIT_RESULT finalize()
Finalize the procedure.
Definition primitive.h:120
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:113
bool isReady() const
Is the primitive well fitted and ready to use (finalize has been called) ?
Definition primitive.h:75
typename DataPoint::VectorType VectorType
Inherited vector type.
Definition primitive.h:37
void init()
Set the evaluation position and reset the internal states.
Definition primitive.h:66
int getNumNeighbors() const
Get number of points added in the neighborhood (with non negative weight)
Definition primitive.h:88
bool needAnotherPass() const
Is another pass required for fitting (finalize has been called and the result is NEED_OTHER_PASS)
Definition primitive.h:85
bool isStable() const
Is the fitted primitive ready to use (finalize has been called and the result is stable)
Definition primitive.h:81
FIT_RESULT m_eCurrentState
Represent the current state of the fit (finalize function update the state)
Definition primitive.h:51
Generic class performing the Fit derivation.
Definition primitive.h:152
typename Base::VectorType VectorType
Inherited vector type.
Definition primitive.h:168
typename DataPoint::MatrixType MatrixType
Inherited matrix type.
Definition primitive.h:169
Eigen::Matrix< Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder > VectorArray
Static array of scalars with a size adapted to the differentiation type.
Definition primitive.h:173
Eigen::Matrix< Scalar, 1, NbDerivatives > ScalarArray
Static array of scalars with a size adapted to the differentiation type.
Definition primitive.h:176
typename Base::WFunctor WFunctor
Weight Function.
Definition primitive.h:170
typename Base::Scalar Scalar
Inherited scalar type.
Definition primitive.h:167
constexpr unsigned int derDimension() const
Number of dimensions used for the differentiation.
Definition primitive.h:224
constexpr bool isSpaceDer() const
State specified at compilation time to differenciate the fit in space.
Definition primitive.h:222
@ Check
Provides base API for primitives.
Definition primitive.h:158
constexpr bool isScaleDer() const
State specified at compilation time to differenciate the fit in scale.
Definition primitive.h:220
ScalarArray m_dSumW
Sum of weight derivatives.
Definition primitive.h:180
bool addLocalNeighbor(Scalar w, const VectorType &localQ, const DataPoint &attributes, ScalarArray &dw)
Definition primitive.h:196
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