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