Ponca  292d8d7132745e0d4242ff9a01e93697fc96b204
Point Cloud Analysis library
Loading...
Searching...
No Matches
basketUnit.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#include "../defines.h"
11#include "concepts.h"
12
13#include <Eigen/Dense>
14
15namespace Ponca
16{
17
25 template <class DataPoint, class _NFilter, typename T = void>
27 {
28 public:
29 using Scalar = typename DataPoint::Scalar;
30 using VectorType = typename DataPoint::VectorType;
31 using NeighborFilter = _NFilter;
32 using NeighborFrame = typename NeighborFilter::NeighborhoodFrame;
33
34 private:
36 int m_nbNeighbors{0};
37
39 Scalar m_sumW{0};
40
41 protected:
44
48
49 public:
50 /**************************************************************************/
51 /* Initialization */
52 /**************************************************************************/
53 PONCA_FITTING_APIDOC_SETWFUNC
54 PONCA_MULTIARCH inline bool setNeighborFilter(const NeighborFilter& _nFilter)
55 {
56 m_nFilter = _nFilter;
57 return true;
58 }
59
60 PONCA_FITTING_APIDOC_INIT
61 PONCA_MULTIARCH inline void init()
62 {
65 }
66
71 PONCA_MULTIARCH [[nodiscard]] inline bool isReady() const
72 {
74 }
75
77 PONCA_MULTIARCH [[nodiscard]] inline bool isStable() const { return m_eCurrentState == STABLE; }
78
81 PONCA_MULTIARCH [[nodiscard]] inline bool needAnotherPass() const { return m_eCurrentState == NEED_OTHER_PASS; }
82
84 PONCA_MULTIARCH [[nodiscard]] inline int getNumNeighbors() const { return m_nbNeighbors; }
85
87 PONCA_MULTIARCH [[nodiscard]] inline Scalar getWeightSum() const { return m_sumW; }
88
90 PONCA_MULTIARCH inline void startNewPass()
91 {
92 m_nbNeighbors = 0;
93 m_sumW = Scalar(0);
94 }
95
97 PONCA_MULTIARCH inline const NeighborFilter& getNeighborFilter() const { return m_nFilter; }
98
99 PONCA_MULTIARCH inline NeighborFrame& getNeighborFrame() { return m_nFilter.frame(); }
100 PONCA_MULTIARCH inline const NeighborFrame& getNeighborFrame() const { return m_nFilter.frame(); }
101
102 PONCA_MULTIARCH inline bool operator==(const BasketUnitBase<DataPoint, NeighborFilter>& other) const
103 {
104 // End of equality recursion.
105
106 // Not checking filter here, we don't have an api for this, are often parameterless, and
107 // the sum of weight is already a good approx once the fit is complete.
108 return m_nbNeighbors == other.m_nbNeighbors && internal::isApprox(m_sumW, other.m_sumW) &&
109 m_eCurrentState == other.m_eCurrentState;
110 }
111
112 public:
114 PONCA_MULTIARCH [[nodiscard]] inline FIT_RESULT getCurrentState() const { return m_eCurrentState; }
115
116 PONCA_FITTING_APIDOC_ADDNEIGHBOR
117 PONCA_MULTIARCH inline void addLocalNeighbor(Scalar w, const VectorType&, const DataPoint&)
118 {
119 m_sumW += w;
120 ++(m_nbNeighbors);
121 }
122
123 PONCA_FITTING_APIDOC_FINALIZE
124 PONCA_MULTIARCH [[nodiscard]] inline FIT_RESULT finalize()
125 {
126 // handle specific configurations
127 if (m_sumW == Scalar(0.) || m_nbNeighbors < 1)
128 {
129 return m_eCurrentState = UNDEFINED;
130 }
131 return m_eCurrentState = STABLE;
132 }
133
134 }; // class BasketUnit
135
154 template <class DataPoint, class _NFilter, int Type, typename T>
156 class BasketDiffUnitBase : public T
157 {
158 PONCA_FITTING_DECLARE_DEFAULT_TYPES
159 PONCA_FITTING_DECLARE_MATRIX_TYPE
160 protected:
161 static constexpr int NbDerivatives =
162 ((Type & FitScaleDer) ? 1 : 0) + ((Type & FitSpaceDer) ? DataPoint::Dim : 0);
163 static constexpr int DerStorageOrder = (Type & FitSpaceDer) ? Eigen::RowMajor : Eigen::ColMajor;
164
165 public:
167 using VectorArray = Eigen::Matrix<Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder>;
168
170 using ScalarArray = Eigen::Matrix<Scalar, 1, NbDerivatives>;
171
172 protected:
173 // computation data
174 ScalarArray m_dSumW{ScalarArray::Zero()};
175
176 public:
177 /************************************************************************/
178 /* Initialization */
179 /************************************************************************/
181 PONCA_MULTIARCH inline void init()
182 {
183 Base::init();
184 m_dSumW.setZero();
185 }
186
187 /************************************************************************/
188 /* Processing */
189 /************************************************************************/
192 PONCA_MULTIARCH inline void addLocalNeighbor(Scalar w, const VectorType& localQ, const DataPoint& attributes,
193 ScalarArray& dw)
194 {
195 Base::addLocalNeighbor(w, localQ, attributes);
196 // call the Primitive Fit (without dw)
197 int spaceId = (Type & FitScaleDer) ? 1 : 0;
198 // compute weight
199 if (Type & FitScaleDer)
200 dw[0] = Base::getNeighborFilter().scaledw(attributes.pos(), attributes);
201
202 if (Type & FitSpaceDer)
203 dw.template segment<int(DataPoint::Dim)>(spaceId) =
204 -Base::getNeighborFilter().spacedw(attributes.pos(), attributes).transpose();
205
206 m_dSumW += dw;
207 }
208
209 /**************************************************************************/
210 /* Use results */
211 /**************************************************************************/
213 PONCA_MULTIARCH [[nodiscard]] static inline constexpr bool isScaleDer() { return bool(Type & FitScaleDer); }
215 PONCA_MULTIARCH [[nodiscard]] static inline constexpr bool isSpaceDer() { return bool(Type & FitSpaceDer); }
217 PONCA_MULTIARCH [[nodiscard]] static inline constexpr unsigned int derDimension() { return NbDerivatives; }
218
219 PONCA_MULTIARCH inline bool operator==(const BasketDiffUnitBase<DataPoint, _NFilter, Type, T>& other) const
220 {
221 return Base::operator==(other) && internal::isApprox(m_dSumW, other.m_dSumW);
222 }
223 };
224
225} // namespace Ponca
Base class of any computation unit of a BasketDiff.
Definition basketUnit.h:157
Eigen::Matrix< Scalar, DataPoint::Dim, NbDerivatives, DerStorageOrder > VectorArray
Static array of scalars with a size adapted to the differentiation type.
Definition basketUnit.h:167
void addLocalNeighbor(Scalar w, const VectorType &localQ, const DataPoint &attributes, ScalarArray &dw)
Definition basketUnit.h:192
static constexpr bool isSpaceDer()
State specified at compilation time to compute derivatives in space.
Definition basketUnit.h:215
static constexpr bool isScaleDer()
State specified at compilation time to compute derivatives in scale.
Definition basketUnit.h:213
typename DataPoint::Scalar Scalar
Alias to scalar type.
Definition basketUnit.h:158
typename Base::VectorType VectorType
Alias to vector type.
Definition basketUnit.h:158
Eigen::Matrix< Scalar, 1, NbDerivatives > ScalarArray
Static array of scalars with a size adapted to the differentiation type.
Definition basketUnit.h:170
ScalarArray m_dSumW
Sum of weight derivatives.
Definition basketUnit.h:174
static constexpr unsigned int derDimension()
Number of dimensions used for the differentiation.
Definition basketUnit.h:217
Base class of any computation unit of a Basket.
Definition basketUnit.h:27
Scalar getWeightSum() const
Get the sum of the weights.
Definition basketUnit.h:87
int getNumNeighbors() const
Get number of points added in the neighborhood (with positive weight).
Definition basketUnit.h:84
bool isStable() const
Is the fitted primitive ready to use (finalize has been called and the result is stable).
Definition basketUnit.h:77
void addLocalNeighbor(Scalar w, const VectorType &, const DataPoint &)
Add a neighbor to perform the fit.
Definition basketUnit.h:117
const NeighborFilter & getNeighborFilter() const
Read access to the NeighborFilter.
Definition basketUnit.h:97
void init()
Set the evaluation position and reset the internal states.
Definition basketUnit.h:61
bool needAnotherPass() const
Is another pass required for fitting (finalize has been called and the result is NEED_OTHER_PASS).
Definition basketUnit.h:81
typename DataPoint::VectorType VectorType
Inherited vector type.
Definition basketUnit.h:30
NeighborFilter m_nFilter
Neighborhood filter.
Definition basketUnit.h:43
FIT_RESULT getCurrentState() const
Definition basketUnit.h:114
void startNewPass()
To be called when starting a new processing pass, ie.
Definition basketUnit.h:90
bool isReady() const
Is the primitive well-fitted and ready to use (finalize has been called) ?
Definition basketUnit.h:71
typename DataPoint::Scalar Scalar
Inherited scalar type.
Definition basketUnit.h:29
_NFilter NeighborFilter
Filter applied on each neighbor.
Definition basketUnit.h:31
FIT_RESULT m_eCurrentState
Represent the current state of the fit (finalize function update the state).
Definition basketUnit.h:47
This concept ensures that the default types and accessors in a Basket are well-formed.
Definition concepts.h:43
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11
@ 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