File: igstkTrackerConfigurationFileReader.h

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 (331 lines) | stat: -rw-r--r-- 11,569 bytes parent folder | download
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*=========================================================================

  Program:   Image Guided Surgery Software Toolkit
  Module:    $RCSfile: igstkTrackerConfigurationFileReader.h,v $
  Language:  C++
  Date:      $Date: 2010-12-14 16:31:33 $
  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.

=========================================================================*/

#ifndef __igstkTrackerConfigurationFileReader_h
#define __igstkTrackerConfigurationFileReader_h

#include "igstkStateMachine.h"
#include "igstkTrackerConfigurationXMLFileReaderBase.h"
#include "igstkTrackerConfiguration.h"
#include "igstkMacros.h"
#include "igstkObject.h"


namespace igstk
{
/** \class TrackerConfigurationFileReader
 * 
 *  \brief This class is an IGSTK wrapper for all readers of xml files 
 *         containing a tracker configuration. 
 *
 *         This class is an IGSTK wrapper for all readers of xml files 
 *         containing a tracker configuration.
 *         The specific type of tracker configuration is defined by the 
 *         actual reader specified with the RequestSetReader() method.
 *
 */
class TrackerConfigurationFileReader : public Object 
{
public:

  /**Macro with standard traits declarations (Self, SuperClass, state machine 
    * etc.). */
  igstkStandardClassTraitsMacro( TrackerConfigurationFileReader, Object )

  /** This is the container type which holds all the data associated with the 
   *  tracker configuration (e.g. COM port...).*/
   typedef const TrackerConfiguration::Pointer  TrackerConfigurationDataType;

   typedef TrackerConfigurationXMLFileReaderBase::Pointer   FileReaderBasePointerType;

  /**
   * Set a specific reader, which also defines the type of the tracker.
   * This method generates two events: If the reader is NULL 
   * SetReaderFailureEvent, otherwise SetReaderSuccessEvent.
   */
   void RequestSetReader( TrackerConfigurationXMLFileReaderBase::Pointer
                         trackerConfigurationReader );

  /**
   * Set the file name from which the data is loaded.
   * This method generates two events: If the file name is empty
   * SetFileNameFailureEvent, otherwise SetFileNameSuccessEvent.
   */
  void RequestSetFileName( const std::string &fileName );

  /**
   * Actually perform the read operation. This method generates a 
   * ReadSuccessEvent upon success and a ReadFailureEvent if reading failed. 
   * If reading fails because the xml reader doesn't match the tracker type
   * then an UnexpectedTrackerTypeEvent is also generated. If
   * the reader hasn't been initialized yet (missing file name or xml reader) 
   * an InvalidRequestErrorEvent is generated.
   */
  void RequestRead();

  /**
   * Get the tracker configuration data. This method generates a 
   * TrackerConfigurationDataEvent if the data was loaded, otherwise an 
   * InvalidRequestErrorEvent.
   */
  void RequestGetData();

  /** This event is generated when the configuration data is requested and the
   *  data is available. */
  class  TrackerConfigurationDataEvent : public IGSTKEvent 
  { 
  public:  
    typedef TrackerConfigurationDataEvent        Self; 
    typedef IGSTKEvent                           Superclass; 
    typedef igstk::TrackerConfiguration::Pointer PayloadType; 
    TrackerConfigurationDataEvent() {} 
    virtual ~TrackerConfigurationDataEvent() {} 
    virtual const char * GetEventName() const { 
      return "TrackerConfigurationDataEvent"; } 
    virtual bool CheckEvent(const ::itk::EventObject* e) const { 
      return dynamic_cast<const Self*>(e); } 
    virtual ::itk::EventObject* MakeObject() const { 
      return new Self; } 
    TrackerConfigurationDataEvent(const Self&s) :Superclass(s){}; 
    const PayloadType Get() const { 
      return m_Payload; }  
    void Set( PayloadType _var ) { 
      m_Payload = _var; }  
  private: 
    void operator=(const Self&);  
    PayloadType  m_Payload; 
  };
 
