ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkPathFunctions.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 __itkPathFunctions_h
19 #define __itkPathFunctions_h
20 
21 #include "itkChainCodePath.h"
22 #include "itkFourierSeriesPath.h"
23 #include <cmath>
24 
25 namespace itk
26 {
31 template< class TChainCodePath, class TPathInput >
32 void MakeChainCodeTracePath(TChainCodePath & chainPath,
33  const TPathInput & inPath,
34  bool restrictMovement = false)
35 {
36  typedef typename TChainCodePath::OffsetType OffsetType;
37  typedef typename TChainCodePath::InputType ChainInputType;
38  typedef typename TChainCodePath::OutputType ChainOutputType;
39  typedef typename TPathInput::InputType InPathInputType;
40  typedef typename TPathInput::OutputType InPathOutputType;
41 
42  OffsetType offset, tempOffset, zeroOffset;
43  InPathInputType inPathInput;
44  int dimension = OffsetType::GetOffsetDimension();
45 
46  zeroOffset.Fill(0);
47 
48  chainPath.Clear();
49  inPathInput = inPath.StartOfInput();
50  chainPath.SetStart( inPath.EvaluateToIndex(inPathInput) );
51 
52  for ( ChainInputType chainInput = 0;; )
53  {
54  offset = inPath.IncrementInput(inPathInput);
55  if ( zeroOffset == offset ) { break; }
56 
57  if ( !restrictMovement )
58  {
59  chainPath.InsertStep(chainInput++, offset);
60  }
61  else
62  {
63  for ( int d = 0; d < dimension; d++ )
64  {
65  tempOffset.Fill(0);
66  tempOffset[d] = offset[d];
67  chainPath.InsertStep(chainInput++, tempOffset);
68  }
69  }
70  }
71 }
72 
79 template< class TFourierSeriesPath, class TChainCodePath >
80 void MakeFourierSeriesPathTraceChainCode(TFourierSeriesPath & FSPath,
81  const TChainCodePath & chainPath,
82  unsigned int numHarmonics = 8)
83 {
84  typedef typename TFourierSeriesPath::IndexType IndexType;
85  typedef typename TFourierSeriesPath::OffsetType OffsetType;
86  typedef typename TFourierSeriesPath::VectorType VectorType;
88 
89  typedef typename TFourierSeriesPath::InputType FSInputType;
90  typedef typename TFourierSeriesPath::OutputType FSOutputType;
91  typedef typename TChainCodePath::InputType ChainInputType;
92  typedef typename TChainCodePath::OutputType ChainOutputType;
93 
94  IndexType index;
95  VectorType indexVector;
96  VectorType cosCoefficient;
97  VectorType sinCoefficient;
98  FSInputType theta;
99  int dimension = OffsetType::GetOffsetDimension();
100  unsigned numSteps = chainPath.NumberOfSteps();
101 
102  const double PI = 4.0 * vcl_atan(1.0);
103 
104  FSPath.Clear();
105 
106  // Adjust our private copy of numHarmonics if necessary
107  if ( numHarmonics <= 1 )
108  {
109  numHarmonics = 2;
110  }
111  else if ( numHarmonics * 2 > numSteps )
112  {
113  numHarmonics = numSteps / 2;
114  }
115 
116  for ( unsigned n = 0; n < numHarmonics; n++ )
117  {
118  index = chainPath.GetStart();
119  cosCoefficient.Fill(0.0);
120  sinCoefficient.Fill(0.0);
121 
122  for ( ChainInputType step = 0; step < numSteps; step++ )
123  {
124  index += chainPath.Evaluate(step);
125  theta = 2 * n * PI * ( double(step + 1) ) / numSteps;
126 
127  // turn the current index into a vector
128  for ( int d = 0; d < dimension; d++ )
129  {
130  indexVector[d] = index[d];
131  }
132  cosCoefficient += indexVector * ( vcl_cos(theta) / numSteps );
133  sinCoefficient += indexVector * ( vcl_sin(theta) / numSteps );
134  }
135 
136  FSPath.AddHarmonic(cosCoefficient, sinCoefficient);
137  }
138 }
139 } // end namespace itk
140 
141 #endif
142