DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkUnaryFunctorLookUpTable.h
Go to the documentation of this file.
1 
18 #ifndef __itkUnaryFunctorLookUpTable_h
19 #define __itkUnaryFunctorLookUpTable_h
20 
21 #include "itkFunctorTableBase.h"
22 
23 namespace itk
24 {
25 
33 template < class TFunctor >
34 class ITK_EXPORT UnaryFunctorLookUpTable
35  : public FunctorTableBase<TFunctor, double, double>
36 {
37 public:
41  typedef SmartPointer<Self> Pointer;
42  typedef SmartPointer<const Self> ConstPointer;
43 
45  itkNewMacro(Self);
46 
49 
53 
55  typedef typename Superclass::STDVectorPointer STDVectorPointer;
56 
57  itkSetMacro(VariableMax, double);
58  itkGetMacro(VariableMax, double);
59 
60  itkSetMacro(VariableMin, double);
61  itkGetMacro(VariableMin, double);
62 
63  itkSetMacro(NumberOfBins, int);
64  itkGetMacro(NumberOfBins, int);
65 
66  itkGetMacro(Table, STDVectorPointer);
67 
69  {
70  BuildTable();
71  }
72 
73  void BuildTable()
74  {
75  utlGlobalException(m_NumberOfBins<=0, "need to compute m_NumberOfBins first");
76  utlGlobalException(m_VariableMax<=m_VariableMin, "m_VariableMax should be larger than m_VariableMin");
77  m_Table = STDVectorPointer(new STDVectorType(m_NumberOfBins+1));
78  m_Delta = (m_VariableMax-m_VariableMin)/(double)m_NumberOfBins;
79  m_DeltaInv = 1.0/m_Delta;
80 
81  for (int i=0; i<= m_NumberOfBins; i++)
82  (*m_Table)[i] = this->m_Functor( m_Delta*i + m_VariableMin );
83  }
84 
85  unsigned long GetTableSize() const
86  {
87  return m_Table->size();
88  }
89 
90  bool IsTableBuilt() const
91  {
92  return m_NumberOfBins>0 && m_Table->size()==m_NumberOfBins+1;
93  }
94 
95  FunctorValueType GetFunctionValue ( const ParametersType& var)
96  {
97  utlGlobalException(m_Table->size()==0, "need to compute m_Table first");
98  if (var<=m_VariableMin)
99  return (*m_Table)[0];
100  if (var>=m_VariableMax)
101  return m_Table->back();
102 
103  double xDouble = (var-m_VariableMin)*m_DeltaInv;
104  int x = (int) std::floor(xDouble);
105  return (*m_Table)[x] + ((*m_Table)[x+1]-(*m_Table)[x])*(xDouble-x);
106  }
107 
108 protected:
109  UnaryFunctorLookUpTable() : Superclass(),
110  m_Table(new STDVectorType())
111  {
112  m_VariableMax = 0;
113  m_VariableMin = 0;
114  m_Delta = 0;
115  m_DeltaInv = 0;
116  m_NumberOfBins = -1;
117  }
119 
120  void PrintSelf(std::ostream& os, Indent indent) const ITK_OVERRIDE
121  {
122  Superclass::PrintSelf(os, indent);
123  PrintVar4(true, m_VariableMax, m_VariableMin, m_NumberOfBins, m_Delta, os<<indent);
124  for (int i=0; i<= m_NumberOfBins; i++)
125  utlPrintVar3(true, i, m_Delta*i+m_VariableMin, (*m_Table)[i]);
126  }
127 
128  typename LightObject::Pointer InternalClone() const ITK_OVERRIDE
129  {
130  typename LightObject::Pointer loPtr = Superclass::InternalClone();
131  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
132  if(rval.IsNull())
133  {
134  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
135  }
136  rval->m_VariableMax = m_VariableMax;
137  rval->m_VariableMin = m_VariableMin;
138  rval->m_Delta = m_Delta;
139  rval->m_DeltaInv = m_DeltaInv;
140  rval->m_NumberOfBins = m_NumberOfBins;
141 
142  rval->m_Table = m_Table;
143  return loPtr;
144  }
145 
148  double m_Delta;
149  double m_DeltaInv;
150 
153 
155  STDVectorPointer m_Table;
156 
157 
158 private:
159  UnaryFunctorLookUpTable(const Self&); //purposely not implemented
160  void operator=(const Self&); //purposely not implemented
161 
162 };
163 
164 
165 }
166 
167 
168 
169 #endif
170 
Superclass::FunctorValueType FunctorValueType
use UnaryFunctorLookUpTable to accelerate evaluation of functions.
#define utlPrintVar3(cond, var1, var2, var3)
Definition: utlCoreMacro.h:558
FunctorValueType GetFunctionValue(const ParametersType &var)
Superclass::ParametersType ParametersType
SmartPointer< const Self > ConstPointer
Superclass::STDVectorType STDVectorType
#define ITK_OVERRIDE
Definition: utlITKMacro.h:46
LightObject::Pointer InternalClone() const ITK_OVERRIDE
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
FunctorTableBase< TFunctor, double, double > Superclass
Superclass::STDVectorPointer STDVectorPointer
#define PrintVar4(cond, var1, var2, var3, var4, os)
Definition: utlCoreMacro.h:470
use FunctorTableBase to accelerate evaluation of functions.