File: itkBSplineInterpolationSODerivativeWeightFunctionTest.cxx

package info (click to toggle)
elastix 5.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,288 kB
  • sloc: cpp: 61,192; lisp: 4,121; python: 777; sh: 227; xml: 182; makefile: 35
file content (289 lines) | stat: -rw-r--r-- 10,019 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#include "itkBSplineInterpolationSecondOrderDerivativeWeightFunction.h"

#include <ctime>
#include <iomanip>

//-------------------------------------------------------------------------------------

int
main( int argc, char * argv[] )
{
  /** Some basic type definitions.
   * NOTE: don't change the dimension or the spline order, since the
   * hard-coded ground truth depends on this.
   */
  const unsigned int Dimension   = 2;
  const unsigned int SplineOrder = 3;
  typedef float CoordinateRepresentationType;
  const double distance = 1e-3; // the allowable distance
  //const double allowedTimeDifference = 0.1; // 10% is considered within limits
  /** The number of calls to Evaluate(). This number gives reasonably
   * fast test results in Release mode.
   */
  unsigned int N = static_cast< unsigned int >( 1e6 );

  /** Other typedefs. */
  typedef itk::BSplineInterpolationSecondOrderDerivativeWeightFunction<
    CoordinateRepresentationType,
    Dimension, SplineOrder >                SODerivativeWeightFunctionType;
  typedef SODerivativeWeightFunctionType::ContinuousIndexType ContinuousIndexType;
  typedef SODerivativeWeightFunctionType::WeightsType         WeightsType;

  std::cerr << "TESTING:\n" << std::endl;

  /**
   * *********** TESTING 1 ************************************************
   */

  std::cerr << "\nTESTING: derivatives (0,1)\n" << std::endl;

  /** Construct several weight functions. */
  SODerivativeWeightFunctionType::Pointer soWeightFunction
    = SODerivativeWeightFunctionType::New();

  /** Create and fill a continuous index.
   * NOTE: don't change this, since the hard-coded ground truth depends on this.
   */
  ContinuousIndexType cindex;
  cindex[ 0 ] =  3.1f;
  cindex[ 1 ] = -2.2f;
  soWeightFunction->SetDerivativeDirections( 0, 1 );

  /** Run evaluate for the second order derivative. */
  WeightsType soWeights = soWeightFunction->Evaluate( cindex );
  std::cerr << "weights (2nd order) " << soWeights << std::endl;

  /** Hard code the ground truth. You should change this if you change the
   * spline order.
   *
   * x1 =  3.1  ->  support y1 =  2  3  4  5  ->  x1 - y1 = 1.1 0.1 -0.9 -1.9
   * x2 = -2.2  ->  support y2 = -4 -3 -3 -1  ->  x2 - y2 = 1.8 0.8 -0.2 -1.2
   *
   * B3 is the third order B-spline. ?etc means repeat ? for ever.
   * The coefficients are:
   *   [ B2(x1-y1i+1/2)-B2(x1-y1i-1/2) ] * [ B2(x2-y2i+1/2)-B2(x2-y2i-1/2) ]
   *
   * B3d(  1.1 ) = -0.405
   * B3d(  0.1 ) = -0.185
   * B3d( -0.9 ) =  0.585
   * B3d( -1.9 ) =  0.005
   * B3d(  1.8 ) = -0.02
   * B3d(  0.8 ) = -0.64
   * B3d( -0.2 ) =  0.34
   * B3d( -1.2 ) =  0.32
   *
   *                       -> i
   *       0.0081    0.0037   -0.0117   -0.0001
   *  |    0.2592    0.1184   -0.3744   -0.0032
   *  j   -0.1377   -0.0629    0.1989    0.0017
   *      -0.1296   -0.0592    0.1872    0.0016
   *
   * These numbers are created by a small Matlab program. So, if this appears
   * to be not a valid check, then we made the same bug twice.
   */
  WeightsType trueSOWeights( 16 );
  trueSOWeights.Fill( 0.0 );
  trueSOWeights[  0 ] =  0.0081;
  trueSOWeights[  1 ] =  0.0037;
  trueSOWeights[  2 ] = -0.0117;
  trueSOWeights[  3 ] = -0.0001;
  trueSOWeights[  4 ] =  0.2592;
  trueSOWeights[  5 ] =  0.1184;
  trueSOWeights[  6 ] = -0.3744;
  trueSOWeights[  7 ] = -0.0032;
  trueSOWeights[  8 ] = -0.1377;
  trueSOWeights[  9 ] = -0.0629;
  trueSOWeights[ 10 ] =  0.1989;
  trueSOWeights[ 11 ] =  0.0017;
  trueSOWeights[ 12 ] = -0.1296;
  trueSOWeights[ 13 ] = -0.0592;
  trueSOWeights[ 14 ] =  0.1872;
  trueSOWeights[ 15 ] =  0.0016;

  /** Compute the distance between the two vectors. */
  double error = 0.0;
  for( unsigned int i = 0; i < soWeights.Size(); ++i )
  {
    error += vnl_math::sqr( soWeights[ i ] - trueSOWeights[ i ] );
  }
  error = std::sqrt( error );

  /** TEST: Compare the two qualitatively. */
  if( error > distance )
  {
    std::cerr << "ERROR: the first order weights differs more than "
              << distance << " from the truth." << std::endl;
    return 1;
  }
  std::cerr << std::showpoint;
  std::cerr << std::scientific;
  std::cerr << std::setprecision( 4 );
  std::cerr << "The distance is: " << error << std::endl;

  /** Time the so implementation. */
  clock_t startClock = clock();
  for( unsigned int i = 0; i < N; ++i )
  {
    soWeightFunction->Evaluate( cindex );
  }
  clock_t endClock = clock();
  clock_t elapsed  = endClock - startClock;
  std::cerr << "The elapsed time for the 2nd order derivative (0,1) is: "
            << elapsed << std::endl;

  /**
   * *********** TESTING 2 ************************************************
   */

  std::cerr << "\n--------------------------------------------------------";
  std::cerr << "\nTESTING: derivatives (0,0)\n" << std::endl;

  /** Run evaluate for the second order derivative. */
  soWeightFunction->SetDerivativeDirections( 0, 0 );
  soWeights = soWeightFunction->Evaluate( cindex );
  std::cerr << "weights (2nd order) " << soWeights << std::endl;

  /** Hard code the ground truth. You should change this if you change the
   * spline order.
   *
   * x1 =  3.1  ->  support y1 =  2  3  4  5  ->  x1 - y1 = 1.1 0.1 -0.9 -1.9
   * x2 = -2.2  ->  support y2 = -4 -3 -3 -1  ->  x2 - y2 = 1.8 0.8 -0.2 -1.2
   *
   * B3 is the third order B-spline. ?etc means repeat ? for ever.
   * The coefficients are:
   *   [ B1(x1-y1i+1) - 2*B1(x1-y1i) + B1(x1-y1i-1) ] * B3(x2-y2i)
   *
   * B3dd(  1.1 ) =  0.9
   * B3dd(  0.1 ) = -1.7
   * B3dd( -0.9 ) =  0.7
   * B3dd( -1.9 ) =  0.1
   * B3  (  1.8 ) =  0.0013etc
   * B3  (  0.8 ) =  0.2826etc
   * B3  ( -0.2 ) =  0.6306etc
   * B3  ( -1.2 ) =  0.0853etc
   *
   *                       -> i
   *      0.0012  -0.00226/   9.3/e-4    1.3/e-4
   *  |   0.2544  -0.48053/   0.19786/   0.02826/
   *  j   0.5676  -1.07213/   0.44146/   0.06306/
   *      0.0768  -0.14506/   0.05973/   0.00853/
   *
   * These numbers are created by a small Matlab program. So, if this appears
   * to be not a valid check, then we made the same bug twice.
   */
  trueSOWeights.Fill( 0.0 );
  trueSOWeights[  0 ] =  1.200000000000e-3;
  trueSOWeights[  1 ] = -2.266666666666e-3;
  trueSOWeights[  2 ] =  9.333333333333e-4;
  trueSOWeights[  3 ] =  1.333333333333e-4;
  trueSOWeights[  4 ] =  2.544000000000e-1;
  trueSOWeights[  5 ] = -4.805333333333e-1;
  trueSOWeights[  6 ] =  1.978666666666e-1;
  trueSOWeights[  7 ] =  2.826666666666e-2;
  trueSOWeights[  8 ] =  5.676000000000e-1;
  trueSOWeights[  9 ] = -1.072133333333;
  trueSOWeights[ 10 ] =  4.414666666666e-1;
  trueSOWeights[ 11 ] =  6.306666666666e-2;
  trueSOWeights[ 12 ] =  7.680000000000e-2;
  trueSOWeights[ 13 ] = -1.450666666666e-1;
  trueSOWeights[ 14 ] =  5.973333333333e-2;
  trueSOWeights[ 15 ] =  8.533333333333e-3;

  /** Compute the distance between the two vectors. */
  error = 0.0;
  for( unsigned int i = 0; i < soWeights.Size(); ++i )
  {
    error += vnl_math::sqr( soWeights[ i ] - trueSOWeights[ i ] );
  }
  error = std::sqrt( error );

  /** TEST: Compare the two qualitatively. */
  if( error > distance )
  {
    std::cerr << "ERROR: the first order weights differs more than "
              << distance << " from the truth." << std::endl;
    return 1;
  }
  std::cerr << std::showpoint;
  std::cerr << std::scientific;
  std::cerr << std::setprecision( 4 );
  std::cerr << "The distance is: " << error << std::endl;

  /** Time the so implementation. */
  startClock = clock();
  for( unsigned int i = 0; i < N; ++i )
  {
    soWeightFunction->Evaluate( cindex );
  }
  endClock = clock();
  elapsed  = endClock - startClock;
  std::cerr << "The elapsed time for the 2nd order derivative (0,0) is: "
            << elapsed << std::endl;

  /**
   * *********** Function TESTING ****************************************
   */

  std::cerr << "\n--------------------------------------------------------";
  std::cerr << "\nFunction TESTING:\n" << std::endl;

  /** Just call all available public functions. */
  SODerivativeWeightFunctionType::IndexType startIndex;
  SODerivativeWeightFunctionType::IndexType trueStartIndex;
  trueStartIndex[ 0 ] =  2;
  trueStartIndex[ 1 ] = -4;
  soWeightFunction->ComputeStartIndex( cindex, startIndex );
  if( startIndex != trueStartIndex )
  {
    std::cerr << "ERROR: wrong start index was computed." << std::endl;
    return 1;
  }

  SODerivativeWeightFunctionType::SizeType trueSize;
  trueSize.Fill( SplineOrder + 1 );
  if( soWeightFunction->GetSupportSize() != trueSize )
  {
    std::cerr << "ERROR: wrong support size was computed." << std::endl;
    return 1;
  }

  if( soWeightFunction->GetNumberOfWeights()
    != static_cast< unsigned long >( std::pow(
    static_cast< float >( SplineOrder + 1 ), 2.0f ) ) )
  {
    std::cerr << "ERROR: wrong number of weights was computed." << std::endl;
    return 1;
  }

  std::cerr << "All public functions returned valid output." << std::endl;

  /**
   * *********** PrintSelf TESTING ****************************************
   */

  std::cerr << "\n--------------------------------------------------------";
  std::cerr << "\nPrintSelf() TESTING:\n" << std::endl;

  soWeightFunction->Print( std::cerr, 0 );

  /** Return a value. */
  return 0;

} // end main