Ponca  292d8d7132745e0d4242ff9a01e93697fc96b204
Point Cloud Analysis library
Loading...
Searching...
No Matches
algebraicSphere.h
1/*
2 This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6
7#pragma once
8
9#include "../../defines.h"
10#include "../concepts.h"
11#include "../../Filters/concepts.h" // HasLocalFrame
12
13#include PONCA_MULTIARCH_INCLUDE_STD(cmath)
14#include PONCA_MULTIARCH_INCLUDE_CU_STD(limits)
15
16#include <Eigen/Core>
17
18#define ALGEBRAIC_SPHERE_REQUIREMENTS ProvidesBasketUnitBase<T>&& HasLocalFrame<_NFilter>
19
20namespace Ponca
21{
32
35 by the evaluation position, the public methods involving a query (such as
36 project, potential, gradient) have to be defined in global
37 coordinates (e.g. you don't need to convert your query in the current locale
38 frame).
39
40 This primitive satisfies: ProvidesAlgebraicSphere
41
42 \todo Deal with planar case (_uq == 0) and what about _ul == 0 ?
43 */
45 template <class DataPoint, class _NFilter, typename T>
46 requires ALGEBRAIC_SPHERE_REQUIREMENTS
47 class AlgebraicSphere : public T
48 {
49 PONCA_FITTING_DECLARE_DEFAULT_TYPES
50
51 protected:
53 bool m_isNormalized{false};
54
55 // results
57 m_uq{0};
58 VectorType m_ul{VectorType::Zero()};
59
60 public:
61 PONCA_EXPLICIT_BASKET_CAST_OPERATORS(AlgebraicSphere, algebraicSphere)
62 PONCA_EXPLICIT_BASKET_CAST_OPERATORS(AlgebraicSphere, implicitPrimitive)
63 PONCA_EXPLICIT_BASKET_CAST_OPERATORS(AlgebraicSphere, projectionOperator)
64 PONCA_EXPLICIT_BASKET_CAST_OPERATORS(AlgebraicSphere, meanCurvature)
65
70 PONCA_MULTIARCH inline void init()
71 {
72 Base::init();
73
74 m_uc = Scalar(0);
75 m_ul = VectorType::Zero();
76 m_uq = Scalar(0);
77
78 m_isNormalized = false;
79 }
80
84 PONCA_MULTIARCH [[nodiscard]] inline bool isValid() const
85 {
86 return !(m_ul.isApprox(VectorType::Zero()) && m_uc == Scalar(0) && m_uq == Scalar(0));
87 }
88
90 PONCA_MULTIARCH inline bool operator==(const AlgebraicSphere<DataPoint, NeighborFilter, T>& other) const
91 {
92 return Base::operator==(other) && isApprox(other);
93 }
94
96 template <typename Other>
97 PONCA_MULTIARCH [[nodiscard]] inline bool isApprox(
98 const Other& other, const Scalar& epsilon = Eigen::NumTraits<Scalar>::dummy_precision()) const
99 {
100 PONCA_MULTIARCH_STD_MATH(pow);
101 const Scalar squaredEpsilon = epsilon * epsilon;
102 return pow(m_uc - other.uc(), Scalar(2)) < squaredEpsilon &&
103 pow(m_uq - other.uq(), Scalar(2)) < squaredEpsilon &&
104 internal::isApprox(m_ul, other.ul(), Scalar(1), epsilon);
105 }
106
108 PONCA_MULTIARCH inline bool operator!=(const AlgebraicSphere<DataPoint, NeighborFilter, T>& other) const
109 {
110 return !((*this) == other);
111 }
112
145 PONCA_MULTIARCH inline void changeBasis(const VectorType& newbasis)
146 {
147 auto& frame = Base::getNeighborFrame();
148
149 VectorType diff = frame.center() - newbasis;
150 frame.changeNeighborhoodFrame(newbasis);
151 Base::init();
152 m_uc = m_uc - m_ul.dot(diff) + m_uq * diff.dot(diff);
153 m_ul = m_ul - Scalar(2.) * m_uq * diff;
154 // m_uq is not changed
155 m_isNormalized = false;
157 }
158
160 PONCA_MULTIARCH [[nodiscard]] inline Scalar uc() const { return m_uc; }
161
163 PONCA_MULTIARCH [[nodiscard]] inline const VectorType& ul() const { return m_ul; }
164
166 PONCA_MULTIARCH [[nodiscard]] inline Scalar uq() const { return m_uq; }
167
169 PONCA_MULTIARCH [[nodiscard]] inline Scalar prattNorm() const
170 {
171 PONCA_MULTIARCH_STD_MATH(sqrt);
172 return sqrt(prattNorm2());
173 }
174
176 PONCA_MULTIARCH [[nodiscard]] inline Scalar prattNorm2() const
177 {
178 return m_ul.squaredNorm() - Scalar(4.) * m_uc * m_uq;
179 }
180
185 PONCA_MULTIARCH inline bool applyPrattNorm()
186 {
187 if (!m_isNormalized)
188 {
189 Scalar pn = prattNorm();
190 m_uc /= pn;
191 m_ul *= Scalar(1.) / pn;
192 m_uq /= pn;
193
194 m_isNormalized = true;
195 }
196 return true;
197 }
198
203 PONCA_MULTIARCH [[nodiscard]] inline Scalar radius() const
204 {
205 PONCA_MULTIARCH_CU_STD_FUNC(numeric_limits);
206 if (isPlane())
207 return numeric_limits<Scalar>::infinity(); // non-sense value
208
209 PONCA_MULTIARCH_STD_MATH(sqrt);
210 Scalar b = Scalar(1.) / m_uq;
211 return Scalar(sqrt(((Scalar(-0.5) * b) * m_ul).squaredNorm() - m_uc * b));
212 }
213
214 PONCA_MULTIARCH [[nodiscard]] inline Scalar kMean() const
215 {
216 if (isPlane())
217 return Scalar(0);
218 return Scalar(2) * (isNormalized() ? m_uq : m_uq / prattNorm());
219 }
220
225 PONCA_MULTIARCH [[nodiscard]] inline VectorType center() const
226 {
227 PONCA_MULTIARCH_CU_STD_FUNC(numeric_limits);
228 if (isPlane())
229 return VectorType::Constant(numeric_limits<Scalar>::infinity()); // non-sense value
230
231 Scalar b = Scalar(1.) / m_uq;
232 return Base::getNeighborFrame().convertToGlobalBasis((Scalar(-0.5) * b) * m_ul);
233 }
234
236 PONCA_MULTIARCH [[nodiscard]] inline bool isNormalized() const { return m_isNormalized; }
237
240 PONCA_MULTIARCH [[nodiscard]] inline Scalar potential(const VectorType& _q) const
241 {
242 // Turn to centered basis
243 const VectorType lq = Base::getNeighborFrame().convertToLocalBasis(_q);
244 return potentialLocal(lq);
245 }
246
249 PONCA_MULTIARCH [[nodiscard]] inline Scalar potential() const { return m_uc; }
250
259 PONCA_MULTIARCH [[nodiscard]] inline VectorType project(const VectorType& _q) const;
260
263 PONCA_MULTIARCH [[nodiscard]] inline VectorType primitiveGradient(const VectorType& _q) const
264 {
265 // Turn to centered basis
266 const VectorType lq = Base::getNeighborFrame().convertToLocalBasis(_q);
267 return primitiveGradientLocal(lq);
268 }
269
272 PONCA_MULTIARCH [[nodiscard]] inline const VectorType& primitiveGradient() const { return m_ul; }
273
278 PONCA_MULTIARCH [[nodiscard]] inline bool isPlane() const
279 {
280 PONCA_MULTIARCH_STD_MATH(abs);
281 bool bPlanar = Eigen::internal::isApprox(m_uq, Scalar(0));
282 bool bReady = Base::isReady();
283 return bReady && bPlanar;
284 }
285
286 protected:
288 PONCA_MULTIARCH [[nodiscard]] inline Scalar potentialLocal(const VectorType& _lq) const;
289
291 PONCA_MULTIARCH [[nodiscard]] inline VectorType primitiveGradientLocal(const VectorType& _lq) const;
292 }; // class AlgebraicSphere
293
294#include "algebraicSphere.hpp"
295} // namespace Ponca
Algebraic Sphere primitive.
Scalar potential() const
Value of the scalar field at the evaluation point.
VectorType center() const
return the estimated center of the sphere
Scalar potentialLocal(const VectorType &_lq) const
Value of the scalar field at the location .
bool applyPrattNorm()
Normalize the scalar field by the Pratt norm.
VectorType primitiveGradientLocal(const VectorType &_lq) const
Approximation of the scalar field gradient at .
void changeBasis(const VectorType &newbasis)
Express the scalar field relatively to a new basis.
const VectorType & primitiveGradient() const
Approximation of the scalar field gradient at the evaluation point.
void init()
Set the scalar field values to 0 and reset the isNormalized() status.
AlgebraicSphere< DataPoint, _NFilter, T > & projectionOperator()
Explicit conversion to CLASSNAME, to access methods potentially hidden by heritage.
AlgebraicSphere< DataPoint, _NFilter, T > & implicitPrimitive()
Explicit conversion to CLASSNAME, to access methods potentially hidden by heritage.
AlgebraicSphere< DataPoint, _NFilter, T > & meanCurvature()
Explicit conversion to CLASSNAME, to access methods potentially hidden by heritage.
Scalar m_uq
Quadratic parameter of the Algebraic hyper-sphere.
Scalar m_uc
Constant parameter of the Algebraic hyper-sphere.
typename DataPoint::Scalar Scalar
Alias to scalar type.
Scalar uc() const
Constant parameter of the Algebraic hyper-sphere.
AlgebraicSphere< DataPoint, _NFilter, T > & algebraicSphere()
Explicit conversion to CLASSNAME, to access methods potentially hidden by heritage.
bool operator!=(const AlgebraicSphere< DataPoint, NeighborFilter, T > &other) const
Comparison operator, convenience function.
bool m_isNormalized
Is the implicit scalar field normalized using Pratt.
VectorType project(const VectorType &_q) const
Project a point on the algebraic hypersphere.
Scalar prattNorm2() const
compute the squared Pratt norm of the implicit scalar field.
bool isApprox(const Other &other, const Scalar &epsilon=Eigen::NumTraits< Scalar >::dummy_precision()) const
Approximate operator.
Scalar prattNorm() const
compute the Pratt norm of the implicit scalar field.
Scalar radius() const
return the estimated radius of the sphere
Scalar uq() const
Quadratic parameter of the Algebraic hyper-sphere.
VectorType m_ul
Linear parameter of the Algebraic hyper-sphere.
const VectorType & ul() const
Linear parameter of the Algebraic hyper-sphere.
typename Base::VectorType VectorType
Alias to vector type.
bool isValid() const
Tell if the sphere as been correctly set. Used to set CONFLICT_ERROR_FOUND during fitting.
bool isPlane() const
Used to know if the fitting result to a plane.
Scalar potential(const VectorType &_q) const
Value of the scalar field at the location .
VectorType primitiveGradient(const VectorType &_q) const
Approximation of the scalar field gradient at .
bool isNormalized() const
State indicating when the sphere has been normalized.
bool operator==(const AlgebraicSphere< DataPoint, NeighborFilter, T > &other) const
Comparison operator.
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11