DGtal  1.4.beta
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)
40 #define BasicFunctors_RECURSES
41 
42 #if !defined BasicFunctors_h
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 = void>
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 
150  template < typename T = void >
151  struct Round
152  {
153  inline
154  auto operator() ( const T & value ) const
155  -> decltype(std::round(value))
156  {
157  return std::round( value );
158  }
159  };
160 
167  template <>
168  struct Round<void>
169  {
170  template < typename T >
171  inline
172  auto operator() ( const T & value ) const
173  -> decltype(std::round(value))
174  {
175  return std::round( value );
176  }
177  };
178 
183  template < typename T = void >
184  struct Floor
185  {
186  inline
187  auto operator() ( const T & value ) const
188  -> decltype(std::floor(value))
189  {
190  return std::floor( value );
191  }
192  };
193 
200  template <>
201  struct Floor<void>
202  {
203  template < typename T >
204  inline
205  auto operator() ( const T & value ) const
206  -> decltype(std::floor(value))
207  {
208  return std::floor( value );
209  }
210  };
211 
216  template < typename T = void >
217  struct Ceil
218  {
219  inline
220  auto operator() ( const T & value ) const
221  -> decltype(std::ceil(value))
222  {
223  return std::ceil( value );
224  }
225  };
226 
233  template <>
234  struct Ceil<void>
235  {
236  template < typename T >
237  inline
238  auto operator() ( const T & value ) const
239  -> decltype(std::ceil(value))
240  {
241  return std::ceil( value );
242  }
243  };
244 
249  template < typename T = void >
250  struct Trunc
251  {
252  inline
253  auto operator() ( const T & value ) const
254  -> decltype(std::trunc(value))
255  {
256  return std::trunc( value );
257  }
258  };
259 
266  template <>
267  struct Trunc<void>
268  {
269  template < typename T >
270  inline
271  auto operator() ( const T & value ) const
272  -> decltype(std::trunc(value))
273  {
274  return std::trunc( value );
275  }
276  };
277 
279 // Some basic unary functors that may be useful
281 
287  struct Identity
288  {
294  template <typename T >
295  inline
296  T operator()(const T& aT) const
297  {
298  return aT;
299  }
300  };
301 
309  template <typename TValue>
311  {
312  public:
313  typedef TValue Value;
314 
319  ConstValue(const Value& aValue = 0)
320  :myValue(aValue) {};
321 
329  template <typename TInput>
330  inline
331  Value operator()(const TInput& /*aInput*/) const
332  {
333  return myValue;
334  }
335 
336  private:
341 
342  };
343 
352  template <typename TQuantity, typename TCell>
354  {
355  public:
356  typedef TCell Cell;
357  typedef TQuantity Quantity;
358 
363  ConstValueCell(const Quantity& aQuantity = 0)
364  :myQuantity(aQuantity) {}
365 
371  inline
372  Quantity operator()(const Cell& /*aInput*/) const
373  {
374  return myQuantity;
375  }
376 
377  private:
382 
383  };
384 
385 
392  template <typename TOutput >
393  struct Cast
394  {
400  template<typename TInput>
401  inline
402  TOutput operator()(const TInput& aInput) const
403  {
404  return static_cast<TOutput>(aInput);
405  }
406  };
407 
416  template <typename TFunctor1, typename TFunctor2, typename ReturnType >
417  class Composer
418  {
419  public:
421  typedef ReturnType Value;
422 
426  Composer(): myF1(NULL), myF2(NULL) {}
432  Composer(const TFunctor1& aF1, const TFunctor2& aF2): myF1(&aF1), myF2(&aF2) {}
437  Composer(const Composer& other): myF1(other.myF1), myF2(other.myF2) {}
438 
443  Composer& operator=(const Composer& other)
444  {
445  if (this != &other)
446  {
447  myF1 = other.myF1;
448  myF2 = other.myF2;
449  }
450  return *this;
451  }
452 
453 
466  template<typename TInput>
467  inline
468  ReturnType operator()(const TInput& aInput) const
469  {
470  ASSERT( myF1 );
471  ASSERT( myF2 );
472  return myF2->operator()( myF1->operator()( aInput ) );
473  }
474 
475  private:
479  const TFunctor1* myF1;
483  const TFunctor2* myF2;
484  };
485 
499 template <typename T, bool isLower = true, bool isEqual = true >
501  {
502  public:
505 
506  typedef T Input;
507 
512  Thresholder(const Input& aT = 0):myT(aT) {}
518  bool operator()(const Input& aI) const {
519  std::less_equal<Input> c;
520  return c(aI,myT);
521  }
522  private:
527 };
528 
529 //specializations
530 template <typename T>
531 struct Thresholder<T,false,false>
532 {
533 
534  public:
537 
538  typedef T Input;
539 
540  Thresholder(const Input& aT = 0):myT(aT) {}
541 
542  bool operator()(const Input& aI) const {
543  std::greater<Input> c;
544  return c(aI,myT);
545  }
546 
547  private:
549 };
550 template <typename T>
551 struct Thresholder<T,false,true>
552  {
553  public:
556 
557  typedef T Input;
558 
559  Thresholder(const Input& aT = 0):myT(aT) {}
560  bool operator()(const Input& aI) const {
561  std::greater_equal<Input> c;
562  return c(aI,myT);
563  }
564 
565  private:
567 };
568 
569 template <typename T>
570 struct Thresholder<T,true,false>
571  {
572  public:
575 
576  typedef T Input;
577 
578  Thresholder(const Input& aT = 0):myT(aT) {}
579 
580  bool operator()(const Input& aI) const {
581  std::less<Input> c;
582  return c(aI,myT);
583  }
584 
585  private:
587 };
588 
589 template <typename T>
590 struct Thresholder<T,true,true>
591  {
592  public:
595 
596  typedef T Input;
597 
598  Thresholder(const Input& aT = 0):myT(aT) {}
599 
600  bool operator()(const Input& aI) const {
601  std::less_equal<Input> c;
602  return c(aI,myT);
603  }
604 
605  private:
607 };
608 
610  // template class PredicateCombiner
620  template <typename TPredicate1, typename TPredicate2,
621  typename TBinaryFunctor = BoolFunctor2 >
623  {
624  typedef TPredicate1 Predicate1;
625  typedef TPredicate2 Predicate2;
626 
635  const Predicate2 & pred2,
636  const TBinaryFunctor & boolFunctor )
637  : myPred1( &pred1 ), myPred2( &pred2 ), myBoolFunctor( &boolFunctor )
638  {
639  }
640 
646  : myPred1( other.myPred1 ), myPred2( other.myPred2 ), myBoolFunctor( other.myBoolFunctor )
647  {
648  }
649 
656  {
657  if (this != &other)
658  {
659  myPred1 = other.myPred1;
660  myPred2 = other.myPred2;
662  }
663  return *this;
664  }
665 
670 
677  template<typename T>
678  bool operator()( const T & t ) const
679  {
680  return myBoolFunctor->operator()( myPred1->operator()( t ),
681  myPred2->operator()( t ) );
682  }
683 
689  const TBinaryFunctor* myBoolFunctor;
690  };
691 
699 template <typename T>
701 {
702 public:
705 
707  typedef T Input;
708 
713 
719  IntervalThresholder(const Input& low, const Input& up)
720  : myTlow( low), myTup ( up ),
721  myPred( myTlow, myTup, AndBoolFct2() ) {}
722 
728  bool operator()(const Input& aI) const
729  {
730  return myPred(aI);
731  }
732 private:
745 };
746 
747 
755  template <typename ReturnType>
756  class Pair1st
757  {
758  public:
759 
768  template <typename TPair>
769  inline
770  ReturnType operator()(const TPair& aPair) const
771  {
772  return aPair.first;
773  }
774 
775  };
776 
784  template <typename ReturnType>
785  class Pair2nd
786  {
787  public:
788 
797  template <typename TPair>
798  inline
799  ReturnType operator()(const TPair& aPair) const
800  {
801  return aPair.second;
802  }
803 
804  };
805 
814  template <typename ReturnType>
816  {
817  public:
818 
827  template <typename TPair>
828  inline
829  ReturnType& operator()(TPair& aPair) const
830  {
831  return aPair.first;
832  }
833 
842  template <typename TPair>
843  inline
844  const ReturnType& operator()(const TPair& aPair) const
845  {
846  return aPair.first;
847  }
848 
849  };
850 
859  template <typename ReturnType>
861  {
862  public:
863 
872  template <typename TPair>
873  inline
874  ReturnType& operator()(TPair& aPair) const
875  {
876  return aPair.second;
877  }
878 
887  template <typename TPair>
888  inline
889  const ReturnType& operator()(const TPair& aPair) const
890  {
891  return aPair.second;
892  }
893 
894  };
895 
904  template<typename TInputType, typename TOutputType>
905  struct Rescaling
906  {
907  TInputType myInitMin;
908  TInputType myInitMax;
909  TInputType myInitRange;
910 
911  TOutputType myNewMin;
912  TOutputType myNewMax;
913  TOutputType myNewRange;
914 
923  Rescaling( const TInputType &initMin, const TInputType &initMax, const TOutputType &newMin, const TOutputType &newMax ) :
924  myInitMin(initMin), myInitMax(initMax), myInitRange(initMax-initMin), myNewMin(newMin), myNewMax(newMax), myNewRange(newMax-newMin) {}
933  inline
934  TOutputType operator() (const TInputType& anInitVal) const
935  { return anInitVal<myInitMin ? myNewMin : anInitVal > myInitMax ? myNewMax : (anInitVal-myInitMin)*myNewRange/myInitRange + myNewMin; }
936  };
937 
938 
947  {
953  GaussianKernel(const double aSigma) :mySigma(aSigma)
954  {
955  myCoef = 1.0/(mySigma * sqrt(2.0*M_PI));
956  myCoef2 = 1.0/(2.0*M_PI);
957  }
958 
963  inline
964  double operator()(const double aVal) const
965  {
966  ASSERT((aVal <= 1) && (aVal>=0));
967  return myCoef*exp(-aVal*aVal*myCoef2);
968  }
969 
971  double mySigma;
972 
974  double myCoef;
975  private:
977  double myCoef2;
978  };
979 }
980 }
982 
983 
984 #endif // !defined BasicFunctors_h
985 
986 #undef BasicFunctors_RECURSES
987 #endif // else defined(BasicFunctors_RECURSES)
Aim: Define a new Functor from the composition of two other functors.
Composer(const TFunctor1 &aF1, const TFunctor2 &aF2)
Composer & operator=(const Composer &other)
const TFunctor2 * myF2
const TFunctor1 * myF1
ReturnType Value
Necessary for DistanceVisitor.
Composer(const Composer &other)
ReturnType operator()(const TInput &aInput) const
Aim: Define a simple functor that returns a constant quantity (0 by default).
Quantity operator()(const Cell &) const
ConstValueCell(const Quantity &aQuantity=0)
Aim: Define a simple functor that returns a constant value (0 by default).
Value operator()(const TInput &) const
ConstValue(const Value &aValue=0)
Aim: A small functor with an operator () that compares one value to an interval.
BOOST_CONCEPT_ASSERT((boost::LessThanComparable< T >))
Thresholder< T, true, true > Tup
PredicateCombiner< Tlow, Tup, AndBoolFct2 > CombinedPredicate
Thresholder< T, false, true > Tlow
predicates type
bool operator()(const Input &aI) const
BOOST_CONCEPT_ASSERT((boost::EqualityComparable< T >))
IntervalThresholder(const Input &low, const Input &up)
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
const ReturnType & operator()(const TPair &aPair) const
Aim: Define a simple functor that returns the first member of a pair.
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...
const ReturnType & operator()(const TPair &aPair) const
ReturnType & operator()(TPair &aPair) const
Aim: Define a simple functor that returns the second member of a pair.
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
Thresholder(const Input &aT=0)
BOOST_CONCEPT_ASSERT((boost::LessThanComparable< T >))
BOOST_CONCEPT_ASSERT((boost::EqualityComparable< T >))
boost::function2< bool, bool, bool > BoolFunctor2
DGtal is the top-level namespace which contains all DGtal functions and types.
T operator()(const T &x) const
Definition: BasicFunctors.h:96
Aim: Define a simple functor using the static cast operator.
TOutput operator()(const TInput &aInput) const
Functor that rounds up.
auto operator()(const T &value) const -> decltype(std::ceil(value))
Functor that rounds down.
auto operator()(const T &value) const -> decltype(std::floor(value))
Aim: defines a functor on double numbers which corresponds to a Gaussian convolution kernel....
double myCoef2
Temporary variable.
double mySigma
Sigma parameter.
double myCoef
Temporary variable.
GaussianKernel(const double aSigma)
double operator()(const double aVal) const
Aim: Define a simple default functor that just returns its argument.
T operator()(const T &aT) const
T operator()(const T &a, const T &b) const
Definition: BasicFunctors.h:75
Duplicated STL functors.
Definition: BasicFunctors.h:65
T operator()(const T &a, const T &b) const
Definition: BasicFunctors.h:67
T operator()(const T &x, const T &y) const
Definition: BasicFunctors.h:85
Aim: The predicate returns true when the given binary functor returns true for the two Predicates giv...
PredicateCombiner & operator=(const PredicateCombiner &other)
PredicateCombiner(const Predicate1 &pred1, const Predicate2 &pred2, const TBinaryFunctor &boolFunctor)
const TBinaryFunctor * myBoolFunctor
aliasing pointer to the binary functor.
PredicateCombiner(const PredicateCombiner &other)
const Predicate2 * myPred2
aliasing pointer to the right predicate.
const Predicate1 * myPred1
aliasing pointer to the left predicate.
bool operator()(const T &t) const
Aim: Functor allowing to rescale a value. Values of the initial scale [initMin,initMax] are rescaled ...
TOutputType operator()(const TInputType &anInitVal) const
Rescaling(const TInputType &initMin, const TInputType &initMax, const TOutputType &newMin, const TOutputType &newMax)
Functor that rounds to the nearest integer.
auto operator()(const T &value) const -> decltype(std::round(value))
BOOST_CONCEPT_ASSERT((boost::LessThanComparable< T >))
BOOST_CONCEPT_ASSERT((boost::EqualityComparable< T >))
BOOST_CONCEPT_ASSERT((boost::EqualityComparable< T >))
BOOST_CONCEPT_ASSERT((boost::LessThanComparable< T >))
BOOST_CONCEPT_ASSERT((boost::EqualityComparable< T >))
BOOST_CONCEPT_ASSERT((boost::LessThanComparable< T >))
BOOST_CONCEPT_ASSERT((boost::EqualityComparable< T >))
BOOST_CONCEPT_ASSERT((boost::LessThanComparable< T >))
Functor that rounds towards zero.
auto operator()(const T &value) const -> decltype(std::trunc(value))
T operator()(const T &x) const
Go to http://www.sgi.com/tech/stl/EqualityComparable.html.
Definition: Boost.dox:46
Go to http://www.sgi.com/tech/stl/LessThanComparable.html.
Definition: Boost.dox:48
int max(int a, int b)