ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkArithmeticOpsFunctors.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkArithmeticOpsFunctors_h
19 #define itkArithmeticOpsFunctors_h
20 
21 #include "itkMath.h"
22 
23 namespace itk
24 {
25 namespace Functor
26 {
27 
33 template< typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1 >
34 class ITK_TEMPLATE_EXPORT Add2
35 {
36 public:
37  Add2() {}
38  ~Add2() {}
39  bool operator!=(const Add2 &) const
40  {
41  return false;
42  }
44 
45  bool operator==(const Add2 & other) const
46  {
47  return !( *this != other );
48  }
49 
50  inline TOutput operator()(const TInput1 & A, const TInput2 & B) const
51  {
52  return static_cast< TOutput >( A + B );
53  }
54 };
55 
56 
62 template< typename TInput1, typename TInput2, typename TInput3, typename TOutput >
63 class ITK_TEMPLATE_EXPORT Add3
64 {
65 public:
66  Add3() {}
67  ~Add3() {}
68  bool operator!=(const Add3 &) const
69  {
70  return false;
71  }
73 
74  bool operator==(const Add3 & other) const
75  {
76  return !( *this != other );
77  }
78 
79  inline TOutput operator()(const TInput1 & A,
80  const TInput2 & B,
81  const TInput3 & C) const
82  { return static_cast<TOutput>( A + B + C ); }
83 };
84 
85 
91 template< typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1 >
92 class ITK_TEMPLATE_EXPORT Sub2
93 {
94 public:
95  Sub2() {}
96  ~Sub2() {}
97  bool operator!=(const Sub2 &) const
98  {
99  return false;
100  }
102 
103  bool operator==(const Sub2 & other) const
104  {
105  return !( *this != other );
106  }
107 
108  inline TOutput operator()(const TInput1 & A, const TInput2 & B) const
109  { return static_cast<TOutput>( A - B ); }
110 };
111 
112 
118 template< typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1 >
119 class ITK_TEMPLATE_EXPORT Mult
120 {
121 public:
122  Mult() {}
123  ~Mult() {}
124  bool operator!=(const Mult &) const
125  {
126  return false;
127  }
129 
130  bool operator==(const Mult & other) const
131  {
132  return !( *this != other );
133  }
134 
135  inline TOutput operator()(const TInput1 & A, const TInput2 & B) const
136  { return static_cast<TOutput>( A * B ); }
137 };
138 
139 
145 template< typename TInput1, typename TInput2, typename TOutput >
146 class ITK_TEMPLATE_EXPORT Div
147 {
148 public:
149  Div() {}
150  ~Div() {}
151  bool operator!=(const Div &) const
152  {
153  return false;
154  }
156 
157  bool operator==(const Div & other) const
158  {
159  return !( *this != other );
160  }
161 
162  inline TOutput operator()(const TInput1 & A, const TInput2 & B) const
163  {
165  {
166  return (TOutput)( A / B );
167  }
168  else
169  {
170  return NumericTraits< TOutput >::max( static_cast<TOutput>(A) );
171  }
172  }
173 };
174 
175 
181 template< typename TNumerator, typename TDenominator=TNumerator, typename TOutput=TNumerator >
182 class ITK_TEMPLATE_EXPORT DivideOrZeroOut
183 {
184 public:
186  {
187  m_Threshold = 1e-5 * NumericTraits< TDenominator >::OneValue();
189  };
191 
193 
194  bool operator!=( const DivideOrZeroOut & other ) const
195  {
196  return !(*this == other);
197  }
198 
199  bool operator==( const DivideOrZeroOut & itkNotUsed(other) ) const
200  {
201  // Always return true for now. Do a comparison to m_Threshold if it is
202  // every made set-able.
203  return true;
204  }
205 
206  inline TOutput operator()( const TNumerator & n, const TDenominator & d ) const
207  {
208  if ( d < m_Threshold )
209  {
210  return m_Constant;
211  }
212  return static_cast< TOutput >( n ) / static_cast< TOutput >( d );
213  }
214  TDenominator m_Threshold;
215  TOutput m_Constant;
216 };
217 
218 
223 template< typename TInput1, typename TInput2, typename TOutput >
224 class ITK_TEMPLATE_EXPORT Modulus
225 {
226 public:
227  Modulus() { }
228  ~Modulus() {}
229 
230  bool operator!=(const Modulus &) const
231  {
232  return false;
233  }
234 
235  bool operator==(const Modulus & other) const
236  {
237  return !( *this != other );
238  }
239 
240  inline TOutput operator()(const TInput1 & A, const TInput2 & B) const
241  {
243  {
244  return static_cast< TOutput >( A % B );
245  }
246  else
247  {
248  return NumericTraits< TOutput >::max( static_cast<TOutput>(A) );
249  }
250  }
251 
252 };
253 
261 template< typename TInput, typename TOutput >
262 class ITK_TEMPLATE_EXPORT ModulusTransform
263 {
264 public:
265  ModulusTransform() { m_Dividend = 5; }
267  void SetDividend(TOutput dividend) { m_Dividend = dividend; }
268 
269  bool operator!=(const ModulusTransform & other) const
270  {
271  if ( m_Dividend != other.m_Dividend )
272  {
273  return true;
274  }
275  return false;
276  }
277 
278  bool operator==(const ModulusTransform & other) const
279  {
280  return !( *this != other );
281  }
282 
283  inline TOutput operator()(const TInput & x) const
284  {
285  TOutput result = static_cast< TOutput >( x % m_Dividend );
286 
287  return result;
288  }
289 
290 private:
291  TInput m_Dividend;
292 };
293 
303 template< class TInput1, class TInput2, class TOutput >
304 class DivFloor
305 {
306 public:
307 
308  bool operator!=(const DivFloor &) const
309  {
310  return false;
311  }
312 
313  bool operator==(const DivFloor & other) const
314  {
315  return !( *this != other );
316  }
317 
318  inline TOutput operator()(const TInput1 & A, const TInput2 & B) const
319  {
320  const double temp = std::floor( double(A) / double(B) );
322  {
323  if ( temp > 0 )
324  {
325  return NumericTraits< TOutput >::max( A );
326  }
327  else
328  {
330  }
331  }
332  return static_cast< TOutput >(temp);
333  }
334 };
335 
347 template< class TInput1, class TInput2, class TOutput >
348 class DivReal
349 {
350 public:
351  // Use default copy, assigned and destructor
352  bool operator!=(const DivReal &) const
353  {
354  return false;
355  }
356 
357  bool operator==(const DivReal & other) const
358  {
359  return !( *this != other );
360  }
361 
362  inline TOutput operator()(const TInput1 & A, const TInput2 & B) const
363  {
364  return static_cast<TOutput>( static_cast<typename NumericTraits<TInput1>::RealType>(A)
365  /
366  static_cast<typename NumericTraits<TInput2>::RealType >(B) );
367  }
368 };
376 template< class TInput1, class TOutput = TInput1 >
378 {
379 public:
382  bool operator!=(const UnaryMinus &) const
383  {
384  return false;
385  }
387 
388  bool operator==(const UnaryMinus & other) const
389  {
390  return !( *this != other );
391  }
392 
393  inline TOutput operator()(const TInput1 & A ) const
394  { return (TOutput)( -A ); }
395 };
396 }
397 }
398 
399 #endif
TOutput operator()(const TInput1 &A, const TInput2 &B) const
bool operator!=(const Modulus &) const
bool operator==(const DivReal &other) const
bool operator==(const Add2 &other) const
TOutput operator()(const TNumerator &n, const TDenominator &d) const
TOutput operator()(const TInput &x) const
bool operator!=(const Mult &) const
bool operator!=(const DivideOrZeroOut &other) const
bool operator!=(const UnaryMinus &) const
TOutput operator()(const TInput1 &A, const TInput2 &B, const TInput3 &C) const
bool operator==(const Modulus &other) const
bool isinf(const T value)
Definition: itkMath.h:790
bool operator==(const ModulusTransform &other) const
bool operator==(const UnaryMinus &other) const
bool operator==(const Div &other) const
bool operator==(const DivideOrZeroOut &) const
bool operator==(const DivFloor &other) const
bool operator==(const Mult &other) const
static ITK_CONSTEXPR_FUNC T max(const T &)
bool operator!=(const DivFloor &) const
Promotes arguments to real type and performs division.
bool operator==(const Add3 &other) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
TOutput operator()(const TInput1 &A) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
bool operator!=(const Sub2 &) const
int floor(const T x)
Definition: itkMath.h:799
bool operator==(const Sub2 &other) const
Cast arguments to double, performs division then takes the floor.
static ITK_CONSTEXPR_FUNC T NonpositiveMin()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
bool operator!=(const Add3 &) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
bool operator!=(const Add2 &) const
bool operator!=(const ModulusTransform &other) const
Define additional traits for native types such as int or float.
bool operator!=(const Div &) const
bool NotAlmostEquals(T1 x1, T2 x2)
Definition: itkMath.h:680
Apply the unary minus operator.
static ITK_CONSTEXPR_VAR double e
The base of the natural logarithm or Euler&#39;s number
Definition: itkMath.h:56
bool operator!=(const DivReal &) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const