DGtal  1.5.beta
FunctorsLambdaMST.h
1 
17 #pragma once
18 
29 #if defined(FunctorsLambdaMST_RECURSES)
30 #error Recursive header files inclusion detected in FunctorsLambdaMST.h
31 #else // defined(FunctorsLambdaMST_RECURSES)
33 #define FunctorsLambdaMST_RECURSES
34 
35 #if !defined FunctorsLambdaMST_h
37 #define FunctorsLambdaMST_h
38 
40 // Inclusions
41 #include <functional>
42 #include <iterator>
43 #include <stdexcept>
44 #include "DGtal/base/Common.h"
45 #include "DGtal/base/IteratorCirculatorTraits.h"
46 #include "DGtal/base/CUnaryFunctor.h"
48 
49 namespace DGtal
50 {
52 
61 namespace functors
62 {
69  {
70  double operator() (double x) const
71  {
72  double e2 = x * x;
73  double e3 = e2 * x;
74  return 64.0 * ( -e3 * e3 + 3.0 * e3 * e2 - 3.0 * e2 * e2 + e3 );
75  }
76  };
83  {
84  double operator() (double x) const
85  {
86  return std::sin ( M_PI * x );
87  }
88  };
89 
96  {
97  double operator() (double x) const
98  {
99  return 2.0 / ( std::exp ( 15.0 * ( x - 0.5 ) ) + std::exp ( -15.0 * ( x - 0.5 ) ) );
100  }
101  };
102 }
103 
111 template<typename DSS, typename LambdaFunction>
113 {
115  // ----------------------- Types ------------------------------
116 public:
118  typedef DSS TDSS;
119 
120  struct Value
121  {
123  double second;
124  Value () : second ( 0. ) {}
125  Value & operator+= ( const Value & ch )
126  {
127  this->first += ch.first;
128  this->second += ch.second;
129  return *this;
130  }
131  };
132 
133 
134  // ----------------------- Interface --------------------------------------
135 public:
143  Value operator() ( const TDSS& aDSS, const int & indexOfPointInDSS, const int & dssLen ) const
144  {
145  Value result;
146  double norm = std::sqrt ( aDSS.a() * aDSS.a() + aDSS.b() * aDSS.b() );
147  result.second = lambdaFunctor ( (double)indexOfPointInDSS / (double)dssLen );
148  if ( norm > 0. )
149  {
150  result.first[0] = result.second * aDSS.b() / norm;
151  result.first[1] = result.second * aDSS.a() / norm;
152  }
153  else
154  {
155  result.first[0] = 0.;
156  result.first[1] = 0.;
157  }
158  return result;
159  }
160 private:
161  // ------------------------- Private Datas --------------------------------
162  LambdaFunction lambdaFunctor;
163 };
164 
172 template<typename DSS, typename LambdaFunction>
174 {
176 public:
177  // ----------------------- Types ------------------------------
179  typedef DSS TDSS;
180  struct Value
181  {
183  double second;
184  Value () : second ( 0. ) {}
185  Value & operator += ( const Value & ch )
186  {
187  this->first += ch.first;
188  this->second += ch.second;
189  return *this;
190  }
191  };
192 
193  // ----------------------- Interface --------------------------------------
201  Value operator() ( const TDSS& aDSS, const unsigned int indexOfPointInDSS, const unsigned int dssLen ) const
202  {
203  Value result;
204  typename DSS::Point3d directionZ3;
205  RealVector direction;
206  typename DSS::PointR3d intercept;
207  typename DSS::PointR3d thikness;
208 
209  aDSS.getParameters ( directionZ3, intercept, thikness );
210  direction[0] = directionZ3[0];
211  direction[1] = directionZ3[1];
212  direction[2] = directionZ3[2];
213 
214  result.second = lambdaFunctor ( (double)indexOfPointInDSS / (double)dssLen );
215 
216  double norm = direction.norm();
217  if ( norm != 0. )
218  direction /= norm;
219  result.first = direction * result.second;
220  return result;
221  }
222 private:
223  // ------------------------- Private Datas --------------------------------
225  LambdaFunction lambdaFunctor;
226 };
227 
228 
234 template<typename DSS >
236 {
237 public:
238 // ----------------------- Types ------------------------------
239  typedef DSS DSSType;
241 
243  bool operator()( const DSSType & ) const
244  {
245  return false;
246  }
247 
249  bool admissibility ( const DSSType &, const Point & ) const
250  {
251  return false;
252  }
253 
255  long int position ( const DSSType &, const Point & ) const
256  {
257  throw std::runtime_error ( "You are not suppose to see this error!" );
258  }
259 };
260 
261 
268 template<typename DSS >
270 {
271 public:
272 // ----------------------- Types ------------------------------
273  typedef DSS DSSType;
275 
277 
281  void init ( double threshold )
282  {
283  if ( threshold < 0. )
284  throw std::runtime_error ( "The threshold has to be positive!" );
285  lenThreshold = threshold;
286  initThreshold = true;
287  }
288 
293  bool operator()( const DSSType & dss ) const
294  {
295  if (! initThreshold )
296  throw std::runtime_error ( "The filter has to be initialized!" );
297 
298  return std::distance ( dss.begin ( ), dss.end ( ) ) < lenThreshold;
299  }
300 
307  bool admissibility ( const DSSType & dss, const Point & p ) const
308  {
309  return ( p - *dss.begin ( ) ).norm ( ) <= lenThreshold || ( p - *( dss.end ( ) - 1 ) ).norm ( ) <= lenThreshold;
310  }
311 
319  long int position ( const DSSType & dss, const Point & p ) const
320  {
321  if ( ( p - *dss.begin ( ) ).norm ( ) <= lenThreshold )
322  return 1;
323  else if ( ( p - *( dss.end ( ) - 1 ) ).norm ( ) <= lenThreshold )
324  return std::distance ( dss.begin ( ), dss.end ( ) ) + 1;
325  else
326  throw std::runtime_error ( "The DSS and the point are not admissible!" );
327  }
328 
329 private:
330  double lenThreshold;
332 };
333 
334 } // namespace DGtal
335 
336 // //
338 
339 #endif // !defined FunctorsLambdaMST_h
340 
341 #undef FunctorsLambdaMST_RECURSES
342 #endif // else defined(FunctorsLambdaMST_RECURSES)
bool operator()(const DSSType &dss) const
long int position(const DSSType &dss, const Point &p) const
bool admissibility(const DSSType &dss, const Point &p) const
IteratorCirculatorTraits< typename DSSType::ConstIterator >::Value Point
IteratorCirculatorTraits< typename DSSType::ConstIterator >::Value Point
bool operator()(const DSSType &) const
Always returns false.
long int position(const DSSType &, const Point &) const
When called always throws an exception.
bool admissibility(const DSSType &, const Point &) const
Always returns false.
double norm(const NormType type=L_2) const
BOOST_CONCEPT_ASSERT((concepts::CUnaryFunctor< LambdaFunction, double, double >))
Value operator()(const TDSS &aDSS, const int &indexOfPointInDSS, const int &dssLen) const
PointVector< 2, double > RealVector
Value operator()(const TDSS &aDSS, const unsigned int indexOfPointInDSS, const unsigned int dssLen) const
BOOST_CONCEPT_ASSERT((concepts::CUnaryFunctor< LambdaFunction, double, double >))
PointVector< 3, double > RealVector
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:90
double operator()(double x) const
DGtal::MelkmanConvexHull< Point, Functor > ch