| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkPathFunctions.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:43 $ |
| 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 _itkPathFunctions_h |
| 19 |
DEF |
#define _itkPathFunctions_h |
| 20 |
|
|
| 21 |
|
|
| 22 |
|
#include "itkPath.h" |
| 23 |
|
#include "itkChainCodePath.h" |
| 24 |
|
#include "itkFourierSeriesPath.h" |
| 25 |
|
#include "itkOffset.h" |
| 26 |
|
#include <math.h> |
| 27 |
|
|
| 28 |
|
// not all versions of math.h seem to define M_PI: |
| 29 |
|
#ifndef M_PI |
| 30 |
|
#define M_PI 3.14159265358979323846 |
| 31 |
|
#endif |
| 32 |
|
|
| 33 |
|
namespace itk |
| 34 |
|
{ |
| 35 |
|
|
| 36 |
|
|
| 37 |
EML |
|
| 38 |
|
/** Make a chain code trace another path of same dimensionality. |
| 39 |
|
* If restrictMovement is true, then individual steps are allowed to move |
| 40 |
|
* through only one dimension at a time; for 2D paths this results in an |
| 41 |
|
* 8-connected chain code. */ |
| 42 |
|
template <class TChainCodePath, class TPathInput> |
| 43 |
|
void MakeChainCodeTracePath( TChainCodePath & chainPath, |
| 44 |
|
const TPathInput & inPath, |
| 45 |
|
bool restrictMovement = false ) |
| 46 |
|
{ |
| 47 |
|
typedef typename TChainCodePath::OffsetType OffsetType; |
| 48 |
|
typedef typename TChainCodePath::InputType ChainInputType; |
| 49 |
|
typedef typename TChainCodePath::OutputType ChainOutputType; |
| 50 |
|
typedef typename TPathInput::InputType InPathInputType; |
| 51 |
|
typedef typename TPathInput::OutputType InPathOutputType; |
| 52 |
|
|
| 53 |
|
OffsetType offset, tempOffset, zeroOffset; |
| 54 |
|
InPathInputType inPathInput; |
| 55 |
|
int dimension = OffsetType::GetOffsetDimension(); |
| 56 |
|
|
| 57 |
|
zeroOffset.Fill(0); |
| 58 |
|
|
| 59 |
|
chainPath.Clear(); |
| 60 |
|
inPathInput = inPath.StartOfInput(); |
| 61 |
|
chainPath.SetStart( inPath.EvaluateToIndex( inPathInput ) ); |
| 62 |
|
|
| 63 |
SEM |
for(ChainInputType chainInput=0;;) |
| 64 |
|
{ |
| 65 |
|
offset = inPath.IncrementInput(inPathInput); |
| 66 |
|
if( zeroOffset == offset ) { break; } |
| 67 |
|
|
| 68 |
|
if( ! restrictMovement ) |
| 69 |
|
{ |
| 70 |
|
chainPath.InsertStep( chainInput++, offset ); |
| 71 |
|
} |
| 72 |
|
else |
| 73 |
|
{ |
| 74 |
|
for( int d=0; d<dimension; d++ ) |
| 75 |
|
{ |
| 76 |
|
tempOffset.Fill(0); |
| 77 |
|
tempOffset[d] = offset[d]; |
| 78 |
|
chainPath.InsertStep( chainInput++, tempOffset ); |
| 79 |
|
} |
| 80 |
|
} |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
EML |
|
| 86 |
|
/** Make a Fourier series path trace a chain code path of same dimensionality. |
| 87 |
|
* numHarmonics is the number of harmonics (frequency coefficients, which |
| 88 |
|
* include the "DC" term) to compute. If chainPath has too few steps to |
| 89 |
|
* calculate numHarmonics (due to the Nyquist criterion), then as many harmonics |
| 90 |
|
* as possible (chainPath->NumberOfSteps()/2) will be calculated. No fewer than |
| 91 |
|
* 2 harmonics will be calcualted. */ |
| 92 |
|
template <class TFourierSeriesPath, class TChainCodePath> |
| 93 |
|
void MakeFourierSeriesPathTraceChainCode( TFourierSeriesPath & FSPath, |
| 94 |
|
const TChainCodePath & chainPath, |
| 95 |
|
unsigned int numHarmonics = 8 ) |
| 96 |
|
{ |
| 97 |
|
typedef typename TFourierSeriesPath::IndexType IndexType; |
| 98 |
|
typedef typename TFourierSeriesPath::OffsetType OffsetType; |
| 99 |
|
typedef typename TFourierSeriesPath::VectorType VectorType; |
| 100 |
|
|
| 101 |
|
typedef typename TFourierSeriesPath::InputType FSInputType; |
| 102 |
|
typedef typename TFourierSeriesPath::OutputType FSOutputType; |
| 103 |
|
typedef typename TChainCodePath::InputType ChainInputType; |
| 104 |
|
typedef typename TChainCodePath::OutputType ChainOutputType; |
| 105 |
|
|
| 106 |
|
IndexType index; |
| 107 |
|
VectorType indexVector; |
| 108 |
|
VectorType cosCoefficient; |
| 109 |
|
VectorType sinCoefficient; |
| 110 |
|
FSInputType theta; |
| 111 |
|
int dimension = OffsetType::GetOffsetDimension(); |
| 112 |
|
unsigned numSteps = chainPath.NumberOfSteps(); |
| 113 |
|
|
| 114 |
|
FSPath.Clear(); |
| 115 |
|
|
| 116 |
|
// Adjust our private copy of numHarmonics if necessary |
| 117 |
|
if( numHarmonics <= 1 ) |
| 118 |
|
numHarmonics = 2; |
| 119 |
|
else if( numHarmonics*2 > numSteps ) |
| 120 |
IND |
****numHarmonics = numSteps / 2; |
| 121 |
|
|
| 122 |
|
for( unsigned n=0; n<numHarmonics; n++ ) |
| 123 |
|
{ |
| 124 |
|
index = chainPath.GetStart(); |
| 125 |
|
cosCoefficient.Fill(0.0); |
| 126 |
|
sinCoefficient.Fill(0.0); |
| 127 |
|
|
| 128 |
|
for( ChainInputType step=0; step<numSteps; step++ ) |
| 129 |
|
{ |
| 130 |
|
index += chainPath.Evaluate( step ); |
| 131 |
|
theta = 2*n*M_PI*(double(step+1))/numSteps; |
| 132 |
|
|
| 133 |
|
// turn the current index into a vector |
| 134 |
|
for( int d=0; d<dimension; d++ ) |
| 135 |
IND |
********indexVector[d] = index[d]; |
| 136 |
|
|
| 137 |
|
cosCoefficient += indexVector * (cos(theta)/numSteps); |
| 138 |
|
sinCoefficient += indexVector * (sin(theta)/numSteps); |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
FSPath.AddHarmonic( cosCoefficient, sinCoefficient ); |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
EML |
|
| 147 |
|
} // namespace itk |
| 148 |
|
|
| 149 |
|
#endif |
| 150 |
|
|