File: ConvertTransformFile.cxx

package info (click to toggle)
ants 2.5.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,672 kB
  • sloc: cpp: 85,685; sh: 15,850; perl: 863; xml: 115; python: 111; makefile: 68
file content (581 lines) | stat: -rw-r--r-- 18,361 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*=========================================================================

  Program:   Advanced Normalization Tools

  Copyright (c) ConsortiumOfANTS. All rights reserved.
  See accompanying COPYING.txt or
 https://github.com/stnava/ANTs/blob/master/ANTSCopyright.txt for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "antsUtilities.h"
#include <algorithm>

#include <iostream>
#include <sys/stat.h>

#include "itksys/SystemTools.hxx"

#include <fstream>
#include <cstdio>

#include "itkTransformFactory.h"
#include "itkAffineTransform.h"
#include "itkTranslationTransform.h"
#include "itkIdentityTransform.h"

#include "itkantsReadWriteTransform.h"

/* Utility to read in a transform file (presumed to be in binary format) and output
 * it in one of several different formats, defaulting to legacy text format for human reading.
 * Options are available to instead output only a transform matrix to a text file,
 * one row per dimension with space-delimited values. This option works only for
 * transforms of MatrixOffsetTransformBase or derived, Translation and Identity transforms. */

namespace ants
{
using namespace std;

/*
 *
 */
bool
FileExists(string strFilename)
{
  struct stat stFileInfo;
  bool        blnReturn;
  int         intStat;

  // Attempt to get the file attributes
  intStat = stat(strFilename.c_str(), &stFileInfo);
  if (intStat == 0)
  {
    // We were able to get the file attributes
    // so the file obviously exists.
    blnReturn = true;
  }
  else
  {
    // We were not able to get the file attributes.
    // This may mean that we don't have permission to
    // access the folder which contains this file. If you
    // need to do that level of checking, lookup the
    // return values of stat which will give you
    // more details on why stat failed.
    blnReturn = false;
  }

  return blnReturn;
}

/*
 *
 */
template <typename TTransform>
bool
GetMatrix(const typename TTransform::Pointer & transform, typename TTransform::MatrixType & matrix, bool outputRAS)
{
  const unsigned int ImageDimension = TTransform::InputSpaceDimension;

  using ScalarType = typename TTransform::ScalarType;

  matrix.Fill(itk::NumericTraits<typename TTransform::ScalarType>::ZeroValue());
  bool done = false;

  // Matrix-offset derived
  {
    using CastTransformType = itk::MatrixOffsetTransformBase<ScalarType, ImageDimension, ImageDimension>;
    typename CastTransformType::Pointer castTransform = dynamic_cast<CastTransformType *>(transform.GetPointer());

    if (castTransform.IsNotNull())
    {
      matrix = castTransform->GetMatrix();
      done = true;
    }
  }

  // Translation
  if (!done)
  {
    using CastTransformType = itk::TranslationTransform<ScalarType, ImageDimension>;
    typename CastTransformType::Pointer castTransform = dynamic_cast<CastTransformType *>(transform.GetPointer());

    if (castTransform.IsNotNull())
    {
      for (unsigned int i = 0; i < ImageDimension; i++)
      {
        matrix(i, i) = itk::NumericTraits<typename TTransform::ScalarType>::OneValue();
      }
      done = true;
    }
  }

  // Identity
  if (!done)
  {
    using CastTransformType = itk::IdentityTransform<ScalarType, ImageDimension>;
    typename CastTransformType::Pointer castTransform = dynamic_cast<CastTransformType *>(transform.GetPointer());

    if (castTransform.IsNotNull())
    {
      for (unsigned int i = 0; i < ImageDimension; i++)
      {
        matrix(i, i) = itk::NumericTraits<typename TTransform::ScalarType>::OneValue();
      }
      done = true;
    }
  }

  if (!done)
  {
    // Unsupported transform type
    return false;
  }

  if (outputRAS)
  {
    // Convert to RAS coordinate system. ITK uses LPS.
    // x and y dimensions are flipped.
    // This code is from c3d app.
    vnl_vector<ScalarType> v_lps_to_ras(ImageDimension, 1.0);
    v_lps_to_ras[0] = -1.0;
    if (ImageDimension > 1)
    {
      v_lps_to_ras[1] = -1.0;
    }
    vnl_diag_matrix<ScalarType> m_lps_to_ras(v_lps_to_ras);
    vnl_matrix<ScalarType>      mold = matrix.GetVnlMatrix().as_matrix();
    matrix.GetVnlMatrix().update(m_lps_to_ras * mold * m_lps_to_ras);
  }

  return true;
}

/*
 *
 */
template <typename TTransform, typename TMatrix>
bool
GetHomogeneousMatrix(const typename TTransform::Pointer & transform, TMatrix & hMatrix, bool outputRAS)
{
  const unsigned int ImageDimension = TTransform::InputSpaceDimension;

  using ScalarType = typename TTransform::ScalarType;

  hMatrix.Fill(itk::NumericTraits<ScalarType>::ZeroValue());

  bool done = false;

  // Get the NxN matrix
  typename TTransform::MatrixType matrix;
  if (!GetMatrix<TTransform>(transform, matrix, outputRAS))
  {
    return false;
  }
  for (unsigned int i = 0; i < ImageDimension; i++)
  {
    for (unsigned int j = 0; j < ImageDimension; j++)
    {
      hMatrix(i, j) = matrix(i, j);
    }
  }

  // Set the lower-right corner to 1
  unsigned int corner = ImageDimension;
  hMatrix(corner, corner) = itk::NumericTraits<typename TTransform::ScalarType>::OneValue();

  //
  // Get the offset
  //

  // Identity
  {
    using CastTransformType = itk::IdentityTransform<ScalarType, ImageDimension>;
    typename CastTransformType::Pointer castTransform = dynamic_cast<CastTransformType *>(transform.GetPointer());

    if (castTransform.IsNotNull())
    {
      // Nothing more to do here.
      return true;
    }
  }

  typename TTransform::OutputVectorType offset;
  offset.Fill(itk::NumericTraits<typename TTransform::ScalarType>::ZeroValue());

  // Matrix-offset derived
  {
    using CastTransformType = itk::MatrixOffsetTransformBase<ScalarType, ImageDimension, ImageDimension>;
    typename CastTransformType::Pointer castTransform = dynamic_cast<CastTransformType *>(transform.GetPointer());

    if (castTransform.IsNotNull())
    {
      offset = castTransform->GetOffset();
      done = true;
    }
  }

  // Translation
  if (!done)
  {
    using CastTransformType = itk::TranslationTransform<ScalarType, ImageDimension>;
    typename CastTransformType::Pointer castTransform = dynamic_cast<CastTransformType *>(transform.GetPointer());

    if (castTransform.IsNotNull())
    {
      offset = castTransform->GetOffset();
      done = true;
    }
  }

  if (!done)
  {
    // Unsupported transform type
    return false;
  }

  if (outputRAS)
  {
    // Convert to RAS coordinate system. ITK uses LPS.
    // x and y dimensions are flipped.
    // This code is from c3d app.
    offset[0] *= -1.0;
    if (ImageDimension > 1)
    {
      offset[1] *= -1.0;
    }
  }
  for (unsigned int i = 0; i < ImageDimension; i++)
  {
    hMatrix(i, ImageDimension) = offset[i];
  }

  return true;
}

/*
 *
 */
template <unsigned int ImageDimension>
int
ConvertTransformFile(int argc, char * argv[])
{
  int  inputFilenamePos = 2;
  int  outFilenamePos = 3;
  bool outputMatrix = false;
  bool outputHomogeneousMatrix = false;
  bool outputAffine = false;
  bool outputRAS = false;

  // User options
  if (argc > 4)
  {
    for (int n = 4; n < argc; n++)
    {
      if (strcmp(argv[n], "--matrix") == 0 || strcmp(argv[n], "-m") == 0)
      {
        // User has requested outputting matrix information only.
        outputMatrix = true;
      }
      else if (strcmp(argv[n], "--homogeneousMatrix") == 0 || strcmp(argv[n], "--hm") == 0)
      {
        // User has requested outputting homogeneous matrix information only.
        outputHomogeneousMatrix = true;
      }
      else if (strcmp(argv[n], "--convertToAffineType") == 0)
      {
        outputAffine = true;
      }
      else if (strcmp(argv[n], "--RAS") == 0 || strcmp(argv[n], "--ras") == 0)
      {
        outputRAS = true;
      }
      else
      {
        std::cout << "Unrecognized option: " << argv[n] << std::endl;
        return EXIT_FAILURE;
      }
    }
    if (outputRAS && !outputMatrix && !outputHomogeneousMatrix)
    {
      std::cout << " '--RAS' option must be used with either of 'matrix' or 'homongeneousMatrix' options." << std::endl;
      return EXIT_FAILURE;
    }
    if ((outputMatrix && outputHomogeneousMatrix) || (outputMatrix && outputAffine) ||
        (outputHomogeneousMatrix && outputAffine))
    {
      std::cout << "Only one primary output option allowed at once." << std::endl;
      return EXIT_FAILURE;
    }
  }

  // Check the filename
  std::string inputFilename = std::string(argv[inputFilenamePos]);
  if (!FileExists(inputFilename))
  {
    std::cout << " file " << inputFilename << " does not exist . " << std::endl;
    return EXIT_FAILURE;
  }

  // Get the output filename
  std::string outFilename = std::string(argv[outFilenamePos]);

  // Read the transform
  using TransformType = itk::Transform<double, ImageDimension, ImageDimension>;
  typename TransformType::Pointer transform;
  using baseTransformType = itk::MatrixOffsetTransformBase<double, ImageDimension, ImageDimension>;
  itk::TransformFactory<baseTransformType>::RegisterTransform();
  transform = itk::ants::ReadTransform<double, ImageDimension>(inputFilename);
  if (transform.IsNull())
  {
    std::cout << "Error while reading transform file. Did you specify the correct dimension?" << std::endl;
    return EXIT_FAILURE;
  }

  //
  // Outputs
  //
  if (outputMatrix || outputHomogeneousMatrix)
  {
    std::ofstream outputStream;
    outputStream.open(outFilename.c_str(), std::ios::out);
    if (outputStream.fail())
    {
      outputStream.close();
      std::cout << "Failed opening the output file " << outFilename << std::endl;
      return EXIT_FAILURE;
    }

    if (outputMatrix)
    {
      using MatrixType = itk::Matrix<typename TransformType::ScalarType, ImageDimension, ImageDimension>;
      MatrixType matrix;
      if (GetMatrix<TransformType>(transform, matrix, outputRAS))
      {
        outputStream << matrix;
      }
      else
      {
        std::cout << "Error. Transform type is unsupported for getting matrix: " << transform->GetNameOfClass()
                  << std::endl;
        return EXIT_FAILURE;
      }
    }
    else
    {
      // Homogeneous matrix
      using MatrixType = itk::Matrix<typename TransformType::ScalarType, ImageDimension + 1, ImageDimension + 1>;
      MatrixType hMatrix;

      if (GetHomogeneousMatrix<TransformType, MatrixType>(transform, hMatrix, outputRAS))
      {
        outputStream << hMatrix;
      }
      else
      {
        std::cout << "Error. Transform type is unsupported for getting matrix: " << transform->GetNameOfClass()
                  << std::endl;
        return EXIT_FAILURE;
      }
    }
    outputStream.close();
    return EXIT_SUCCESS;
  }

  if (outputAffine)
  {
    // Convert to Affine and output as binary.
    // This is done by taking the matrix and offset from the transform
    // and assigning them to a new affine transform.
    using CastTransformType =
      itk::MatrixOffsetTransformBase<typename TransformType::ScalarType, ImageDimension, ImageDimension>;
    typename CastTransformType::Pointer matrixOffsetTransform =
      dynamic_cast<CastTransformType *>(transform.GetPointer());
    if (matrixOffsetTransform.IsNull())
    {
      std::cout
        << "The transform read from file is not derived from MatrixOffsetTransformBase. Cannot convert to Affine."
        << std::endl;
      return EXIT_FAILURE;
    }
    if (itksys::SystemTools::GetFilenameLastExtension(outFilename) != ".mat")
    {
      std::cout << "Output filename '" << outFilename << "' must end in '.mat' for binary output." << std::endl;
      return EXIT_FAILURE;
    }
    using AffineTransformType = itk::AffineTransform<typename TransformType::ScalarType, ImageDimension>;
    typename AffineTransformType::Pointer newAffineTransform = AffineTransformType::New();
    newAffineTransform->SetMatrix(matrixOffsetTransform->GetMatrix());
    newAffineTransform->SetOffset(matrixOffsetTransform->GetOffset());
    transform = dynamic_cast<TransformType *>(newAffineTransform.GetPointer());
    if (transform.IsNull())
    {
      std::cout << "Unexpected error casting from affine transform to transform type." << std::endl;
      return EXIT_FAILURE;
    }
    int result = itk::ants::WriteTransform<double, ImageDimension>(transform, outFilename);
    if (result == EXIT_FAILURE)
    {
      std::cout << "Failed writing converted transform to binary format." << std::endl;
      return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
  }

  // Default behavior.
  // Write it out as a text file using the legacy txt transform format
  if (itksys::SystemTools::GetFilenameLastExtension(outFilename) != ".txt" &&
      itksys::SystemTools::GetFilenameLastExtension(outFilename) != ".tfm")
  {
    std::cout << "Output filename '" << outFilename << "' must end in '.txt' or '.tfm' for text-format output."
              << std::endl;
    return EXIT_FAILURE;
  }
  int result = itk::ants::WriteTransform<double, ImageDimension>(transform, outFilename);
  if (result == EXIT_FAILURE)
  {
    std::cout << "Failed writing transform to text format." << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

// entry point for the library; parameter 'args' is equivalent to 'argv' in (argc,argv) of commandline parameters to
// 'main()'
/*
 *
 */
int
ConvertTransformFile(std::vector<std::string> args, std::ostream * /*out_stream = nullptr */)
{
  // put the arguments coming in as 'args' into standard (argc,argv) format;
  // 'args' doesn't have the command name as first, argument, so add it manually;
  // 'args' may have adjacent arguments concatenated into one argument,
  // which the parser should handle
  args.insert(args.begin(), "ConvertTransformFile");

  int     argc = args.size();
  char ** argv = new char *[args.size() + 1];
  for (unsigned int i = 0; i < args.size(); ++i)
  {
    // allocate space for the string plus a null character
    argv[i] = new char[args[i].length() + 1];
    std::strncpy(argv[i], args[i].c_str(), args[i].length());
    // place the null character in the end
    argv[i][args[i].length()] = '\0';
  }
  argv[argc] = nullptr;
  // class to automatically cleanup argv upon destruction
  class Cleanup_argv
  {
  public:
    Cleanup_argv(char ** argv_, int argc_plus_one_)
      : argv(argv_)
      , argc_plus_one(argc_plus_one_)
    {}

    ~Cleanup_argv()
    {
      for (unsigned int i = 0; i < argc_plus_one; ++i)
      {
        delete[] argv[i];
      }
      delete[] argv;
    }

  private:
    char **      argv;
    unsigned int argc_plus_one;
  };
  Cleanup_argv cleanup_argv(argv, argc + 1);

  // antscout->set_stream( out_stream );

  if (argc < 4 || (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0))
  {
    std::cout << "USAGE:  " << std::endl
              << " " << argv[0] << " dimensions inputTransformFile.ext outputTransformFile.ext [OPTIONS]" << std::endl
              << std::endl;
    std::cout << "COMMAND: " << std::endl
              << " Utility to read in a transform file (presumed to be in binary format) " << std::endl
              << " and output it in various formats. Default output is legacy human-readable" << std::endl
              << " text format.  Without any options, the output filename extension must be " << std::endl
              << " .txt or .tfm to signify a text-formatted transform file. " << std::endl
              << std::endl
              << " OPTIONS: " << std::endl
              << std::endl
              << " --matrix, -m " << std::endl
              << "   Output only the transform matrix (from transform::GetMatrix() )" << std::endl
              << "   to a text file, one row per line with space-delimited values. " << std::endl
              << "   Only works for transforms of type identity, translation or " << std::endl
              << "   MatrixOffsetTransformBase and its derived types." << std::endl
              << "   The output filename must end in '.mat'." << std::endl
              << std::endl
              << " --homogeneousMatrix, --hm" << std::endl
              << "   Output an N+1 square homogeneous matrix from the transform matrix and offset." << std::endl
              << "   Only works for transforms of type identity, translation or " << std::endl
              << "   MatrixOffsetTransformBase and its derived types." << std::endl
              << "   The output filename must end in '.mat'." << std::endl
              << std::endl
              << " --RAS, --ras" << std::endl
              << "   Combined with the 'matrix' or 'homogeneousMatrix' options, this will convert" << std::endl
              << "   the output into the RAS coordinate system (Right, Anterior, Superior)." << std::endl
              << "   Otherwise, the output is in the LPS coordinate system (Left, Posterior," << std::endl
              << "   Superior), which is used by ITK. RAS is used, for example, by Slicer. " << std::endl
              << std::endl
              << " --convertToAffineType" << std::endl
              << "   Convert the input transform type to AffineTransform using the transform's " << std::endl
              << "   matrix and offset, and output again as as a binary transform file." << std::endl
              << "   This is useful for using transforms in programs" << std::endl
              << "   that do not register all available Transform factory types." << std::endl
              << std::endl;
    if (argc < 4)
    {
      return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
  }
  if (argc > 6)
  {
    std::cout << "Too many arguments." << std::endl;
    return EXIT_FAILURE;
  }

  // Get the image dimension
  unsigned int dimension = std::stoi(argv[1]);

  switch (dimension)
  {
    case 1:
    {
      return ConvertTransformFile<1>(argc, argv);
    }
    break;
    case 2:
    {
      return ConvertTransformFile<2>(argc, argv);
    }
    break;
    case 3:
    {
      return ConvertTransformFile<3>(argc, argv);
    }
    break;
    case 4:
    {
      return ConvertTransformFile<4>(argc, argv);
    }
    break;
    default:
      std::cout << "Unsupported dimension " << dimension << "." << std::endl;
      return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}
} // namespace ants