ITK  5.4.0
Insight Toolkit
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
24
25namespace itk
26{
27namespace Functor
28{
29
58template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
59class ITK_TEMPLATE_EXPORT LogicOpBase
60{
61public:
64 {
65 m_ForegroundValue = itk::NumericTraits<TOutput>::OneValue();
66 m_BackgroundValue = TOutput{};
67 }
70 ~LogicOpBase() = default;
71
72 bool
73 operator==(const Self &) const
74 {
75 return true;
76 }
77
79
80 void
81 SetForegroundValue(const TOutput & FG)
82 {
83 m_ForegroundValue = FG;
84 }
85 void
86 SetBackgroundValue(const TOutput & BG)
87 {
88 m_BackgroundValue = BG;
89 }
90
91 TOutput
93 {
94 return (m_ForegroundValue);
95 }
96 TOutput
98 {
99 return (m_BackgroundValue);
100 }
101
102protected:
105};
106
117template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
118class ITK_TEMPLATE_EXPORT Equal : public LogicOpBase<TInput1, TInput2, TOutput>
119{
120public:
121 using Self = Equal;
122
123 bool
124 operator==(const Self &) const
125 {
126 return true;
127 }
128
130
131 inline TOutput
132 operator()(const TInput1 & A, const TInput2 & B) const
133 {
134 if (Math::ExactlyEquals(A, static_cast<TInput1>(B)))
135 {
136 return this->m_ForegroundValue;
137 }
138 return this->m_BackgroundValue;
139 }
140};
151template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
152class ITK_TEMPLATE_EXPORT NotEqual : public LogicOpBase<TInput1, TInput2, TOutput>
153{
154public:
155 using Self = NotEqual;
156
157 bool
158 operator==(const Self &) const
159 {
160 return true;
161 }
162
164
165 inline TOutput
166 operator()(const TInput1 & A, const TInput2 & B) const
167 {
168 if (Math::NotExactlyEquals(A, B))
169 {
170 return this->m_ForegroundValue;
171 }
172 return this->m_BackgroundValue;
173 }
174};
175
186template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
187class ITK_TEMPLATE_EXPORT GreaterEqual : public LogicOpBase<TInput1, TInput2, TOutput>
188{
189public:
191
192 bool
193 operator==(const Self &) const
194 {
195 return true;
196 }
197
199
200 inline TOutput
201 operator()(const TInput1 & A, const TInput2 & B) const
202 {
203 if (A >= B)
204 {
205 return this->m_ForegroundValue;
206 }
207 return this->m_BackgroundValue;
208 }
209};
210
211
221template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
222class ITK_TEMPLATE_EXPORT Greater : public LogicOpBase<TInput1, TInput2, TOutput>
223{
224public:
225 using Self = Greater;
226
227 bool
228 operator==(const Self &) const
229 {
230 return true;
231 }
232
234
235 inline TOutput
236 operator()(const TInput1 & A, const TInput2 & B) const
237 {
238 if (A > B)
239 {
240 return this->m_ForegroundValue;
241 }
242 return this->m_BackgroundValue;
243 }
244};
245
246
256template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
257class ITK_TEMPLATE_EXPORT LessEqual : public LogicOpBase<TInput1, TInput2, TOutput>
258{
259public:
261
262 bool
263 operator==(const Self &) const
264 {
265 return true;
266 }
267
269
270 inline TOutput
271 operator()(const TInput1 & A, const TInput2 & B) const
272 {
273 if (A <= B)
274 {
275 return this->m_ForegroundValue;
276 }
277 return this->m_BackgroundValue;
278 }
279};
280
281
291template <typename TInput1, typename TInput2 = TInput1, typename TOutput = TInput1>
292class ITK_TEMPLATE_EXPORT Less : public LogicOpBase<TInput1, TInput2, TOutput>
293{
294public:
295 using Self = Less;
296
297 bool
298 operator==(const Self &) const
299 {
300 return true;
301 }
302
304
305 inline TOutput
306 operator()(const TInput1 & A, const TInput2 & B) const
307 {
308 if (A < B)
309 {
310 return this->m_ForegroundValue;
311 }
312 return this->m_BackgroundValue;
313 }
314};
315
316
322template <typename TInput, typename TOutput = TInput>
323class ITK_TEMPLATE_EXPORT NOT : public LogicOpBase<TInput, TInput, TOutput>
324{
325public:
326 bool
327 operator==(const NOT &) const
328 {
329 return true;
330 }
331
333
334 inline TOutput
335 operator()(const TInput & A) const
336 {
337 if (!A)
338 {
339 return this->m_ForegroundValue;
340 }
341 return this->m_BackgroundValue;
342 }
343};
344
350template <typename TInput1, typename TInput2, typename TInput3, typename TOutput>
351class ITK_TEMPLATE_EXPORT TernaryOperator
352{
353public:
354 bool
356 {
357 return true;
358 }
359
361
362 inline TOutput
363 operator()(const TInput1 & A, const TInput2 & B, const TInput3 & C) const
364 {
365 if (A)
366 {
367 return static_cast<TOutput>(B);
368 }
369 else
370 {
371 return static_cast<TOutput>(C);
372 }
373 }
374};
375
376} // namespace Functor
377} // namespace itk
378
379#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)
Base class for some logic functors. Provides the Foreground and background setting methods.
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self)
bool operator==(const Self &) const
void SetForegroundValue(const TOutput &FG)
void SetBackgroundValue(const TOutput &BG)
TOutput GetForegroundValue() const
TOutput GetBackgroundValue() const
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
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:726
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:737
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....