Shapeworks Studio  2.1
Shape analysis software suite
psmCommandLineTool.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  * or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  *=========================================================================*/
17 
18 #include <iostream>
19 #include "itkPSMCommandLineClass.h"
20 #include "itkPSMProjectReader.h"
21 #include "itkImage.h"
22 #include "itkImageFileReader.h"
23 #include "itkExceptionObject.h"
24 #include "itkGDCMImageIOFactory.h"
25 #include "itkMetaImageIOFactory.h"
26 #include "itkObjectFactoryBase.h"
27 #include "itkNrrdImageIOFactory.h"
28 #include "itkExceptionObject.h"
29 
30 
31 int main(int argc, char *argv[])
32 {
33  std::string output_path = "";
34  std::string input_path_prefix = "";
35  std::string errstring = "";
36  // Check for proper arguments
37  if (argc < 2)
38  {
39  std::cout << "Wrong number of arguments. \nUse: "
40  << "psmCommandLineTool parameter_file [output_path] [input_path]\n"
41  << "See itk::PSMParameterFileReader for documentation on the parameter file format.\n"
42  << "Note that input_path will be prefixed to any file names and paths in the xml parameter file.\n"
43  << std::endl;
44  return EXIT_FAILURE;
45  }
46 
47  if (argc > 2)
48  {
49  output_path = std::string(argv[2]);
50  }
51 
52  if (argc > 3)
53  {
54  input_path_prefix = std::string(argv[3]);
55  }
56 
57  try
58  {
59  // The following are called to fix an ITK runtime error where image format is not recognized.
60  itk::ObjectFactoryBase::RegisterFactory(itk::MetaImageIOFactory::New());
61  itk::ObjectFactoryBase::RegisterFactory(itk::GDCMImageIOFactory::New());
62  itk::ObjectFactoryBase::RegisterFactory(itk::NrrdImageIOFactory::New());
63 
64  // Read and parse the project file
65  // itk::PSMProjectReader::Pointer xmlReader = itk::PSMProjectReader::New();
66  // xmlReader->SetFileName(argv[1]);
67  // xmlReader->Update();
68  // itk::PSMProject::Pointer project = xmlReader->GetOutput();
69 
70  // The dimensions of the input images need to be checked in order to
71  // correctly initialize PSMCommandLineClass.
72  // const std::vector<std::string> &dt_files = project->GetDistanceTransforms();
73  // std::string fname = input_path_prefix + dt_files[0];
74 
75  // 1. First try to read the file. This is just a convenient way to
76  // check whether the file is valid or not. An exception will be
77  // thrown by ITK otherwise. This is slow, but an easy solution.
78  // itk::ImageFileReader<itk::Image<unsigned int, 3>>::Pointer
79  // testreader = itk::ImageFileReader<itk::Image<unsigned int, 3>>::New();
80  // testreader->SetFileName(fname);
81  // testreader->Update();
82 
83  // 2. If the file was read successfully, we now just probe the image
84  // information. Note that ITK does not provide a method under
85  // ImageIOBase to actually check the validity of a file, so the
86  // following is dangerous unless you know that the file actually
87  // exists.
88  // std::cout << "Checking input image dimensions of " << fname << std::endl;
89  // typedef itk::ImageIOBase::IOComponentType ScalarPixelType;
90  // itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(
91  // fname.c_str(), itk::ImageIOFactory::ReadMode);
92  // imageIO->SetFileName(fname);
93 
94  // imageIO->ReadImageInformation();
95  // const size_t numOfDimensions = imageIO->GetNumberOfDimensions();
96  // std::cout << "Number of dimensions: " << numOfDimensions << std::endl;
97  const size_t numOfDimensions = 3;
98  if (numOfDimensions == 2)
99  {
100  std::cout << "Running 2D optimization code" << std::endl;
101  itk::PSMCommandLineClass<2>::Pointer psmClass
103  psmClass->Run(argv[1], input_path_prefix, output_path);
104  } else if (numOfDimensions == 3)
105  {
106  std::cout << "Running 3D optimization code" << std::endl;
107  itk::PSMCommandLineClass<3>::Pointer psmClass
109  psmClass->Run(argv[1], input_path_prefix, output_path);
110  }
111  }
112 
113  catch (itk::ExceptionObject &e)
114  {
115  std::cerr << "ITK exception with description: " << e.GetDescription()
116  << "\n at location:" << e.GetLocation()
117  << "\n in file:" << e.GetFile() << std::endl;
118  return EXIT_FAILURE;
119  }
120 
121  catch (...)
122  {
123  errstring = "Unknown exception thrown";
124  return EXIT_FAILURE;
125  }
126 
127  return EXIT_SUCCESS;
128 }
void Run(const char *fname, std::string input_path_prefix, std::string output_path)
This class provides a command line tool to run the Particle Shape Modeling. It runs the optimization ...