ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkPathFunctions.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 __itkPathFunctions_h
00019 #define __itkPathFunctions_h
00020 
00021 #include "itkChainCodePath.h"
00022 #include "itkFourierSeriesPath.h"
00023 #include <math.h>
00024 
00025 namespace itk
00026 {
00031 template< class TChainCodePath, class TPathInput >
00032 void MakeChainCodeTracePath(TChainCodePath & chainPath,
00033                             const TPathInput & inPath,
00034                             bool restrictMovement = false)
00035 {
00036   typedef typename TChainCodePath::OffsetType OffsetType;
00037   typedef typename TChainCodePath::InputType  ChainInputType;
00038   typedef typename TChainCodePath::OutputType ChainOutputType;
00039   typedef typename TPathInput::InputType      InPathInputType;
00040   typedef typename TPathInput::OutputType     InPathOutputType;
00041 
00042   OffsetType      offset, tempOffset, zeroOffset;
00043   InPathInputType inPathInput;
00044   int             dimension = OffsetType::GetOffsetDimension();
00045 
00046   zeroOffset.Fill(0);
00047 
00048   chainPath.Clear();
00049   inPathInput = inPath.StartOfInput();
00050   chainPath.SetStart( inPath.EvaluateToIndex(inPathInput) );
00051 
00052   for ( ChainInputType chainInput = 0;; )
00053     {
00054     offset  = inPath.IncrementInput(inPathInput);
00055     if ( zeroOffset == offset ) { break; }
00056 
00057     if ( !restrictMovement )
00058       {
00059       chainPath.InsertStep(chainInput++, offset);
00060       }
00061     else
00062       {
00063       for ( int d = 0; d < dimension; d++ )
00064         {
00065         tempOffset.Fill(0);
00066         tempOffset[d] = offset[d];
00067         chainPath.InsertStep(chainInput++, tempOffset);
00068         }
00069       }
00070     }
00071 }
00072 
00079 template< class TFourierSeriesPath, class TChainCodePath >
00080 void MakeFourierSeriesPathTraceChainCode(TFourierSeriesPath & FSPath,
00081                                          const TChainCodePath & chainPath,
00082                                          unsigned int numHarmonics = 8)
00083 {
00084   typedef typename TFourierSeriesPath::IndexType  IndexType;
00085   typedef typename TFourierSeriesPath::OffsetType OffsetType;
00086   typedef typename TFourierSeriesPath::VectorType VectorType;
00088 
00089   typedef typename TFourierSeriesPath::InputType  FSInputType;
00090   typedef typename TFourierSeriesPath::OutputType FSOutputType;
00091   typedef typename TChainCodePath::InputType      ChainInputType;
00092   typedef typename TChainCodePath::OutputType     ChainOutputType;
00093 
00094   IndexType   index;
00095   VectorType  indexVector;
00096   VectorType  cosCoefficient;
00097   VectorType  sinCoefficient;
00098   FSInputType theta;
00099   int         dimension =     OffsetType::GetOffsetDimension();
00100   unsigned    numSteps  =     chainPath.NumberOfSteps();
00101 
00102   const double PI = 4.0 * vcl_atan(1.0);
00103 
00104   FSPath.Clear();
00105 
00106   // Adjust our private copy of numHarmonics if necessary
00107   if ( numHarmonics <= 1 )
00108     {
00109     numHarmonics = 2;
00110     }
00111   else if ( numHarmonics * 2 > numSteps )
00112     {
00113     numHarmonics = numSteps / 2;
00114     }
00115 
00116   for ( unsigned n = 0; n < numHarmonics; n++ )
00117     {
00118     index = chainPath.GetStart();
00119     cosCoefficient.Fill(0.0);
00120     sinCoefficient.Fill(0.0);
00121 
00122     for ( ChainInputType step = 0; step < numSteps; step++ )
00123       {
00124       index += chainPath.Evaluate(step);
00125       theta = 2 * n * PI * ( double(step + 1) ) / numSteps;
00126 
00127       // turn the current index into a vector
00128       for ( int d = 0; d < dimension; d++ )
00129         {
00130         indexVector[d] = index[d];
00131         }
00132       cosCoefficient += indexVector * ( vcl_cos(theta) / numSteps );
00133       sinCoefficient += indexVector * ( vcl_sin(theta) / numSteps );
00134       }
00135 
00136     FSPath.AddHarmonic(cosCoefficient, sinCoefficient);
00137     }
00138 }
00139 } // end namespace itk
00140 
00141 #endif
00142