Ponca  40f245e28b920cbb763a1c6282156c87c626f24c
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
8#pragma once
9
10#include "./defines.h"
11
12#include PONCA_MULTIARCH_INCLUDE_STD(cmath)
13#include PONCA_MULTIARCH_INCLUDE_STD(limits)
14
15#include <Eigen/Core>
16
17namespace Ponca
18{
19
45template < class DataPoint, class _WFunctor, typename T >
46class AlgebraicSphere : public T
47{
48 PONCA_FITTING_DECLARE_DEFAULT_TYPES
49
50protected:
51 enum
52 {
53 check = Base::PROVIDES_PRIMITIVE_BASE,
55 };
56
57protected:
60
61// results
62public:
64 m_uq {0};
65 VectorType m_ul {VectorType::Zero()};
67public:
68 PONCA_EXPLICIT_CAST_OPERATORS(AlgebraicSphere,algebraicSphere)
69
70
74 PONCA_MULTIARCH inline void init()
75 {
76 Base::init();
77
78 m_uc = Scalar(0);
79 m_ul = VectorType::Zero();
80 m_uq = Scalar(0);
81
82 m_isNormalized = false;
83 }
84
88 PONCA_MULTIARCH inline bool isValid() const{
89 return !( m_ul.isApprox(VectorType::Zero()) && m_uc == Scalar(0) && m_uq == Scalar(0) );
90 }
91
93 PONCA_MULTIARCH inline bool operator==(const AlgebraicSphere<DataPoint, WFunctor, T>& other) const{
94 PONCA_MULTIARCH_STD_MATH(pow);
95 const Scalar epsilon = Eigen::NumTraits<Scalar>::dummy_precision();
96 const Scalar squaredEpsilon = epsilon*epsilon;
97 return pow(m_uc - other.m_uc, Scalar(2)) < squaredEpsilon &&
98 pow(m_uq - other.m_uq, Scalar(2)) < squaredEpsilon &&
99 m_ul.isApprox(other.m_ul);
100 }
102 template<typename Other>
103 PONCA_MULTIARCH inline bool isApprox(const Other& other, const Scalar& epsilon = Eigen::NumTraits<Scalar>::dummy_precision()) const{
104 PONCA_MULTIARCH_STD_MATH(pow);
105 const Scalar squaredEpsilon = epsilon*epsilon;
106 return pow(m_uc - other.m_uc, Scalar(2)) < squaredEpsilon &&
107 pow(m_uq - other.m_uq, Scalar(2)) < squaredEpsilon &&
108 m_ul.isApprox(other.m_ul);
109 }
110
111
113 PONCA_MULTIARCH inline bool operator!=(const AlgebraicSphere<DataPoint, WFunctor, T>& other) const{
114 return ! ((*this) == other);
115 }
116
146 PONCA_MULTIARCH inline void changeBasis(const VectorType& newbasis)
147 {
148 VectorType diff = Base::m_w.basisCenter() - newbasis;
149 Base::setWeightFunc({newbasis, Base::m_w.evalScale()});
150 Base::init();
151 m_uc = m_uc - m_ul.dot(diff) + m_uq * diff.dot(diff);
152 m_ul = m_ul - Scalar(2.)*m_uq*diff;
153 //m_uq is not changed
154 m_isNormalized = false;
156 }
157
159 PONCA_MULTIARCH inline Scalar prattNorm() const
160 {
161 PONCA_MULTIARCH_STD_MATH(sqrt);
162 return sqrt(prattNorm2());
163 }
164
166 PONCA_MULTIARCH inline Scalar prattNorm2() const
167 {
168 return m_ul.squaredNorm() - Scalar(4.) * m_uc * m_uq;
169 }
170
175 PONCA_MULTIARCH inline bool applyPrattNorm()
176 {
177 if (! m_isNormalized)
178 {
179 Scalar pn = prattNorm();
180 m_uc /= pn;
181 m_ul *= Scalar(1.)/pn;
182 m_uq /= pn;
183
184 m_isNormalized = true;
185 }
186 return true;
187 }
188
193 PONCA_MULTIARCH inline Scalar radius() const
194 {
195 PONCA_MULTIARCH_STD_MATH(numeric_limits);
196 if(isPlane())
197 return numeric_limits<Scalar>::infinity(); // non-sense value
198
199 PONCA_MULTIARCH_STD_MATH(sqrt);
200 Scalar b = Scalar(1.)/m_uq;
201 return Scalar(sqrt( ((Scalar(-0.5)*b)*m_ul).squaredNorm() - m_uc*b ));
202 }
203
208 PONCA_MULTIARCH inline VectorType center() const
209 {
210 PONCA_MULTIARCH_STD_MATH(numeric_limits);
211 if(isPlane())
212 return VectorType::Constant(numeric_limits<Scalar>::infinity()); // non-sense value
213
214 Scalar b = Scalar(1.)/m_uq;
215 return Base::m_w.convertToGlobalBasis((Scalar(-0.5)*b)*m_ul);
216 }
217
219 PONCA_MULTIARCH inline bool isNormalized() const { return m_isNormalized; }
220
223 PONCA_MULTIARCH inline Scalar potential (const VectorType& _q) const;
224
227 PONCA_MULTIARCH inline Scalar potential() const { return m_uc; }
228
237 PONCA_MULTIARCH inline VectorType project (const VectorType& _q) const;
238
246 PONCA_MULTIARCH inline VectorType projectDescent (const VectorType& _q, int nbIter = 16) const;
247
250 PONCA_MULTIARCH inline VectorType primitiveGradient (const VectorType& _q) const;
251
254 PONCA_MULTIARCH inline const VectorType& primitiveGradient () const { return m_ul; }
255
260 PONCA_MULTIARCH inline bool isPlane() const
261 {
262 PONCA_MULTIARCH_STD_MATH(abs);
263 bool bPlanar = Eigen::internal::isApprox(m_uq,Scalar(0));
264 bool bReady = Base::isReady();
265 return bReady && bPlanar;
266 }
267
268}; //class AlgebraicSphere
269
270#include "algebraicSphere.hpp"
271}
Algebraic Sphere primitive.
void changeBasis(const VectorType &newbasis)
Express the scalar field relatively to a new basis.
bool isValid() const
Tell if the sphere as been correctly set. Used to set CONFLICT_ERROR_FOUND during fitting.
const VectorType & primitiveGradient() const
Approximation of the scalar field gradient at the evaluation point.
Scalar potential() const
Value of the scalar field at the evaluation point.
bool m_isNormalized
Is the implicit scalar field normalized using Pratt.
@ check
Requires PrimitiveBase.
@ PROVIDES_ALGEBRAIC_SPHERE
Provides Algebraic Sphere.
Scalar radius() const
return the estimated radius of the sphere
bool isApprox(const Other &other, const Scalar &epsilon=Eigen::NumTraits< Scalar >::dummy_precision()) const
Approximate operator.
void init()
Set the scalar field values to 0 and reset the isNormalized() status.
Scalar m_uc
Constant parameter of the Algebraic hyper-sphere.
bool isPlane() const
Used to know if the fitting result to a plane.
VectorType project(const VectorType &_q) const
Project a point on the algebraic hypersphere.
bool operator==(const AlgebraicSphere< DataPoint, WFunctor, T > &other) const
Comparison operator.
VectorType m_ul
Linear parameter of the Algebraic hyper-sphere.
typename DataPoint::Scalar Scalar
Alias to scalar type.
Scalar prattNorm() const
compute the Pratt norm of the implicit scalar field.
Scalar prattNorm2() const
compute the squared Pratt norm of the implicit scalar field.
bool operator!=(const AlgebraicSphere< DataPoint, WFunctor, T > &other) const
Comparison operator, convenience function.
bool isNormalized() const
State indicating when the sphere has been normalized.
VectorType center() const
return the estimated center of the sphere
Scalar m_uq
Quadratic parameter of the Algebraic hyper-sphere.
bool applyPrattNorm()
Normalize the scalar field by the Pratt norm.
VectorType projectDescent(const VectorType &_q, int nbIter=16) const
Project a point on the algebraic hypersphere using Gradient Descent This projection is realized by fo...
typename Base::VectorType VectorType
Alias to vector type.
AlgebraicSphere< DataPoint, _WFunctor, T > & algebraicSphere()
Explicit conversion to AlgebraicSphere , to access methods potentially hidden by heritage.
This Source Code Form is subject to the terms of the Mozilla Public License, v.