KWStyle - itkSliceIterator.h
 
Matrix View
Description

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

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