ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkChainCodePath2D.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkChainCodePath2D_h
19 #define __itkChainCodePath2D_h
20 
21 #include "itkChainCodePath.h"
22 #include "itkIndex.h"
23 
24 #include <vector>
25 #include <string>
26 
27 namespace itk
28 {
50 class ITK_EXPORT ChainCodePath2D:public
51  ChainCodePath< 2 >
52 {
53 public:
55  itkStaticConstMacro(Dimension, unsigned int, 2);
56 
60 
63 
66 
68  typedef Superclass::OutputType OutputType;
69  typedef Superclass::InputType InputType;
70 
74 
76  typedef Superclass::ChainCodeType ChainCodeType;
77  typedef Superclass::ChainCodeSizeType ChainCodeSizeType;
78 
80  typedef std::vector< int > ChainCode2DType;
81 
82  // Functions inherited from Path
83 
85  virtual OutputType Evaluate(const InputType & input) const;
86 
88  virtual IndexType EvaluateToIndex(const InputType & input) const;
89 
94  virtual OffsetType IncrementInput(InputType & input) const;
95 
96  // Functions specific to ChainCodePath and its descendents
97 
99  itkNewMacro(Self);
100 
102  inline ChainCodeSizeType NumberOfSteps() const { return m_Chain2D.size(); }
103 
105  inline void InsertStep(InputType position, int encodedStep)
106  {
107  m_Chain2D.insert(m_Chain2D.begin() + position, encodedStep);
108  this->Modified();
109  }
111 
112  inline void InsertStep(InputType position, OffsetType step)
113  {
114  m_Chain2D.insert( m_Chain2D.begin() + position, EncodeOffset(step) );
115  this->Modified();
116  }
117 
119  inline void ChangeStep(InputType position, int encodedStep)
120  {
121  m_Chain2D[position] = encodedStep;
122  this->Modified();
123  }
125 
126  inline void ChangeStep(InputType position, OffsetType step)
127  {
128  m_Chain2D[position] = EncodeOffset(step);
129  this->Modified();
130  }
131 
133  virtual inline void Clear()
134  {
135  m_Chain2D.clear();
136  this->Modified();
137  }
139 
140  std::string GetChainCodeAsString(void) const;
141 
142 protected:
143  ChainCodePath2D();
144  ~ChainCodePath2D();
145  void PrintSelf(std::ostream & os, Indent indent) const;
146 
148  inline int EncodeOffset(OffsetType step) const
149  {
150  return m_FreemanCode[step[0] + 1][step[1] + 1];
151  }
152 
153  inline OffsetType DecodeOffset(int encodedStep) const
154  {
155  return m_ReverseFreemanCode[encodedStep];
156  }
157 
158 private:
159  ChainCodePath2D(const Self &); //purposely not implemented
160  void operator=(const Self &); //purposely not implemented
161 
162  ChainCode2DType m_Chain2D; // the Freeman-encoded chain code
163 
164  // FreemanCode[][] implements a lookup table for converting offsets to a
165  // Freeman code. Within each dimension, the only allowable offset values are
166  // { -1, 0, 1 }. The y-axis is assumed to point up. It is initialized in the
167  // constructor. Use it as follows:
168  //
169  // encodedValue = m_FreemanCode[ x offset + 1 ][ y offset + 1 ]
170  //
171  int m_FreemanCode[3][3];
172 
173  // m_ReverseFreemanCode[ encodedValue ] implements a lookup table for the
174  // inverse of m_FreemanCode[][]. It is initialized in the constructor.
175  OffsetType m_ReverseFreemanCode[9];
176 };
177 } // end namespace itk
178 
179 #endif
180