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 true;
43  }
45 
46  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Add2);
47 
48  inline TOutput
49  operator()(const TInput1 & A, const TInput2 & B) const
50  {
51  return static_cast<TOutput>(A + B);
52  }
53 };
54 
55 
61 template <typename TInput1, typename TInput2, typename TInput3, typename TOutput>
62 class ITK_TEMPLATE_EXPORT Add3
63 {
64 public:
65  Add3() = default;
66  ~Add3() = default;
67  bool
68  operator==(const Add3 &) const
69  {
70  return true;
71  }
73 
74  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Add3);
75 
76  inline TOutput
77  operator()(const TInput1 & A, const TInput2 & B, const TInput3 & C) const
78  {
79  return static_cast<TOutput>(A + B + C);
80  }
81 };
82 
83 
89 template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
90 class ITK_TEMPLATE_EXPORT Sub2
91 {
92 public:
93  Sub2() = default;
94  ~Sub2() = default;
95  bool
96  operator==(const Sub2 &) const
97  {
98  return true;
99  }
101 
102  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Sub2);
103 
104  inline TOutput
105  operator()(const TInput1 & A, const TInput2 & B) const
106  {
107  return static_cast<TOutput>(A - B);
108  }
109 };
110 
111 
117 template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
118 class ITK_TEMPLATE_EXPORT Mult
119 {
120 public:
121  Mult() = default;
122  ~Mult() = default;
123  bool
124  operator==(const Mult &) const
125  {
126  return true;
127  }
129 
130  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Mult);
131 
132  inline TOutput
133  operator()(const TInput1 & A, const TInput2 & B) const
134  {
135  return static_cast<TOutput>(A * B);
136  }
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
152  operator==(const Div &) const
153  {
154  return true;
155  }
157 
158  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Div);
159 
160  inline TOutput
161  operator()(const TInput1 & A, const TInput2 & B) const
162  {
164  {
165  return (TOutput)(A / B);
166  }
167  else
168  {
169  return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
170  }
171  }
172 };
173 
174 
180 template <typename TNumerator, typename TDenominator = TNumerator, typename TOutput = TNumerator>
181 class ITK_TEMPLATE_EXPORT DivideOrZeroOut
182 {
183 public:
185  {
186  m_Threshold = 1e-5 * NumericTraits<TDenominator>::OneValue();
187  m_Constant = NumericTraits<TOutput>::ZeroValue();
188  };
190 
191  ~DivideOrZeroOut() = default;
192 
193  bool
194  operator==(const DivideOrZeroOut & itkNotUsed(other)) const
195  {
196  // Always return true for now. Do a comparison to m_Threshold if it is
197  // every made set-able.
198  return true;
199  }
200 
201  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(DivideOrZeroOut);
202 
203  inline TOutput
204  operator()(const TNumerator & n, const TDenominator & d) const
205  {
206  if (d < m_Threshold)
207  {
208  return m_Constant;
209  }
210  return static_cast<TOutput>(n) / static_cast<TOutput>(d);
211  }
212  TDenominator m_Threshold;
213  TOutput m_Constant;
214 };
215 
216 
222 template <typename TInput1, typename TInput2, typename TOutput>
223 class ITK_TEMPLATE_EXPORT Modulus
224 {
225 public:
226  Modulus() = default;
227  ~Modulus() = default;
229 
230  bool
231  operator==(const Modulus &) const
232  {
233  return true;
234  }
235 
236  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Modulus);
237 
238  inline TOutput
239  operator()(const TInput1 & A, const TInput2 & B) const
240  {
242  {
243  return static_cast<TOutput>(A % B);
244  }
245  else
246  {
247  return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
248  }
249  }
250 };
251 
252 #if !defined(ITK_FUTURE_LEGACY_REMOVE)
253 
262 template <typename TInput, typename TOutput>
263 class ITK_TEMPLATE_EXPORT ModulusTransform
264 {
265 public:
266  ModulusTransform() { m_Dividend = 5; }
267  ~ModulusTransform() = default;
268  void
269  SetDividend(TOutput dividend)
270  {
271  m_Dividend = dividend;
272  }
274 
275  bool
276  operator==(const ModulusTransform & other) const
277  {
278  return m_Dividend == other.m_Dividend;
279  }
280 
281  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ModulusTransform);
282 
283  inline TOutput
284  operator()(const TInput & x) const
285  {
286  auto result = static_cast<TOutput>(x % m_Dividend);
287 
288  return result;
289  }
290 
291 private:
292  TInput m_Dividend;
293 };
294 
295 #endif
296 
306 template <class TInput1, class TInput2, class TOutput>
307 class DivFloor
308 {
309 public:
310  bool
311  operator==(const DivFloor &) const
312  {
313  return true;
314  }
315 
317 
318  inline TOutput
319  operator()(const TInput1 & A, const TInput2 & B) const
320  {
321  const double temp = std::floor(double(A) / double(B));
322  if (NumericTraits<TOutput>::IsInteger && Math::isinf(temp))
323  {
324  if (temp > 0)
325  {
326  return NumericTraits<TOutput>::max(A);
327  }
328  else
329  {
331  }
332  }
333  return static_cast<TOutput>(temp);
334  }
335 };
336 
348 template <class TInput1, class TInput2, class TOutput>
349 class DivReal
350 {
351 public:
352  // Use default copy, assigned and destructor
353  bool
354  operator==(const DivReal &) const
355  {
356  return true;
357  }
358 
360 
361  inline TOutput
362  operator()(const TInput1 & A, const TInput2 & B) const
363  {
364  return static_cast<TOutput>(static_cast<typename NumericTraits<TInput1>::RealType>(A) /
365  static_cast<typename NumericTraits<TInput2>::RealType>(B));
366  }
367 };
375 template <class TInput1, class TOutput = TInput1>
377 {
378 public:
379  UnaryMinus() = default;
380  ~UnaryMinus() = default;
381  bool
382  operator==(const UnaryMinus &) const
383  {
384  return true;
385  }
387 
389 
390  inline TOutput
391  operator()(const TInput1 & A) const
392  {
393  return (TOutput)(-A);
394  }
395 };
396 } // namespace Functor
397 } // namespace itk
398 
399 #endif
itk::Functor::Mult::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:133
itk::Functor::UnaryMinus::operator()
TOutput operator()(const TInput1 &A) const
Definition: itkArithmeticOpsFunctors.h:391
itk::Functor::UnaryMinus::UnaryMinus
UnaryMinus()=default
itk::Functor::UnaryMinus::operator==
bool operator==(const UnaryMinus &) const
Definition: itkArithmeticOpsFunctors.h:382
itk::Functor::Add2::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:49
itk::NumericTraits::NonpositiveMin
static constexpr T NonpositiveMin()
Definition: itkNumericTraits.h:97
itk::Functor::Sub2
Definition: itkArithmeticOpsFunctors.h:90
itk::Functor::Div::operator==
bool operator==(const Div &) const
Definition: itkArithmeticOpsFunctors.h:152
itk::Functor::Add3
Definition: itkArithmeticOpsFunctors.h:62
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:40
itk::Functor::Modulus
Definition: itkArithmeticOpsFunctors.h:223
itk::Functor::DivideOrZeroOut::operator==
bool operator==(const DivideOrZeroOut &) const
Definition: itkArithmeticOpsFunctors.h:194
itk::Functor::Add3::operator==
bool operator==(const Add3 &) const
Definition: itkArithmeticOpsFunctors.h:68
itk::Functor::DivideOrZeroOut::operator()
TOutput operator()(const TNumerator &n, const TDenominator &d) const
Definition: itkArithmeticOpsFunctors.h:204
itk::Functor::DivideOrZeroOut
Definition: itkArithmeticOpsFunctors.h:181
itk::Functor::Add2
Definition: itkArithmeticOpsFunctors.h:34
itk::Functor::Sub2::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:105
itk::Functor::Add3::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B, const TInput3 &C) const
Definition: itkArithmeticOpsFunctors.h:77
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::UnaryMinus::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(UnaryMinus)
itk::Functor::DivideOrZeroOut::m_Constant
TOutput m_Constant
Definition: itkArithmeticOpsFunctors.h:213
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:536
itk::Functor::DivReal::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:362
itk::Functor::DivFloor
Cast arguments to double, performs division then takes the floor.
Definition: itkArithmeticOpsFunctors.h:307
itk::Functor::DivideOrZeroOut::m_Threshold
TDenominator m_Threshold
Definition: itkArithmeticOpsFunctors.h:212
itk::Functor::Modulus::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:239
itk::Functor::Mult::operator==
bool operator==(const Mult &) const
Definition: itkArithmeticOpsFunctors.h:124
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:184
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
itk::Functor::Modulus::operator==
bool operator==(const Modulus &) const
Definition: itkArithmeticOpsFunctors.h:231
itk::Functor::DivReal
Promotes arguments to real type and performs division.
Definition: itkArithmeticOpsFunctors.h:349
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:376
itk::Functor::DivReal::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(DivReal)
itk::Functor::Div
Definition: itkArithmeticOpsFunctors.h:146
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::DivFloor::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:319
itk::Functor::DivFloor::operator==
bool operator==(const DivFloor &) const
Definition: itkArithmeticOpsFunctors.h:311
itk::Functor::Mult
Definition: itkArithmeticOpsFunctors.h:118
itkMath.h
itk::Functor::Sub2::operator==
bool operator==(const Sub2 &) const
Definition: itkArithmeticOpsFunctors.h:96
itk::Functor::DivReal::operator==
bool operator==(const DivReal &) const
Definition: itkArithmeticOpsFunctors.h:354
itk::Functor::Div::operator()
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Definition: itkArithmeticOpsFunctors.h:161