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 
152  return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
153  }
154 };
155 
156 
162 template <typename TNumerator, typename TDenominator = TNumerator, typename TOutput = TNumerator>
163 class ITK_TEMPLATE_EXPORT DivideOrZeroOut
164 {
165 public:
167  {
168  m_Threshold = 1e-5 * NumericTraits<TDenominator>::OneValue();
169  m_Constant = TOutput{};
170  }
173  ~DivideOrZeroOut() = default;
174 
175  bool
176  operator==(const DivideOrZeroOut & itkNotUsed(other)) const
177  {
178  // Always return true for now. Do a comparison to m_Threshold if it is
179  // every made set-able.
180  return true;
181  }
182 
183  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(DivideOrZeroOut);
184 
185  inline TOutput
186  operator()(const TNumerator & n, const TDenominator & d) const
187  {
188  if (d < m_Threshold)
189  {
190  return m_Constant;
191  }
192  return static_cast<TOutput>(n) / static_cast<TOutput>(d);
193  }
194  TDenominator m_Threshold;
195  TOutput m_Constant;
196 };
197 
198 
204 template <typename TInput1, typename TInput2, typename TOutput>
205 class ITK_TEMPLATE_EXPORT Modulus
206 {
207 public:
208  bool
209  operator==(const Modulus &) const
210  {
211  return true;
212  }
213 
214  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Modulus);
215 
216  inline TOutput
217  operator()(const TInput1 & A, const TInput2 & B) const
218  {
219  if (B != TInput2{})
220  {
221  return static_cast<TOutput>(A % B);
222  }
223 
224  return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
225  }
226 };
227 
228 #if !defined(ITK_FUTURE_LEGACY_REMOVE)
229 
238 template <typename TInput, typename TOutput>
239 class ITK_TEMPLATE_EXPORT ModulusTransform
240 {
241 public:
242  ModulusTransform() { m_Dividend = 5; }
243  ~ModulusTransform() = default;
244  void
245  SetDividend(TOutput dividend)
246  {
247  m_Dividend = dividend;
248  }
251  bool
252  operator==(const ModulusTransform & other) const
253  {
254  return m_Dividend == other.m_Dividend;
255  }
256 
257  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ModulusTransform);
258 
259  inline TOutput
260  operator()(const TInput & x) const
261  {
262  auto result = static_cast<TOutput>(x % m_Dividend);
263 
264  return result;
265  }
266 
267 private:
268  TInput m_Dividend;
269 };
270 
271 #endif
272 
282 template <class TInput1, class TInput2, class TOutput>
283 class DivFloor
284 {
285 public:
286  bool
287  operator==(const DivFloor &) const
288  {
289  return true;
290  }
291 
293 
294  inline TOutput
295  operator()(const TInput1 & A, const TInput2 & B) const
296  {
297  const double temp = std::floor(static_cast<double>(A) / static_cast<double>(B));
298  if (std::is_integral_v<TOutput> && Math::isinf(temp))
299  {
300  if (temp > 0)
301  {
302  return NumericTraits<TOutput>::max(A);
303  }
304 
306  }
307  return static_cast<TOutput>(temp);
308  }
309 };
310 
322 template <class TInput1, class TInput2, class TOutput>
323 class DivReal
324 {
325 public:
326  // Use default copy, assigned and destructor
327  bool
328  operator==(const DivReal &) const
329  {
330  return true;
331  }
332 
334 
335  inline TOutput
336  operator()(const TInput1 & A, const TInput2 & B) const
337  {
338  return static_cast<TOutput>(static_cast<typename NumericTraits<TInput1>::RealType>(A) /
339  static_cast<typename NumericTraits<TInput2>::RealType>(B));
340  }
341 };
349 template <class TInput1, class TOutput = TInput1>
351 {
352 public:
353  bool
354  operator==(const UnaryMinus &) const
355  {
356  return true;
357  }
358 
360 
361  inline TOutput
362  operator()(const TInput1 & A) const
363  {
364  return (TOutput)(-A);
365  }
366 };
367 } // namespace Functor
368 } // namespace itk
369 
370 #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:362
itk::Functor::UnaryMinus::operator==
bool operator==(const UnaryMinus &) const
Definition: itkArithmeticOpsFunctors.h:354
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:205
itk::Functor::DivideOrZeroOut::operator==
bool operator==(const DivideOrZeroOut &) const
Definition: itkArithmeticOpsFunctors.h:176
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:186
itk::Functor::DivideOrZeroOut
Definition: itkArithmeticOpsFunctors.h:163
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:195
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:552
itk::Functor::DivReal::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:336
itk::Functor::DivFloor
Cast arguments to double, performs division then takes the floor.
Definition: itkArithmeticOpsFunctors.h:283
itk::Functor::DivideOrZeroOut::m_Threshold
TDenominator m_Threshold
Definition: itkArithmeticOpsFunctors.h:194
itk::Functor::Modulus::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:217
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:166
itk::Functor::Modulus::operator==
bool operator==(const Modulus &) const
Definition: itkArithmeticOpsFunctors.h:209
itk::Functor::DivReal
Promotes arguments to real type and performs division.
Definition: itkArithmeticOpsFunctors.h:323
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:350
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:295
itk::Functor::DivFloor::operator==
bool operator==(const DivFloor &) const
Definition: itkArithmeticOpsFunctors.h:287
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:328
itk::Functor::Div::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:145