Ponca  bab7704293a2c36e5bed9dea40def7ba839bfe08
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
27 template <class DataPoint, class _NFilter, typename T = void>
29 {
30 protected:
31 enum
32 {
34 };
35
36 public:
37 using Scalar = typename DataPoint::Scalar;
38 using VectorType = typename DataPoint::VectorType;
39 using NeighborFilter = _NFilter;
41 private:
43 int m_nbNeighbors{0};
44
46 Scalar m_sumW{0};
47
48 protected:
51
55
56 public:
57 /**************************************************************************/
58 /* Initialization */
59 /**************************************************************************/
60 PONCA_FITTING_APIDOC_SETWFUNC
61 PONCA_MULTIARCH inline void setNeighborFilter(const NeighborFilter& _nFilter) { m_nFilter = _nFilter; }
62
63 PONCA_FITTING_APIDOC_INIT
64 PONCA_MULTIARCH inline void init()
65 {
68 }
69
73 PONCA_MULTIARCH [[nodiscard]] inline bool isReady() const
74 {
76 }
77
79 PONCA_MULTIARCH [[nodiscard]] inline bool isStable() const { return m_eCurrentState == STABLE; }
80
83 PONCA_MULTIARCH [[nodiscard]] inline bool needAnotherPass() const { return m_eCurrentState == NEED_OTHER_PASS; }
84
86 PONCA_MULTIARCH [[nodiscard]] inline int getNumNeighbors() const { return m_nbNeighbors; }
87
89 PONCA_MULTIARCH [[nodiscard]] inline Scalar getWeightSum() const { return m_sumW; }
90
92 PONCA_MULTIARCH inline void startNewPass()
93 {
94 m_nbNeighbors = 0;
95 m_sumW = Scalar(0);
96 }
97
99 PONCA_MULTIARCH inline const NeighborFilter& getNeighborFilter() const { return m_nFilter; }
100
101 public:
103 PONCA_MULTIARCH [[nodiscard]] inline FIT_RESULT getCurrentState() const { return m_eCurrentState; }
104
105 PONCA_FITTING_APIDOC_ADDNEIGHBOR
106 PONCA_MULTIARCH inline void addLocalNeighbor(Scalar w, const VectorType&, const DataPoint&)
107 {
108 m_sumW += w;
109 ++(m_nbNeighbors);
110 }
111
112 PONCA_FITTING_APIDOC_FINALIZE
113 PONCA_MULTIARCH inline FIT_RESULT finalize()
114 {
115 // handle specific configurations
116 if (m_sumW == Scalar(0.) || m_nbNeighbors < 1)
117 {
118 return m_eCurrentState = UNDEFINED;
119 }
120 return m_eCurrentState = STABLE;
121 }
122
123 }; // class Primitive
124
145 template <class DataPoint, class _NFilter, int Type, typename T>
146 class PrimitiveDer : public T
147 {
148 PONCA_FITTING_DECLARE_DEFAULT_TYPES
149 PONCA_FITTING_DECLARE_MATRIX_TYPE
150 protected:
151 enum
152 {
153 Check = Base::PROVIDES_PRIMITIVE_BASE,
154 PROVIDES_PRIMITIVE_DERIVATIVE
155 };
156
157 protected:
158 static constexpr int NbDerivatives =
159 ((Type & FitScaleDer) ? 1 : 0) + ((Type & FitSpaceDer) ? DataPoint::Dim : 0);
160 static constexpr int DerStorageOrder = (Type & FitSpaceDer) ? Eigen::RowMajor : Eigen::ColMajor;
161
162 public:
164 typedef Eigen::Matrix<Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder> VectorArray;
165
167 typedef Eigen::Matrix<Scalar, 1, NbDerivatives> ScalarArray;
168
169 protected:
170 // computation data
173 public:
174 /************************************************************************/
175 /* Initialization */
176 /************************************************************************/
178 PONCA_MULTIARCH inline void init()
179 {
180 Base::init();
181 m_dSumW.setZero();
182 }
183
184 /************************************************************************/
185 /* Processing */
186 /************************************************************************/
189 PONCA_MULTIARCH inline void addLocalNeighbor(Scalar w, const VectorType& localQ, const DataPoint& attributes,
190 ScalarArray& dw)
191 {
192 Base::addLocalNeighbor(w, localQ, attributes);
193 // call the Primitive Fit (without dw)
194 int spaceId = (Type & FitScaleDer) ? 1 : 0;
195 // compute weight
196 if (Type & FitScaleDer)
197 dw[0] = Base::getNeighborFilter().scaledw(attributes.pos(), attributes);
198
199 if (Type & FitSpaceDer)
200 dw.template segment<int(DataPoint::Dim)>(spaceId) =
201 -Base::getNeighborFilter().spacedw(attributes.pos(), attributes).transpose();
202
203 m_dSumW += dw;
204 }
205
206 /**************************************************************************/
207 /* Use results */
208 /**************************************************************************/
210 PONCA_MULTIARCH [[nodiscard]] static inline constexpr bool isScaleDer() { return bool(Type & FitScaleDer); }
212 PONCA_MULTIARCH [[nodiscard]] static inline constexpr bool isSpaceDer() { return bool(Type & FitSpaceDer); }
214 PONCA_MULTIARCH [[nodiscard]] static inline constexpr unsigned int derDimension() { return NbDerivatives; }
215 };
216
217} // namespace Ponca
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:79
typename DataPoint::Scalar Scalar
Inherited scalar type.
Definition primitive.h:37
void startNewPass()
To be called when starting a new processing pass, ie.
Definition primitive.h:92
@ PROVIDES_PRIMITIVE_BASE
Provides base API for primitives.
Definition primitive.h:33
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:73
_NFilter NeighborFilter
Filter applied on each neighbor.
Definition primitive.h:39
Scalar getWeightSum() const
Get the sum of the weights.
Definition primitive.h:89
void init()
Set the evaluation position and reset the internal states.
Definition primitive.h:64
int getNumNeighbors() const
Get number of points added in the neighborhood (with non negative weight)
Definition primitive.h:86
FIT_RESULT finalize()
Finalize the procedure.
Definition primitive.h:113
const NeighborFilter & getNeighborFilter() const
Read access to the NeighborFilter.
Definition primitive.h:99
typename DataPoint::VectorType VectorType
Inherited vector type.
Definition primitive.h:38
NeighborFilter m_nFilter
Neighborhood filter.
Definition primitive.h:50
void addLocalNeighbor(Scalar w, const VectorType &, const DataPoint &)
Add a neighbor to perform the fit.
Definition primitive.h:106
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:103
bool needAnotherPass() const
Is another pass required for fitting (finalize has been called and the result is NEED_OTHER_PASS)
Definition primitive.h:83
Generic class performing the Fit derivation.
Definition primitive.h:147
static constexpr bool isScaleDer()
State specified at compilation time to compute derivatives in scale.
Definition primitive.h:210
Eigen::Matrix< Scalar, 1, NbDerivatives > ScalarArray
Static array of scalars with a size adapted to the differentiation type.
Definition primitive.h:167
ScalarArray m_dSumW
Sum of weight derivatives.
Definition primitive.h:171
static constexpr bool isSpaceDer()
State specified at compilation time to compute derivatives in space.
Definition primitive.h:212
@ Check
Provides base API for primitives.
Definition primitive.h:153
typename DataPoint::Scalar Scalar
Alias to scalar type.
Definition primitive.h:148
typename Base::VectorType VectorType
Alias to vector type.
Definition primitive.h:148
static constexpr unsigned int derDimension()
Number of dimensions used for the differentiation.
Definition primitive.h:214
void addLocalNeighbor(Scalar w, const VectorType &localQ, const DataPoint &attributes, ScalarArray &dw)
Definition primitive.h:189
Eigen::Matrix< Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder > VectorArray
Static array of scalars with a size adapted to the differentiation type.
Definition primitive.h:164
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