ITK  5.4.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  * https://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  bool
38  operator==(const Add2 &) const
39  {
40  return true;
41  }
42 
43  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Add2);
44 
45  inline TOutput
46  operator()(const TInput1 & A, const TInput2 & B) const
47  {
48  return static_cast<TOutput>(A + B);
49  }
50 };
51 
52 
58 template <typename TInput1, typename TInput2, typename TInput3, typename TOutput>
59 class ITK_TEMPLATE_EXPORT Add3
60 {
61 public:
62  bool
63  operator==(const Add3 &) const
64  {
65  return true;
66  }
67 
68  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Add3);
69 
70  inline TOutput
71  operator()(const TInput1 & A, const TInput2 & B, const TInput3 & C) const
72  {
73  return static_cast<TOutput>(A + B + C);
74  }
75 };
76 
77 
83 template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
84 class ITK_TEMPLATE_EXPORT Sub2
85 {
86 public:
87  bool
88  operator==(const Sub2 &) const
89  {
90  return true;
91  }
92 
93  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Sub2);
94 
95  inline TOutput
96  operator()(const TInput1 & A, const TInput2 & B) const
97  {
98  return static_cast<TOutput>(A - B);
99  }
100 };
101 
102 
108 template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
109 class ITK_TEMPLATE_EXPORT Mult
110 {
111 public:
112  bool
113  operator==(const Mult &) const
114  {
115  return true;
116  }
117 
118  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Mult);
119 
120  inline TOutput
121  operator()(const TInput1 & A, const TInput2 & B) const
122  {
123  return static_cast<TOutput>(A * B);
124  }
125 };
126 
127 
133 template <typename TInput1, typename TInput2, typename TOutput>
134 class ITK_TEMPLATE_EXPORT Div
135 {
136 public:
137  bool
138  operator==(const Div &) const
139  {
140  return true;
141  }
142 
143  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Div);
144 
145  inline TOutput
146  operator()(const TInput1 & A, const TInput2 & B) const
147  {
148  if (itk::Math::NotAlmostEquals(B, TInput2{}))
149  {
150  return (TOutput)(A / B);
151  }
152  else
153  {
154  return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
155  }
156  }
157 };
158 
159 
165 template <typename TNumerator, typename TDenominator = TNumerator, typename TOutput = TNumerator>
166 class ITK_TEMPLATE_EXPORT DivideOrZeroOut
167 {
168 public:
170  {
171  m_Threshold = 1e-5 * NumericTraits<TDenominator>::OneValue();
172  m_Constant = TOutput{};
173  };
176  ~DivideOrZeroOut() = default;
177 
178  bool
179  operator==(const DivideOrZeroOut & itkNotUsed(other)) const
180  {
181  // Always return true for now. Do a comparison to m_Threshold if it is
182  // every made set-able.
183  return true;
184  }
185 
186  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(DivideOrZeroOut);
187 
188  inline TOutput
189  operator()(const TNumerator & n, const TDenominator & d) const
190  {
191  if (d < m_Threshold)
192  {
193  return m_Constant;
194  }
195  return static_cast<TOutput>(n) / static_cast<TOutput>(d);
196  }
197  TDenominator m_Threshold;
198  TOutput m_Constant;
199 };
200 
201 
207 template <typename TInput1, typename TInput2, typename TOutput>
208 class ITK_TEMPLATE_EXPORT Modulus
209 {
210 public:
211  bool
212  operator==(const Modulus &) const
213  {
214  return true;
215  }
216 
217  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Modulus);
218 
219  inline TOutput
220  operator()(const TInput1 & A, const TInput2 & B) const
221  {
222  if (B != TInput2{})
223  {
224  return static_cast<TOutput>(A % B);
225  }
226  else
227  {
228  return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
229  }
230  }
231 };
232 
233 #if !defined(ITK_FUTURE_LEGACY_REMOVE)
234 
243 template <typename TInput, typename TOutput>
244 class ITK_TEMPLATE_EXPORT ModulusTransform
245 {
246 public:
247  ModulusTransform() { m_Dividend = 5; }
248  ~ModulusTransform() = default;
249  void
250  SetDividend(TOutput dividend)
251  {
252  m_Dividend = dividend;
253  }
256  bool
257  operator==(const ModulusTransform & other) const
258  {
259  return m_Dividend == other.m_Dividend;
260  }
261 
262  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ModulusTransform);
263 
264  inline TOutput
265  operator()(const TInput & x) const
266  {
267  auto result = static_cast<TOutput>(x % m_Dividend);
268 
269  return result;
270  }
271 
272 private:
273  TInput m_Dividend;
274 };
275 
276 #endif
277 
287 template <class TInput1, class TInput2, class TOutput>
288 class DivFloor
289 {
290 public:
291  bool
292  operator==(const DivFloor &) const
293  {
294  return true;
295  }
296 
298 
299  inline TOutput
300  operator()(const TInput1 & A, const TInput2 & B) const
301  {
302  const double temp = std::floor(static_cast<double>(A) / static_cast<double>(B));
303  if (std::is_integral_v<TOutput> && Math::isinf(temp))
304  {
305  if (temp > 0)
306  {
307  return NumericTraits<TOutput>::max(A);
308  }
309  else
310  {
312  }
313  }
314  return static_cast<TOutput>(temp);
315  }
316 };
317 
329 template <class TInput1, class TInput2, class TOutput>
330 class DivReal
331 {
332 public:
333  // Use default copy, assigned and destructor
334  bool
335  operator==(const DivReal &) const
336  {
337  return true;
338  }
339 
341 
342  inline TOutput
343  operator()(const TInput1 & A, const TInput2 & B) const
344  {
345  return static_cast<TOutput>(static_cast<typename NumericTraits<TInput1>::RealType>(A) /
346  static_cast<typename NumericTraits<TInput2>::RealType>(B));
347  }
348 };
356 template <class TInput1, class TOutput = TInput1>
358 {
359 public:
360  bool
361  operator==(const UnaryMinus &) const
362  {
363  return true;
364  }
365 
367 
368  inline TOutput
369  operator()(const TInput1 & A) const
370  {
371  return (TOutput)(-A);
372  }
373 };
374 } // namespace Functor
375 } // namespace itk
376 
377 #endif
itk::Functor::Mult::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:121
itk::Functor::UnaryMinus::operator()
TOutput operator()(const TInput1 &A) const
Definition: itkArithmeticOpsFunctors.h:369
itk::Functor::UnaryMinus::operator==
bool operator==(const UnaryMinus &) const
Definition: itkArithmeticOpsFunctors.h:361
itk::Functor::Add2::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:46
itk::NumericTraits::NonpositiveMin
static constexpr T NonpositiveMin()
Definition: itkNumericTraits.h:98
itk::Functor::Sub2
Definition: itkArithmeticOpsFunctors.h:84
itk::Functor::Div::operator==
bool operator==(const Div &) const
Definition: itkArithmeticOpsFunctors.h:138
itk::Functor::Add3
Definition: itkArithmeticOpsFunctors.h:59
itk::Functor::DivFloor::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(DivFloor)
itk::Functor::Add2::operator==
bool operator==(const Add2 &) const
Definition: itkArithmeticOpsFunctors.h:38
itk::Functor::Modulus
Definition: itkArithmeticOpsFunctors.h:208
itk::Functor::DivideOrZeroOut::operator==
bool operator==(const DivideOrZeroOut &) const
Definition: itkArithmeticOpsFunctors.h:179
itk::Functor::Add3::operator==
bool operator==(const Add3 &) const
Definition: itkArithmeticOpsFunctors.h:63
itk::Functor::DivideOrZeroOut::operator()
TOutput operator()(const TNumerator &n, const TDenominator &d) const
Definition: itkArithmeticOpsFunctors.h:189
itk::Functor::DivideOrZeroOut
Definition: itkArithmeticOpsFunctors.h:166
itk::Functor::Add2
Definition: itkArithmeticOpsFunctors.h:34
itk::Functor::Sub2::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:96
itk::Functor::Add3::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B, const TInput3 &C) const
Definition: itkArithmeticOpsFunctors.h:71
itk::NumericTraits::OneValue
static T OneValue()
Definition: itkNumericTraits.h:157
itk::Math::NotAlmostEquals
bool NotAlmostEquals(T1 x1, T2 x2)
Definition: itkMath.h:696
itk::Functor::UnaryMinus::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(UnaryMinus)
itk::Functor::DivideOrZeroOut::m_Constant
TOutput m_Constant
Definition: itkArithmeticOpsFunctors.h:198
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:545
itk::Functor::DivReal::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:343
itk::Functor::DivFloor
Cast arguments to double, performs division then takes the floor.
Definition: itkArithmeticOpsFunctors.h:288
itk::Functor::DivideOrZeroOut::m_Threshold
TDenominator m_Threshold
Definition: itkArithmeticOpsFunctors.h:197
itk::Functor::Modulus::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:220
itk::Functor::Mult::operator==
bool operator==(const Mult &) const
Definition: itkArithmeticOpsFunctors.h:113
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:59
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:168
itk::Functor::DivideOrZeroOut::DivideOrZeroOut
DivideOrZeroOut()
Definition: itkArithmeticOpsFunctors.h:169
itk::Functor::Modulus::operator==
bool operator==(const Modulus &) const
Definition: itkArithmeticOpsFunctors.h:212
itk::Functor::DivReal
Promotes arguments to real type and performs division.
Definition: itkArithmeticOpsFunctors.h:330
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:357
itk::Functor::DivReal::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(DivReal)
itk::Functor::Div
Definition: itkArithmeticOpsFunctors.h:134
itk::Math::e
static constexpr double e
Definition: itkMath.h:56
itk::NumericTraits::RealType
double RealType
Definition: itkNumericTraits.h:85
itk::Functor::DivFloor::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:300
itk::Functor::DivFloor::operator==
bool operator==(const DivFloor &) const
Definition: itkArithmeticOpsFunctors.h:292
itk::Functor::Mult
Definition: itkArithmeticOpsFunctors.h:109
itkMath.h
itk::Functor::Sub2::operator==
bool operator==(const Sub2 &) const
Definition: itkArithmeticOpsFunctors.h:88
itk::Functor::DivReal::operator==
bool operator==(const DivReal &) const
Definition: itkArithmeticOpsFunctors.h:335
itk::Functor::Div::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:146