ITK  5.0.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() = default;
38  ~Add2() = default;
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() = default;
67  ~Add3() = default;
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() = default;
96  ~Sub2() = default;
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() = default;
123  ~Mult() = default;
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() = default;
150  ~Div() = default;
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 
192  ~DivideOrZeroOut() = default;
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() = default;
228  ~Modulus() = default;
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 
254 #if ! defined ( ITK_FUTURE_LEGACY_REMOVE )
255 
263 template< typename TInput, typename TOutput >
264 class ITK_TEMPLATE_EXPORT ModulusTransform
265 {
266 public:
267  ModulusTransform() { m_Dividend = 5; }
268  ~ModulusTransform() = default;
269  void SetDividend(TOutput dividend) { m_Dividend = dividend; }
270 
271  bool operator!=(const ModulusTransform & other) const
272  {
273  if ( m_Dividend != other.m_Dividend )
274  {
275  return true;
276  }
277  return false;
278  }
279 
280  bool operator==(const ModulusTransform & other) const
281  {
282  return !( *this != other );
283  }
284 
285  inline TOutput operator()(const TInput & x) const
286  {
287  auto result = static_cast< TOutput >( x % m_Dividend );
288 
289  return result;
290  }
291 
292 private:
293  TInput m_Dividend;
294 };
295 
296 #endif
297 
307 template< class TInput1, class TInput2, class TOutput >
308 class DivFloor
309 {
310 public:
311 
312  bool operator!=(const DivFloor &) const
313  {
314  return false;
315  }
316 
317  bool operator==(const DivFloor & other) const
318  {
319  return !( *this != other );
320  }
321 
322  inline TOutput operator()(const TInput1 & A, const TInput2 & B) const
323  {
324  const double temp = std::floor( double(A) / double(B) );
325  if(NumericTraits< TOutput >::IsInteger && Math::isinf(temp))
326  {
327  if ( temp > 0 )
328  {
329  return NumericTraits< TOutput >::max( A );
330  }
331  else
332  {
334  }
335  }
336  return static_cast< TOutput >(temp);
337  }
338 };
339 
351 template< class TInput1, class TInput2, class TOutput >
352 class DivReal
353 {
354 public:
355  // Use default copy, assigned and destructor
356  bool operator!=(const DivReal &) const
357  {
358  return false;
359  }
360 
361  bool operator==(const DivReal & other) const
362  {
363  return !( *this != other );
364  }
365 
366  inline TOutput operator()(const TInput1 & A, const TInput2 & B) const
367  {
368  return static_cast<TOutput>( static_cast<typename NumericTraits<TInput1>::RealType>(A)
369  /
370  static_cast<typename NumericTraits<TInput2>::RealType >(B) );
371  }
372 };
380 template< class TInput1, class TOutput = TInput1 >
382 {
383 public:
384  UnaryMinus() = default;
385  ~UnaryMinus() = default;
386  bool operator!=(const UnaryMinus &) const
387  {
388  return false;
389  }
391 
392  bool operator==(const UnaryMinus & other) const
393  {
394  return !( *this != other );
395  }
396 
397  inline TOutput operator()(const TInput1 & A ) const
398  { return (TOutput)( -A ); }
399 };
400 }
401 }
402 
403 #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
Define numeric traits for std::vector.
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 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
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
bool operator==(const Sub2 &other) const
Cast arguments to double, performs division then takes the floor.
TOutput operator()(const TInput1 &A, const TInput2 &B) const
static constexpr double e
The base of the natural logarithm or Euler&#39;s number
Definition: itkMath.h:53
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
bool operator!=(const Div &) const
bool NotAlmostEquals(T1 x1, T2 x2)
Definition: itkMath.h:677
Apply the unary minus operator.
bool operator!=(const DivReal &) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const