Ponca  f7f7d7cc113b4e53be6bee25005fbdbe7e016293
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 if(isPlane())
196 {
197 //return infinity (non-sense value)
198#ifdef __CUDACC__
199 Scalar inf = 0.;
200 return Scalar(1.)/inf;
201#else
202 return std::numeric_limits<Scalar>::infinity();
203#endif
204 }
205
206 PONCA_MULTIARCH_STD_MATH(sqrt);
207 Scalar b = Scalar(1.)/m_uq;
208 return Scalar(sqrt( ((Scalar(-0.5)*b)*m_ul).squaredNorm() - m_uc*b ));
209 }
210
215 PONCA_MULTIARCH inline VectorType center() const
216 {
217 if(isPlane())
218 {
219 //return infinity (non-sense value)
220#ifdef __CUDACC__
221 return VectorType::Constant(Scalar(1.)/Scalar(0));
222#else
223 return VectorType::Constant(std::numeric_limits<Scalar>::infinity());
224#endif
225 }
226
227 Scalar b = Scalar(1.)/m_uq;
228 return Base::m_w.convertToGlobalBasis((Scalar(-0.5)*b)*m_ul);
229 }
230
232 PONCA_MULTIARCH inline bool isNormalized() const { return m_isNormalized; }
233
236 PONCA_MULTIARCH inline Scalar potential (const VectorType& _q) const;
237
240 PONCA_MULTIARCH inline Scalar potential() const { return m_uc; }
241
250 PONCA_MULTIARCH inline VectorType project (const VectorType& _q) const;
251
259 PONCA_MULTIARCH inline VectorType projectDescent (const VectorType& _q, int nbIter = 16) const;
260
263 PONCA_MULTIARCH inline VectorType primitiveGradient (const VectorType& _q) const;
264
267 PONCA_MULTIARCH inline const VectorType& primitiveGradient () const { return m_ul; }
268
273 PONCA_MULTIARCH inline bool isPlane() const
274 {
275 PONCA_MULTIARCH_STD_MATH(abs);
276 bool bPlanar = Eigen::internal::isApprox(m_uq,Scalar(0));
277 bool bReady = Base::isReady();
278 return bReady && bPlanar;
279 }
280
281}; //class AlgebraicSphere
282
283#include "algebraicSphere.hpp"
284}
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.