File: itkIOTransformTxtTest.cxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 489,260 kB
  • sloc: cpp: 557,342; ansic: 146,850; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 129; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (280 lines) | stat: -rw-r--r-- 8,952 bytes parent folder | download | duplicates (3)
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
/*=========================================================================
 *
 *  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.
 *
 *=========================================================================*/

#include <iostream>
#include <fstream>
#include "itkTestingMacros.h"
#include "itkTxtTransformIOFactory.h"
#include "itkTransformFileWriter.h"
#include "itkTransformFileReader.h"
#include "itkAffineTransform.h"
#include "itkTransformFactory.h"
#include "itkSimilarity2DTransform.h"
#include "itkBSplineTransform.h"
#include "itksys/SystemTools.hxx"

template<typename ScalarType>
static int oneTest(const std::string & outputDirectory, const char *goodname,const char *badname)
{
  unsigned int i;
  typedef itk::AffineTransform<ScalarType,4>  AffineTransformType;
  typedef itk::AffineTransform<ScalarType,10> AffineTransformTypeNotRegistered;
  typename AffineTransformType::Pointer affine = AffineTransformType::New();
  typename AffineTransformType::InputPointType cor;

  itk::ObjectFactoryBase::RegisterFactory(itk::TxtTransformIOFactory::New() );

  // Set it's parameters
    {
    typename AffineTransformType::ParametersType p = affine->GetParameters();
    for ( i = 0; i < p.GetSize(); i++ )
      {
      p[i] = i;
      }
    affine->SetParameters ( p );
    }
    {
    typename AffineTransformType::FixedParametersType p = affine->GetFixedParameters ();
    for ( i = 0; i < p.GetSize(); i++ )
      {
      p[i] = i;
      }
    affine->SetFixedParameters ( p );
    }
  typename itk::TransformFileWriterTemplate<ScalarType>::Pointer writer;
  typename itk::TransformFileReaderTemplate<ScalarType>::Pointer reader;

  reader = itk::TransformFileReaderTemplate<ScalarType>::New();
  writer = itk::TransformFileWriterTemplate<ScalarType>::New();
  writer->AddTransform(affine);

  writer->SetFileName( outputDirectory + goodname );
  reader->SetFileName( outputDirectory + goodname );

  // Testing writing std::cout << "Testing write : ";
  affine->Print ( std::cout );
  try
    {
    writer->Update();
    std::cout << std::endl;
    std::cout << "Testing read : " << std::endl;
    reader->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Error while saving the transforms" << std::endl;
    std::cerr << excp << std::endl;
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
    }


  try
    {
    const typename itk::TransformFileReaderTemplate<ScalarType>::TransformListType * list = reader->GetTransformList();
    typename itk::TransformFileReaderTemplate<ScalarType>::TransformListType::const_iterator lit = list->begin();
    while ( lit != list->end() )
      {
      (*lit)->Print ( std::cout );
      ++lit;
      }

    if ( list->size() != 1 )
      {
      std::cerr << "Failure: Read too many transforms!" << std::endl;
      return EXIT_FAILURE;
      }

    if( dynamic_cast<AffineTransformType*>(list->front().GetPointer()) == ITK_NULLPTR )
      {
      std::cerr << "Failure to dynamic_cast read transform!" << std::endl;
      return EXIT_FAILURE;
      }

    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Error while saving the transforms" << std::endl;
    std::cerr << excp << std::endl;
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
    }


  std::cout << "Creating bad writer" << std::endl;
  typename AffineTransformTypeNotRegistered::Pointer Bogus = AffineTransformTypeNotRegistered::New();

  // Set it's parameters
    {
    typename AffineTransformType::ParametersType p = Bogus->GetParameters();
    for ( i = 0; i < p.GetSize(); i++ )
      {
      p[i] = i;
      }
    Bogus->SetParameters ( p );
    }
    {
    typename AffineTransformType::FixedParametersType p = Bogus->GetFixedParameters ();
    for ( i = 0; i < p.GetSize(); i++ )
      {
      p[i] = i;
      }
    Bogus->SetFixedParameters ( p );
    }

  typename itk::TransformFileWriterTemplate<ScalarType>::Pointer badwriter;
  typename itk::TransformFileReaderTemplate<ScalarType>::Pointer badreader;
  badreader = itk::TransformFileReaderTemplate<ScalarType>::New();
  badwriter = itk::TransformFileWriterTemplate<ScalarType>::New();
  badwriter->AddTransform( Bogus );
  badwriter->SetFileName( outputDirectory + badname );
  badreader->SetFileName( outputDirectory + badname );

  // Testing writing
  std::cout << "Testing write of non register transform : " << std::endl;
  std::cout << std::flush;
  try
    {
    badwriter->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Error while saving the transforms" << std::endl;
    std::cerr << excp << std::endl;
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
    }

  // Testing writing
  std::cout << "Testing read of non register transform : " << std::endl;
  std::cout << std::flush;
  bool caught = false;
  try
    {
    badreader->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    caught = true;
    std::cout << "Caught exception as expected" << std::endl;
    std::cout << excp << std::endl;
    }
  if ( !caught )
    {
    std::cerr << "Did not catch non registered transform" << std::endl;
    std::cout << "[FAILED]" << std::endl;
    return EXIT_FAILURE;
    }
  std::cout << "[PASSED]" << std::endl;

  return EXIT_SUCCESS;
}

