ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkChainCodePath2D.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 __itkChainCodePath2D_h
00019 #define __itkChainCodePath2D_h
00020 
00021 #include "itkChainCodePath.h"
00022 #include "itkIndex.h"
00023 
00024 #include <vector>
00025 #include <string>
00026 
00027 namespace itk
00028 {
00050 class ITK_EXPORT ChainCodePath2D:public
00051   ChainCodePath< 2 >
00052 {
00053 public:
00055   itkStaticConstMacro(Dimension, unsigned int, 2);
00056 
00058   typedef ChainCodePath2D    Self;
00059   typedef ChainCodePath< 2 > Superclass;
00060 
00061   typedef SmartPointer< Self >       Pointer;
00062   typedef SmartPointer< const Self > ConstPointer;
00063 
00065   itkTypeMacro(ChainCodePath2D, ChainCodePath);
00066 
00068   typedef Superclass::OutputType OutputType;
00069   typedef Superclass::InputType  InputType;
00070 
00072   typedef OutputType OffsetType;
00073   typedef Index< 2 > IndexType;
00074 
00076   typedef Superclass::ChainCodeType     ChainCodeType;
00077   typedef Superclass::ChainCodeSizeType ChainCodeSizeType;
00078 
00080   typedef std::vector< int > ChainCode2DType;
00081 
00082   // Functions inherited from Path
00083 
00085   virtual OutputType Evaluate(const InputType & input) const;
00086 
00088   virtual IndexType EvaluateToIndex(const InputType & input) const;
00089 
00094   virtual OffsetType IncrementInput(InputType & input) const;
00095 
00096   // Functions specific to ChainCodePath and its descendents
00097 
00099   itkNewMacro(Self);
00100 
00102   inline ChainCodeSizeType NumberOfSteps() const { return m_Chain2D.size(); }
00103 
00105   inline void InsertStep(InputType position, int encodedStep)
00106   {
00107     m_Chain2D.insert(m_Chain2D.begin() + position, encodedStep);
00108     this->Modified();
00109   }
00111 
00112   inline void InsertStep(InputType position, OffsetType step)
00113   {
00114     m_Chain2D.insert( m_Chain2D.begin() + position, EncodeOffset(step) );
00115     this->Modified();
00116   }
00117 
00119   inline void ChangeStep(InputType position, int encodedStep)
00120   {
00121     m_Chain2D[position] = encodedStep;
00122     this->Modified();
00123   }
00125 
00126   inline void ChangeStep(InputType position, OffsetType step)
00127   {
00128     m_Chain2D[position] = EncodeOffset(step);
00129     this->Modified();
00130   }
00131 
00133   virtual inline void Clear()
00134   {
00135     m_Chain2D.clear();
00136     this->Modified();
00137   }
00139 
00140   std::string GetChainCodeAsString(void) const;
00141 
00142 protected:
00143   ChainCodePath2D();
00144   ~ChainCodePath2D();
00145   void PrintSelf(std::ostream & os, Indent indent) const;
00146 
00148   inline int EncodeOffset(OffsetType step) const
00149   {
00150     return m_FreemanCode[step[0] + 1][step[1] + 1];
00151   }
00152 
00153   inline OffsetType DecodeOffset(int encodedStep) const
00154   {
00155     return m_ReverseFreemanCode[encodedStep];
00156   }
00157 
00158 private:
00159   ChainCodePath2D(const Self &); //purposely not implemented
00160   void operator=(const Self &);  //purposely not implemented
00161 
00162   ChainCode2DType m_Chain2D;    // the Freeman-encoded chain code
00163 
00164   // FreemanCode[][] implements a lookup table for converting offsets to a
00165   // Freeman code.  Within each dimension, the only allowable offset values are
00166   // { -1, 0, 1 }.  The y-axis is assumed to point up.  It is initialized in the
00167   // constructor.  Use it as follows:
00168   //
00169   //   encodedValue = m_FreemanCode[ x offset + 1 ][ y offset + 1 ]
00170   //
00171   int m_FreemanCode[3][3];
00172 
00173   // m_ReverseFreemanCode[ encodedValue ] implements a lookup table for the
00174   // inverse of m_FreemanCode[][].  It is initialized in the constructor.
00175   OffsetType m_ReverseFreemanCode[9];
00176 };
00177 } // end namespace itk
00178 
00179 #endif
00180