DGtal  0.9.2
BasicFunctors.h
1 
17 #pragma once
18 
36 #if defined(BasicFunctors_RECURSES)
37 #error Recursive header files inclusion detected in BasicFunctors.h
38 #else // defined(BasicFunctors_RECURSES)
39 
40 #define BasicFunctors_RECURSES
41 
42 #if !defined BasicFunctors_h
43 
44 #define BasicFunctors_h
45 
47 // Inclusions
48 #include <algorithm>
49 #include <functional>
50 #include <cmath>
51 #include "DGtal/base/Common.h"
52 #include "DGtal/base/BasicBoolFunctors.h"
54 
55 namespace DGtal
56 {
57 namespace functors
58 {
62  template<typename T>
63  struct Min : std::binary_function <T,T,T>
64  {
65  inline
66  T operator() (const T&a, const T&b) const
67  { return std::min(a,b); }
68  };
69 
70  template<typename T>
71  struct Max : std::binary_function <T,T,T>
72  {
73  inline
74  T operator() (const T&a, const T&b) const
75  { return std::max(a,b); }
76  };
77 
81  template <class T>
82  struct Minus : std::binary_function <T,T,T>
83  {
84  T operator() (const T& x, const T& y) const
85  {return x-y;}
86  };
87 
91  template <class T>
92  struct Abs : std::unary_function<T,T>
93  {
94  inline
95  T operator() (const T &x) const
96  {
97  if (x < 0)
98  return -x;
99  else
100  return x;
101  }
102  };
103 
107  template <class T>
108  struct UnaryMinus : std::unary_function<T,T>
109  {
114  inline
115  T operator() (const T &x) const
116  {
117  return -x;
118  }
119  };
120 
124  template <class T>
125  struct MultiplicationByScalar : std::unary_function<T,T>
126  {
127  inline
128  MultiplicationByScalar( const T & aValue )
129  : myValue( aValue )
130  {}
131 
136  inline
137  T operator() (const T &x) const
138  {
139  return myValue * x;
140  }
141 
143  };
144 
146 // Some basic unary functors that may be useful
148 
154  struct Identity
155  {
161  template <typename T >
162  inline
163  T operator()(const T& aT) const
164  {
165  return aT;
166  }
167  };
168 
176  template <typename TValue>
177  class ConstValue : std::unary_function <TValue,TValue>
178  {
179  public:
180  typedef TValue Value;
181 
186  ConstValue(const Value& aValue = 0)
187  :myValue(aValue) {};
188 
196  template <typename TInput>
197  inline
198  Value operator()(const TInput& /*aInput*/) const
199  {
200  return myValue;
201  }
202 
203  private:
207  Value myValue;
208 
209  };
210 
219  template <typename TQuantity, typename TCell>
220  class ConstValueCell : std::unary_function <TQuantity,TQuantity>
221  {
222  public:
223  typedef TCell Cell;
224  typedef TQuantity Quantity;
225 
230  ConstValueCell(const Quantity& aQuantity = 0)
231  :myQuantity(aQuantity) {}
232 
238  inline
239  Quantity operator()(const Cell& /*aInput*/) const
240  {
241  return myQuantity;
242  }
243 
244  private:
248  Quantity myQuantity;
249 
250  };
251 
252 
259  template <typename TOutput >
260  struct Cast
261  {
267  template<typename TInput>
268  inline
269  TOutput operator()(const TInput& aInput) const
270  {
271  return static_cast<TOutput>(aInput);
272  }
273  };
274 
275 
284  template <typename TFunctor1, typename TFunctor2, typename ReturnType >
285  class Composer
286  {
287  public:
289  typedef ReturnType Value;
290 
294  Composer(): myF1(NULL), myF2(NULL) {}
300  Composer(const TFunctor1& aF1, const TFunctor2& aF2): myF1(&aF1), myF2(&aF2) {}
305  Composer(const Composer& other): myF1(other.myF1), myF2(other.myF2) {}
306 
311  Composer& operator=(const Composer& other)
312  {
313  if (this != &other)
314  {
315  myF1 = other.myF1;
316  myF2 = other.myF2;
317  }
318  return *this;
319  }
320 
321 
334  template<typename TInput>
335  inline
336  ReturnType operator()(const TInput& aInput) const
337  {
338  ASSERT( myF1 );
339  ASSERT( myF2 );
340  return myF2->operator()( myF1->operator()( aInput ) );
341  }
342 
343  private:
347  const TFunctor1* myF1;
351  const TFunctor2* myF2;
352  };
353 
367 template <typename T, bool isLower = true, bool isEqual = true >
369  : public std::unary_function <T,bool>
370  {
371  public:
374 
375  typedef T Input;
376 
381  Thresholder(const Input& aT = 0):myT(aT) {}
387  bool operator()(const Input& aI) const {
388  std::less_equal<Input> c;
389  return c(aI,myT);
390  }
391  private:
395  Input myT;
396 };
397 
398 //specializations
399 template <typename T>
400 struct Thresholder<T,false,false>
401  : public std::unary_function <T,bool>
402 {
403 
404  public:
407 
408  typedef T Input;
409 
410  Thresholder(const Input& aT = 0):myT(aT) {}
411 
412  bool operator()(const Input& aI) const {
413  std::greater<Input> c;
414  return c(aI,myT);
415  }
416 
417  private:
418  Input myT;
419 };
420 template <typename T>
421 struct Thresholder<T,false,true>
422  : public std::unary_function <T,bool>
423  {
424  public:
427 
428  typedef T Input;
429 
430  Thresholder(const Input& aT = 0):myT(aT) {}
431  bool operator()(const Input& aI) const {
432  std::greater_equal<Input> c;
433  return c(aI,myT);
434  }
435 
436  private:
437  Input myT;
438 };
439 
440 template <typename T>
441 struct Thresholder<T,true,false>
442  : public std::unary_function <T,bool>
443  {
444  public:
447 
448  typedef T Input;
449 
450  Thresholder(const Input& aT = 0):myT(aT) {}
451 
452  bool operator()(const Input& aI) const {
453  std::less<Input> c;
454  return c(aI,myT);
455  }
456 
457  private:
458  Input myT;
459 };
460 
461 template <typename T>
462 struct Thresholder<T,true,true>
463  : public std::unary_function <T,bool>
464  {
465  public:
468 
469  typedef T Input;
470 
471  Thresholder(const Input& aT = 0):myT(aT) {}
472 
473  bool operator()(const Input& aI) const {
474  std::less_equal<Input> c;
475  return c(aI,myT);
476  }
477 
478  private:
479  Input myT;
480 };
481 
483  // template class PredicateCombiner
493  template <typename TPredicate1, typename TPredicate2,
494  typename TBinaryFunctor = BoolFunctor2 >
496  {
497  typedef TPredicate1 Predicate1;
498  typedef TPredicate2 Predicate2;
499 
507  PredicateCombiner( const Predicate1 & pred1,
508  const Predicate2 & pred2,
509  const TBinaryFunctor & boolFunctor )
510  : myPred1( &pred1 ), myPred2( &pred2 ), myBoolFunctor( &boolFunctor )
511  {
512  }
513 
519  : myPred1( other.pred1 ), myPred2( other.pred2 ), myBoolFunctor( other.boolFunctor )
520  {
521  }
522 
529  {
530  if (this != &other)
531  {
532  myPred1 = other.myPred1;
533  myPred2 = other.myPred2;
535  }
536  return *this;
537  }
538 
543 
550  template<typename T>
551  bool operator()( const T & t ) const
552  {
553  return myBoolFunctor->operator()( myPred1->operator()( t ),
554  myPred2->operator()( t ) );
555  }
556 
558  const Predicate1* myPred1;
560  const Predicate2* myPred2;
562  const TBinaryFunctor* myBoolFunctor;
563  };
564 
572 template <typename T>
574  : public std::unary_function <T,bool>
575 {
576 public:
579 
581  typedef T Input;
582 
587 
593  IntervalThresholder(const Input& low, const Input& up)
594  : myTlow( low), myTup ( up ),
595  myPred( myTlow, myTup, AndBoolFct2() ) {}
596 
602  bool operator()(const Input& aI) const
603  {
604  return myPred(aI);
605  }
606 private:
610  Tlow myTlow;
614  Tup myTup;
618  CombinedPredicate myPred;
619 };
620 
621 
629  template <typename ReturnType>
630  class Pair1st
631  {
632  public:
633 
642  template <typename TPair>
643  inline
644  ReturnType operator()(const TPair& aPair) const
645  {
646  return aPair.first;
647  }
648 
649  };
650 
658  template <typename ReturnType>
659  class Pair2nd
660  {
661  public:
662 
671  template <typename TPair>
672  inline
673  ReturnType operator()(const TPair& aPair) const
674  {
675  return aPair.second;
676  }
677 
678  };
679 
688  template <typename ReturnType>
690  {
691  public:
692 
701  template <typename TPair>
702  inline
703  ReturnType& operator()(TPair& aPair) const
704  {
705  return aPair.first;
706  }
707 
716  template <typename TPair>
717  inline
718  const ReturnType& operator()(const TPair& aPair) const
719  {
720  return aPair.first;
721  }
722 
723  };
724 
733  template <typename ReturnType>
735  {
736  public:
737 
746  template <typename TPair>
747  inline
748  ReturnType& operator()(TPair& aPair) const
749  {
750  return aPair.second;
751  }
752 
761  template <typename TPair>
762  inline
763  const ReturnType& operator()(const TPair& aPair) const
764  {
765  return aPair.second;
766  }
767 
768  };
769 
778  template<typename TInputType, typename TOutputType>
779  struct Rescaling
780  {
781  TInputType myInitMin;
782  TInputType myInitMax;
783  TInputType myInitRange;
784 
785  TOutputType myNewMin;
786  TOutputType myNewMax;
787  TOutputType myNewRange;
788 
797  Rescaling( const TInputType &initMin, const TInputType &initMax, const TOutputType &newMin, const TOutputType &newMax ) :
798  myInitMin(initMin), myInitMax(initMax), myInitRange(initMax-initMin), myNewMin(newMin), myNewMax(newMax), myNewRange(newMax-newMin) {}
807  inline
808  TOutputType operator() (const TInputType& anInitVal) const
809  { return anInitVal<myInitMin ? myNewMin : anInitVal > myInitMax ? myNewMax : (anInitVal-myInitMin)*myNewRange/myInitRange + myNewMin; }
810  };
811 
812 
821  {
827  GaussianKernel(const double aSigma) :mySigma(aSigma)
828  {
829  myCoef = 1.0/(mySigma * sqrt(2.0*M_PI));
830  myCoef2 = 1.0/(2.0*M_PI);
831  }
832 
837  inline
838  double operator()(const double aVal) const
839  {
840  ASSERT((aVal <= 1) && (aVal>=0));
841  return myCoef*exp(-aVal*aVal*myCoef2);
842  }
843 
845  double mySigma;
846 
848  double myCoef;
849  private:
851  double myCoef2;
852  };
853 }
854 }
856 
857 
858 #endif // !defined BasicFunctors_h
859 
860 #undef BasicFunctors_RECURSES
861 #endif // else defined(BasicFunctors_RECURSES)
Aim: defines a functor on double numbers which corresponds to a Gaussian convolution kernel...
const ReturnType & operator()(const TPair &aPair) const
Aim: Define a simple functor that returns a constant value (0 by default).
const ReturnType & operator()(const TPair &aPair) const
Rescaling(const TInputType &initMin, const TInputType &initMax, const TOutputType &newMin, const TOutputType &newMax)
TOutputType operator()(const TInputType &anInitVal) const
PredicateCombiner & operator=(const PredicateCombiner &other)
T operator()(const T &aT) const
bool operator()(const Input &aI) const
PredicateCombiner(const Predicate1 &pred1, const Predicate2 &pred2, const TBinaryFunctor &boolFunctor)
Go to http://www.sgi.com/tech/stl/LessThanComparable.html.
Definition: Boost.dox:48
bool operator()(const Input &aI) const
const TFunctor2 * myF2
GaussianKernel(const double aSigma)
Aim: Define a simple unary functor that returns a reference on the first member of a pair in order to...
Aim: Define a simple functor that returns the second member of a pair.
BOOST_CONCEPT_ASSERT((boost::EqualityComparable< T >))
Aim: Define a simple functor using the static cast operator.
PredicateCombiner< Tlow, Tup, AndBoolFct2 > CombinedPredicate
Composer(const TFunctor1 &aF1, const TFunctor2 &aF2)
Composer & operator=(const Composer &other)
Thresholder< T, true, true > Tup
ReturnType operator()(const TPair &aPair) const
Aim: Define a simple unary functor that returns a reference on the first member of a pair in order to...
ReturnType & operator()(TPair &aPair) const
T operator()(const T &x, const T &y) const
Definition: BasicFunctors.h:84
Go to http://www.sgi.com/tech/stl/EqualityComparable.html.
Definition: Boost.dox:46
T operator()(const T &a, const T &b) const
Definition: BasicFunctors.h:66
Composer(const Composer &other)
ConstValue(const Value &aValue=0)
boost::function2< bool, bool, bool > BoolFunctor2
BOOST_CONCEPT_ASSERT((boost::EqualityComparable< T >))
Aim: Define a simple default functor that just returns its argument.
Aim: Define a new Functor from the composition of two other functors.
Aim: A small functor with an operator () that compares one value to a threshold value according to tw...
ReturnType operator()(const TPair &aPair) const
Aim: A small functor with an operator () that compares one value to an interval.
Thresholder< T, false, true > Tlow
predicates type
PredicateCombiner(const PredicateCombiner &other)
Value operator()(const TInput &) const
double mySigma
Sigma parameter.
Aim: Functor allowing to rescale a value. Values of the initial scale [initMin,initMax] are rescaled ...
bool operator()(const T &t) const
const Predicate2 * myPred2
aliasing pointer to the right predicate.
double myCoef
Temporary variable.
const TBinaryFunctor * myBoolFunctor
aliasing pointer to the binary functor.
T operator()(const T &x) const
ReturnType operator()(const TInput &aInput) const
const TFunctor1 * myF1
DGtal is the top-level namespace which contains all DGtal functions and types.
double operator()(const double aVal) const
T operator()(const T &a, const T &b) const
Definition: BasicFunctors.h:74
T operator()(const T &x) const
Definition: BasicFunctors.h:95
ReturnType & operator()(TPair &aPair) const
IntervalThresholder(const Input &low, const Input &up)
Aim: The predicate returns true when the given binary functor returns true for the two Predicates giv...
ConstValueCell(const Quantity &aQuantity=0)
const Predicate1 * myPred1
aliasing pointer to the left predicate.
Duplicated STL functors.
Definition: BasicFunctors.h:63
Quantity operator()(const Cell &) const
bool operator()(const Input &aI) const
ReturnType Value
Necessary for DistanceVisitor.
Aim: Define a simple functor that returns a constant quantity (0 by default).
Aim: Define a simple functor that returns the first member of a pair.
TOutput operator()(const TInput &aInput) const
double myCoef2
Temporary variable.
Thresholder(const Input &aT=0)