ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkLogicOpsFunctors.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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  * http://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 
25 namespace itk
26 {
27 namespace Functor
28 {
29 
57 template< typename TInput1, typename TInput2=TInput1, typename TOutput=TInput1 >
58 class ITK_TEMPLATE_EXPORT LogicOpBase
59 {
60 public:
61  using Self = LogicOpBase;
63  {
64  m_ForegroundValue=itk::NumericTraits<TOutput>::OneValue();
65  m_BackgroundValue=itk::NumericTraits<TOutput>::ZeroValue();
66  }
67 
68  ~LogicOpBase()= default;
69 
70 
71  bool operator!=( const Self & ) const
72  {
73  return false;
74  }
75  bool operator==( const Self & other ) const
76  {
77  return !(*this != other);
78  }
79 
80  void SetForegroundValue(const TOutput &FG)
81  {
82  m_ForegroundValue=FG;
83  }
84  void SetBackgroundValue(const TOutput &BG)
85  {
86  m_BackgroundValue=BG;
87  }
88 
89  TOutput GetForegroundValue() const
90  {
91  return(m_ForegroundValue);
92  }
93  TOutput GetBackgroundValue() const
94  {
95  return(m_BackgroundValue);
96  }
97 
98 protected:
101 
102 };
103 
113 template< typename TInput1, typename TInput2=TInput1, typename TOutput=TInput1 >
114 class ITK_TEMPLATE_EXPORT Equal : public LogicOpBase<TInput1, TInput2, TOutput>
115 {
116 public:
117  using Self = Equal;
118 
119  Equal() = default;
120  ~Equal() = default;
121 
122  bool operator!=( const Self & ) const
123  {
124  return false;
125  }
126  bool operator==( const Self & other ) const
127  {
128  return !(*this != other);
129  }
130  inline TOutput operator()( const TInput1 & A, const TInput2 & B) const
131  {
132  if( Math::ExactlyEquals(A, static_cast<TInput1>(B)) )
133  {
134  return this->m_ForegroundValue;
135  }
136  return this->m_BackgroundValue;
137  }
138 
139 };
149 template< typename TInput1, typename TInput2=TInput1, typename TOutput=TInput1 >
150 class ITK_TEMPLATE_EXPORT NotEqual : public LogicOpBase<TInput1, TInput2, TOutput>
151 {
152 public:
153  using Self = NotEqual;
154 
155  NotEqual() = default;
156  ~NotEqual() = default;
157  bool operator!=( const Self & ) const
158  {
159  return false;
160  }
161  bool operator==( const Self & other ) const
162  {
163  return !(*this != other);
164  }
165  inline TOutput operator()( const TInput1 & A, const TInput2 & B) const
166  {
167  if( Math::NotExactlyEquals(A, B) )
168  {
169  return this->m_ForegroundValue;
170  }
171  return this->m_BackgroundValue;
172  }
173 
174 };
175 
185 template< typename TInput1, typename TInput2=TInput1, typename TOutput=TInput1 >
186 class ITK_TEMPLATE_EXPORT GreaterEqual : public LogicOpBase<TInput1, TInput2, TOutput>
187 {
188 public:
190  GreaterEqual() = default;
191  ~GreaterEqual() = default;
192 
193  bool operator!=( const Self & ) const
194  {
195  return false;
196  }
197  bool operator==( const Self & other ) const
198  {
199  return !(*this != other);
200  }
201  inline TOutput 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 
212 
221 template< typename TInput1, typename TInput2=TInput1, typename TOutput=TInput1 >
222 class ITK_TEMPLATE_EXPORT Greater : public LogicOpBase<TInput1, TInput2, TOutput>
223 {
224 public:
225  using Self = Greater;
226  Greater() = default;
227  ~Greater() = default;
228  bool operator!=( const Self & ) const
229  {
230  return false;
231  }
232  bool operator==( const Self & other ) const
233  {
234  return !(*this != other);
235  }
236  inline TOutput 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 
255 template< typename TInput1, typename TInput2=TInput1, typename TOutput=TInput1 >
256 class ITK_TEMPLATE_EXPORT LessEqual : public LogicOpBase<TInput1, TInput2, TOutput>
257 {
258 public:
259  using Self = LessEqual;
260 
261  LessEqual()= default;
262  ~LessEqual()= default;
263  bool operator!=( const Self & ) const
264  {
265  return false;
266  }
267  bool operator==( const Self & other ) const
268  {
269  return !(*this != other);
270  }
271  inline TOutput 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 
282 
291 template< typename TInput1, typename TInput2=TInput1, typename TOutput=TInput1 >
292 class ITK_TEMPLATE_EXPORT Less : public LogicOpBase<TInput1, TInput2, TOutput>
293 {
294 public:
295  using Self = Less;
296  Less() = default;
297  ~Less() = default;
298  bool operator!=( const Self & ) const
299  {
300  return false;
301  }
302  bool operator==( const Self & other ) const
303  {
304  return !(*this != other);
305  }
306  inline TOutput 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 
317 
323 template< typename TInput, typename TOutput = TInput >
324 class ITK_TEMPLATE_EXPORT NOT : public LogicOpBase<TInput, TInput, TOutput>
325 {
326 public:
327  NOT() = default;
328  ~NOT() = default;
329  bool operator!=(const NOT &) const
330  {
331  return false;
332  }
334 
335  bool operator==(const NOT & other) const
336  {
337  return !( *this != other );
338  }
339 
340  inline TOutput operator()(const TInput & A) const
341  {
342  if( !A )
343  {
344  return this->m_ForegroundValue;
345  }
346  return this->m_BackgroundValue;
347  }
348 };
349 
355 template< typename TInput1, typename TInput2, typename TInput3, typename TOutput >
356 class ITK_TEMPLATE_EXPORT TernaryOperator
357 {
358 public:
359  TernaryOperator() = default;
360  ~TernaryOperator() = default;
361  bool operator!=(const TernaryOperator &) const
362  {
363  return false;
364  }
366 
367  bool operator==(const TernaryOperator & other) const
368  {
369  return !( *this != other );
370  }
371 
372  inline TOutput operator()(const TInput1 & A,
373  const TInput2 & B,
374  const TInput3 & C) const
375  {
376  if (A)
377  {
378  return static_cast<TOutput>( B );
379  }
380  else
381  {
382  return static_cast<TOutput>( C );
383  }
384  }
385 };
386 
387 }
388 }
389 
390 #endif
bool operator!=(const Self &) const
bool operator==(const Self &other) const
bool operator!=(const Self &) const
void SetBackgroundValue(const TOutput &BG)
bool operator!=(const Self &) const
bool operator==(const Self &other) const
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potetially different types...
Definition: itkMath.h:707
Functor for != operation on images and constants.
bool operator!=(const Self &) const
Return argument 2 if argument 1 is false, and argument 3 otherwise.
TOutput operator()(const TInput1 &A, const TInput2 &B) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
TOutput operator()(const TInput &A) const
bool operator!=(const TernaryOperator &) const
Functor for == operation on images and constants.
bool operator!=(const Self &) const
TOutput GetBackgroundValue() const
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:718
Functor for &gt; operation on images and constants.
Functor for &gt;= operation on images and constants.
bool operator!=(const Self &) const
bool operator==(const TernaryOperator &other) const
Functor for &lt; operation on images and constants.
bool operator==(const Self &other) const
Unary logical NOT functor.
void SetForegroundValue(const TOutput &FG)
TOutput operator()(const TInput1 &A, const TInput2 &B) const
Base class for some logic functors. Provides the Foreground and background setting methods...
TOutput operator()(const TInput1 &A, const TInput2 &B) const
TOutput operator()(const TInput1 &A, const TInput2 &B) const
bool operator==(const Self &other) const
bool operator==(const Self &other) const
bool operator==(const Self &other) const
Functor for &lt;= operation on images and constants.
bool operator==(const NOT &other) const
bool operator!=(const NOT &) const
bool operator==(const Self &other) const
TOutput GetForegroundValue() const
bool operator!=(const Self &) const
TOutput operator()(const TInput1 &A, const TInput2 &B, const TInput3 &C) const