File: itkThinPlateTransformWriteReadTest.cxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (191 lines) | stat: -rw-r--r-- 7,290 bytes parent folder | download | duplicates (4)
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
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  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.
 *
 *=========================================================================*/

/**
 * This tests that kernel transforms are initialized correctly when they are loaded
 * A transform is created, written to a file, loaded, and the result of the transform
 * are compared to the expected results.
 */

#include "itkTransformFileWriter.h"
#include "itkTransformFileReader.h"
#include "itkElasticBodySplineKernelTransform.h"
#include "itkElasticBodyReciprocalSplineKernelTransform.h"
#include "itkThinPlateSplineKernelTransform.h"
#include "itkThinPlateR2LogRSplineKernelTransform.h"
#include "itkVolumeSplineKernelTransform.h"
#include "itksys/SystemTools.hxx"
#include "itkTestingMacros.h"
#include "itkHDF5TransformIOFactory.h"
#include "itkHDF5TransformIO.h"
#include <itkTransformFactory.h>

template<typename TransformType>
static int ReadWriteTest(const char * const fileName)
{
  const double epsilon = 1e-12;
  typedef typename TransformType::ScalarType ParametersValueType;

  // Create transform
  typename TransformType::Pointer     transform       = TransformType::New();
  typedef typename TransformType::InputPointType PointType;
  typedef typename TransformType::PointsIterator PointsIteratorType;
  typedef typename TransformType::PointSetType   PointSetType;

  PointType sourcePoint;
  PointType targetPoint;
  PointType mappedPoint;

  // Reserve memory for the number of points
  typename PointSetType::Pointer sourceLandmarks = PointSetType::New();
  typename PointSetType::Pointer targetLandmarks = PointSetType::New();

  sourceLandmarks->GetPoints()->Reserve( 4 );
  targetLandmarks->GetPoints()->Reserve( 4 );

  // Create landmark sets
  PointsIteratorType sourceit = sourceLandmarks->GetPoints()->Begin();
  PointsIteratorType targetit = targetLandmarks->GetPoints()->Begin();

  PointsIteratorType sourceend = sourceLandmarks->GetPoints()->End();

  for (int i = 0; i < 2; i++)
    {
    for (int j = 0; j < 2; j++)
      {
      sourcePoint[0] = j;
      sourcePoint[1] = i;
      sourceit.Value() = sourcePoint;
      targetPoint[0] = 3*j;
      targetPoint[1] = 3*i;
      targetit.Value() = targetPoint;
      sourceit++;
      targetit++;
      }
    }

  transform->SetSourceLandmarks( sourceLandmarks );
  transform->SetTargetLandmarks( targetLandmarks );
  transform->ComputeWMatrix();

  sourceit = sourceLandmarks->GetPoints()->Begin();
  targetit = targetLandmarks->GetPoints()->Begin();

  sourceend = sourceLandmarks->GetPoints()->End();
  while( sourceit != sourceend )
    {
    sourcePoint = sourceit.Value();
    targetPoint = targetit.Value();
    mappedPoint = transform->TransformPoint(sourcePoint);
    std::cout << sourcePoint << " : " << targetPoint;
    std::cout << " warps to: " << mappedPoint << std::endl;
    if( mappedPoint.EuclideanDistanceTo( targetPoint ) > epsilon )
      {
      return EXIT_FAILURE;
      }
    ++sourceit;
    ++targetit;
    }

  // Write transform to file
  typedef typename itk::TransformFileWriterTemplate<ParametersValueType>      TransformWriterType;
  typename TransformWriterType::Pointer transformWriter = TransformWriterType::New();
  transformWriter->SetFileName( fileName );
  transformWriter->AddTransform( transform );
  TRY_EXPECT_NO_EXCEPTION( transformWriter->Update() );

  // Read transform file
  typedef typename itk::TransformFileReaderTemplate<ParametersValueType>      TransformReaderType;
  typename TransformReaderType::Pointer transformReader = TransformReaderType::New();
  transformReader->SetFileName(fileName);
  TRY_EXPECT_NO_EXCEPTION( transformReader->Update() );

  // Compare read transform results with expected results
  const typename TransformReaderType::TransformListType * list = transformReader->GetTransformList();
  if ( list->size() != 1 )
    {
    std::cerr << "Failure: There should be only one transform in the file!" << std::endl;
    return EXIT_FAILURE;
    }

  TransformType* readTransform = static_cast<TransformType*>(list->front().GetPointer());
  sourceit = sourceLandmarks->GetPoints()->Begin();
  targetit = targetLandmarks->GetPoints()->Begin();
  sourceend = sourceLandmarks->GetPoints()->End();
  while( sourceit != sourceend )
    {
    sourcePoint = sourceit.Value();
    targetPoint = targetit.Value();
    mappedPoint = readTransform->TransformPoint(sourcePoint);
    std::cout << sourcePoint << " : " << targetPoint;
    std::cout << " warps to: " << mappedPoint << std::endl;
    if( mappedPoint.EuclideanDistanceTo( targetPoint ) > epsilon )
      {
      return EXIT_FAILURE;
      }
    ++sourceit;
    ++targetit;
    }
  return EXIT_SUCCESS;
}

int itkThinPlateTransformWriteReadTest( int argc, char *argv[] )
{
  if( argc != 2 )
    {
    std::cerr << "Usage: " << argv[0] << " outputDirectory" << std::endl;
    return EXIT_FAILURE;
    }

  itksys::SystemTools::ChangeDirectory(argv[1]);

  typedef itk::ElasticBodySplineKernelTransform<double, 2>           EBSTransform2DType;
  typedef itk::ElasticBodyReciprocalSplineKernelTransform<double, 2> EBRSTransform2DType;
  typedef itk::ThinPlateSplineKernelTransform<double, 2>             TPSTransform2DType;
  typedef itk::ThinPlateR2LogRSplineKernelTransform<double, 2>       TPR2LRSTransform2DType;
  typedef itk::VolumeSplineKernelTransform<double, 2>                VSTransform2DType;

  // Registers all transforms that are tested
  itk::TransformFactory<EBSTransform2DType>::RegisterTransform();
  itk::TransformFactory<EBRSTransform2DType>::RegisterTransform();
  itk::TransformFactory<TPSTransform2DType>::RegisterTransform();
  itk::TransformFactory<TPR2LRSTransform2DType>::RegisterTransform();
  itk::TransformFactory<VSTransform2DType>::RegisterTransform();

  // Register transform IO used to save the transforms
  itk::ObjectFactoryBase::RegisterFactory(itk::HDF5TransformIOFactory::New() );

  // Run tests
  int resultEBS = ReadWriteTest<EBSTransform2DType>("ElasticBodySplineKernelTransform_double_2.h5");
  int resultEBRS = ReadWriteTest<EBRSTransform2DType>("ElasticBodyReciprocalSplineKernelTransform_double_2.h5");
  int resultTPS = ReadWriteTest<TPSTransform2DType>("ThinPlateSplineKernelTransform_double_2.h5");
  int resultTPR2 = ReadWriteTest<TPR2LRSTransform2DType>("ThinPlateR2LogRSplineKernelTransform_double_2.h5");
  int resultVS = ReadWriteTest<VSTransform2DType>("VolumeSplineKernelTransform_double_2.h5");

  // Check results
  if( resultEBS != EXIT_SUCCESS
   || resultEBRS != EXIT_SUCCESS
   || resultTPS != EXIT_SUCCESS
   || resultTPR2 != EXIT_SUCCESS
   || resultVS != EXIT_SUCCESS
   )
   {
     return EXIT_FAILURE;
   }
   return EXIT_SUCCESS;
}