File: itkAdvancedBSplineDeformableTransformTest.cxx

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (424 lines) | stat: -rw-r--r-- 16,116 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*=========================================================================
 *
 *  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 "itkAdvancedBSplineDeformableTransform.h"
#include "itkBSplineDeformableTransform.h" // original ITK
//#include "itkBSplineTransform.h" // new ITK4
#include "itkGridScheduleComputer.h"

#include <ctime>
#include <fstream>
#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 = 3;
  const unsigned int SplineOrder = 3;
  using CoordinateRepresentationType = float;
  // const double distance = 1e-3; // the allowable distance
  // const double allowedTimeDifference = 0.1; // 10% is considered within limits

  /** The number of calls to Evaluate(). Distinguish between
   * Debug and Release mode.
   */
#ifndef NDEBUG
  unsigned int N = static_cast<unsigned int>(1e3);
#else
  unsigned int N = static_cast<unsigned int>(0.5e5);
#endif
  std::cerr << "N = " << N << std::endl;

  /** Check. */
  if (argc != 2)
  {
    std::cerr << "ERROR: You should specify a text file with the B-spline transformation parameters." << std::endl;
    return 1;
  }

  /** Other typedefs. */
  using TransformType = itk::AdvancedBSplineDeformableTransform<CoordinateRepresentationType, Dimension, SplineOrder>;
  using ITKTransformType = itk::BSplineDeformableTransform<
    // typedef itk::BSplineTransform<
    CoordinateRepresentationType,
    Dimension,
    SplineOrder>;
  using JacobianType = TransformType::JacobianType;
  using SpatialJacobianType = TransformType::SpatialJacobianType;
  using SpatialHessianType = TransformType::SpatialHessianType;
  using JacobianOfSpatialJacobianType = TransformType::JacobianOfSpatialJacobianType;
  using JacobianOfSpatialHessianType = TransformType::JacobianOfSpatialHessianType;
  using NonZeroJacobianIndicesType = TransformType::NonZeroJacobianIndicesType;
  using NumberOfParametersType = TransformType::NumberOfParametersType;
  using InputPointType = TransformType::InputPointType;
  using OutputPointType = TransformType::OutputPointType;
  using ParametersType = TransformType::ParametersType;
  using InputImageType = itk::Image<CoordinateRepresentationType, Dimension>;
  using RegionType = InputImageType::RegionType;
  using SizeType = InputImageType::SizeType;
  using IndexType = InputImageType::IndexType;
  using SpacingType = InputImageType::SpacingType;
  using OriginType = InputImageType::PointType;
  using DirectionType = InputImageType::DirectionType;

  /** Create the transform. */
  auto transform = TransformType::New();
  auto transformITK = ITKTransformType::New();

  /** Setup the B-spline transform:
   * (GridSize 44 43 35)
   * (GridIndex 0 0 0)
   * (GridSpacing 10.7832773148 11.2116431394 11.8648235177)
   * (GridOrigin -237.6759555555 -239.9488431747 -344.2315805162)
   */
  SizeType gridSize;
  gridSize[0] = 44;
  gridSize[1] = 43;
  gridSize[2] = 35;
  IndexType  gridIndex{};
  RegionType gridRegion;
  gridRegion.SetSize(gridSize);
  gridRegion.SetIndex(gridIndex);
  SpacingType gridSpacing;
  gridSpacing[0] = 10.7832773148;
  gridSpacing[1] = 11.2116431394;
  gridSpacing[2] = 11.8648235177;
  OriginType gridOrigin;
  gridOrigin[0] = -237.6759555555;
  gridOrigin[1] = -239.9488431747;
  gridOrigin[2] = -344.2315805162;
  const auto gridDirection = DirectionType::GetIdentity();

  transform->SetGridOrigin(gridOrigin);
  transform->SetGridSpacing(gridSpacing);
  transform->SetGridRegion(gridRegion);
  transform->SetGridDirection(gridDirection);

  transformITK->SetGridOrigin(gridOrigin);
  transformITK->SetGridSpacing(gridSpacing);
  transformITK->SetGridRegion(gridRegion);
  transformITK->SetGridDirection(gridDirection);

  // ParametersType fixPar( Dimension * ( 3 + Dimension ) );
  // fixPar[ 0 ] = gridSize[ 0 ]; fixPar[ 1 ] = gridSize[ 1 ]; fixPar[ 2 ] = gridSize[ 2 ];
  // fixPar[ 3 ] = gridOrigin[ 0 ]; fixPar[ 4 ] = gridOrigin[ 1 ]; fixPar[ 5 ] = gridOrigin[ 2 ];
  // fixPar[ 6 ] = gridSpacing[ 0 ]; fixPar[ 7 ] = gridSpacing[ 1 ]; fixPar[ 8 ] = gridSpacing[ 2 ];
  // fixPar[ 9 ] = gridDirection[ 0 ][ 0 ]; fixPar[ 10 ] = gridDirection[ 0 ][ 1 ]; fixPar[ 11 ] = gridDirection[ 0 ][ 2
  // ]; fixPar[ 12 ] = gridDirection[ 1 ][ 0 ]; fixPar[ 13 ] = gridDirection[ 1 ][ 1 ]; fixPar[ 14 ] = gridDirection[ 1
  // ][ 2 ]; fixPar[ 15 ] = gridDirection[ 2 ][ 0 ]; fixPar[ 16 ] = gridDirection[ 2 ][ 1 ]; fixPar[ 17 ] =
  // gridDirection[ 2 ][ 2 ]; transformITK->SetFixedParameters( fixPar );

  /** Now read the parameters as defined in the file par.txt. */
  ParametersType parameters(transform->GetNumberOfParameters());
  std::ifstream  input(argv[1]);
  if (input.is_open())
  {
    for (unsigned int i = 0; i < parameters.GetSize(); ++i)
    {
      input >> parameters[i];
    }
  }
  else
  {
    std::cerr << "ERROR: could not open the text file containing the parameter values." << std::endl;
    return 1;
  }
  transform->SetParameters(parameters);
  transformITK->SetParameters(parameters);

  /** Get the number of nonzero Jacobian indices. */
  const NumberOfParametersType nonzji = transform->GetNumberOfNonZeroJacobianIndices();

  /** Declare variables. */
  InputPointType inputPoint;
  inputPoint.Fill(4.1f);
  JacobianType                  jacobian;
  SpatialJacobianType           spatialJacobian;
  SpatialHessianType            spatialHessian;
  JacobianOfSpatialJacobianType jacobianOfSpatialJacobian;
  JacobianOfSpatialHessianType  jacobianOfSpatialHessian;
  NonZeroJacobianIndicesType    nzji;

  /** Resize some of the variables. */
  nzji.resize(nonzji);
  jacobian.set_size(Dimension, nonzji);
  jacobianOfSpatialJacobian.resize(nonzji);
  jacobianOfSpatialHessian.resize(nonzji);
  jacobian.Fill(0.0);

  /**
   *
   * Call functions for testing that they don't crash.
   *
   */

  /** The Jacobian. */
  transform->GetJacobian(inputPoint, jacobian, nzji);

  /** The spatial Jacobian. */
  transform->GetSpatialJacobian(inputPoint, spatialJacobian);

  /** The spatial Hessian. */
  transform->GetSpatialHessian(inputPoint, spatialHessian);

  /** The Jacobian of the spatial Jacobian. */
  transform->GetJacobianOfSpatialJacobian(inputPoint, jacobianOfSpatialJacobian, nzji);

  transform->GetJacobianOfSpatialJacobian(inputPoint, spatialJacobian, jacobianOfSpatialJacobian, nzji);

  /** The Jacobian of the spatial Hessian. */
  transform->GetJacobianOfSpatialHessian(inputPoint, jacobianOfSpatialHessian, nzji);

  transform->GetJacobianOfSpatialHessian(inputPoint, spatialHessian, jacobianOfSpatialHessian, nzji);

  //   /***/
  //   transform->GetSpatialHessian( inputPoint, spatialHessian );
  //   for ( unsigned int i = 0; i < Dimension; ++i )
  //   {
  //     std::cerr << spatialHessian[ i ] << std::endl;
  //   }
  //
  //   /***/
  //   transform->GetJacobianOfSpatialHessian( inputPoint,
  //     spatialHessian, jacobianOfSpatialHessian, nzji );
  //   for ( unsigned int mu = 0; mu < 2; ++mu )
  //   {
  //     for ( unsigned int i = 0; i < Dimension; ++i )
  //     {
  //       std::cerr << jacobianOfSpatialHessian[ mu ][ i ] << std::endl;
  //     }
  //   }

  /**
   *
   * Call functions for timing.
   *
   */

  clock_t startClock, endClock, clockITK;

  /** Time the implementation of the Jacobian. */
  startClock = clock();
  for (unsigned int i = 0; i < N * 10; ++i)
  {
    transform->GetJacobian(inputPoint, jacobian, nzji);
  }
  endClock = clock();
  clockITK = endClock - startClock;
  std::cerr << "The elapsed time for the Jacobian is: " << clockITK / 1000.0 << " s." << std::endl;

  /** Time the implementation of the spatial Jacobian. */
  startClock = clock();
  for (unsigned int i = 0; i < N; ++i)
  {
    transform->GetSpatialJacobian(inputPoint, spatialJacobian);
  }
  endClock = clock();
  clockITK = endClock - startClock;
  std::cerr << "The elapsed time for the spatial Jacobian is: " << clockITK / 1000.0 << " s." << std::endl;

  /** Time the implementation of the spatial Hessian. */
  startClock = clock();
  for (unsigned int i = 0; i < N; ++i)
  {
    transform->GetSpatialHessian(inputPoint, spatialHessian);
  }
  endClock = clock();
  clockITK = endClock - startClock;
  std::cerr << "The elapsed time for the spatial Hessian is: " << clockITK / 1000.0 << " s." << std::endl;

  /** Time the implementation of the Jacobian of the spatial Jacobian. */
  startClock = clock();
  for (unsigned int i = 0; i < N; ++i)
  {
    transform->GetJacobianOfSpatialJacobian(inputPoint, jacobianOfSpatialJacobian, nzji);
  }
  endClock = clock();
  clockITK = endClock - startClock;
  std::cerr << "The elapsed time for the Jacobian of the spatial Jacobian is: " << clockITK / 1000.0 << " s."
            << std::endl;

  /** Time the implementation of the Jacobian of the spatial Hessian. */
  startClock = clock();
  for (unsigned int i = 0; i < N; ++i)
  {
    transform->GetJacobianOfSpatialHessian(inputPoint, jacobianOfSpatialHessian, nzji);
  }
  endClock = clock();
  clockITK = endClock - startClock;
  std::cerr << "The elapsed time for the Jacobian of the spatial Hessian is: " << clockITK / 1000.0 << " s."
            << std::endl;

  /** Time the implementation of the spatial Jacobian and its Jacobian. */
  startClock = clock();
  for (unsigned int i = 0; i < N; ++i)
  {
    transform->GetSpatialJacobian(inputPoint, spatialJacobian);
    transform->GetJacobianOfSpatialJacobian(inputPoint, jacobianOfSpatialJacobian, nzji);
  }
  endClock = clock();
  clockITK = endClock - startClock;
  std::cerr << "The elapsed time for the (Jacobian of the) spatial Jacobian (2 func) is: " << clockITK / 1000.0 << " s."
            << std::endl;

  /** Time the implementation of the spatial Jacobian and its Jacobian. */
  startClock = clock();
  for (unsigned int i = 0; i < N; ++i)
  {
    transform->GetJacobianOfSpatialJacobian(inputPoint, spatialJacobian, jacobianOfSpatialJacobian, nzji);
  }
  endClock = clock();
  clockITK = endClock - startClock;
  std::cerr << "The elapsed time for the (Jacobian of the) spatial Jacobian (1 func) is: " << clockITK / 1000.0 << " s."
            << std::endl;

  /** Time the implementation of the spatial Hessian. */
  startClock = clock();
  for (unsigned int i = 0; i < N; ++i)
  {
    transform->GetSpatialHessian(inputPoint, spatialHessian);
    transform->GetJacobianOfSpatialHessian(inputPoint, jacobianOfSpatialHessian, nzji);
  }
  endClock = clock();
  clockITK = endClock - startClock;
  std::cerr << "The elapsed time for the (Jacobian of the) spatial Hessian (2 func) is: " << clockITK / 1000.0 << " s."
            << std::endl;

  /** Additional checks. */
  if (!transform->GetHasNonZeroSpatialHessian())
  {
    std::cerr << "ERROR: GetHasNonZeroSpatialHessian() should return true." << std::endl;
    return 1;
  }
  if (!transform->GetHasNonZeroJacobianOfSpatialHessian())
  {
    std::cerr << "ERROR: GetHasNonZeroJacobianOfSpatialHessian() should return true." << std::endl;
    return 1;
  }

  /** These should return the same values as the original ITK functions. */
  OutputPointType opp1 = transform->TransformPoint(inputPoint);
  OutputPointType opp2 = transformITK->TransformPoint(inputPoint);
  double          differenceNorm = 0.0;
  for (unsigned int i = 0; i < Dimension; ++i)
  {
    differenceNorm += (opp1[i] - opp2[i]) * (opp1[i] - opp2[i]);
  }
  if (std::sqrt(differenceNorm) > 1e-10)
  {
    std::cerr << "ERROR: Advanced B-spline TransformPoint() returning incorrect result." << std::endl;
    return 1;
  }

  JacobianType jacobianITK{};
  transformITK->ComputeJacobianWithRespectToParameters(inputPoint, jacobianITK);
  JacobianType jacobianElastix;
  jacobianElastix.set_size(Dimension, nzji.size());
  jacobianElastix.fill(0.0);
  transform->GetJacobian(inputPoint, jacobianElastix, nzji);
  // ITK4 B-spline is non-local and returns a full matrix. Cannot compute diff like this anymore:
  // JacobianType jacobianDifferenceMatrix = jacobianElastix - jacobianITK;
  // I believe there are future plans to re-enable local support B-splines
  double jacDiff = 0.0;
  for (unsigned int i = 0; i < nzji.size(); ++i)
  {
    for (unsigned int j = 0; j < Dimension; ++j)
    {
      jacDiff += vnl_math::sqr(jacobianElastix[j][i] - jacobianITK[j][nzji[i]]);
    }
  }
  // if ( jacobianDifferenceMatrix.frobenius_norm() > 1e-10 )
  if (std::sqrt(jacDiff) > 1e-10)
  {
    std::cerr << "ERROR: Advanced B-spline GetJacobian() returning incorrect result." << std::endl;
    return 1;
  }

  //// Check
  // JacobianType jacobian1, jacobian2;
  // jacobian1.set_size( Dimension, nzji.size() ); jacobian2.set_size( Dimension, nzji.size() );
  // jacobian1.fill( 0.0 ); jacobian2.Fill( 0.0 );
  // transform->GetJacobian( inputPoint, jacobian1, nzji );
  // transform->GetJacobian_opt( inputPoint, jacobian2, nzji );
  // JacobianType jacobianDifferenceMatrix = jacobian1 - jacobian2;
  // if ( jacobianDifferenceMatrix.frobenius_norm () > 1e-10 )
  //{
  //  std::cerr << "ERROR: Advanced B-spline GetJacobian_opt() returning incorrect result." << std::endl;
  //  return 1;
  //}

  //// Check
  // SpatialHessianType spatialHessian1, spatialHessian2;
  // JacobianOfSpatialHessianType jacobianOfSpatialHessian1, jacobianOfSpatialHessian2;
  // NonZeroJacobianIndicesType nzji1, nzji2;
  // transform->GetJacobianOfSpatialHessian( inputPoint,
  //  spatialHessian1, jacobianOfSpatialHessian1, nzji1 );
  // transform->GetJacobianOfSpatialHessian_opt( inputPoint,
  //  spatialHessian2, jacobianOfSpatialHessian2, nzji2 );
  // double shDiff = 0.0;
  // for( unsigned int i = 0; i < Dimension; i++ ) {
  //  for( unsigned int j = 0; j < Dimension; j++ ) {
  //    for( unsigned int k = 0; k < Dimension; k++ ) {
  //      shDiff += vnl_math::sqr( spatialHessian1[i][j][k] - spatialHessian2[i][j][k] );
  //    } } }
  // double jshDiff = 0.0;
  // for( unsigned int mu = 0; mu < nzji.size(); mu++ ) {
  //  for( unsigned int i = 0; i < Dimension; i++ ) {
  //    for( unsigned int j = 0; j < Dimension; j++ ) {
  //      for( unsigned int k = 0; k < Dimension; k++ ) {
  //        jshDiff += vnl_math::sqr( jacobianOfSpatialHessian1[mu][i][j][k] - jacobianOfSpatialHessian2[mu][i][j][k] );
  //      } } } }
  // if ( std::sqrt( shDiff ) > 1e-8 )
  //{
  //  std::cerr << "ERROR: Advanced B-spline GetJacobianOfSpatialHessian_opt() "
  //    << "returning incorrect spatial Hessian result: MSD = "
  //    << std::sqrt( shDiff ) << std::endl;
  //  return 1;
  //}
  // if ( std::sqrt( jshDiff ) > 1e-8 )
  //{
  //  std::cerr << "ERROR: Advanced B-spline GetJacobianOfSpatialHessian_opt() "
  //    << "returning incorrect Jacobian of spatial Hessian result: MSD = "
  //    << std::sqrt( jshDiff ) << std::endl;
  //  return 1;
  //}

  if ((transform->GetParameters() - transformITK->GetParameters()).two_norm() > 1e-10)
  {
    std::cerr << "ERROR: Advanced B-spline GetParameters() returning incorrect result." << std::endl;
    return 1;
  }

  if ((transform->GetFixedParameters() - transformITK->GetFixedParameters()).two_norm() > 1e-10)
  {
    std::cerr << "ERROR: Advanced B-spline GetFixedParameters() returning incorrect result." << std::endl;
    return 1;
  }

  /** Exercise PrintSelf(). */
  transform->Print(std::cerr);

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

} // end main