ITK  4.13.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< typename TChainCodePath, typename 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 TPathInput::InputType InPathInputType;
39 
40  OffsetType offset, tempOffset, zeroOffset;
41  InPathInputType inPathInput;
42  int dimension = OffsetType::GetOffsetDimension();
43 
44  zeroOffset.Fill(0);
45 
46  chainPath.Clear();
47  inPathInput = inPath.StartOfInput();
48  chainPath.SetStart( inPath.EvaluateToIndex(inPathInput) );
49 
50  for ( ChainInputType chainInput = 0;; )
51  {
52  offset = inPath.IncrementInput(inPathInput);
53  if ( zeroOffset == offset ) { break; }
54 
55  if ( !restrictMovement )
56  {
57  chainPath.InsertStep(chainInput++, offset);
58  }
59  else
60  {
61  for ( int d = 0; d < dimension; d++ )
62  {
63  tempOffset.Fill(0);
64  tempOffset[d] = offset[d];
65  chainPath.InsertStep(chainInput++, tempOffset);
66  }
67  }
68  }
69 }
70 
77 template< typename TFourierSeriesPath, typename TChainCodePath >
78 void MakeFourierSeriesPathTraceChainCode(TFourierSeriesPath & FSPath,
79  const TChainCodePath & chainPath,
80  unsigned int numHarmonics = 8)
81 {
83  typedef typename TFourierSeriesPath::OffsetType OffsetType;
86 
87  typedef typename TFourierSeriesPath::InputType FSInputType;
88  typedef typename TChainCodePath::InputType ChainInputType;
89 
90  IndexType index;
91  VectorType indexVector;
92  VectorType cosCoefficient;
93  VectorType sinCoefficient;
94  FSInputType theta;
95  int dimension = OffsetType::GetOffsetDimension();
96  size_t numSteps = chainPath.NumberOfSteps();
97 
98  const double PI = 4.0 * std::atan(1.0);
99 
100  FSPath.Clear();
101 
102  // Adjust our private copy of numHarmonics if necessary
103  if ( numHarmonics <= 1 )
104  {
105  numHarmonics = 2;
106  }
107  else if ( numHarmonics * 2 > numSteps )
108  {
109  numHarmonics = numSteps / 2;
110  }
111 
112  for ( unsigned n = 0; n < numHarmonics; n++ )
113  {
114  index = chainPath.GetStart();
115  cosCoefficient.Fill(0.0);
116  sinCoefficient.Fill(0.0);
117 
118  for ( ChainInputType step = 0; step < numSteps; step++ )
119  {
120  index += chainPath.Evaluate(step);
121  theta = 2 * n * PI * ( double(step + 1) ) / numSteps;
122 
123  // turn the current index into a vector
124  for ( int d = 0; d < dimension; d++ )
125  {
126  indexVector[d] = index[d];
127  }
128  cosCoefficient += indexVector * ( std::cos(theta) / numSteps );
129  sinCoefficient += indexVector * ( std::sin(theta) / numSteps );
130  }
131 
132  FSPath.AddHarmonic(cosCoefficient, sinCoefficient);
133  }
134 }
135 } // end namespace itk
136 
137 #endif
void MakeFourierSeriesPathTraceChainCode(TFourierSeriesPath &FSPath, const TChainCodePath &chainPath, unsigned int numHarmonics=8)
void MakeChainCodeTracePath(TChainCodePath &chainPath, const TPathInput &inPath, bool restrictMovement=false)