//
// test endless loop bug in transform reader, triggered by no
// EOL at end of file.
// This test will exercise this reported bug:
// http://public.kitware.com/Bug/view.php?id=7028
template<typename ScalarType>
int
secondTest( const std::string & outputDirectory )
{
  std::filebuf fb;
  fb.open( (outputDirectory + "IllegalTransform.txt").c_str(), std::ios::out );
  std::ostream os( &fb );
  os << "#Insight Transform File V1.0"
     << std::endl
     << "#Transform 0"
     << std::endl
     << "Transform: AffineTransform_double_10_10"
     << std::endl
     << "Parameters: "
     << "  0 1 2 3 4 5 6 7 8 9 10 11 12"
     << " 13 14 15 16 17 18 19 20 21 22"
     << std::endl
     << "FixedParameters: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18";
  fb.close();
  typename itk::TransformFileReaderTemplate<ScalarType>::Pointer reader;
  reader = itk::TransformFileReaderTemplate<ScalarType>::New();
  reader->SetFileName( outputDirectory + "IllegalTransform.txt" );
  try
    {
    reader->Update();
    std::cerr << "FAILED to throw expected exception" << std::endl;
    const typename itk::TransformFileReaderTemplate<ScalarType>::TransformListType * list = reader->GetTransformList();
    typename itk::TransformFileReaderTemplate<ScalarType>::TransformListType::const_iterator lit =
      list->begin();
    while ( lit != list->end() )
      {
      (*lit)->Print ( std::cout );
      ++lit;
      }
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "EXPECTED Error while reading the transforms" << std::endl;
    std::cerr << excp << std::endl;
    std::cout << "[SUCCESS]" << std::endl;
    return EXIT_SUCCESS;
    }
  return EXIT_FAILURE;
}


int
templatelessTest( const std::string & outputDirectory )
{
  const std::string outputFile = outputDirectory + "itkIOTransformTxtTestRigid2DTransform.tfm";

  typedef itk::Rigid2DTransform< float > TransformType;
  TransformType::Pointer transform = TransformType::New();

  itk::TransformFileWriter::Pointer writer = itk::TransformFileWriter::New();
  writer->SetInput( transform );
  writer->SetFileName( outputFile );
  TRY_EXPECT_NO_EXCEPTION( writer->Update() );

  return EXIT_SUCCESS;
}


int itkIOTransformTxtTest(int argc, char* argv[])
{
  if (argc < 2)
    {
    std::cerr << "Usage: "
              << argv[0]
              << " outputDirectory"
              << std::endl;

    itksys::SystemTools::ChangeDirectory(argv[1]);
    }
  const std::string outputDirectory = std::string(argv[1]) + "/";
  const int result1 =  oneTest<float>( outputDirectory, "Transforms_float.txt", "TransformsBad_float.txt" );
  const int result2 =  secondTest<float>( outputDirectory );

  const int result3 =  oneTest<double>( outputDirectory, "Transforms_double.txt", "TransformsBad_double.txt" );
  const int result4 =  secondTest<double>( outputDirectory );

  const int result5 = templatelessTest( outputDirectory );

  return  ( result1 == EXIT_SUCCESS && result2 == EXIT_SUCCESS &&
            result3 == EXIT_SUCCESS && result4 == EXIT_SUCCESS &&
            result5 == EXIT_SUCCESS )? EXIT_SUCCESS : EXIT_FAILURE
          ;
}