File: igstkMicronConfigurationXMLFileReader.cxx

package info (click to toggle)
igstk 4.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 38,980 kB
  • sloc: cpp: 86,267; xml: 96; makefile: 75; python: 38
file content (242 lines) | stat: -rw-r--r-- 7,388 bytes parent folder | download | duplicates (2)
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
/*=========================================================================

  Program:   Image Guided Surgery Software Toolkit
  Module:    $RCSfile: igstkMicronConfigurationXMLFileReader.cxx,v $
  Language:  C++
  Date:      $Date: 2009-02-06 01:48:11 $
  Version:   $Revision: 1.2 $

  Copyright (c) ISC  Insight Software Consortium.  All rights reserved.
  See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm 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 <itksys/SystemTools.hxx>

#include "igstkMicronConfigurationXMLFileReader.h"
#include "igstkMicronTrackerConfiguration.h"

namespace igstk
{


void 
MicronConfigurationXMLFileReader::StartElement( 
  const char * name, 
  const char **atts )
{
  //let the super class try to analyze the current tag
  Superclass::StartElement( name, atts );
  if( itksys::SystemTools::Strucmp( name, 
        "camera_calibration_directory" ) == 0 ||
      itksys::SystemTools::Strucmp( name, "initialization_file" ) == 0 ||
      itksys::SystemTools::Strucmp( name, "templates_directory" ) == 0 ||
      itksys::SystemTools::Strucmp( name, "tool" ) == 0 ) 
    {
    if( !m_ReadingTrackerConfiguration )
      throw FileFormatException( "Tag not nested correctly in xml structure." );
    }  
}


void 
MicronConfigurationXMLFileReader::EndElement( 
  const char *name )
{
  //let the super class try to analyze the current tag
  Superclass::EndElement( name );
  
         
  if( m_ReadingTrackerConfiguration &&
      itksys::SystemTools::Strucmp( name, "camera_calibration_directory" ) == 0 )
    {
    ProcessMicronCalibrationDirectory();
    }
  else if( m_ReadingTrackerConfiguration &&
           itksys::SystemTools::Strucmp( name, "initialization_file" ) == 0 )
    {
    ProcessMicronInitializationFile();
    }
  else if( m_ReadingTrackerConfiguration &&
           itksys::SystemTools::Strucmp( name, "templates_directory" ) == 0 )
    {
    ProcessMicronTemplatesDirectory();
    }
  else if( m_ReadingToolConfiguration &&
           itksys::SystemTools::Strucmp( name, "marker_name" ) == 0 )
    {
    ProcessMarkerName();
    }
}


std::string 
MicronConfigurationXMLFileReader::GetSystemType()
{
  return "micron";
}


double 
MicronConfigurationXMLFileReader::GetMaximalRefreshRate()
{
  igstk::MicronTrackerConfiguration::Pointer trackerConfig =
    igstk::MicronTrackerConfiguration::New();

  return trackerConfig->GetMaximalRefreshRate();
}


void 
MicronConfigurationXMLFileReader::ProcessMicronCalibrationDirectory() 
throw ( FileFormatException )
{
  if( !itksys::SystemTools::FileExists( this->m_CurrentTagData.c_str() ) ||
    !itksys::SystemTools::FileIsDirectory( this->m_CurrentTagData.c_str()  ) )
    {
    throw FileFormatException( 
      "Invalid string given as camera_calibration_directory tag" );
    }
  this->m_MicronCalibrationDirectory = this->m_CurrentTagData;
}


void 
MicronConfigurationXMLFileReader::ProcessMicronInitializationFile() 
throw ( FileFormatException )
{
  if( !itksys::SystemTools::FileExists( this->m_CurrentTagData.c_str() ) ||
      itksys::SystemTools::FileIsDirectory( this->m_CurrentTagData.c_str()  ) )
    {
    throw FileFormatException( 
      "Invalid string given as initialization_file tag" );
    }
  this->m_MicronInitializationFile = this->m_CurrentTagData;
}


void 
MicronConfigurationXMLFileReader::ProcessMicronTemplatesDirectory() 
throw ( FileFormatException )
{
  if( !itksys::SystemTools::FileExists( this->m_CurrentTagData.c_str() ) ||
      !itksys::SystemTools::FileIsDirectory( this->m_CurrentTagData.c_str()  ) )
    {
    throw FileFormatException( 
      "Invalid string given as templates_directory tag" );
    }
  this->m_MicronTemplatesDirectory = this->m_CurrentTagData;
}

void 
MicronConfigurationXMLFileReader::ProcessMarkerName() 
throw ( FileFormatException )
{
  this->m_CurrentMarkerName = this->m_CurrentTagData;
}


void 
MicronConfigurationXMLFileReader::ProcessToolData() 
throw ( FileFormatException )
{
  if( this->m_CurrentToolName.empty() )
    {
    throw FileFormatException( "\"name\" missing for one of the tools." );
    }
  if( this->m_CurrentMarkerName.empty() )
    {
    throw FileFormatException( "\"marker_name\" missing for one of the tools." );
    }

  //if the tool section does not have a "calibration_file" tag 
  //we assume the calibration is identity
  igstk::MicronToolConfiguration *toolConfig = 
      new igstk::MicronToolConfiguration();
  
  toolConfig->SetToolName( this->m_CurrentToolName );
  toolConfig->SetMarkerName( this->m_CurrentMarkerName );
  toolConfig->SetCalibrationTransform( this->m_CurrentToolCalibration );  
  if( this->m_CurrentToolIsReference )
    {
    this->m_ReferenceTool = toolConfig;
    }
  else
    {
    this->m_TrackerToolList.push_back( toolConfig );
    }

  //reset all tool data to initial state
  this->m_CurrentToolName.clear();
  this->m_CurrentMarkerName.clear();
  this->m_CurrentToolCalibration.SetToIdentity( 
    igstk::TimeStamp::GetLongestPossibleTime() );
}


bool 
MicronConfigurationXMLFileReader::HaveConfigurationData()
{
  //check that we have a refresh rate for the tracking system, at least 
  //one tool
  return ( this->m_HaveRefreshRate &&
           this->m_TrackerToolList.size() != 0 &&
           !this->m_MicronCalibrationDirectory.empty() &&
           !this->m_MicronInitializationFile.empty() &&
           !this->m_MicronTemplatesDirectory.empty() );
}


const igstk::TrackerConfiguration::Pointer
MicronConfigurationXMLFileReader::GetTrackerConfigurationData()
throw ( FileFormatException )
{
  igstk::MicronTrackerConfiguration::Pointer trackerConfig = 
    igstk::MicronTrackerConfiguration::New();
    
  //this request is guaranteed to succeed as the refresh rate
  //is validated in the TrackerConfigurationXMLReaderBase
  trackerConfig->RequestSetFrequency( this->m_RefreshRate );
  
  trackerConfig->SetCameraCalibrationFileDirectory( 
    this->m_MicronCalibrationDirectory );
  trackerConfig->SetInitializationFile( this->m_MicronInitializationFile );
  trackerConfig->SetTemplatesDirectory( this->m_MicronTemplatesDirectory );

  //add the tools
  std::vector<TrackerToolConfiguration *>::iterator it, end; 
  it = this->m_TrackerToolList.begin();
  end = this->m_TrackerToolList.end();  
  
  AddToolFailureObserver::Pointer failureObserver =
    AddToolFailureObserver::New();
  trackerConfig->AddObserver( igstk::TrackerConfiguration::AddToolFailureEvent(),
                              failureObserver );
  for(; it!=end; it++ )
    {
    trackerConfig->RequestAddTool( *it );
    if( failureObserver->GotAddToolFailure() )
      {
      throw FileFormatException( failureObserver->GetAddToolFailure() );
      }
    }
  if( this->m_ReferenceTool != NULL )
    {
    trackerConfig->RequestAddReference( this->m_ReferenceTool );
    if( failureObserver->GotAddToolFailure() )
      {
      throw FileFormatException( failureObserver->GetAddToolFailure() );
      }
    }
 
  //explicitly upcast to avoid the compiler warning
  igstk::TrackerConfiguration::Pointer genericConfig;
  genericConfig  = trackerConfig;
  return genericConfig;
}

} //namespace