Ponca  40f245e28b920cbb763a1c6282156c87c626f24c
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
154template < class DataPoint, class _WFunctor, int Type, typename T>
155class PrimitiveDer : public T
156{
157private:
158 typedef T Base;
160protected:
161 enum {
162 Check = Base::PROVIDES_PRIMITIVE_BASE,
163 PROVIDES_PRIMITIVE_DERIVATIVE
164 };
165
166protected:
167 static constexpr int NbDerivatives = ((Type & FitScaleDer) ? 1 : 0 ) + ((Type & FitSpaceDer) ? DataPoint::Dim : 0);
168 static constexpr int DerStorageOrder = (Type & FitSpaceDer) ? Eigen::RowMajor : Eigen::ColMajor;
169
170public:
171 using Scalar = typename Base::Scalar;
172 using VectorType = typename Base::VectorType;
173 using MatrixType = typename DataPoint::MatrixType;
174 using WFunctor = typename Base::WFunctor;
177 typedef Eigen::Matrix<Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder> VectorArray;
178
180 typedef Eigen::Matrix<Scalar, 1, NbDerivatives> ScalarArray;
181
182protected:
183 // computation data
186public:
187
188 /************************************************************************/
189 /* Initialization */
190 /************************************************************************/
192 PONCA_MULTIARCH inline void init()
193 { Base::init(); m_dSumW.setZero(); }
194
195 /************************************************************************/
196 /* Processing */
197 /************************************************************************/
200 PONCA_MULTIARCH inline bool addLocalNeighbor(Scalar w,
201 const VectorType &localQ,
202 const DataPoint &attributes,
203 ScalarArray &dw)
204 {
205 if( Base::addLocalNeighbor(w, localQ, attributes) ) { // call the Primitive Fit (without dw)
206 int spaceId = (Type & FitScaleDer) ? 1 : 0;
207 // compute weight
208 if (Type & FitScaleDer)
209 dw[0] = Base::m_w.scaledw(attributes.pos(), attributes);
210
211 if (Type & FitSpaceDer)
212 dw.template segment<int(DataPoint::Dim)>(spaceId) = -Base::m_w.spacedw(attributes.pos(), attributes).transpose();
213
214 m_dSumW += dw;
215 return true;
216 }
217 return false;
218 }
219
220 /**************************************************************************/
221 /* Use results */
222 /**************************************************************************/
224 PONCA_MULTIARCH inline constexpr bool isScaleDer() const {return bool(Type & FitScaleDer);}
226 PONCA_MULTIARCH inline constexpr bool isSpaceDer() const {return bool(Type & FitSpaceDer);}
228 PONCA_MULTIARCH inline constexpr unsigned int derDimension() const { return NbDerivatives;}
229
230};
231
232
233
234}
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:156
typename Base::VectorType VectorType
Inherited vector type.
Definition primitive.h:172
typename DataPoint::MatrixType MatrixType
Inherited matrix type.
Definition primitive.h:173
Eigen::Matrix< Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder > VectorArray
Static array of scalars with a size adapted to the differentiation type.
Definition primitive.h:177
Eigen::Matrix< Scalar, 1, NbDerivatives > ScalarArray
Static array of scalars with a size adapted to the differentiation type.
Definition primitive.h:180
typename Base::WFunctor WFunctor
Weight Function.
Definition primitive.h:174
typename Base::Scalar Scalar
Inherited scalar type.
Definition primitive.h:171
constexpr unsigned int derDimension() const
Number of dimensions used for the differentiation.
Definition primitive.h:228
constexpr bool isSpaceDer() const
State specified at compilation time to differenciate the fit in space.
Definition primitive.h:226
@ Check
Provides base API for primitives.
Definition primitive.h:162
constexpr bool isScaleDer() const
State specified at compilation time to differenciate the fit in scale.
Definition primitive.h:224
ScalarArray m_dSumW
Sum of weight derivatives.
Definition primitive.h:184
bool addLocalNeighbor(Scalar w, const VectorType &localQ, const DataPoint &attributes, ScalarArray &dw)
Definition primitive.h:200
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