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
24{
25namespace Functor
26{
27
32template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
33class ITK_TEMPLATE_EXPORT Add2
34{
35public:
36 bool
37 operator==(const Add2 &) const
38 {
39 return true;
40 }
41
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
57template <typename TInput1, typename TInput2, typename TInput3, typename TOutput>
58class ITK_TEMPLATE_EXPORT Add3
59{
60public:
61 bool
62 operator==(const Add3 &) const
63 {
64 return true;
65 }
66
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
82template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
83class ITK_TEMPLATE_EXPORT Sub2
84{
85public:
86 bool
87 operator==(const Sub2 &) const
88 {
89 return true;
90 }
91
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
107template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
108class ITK_TEMPLATE_EXPORT Mult
109{
110public:
111 bool
112 operator==(const Mult &) const
113 {
114 return true;
115 }
116
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
132template <typename TInput1, typename TInput2, typename TOutput>
133class ITK_TEMPLATE_EXPORT Div
134{
135public:
136 bool
137 operator==(const Div &) const
138 {
139 return true;
140 }
141
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
162template <typename TNumerator, typename TDenominator = TNumerator, typename TOutput = TNumerator>
163class ITK_TEMPLATE_EXPORT DivideOrZeroOut
164{
165public:
167 {
169 m_Constant = TOutput{};
170 }
171
172
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
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
204template <typename TInput1, typename TInput2, typename TOutput>
205class ITK_TEMPLATE_EXPORT Modulus
206{
207public:
208 bool
209 operator==(const Modulus &) const
210 {
211 return true;
212 }
213
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
238template <typename TInput, typename TOutput>
239class ITK_TEMPLATE_EXPORT ModulusTransform
240{
241public:
242 ModulusTransform() { m_Dividend = 5; }
243 ~ModulusTransform() = default;
244 void
245 SetDividend(TOutput dividend)
246 {
247 m_Dividend = dividend;
248 }
250
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
267private:
268 TInput m_Dividend;
269};
270
271#endif
272
282template <class TInput1, class TInput2, class TOutput>
284{
285public:
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 {
303 }
304
306 }
307 return static_cast<TOutput>(temp);
308 }
309};
310
322template <class TInput1, class TInput2, class TOutput>
324{
325public:
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};
342
349template <class TInput1, class TOutput = TInput1>
351{
352public:
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_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)
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:690
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....