ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkLogicOpsFunctors.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 itkLogicOpsFunctors_h
19#define itkLogicOpsFunctors_h
20
21#include "itkNumericTraits.h"
22#include "itkMath.h"
23
24namespace itk::Functor
25{
26
55
56template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
57class ITK_TEMPLATE_EXPORT LogicOpBase
58{
59public:
62 : m_ForegroundValue(itk::NumericTraits<TOutput>::OneValue())
63 , m_BackgroundValue(TOutput{})
64 {}
65
66 bool
67 operator==(const Self &) const
68 {
69 return true;
70 }
71
73
74 void
75 SetForegroundValue(const TOutput & FG)
76 {
78 }
79 void
80 SetBackgroundValue(const TOutput & BG)
81 {
83 }
84
85 [[nodiscard]] TOutput
87 {
88 return m_ForegroundValue;
89 }
90 [[nodiscard]] TOutput
92 {
93 return m_BackgroundValue;
94 }
95
96protected:
99};
100
110
111template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
112class ITK_TEMPLATE_EXPORT Equal : public LogicOpBase<TInput1, TInput2, TOutput>
113{
114public:
115 using Self = Equal;
116
117 bool
118 operator==(const Self &) const
119 {
120 return true;
121 }
122
124
125 inline TOutput
126 operator()(const TInput1 & A, const TInput2 & B) const
127 {
128 if (Math::ExactlyEquals(A, static_cast<TInput1>(B)))
129 {
130 return this->m_ForegroundValue;
131 }
132 return this->m_BackgroundValue;
133 }
134};
135
144
145template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
146class ITK_TEMPLATE_EXPORT NotEqual : public LogicOpBase<TInput1, TInput2, TOutput>
147{
148public:
149 using Self = NotEqual;
150
151 bool
152 operator==(const Self &) const
153 {
154 return true;
155 }
156
158
159 inline TOutput
160 operator()(const TInput1 & A, const TInput2 & B) const
161 {
162 if (Math::NotExactlyEquals(A, B))
163 {
164 return this->m_ForegroundValue;
165 }
166 return this->m_BackgroundValue;
167 }
168};
169
179
180template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
181class ITK_TEMPLATE_EXPORT GreaterEqual : public LogicOpBase<TInput1, TInput2, TOutput>
182{
183public:
185
186 bool
187 operator==(const Self &) const
188 {
189 return true;
190 }
191
193
194 inline TOutput
195 operator()(const TInput1 & A, const TInput2 & B) const
196 {
197 if (A >= B)
198 {
199 return this->m_ForegroundValue;
200 }
201 return this->m_BackgroundValue;
202 }
203};
204
205
215template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
216class ITK_TEMPLATE_EXPORT Greater : public LogicOpBase<TInput1, TInput2, TOutput>
217{
218public:
219 using Self = Greater;
220
221 bool
222 operator==(const Self &) const
223 {
224 return true;
225 }
226
228
229 inline TOutput
230 operator()(const TInput1 & A, const TInput2 & B) const
231 {
232 if (A > B)
233 {
234 return this->m_ForegroundValue;
235 }
236 return this->m_BackgroundValue;
237 }
238};
239
240
250template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
251class ITK_TEMPLATE_EXPORT LessEqual : public LogicOpBase<TInput1, TInput2, TOutput>
252{
253public:
255
256 bool
257 operator==(const Self &) const
258 {
259 return true;
260 }
261
263
264 inline TOutput
265 operator()(const TInput1 & A, const TInput2 & B) const
266 {
267 if (A <= B)
268 {
269 return this->m_ForegroundValue;
270 }
271 return this->m_BackgroundValue;
272 }
273};
274
275
285template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
286class ITK_TEMPLATE_EXPORT Less : public LogicOpBase<TInput1, TInput2, TOutput>
287{
288public:
289 using Self = Less;
290
291 bool
292 operator==(const Self &) const
293 {
294 return true;
295 }
296
298
299 inline TOutput
300 operator()(const TInput1 & A, const TInput2 & B) const
301 {
302 if (A < B)
303 {
304 return this->m_ForegroundValue;
305 }
306 return this->m_BackgroundValue;
307 }
308};
309
310
316template <typename TInput, typename TOutput = TInput>
317class ITK_TEMPLATE_EXPORT NOT : public LogicOpBase<TInput, TInput, TOutput>
318{
319public:
320 bool
321 operator==(const NOT &) const
322 {
323 return true;
324 }
325
327
328 inline TOutput
329 operator()(const TInput & A) const
330 {
331 if (!A)
332 {
333 return this->m_ForegroundValue;
334 }
335 return this->m_BackgroundValue;
336 }
337};
338
344template <typename TInput1, typename TInput2, typename TInput3, typename TOutput>
345class ITK_TEMPLATE_EXPORT TernaryOperator
346{
347public:
348 bool
350 {
351 return true;
352 }
353
355
356 inline TOutput
357 operator()(const TInput1 & A, const TInput2 & B, const TInput3 & C) const
358 {
359 if (A)
360 {
361 return static_cast<TOutput>(B);
362 }
363
364 return static_cast<TOutput>(C);
365 }
366};
367
368} // namespace itk::Functor
369
370
371#endif
Functor for == operation on images and constants.
bool operator==(const Self &) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Functor for >= operation on images and constants.
bool operator==(const Self &) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Functor for > operation on images and constants.
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
TOutput operator()(const TInput1 &A, const TInput2 &B) const
bool operator==(const Self &) const
Functor for <= operation on images and constants.
TOutput operator()(const TInput1 &A, const TInput2 &B) const
bool operator==(const Self &) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
Functor for < operation on images and constants.
TOutput operator()(const TInput1 &A, const TInput2 &B) const
bool operator==(const Self &) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
bool operator==(const Self &) const
void SetForegroundValue(const TOutput &FG)
void SetBackgroundValue(const TOutput &BG)
Unary logical NOT functor.
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(NOT)
TOutput operator()(const TInput &A) const
bool operator==(const NOT &) const
Functor for != operation on images and constants.
bool operator==(const Self &) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Return argument 2 if argument 1 is false, and argument 3 otherwise.
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(TernaryOperator)
bool operator==(const TernaryOperator &) const
TOutput operator()(const TInput1 &A, const TInput2 &B, const TInput3 &C) const
Define additional traits for native types such as int or float.
constexpr bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potentially different types.
Definition itkMath.h:743
constexpr bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition itkMath.h:754
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....