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
19 #include <iostream>
20 #include "itkFourierSeriesPath.h"
21 #include "itkTestingMacros.h"
22
itkFourierSeriesPathTest(int,char * [])23 int itkFourierSeriesPathTest( int, char*[] )
24 {
25 constexpr unsigned int Dimension = 2;
26 using PathType = itk::FourierSeriesPath< Dimension >;
27 using InputType = PathType::InputType;
28 using OffsetType = PathType::OffsetType;
29 using VectorType = PathType::VectorType;
30
31 bool passed = true;
32
33 InputType input;
34 OffsetType offset;
35 VectorType cosV, sinV;
36
37 PathType::Pointer path = PathType::New();
38
39 EXERCISE_BASIC_OBJECT_METHODS( path, FourierSeriesPath, ParametricPath );
40
41 // Average value is (5,5)
42 cosV.Fill( 5 );
43 sinV.Fill( 0 );
44 path->AddHarmonic( cosV, sinV );
45 cosV.Fill( 2.7 );
46 sinV.Fill( 3.2 );
47 path->AddHarmonic( cosV, sinV );
48
49 std::cout << "Evaluating at 0, 0.5, and 1.0: " << path->Evaluate( 0 ) << ", "
50 << path->Evaluate( 0.5 ) << ", " << path->Evaluate( 1.0 ) << std::endl;
51 // Floating point can be imprecise, so convert to rounded int for comparison
52 if( int( 0.5 + 1000 * ( path->Evaluate( 1.0 ) )[0] ) !=
53 int( 0.5 + 1000 * ( path->Evaluate( 0.0 ) )[0] ) ||
54 int( 0.5 + 1000 * ( path->Evaluate( 1.0 ) )[1] ) !=
55 int( 0.5 + 1000 * ( path->Evaluate( 0.0 ) )[1] ) )
56 {
57 std::cout << "Evaluate() Failed" << std::endl;
58 passed = false;
59 }
60
61 std::cout << "Evaluating to an index at 0, 0.5, and 1.0: "
62 << path->EvaluateToIndex(0) << ", " << path->EvaluateToIndex( 0.5 )
63 << ", " << path->EvaluateToIndex( 1.0 ) << std::endl;
64 if( path->EvaluateToIndex( 1.0 ) != path->EvaluateToIndex( 0.0 ) )
65 {
66 std::cout << "FourierSeriesPathTest: EvaluateToIndex() Failed" << std::endl;
67 passed = false;
68 }
69
70 std::cout << "Evaluating the derivative at 0, 0.5, and 1.0: "
71 << path->EvaluateDerivative( 0 ) << ", " << path->EvaluateDerivative( 0.5 )
72 << ", " << path->EvaluateDerivative( 1.0 ) << std::endl;
73 // Floating point can be imprecise, so convert to rounded int for comparison
74 if( int( 0.5 + 1000 * ( path->EvaluateDerivative( 1.0 ) )[0] ) !=
75 int( 0.5 + 1000 * ( path->EvaluateDerivative( 0.0 ) )[0] ) ||
76 int( 0.5 + 1000 * ( path->EvaluateDerivative( 1.0 ) )[1] ) !=
77 int( 0.5 + 1000 * ( path->EvaluateDerivative( 0.0 ) )[1] ) )
78 {
79 std::cout << "EvaluateDerivative() Failed" << std::endl;
80 passed = false;
81 }
82
83 input = 0;
84 offset = path->IncrementInput( input );
85 std::cout << "Incrementing the input from 0 to " << input << ": " << offset
86 << std::endl;
87
88 input = 0.5;
89 offset = path->IncrementInput( input );
90 std::cout << "Incrementing the input from 0.5 to " << input << ": " << offset
91 << std::endl;
92 if( offset[0] != -1 || offset[1] != -1 )
93 {
94 std::cout << "IncrementInput() Failed" << std::endl;
95 passed = false;
96 }
97
98 if( passed )
99 {
100 std::cout << "Test passed" << std::endl;
101 return EXIT_SUCCESS;
102 }
103 else
104 {
105 std::cout << "Test failed" << std::endl;
106 return EXIT_FAILURE;
107 }
108 }
109