ITK  5.2.0
Insight Toolkit
itkSliceIterator.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  * 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 itkSliceIterator_h
19 #define itkSliceIterator_h
20 
21 #include "itkMacro.h"
22 #include "itkIntTypes.h"
23 #include <valarray>
24 
25 namespace itk
26 {
47 template <typename TPixel, typename TContainer>
49 {
50 public:
52  SliceIterator(TContainer * n, std::slice s)
54  , m_Pos(0)
55  , m_Slice(s)
56  {}
57 
61  {
62  SliceIterator ans = *this;
63 
64  ans.m_Pos = 0;
65  return ans;
66  }
67 
70  End()
71  {
72  SliceIterator ans = *this;
73 
74  ans.m_Pos = static_cast<OffsetValueType>(m_Slice.size());
75  return ans;
76  }
77 
81  {
82  m_Pos++;
83  return *this;
84  }
85 
89  {
90  SliceIterator ans = *this;
91 
92  m_Pos++;
93  return ans;
94  }
95 
98  TPixel & operator[](OffsetValueType n) { return this->Loc(m_Pos = n); }
99 
102  TPixel & operator*() { return Loc(m_Pos); }
103 
106  bool
107  operator==(const SliceIterator & orig)
108  {
109  return orig.m_Pos == this->m_Pos && orig.m_Slice.stride() == this->m_Slice.stride() &&
110  orig.m_Slice.start() == this->m_Slice.start();
111  }
112 
114  bool
115  operator!=(const SliceIterator & orig)
116  {
117  return !operator==(orig);
118  }
119 
123  bool
124  operator<(const SliceIterator & orig)
125  {
126  return this->m_Pos < orig.m_Pos && this->m_Slice.stride() == orig.m_Slice.stride() &&
127  this->m_Slice.start() == orig.m_Slice.start();
128  }
129 
130 private:
132  TPixel &
134  {
135  const auto start = static_cast<OffsetValueType>(m_Slice.start());
136  const auto stride = static_cast<OffsetValueType>(m_Slice.stride());
138 
139  return (*m_ContainerPointer)[start + n * stride];
140  }
141 
143  TContainer * m_ContainerPointer;
144 
147 
149  std::slice m_Slice;
150 };
151 } // end namespace itk
152 
153 #endif
itk::SliceIterator::operator==
bool operator==(const SliceIterator &orig)
Definition: itkSliceIterator.h:107
itk::SliceIterator
A flexible iterator for itk containers(i.e. itk::Neighborhood) that support pixel access through oper...
Definition: itkSliceIterator.h:48
itk::SliceIterator::m_ContainerPointer
TContainer * m_ContainerPointer
Definition: itkSliceIterator.h:143
itk::SliceIterator::Loc
TPixel & Loc(OffsetValueType n) const
Definition: itkSliceIterator.h:133
itk::SliceIterator::operator*
TPixel & operator*()
Definition: itkSliceIterator.h:102
itk::SliceIterator::SliceIterator
SliceIterator(TContainer *n, std::slice s)
Definition: itkSliceIterator.h:52
itk::SliceIterator::operator++
SliceIterator operator++()
Definition: itkSliceIterator.h:80
itk::SliceIterator::Begin
SliceIterator Begin()
Definition: itkSliceIterator.h:60
itk::SliceIterator::operator++
SliceIterator operator++(int)
Definition: itkSliceIterator.h:88
itkMacro.h
itk::SliceIterator::operator<
bool operator<(const SliceIterator &orig)
Definition: itkSliceIterator.h:124
itk::SliceIterator::End
SliceIterator End()
Definition: itkSliceIterator.h:70
itk::SliceIterator::m_Pos
OffsetValueType m_Pos
Definition: itkSliceIterator.h:146
itkIntTypes.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::SliceIterator::operator!=
bool operator!=(const SliceIterator &orig)
Definition: itkSliceIterator.h:115
itk::SliceIterator::m_Slice
std::slice m_Slice
Definition: itkSliceIterator.h:149
itk::SliceIterator::operator[]
TPixel & operator[](OffsetValueType n)
Definition: itkSliceIterator.h:98