DGtal  0.9.4.1
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/io/Color.h"
52 #include "DGtal/base/Common.h"
53 #include "DGtal/base/BasicBoolFunctors.h"
55 
56 namespace DGtal
57 {
58  namespace functors
59 {
63  template<typename T>
64  struct Min
65  {
66  inline
67  T operator() (const T&a, const T&b) const
68  { return std::min(a,b); }
69  };
70 
71  template<typename T>
72  struct Max
73  {
74  inline
75  T operator() (const T&a, const T&b) const
76  { return std::max(a,b); }
77  };
78 
82  template <class T>
83  struct Minus
84  {
85  T operator() (const T& x, const T& y) const
86  {return x-y;}
87  };
88 
92  template <class T>
93  struct Abs
94  {
95  inline
96  T operator() (const T &x) const
97  {
98  if (x < 0)
99  return -x;
100  else
101  return x;
102  }
103  };
104 
108  template <class T>
109  struct UnaryMinus
110  {
115  inline
116  T operator() (const T &x) const
117  {
118  return -x;
119  }
120  };
121 
125  template <class T>
127  {
128  inline
129  MultiplicationByScalar( const T & aValue )
130  : myValue( aValue )
131  {}
132 
137  inline
138  T operator() (const T &x) const
139  {
140  return myValue * x;
141  }
142 
144  };
145 
147 // Some basic unary functors that may be useful
149 
155  struct Identity
156  {
162  template <typename T >
163  inline
164  T operator()(const T& aT) const
165  {
166  return aT;
167  }
168  };
169 
177  template <typename TValue>
179  {
180  public:
181  typedef TValue Value;
182 
187  ConstValue(const Value& aValue = 0)
188  :myValue(aValue) {};
189 
197  template <typename TInput>
198  inline
199  Value operator()(const TInput& /*aInput*/) const
200  {
201  return myValue;
202  }
203 
204  private:
209 
210  };
211 
220  template <typename TQuantity, typename TCell>
222  {
223  public:
224  typedef TCell Cell;
225  typedef TQuantity Quantity;
226 
231  ConstValueCell(const Quantity& aQuantity = 0)
232  :myQuantity(aQuantity) {}
233 
239  inline
240  Quantity operator()(const Cell& /*aInput*/) const
241  {
242  return myQuantity;
243  }
244 
245  private:
250 
251  };
252 
253 
260  template <typename TOutput >
261  struct Cast
262  {
268  template<typename TInput>
269  inline
270  TOutput operator()(const TInput& aInput) const
271  {
272  return static_cast<TOutput>(aInput);
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  {
370  public:
373 
374  typedef T Input;
375 
380  Thresholder(const Input& aT = 0):myT(aT) {}
386  bool operator()(const Input& aI) const {
387  std::less_equal<Input> c;
388  return c(aI,myT);
389  }
390  private:
395 };
396 
397 //specializations
398 template <typename T>
399 struct Thresholder<T,false,false>
400 {
401 
402  public:
405 
406  typedef T Input;
407 
408  Thresholder(const Input& aT = 0):myT(aT) {}
409 
410  bool operator()(const Input& aI) const {
411  std::greater<Input> c;
412  return c(aI,myT);
413  }
414 
415  private:
417 };
418 template <typename T>
419 struct Thresholder<T,false,true>
420  {
421  public:
424 
425  typedef T Input;
426 
427  Thresholder(const Input& aT = 0):myT(aT) {}
428  bool operator()(const Input& aI) const {
429  std::greater_equal<Input> c;
430  return c(aI,myT);
431  }
432 
433  private:
435 };
436 
437 template <typename T>
438 struct Thresholder<T,true,false>
439  {
440  public:
443 
444  typedef T Input;
445 
446  Thresholder(const Input& aT = 0):myT(aT) {}
447 
448  bool operator()(const Input& aI) const {
449  std::less<Input> c;
450  return c(aI,myT);
451  }
452 
453  private:
455 };
456 
457 template <typename T>
458 struct Thresholder<T,true,true>
459  {
460  public:
463 
464  typedef T Input;
465 
466  Thresholder(const Input& aT = 0):myT(aT) {}
467 
468  bool operator()(const Input& aI) const {
469  std::less_equal<Input> c;
470  return c(aI,myT);
471  }
472 
473  private:
475 };
476 
478  // template class PredicateCombiner
488  template <typename TPredicate1, typename TPredicate2,
489  typename TBinaryFunctor = BoolFunctor2 >
491  {
492  typedef TPredicate1 Predicate1;
493  typedef TPredicate2 Predicate2;
494 
503  const Predicate2 & pred2,
504  const TBinaryFunctor & boolFunctor )
505  : myPred1( &pred1 ), myPred2( &pred2 ), myBoolFunctor( &boolFunctor )
506  {
507  }
508 
514  : myPred1( other.pred1 ), myPred2( other.pred2 ), myBoolFunctor( other.boolFunctor )
515  {
516  }
517 
524  {
525  if (this != &other)
526  {
527  myPred1 = other.myPred1;
528  myPred2 = other.myPred2;
530  }
531  return *this;
532  }
533 
538 
545  template<typename T>
546  bool operator()( const T & t ) const
547  {
548  return myBoolFunctor->operator()( myPred1->operator()( t ),
549  myPred2->operator()( t ) );
550  }
551 
557  const TBinaryFunctor* myBoolFunctor;
558  };
559 
567 template <typename T>
569 {
570 public:
573 
575  typedef T Input;
576 
581 
587  IntervalThresholder(const Input& low, const Input& up)
588  : myTlow( low), myTup ( up ),
589  myPred( myTlow, myTup, AndBoolFct2() ) {}
590 
596  bool operator()(const Input& aI) const
597  {
598  return myPred(aI);
599  }
600 private:
613 };
614 
615 
623  template <typename ReturnType>
624  class Pair1st
625  {
626  public:
627 
636  template <typename TPair>
637  inline
638  ReturnType operator()(const TPair& aPair) const
639  {
640  return aPair.first;
641  }
642 
643  };
644 
652  template <typename ReturnType>
653  class Pair2nd
654  {
655  public:
656 
665  template <typename TPair>
666  inline
667  ReturnType operator()(const TPair& aPair) const
668  {
669  return aPair.second;
670  }
671 
672  };
673 
682  template <typename ReturnType>
684  {
685  public:
686 
695  template <typename TPair>
696  inline
697  ReturnType& operator()(TPair& aPair) const
698  {
699  return aPair.first;
700  }
701 
710  template <typename TPair>
711  inline
712  const ReturnType& operator()(const TPair& aPair) const
713  {
714  return aPair.first;
715  }
716 
717  };
718 
727  template <typename ReturnType>
729  {
730  public:
731 
740  template <typename TPair>
741  inline
742  ReturnType& operator()(TPair& aPair) const
743  {
744  return aPair.second;
745  }
746 
755  template <typename TPair>
756  inline
757  const ReturnType& operator()(const TPair& aPair) const
758  {
759  return aPair.second;
760  }
761 
762  };
763 
772  template<typename TInputType, typename TOutputType>
773  struct Rescaling
774  {
775  TInputType myInitMin;
776  TInputType myInitMax;
777  TInputType myInitRange;
778 
779  TOutputType myNewMin;
780  TOutputType myNewMax;
781  TOutputType myNewRange;
782 
791  Rescaling( const TInputType &initMin, const TInputType &initMax, const TOutputType &newMin, const TOutputType &newMax ) :
792  myInitMin(initMin), myInitMax(initMax), myInitRange(initMax-initMin), myNewMin(newMin), myNewMax(newMax), myNewRange(newMax-newMin) {}
801  inline
802  TOutputType operator() (const TInputType& anInitVal) const
803  { return anInitVal<myInitMin ? myNewMin : anInitVal > myInitMax ? myNewMax : (anInitVal-myInitMin)*myNewRange/myInitRange + myNewMin; }
804  };
805 
806 
815  {
821  GaussianKernel(const double aSigma) :mySigma(aSigma)
822  {
823  myCoef = 1.0/(mySigma * sqrt(2.0*M_PI));
824  myCoef2 = 1.0/(2.0*M_PI);
825  }
826 
831  inline
832  double operator()(const double aVal) const
833  {
834  ASSERT((aVal <= 1) && (aVal>=0));
835  return myCoef*exp(-aVal*aVal*myCoef2);
836  }
837 
839  double mySigma;
840 
842  double myCoef;
843  private:
845  double myCoef2;
846  };
847 }
848 }
850 
851 
852 #endif // !defined BasicFunctors_h
853 
854 #undef BasicFunctors_RECURSES
855 #endif // else defined(BasicFunctors_RECURSES)
Aim: defines a functor on double numbers which corresponds to a Gaussian convolution kernel...
Aim: Define a simple functor that returns a constant value (0 by default).
Rescaling(const TInputType &initMin, const TInputType &initMax, const TOutputType &newMin, const TOutputType &newMax)
PredicateCombiner & operator=(const PredicateCombiner &other)
double operator()(const double aVal) const
T operator()(const T &x) const
const ReturnType & operator()(const TPair &aPair) const
T operator()(const T &x) const
Definition: BasicFunctors.h:96
PredicateCombiner(const Predicate1 &pred1, const Predicate2 &pred2, const TBinaryFunctor &boolFunctor)
Go to http://www.sgi.com/tech/stl/LessThanComparable.html.
Definition: Boost.dox:48
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.
Value operator()(const TInput &) const
PredicateCombiner< Tlow, Tup, AndBoolFct2 > CombinedPredicate
Composer(const TFunctor1 &aF1, const TFunctor2 &aF2)
Composer & operator=(const Composer &other)
T operator()(const T &x, const T &y) const
Definition: BasicFunctors.h:85
Thresholder< T, true, true > Tup
bool operator()(const Input &aI) const
Aim: Define a simple unary functor that returns a reference on the first member of a pair in order to...
Go to http://www.sgi.com/tech/stl/EqualityComparable.html.
Definition: Boost.dox:46
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.
T operator()(const T &a, const T &b) const
Definition: BasicFunctors.h:75
ReturnType operator()(const TInput &aInput) const
ReturnType operator()(const TPair &aPair) const
const ReturnType & operator()(const TPair &aPair) const
Aim: A small functor with an operator () that compares one value to a threshold value according to tw...
bool operator()(const Input &aI) const
Aim: A small functor with an operator () that compares one value to an interval.
Thresholder< T, false, true > Tlow
predicates type
TOutput operator()(const TInput &aInput) const
PredicateCombiner(const PredicateCombiner &other)
Quantity operator()(const Cell &) const
double mySigma
Sigma parameter.
bool operator()(const T &t) const
Aim: Functor allowing to rescale a value. Values of the initial scale [initMin,initMax] are rescaled ...
const Predicate2 * myPred2
aliasing pointer to the right predicate.
double myCoef
Temporary variable.
const TBinaryFunctor * myBoolFunctor
aliasing pointer to the binary functor.
ReturnType & operator()(TPair &aPair) const
const TFunctor1 * myF1
DGtal is the top-level namespace which contains all DGtal functions and types.
int max(int a, int b)
IntervalThresholder(const Input &low, const Input &up)
Aim: The predicate returns true when the given binary functor returns true for the two Predicates giv...
T operator()(const T &a, const T &b) const
Definition: BasicFunctors.h:67
ConstValueCell(const Quantity &aQuantity=0)
const Predicate1 * myPred1
aliasing pointer to the left predicate.
Duplicated STL functors.
Definition: BasicFunctors.h:64
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.
T operator()(const T &aT) const
TOutputType operator()(const TInputType &anInitVal) const
double myCoef2
Temporary variable.
Thresholder(const Input &aT=0)
ReturnType operator()(const TPair &aPair) const
ReturnType & operator()(TPair &aPair) const