DGtal 2.2.0
Loading...
Searching...
No Matches
SphereFittingEstimator.h
1
16
17#pragma once
18
33
34#if defined(SphereFittingEstimator_RECURSES)
35#error Recursive header files inclusion detected in SphereFittingEstimator.h
36#else // defined(SphereFittingEstimator_RECURSES)
38#define SphereFittingEstimator_RECURSES
39
40#if !defined SphereFittingEstimator_h
42#define SphereFittingEstimator_h
43
45// Inclusions
46#include <iostream>
47#include <DGtal/base/Common.h>
48#include <DGtal/topology/SCellsFunctors.h>
49
50#ifndef DGTAL_WITH_PONCA
51#error You need to have activated Ponca (DGTAL_WITH_PONCA) to include this file.
52#endif
53
54//Ponca includes
55#include <Ponca/Fitting>
56#include <Eigen/Eigen>
57#include <vector>
58
60
61namespace DGtal
62{
63 namespace functors
64 {
66 // template class SphereFittingEstimator
85 template <typename TSurfel,
86 typename TEmbedder,
87 typename TNormalVectorEstimatorCache>
89 {
90 public:
91
92
94 {
95 public:
96 enum {Dim = 3};
97 typedef double Scalar;
98 typedef Eigen::Matrix<Scalar, Dim, 1> VectorType;
99 typedef Eigen::Matrix<Scalar, Dim, Dim> MatrixType;
100
101 PONCA_MULTIARCH inline PoncaPoint(const VectorType& _pos = VectorType::Zero(),
102 const VectorType& _normal = VectorType::Zero())
103 : m_pos(_pos), m_normal(_normal) {}
104
105 PONCA_MULTIARCH inline const VectorType& pos() const { return m_pos; }
106 PONCA_MULTIARCH inline const VectorType& normal() const { return m_normal; }
107
108 PONCA_MULTIARCH inline VectorType& pos() { return m_pos; }
109 PONCA_MULTIARCH inline VectorType& normal() { return m_normal; }
110
111 private:
113 };
114
115
116 typedef TSurfel Surfel;
117 typedef TEmbedder SCellEmbedder;
118 typedef typename SCellEmbedder::RealPoint RealPoint;
119
120 typedef TNormalVectorEstimatorCache NormalVectorEstimatorCache;
121
122 typedef typename PoncaPoint::Scalar Scalar;
124
125 typedef Ponca::DistWeightFunc<PoncaPoint, Ponca::SmoothWeightKernel<Scalar>> WeightFunc;
126 typedef Ponca::Basket<PoncaPoint, WeightFunc, Ponca::OrientedSphereFit, Ponca::GLSParam> Fit;
127
129 struct Quantity
130 {
132 double radius;
133 double tau;
134 double kappa;
136
138 Quantity(RealPoint p, double rad, double _tau,
139 double _kappa, RealPoint _eta): center(p), radius(rad),
140 tau(_tau), kappa(_kappa),
141 eta(_eta) {}
143 bool operator==(Quantity aq) {return (center==aq.center) && (radius==aq.radius);}
144 bool operator<(Quantity aq) {return (center<aq.center) && (radius<aq.radius);}
145 bool operator!=(Quantity aq) {return !(*this == aq);}
146 };
147
148
159 const double h,
160 const double radius,
162 myEmbedder(&anEmbedder), myH(h), myRadius(radius), myNormalEsitmatorCache(&anEstimator)
163 {
164 myFit = new Fit();
165 }
166
167
172 {
173 delete myFit ;
174 }
175
176
183 void pushSurfel(const Surfel & aSurf,
184 const double aDistance)
185 {
186 BOOST_VERIFY(aDistance==aDistance);
187
188 RealPoint p = myEmbedder->operator()(aSurf);
189 RealPoint norm = myNormalEsitmatorCache->eval(aSurf);
190 VectorType pp;
191 pp(0) = p[0]*myH;
192 pp(1) = p[1]*myH;
193 pp(2) = p[2]*myH;
194 VectorType normal;
195 normal(0) = norm[0];
196 normal(1) = norm[1];
197 normal(2) = norm[2];
198 PoncaPoint point(pp, normal);
199 if (myFirstPoint)
200 {
201 myFirstPoint = false;
202 myFit->setWeightFunc({pp, myRadius});
203 myFit->init();
204 }
205 else
206 myFit->addNeighbor(point);
207
208#ifdef DGTAL_DEV_VERBOSE
209 trace.info() <<"#";
210#endif
211 }
212
219 {
220 myFit->finalize();
221
222#ifdef DGTAL_DEV_VERBOSE
223 trace.info() <<std::endl;
224
225 //Test if the fitting ended without errors
226 if(myFit->isStable())
227 {
228 std::cout << "Center: [" << myFit->center().transpose() << "] ; radius: " << myFit->radius() << std::endl;
229
230 std::cout << "Pratt normalization"
231 << (myFit->applyPrattNorm() ? " is now done." : " has already been applied.") << std::endl;
232
233
234 std::cout << "Fitted Sphere: " << std::endl
235 << "\t Tau : " << myFit->tau() << std::endl
236 << "\t Eta : " << myFit->eta().transpose() << std::endl
237 << "\t Kappa: " << myFit->kappa() << std::endl;
238
239 }
240 else
241 {
242 std::cout << "Ooops... not stable result"<<std::endl;
243 }
244#endif
245 Quantity res;
246 res.center = RealPoint((myFit->center())(0),
247 (myFit->center())(1),
248 (myFit->center())(2));
249 res.radius = myFit->radius();
250 res.tau = myFit->tau();
251 res.kappa = myFit->kappa();
252 res.eta = RealPoint((myFit->eta())(0),
253 (myFit->eta())(1),
254 (myFit->eta())(2));
255 return res;
256 }
257
258
263 void reset()
264 {
265 delete myFit;
266 myFit = new Fit();
267 myFirstPoint = true;
268 }
269
270
271 private:
272
275
278
280 double myH;
281
283 double myRadius;
284
287
290
291 }; // end of class SphereFittingEstimator
292 }
293} // namespace DGtal
294
295
296// //
298
299#endif // !defined SphereFittingEstimator_h
300
301#undef SphereFittingEstimator_RECURSES
302#endif // else defined(SphereFittingEstimator_RECURSES)
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition ConstAlias.h:187
std::ostream & info()
PONCA_MULTIARCH const VectorType & normal() const
PONCA_MULTIARCH const VectorType & pos() const
PONCA_MULTIARCH PoncaPoint(const VectorType &_pos=VectorType::Zero(), const VectorType &_normal=VectorType::Zero())
Ponca::DistWeightFunc< PoncaPoint, Ponca::SmoothWeightKernel< Scalar > > WeightFunc
const NormalVectorEstimatorCache * myNormalEsitmatorCache
NormalVectorCache.
Ponca::Basket< PoncaPoint, WeightFunc, Ponca::OrientedSphereFit, Ponca::GLSParam > Fit
bool myFirstPoint
Boolean for initial point.
const SCellEmbedder * myEmbedder
Alias of the geometrical embedder.
SphereFittingEstimator(ConstAlias< SCellEmbedder > anEmbedder, const double h, const double radius, ConstAlias< NormalVectorEstimatorCache > anEstimator)
TNormalVectorEstimatorCache NormalVectorEstimatorCache
void pushSurfel(const Surfel &aSurf, const double aDistance)
functors namespace gathers all DGtal functors.
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Quantity type: a 3-sphere (model of CQuantity).
Quantity(RealPoint p, double rad, double _tau, double _kappa, RealPoint _eta)