   /**
   * Class that observers both read success, 
   * igstk::TrackerConfigurationFileReader::ReadSuccessEventand 
   * and read failure,
   * igstk::TrackerConfigurationFileReader::ReadFailureEvent
   * events generated by the TrackerConfigurationFileReader class.
   */
  class ReadTrackerConfigurationFailSuccessObserver : public ::itk::Command
  {
  public:
    typedef  ReadTrackerConfigurationFailSuccessObserver    Self;
    typedef  ::itk::Command             Superclass;
    typedef  ::itk::SmartPointer<Self>  Pointer;
    itkNewMacro( Self );
  protected:
    ReadTrackerConfigurationFailSuccessObserver() : 
      m_GotFailure( false ), m_GotSuccess( false ) {}
    ~ReadTrackerConfigurationFailSuccessObserver() {}
  public:
    void Execute(itk::Object *caller, const itk::EventObject & event)
    {
      const itk::Object * constCaller = caller;
      this->Execute( constCaller, event );
    }
    void Execute(const itk::Object * itkNotUsed(caller), const itk::EventObject & event)
    {
    const igstk::TrackerConfigurationFileReader::ReadSuccessEvent *rse = 
      dynamic_cast<const 
        igstk::TrackerConfigurationFileReader::ReadSuccessEvent *>( &event);

    if( rse )
      {
      m_GotSuccess = true;
      }
    else
      {
      const igstk::TrackerConfigurationFileReader::ReadFailureEvent *rfe = 
        dynamic_cast<const 
          igstk::TrackerConfigurationFileReader::ReadFailureEvent *>( &event);

      if( rfe )
        {
        m_GotFailure = true;
        m_FailureMessage = rfe->Get();
        }
      }
    }

    bool GotSuccess() {return this->m_GotSuccess;}
    bool GotFailure() {return this->m_GotFailure;}
    void Reset() 
    { 
      this->m_GotSuccess = false; 
      this->m_GotFailure = false;
      this->m_FailureMessage.clear();
    }
    std::string GetFailureMessage() const
    {
      return m_FailureMessage;
    }
  private:
    std::string m_FailureMessage;
    bool m_GotFailure;
    bool m_GotSuccess;
  };

    /** This event is generated if setting the xml reader succeeded. */
  igstkEventMacro( SetReaderSuccessEvent, IGSTKEvent );

    /** This event is generated if setting the xml reader failed. */
  igstkEventMacro( SetReaderFailureEvent, IGSTKErrorEvent );

    /** This event is generated if setting the file name succeeded. */
  igstkEventMacro( SetFileNameSuccessEvent, IGSTKEvent );
 
    /** This event is generated if setting the file name failed. */
  igstkEventMacro( SetFileNameFailureEvent, IGSTKErrorEvent );

    /** This event is generated if the reading succeeded. */
  igstkEventMacro( ReadSuccessEvent, IGSTKEvent );

    /** This event is generated if reading failed. */
  igstkLoadedEventMacro( ReadFailureEvent, IGSTKErrorEvent, 
                         EventHelperType::StringType );

    /** This event is generated if reading failed because of a mismatch between
      the reader and the actual tracker data. */
  igstkEventMacro( UnexpectedTrackerTypeEvent, IGSTKEvent );

