File: igstkTrackerConfigurationXMLFileReaderBase.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 (306 lines) | stat: -rw-r--r-- 9,379 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*=========================================================================

  Program:   Image Guided Surgery Software Toolkit
  Module:    $RCSfile: igstkTrackerConfigurationXMLFileReaderBase.cxx,v $
  Language:  C++
  Date:      $Date: 2009-01-30 20:48:03 $
  Version:   $Revision: 1.1 $

  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 <algorithm>

#include "igstkTrackerConfigurationXMLFileReaderBase.h"
#include "igstkRigidTransformXMLFileReader.h"

namespace igstk
{

int 
TrackerConfigurationXMLFileReaderBase::CanReadFile(const char* name)
{
  //check that we have a valid file name, it is not a directory, and the 
  //file exists
  if(itksys::SystemTools::FileLength( name ) == 0 ||
     !itksys::SystemTools::FileExists( name ) ||
     itksys::SystemTools::FileIsDirectory( name ))
    {
    return 0;
    }
  return 1;
}


void 
TrackerConfigurationXMLFileReaderBase::StartElement( const char * name, 
                                                     const char **atts )
{
  this->m_CurrentTagData.clear();

  //check for the root of the xml tree
  if( itksys::SystemTools::Strucmp( name, "tracking_system" ) == 0 ) 
    {
    //there can only be one "tracking_system" per xml file
    if( HaveConfigurationData() )
      {
      throw FileFormatException(
        std::string( 
          "The tag \"tracking_system\" appears multiple times, should only appear once." 
          ) );
      }
    this->m_ReadingTrackerConfiguration = true;
    ProcessTrackingSystemAttributes( atts );
    }
  else if( itksys::SystemTools::Strucmp( name, "refresh_rate" ) == 0 ) 
    {
    if( !m_ReadingTrackerConfiguration )
      {
      throw FileFormatException( 
        "\"refresh_rate\" tag not nested in \"tracking_system\" tag." );
      }
    }
  else if( itksys::SystemTools::Strucmp( name, "tool" ) == 0 )
    {
    if( !m_ReadingTrackerConfiguration )
      {
      throw FileFormatException( "Tag not nested correctly in xml structure." );
      }
    if( m_ReadingToolConfiguration )
      {
      throw FileFormatException( "Self nested \"tool\" tag not allowed." );
      }
    else
      {
      this->m_ReadingToolConfiguration = true;
      ProcessToolAttributes( atts );
      }
    }
  else if( itksys::SystemTools::Strucmp( name, "name" ) == 0 ||
           itksys::SystemTools::Strucmp( name, "calibration_file" ) == 0 )
    {
    if( !m_ReadingToolConfiguration )
      {
      throw FileFormatException( "Tag not nested correctly in xml structure." );
      }
    }
}


void 
TrackerConfigurationXMLFileReaderBase::EndElement( const char *name )
{
  //trim the tag's data string and check that it
  //isn't empty (empty tags are not allowed)
  size_t startpos = this->m_CurrentTagData.find_first_not_of(" \t\n\r");
  size_t endpos = this->m_CurrentTagData.find_last_not_of(" \t\n\r");   
  if( startpos == std::string::npos || endpos == std::string::npos )
    {
    this->m_CurrentTagData.clear();
    }
  else
    {
    this->m_CurrentTagData = this->m_CurrentTagData.substr( startpos, 
                                                            endpos-startpos+1 );
    }
  //replace all newlines ('\n') and carriage returns ('\r') with space
  std::replace( this->m_CurrentTagData.begin(), 
                this->m_CurrentTagData.end(), 
                '\n', ' ' );
  std::replace( this->m_CurrentTagData.begin(), 
                this->m_CurrentTagData.end(), 
                '\r', ' ' );
                    
  if( this->m_CurrentTagData.empty() )
    {
    std::ostringstream outStr; 
    outStr<<"Empty tag ("<<name<<") not allowed in tracker settings";
    throw FileFormatException( outStr.str() );
    }

  if( itksys::SystemTools::Strucmp( name, "tracking_system" ) == 0 )
    {
    this->m_ReadingTrackerConfiguration = false;
    //see that we got all the data, perhaps replace with a more
    //specific message with regard to the missing tag(s)
    if( !HaveConfigurationData() )
      {
      throw FileFormatException( std::string( "Missing tag(s) in xml file." ) );
      }
    }
  if( m_ReadingTrackerConfiguration &&
      itksys::SystemTools::Strucmp( name, "refresh_rate" ) == 0 )
    {
    ProcessRefreshRate();
    this->m_HaveRefreshRate = true;
    }
  if( m_ReadingToolConfiguration &&
      itksys::SystemTools::Strucmp( name, "name" ) == 0 )
    {
    ProcessToolName();
    }
  if( m_ReadingToolConfiguration &&
      itksys::SystemTools::Strucmp( name, "calibration_file" ) == 0 )
    {
    ProcessToolCalibration();
    }
 if( itksys::SystemTools::Strucmp( name, "tool" ) == 0 )
   {
   this->m_ReadingToolConfiguration = false;
   ProcessToolData();
   }
}


void 
TrackerConfigurationXMLFileReaderBase::CharacterDataHandler( 
const char *inData, int inLength )
{
  this->m_CurrentTagData.append( inData, inLength );
}


void 
TrackerConfigurationXMLFileReaderBase::ProcessTrackingSystemAttributes( 
  const char **atts ) throw ( 
    FileFormatException, UnexpectedTrackerTypeException )
{
  bool systemTypeFound = false;

  //go over all the attribute-value pairs
  for( int i=0; atts[i] != NULL; i+=2 ) 
    {
    if( itksys::SystemTools::Strucmp( atts[i], "type" ) == 0 )
      {
      if( itksys::SystemTools::Strucmp( atts[i+1], 
            GetSystemType().c_str() ) != 0 )
        {
        throw UnexpectedTrackerTypeException();
        }
      else
        {
        systemTypeFound = true;
        }
      }
    }
  if( !systemTypeFound )
    {
    throw FileFormatException( 
      "\"type\" attribute missing in tracking_system tag." );
    }
}


void 
TrackerConfigurationXMLFileReaderBase::ProcessRefreshRate() 
throw ( FileFormatException )
{
  double refreshRate;
  std::istringstream instr;  
  instr.str( this->m_CurrentTagData );
  instr>>refreshRate;
  //check that we got to the end of the stream, (the string 
  //m_CurrentTagData has no trailing white spaces, trimmed in the 
  //EndElement method)
  if( !instr.eof() )
    {
    throw FileFormatException( 
      "Error in refresh rate data, possibly non numeric values" );
    }

  if( refreshRate<=0 || refreshRate>GetMaximalRefreshRate() )
    {
    std::ostringstream msg;
    msg.str("");
    msg<<"Invalid tracker frequency specified ("<<refreshRate<<").";
    msg<< " Valid values are in [0,"<<GetMaximalRefreshRate()<<"].";
    throw FileFormatException( msg.str() );
    }
  this->m_RefreshRate = refreshRate;
}


void 
TrackerConfigurationXMLFileReaderBase::ProcessToolAttributes( 
const char **atts ) throw ( FileFormatException )
{
  //by default the tool isn't a reference
  this->m_CurrentToolIsReference = false;

  //go over all the attribute-value pairs
  for( int i=0; atts[i] != NULL; i+=2 ) 
    {
    if( itksys::SystemTools::Strucmp( atts[i], "usage" ) == 0 &&
        itksys::SystemTools::Strucmp( atts[i+1], "reference" ) == 0 )
        this->m_CurrentToolIsReference = true;
    }
  if( this->m_CurrentToolIsReference && this->m_ReferenceTool != NULL )
    {
    throw FileFormatException( "Multiple tools defined as reference." );
    }
}


void 
TrackerConfigurationXMLFileReaderBase::ProcessToolName() 
throw ( FileFormatException )
{
  this->m_CurrentToolName = this->m_CurrentTagData;
}


void 
TrackerConfigurationXMLFileReaderBase::ProcessToolCalibration() 
throw ( FileFormatException )
{
  std::ostringstream msg;

  //check that the calibration file name is valid
  if(itksys::SystemTools::FileLength( this->m_CurrentTagData.c_str() ) == 0 ||
    !itksys::SystemTools::FileExists( this->m_CurrentTagData.c_str() ) ||
    itksys::SystemTools::FileIsDirectory( this->m_CurrentTagData.c_str() ) )
    {
    msg<<"Calibration file ("<<this->m_CurrentTagData;
    msg<<") does not exist or is a directory";
    throw FileFormatException( msg.str() );
    }
  //try to read the file
  igstk::RigidTransformXMLFileReader::Pointer xmlReader = 
    igstk::RigidTransformXMLFileReader::New();

  xmlReader->SetFilename( this->m_CurrentTagData );
  //may cause exceptions which the user should take care of
  xmlReader->GenerateOutputInformation();
  //xml reading is successful (no exception generation) even 
  //when the file is empty, need to check that the data was
  //actually read
  if( !xmlReader->HaveTransformData() )
    {
    msg<<"Calibration file ("<<this->m_CurrentTagData;
    msg<<") doesn't contain transformation data";  
    throw FileFormatException( msg.str() );
    }
  igstk::PrecomputedTransformData::Pointer calibrationData = 
    xmlReader->GetTransformData();

  RequestTransformObserver::Pointer transformObserver = 
    RequestTransformObserver::New();
  calibrationData->AddObserver( 
    igstk::PrecomputedTransformData::TransformTypeEvent(), 
                                transformObserver );
  calibrationData->RequestTransform();
  igstk::Transform *transform = 
    static_cast<igstk::Transform *>(transformObserver->GetRequestTransform());

  //load the tools calibration data
  this->m_CurrentToolCalibration = *transform;
}

} //namespace