ITK  6.0.0
Insight Toolkit
itkPathFunctions.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
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 * https://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"
23#include <cmath>
24
25namespace itk
26{
31template <typename TChainCodePath, typename TPathInput>
32void
33MakeChainCodeTracePath(TChainCodePath & chainPath, const TPathInput & inPath, bool restrictMovement = false)
34{
35 using OffsetType = typename TChainCodePath::OffsetType;
36 using ChainInputType = typename TChainCodePath::InputType;
37 using InPathInputType = typename TPathInput::InputType;
38
39 const int dimension = OffsetType::GetOffsetDimension();
40
41 constexpr OffsetType zeroOffset{};
42
43 chainPath.Clear();
44 InPathInputType inPathInput = inPath.StartOfInput();
45 chainPath.SetStart(inPath.EvaluateToIndex(inPathInput));
46
47 for (ChainInputType chainInput = 0;;)
48 {
49 OffsetType offset = inPath.IncrementInput(inPathInput);
50 if (zeroOffset == offset)
51 {
52 break;
53 }
54
55 if (!restrictMovement)
56 {
57 chainPath.InsertStep(chainInput++, offset);
58 }
59 else
60 {
61 for (int d = 0; d < dimension; ++d)
62 {
63 OffsetType tempOffset{};
64 tempOffset[d] = offset[d];
65 chainPath.InsertStep(chainInput++, tempOffset);
66 }
67 }
68 }
69}
70
77template <typename TFourierSeriesPath, typename TChainCodePath>
78void
79MakeFourierSeriesPathTraceChainCode(TFourierSeriesPath & FSPath,
80 const TChainCodePath & chainPath,
81 unsigned int numHarmonics = 8)
82{
84 using OffsetType = typename TFourierSeriesPath::OffsetType;
88 using FSInputType = typename TFourierSeriesPath::InputType;
89 using ChainInputType = typename TChainCodePath::InputType;
90
91 const int dimension = OffsetType::GetOffsetDimension();
92 const size_t numSteps = chainPath.NumberOfSteps();
93
94 const double PI = 4.0 * std::atan(1.0);
95
96 FSPath.Clear();
97
98 // Adjust our private copy of numHarmonics if necessary
99 if (numHarmonics <= 1)
100 {
101 numHarmonics = 2;
102 }
103 else if (numHarmonics * 2 > numSteps)
104 {
105 numHarmonics = numSteps / 2;
106 }
107
108 for (unsigned int n = 0; n < numHarmonics; ++n)
109 {
110 IndexType index = chainPath.GetStart();
111 VectorType cosCoefficient{};
112 VectorType sinCoefficient{};
113
114 for (ChainInputType step = 0; step < numSteps; ++step)
115 {
116 index += chainPath.Evaluate(step);
117 const FSInputType theta = 2 * n * PI * (static_cast<double>(step + 1)) / numSteps;
118
119 // turn the current index into a vector
120 VectorType indexVector;
121 for (int d = 0; d < dimension; ++d)
122 {
123 indexVector[d] = index[d];
124 }
125 cosCoefficient += indexVector * (std::cos(theta) / numSteps);
126 sinCoefficient += indexVector * (std::sin(theta) / numSteps);
127 }
128
129 FSPath.AddHarmonic(cosCoefficient, sinCoefficient);
130 }
131}
132} // end namespace itk
133
134#endif
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void MakeFourierSeriesPathTraceChainCode(TFourierSeriesPath &FSPath, const TChainCodePath &chainPath, unsigned int numHarmonics=8)
void MakeChainCodeTracePath(TChainCodePath &chainPath, const TPathInput &inPath, bool restrictMovement=false)