ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkImageLinearConstIteratorWithIndex.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 __itkImageLinearConstIteratorWithIndex_h
00019 #define __itkImageLinearConstIteratorWithIndex_h
00020 
00021 #include "itkImageConstIteratorWithIndex.h"
00022 
00023 namespace itk
00024 {
00102 template< typename TImage >
00103 class ITK_EXPORT ImageLinearConstIteratorWithIndex:public ImageConstIteratorWithIndex< TImage >
00104 {
00105 public:
00107   typedef ImageLinearConstIteratorWithIndex     Self;
00108   typedef ImageConstIteratorWithIndex< TImage > Superclass;
00109 
00114   typedef typename TImage::IndexType IndexType;
00115 
00120   typedef typename TImage::RegionType RegionType;
00121 
00126   typedef TImage ImageType;
00127 
00131   typedef typename TImage::PixelContainer  PixelContainer;
00132   typedef typename PixelContainer::Pointer PixelContainerPointer;
00133 
00135   ImageLinearConstIteratorWithIndex():ImageConstIteratorWithIndex< TImage >(), m_Direction(0) {}
00136 
00139   ImageLinearConstIteratorWithIndex(const ImageType *ptr, const RegionType & region);
00140 
00147   ImageLinearConstIteratorWithIndex(const ImageConstIteratorWithIndex< TImage > & it) : m_Direction(0)
00148   { this->ImageConstIteratorWithIndex< TImage >::operator=(it); }
00149 
00152   inline void NextLine(void);
00153 
00156   inline void PreviousLine(void);
00157 
00160   void GoToBeginOfLine(void);
00161 
00164   void GoToReverseBeginOfLine(void);
00165 
00168   void GoToEndOfLine(void);
00169 
00171   inline bool IsAtEndOfLine(void)
00172   {
00173     return this->m_PositionIndex[m_Direction] >= this->m_EndIndex[m_Direction];
00174   }
00175 
00177   inline bool IsAtReverseEndOfLine(void)
00178   {
00179     return this->m_PositionIndex[m_Direction] < this->m_BeginIndex[m_Direction];
00180   }
00181 
00183   inline void SetDirection(unsigned int direction)
00184   {
00185     if ( direction >= TImage::ImageDimension )
00186       {
00187       itkGenericExceptionMacro(
00188         << "In image of dimension " << TImage::ImageDimension << " Direction " << direction << " sas selected");
00189       }
00190     m_Direction = direction;
00191     m_Jump = this->m_OffsetTable[m_Direction];
00192   }
00194 
00196   unsigned int GetDirection()
00197   {
00198     return m_Direction;
00199   }
00200 
00203   inline Self & operator++()
00204   {
00205     this->m_PositionIndex[m_Direction]++;
00206     this->m_Position += m_Jump;
00207     return *this;
00208   }
00210 
00213   inline Self & operator--()
00214   {
00215     this->m_PositionIndex[m_Direction]--;
00216     this->m_Position -= m_Jump;
00217     return *this;
00218   }
00220 
00221 private:
00222   OffsetValueType m_Jump;
00223   unsigned int    m_Direction;
00224 };
00225 
00226 //----------------------------------------------------------------------
00227 //  Go to next line
00228 //----------------------------------------------------------------------
00229 template< class TImage >
00230 inline
00231 void
00232 ImageLinearConstIteratorWithIndex< TImage >
00233 ::NextLine(void)
00234 {
00235   this->m_Position -= this->m_OffsetTable[m_Direction]
00236                       * ( this->m_PositionIndex[m_Direction] - this->m_BeginIndex[m_Direction] );
00237 
00238   this->m_PositionIndex[m_Direction] = this->m_BeginIndex[m_Direction];
00239 
00240   for ( unsigned int n = 0; n < TImage::ImageDimension; n++ )
00241   {
00242     this->m_Remaining = false;
00243 
00244     if ( n == m_Direction )
00245     {
00246       continue;
00247     }
00248 
00249     this->m_PositionIndex[n]++;
00250     if ( this->m_PositionIndex[n] <  this->m_EndIndex[n] )
00251     {
00252       this->m_Position += this->m_OffsetTable[n];
00253       this->m_Remaining = true;
00254       break;
00255     }
00256     else
00257     {
00258       this->m_Position -= this->m_OffsetTable[n] * ( this->m_Region.GetSize()[n] - 1 );
00259       this->m_PositionIndex[n] = this->m_BeginIndex[n];
00260     }
00261   }
00262 }
00263 
00264 //----------------------------------------------------------------------
00265 //  Pass to the last pixel on the previous line
00266 //----------------------------------------------------------------------
00267 template< class TImage >
00268 inline
00269 void
00270 ImageLinearConstIteratorWithIndex< TImage >
00271 ::PreviousLine(void)
00272 {
00273   this->m_Position += this->m_OffsetTable[m_Direction]
00274                       * ( this->m_EndIndex[m_Direction] - 1 - this->m_PositionIndex[m_Direction] );
00275 
00276   this->m_PositionIndex[m_Direction] = this->m_EndIndex[m_Direction] - 1;
00277 
00278   for ( unsigned int n = 0; n < TImage::ImageDimension; n++ )
00279   {
00280     this->m_Remaining = false;
00281 
00282     if ( n == m_Direction )
00283     {
00284       continue;
00285     }
00286 
00287     this->m_PositionIndex[n]--;
00288     if ( this->m_PositionIndex[n] >=  this->m_BeginIndex[n] )
00289     {
00290       this->m_Position -= this->m_OffsetTable[n];
00291       this->m_Remaining = true;
00292       break;
00293     }
00294     else
00295     {
00296       this->m_Position += this->m_OffsetTable[n] * ( this->m_Region.GetSize()[n] - 1 );
00297       this->m_PositionIndex[n] = this->m_EndIndex[n] - 1;
00298     }
00299   }
00300 }
00301 } // end namespace itk
00302 
00303 // Define instantiation macro for this template.
00304 #define ITK_TEMPLATE_ImageLinearConstIteratorWithIndex(_, EXPORT, TypeX, TypeY)     \
00305   namespace itk                                                                     \
00306   {                                                                                 \
00307   _( 1 ( class EXPORT ImageLinearConstIteratorWithIndex< ITK_TEMPLATE_1 TypeX > ) ) \
00308   namespace Templates                                                               \
00309   {                                                                                 \
00310   typedef ImageLinearConstIteratorWithIndex< ITK_TEMPLATE_1 TypeX >                 \
00311   ImageLinearConstIteratorWithIndex##TypeY;                                       \
00312   }                                                                                 \
00313   }
00314 
00315 #if ITK_TEMPLATE_EXPLICIT
00316 #include "Templates/itkImageLinearConstIteratorWithIndex+-.h"
00317 #endif
00318 
00319 #if ITK_TEMPLATE_TXX
00320 #include "itkImageLinearConstIteratorWithIndex.hxx"
00321 #endif
00322 
00323 #endif
00324