ITK  5.3.0
Insight Toolkit
itkArithmeticOpsFunctors.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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
40  operator!=(const Add2 &) const
41  {
42  return false;
43  }
45 
46  bool
47  operator==(const Add2 & other) const
48  {
49  return !(*this != other);
50  }
51 
52  inline TOutput
53  operator()(const TInput1 & A, const TInput2 & B) const
54  {
55  return static_cast<TOutput>(A + B);
56  }
57 };
58 
59 
65 template <typename TInput1, typename TInput2, typename TInput3, typename TOutput>
66 class ITK_TEMPLATE_EXPORT Add3
67 {
68 public:
69  Add3() = default;
70  ~Add3() = default;
71  bool
72  operator!=(const Add3 &) const
73  {
74  return false;
75  }
77 
78  bool
79  operator==(const Add3 & other) const
80  {
81  return !(*this != other);
82  }
83 
84  inline TOutput
85  operator()(const TInput1 & A, const TInput2 & B, const TInput3 & C) const
86  {
87  return static_cast<TOutput>(A + B + C);
88  }
89 };
90 
91 
97 template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
98 class ITK_TEMPLATE_EXPORT Sub2
99 {
100 public:
101  Sub2() = default;
102  ~Sub2() = default;
103  bool
104  operator!=(const Sub2 &) const
105  {
106  return false;
107  }
109 
110  bool
111  operator==(const Sub2 & other) const
112  {
113  return !(*this != other);
114  }
115 
116  inline TOutput
117  operator()(const TInput1 & A, const TInput2 & B) const
118  {
119  return static_cast<TOutput>(A - B);
120  }
121 };
122 
123 
129 template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
130 class ITK_TEMPLATE_EXPORT Mult
131 {
132 public:
133  Mult() = default;
134  ~Mult() = default;
135  bool
136  operator!=(const Mult &) const
137  {
138  return false;
139  }
141 
142  bool
143  operator==(const Mult & other) const
144  {
145  return !(*this != other);
146  }
147 
148  inline TOutput
149  operator()(const TInput1 & A, const TInput2 & B) const
150  {
151  return static_cast<TOutput>(A * B);
152  }
153 };
154 
155 
161 template <typename TInput1, typename TInput2, typename TOutput>
162 class ITK_TEMPLATE_EXPORT Div
163 {
164 public:
165  Div() = default;
166  ~Div() = default;
167  bool
168  operator!=(const Div &) const
169  {
170  return false;
171  }
173 
174  bool
175  operator==(const Div & other) const
176  {
177  return !(*this != other);
178  }
179 
180  inline TOutput
181  operator()(const TInput1 & A, const TInput2 & B) const
182  {
184  {
185  return (TOutput)(A / B);
186  }
187  else
188  {
189  return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
190  }
191  }
192 };
193 
194 
200 template <typename TNumerator, typename TDenominator = TNumerator, typename TOutput = TNumerator>
201 class ITK_TEMPLATE_EXPORT DivideOrZeroOut
202 {
203 public:
205  {
206  m_Threshold = 1e-5 * NumericTraits<TDenominator>::OneValue();
207  m_Constant = NumericTraits<TOutput>::ZeroValue();
208  };
210 
211  ~DivideOrZeroOut() = default;
212 
213  bool
214  operator!=(const DivideOrZeroOut & other) const
215  {
216  return !(*this == other);
217  }
218 
219  bool
220  operator==(const DivideOrZeroOut & itkNotUsed(other)) const
221  {
222  // Always return true for now. Do a comparison to m_Threshold if it is
223  // every made set-able.
224  return true;
225  }
226 
227  inline TOutput
228  operator()(const TNumerator & n, const TDenominator & d) const
229  {
230  if (d < m_Threshold)
231  {
232  return m_Constant;
233  }
234  return static_cast<TOutput>(n) / static_cast<TOutput>(d);
235  }
236  TDenominator m_Threshold;
237  TOutput m_Constant;
238 };
239 
240 
246 template <typename TInput1, typename TInput2, typename TOutput>
247 class ITK_TEMPLATE_EXPORT Modulus
248 {
249 public:
250  Modulus() = default;
251  ~Modulus() = default;
253 
254  bool
255  operator!=(const Modulus &) const
256  {
257  return false;
258  }
259 
260  bool
261  operator==(const Modulus & other) const
262  {
263  return !(*this != other);
264  }
265 
266  inline TOutput
267  operator()(const TInput1 & A, const TInput2 & B) const
268  {
270  {
271  return static_cast<TOutput>(A % B);
272  }
273  else
274  {
275  return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
276  }
277  }
278 };
279 
280 #if !defined(ITK_FUTURE_LEGACY_REMOVE)
281 
290 template <typename TInput, typename TOutput>
291 class ITK_TEMPLATE_EXPORT ModulusTransform
292 {
293 public:
294  ModulusTransform() { m_Dividend = 5; }
295  ~ModulusTransform() = default;
296  void
297  SetDividend(TOutput dividend)
298  {
299  m_Dividend = dividend;
300  }
302 
303  bool
304  operator!=(const ModulusTransform & other) const
305  {
306  if (m_Dividend != other.m_Dividend)
307  {
308  return true;
309  }
310  return false;
311  }
312 
313  bool
314  operator==(const ModulusTransform & other) const
315  {
316  return !(*this != other);
317  }
318 
319  inline TOutput
320  operator()(const TInput & x) const
321  {
322  auto result = static_cast<TOutput>(x % m_Dividend);
323 
324  return result;
325  }
326 
327 private:
328  TInput m_Dividend;
329 };
330 
331 #endif
332 
342 template <class TInput1, class TInput2, class TOutput>
343 class DivFloor
344 {
345 public:
346  bool
347  operator!=(const DivFloor &) const
348  {
349  return false;
350  }
351 
352  bool
353  operator==(const DivFloor & other) const
354  {
355  return !(*this != other);
356  }
357 
358  inline TOutput
359  operator()(const TInput1 & A, const TInput2 & B) const
360  {
361  const double temp = std::floor(double(A) / double(B));
362  if (NumericTraits<TOutput>::IsInteger && Math::isinf(temp))
363  {
364  if (temp > 0)
365  {
366  return NumericTraits<TOutput>::max(A);
367  }
368  else
369  {
371  }
372  }
373  return static_cast<TOutput>(temp);
374  }
375 };
376 
388 template <class TInput1, class TInput2, class TOutput>
389 class DivReal
390 {
391 public:
392  // Use default copy, assigned and destructor
393  bool
394  operator!=(const DivReal &) const
395  {
396  return false;
397  }
398 
399  bool
400  operator==(const DivReal & other) const
401  {
402  return !(*this != other);
403  }
404 
405  inline TOutput
406  operator()(const TInput1 & A, const TInput2 & B) const
407  {
408  return static_cast<TOutput>(static_cast<typename NumericTraits<TInput1>::RealType>(A) /
409  static_cast<typename NumericTraits<TInput2>::RealType>(B));
410  }
411 };
419 template <class TInput1, class TOutput = TInput1>
421 {
422 public:
423  UnaryMinus() = default;
424  ~UnaryMinus() = default;
425  bool
426  operator!=(const UnaryMinus &) const
427  {
428  return false;
429  }
431 
432  bool
433  operator==(const UnaryMinus & other) const
434  {
435  return !(*this != other);
436  }
437 
438  inline TOutput
439  operator()(const TInput1 & A) const
440  {
441  return (TOutput)(-A);
442  }
443 };
444 } // namespace Functor
445 } // namespace itk
446 
447 #endif
itk::Functor::Mult::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:149
itk::Functor::UnaryMinus::operator()
TOutput operator()(const TInput1 &A) const
Definition: itkArithmeticOpsFunctors.h:439
itk::Functor::DivReal::operator==
bool operator==(const DivReal &other) const
Definition: itkArithmeticOpsFunctors.h:400
itk::Functor::UnaryMinus::UnaryMinus
UnaryMinus()=default
itk::Functor::Mult::operator==
bool operator==(const Mult &other) const
Definition: itkArithmeticOpsFunctors.h:143
itk::Functor::UnaryMinus::operator!=
bool operator!=(const UnaryMinus &) const
Definition: itkArithmeticOpsFunctors.h:426
itk::Functor::Add2::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:53
itk::Functor::UnaryMinus::operator==
bool operator==(const UnaryMinus &other) const
Definition: itkArithmeticOpsFunctors.h:433
itk::Functor::Add3::operator==
bool operator==(const Add3 &other) const
Definition: itkArithmeticOpsFunctors.h:79
itk::NumericTraits::NonpositiveMin
static constexpr T NonpositiveMin()
Definition: itkNumericTraits.h:97
itk::Functor::Sub2
Definition: itkArithmeticOpsFunctors.h:98
itk::Functor::Add3
Definition: itkArithmeticOpsFunctors.h:66
itk::Functor::Modulus
Definition: itkArithmeticOpsFunctors.h:247
itk::Functor::Sub2::operator!=
bool operator!=(const Sub2 &) const
Definition: itkArithmeticOpsFunctors.h:104
itk::Functor::DivideOrZeroOut::operator==
bool operator==(const DivideOrZeroOut &) const
Definition: itkArithmeticOpsFunctors.h:220
itk::Functor::Add2::operator!=
bool operator!=(const Add2 &) const
Definition: itkArithmeticOpsFunctors.h:40
itk::Functor::DivFloor::operator!=
bool operator!=(const DivFloor &) const
Definition: itkArithmeticOpsFunctors.h:347
itk::Functor::Modulus::operator!=
bool operator!=(const Modulus &) const
Definition: itkArithmeticOpsFunctors.h:255
itk::Functor::DivideOrZeroOut::operator()
TOutput operator()(const TNumerator &n, const TDenominator &d) const
Definition: itkArithmeticOpsFunctors.h:228
itk::Functor::DivideOrZeroOut
Definition: itkArithmeticOpsFunctors.h:201
itk::Functor::Modulus::operator==
bool operator==(const Modulus &other) const
Definition: itkArithmeticOpsFunctors.h:261
itk::Functor::Add3::operator!=
bool operator!=(const Add3 &) const
Definition: itkArithmeticOpsFunctors.h:72
itk::Functor::Add2
Definition: itkArithmeticOpsFunctors.h:34
itk::Functor::Sub2::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:117
itk::Functor::Add3::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B, const TInput3 &C) const
Definition: itkArithmeticOpsFunctors.h:85
itk::NumericTraits::OneValue
static T OneValue()
Definition: itkNumericTraits.h:156
itk::Math::NotAlmostEquals
bool NotAlmostEquals(T1 x1, T2 x2)
Definition: itkMath.h:693
itk::Functor::Add2::operator==
bool operator==(const Add2 &other) const
Definition: itkArithmeticOpsFunctors.h:47
itk::Functor::Div::operator==
bool operator==(const Div &other) const
Definition: itkArithmeticOpsFunctors.h:175
itk::Functor::DivideOrZeroOut::m_Constant
TOutput m_Constant
Definition: itkArithmeticOpsFunctors.h:237
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:532
itk::Functor::DivReal::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:406
itk::Functor::DivFloor
Cast arguments to double, performs division then takes the floor.
Definition: itkArithmeticOpsFunctors.h:343
itk::Functor::DivideOrZeroOut::m_Threshold
TDenominator m_Threshold
Definition: itkArithmeticOpsFunctors.h:236
itk::Functor::Modulus::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:267
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:167
itk::Functor::DivideOrZeroOut::DivideOrZeroOut
DivideOrZeroOut()
Definition: itkArithmeticOpsFunctors.h:204
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itk::operator!=
bool operator!=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:539
itk::Functor::Mult::operator!=
bool operator!=(const Mult &) const
Definition: itkArithmeticOpsFunctors.h:136
itk::Functor::DivFloor::operator==
bool operator==(const DivFloor &other) const
Definition: itkArithmeticOpsFunctors.h:353
itk::Functor::DivReal
Promotes arguments to real type and performs division.
Definition: itkArithmeticOpsFunctors.h:389
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Functor::UnaryMinus
Apply the unary minus operator.
Definition: itkArithmeticOpsFunctors.h:420
itk::Functor::Div::operator!=
bool operator!=(const Div &) const
Definition: itkArithmeticOpsFunctors.h:168
itk::Functor::Div
Definition: itkArithmeticOpsFunctors.h:162
itk::Math::e
static constexpr double e
Definition: itkMath.h:54
itk::Functor::UnaryMinus::~UnaryMinus
~UnaryMinus()=default
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:84
itk::Functor::DivideOrZeroOut::operator!=
bool operator!=(const DivideOrZeroOut &other) const
Definition: itkArithmeticOpsFunctors.h:214
itk::Functor::Sub2::operator==
bool operator==(const Sub2 &other) const
Definition: itkArithmeticOpsFunctors.h:111
itk::Functor::DivFloor::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:359
itk::Functor::Mult
Definition: itkArithmeticOpsFunctors.h:130
itkMath.h
itk::Functor::DivReal::operator!=
bool operator!=(const DivReal &) const
Definition: itkArithmeticOpsFunctors.h:394
itk::Functor::Div::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:181