Ponca  b63e69866b9b277a96802d3d06e6492d50ffc055
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 _NFilter, typename T = void >
29{
30protected:
31 enum {
33 };
34
35public:
36 using Scalar = typename DataPoint::Scalar;
37 using VectorType = typename DataPoint::VectorType;
38 using NeighborFilter = _NFilter;
40private:
42 int m_nbNeighbors {0};
43
45 Scalar m_sumW {0};
46
48 NeighborFilter m_nFilter;
49
50protected:
51
55
56public:
57 /**************************************************************************/
58 /* Initialization */
59 /**************************************************************************/
60 PONCA_FITTING_APIDOC_SETWFUNC
61 PONCA_MULTIARCH inline void setNeighborFilter (const NeighborFilter& _nFilter) {
62 m_nFilter = _nFilter;
63 }
64
65 PONCA_FITTING_APIDOC_INIT
66 PONCA_MULTIARCH inline void init()
67 {
70 }
71
75 PONCA_MULTIARCH [[nodiscard]] inline bool isReady() const
76 {
78 }
79
81 PONCA_MULTIARCH [[nodiscard]] inline bool isStable() const { return m_eCurrentState == STABLE; }
82
85 PONCA_MULTIARCH [[nodiscard]] inline bool needAnotherPass() const { return m_eCurrentState == NEED_OTHER_PASS; }
86
88 PONCA_MULTIARCH [[nodiscard]] inline int getNumNeighbors() const { return m_nbNeighbors; }
89
91 PONCA_MULTIARCH [[nodiscard]] 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 [[nodiscard]] inline const NeighborFilter& getNeighborFilter() const
101 {
102 return m_nFilter;
103 }
104
105protected:
107 PONCA_MULTIARCH [[nodiscard]] inline NeighborFilter& getNeighborFilter()
108 {
109 return m_nFilter;
110 }
111
112public:
114 PONCA_MULTIARCH [[nodiscard]] inline FIT_RESULT getCurrentState() const
115 {
116 return m_eCurrentState;
117 }
118
119 PONCA_FITTING_APIDOC_ADDNEIGHBOR
120 PONCA_MULTIARCH [[nodiscard]] inline bool addLocalNeighbor(Scalar w, const VectorType &, const DataPoint &) {
121 m_sumW += w;
122 ++(m_nbNeighbors);
123 return true;
124 }
125
126 PONCA_FITTING_APIDOC_FINALIZE
127 PONCA_MULTIARCH inline FIT_RESULT finalize(){
128 // handle specific configurations
129 if (m_sumW == Scalar(0.) || m_nbNeighbors < 1) {
130 return m_eCurrentState = UNDEFINED;
131 }
132 return m_eCurrentState = STABLE;
133 }
134
135}; //class Primitive
136
137
138
139
140
161template < class DataPoint, class _NFilter, int Type, typename T>
162class PrimitiveDer : public T
163{
164 PONCA_FITTING_DECLARE_DEFAULT_TYPES
165 PONCA_FITTING_DECLARE_MATRIX_TYPE
166protected:
167 enum {
168 Check = Base::PROVIDES_PRIMITIVE_BASE,
169 PROVIDES_PRIMITIVE_DERIVATIVE
170 };
171
172protected:
173 static constexpr int NbDerivatives = ((Type & FitScaleDer) ? 1 : 0 ) + ((Type & FitSpaceDer) ? DataPoint::Dim : 0);
174 static constexpr int DerStorageOrder = (Type & FitSpaceDer) ? Eigen::RowMajor : Eigen::ColMajor;
175
176public:
177
179 typedef Eigen::Matrix<Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder> VectorArray;
180
182 typedef Eigen::Matrix<Scalar, 1, NbDerivatives> ScalarArray;
183
184protected:
185 // computation data
188public:
189
190 /************************************************************************/
191 /* Initialization */
192 /************************************************************************/
194 PONCA_MULTIARCH inline void init()
195 { Base::init(); m_dSumW.setZero(); }
196
197 /************************************************************************/
198 /* Processing */
199 /************************************************************************/
202 PONCA_MULTIARCH inline bool addLocalNeighbor(Scalar w,
203 const VectorType &localQ,
204 const DataPoint &attributes,
205 ScalarArray &dw)
206 {
207 if( Base::addLocalNeighbor(w, localQ, attributes) ) { // call the Primitive Fit (without dw)
208 int spaceId = (Type & FitScaleDer) ? 1 : 0;
209 // compute weight
210 if (Type & FitScaleDer)
211 dw[0] = Base::getNeighborFilter().scaledw(attributes.pos(), attributes);
212
213 if (Type & FitSpaceDer)
214 dw.template segment<int(DataPoint::Dim)>(spaceId) = -Base::getNeighborFilter().spacedw(attributes.pos(), attributes).transpose();
215
216 m_dSumW += dw;
217 return true;
218 }
219 return false;
220 }
221
222 /**************************************************************************/
223 /* Use results */
224 /**************************************************************************/
226 PONCA_MULTIARCH [[nodiscard]] inline constexpr bool isScaleDer() const {return bool(Type & FitScaleDer);}
228 PONCA_MULTIARCH [[nodiscard]] inline constexpr bool isSpaceDer() const {return bool(Type & FitSpaceDer);}
230 PONCA_MULTIARCH [[nodiscard]] inline constexpr unsigned int derDimension() const { return NbDerivatives;}
231
232};
233
234
235
236}
Primitive base class.
Definition primitive.h:29
bool isStable() const
Is the fitted primitive ready to use (finalize has been called and the result is stable)
Definition primitive.h:81
typename DataPoint::Scalar Scalar
Inherited scalar type.
Definition primitive.h:36
void startNewPass()
To be called when starting a new processing pass, ie.
Definition primitive.h:94
bool addLocalNeighbor(Scalar w, const VectorType &, const DataPoint &)
Add a neighbor to perform the fit.
Definition primitive.h:120
void setNeighborFilter(const NeighborFilter &_nFilter)
Init the WeightFunc, without changing the other internal states.
Definition primitive.h:61
bool isReady() const
Is the primitive well fitted and ready to use (finalize has been called) ?
Definition primitive.h:75
_NFilter NeighborFilter
Filter applied on each neighbor.
Definition primitive.h:38
Scalar getWeightSum() const
Get the sum of the weights.
Definition primitive.h:91
@ PROVIDES_PRIMITIVE_BASE
Provides base API for primitives.
Definition primitive.h:32
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
NeighborFilter & getNeighborFilter()
Write access to the NeighborFilter.
Definition primitive.h:107
FIT_RESULT finalize()
Finalize the procedure.
Definition primitive.h:127
const NeighborFilter & getNeighborFilter() const
Read access to the NeighborFilter.
Definition primitive.h:100
typename DataPoint::VectorType VectorType
Inherited vector type.
Definition primitive.h:37
FIT_RESULT m_eCurrentState
Represent the current state of the fit (finalize function update the state)
Definition primitive.h:54
FIT_RESULT getCurrentState() const
Definition primitive.h:114
bool needAnotherPass() const
Is another pass required for fitting (finalize has been called and the result is NEED_OTHER_PASS)
Definition primitive.h:85
Generic class performing the Fit derivation.
Definition primitive.h:163
constexpr bool isSpaceDer() const
State specified at compilation time to compute derivatives in space.
Definition primitive.h:228
Eigen::Matrix< Scalar, 1, NbDerivatives > ScalarArray
Static array of scalars with a size adapted to the differentiation type.
Definition primitive.h:182
ScalarArray m_dSumW
Sum of weight derivatives.
Definition primitive.h:186
constexpr bool isScaleDer() const
State specified at compilation time to compute derivatives in scale.
Definition primitive.h:226
bool addLocalNeighbor(Scalar w, const VectorType &localQ, const DataPoint &attributes, ScalarArray &dw)
Definition primitive.h:202
typename DataPoint::Scalar Scalar
Alias to scalar type.
Definition primitive.h:164
typename Base::VectorType VectorType
Alias to vector type.
Definition primitive.h:164
@ Check
Provides base API for primitives.
Definition primitive.h:168
Eigen::Matrix< Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder > VectorArray
Static array of scalars with a size adapted to the differentiation type.
Definition primitive.h:179
constexpr unsigned int derDimension() const
Number of dimensions used for the differentiation.
Definition primitive.h:230
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