Ponca  aa50bfdf187919869239c5b44b748842569114c1
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(const VectorType& _basisCenter = VectorType::Zero())
75 {
76 Base::init(_basisCenter);
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 }
101
103 PONCA_MULTIARCH inline bool operator!=(const AlgebraicSphere<DataPoint, WFunctor, T>& other) const{
104 return ! ((*this) == other);
105 }
106
136 PONCA_MULTIARCH inline void changeBasis(const VectorType& newbasis)
137 {
138 VectorType diff = Base::m_w.basisCenter() - newbasis;
139 Base::m_w.init( newbasis );
140 m_uc = m_uc - m_ul.dot(diff) + m_uq * diff.dot(diff);
141 m_ul = m_ul - Scalar(2.)*m_uq*diff;
142 //m_uq is not changed
143 m_isNormalized = false;
145 }
146
148 PONCA_MULTIARCH inline Scalar prattNorm() const
149 {
150 PONCA_MULTIARCH_STD_MATH(sqrt);
151 return sqrt(prattNorm2());
152 }
153
155 PONCA_MULTIARCH inline Scalar prattNorm2() const
156 {
157 return m_ul.squaredNorm() - Scalar(4.) * m_uc * m_uq;
158 }
159
164 PONCA_MULTIARCH inline bool applyPrattNorm()
165 {
166 if (! m_isNormalized)
167 {
168 Scalar pn = prattNorm();
169 m_uc /= pn;
170 m_ul *= Scalar(1.)/pn;
171 m_uq /= pn;
172
173 m_isNormalized = true;
174 }
175 return true;
176 }
177
182 PONCA_MULTIARCH inline Scalar radius() const
183 {
184 if(isPlane())
185 {
186 //return infinity (non-sense value)
187#ifdef __CUDACC__
188 Scalar inf = 0.;
189 return Scalar(1.)/inf;
190#else
191 return std::numeric_limits<Scalar>::infinity();
192#endif
193 }
194
195 PONCA_MULTIARCH_STD_MATH(sqrt);
196 Scalar b = Scalar(1.)/m_uq;
197 return Scalar(sqrt( ((Scalar(-0.5)*b)*m_ul).squaredNorm() - m_uc*b ));
198 }
199
204 PONCA_MULTIARCH inline VectorType center() const
205 {
206 if(isPlane())
207 {
208 //return infinity (non-sense value)
209#ifdef __CUDACC__
210 return VectorType::Constant(Scalar(1.)/Scalar(0));
211#else
212 return VectorType::Constant(std::numeric_limits<Scalar>::infinity());
213#endif
214 }
215
216 Scalar b = Scalar(1.)/m_uq;
217 return Base::m_w.convertToGlobalBasis((Scalar(-0.5)*b)*m_ul);
218 }
219
221 PONCA_MULTIARCH inline bool isNormalized() const { return m_isNormalized; }
222
225 PONCA_MULTIARCH inline Scalar potential (const VectorType& _q) const;
226
229 PONCA_MULTIARCH inline Scalar potential() const { return m_uc; }
230
239 PONCA_MULTIARCH inline VectorType project (const VectorType& _q) const;
240
248 PONCA_MULTIARCH inline VectorType projectDescent (const VectorType& _q, int nbIter = 16) const;
249
252 PONCA_MULTIARCH inline VectorType primitiveGradient (const VectorType& _q) const;
253
256 PONCA_MULTIARCH inline const VectorType& primitiveGradient () const { return m_ul; }
257
262 PONCA_MULTIARCH inline bool isPlane() const
263 {
264 PONCA_MULTIARCH_STD_MATH(abs);
265 Scalar epsilon = Eigen::NumTraits<Scalar>::dummy_precision();
266 bool bPlanar = Eigen::internal::isMuchSmallerThan(abs(m_uq), Scalar(1.), epsilon);
267 bool bReady = Base::isReady();
268
269 return bReady && bPlanar;
270 }
271
272}; //class AlgebraicSphere
273
274#include "algebraicSphere.hpp"
275}
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
Scalar m_uc
Constant parameter of the Algebraic hyper-sphere.
bool isPlane() const
Used to know if the fitting result to a plane.
void init(const VectorType &_basisCenter=VectorType::Zero())
Set the scalar field values to 0 and reset the isNormalized() status.
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.