ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSliceIterator.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkSliceIterator_h
00019 #define __itkSliceIterator_h
00020 
00021 #include "itkMacro.h"
00022 #include "itkIntTypes.h"
00023 #include <valarray>
00024 
00025 namespace itk
00026 {
00047 template< class TPixel, class TContainer >
00048 class ITK_EXPORT SliceIterator
00049 {
00050 public:
00052   SliceIterator(TContainer *n, std::slice s):
00053     m_ContainerPointer(n), m_Pos(0), m_Slice(s) {}
00054 
00056   SliceIterator Begin()
00057   {
00058     SliceIterator ans = *this;
00059 
00060     ans.m_Pos = 0;
00061     return ans;
00062   }
00063 
00065   SliceIterator End()
00066   {
00067     SliceIterator ans = *this;
00068 
00069     ans.m_Pos = static_cast< OffsetValueType >( m_Slice.size() );
00070     return ans;
00071   }
00072 
00074   SliceIterator operator++()
00075   {
00076     m_Pos++;
00077     return *this;
00078   }
00079 
00081   SliceIterator operator++(int)
00082   {
00083     SliceIterator ans  = *this;
00084 
00085     m_Pos++;
00086     return ans;
00087   }
00088 
00091   TPixel & operator[](OffsetValueType n)
00092   { return this->Loc(m_Pos = n); }
00093 
00096   TPixel & operator*()
00097   { return Loc(m_Pos); }
00098 
00101   bool operator==(const SliceIterator & orig)
00102   {
00103     return orig.m_Pos == this->m_Pos
00104            &&   orig.m_Slice.stride() == this->m_Slice.stride()
00105            &&   orig.m_Slice.start() == this->m_Slice.start();
00106   }
00107 
00109   bool operator!=(const SliceIterator & orig)
00110   {
00111     return !operator==(orig);
00112   }
00113 
00117   bool operator<(const SliceIterator & orig)
00118   {
00119     return this->m_Pos < orig.m_Pos
00120            &&   this->m_Slice.stride() == orig.m_Slice.stride()
00121            &&   this->m_Slice.start() == orig.m_Slice.start();
00122   }
00123 
00124 private:
00126   TPixel & Loc(OffsetValueType n) const
00127   {
00128     const OffsetValueType start  = static_cast< OffsetValueType >( m_Slice.start() );
00129     const OffsetValueType stride = static_cast< OffsetValueType >( m_Slice.stride() );
00131 
00132     return ( *m_ContainerPointer )[start + n * stride];
00133   }
00134 
00136   TContainer *m_ContainerPointer;
00137 
00139   OffsetValueType m_Pos;
00140 
00142   std::slice m_Slice;
00143 };
00144 } // end namespace itk
00145 
00146 #endif
00147