ANIMA  4.0
animaTransformSerieXmlGenerator.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 
3 #include <string>
4 #include <fstream>
5 
6 #include <itkTransformFileReader.h>
7 #include <itkImageFileReader.h>
8 
9 int main(int ac, const char **av)
10 {
11  TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
12 
13  TCLAP::MultiArg<std::string> inputArg("i", "input", "multiple input transform names",true,"input files",cmd);
14  TCLAP::MultiArg<unsigned int> invertArg("I", "invert", "multiple invert transform arguments",false,"invert inputs",cmd);
15 
16  TCLAP::ValueArg<std::string> outputArg("o","output","output xml filename",true,"","output filename",cmd);
17 
18  TCLAP::SwitchArg denseArg("D","dense","Non linear transfroms are dense fields (default: they are SVFs)",cmd,false);
19 
20  try
21  {
22  cmd.parse(ac,av);
23  }
24  catch (TCLAP::ArgException& e)
25  {
26  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
27  return EXIT_FAILURE;
28  }
29 
30  std::vector <std::string> inputNames = inputArg.getValue();
31  std::vector <unsigned int> inputInverts = invertArg.getValue();
32 
33  bool useInvert = (inputInverts.size() == inputNames.size());
34 
35  std::ofstream outputFile(outputArg.getValue().c_str());
36  outputFile << "<?xml version=\"1.0\"?>" << std::endl;
37  outputFile << "<TransformationList>" << std::endl;
38 
39  for (unsigned int i = 0;i < inputNames.size();++i)
40  {
41  // Try first to read as a linear transform
42  bool linearSuccess = true;
43  try
44  {
45  itk::TransformFileReader::Pointer trReader = itk::TransformFileReader::New();
46  trReader->SetFileName(inputNames[i]);
47 
48  trReader->Update();
49  }
50  catch (itk::ExceptionObject &e)
51  {
52  linearSuccess = false;
53  }
54 
55  if (linearSuccess)
56  {
57  outputFile << "<Transformation>" << std::endl;
58  outputFile << "<Type>linear</Type>" << std::endl;
59 
60  if (useInvert)
61  outputFile << "<Inversion>" << inputInverts[i] << "</Inversion>" << std::endl;
62  else
63  outputFile << "<Inversion>0</Inversion>" << std::endl;
64 
65  outputFile << "<Path>" << inputNames[i] << "</Path>" << std::endl;
66  outputFile << "</Transformation>" << std::endl;
67 
68  continue;
69  }
70 
71  // Then try non linear transform
72  bool nonLinearSuccess = true;
73  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(inputNames[i].c_str(), itk::ImageIOFactory::ReadMode);
74 
75  if (!imageIO)
76  nonLinearSuccess = false;
77 
78  if (nonLinearSuccess)
79  {
80  outputFile << "<Transformation>" << std::endl;
81 
82  if (denseArg.isSet())
83  outputFile << "<Type>dense</Type>" << std::endl;
84  else
85  outputFile << "<Type>svf</Type>" << std::endl;
86 
87  if (useInvert)
88  outputFile << "<Inversion>" << inputInverts[i] << "</Inversion>" << std::endl;
89  else
90  outputFile << "<Inversion>0</Inversion>" << std::endl;
91 
92  outputFile << "<Path>" << inputNames[i] << "</Path>" << std::endl;
93  outputFile << "</Transformation>" << std::endl;
94 
95  continue;
96  }
97 
98  // else error and quit
99  std::cerr << inputNames[i] << " is not a readable transform, exiting" << std::endl;
100  return 1;
101  }
102 
103  outputFile << "</TransformationList>" << std::endl;
104  outputFile.close();
105 
106  std::cout << "Generated an XML file " << outputArg.getValue() << " with " << inputNames.size() << " transformations..." << std::endl;
107 
108  return 0;
109 }
int main(int ac, const char **av)