   /**
   * Class that observers both read success, 
   * igstk::OIGTLinkTrackerConfigurationFileReader::ReadSuccessEvent
   * and read failure, 
   * igstk::OIGTLinkTrackerConfigurationFileReader::ReadFailureEvent,
   * igstk::OIGTLinkTrackerConfigurationFileReader::UnexpectedTrackerTypeEvent,
   * events generated by the OIGTLinkTrackerConfigurationFileReader class.
   */
  class ReadFailSuccessObserver : public ::itk::Command
  {
  public:
    typedef  ReadFailSuccessObserver    Self;
    typedef  ::itk::Command             Superclass;
    typedef  ::itk::SmartPointer<Self>  Pointer;
    itkNewMacro( Self );
  protected:
    ReadFailSuccessObserver() 
      { 
      m_GotFailure = false, 
      m_GotSuccess = false,
      m_GotUnexpectedTrackerType = false; 
      }
    ~ReadFailSuccessObserver() {}
  public:
    void Execute(itk::Object *caller, const itk::EventObject & event)
    {
      const itk::Object * constCaller = caller;
      this->Execute( constCaller, event );
    }
    void Execute(const itk::Object * itkNotUsed(caller), const itk::EventObject & event)
    {
      if( dynamic_cast<const 
          igstk::TrackerConfigurationFileReader::ReadSuccessEvent *>( &event) )
        {
        m_GotSuccess = true;
        }
      else if( const igstk::TrackerConfigurationFileReader::ReadFailureEvent *rfe = 
        dynamic_cast<const 
          igstk::TrackerConfigurationFileReader::ReadFailureEvent *>( &event) )
        {
        m_GotFailure = true;
        m_FailureMessage = rfe->Get();
        }
      else if( dynamic_cast<const igstk::TrackerConfigurationFileReader::
                 UnexpectedTrackerTypeEvent *>( &event) )
        {
        m_GotUnexpectedTrackerType = true;
        }
    }
    bool GotSuccess() {return this->m_GotSuccess;}
    bool GotFailure() {return this->m_GotFailure;}
    bool GotUnexpectedTrackerType() {return this->m_GotUnexpectedTrackerType;}
    void Reset() 
    { 
      this->m_GotSuccess = false; 
      this->m_GotFailure = false;
      this->m_GotUnexpectedTrackerType = false;
      this->m_FailureMessage.clear();
    }
    std::string GetFailureMessage() const
    {
      return m_FailureMessage;
    }
  private:
    std::string m_FailureMessage;
    bool m_GotSuccess;
    bool m_GotFailure;
    bool m_GotUnexpectedTrackerType;
  };

protected:

  TrackerConfigurationFileReader( void );
  virtual ~TrackerConfigurationFileReader( void );

  /** Print the object information in a stream. */
  void PrintSelf( std::ostream& os, itk::Indent indent ) const;

private:

  /** List of state machine states */
  igstkDeclareStateMacro( Idle );
  igstkDeclareStateMacro( AttemptingSetFileName );
  igstkDeclareStateMacro( AttemptingSetReader );
  igstkDeclareStateMacro( FileNameSet );
  igstkDeclareStateMacro( ReaderSet );
  igstkDeclareStateMacro( AttemptingSetReaderHaveFileName );
  igstkDeclareStateMacro( AttemptingSetFileNameHaveReader );
  igstkDeclareStateMacro( Initialized );  
  igstkDeclareStateMacro( AttemptingRead );
  igstkDeclareStateMacro( HaveData );

  /** List of state machine inputs */
  igstkDeclareInputMacro( SetReader );
  igstkDeclareInputMacro( SetFileName );
  igstkDeclareInputMacro( Read );
  igstkDeclareInputMacro( GetData  );
  igstkDeclareInputMacro( Success );
  igstkDeclareInputMacro( Failure );

  /**List of state machine actions*/
  void ReportInvalidRequestProcessing();  
  void SetReaderProcessing();
  void ReportSetReaderSuccessProcessing();
  void ReportSetReaderFailureProcessing();
  void SetFileNameProcessing();
  void ReportSetFileNameSuccessProcessing();
  void ReportSetFileNameFailureProcessing();
  void ReadProcessing();
  void ReportReadSuccessProcessing();
  void ReportReadFailureProcessing();
  void GetDataProcessing();

  igstk::TrackerConfiguration::Pointer m_TrackerConfiguration;
  
  FileReaderBasePointerType m_TmpXMLFileReader;
  FileReaderBasePointerType m_XMLFileReader;

  std::string m_TmpFileName;
  std::string m_FileName;

              //if reading fails the ReadProcessing() method updates this string
              //with an appropriate error message which the user recieves when
              //the ReportReadFailureProcessing() generates the ReadFailureEvent
  EventHelperType::StringType m_ReadFailureErrorMessage;
};


}
#endif //__igstkTrackerConfigurationFileReader_h