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 itkConditionalConstIterator_h
19 #define itkConditionalConstIterator_h
20 
21 #include "itkIndex.h"
22 
23 namespace itk
24 {
25 /** \class ConditionalConstIterator
26  *  \brief A base class for other iterators where membership in the set
27  *         of output pixels is conditional upon some property, calculation,
28  *         etc. For example, a threshold iterator might walk a region and
29  *         return only those pixels which meet a minimum intensity condition.
30  *
31  * This class is the const version of the ConditionalIterator.
32  * For this reason, it doesn't support the Set() method.
33  *
34  * \ingroup ImageIterators
35  * \ingroup ITKCommon
36  */
37 template< typename TImage >
38 class ITK_TEMPLATE_EXPORT ConditionalConstIterator
39 {
40 public:
41   /** Standard class type aliases. */
42   using Self = ConditionalConstIterator;
43 
44   /** Dimension of the image the iterator walks.  This constant is needed so
45    * that functions that are templated over image iterator type (as opposed to
46    * being templated over pixel type and dimension) can have compile time
47    * access to the dimension of the image that the iterator walks. */
48   static constexpr unsigned int NDimension = TImage::ImageDimension;
49 
50   /** Index type alias support. */
51   using IndexType = typename TImage::IndexType;
52 
53   /** Size type alias support. */
54   using SizeType = typename TImage::SizeType;
55 
56   /** Region type alias support. */
57   using RegionType = typename TImage::RegionType;
58 
59   /** Image type alias support. */
60   using ImageType = TImage;
61 
62   /** Internal Pixel Type */
63   using InternalPixelType = typename TImage::InternalPixelType;
64 
65   /** External Pixel Type */
66   using PixelType = typename TImage::PixelType;
67 
68   /** Compute whether the index of interest should be included in the flood */
69   virtual bool IsPixelIncluded(const IndexType & index) const = 0;
70 
71   /** operator= is provided to make sure the handle to the image is properly
72    * reference counted. */
73   Self & operator=(const Self & it)
74   {
75     m_IsAtEnd = it.m_IsAtEnd; // copy the end flag
76     m_Image = it.m_Image;     // copy the smart pointer
77     m_Region = it.m_Region;   // copy the region
78     return *this;
79   }
80 
81   /** Get the dimension (size) of the index. */
GetIteratorDimension()82   static unsigned int GetIteratorDimension()
83   {
84     return Self::NDimension;
85   }
86 
87   /** Get the index at the current iterator location. */
88   virtual const IndexType GetIndex() = 0;
89 
90   /** Get the pixel value at the current iterator location. */
91   virtual const PixelType Get() const = 0;
92 
93   /** Is the iterator at the end of the region? */
94   virtual bool IsAtEnd() const = 0;
95 
96   /** Walk forward one index. */
97   virtual void operator++() = 0;
98 
99   /** Constructor */
100   ConditionalConstIterator() = default;
101 
102   /** Destructor */
103   virtual ~ConditionalConstIterator() = default;
104 
105 protected: //made protected so other iterators can access
106   /** Smart pointer to the source image. */
107   //SmartPointer<const ImageType> m_Image;
108   typename ImageType::ConstWeakPointer m_Image;
109 
110   /** Region type to iterate over. */
111   RegionType m_Region;
112 
113   /** Is the iterator at the end of its walk? */
114   bool m_IsAtEnd{false};
115 };
116 } // end namespace itk
117 
118 #endif
119