| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkChainCodePath2D.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:33 $ |
| 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 |
|
|
| 18 |
DEF |
#ifndef _itkChainCodePath2D_h |
| 19 |
DEF |
#define _itkChainCodePath2D_h |
| 20 |
|
|
| 21 |
|
#include "itkChainCodePath.h" |
| 22 |
|
#include "itkObjectFactory.h" |
| 23 |
|
#include "itkIndex.h" |
| 24 |
|
#include "itkOffset.h" |
| 25 |
|
#include <vector> |
| 26 |
|
#include <string> |
| 27 |
|
|
| 28 |
|
|
| 29 |
|
namespace itk |
| 30 |
|
{ |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
/** \class ChainCodePath2D |
| 34 |
|
* \brief Represent a 2D path as a sequence of connected image index offsets |
| 35 |
|
* |
| 36 |
|
* This class is intended to represent sequences of connected indices in a 2D |
| 37 |
LEN |
* image plane. It does so by storing the offset of each index from its immediately |
| 38 |
LEN |
* preceeding, connected, index using a standard Freeman code (1=up, 2=up to the right, and so on proceeding clockwise to 8=up to the left). The only image index stored directly is that |
| 39 |
|
* of the first index. ChainCodePath2D maps a 1D integer input (step number) to |
| 40 |
|
* a 2D interger output (either an offset or an image index, depending on |
| 41 |
|
* whether Evaluate or EvaluateToIndex is called). |
| 42 |
|
* |
| 43 |
|
* \sa ChainCodePath |
| 44 |
|
* \sa ParametricPath |
| 45 |
|
* \sa Path |
| 46 |
|
* \sa Index |
| 47 |
|
* |
| 48 |
|
* \ingroup PathObjects |
| 49 |
|
*/ |
| 50 |
|
class ITKCommon_EXPORT ChainCodePath2D : public |
| 51 |
|
ChainCodePath<2> |
| 52 |
|
{ |
| 53 |
|
public: |
| 54 |
|
/** Dimension underlying input image. */ |
| 55 |
|
itkStaticConstMacro(Dimension, unsigned int, 2); |
| 56 |
|
|
| 57 |
|
/** Standard class typedefs. */ |
| 58 |
|
typedef ChainCodePath2D Self; |
| 59 |
|
typedef ChainCodePath<2> Superclass; |
| 60 |
|
|
| 61 |
|
typedef SmartPointer<Self> Pointer; |
| 62 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 63 |
|
|
| 64 |
|
/** Run-time type information (and related methods). */ |
| 65 |
|
itkTypeMacro(ChainCodePath2D, ChainCodePath); |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
/** OutputType typedef support. */ |
| 69 |
|
typedef Superclass::OutputType OutputType; |
| 70 |
|
typedef Superclass::InputType InputType; |
| 71 |
|
|
| 72 |
|
/** The output type of this function is an Index */ |
| 73 |
|
typedef OutputType OffsetType; |
| 74 |
|
typedef Index<2> IndexType; |
| 75 |
|
|
| 76 |
|
/** ChainCodeType is a usless relic of the parent class */ |
| 77 |
|
typedef std::vector<OffsetType> ChainCodeType; |
| 78 |
|
/** ChainCodePath2D stores its data as a Freeman-encoded chain code */ |
| 79 |
|
typedef std::vector<int> ChainCode2DType; |
| 80 |
|
|
| 81 |
|
|
| 82 |
EML |
|
| 83 |
|
// Functions inherited from Path |
| 84 |
|
|
| 85 |
|
/** Evaluate the chaincode for the offset at the specified path-position. */ |
| 86 |
|
virtual OutputType Evaluate( const InputType & input ) const; |
| 87 |
|
|
| 88 |
|
/** Like Evaluate(), but returns the index at the specified path-position. */ |
| 89 |
|
virtual IndexType EvaluateToIndex( const InputType & input ) const; |
| 90 |
|
|
| 91 |
|
/** Increment the input variable passed by reference and return the offset |
| 92 |
|
* stored at the previous path-position. If the chaincode is unable to be |
| 93 |
|
* incremented, input is not changed and an offset of zero is returned, which |
| 94 |
|
* may be used to check for the end of the chain code. */ |
| 95 |
|
virtual OffsetType IncrementInput(InputType & input) const; |
| 96 |
|
|
| 97 |
|
|
| 98 |
EML |
|
| 99 |
|
// Functions specific to ChainCodePath and its descendents |
| 100 |
|
|
| 101 |
|
/** New() method for dynamic construction */ |
| 102 |
|
itkNewMacro( Self ); |
| 103 |
|
|
| 104 |
|
/** How many steps in the chaincode? */ |
| 105 |
|
inline unsigned int NumberOfSteps() const { return m_Chain2D.size(); } |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
/** Insert a new step into the chaincode at a specified position */ |
| 109 |
|
inline void InsertStep( InputType position, int encodedStep ) |
| 110 |
IND |
**{ |
| 111 |
|
m_Chain2D.insert( m_Chain2D.begin()+position, encodedStep ); |
| 112 |
|
this->Modified(); |
| 113 |
IND |
**} |
| 114 |
|
inline void InsertStep( InputType position, OffsetType step ) |
| 115 |
IND |
**{ |
| 116 |
|
m_Chain2D.insert( m_Chain2D.begin()+position, EncodeOffset(step) ); |
| 117 |
|
this->Modified(); |
| 118 |
IND |
**} |
| 119 |
|
|
| 120 |
|
|
| 121 |
|
/** Change the direction of a step in the chaincode */ |
| 122 |
|
inline void ChangeStep( InputType position, int encodedStep ) |
| 123 |
IND |
**{ |
| 124 |
|
m_Chain2D[position]=encodedStep; |
| 125 |
|
this->Modified(); |
| 126 |
IND |
**} |
| 127 |
|
inline void ChangeStep( InputType position, OffsetType step ) |
| 128 |
IND |
**{ |
| 129 |
|
m_Chain2D[position]=EncodeOffset(step); |
| 130 |
|
this->Modified(); |
| 131 |
IND |
**} |
| 132 |
|
|
| 133 |
|
/** Remove all steps from the chain code */ |
| 134 |
|
virtual inline void Clear() |
| 135 |
|
{ |
| 136 |
|
m_Chain2D.clear(); |
| 137 |
|
this->Modified(); |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
std::string GetChainCodeAsString(void) const; |
| 141 |
|
|
| 142 |
|
|
| 143 |
EML |
|
| 144 |
|
protected: |
| 145 |
|
ChainCodePath2D(); |
| 146 |
|
~ChainCodePath2D(); |
| 147 |
|
void PrintSelf(std::ostream& os, Indent indent) const; |
| 148 |
|
|
| 149 |
|
/** Encode and Decode between an offset and a Freeman code */ |
| 150 |
|
inline int EncodeOffset( OffsetType step ) const |
| 151 |
|
{ |
| 152 |
|
return m_FreemanCode[ step[0] + 1 ][ step[1] + 1 ]; |
| 153 |
|
} |
| 154 |
|
inline OffsetType DecodeOffset( int encodedStep ) const |
| 155 |
|
{ |
| 156 |
|
return m_ReverseFreemanCode[ encodedStep ]; |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
|
| 160 |
EML |
|
| 161 |
|
private: |
| 162 |
|
ChainCodePath2D(const Self&); //purposely not implemented |
| 163 |
|
void operator=(const Self&); //purposely not implemented |
| 164 |
|
|
| 165 |
|
ChainCode2DType m_Chain2D; // the Freeman-encoded chain code |
| 166 |
|
|
| 167 |
|
// FreemanCode[][] implements a lookup table for converting offsets to a |
| 168 |
|
// Freeman code. Within each dimension, the only allowable offset values are |
| 169 |
|
// { -1, 0, 1 }. The y-axis is assumed to point up. It is initialized in the |
| 170 |
|
// constructor. Use it as follows: |
| 171 |
|
// |
| 172 |
|
// encodedValue = m_FreemanCode[ x offset + 1 ][ y offset + 1 ] |
| 173 |
|
// |
| 174 |
|
int m_FreemanCode[3][3]; |
| 175 |
|
|
| 176 |
|
// m_ReverseFreemanCode[ encodedValue ] implements a lookup table for the |
| 177 |
|
// inverse of m_FreemanCode[][]. It is initialized in the constructor. |
| 178 |
|
OffsetType m_ReverseFreemanCode[9]; |
| 179 |
|
}; |
| 180 |
|
|
| 181 |
|
} // namespace itk |
| 182 |
|
|
| 183 |
|
#endif |
| 184 |
|
|