~ubuntu-branches/ubuntu/lucid/igstk/lucid

« back to all changes in this revision

Viewing changes to Examples/TrackerConfiguration/igstkTrackerController.h

  • Committer: Bazaar Package Importer
  • Author(s): Dominique Belhachemi
  • Date: 2009-10-07 17:35:43 UTC
  • mfrom: (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091007173543-69eyhyjrh0bfoqe5
Tags: 4.2.0-2
* Fixed link issue (Closes: #549799)
* switch to quilt

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
 
 
3
  Program:   Image Guided Surgery Software Toolkit
 
4
  Module:    $RCSfile: igstkTrackerController.h,v $
 
5
  Language:  C++
 
6
  Date:      $Date: 2009-04-20 18:23:54 $
 
7
  Version:   $Revision: 1.4 $
 
8
 
 
9
  Copyright (c) ISC  Insight Software Consortium.  All rights reserved.
 
10
  See IGSTKCopyright.txt or http://www.igstk.org/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
 
 
18
#ifndef __igstkTrackerController_h
 
19
#define __igstkTrackerController_h
 
20
 
 
21
#include "igstkConfigure.h"
 
22
 
 
23
#include "igstkTrackerConfiguration.h"
 
24
#include "igstkPolarisTrackerConfiguration.h"
 
25
#include "igstkAuroraTrackerConfiguration.h"
 
26
#include "igstkAscensionTrackerConfiguration.h"
 
27
 
 
28
#include "igstkMacros.h"
 
29
#include "igstkEvents.h"
 
30
#include "igstkObject.h"
 
31
#include "igstkSerialCommunication.h"
 
32
#include "igstkTransform.h"
 
33
 
 
34
#include "igstkTracker.h"
 
35
#include "igstkTrackerTool.h"
 
36
#include "igstkPolarisTrackerTool.h"
 
37
#include "igstkAuroraTrackerTool.h"
 
38
#include "igstkAscensionTrackerTool.h"
 
39
 
 
40
#include "igstkSpatialObject.h"
 
41
 
 
42
#ifdef IGSTK_USE_MicronTracker
 
43
#include "igstkMicronTrackerConfiguration.h"
 
44
#include "igstkMicronTrackerTool.h"
 
45
#endif
 
46
 
 
47
 
 
48
namespace igstk
 
49
{
 
50
 
 
51
/** \class TrackerController
 
52
 *
 
53
 *  \brief This class encapsulates the startup and shutdown procedures required
 
54
 *         for the tracker classes supported by IGSTK.
 
55
 *
 
56
 *  This class enables the user to intialize a tracker with minimal effort. 
 
57
 *  Initialization is performed by passing the controller a TrackerConfiguration
 
58
 *  object. The controller identifies the specific tracker internally. The
 
59
 *  user can then request to intialize the tracker or to tell the tracker 
 
60
 *  controller to shut down a tracker that is running. 
 
61
 *
 
62
 *  NOTE: This class is usefull only when all tools are connected to the tracker 
 
63
 *        in advance (no swapping tools in and out after initialization).
 
64
 */
 
65
class TrackerController : public Object
 
66
{
 
67
public:
 
68
  /**This is the container type in which the tools are found.*/   
 
69
  typedef std::map < std::string, igstk::TrackerTool::Pointer > 
 
70
    ToolContainerType;
 
71
 
 
72
  typedef std::pair < std::string, igstk::TrackerTool::Pointer > 
 
73
    ToolEntryType;
 
74
 
 
75
  typedef Transform                                        TransformType;
 
76
 
 
77
  typedef SpatialObject                                    SpatialObjectType;
 
78
 
 
79
  /** Macro with standard traits declarations (Self, SuperClass, State 
 
80
   *  Machine etc.). */
 
81
  igstkStandardClassTraitsMacro( TrackerController, Object )
 
82
 
 
83
  /**
 
84
   * Initialize a tracker using the given configuration. Communication with the
 
85
   * haredware is established and the tracker is initialized.
 
86
   * NOTE: If a tracker was already initialized using this object it will be 
 
87
   *       shut down (stop tracking, close communication, and if using serial
 
88
   *       communication that will be closed too).
 
89
   *
 
90
   * @param configuration A specific tracker configuration (Aurora/Micron...)
 
91
   *                      which is identified at runtime.
 
92
   * This method generates two events, InitializeEvent if everything
 
93
   * went well and, InitializeErrorEvent if the tracker could not be 
 
94
   * initialized (the specific error is described in the string contained in the
 
95
   * error object).
 
96
   */
 
97
  void RequestInitialize(const TrackerConfiguration *configuration);
 
98
 
 
99
  /**
 
100
   * Start the tracking. The method generates igstk::TrackerStartTrackingEvent 
 
101
   * and igstk::TrackerStartTrackingErrorEvent (see igstkTracker.h).
 
102
   */
 
103
  void RequestStartTracking();
 
104
 
 
105
  /**
 
106
   * Stop the tracking. The method generates igstk::TrackerStopTrackingEvent and
 
107
   * igstk::TrackerStopTrackingErrorEvent (see igstkTracker.h).
 
108
   */
 
109
  void RequestStopTracking();
 
110
 
 
111
  /**
 
112
   * This method stops the tracking and closes communication.
 
113
   * The method generates igstk::TrackerStopTrackingEvent or 
 
114
   * igstk::TrackerStopTrackingErrorEvent followed by 
 
115
   * CloseCommunicationEvent or CloseCommunicationErrorEvent.
 
116
   */
 
117
  void RequestShutdown();
 
118
 
 
119
  /**
 
120
   * Get the container (ToolContainerType) with all the tools.   
 
121
   * The method generates a RequestToolsEvent if the tracker was previously 
 
122
   * initialized successfuly, otherwise it generates an 
 
123
   * InvalidRequestErrorEvent.*/
 
124
  void RequestGetNonReferenceToolList();
 
125
 
 
126
  /**
 
127
   * Get a specific tracker tool according to its unique name.   
 
128
   * The method generates a RequestToolEvent if the tracker was 
 
129
   * previously initialized successfuly and the tool exists, otherwise it 
 
130
   * generates a RequestToolErrorEvent if the tool doesn't exist, or
 
131
   * InvalidRequestErrorEvent if the tracker wasn't initialized.*/
 
132
  void RequestGetTool( const std::string &toolName );
 
133
 
 
134
  /**
 
135
   * Get the reference tool it it exists.   
 
136
   * The method generates a RequestToolEvent if the tracker was 
 
137
   * previously initialized successfuly, otherwise it generates an 
 
138
   * InvalidRequestErrorEvent.
 
139
   * Note that when no reference tool is used the payload of the event
 
140
   * will be a NULL pointer.*/
 
141
  void RequestGetReferenceTool();
 
142
 
 
143
  /** 
 
144
   * 
 
145
   * Sets the tracker's parent spatial object
 
146
   */
 
147
  void RequestSetParentSpatialObject( TransformType transform, 
 
148
                                      SpatialObjectType * spatialObject);
 
149
 
 
150
  /** 
 
151
  * Adds a Spatial Object as a child of the tracker. For example, if you want 
 
152
  * to display the tracker's working volume (e.g. frustum or cube).*/
 
153
  void RequestAddChildSpatialObject( TransformType transform, 
 
154
                                     SpatialObjectType * spatialObject);
 
155
 
 
156
  /** This event is generated if the initialization succeeds. */
 
157
  igstkEventMacro( InitializeEvent, IGSTKEvent );
 
158
 
 
159
  /** This event is generated if the initialization fails.*/
 
160
  igstkEventMacro( InitializeErrorEvent, IGSTKErrorWithStringEvent ); 
 
161
 
 
162
  /** This event is generated if the communication was closed successfully. */
 
163
  igstkEventMacro( CloseCommunicationEvent, IGSTKEvent );
 
164
 
 
165
  /** This event is generated if closing communication failed.*/
 
166
  igstkEventMacro( CloseCommunicationErrorEvent, IGSTKErrorWithStringEvent ); 
 
167
 
 
168
  /** This event is generated if the user requests the tracker tools and the 
 
169
   *  tracker is initialized. */
 
170
  igstkLoadedEventMacro( RequestToolsEvent, 
 
171
                         IGSTKEvent, 
 
172
                         ToolContainerType );
 
173
 
 
174
  /** This event is generated if the user requests a specific tracker tool and 
 
175
   *  the tracker is initialized. */
 
176
  igstkLoadedEventMacro( RequestToolEvent, 
 
177
                         IGSTKEvent, 
 
178
                         ToolEntryType );
 
179
 
 
180
  /** This event is generated if the user requested a specific tracker tool and
 
181
   *  the tool does not exist.*/
 
182
  igstkEventMacro( RequestToolErrorEvent, 
 
183
                   IGSTKErrorEvent );
 
184
 
 
185
protected:
 
186
 
 
187
  TrackerController( void );
 
188
  virtual ~TrackerController( void );
 
189
 
 
190
 
 
191
private:   
 
192
 
 
193
  /** List of state machine states */
 
194
  igstkDeclareStateMacro( Idle );
 
195
  igstkDeclareStateMacro( AttemptingToInitialize );
 
196
  igstkDeclareStateMacro( AttemptingToInitializePolarisVicra );
 
197
  igstkDeclareStateMacro( AttemptingToInitializePolarisHybrid );
 
198
  igstkDeclareStateMacro( AttemptingToInitializeAurora );
 
199
  igstkDeclareStateMacro( AttemptingToInitializeAscension );
 
200
  igstkDeclareStateMacro( AttemptingToInitializeMicron );
 
201
  igstkDeclareStateMacro( Initialized );
 
202
  igstkDeclareStateMacro( AttemptingToStartTracking );
 
203
  igstkDeclareStateMacro( Tracking );
 
204
  igstkDeclareStateMacro( AttemptingToCloseCommunication );
 
205
  igstkDeclareStateMacro( AttemptingToStopTracking );
 
206
 
 
207
  /** List of state machine inputs */
 
208
  igstkDeclareInputMacro( TrackerInitialize );
 
209
  igstkDeclareInputMacro( PolarisVicraInitialize );
 
210
  igstkDeclareInputMacro( PolarisHybridInitialize );
 
211
  igstkDeclareInputMacro( AuroraInitialize );
 
212
  igstkDeclareInputMacro( AscensionInitialize );
 
213
  igstkDeclareInputMacro( MicronInitialize );  
 
214
  igstkDeclareInputMacro( StartTracking );
 
215
  igstkDeclareInputMacro( StopTracking );
 
216
  igstkDeclareInputMacro( Failed  );
 
217
  igstkDeclareInputMacro( Succeeded  );
 
218
  igstkDeclareInputMacro( CloseCommunication );
 
219
  igstkDeclareInputMacro( GetTools  );
 
220
  igstkDeclareInputMacro( GetTool  );
 
221
  igstkDeclareInputMacro( GetReferenceTool  );
 
222
  igstkDeclareInputMacro( SetParentSpatialObject  );
 
223
  igstkDeclareInputMacro( SetChildSpatialObject  );
 
224
 
 
225
 
 
226
  /**List of state machine actions*/
 
227
  void TrackerInitializeProcessing();  
 
228
  void PolarisVicraInitializeProcessing();
 
229
  void PolarisHybridInitializeProcessing();
 
230
  void AuroraInitializeProcessing();
 
231
  void AscensionInitializeProcessing();
 
232
  void MicronInitializeProcessing();
 
233
  void StartTrackingProcessing();
 
234
  void StopTrackingProcessing();
 
235
  void CloseCommunicationProcessing();
 
236
  void GetToolsProcessing();
 
237
  void GetToolProcessing();
 
238
  void GetReferenceToolProcessing();
 
239
  void SetParentSpatialObjectProcessing();
 
240
  void SetChildSpatialObjectProcessing();
 
241
 
 
242
  void ReportInvalidRequestProcessing();
 
243
 
 
244
  void ReportInitializationFailureProcessing();
 
245
  void ReportInitializationSuccessProcessing();
 
246
 
 
247
  void ReportStartTrackingFailureProcessing();
 
248
  void ReportStartTrackingSuccessProcessing();
 
249
 
 
250
  void ReportStopTrackingFailureProcessing();
 
251
  void ReportStopTrackingSuccessProcessing();
 
252
 
 
253
  void ReportCloseCommunicationFailureProcessing();
 
254
  void ReportCloseCommunicationSuccessProcessing();
 
255
 
 
256
  bool InitializeSerialCommunication();
 
257
  PolarisTrackerTool::Pointer InitializePolarisWirelessTool(
 
258
    const PolarisWirelessToolConfiguration *toolConfiguration );
 
259
  PolarisTrackerTool::Pointer InitializePolarisWiredTool(
 
260
    const PolarisWiredToolConfiguration *toolConfiguration );
 
261
  AuroraTrackerTool::Pointer InitializeAuroraTool(
 
262
    const AuroraToolConfiguration *toolConfiguration );
 
263
 
 
264
#ifdef IGSTK_USE_MicronTracker
 
265
 
 
266
  MicronTrackerTool::Pointer InitializeMicronTool(
 
267
    const MicronToolConfiguration *toolConfiguration );
 
268
 
 
269
#endif
 
270
  AscensionTrackerTool::Pointer InitializeAscensionTool(
 
271
    const AscensionToolConfiguration *toolConfiguration );
 
272
 
 
273
 
 
274
  /**
 
275
   * \class Observer for the TrackerToolAttachmentToTrackerErrorEvent.
 
276
   */
 
277
  class ToolAttachErrorObserver : public ::itk::Command 
 
278
  { 
 
279
  public:
 
280
    typedef  ToolAttachErrorObserver    Self; 
 
281
    typedef  ::itk::Command             Superclass;
 
282
    typedef  ::itk::SmartPointer<Self>  Pointer;
 
283
    itkNewMacro( Self );
 
284
 
 
285
    void Execute(itk::Object *caller, const itk::EventObject & event)
 
286
    {
 
287
    const itk::Object * constCaller = caller;
 
288
    this->Execute( constCaller, event );
 
289
    }
 
290
 
 
291
 
 
292
    void Execute(const itk::Object *caller, const itk::EventObject & event)
 
293
    {
 
294
      if( dynamic_cast<const TrackerToolAttachmentToTrackerErrorEvent *> ( 
 
295
          &event ) )
 
296
      {                
 
297
        m_GotObject = true;
 
298
      }
 
299
    }
 
300
 
 
301
    bool GotToolAttachError() const
 
302
    {
 
303
      return m_GotObject;
 
304
    }
 
305
    void Reset() 
 
306
    {
 
307
      m_GotObject = false; 
 
308
    }
 
309
 
 
310
  protected:
 
311
    ToolAttachErrorObserver()
 
312
    {
 
313
      m_GotObject = false;
 
314
    }
 
315
    ~ToolAttachErrorObserver() { }
 
316
  private:
 
317
    bool m_GotObject;
 
318
  };
 
319
 
 
320
  class ErrorObserver : public itk::Command
 
321
    {
 
322
    public:
 
323
    typedef ErrorObserver                    Self;
 
324
    typedef ::itk::Command                   Superclass;
 
325
    typedef ::itk::SmartPointer<Self>        Pointer;
 
326
    typedef ::itk::SmartPointer<const Self>  ConstPointer;
 
327
 
 
328
    igstkNewMacro(Self)
 
329
    igstkTypeMacro(ErrorObserver, itk::Command)
 
330
 
 
331
    /** When an error occurs in an IGSTK component it will invoke this method 
 
332
     *  with the appropriate error event object as a parameter.*/
 
333
    virtual void Execute(itk::Object *caller, 
 
334
                         const itk::EventObject & event) throw (std::exception);
 
335
 
 
336
    /** When an error occurs in an IGSTK component it will invoke this method 
 
337
     *  with the appropriate error event object as a parameter.*/
 
338
    virtual void Execute(const itk::Object *caller, 
 
339
                         const itk::EventObject & event) throw (std::exception);
 
340
 
 
341
    /**Clear the current error.*/
 
342
    void ClearError() { this->m_ErrorOccured = false; 
 
343
                        this->m_ErrorMessage.clear(); }
 
344
    /**If an error occurs in one of the observed IGSTK components this method 
 
345
     * will return true.*/
 
346
    bool ErrorOccured() { return this->m_ErrorOccured; }
 
347
    /**Get the error message associated with the last IGSTK error.*/
 
348
    void GetErrorMessage(std::string &errorMessage) 
 
349
      {
 
350
      errorMessage = this->m_ErrorMessage;
 
351
      }
 
352
 
 
353
  protected:
 
354
 
 
355
    /**Construct an error observer for all the possible errors that occur in 
 
356
     * the observed IGSTK components.*/
 
357
    ErrorObserver();
 
358
    virtual ~ErrorObserver(){}
 
359
  private:
 
360
    bool                               m_ErrorOccured;
 
361
    std::string                        m_ErrorMessage;
 
362
    std::map<std::string,std::string>  m_ErrorEvent2ErrorMessage;
 
363
 
 
364
    //purposely not implemented
 
365
    ErrorObserver(const Self&);
 
366
    void operator=(const Self&); 
 
367
    };
 
368
 
 
369
  class TrackerUpdateObserver : public itk::Command
 
370
    {
 
371
 
 
372
    public:
 
373
    typedef TrackerUpdateObserver                    Self;
 
374
    typedef ::itk::Command                           Superclass;
 
375
    typedef ::itk::SmartPointer<Self>                Pointer;
 
376
    typedef ::itk::SmartPointer<const Self>          ConstPointer;
 
377
 
 
378
    void SetParent( TrackerController *parent );
 
379
 
 
380
    igstkNewMacro(Self)
 
381
    igstkTypeMacro(TrackerUpdateObserver, itk::Command)
 
382
 
 
383
    /** When an error occurs in an IGSTK component it will invoke this method 
 
384
     *  with the appropriate error event object as a parameter.*/
 
385
    virtual void Execute(itk::Object *caller, 
 
386
                         const itk::EventObject & event) throw (std::exception);
 
387
 
 
388
    /** When an error occurs in an IGSTK component it will invoke this method 
 
389
     *  with the appropriate error event object as a parameter.*/
 
390
    virtual void Execute(const itk::Object *caller, 
 
391
                         const itk::EventObject & event) throw (std::exception);
 
392
    private:
 
393
 
 
394
    TrackerController *m_parent;
 
395
    };
 
396
 
 
397
  //this object observes the tracker's igstk::TrackerUpdateStatusEvent and
 
398
  //igstk::TrackerUpdateStatusErrorEvent it then causes its parent to invoke
 
399
  //these events too. In this manner the user is inderictly observing the 
 
400
  //tracker
 
401
  TrackerUpdateObserver::Pointer m_TrackerUpdateStatusObserver;
 
402
 
 
403
  ErrorObserver::Pointer m_ErrorObserver;
 
404
  std::string m_ErrorMessage;
 
405
 
 
406
  SpatialObjectType     *m_TmpSpatialObject;
 
407
  TransformType          m_TmpTransform;
 
408
 
 
409
  TrackerConfiguration  *m_TmpTrackerConfiguration;
 
410
  TrackerConfiguration  *m_TrackerConfiguration;
 
411
  std::string m_RequestedToolName;
 
412
 
 
413
  Tracker::Pointer m_Tracker;
 
414
  std::map<std::string, TrackerTool::Pointer> m_Tools; 
 
415
  TrackerTool::Pointer m_ReferenceTool;
 
416
  SerialCommunication::Pointer m_SerialCommunication;     
 
417
};
 
418
 
 
419
} // end of namespace
 
420
#endif