ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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
23namespace itk::Functor
24{
25
30template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
31class ITK_TEMPLATE_EXPORT Add2
32{
33public:
34 bool
35 operator==(const Add2 &) const
36 {
37 return true;
38 }
39
41
42 inline TOutput
43 operator()(const TInput1 & A, const TInput2 & B) const
44 {
45 return static_cast<TOutput>(A + B);
46 }
47};
48
49
55template <typename TInput1, typename TInput2, typename TInput3, typename TOutput>
56class ITK_TEMPLATE_EXPORT Add3
57{
58public:
59 bool
60 operator==(const Add3 &) const
61 {
62 return true;
63 }
64
66
67 inline TOutput
68 operator()(const TInput1 & A, const TInput2 & B, const TInput3 & C) const
69 {
70 return static_cast<TOutput>(A + B + C);
71 }
72};
73
74
80template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
81class ITK_TEMPLATE_EXPORT Sub2
82{
83public:
84 bool
85 operator==(const Sub2 &) const
86 {
87 return true;
88 }
89
91
92 inline TOutput
93 operator()(const TInput1 & A, const TInput2 & B) const
94 {
95 return static_cast<TOutput>(A - B);
96 }
97};
98
99
105template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
106class ITK_TEMPLATE_EXPORT Mult
107{
108public:
109 bool
110 operator==(const Mult &) const
111 {
112 return true;
113 }
114
116
117 inline TOutput
118 operator()(const TInput1 & A, const TInput2 & B) const
119 {
120 return static_cast<TOutput>(A * B);
121 }
122};
123
124
130template <typename TInput1, typename TInput2, typename TOutput>
131class ITK_TEMPLATE_EXPORT Div
132{
133public:
134 bool
135 operator==(const Div &) const
136 {
137 return true;
138 }
139
141
142 inline TOutput
143 operator()(const TInput1 & A, const TInput2 & B) const
144 {
145 if (itk::Math::NotAlmostEquals(B, TInput2{}))
146 {
147 return (TOutput)(A / B);
148 }
149
150 return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
151 }
152};
153
154
160template <typename TNumerator, typename TDenominator = TNumerator, typename TOutput = TNumerator>
161class ITK_TEMPLATE_EXPORT DivideOrZeroOut
162{
163public:
165 : m_Threshold(1e-5 * NumericTraits<TDenominator>::OneValue())
166 , m_Constant(TOutput{})
167 {}
168
169 ~DivideOrZeroOut() = default;
170
171 bool
172 operator==(const DivideOrZeroOut & itkNotUsed(other)) const
173 {
174 // Always return true for now. Do a comparison to m_Threshold if it is
175 // every made set-able.
176 return true;
177 }
178
180
181 inline TOutput
182 operator()(const TNumerator & n, const TDenominator & d) const
183 {
184 if (d < m_Threshold)
185 {
186 return m_Constant;
187 }
188 return static_cast<TOutput>(n) / static_cast<TOutput>(d);
189 }
190 TDenominator m_Threshold;
191 TOutput m_Constant;
192};
193
194
200template <typename TInput1, typename TInput2, typename TOutput>
201class ITK_TEMPLATE_EXPORT Modulus
202{
203public:
204 bool
205 operator==(const Modulus &) const
206 {
207 return true;
208 }
209
211
212 inline TOutput
213 operator()(const TInput1 & A, const TInput2 & B) const
214 {
215 if (B != TInput2{})
216 {
217 return static_cast<TOutput>(A % B);
218 }
219
220 return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
221 }
222};
223
224#if !defined(ITK_FUTURE_LEGACY_REMOVE)
225
234template <typename TInput, typename TOutput>
235class ITK_TEMPLATE_EXPORT ModulusTransform
236{
237public:
238 ModulusTransform() { m_Dividend = 5; }
239 ~ModulusTransform() = default;
240 void
241 SetDividend(TOutput dividend)
242 {
243 m_Dividend = dividend;
244 }
245
246 bool
247 operator==(const ModulusTransform & other) const
248 {
249 return m_Dividend == other.m_Dividend;
250 }
251
252 ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ModulusTransform);
253
254 inline TOutput
255 operator()(const TInput & x) const
256 {
257 auto result = static_cast<TOutput>(x % m_Dividend);
258
259 return result;
260 }
261
262private:
263 TInput m_Dividend;
264};
265
266#endif
267
277template <class TInput1, class TInput2, class TOutput>
279{
280public:
281 bool
282 operator==(const DivFloor &) const
283 {
284 return true;
285 }
286
288
289 inline TOutput
290 operator()(const TInput1 & A, const TInput2 & B) const
291 {
292 const double temp = std::floor(static_cast<double>(A) / static_cast<double>(B));
293 if (std::is_integral_v<TOutput> && Math::isinf(temp))
294 {
295 if (temp > 0)
296 {
298 }
299
301 }
302 return static_cast<TOutput>(temp);
303 }
304};
305
317template <class TInput1, class TInput2, class TOutput>
319{
320public:
321 // Use default copy, assigned and destructor
322 bool
323 operator==(const DivReal &) const
324 {
325 return true;
326 }
327
329
330 inline TOutput
331 operator()(const TInput1 & A, const TInput2 & B) const
332 {
333 return static_cast<TOutput>(static_cast<typename NumericTraits<TInput1>::RealType>(A) /
334 static_cast<typename NumericTraits<TInput2>::RealType>(B));
335 }
336};
337
344template <class TInput1, class TOutput = TInput1>
346{
347public:
348 bool
349 operator==(const UnaryMinus &) const
350 {
351 return true;
352 }
353
355
356 inline TOutput
357 operator()(const TInput1 & A) const
358 {
359 return (TOutput)(-A);
360 }
361};
362} // namespace itk::Functor
363
364
365#endif
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Add2)
bool operator==(const Add2 &) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
bool operator==(const Add3 &) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Add3)
TOutput operator()(const TInput1 &A, const TInput2 &B, const TInput3 &C) const
Cast arguments to double, performs division then takes the floor.
bool operator==(const DivFloor &) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(DivFloor)
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Promotes arguments to real type and performs division.
TOutput operator()(const TInput1 &A, const TInput2 &B) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(DivReal)
bool operator==(const DivReal &) const
bool operator==(const Div &) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Div)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(DivideOrZeroOut)
TOutput operator()(const TNumerator &n, const TDenominator &d) const
bool operator==(const DivideOrZeroOut &other) const
bool operator==(const Modulus &) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Modulus)
TOutput operator()(const TInput1 &A, const TInput2 &B) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Mult)
bool operator==(const Mult &) const
bool operator==(const Sub2 &) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Sub2)
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Apply the unary minus operator.
bool operator==(const UnaryMinus &) const
TOutput operator()(const TInput1 &A) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(UnaryMinus)
Define additional traits for native types such as int or float.
static constexpr T NonpositiveMin()
static constexpr T max(const T &)
bool ITKIOXML_EXPORT operator==(itk::FancyString &s, const std::string &)
bool NotAlmostEquals(T1 x1, T2 x2)
Definition itkMath.h:686