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
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 else
152 {
153 return NumericTraits<TOutput>::max(static_cast<TOutput>(A));
154 }
155 }
156};
157
158
164template <typename TNumerator, typename TDenominator = TNumerator, typename TOutput = TNumerator>
165class ITK_TEMPLATE_EXPORT DivideOrZeroOut
166{
167public:
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
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
206template <typename TInput1, typename TInput2, typename TOutput>
207class ITK_TEMPLATE_EXPORT Modulus
208{
209public:
210 bool
211 operator==(const Modulus &) const
212 {
213 return true;
214 }
215
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
242template <typename TInput, typename TOutput>
243class ITK_TEMPLATE_EXPORT ModulusTransform
244{
245public:
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
271private:
272 TInput m_Dividend;
273};
274
275#endif
276
286template <class TInput1, class TInput2, class TOutput>
288{
289public:
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 {
307 }
308 else
309 {
311 }
312 }
313 return static_cast<TOutput>(temp);
314 }
315};
316
328template <class TInput1, class TInput2, class TOutput>
330{
331public:
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};
355template <class TInput1, class TOutput = TInput1>
357{
358public:
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_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 &) 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 NotAlmostEquals(T1 x1, T2 x2)
Definition: itkMath.h:696
static constexpr double e
Definition: itkMath.h:56
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:543