ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkSliceIterator.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 __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< class TPixel, class TContainer >
48 class ITK_EXPORT SliceIterator
49 {
50 public:
52  SliceIterator(TContainer *n, std::slice s):
53  m_ContainerPointer(n), m_Pos(0), m_Slice(s) {}
54 
56  SliceIterator Begin()
57  {
58  SliceIterator ans = *this;
59 
60  ans.m_Pos = 0;
61  return ans;
62  }
63 
66  {
67  SliceIterator ans = *this;
68 
69  ans.m_Pos = static_cast< OffsetValueType >( m_Slice.size() );
70  return ans;
71  }
72 
74  SliceIterator operator++()
75  {
76  m_Pos++;
77  return *this;
78  }
79 
81  SliceIterator operator++(int)
82  {
83  SliceIterator ans = *this;
84 
85  m_Pos++;
86  return ans;
87  }
88 
91  TPixel & operator[](OffsetValueType n)
92  { return this->Loc(m_Pos = n); }
93 
96  TPixel & operator*()
97  { return Loc(m_Pos); }
98 
101  bool operator==(const SliceIterator & orig)
102  {
103  return orig.m_Pos == this->m_Pos
104  && orig.m_Slice.stride() == this->m_Slice.stride()
105  && orig.m_Slice.start() == this->m_Slice.start();
106  }
107 
109  bool operator!=(const SliceIterator & orig)
110  {
111  return !operator==(orig);
112  }
113 
117  bool operator<(const SliceIterator & orig)
118  {
119  return this->m_Pos < orig.m_Pos
120  && this->m_Slice.stride() == orig.m_Slice.stride()
121  && this->m_Slice.start() == orig.m_Slice.start();
122  }
123 
124 private:
126  TPixel & Loc(OffsetValueType n) const
127  {
128  const OffsetValueType start = static_cast< OffsetValueType >( m_Slice.start() );
129  const OffsetValueType stride = static_cast< OffsetValueType >( m_Slice.stride() );
131 
132  return ( *m_ContainerPointer )[start + n * stride];
133  }
134 
136  TContainer *m_ContainerPointer;
137 
140 
142  std::slice m_Slice;
143 };
144 } // end namespace itk
145 
146 #endif
147