Orfeo Toolbox  3.16
itkTransformFileReader.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkTransformFileReader.cxx,v $
5  Language: C++
6  Date: $Date: 2010-02-08 20:23:21 $
7  Version: $Revision: 1.26 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkTransformFileReader_cxx
18 #define __itkTransformFileReader_cxx
19 
20 #include "itkTransformFileReader.h"
21 #include "itkTransformBase.h"
23 
24 #include <itksys/ios/fstream>
25 #include <itksys/ios/sstream>
26 
27 namespace itk
28 {
29 
30 std::string trim(std::string const& source, char const* delims = " \t\r\n") {
31  std::string result(source);
32  std::string::size_type index = result.find_last_not_of(delims);
33  if(index != std::string::npos)
34  {
35  result.erase(++index);
36  }
37 
38  index = result.find_first_not_of(delims);
39  if(index != std::string::npos)
40  {
41  result.erase(0, index);
42  }
43  else
44  {
45  result.erase();
46  }
47  return result;
48 }
49 
53 {
54  m_FileName = "";
56 }
57 
61 {
62 }
63 
67 {
68  TransformPointer transform;
69  std::ifstream in;
70  in.open ( m_FileName.c_str(), std::ios::in | std::ios::binary );
71  if( in.fail() )
72  {
73  in.close();
74  itkExceptionMacro ( "The file could not be opened for read access "
75  << std::endl << "Filename: \"" << m_FileName << "\"" );
76  }
77 
78  OStringStream InData;
79 
80  // in.get ( InData );
81  std::filebuf *pbuf;
82  pbuf=in.rdbuf();
83 
84  // get file size using buffer's members
85  int size=static_cast<int>(pbuf->pubseekoff (0,std::ios::end,std::ios::in));
86  pbuf->pubseekpos (0,std::ios::in);
87 
88  // allocate memory to contain file data
89  char* buffer=new char[size+1];
90 
91  // get file data
92  pbuf->sgetn (buffer,size);
93  buffer[size]='\0';
94  itkDebugMacro ( "Read file transform Data" );
95  InData << buffer;
96 
97  delete[] buffer;
98  std::string data = InData.str();
99  in.close();
100 
101  // Read line by line
102  vnl_vector<double> VectorBuffer;
103  std::string::size_type position = 0;
104 
105  Array<double> TmpParameterArray;
106  Array<double> TmpFixedParameterArray;
107  TmpParameterArray.clear();
108  TmpFixedParameterArray.clear();
109  bool haveFixedParameters = false;
110  bool haveParameters = false;
111 
112  //
113  // check for line end convention
114  std::string line_end("\n");
115  if(data.find('\n') == std::string::npos)
116  {
117  if(data.find('\r') == std::string::npos)
118  {
119  itkExceptionMacro ( "No line ending character found, not a valid ITK Transform TXT file" );
120  }
121  line_end = "\r";
122  }
123  while ( position < data.size() )
124  {
125  // Find the next string
126  std::string::size_type end = data.find ( line_end, position );
127  std::string line = trim ( data.substr ( position, end - position ) );
128  position = end+1;
129  itkDebugMacro ("Found line: \"" << line << "\"" );
130 
131  if ( line.length() == 0 )
132  {
133  continue;
134  }
135  if ( line[0] == '#' || std::string::npos == line.find_first_not_of ( " \t" ) )
136  {
137  // Skip lines beginning with #, or blank lines
138  continue;
139  }
140 
141  // Get the name
142  end = line.find ( ":" );
143  if ( end == std::string::npos )
144  {
145  // Throw an error
146  itkExceptionMacro ( "Tags must be delimited by :" );
147  }
148  std::string Name = trim ( line.substr ( 0, end ) );
149  std::string Value = trim ( line.substr ( end + 1, line.length() ) );
150  // Push back
151  itkDebugMacro ( "Name: \"" << Name << "\"" );
152  itkDebugMacro ( "Value: \"" << Value << "\"" );
153  itksys_ios::istringstream parse ( Value );
154  VectorBuffer.clear();
155  if ( Name == "Transform" )
156  {
157  // Instantiate the transform
158  itkDebugMacro ( "About to call ObjectFactory" );
160  i = ObjectFactoryBase::CreateInstance ( Value.c_str() );
161  itkDebugMacro ( "After call ObjectFactory");
162  TransformType* ptr = dynamic_cast<TransformBase*> ( i.GetPointer() );
163  if ( ptr == NULL )
164  {
165  OStringStream msg;
166  msg << "Could not create an instance of " << Value << std::endl
167  << "The usual cause of this error is not registering the "
168  << "transform with TransformFactory" << std::endl;
169  msg << "Currently registered Transforms: " << std::endl;
170  std::list<std::string> names = TransformFactoryBase::GetFactory()->GetClassOverrideWithNames();
171  std::list<std::string>::iterator it;
172  for ( it = names.begin(); it != names.end(); it++ )
173  {
174  msg << "\t\"" << *it << "\"" << std::endl;
175  }
176  itkExceptionMacro ( << msg.str() );
177  return;
178  }
179  transform = ptr;
180  m_TransformList.push_back ( transform );
181  }
182  else if ( Name == "Parameters" || Name == "FixedParameters" )
183  {
184  VectorBuffer.clear();
185 
186  // Read them
187  parse >> VectorBuffer;
188  itkDebugMacro ( "Parsed: " << VectorBuffer );
189  if ( Name == "Parameters" )
190  {
191  TmpParameterArray = VectorBuffer;
192  itkDebugMacro ( "Setting Parameters: " << TmpParameterArray );
193  if ( haveFixedParameters )
194  {
195  transform->SetFixedParameters ( TmpFixedParameterArray );
196  itkDebugMacro ( "Set Transform Fixed Parameters" );
197  transform->SetParametersByValue ( TmpParameterArray );
198  itkDebugMacro ( "Set Transform Parameters" );
199  TmpParameterArray.clear();
200  TmpFixedParameterArray.clear();
201  haveFixedParameters = false;
202  haveParameters = false;
203  }
204  else
205  {
206  haveParameters = true;
207  }
208  }
209  else if ( Name == "FixedParameters" )
210  {
211  TmpFixedParameterArray = VectorBuffer;
212  itkDebugMacro ( "Setting Fixed Parameters: " << TmpFixedParameterArray );
213  if ( !transform )
214  {
215  itkExceptionMacro ( "Please set the transform before parameters or fixed parameters" );
216  }
217  if ( haveParameters )
218  {
219  transform->SetFixedParameters ( TmpFixedParameterArray );
220  itkDebugMacro ( "Set Transform Fixed Parameters" );
221  transform->SetParametersByValue ( TmpParameterArray );
222  itkDebugMacro ( "Set Transform Parameters" );
223  TmpParameterArray.clear();
224  TmpFixedParameterArray.clear();
225  haveFixedParameters = false;
226  haveParameters = false;
227  }
228  else
229  {
230  haveFixedParameters = true;
231  }
232  }
233  }
234  }
235 }
236 
237 } // namespace itk
238 
239 #endif

Generated at Sun May 19 2013 00:10:44 for Orfeo Toolbox with doxygen 1.8.3.1