KWStyle - itkConstSliceIterator.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkConstSliceIterator.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:34 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 #ifndef __itkConstSliceIterator_h
18 #define __itkConstSliceIterator_h
19
20 #include "itkMacro.h"
21 #include "itkExceptionObject.h"
22 #include <valarray>
23 namespace itk {
24
25 /**
26  * \class ConstSliceIterator
27  * \brief A flexible iterator for itk containers(i.e. itk::Neighborhood)
28  * that support pixel access through operator[].
29  *
30  * ConstSliceIterator allows const iteration along a std::slice through the
31  * container. 
32  * A slice is a construct that defines a starting position, stride length
33  * (distance between adjacent elements), and a length.
34  *
35  * Any container with operator[] const is supported.  Because it uses this
36  *  interface the iterator is only as efficient as the implementation of a
37  * container's operator[] method.
38  *
39  * References:
40  * Modeled after a slice iterator proposed by Bjarne Stroustrup
41  * in C++ Programming Language, Third Edition. Bjarne Stroustrup.  Addison
42  * Wesley, Reading, MA. 1997.
43  *
44  * \ingroup Iterators
45  *
46  */
47 template<class TPixel, class TContainer>
48 class ITK_EXPORT ConstSliceIterator
49 {
50 public:
51   /** Constructor. */
52   ConstSliceIterator(const TContainer *n, std::slice s)
53 IND ****: m_ContainerPointer(n), m_Pos(0), m_Slice(s) {}
54
55   /** Returns a ConstSliceIterator that points to the beginning of the slice. */
56   ConstSliceIterator Begin()
57 IND **{
58     ConstSliceIterator ans = *this;
59     ans.m_Pos = 0;
60     return ans;
61 IND **}
62   
63 LEN   /** Returns a ConstSliceIterator that points to one past the end of the slice. */
64   ConstSliceIterator End()
65 IND **{
66     ConstSliceIterator ans = *this;
67     ans.m_Pos = m_Slice.size();
68     return ans;
69 IND **}
70
71   /** Increments the iterator. */
72   ConstSliceIterator operator++()
73 IND **{
74     m_Pos++;
75     return *this;
76 IND **}
77
78   /** Increments the iterator. */
79   ConstSliceIterator operator++(int)
80 IND **{
81     ConstSliceIterator ans  = *this;
82     m_Pos++;
83     return ans;
84 IND **}
85
86   /** Returns the element at position n of the slice. Sets the
87    * iterator to point to position n. */
88   const TPixel& operator[](unsigned long n)
89 IND **{
90     return this->Loc(m_Pos=n);
91 IND **}
92
93   /** Dereferences the iterator, returning the value that it points
94    * to. */
95   const TPixel& operator*()
96 IND **{
97     return Loc(m_Pos);
98 IND **}
99
100   /** Returns the logical && of the boolean == of two slice iterator positions,
101    * stride, and start locations. */
102   bool operator==(const ConstSliceIterator &orig)
103 IND **{
104     return orig.m_Pos==this->m_Pos
105 IND ******&&   orig.m_Slice.stride()==this->m_Slice.stride()
106 IND ******&&   orig.m_Slice.start() ==this->m_Slice.start();
107 IND **}
108   
109   /** Returns the logical inverse of the boolean == of two slice iterators. */
110   bool operator!=(const ConstSliceIterator &orig)
111 IND **{
112     return ! operator==(orig);
113 IND **}
114
115   /** Returns the boolean < of two slice iterator positions.  Result
116    * is only true if the slice iterators have the same stride and
117    * start location. */
118   bool operator<(const ConstSliceIterator &orig)
119   {
120     return this->m_Pos < orig.m_Pos
121       &&   this->m_Slice.stride()==orig.m_Slice.stride()
122 IND ******&&   this->m_Slice.start()==orig.m_Slice.start();
123 IND **}
124   
125 private:
126   /** Returns the value located at position n of the slice. */
127   const TPixel& Loc(unsigned long n) const
128 IND **{
129     return (*m_ContainerPointer)[m_Slice.start()
130 IND ***************************************+ n * m_Slice.stride()];
131 IND **}  
132
133   /** Pointer to the container referenced by the slice iterator. */
134   const TContainer *m_ContainerPointer;
135
136   /** Current position within the slice. */
137   unsigned long m_Pos;
138
139   /** Slice structure information. */
140   std::slice m_Slice;
141 };
142   
143 // end namespace itk
144
145 #endif
146

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:03PM
© Kitware Inc.