Shapeworks Studio  2.1
Shape analysis software suite
itkPSMProjectTest.cxx
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #include <iostream>
19 
20 #include "itkMacro.h"
21 #include "itkPSMProject.h"
22 #include "itkPSMProjectReader.h"
23 #include "itkPSMProjectWriter.h"
24 
26 int itkPSMProjectTest(int argc, char* argv[] )
27 {
28  bool passed = true;
29  std::string errstring = "";
30 
31  // Check for proper arguments
32  if (argc < 2)
33  {
34  std::cout << "Wrong number of arguments. \nUse: "
35  << "itkPSMProjectTest PSMProjectFile.xml <outputfile.xml (optional>\n"
36  << "See itk::PSMProject and itk::PSMDOMNode for documentation on the parameter file format."
37  << std::endl;
38  return EXIT_FAILURE;
39  }
40 
41  try
42  {
43  // Try to read a valid PSM Project XML file
44  itk::PSMProjectReader::Pointer reader = itk::PSMProjectReader::New();
45  reader->SetFileName(argv[1]);
46  reader->Update();
47  itk::PSMProject::Pointer project = reader->GetOutput();
48 
49  // Print out the project
50  std::cout << project << std::endl;
51 
52  // Stream the project to std::out
53  project->Stream(std::cout);
54 
55  // Can we retrieve all of the appropriate fields?
56  // Distance transforms ...
57  const std::vector<std::string> &dt = project->GetDistanceTransforms();
58  std::cout << itk::PSMProject::distance_transforms_tag << std::endl;
59  for (unsigned int i = 0; i < dt.size(); i++)
60  {
61  std::cout << " " << dt[i] << std::endl;
62  }
63 
64  // Models
65  const std::vector<std::string> &model = project->GetModel(std::string("my_model"));
66  std::cout << itk::PSMProject::model_tag << std::endl;
67  for (unsigned int i = 0; i < model.size(); i++)
68  {
69  std::cout << " " << model[i] << std::endl;
70  }
71 
72  // project->GetSegmentations();
73  // project->GetCorrespondences();
74 
75  if (argc > 2) // also write an output file
76  {
77  itk::PSMProjectWriter::Pointer writer = itk::PSMProjectWriter::New();
78  writer->SetFileName(argv[2]);
79  writer->SetInput(project);
80  writer->Update();
81  }
82  }
83  catch(itk::ExceptionObject &e)
84  {
85  errstring = "ITK exception with description: " + std::string(e.GetDescription())
86  + std::string("\n at location:") + std::string(e.GetLocation())
87  + std::string("\n in file:") + std::string(e.GetFile());
88  passed = false;
89  }
90  catch(...)
91  {
92  errstring = "Unknown exception thrown";
93  passed = false;
94  }
95 
96  if (passed)
97  {
98  std::cout << "All tests passed" << std::endl;
99  return EXIT_SUCCESS;
100  }
101  else
102  {
103  std::cout << "Test failed with the following error:" << std::endl;
104  std::cout << errstring << std::endl;
105  return EXIT_FAILURE;
106  }
107 }
108