PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusDevice.h
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #ifndef __vtkPlusDevice_h
8 #define __vtkPlusDevice_h
9 
10 // Local includes
11 #include "igsioCommon.h"
12 #include "PlusConfigure.h"
13 #include "PlusStreamBufferItem.h"
14 #include "vtkPlusChannel.h"
15 #include "vtkPlusDataCollectionExport.h"
16 
17 // VTK includes
18 #include <vtkImageAlgorithm.h>
19 #include <vtkMultiThreader.h>
20 #include <vtkStdString.h>
21 
22 #include <set>
23 
24 // STL includes
25 #include <string>
26 
27 class vtkPlusBuffer;
29 class vtkPlusDataSource;
30 class vtkPlusDevice;
32 class vtkXMLDataElement;
33 
34 typedef std::vector<vtkPlusChannel*> ChannelContainer;
35 typedef ChannelContainer::const_iterator ChannelContainerConstIterator;
36 typedef ChannelContainer::iterator ChannelContainerIterator;
37 
38 typedef std::vector<vtkPlusBuffer*> StreamBufferContainer;
39 typedef StreamBufferContainer::const_iterator StreamBufferContainerConstIterator;
40 typedef StreamBufferContainer::iterator StreamBufferContainerIterator;
41 
42 typedef std::map<int, vtkPlusBuffer*> StreamBufferMapContainer;
43 typedef StreamBufferMapContainer::const_iterator StreamBufferMapContainerConstIterator;
44 typedef StreamBufferMapContainer::iterator StreamBufferMapContainerIterator;
45 
46 typedef std::vector<vtkPlusDevice*> DeviceCollection;
47 typedef std::vector<vtkPlusDevice*>::iterator DeviceCollectionIterator;
48 typedef std::vector<vtkPlusDevice*>::const_iterator DeviceCollectionConstIterator;
49 
60 class vtkPlusDataCollectionExport vtkPlusDevice : public vtkImageAlgorithm
61 {
62 public:
63  static vtkPlusDevice* New();
64  vtkTypeMacro(vtkPlusDevice, vtkImageAlgorithm);
65  virtual void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
66 
67  static const std::string BMODE_PORT_NAME;
68  static const std::string RFMODE_PORT_NAME;
69  static const std::string PARAMETERS_XML_ELEMENT_TAG;
70  static const std::string PARAMETER_XML_ELEMENT_TAG;
71 
76  virtual PlusStatus Probe();
77 
79  virtual std::string GetSdkVersion();
80 
82  virtual PlusStatus ReadConfiguration(vtkXMLDataElement*);
83 
85  virtual PlusStatus WriteConfiguration(vtkXMLDataElement*);
86 
88  virtual PlusStatus Connect();
89 
94  virtual PlusStatus ForceUpdate();
95 
101  virtual PlusStatus Disconnect();
102 
106  virtual bool IsResettable();
107 
112  virtual PlusStatus StartRecording();
113 
115  virtual PlusStatus StopRecording();
116 
118  static PlusStatus GetToolReferenceFrameFromTrackedFrame(igsioTrackedFrame& aFrame, std::string& aToolReferenceFrameName);
119 
126  //virtual PlusStatus SetBuffer(vtkPlusBuffer* newBuffer);
127 
132  virtual PlusStatus SetBufferSize(vtkPlusChannel& aChannel, int FrameBufferSize, const std::string& aSourceId = std::string(""));
134  virtual PlusStatus GetBufferSize(vtkPlusChannel& aChannel, int& outVal, const std::string& aSourceId = std::string(""));
135 
137  virtual void SetStartTime(double startTime);
138 
140  virtual double GetStartTime();
141 
143  virtual bool IsTracker() const;
144 
145  virtual bool IsVirtual() const;
146 
151  virtual PlusStatus Reset();
152 
154  void ClearAllBuffers();
155 
157  virtual PlusStatus WriteToolsToSequenceFile(const std::string& filename, bool useCompression = false);
158 
160  void DeepCopy(const vtkPlusDevice& device);
161 
163  double GetInternalUpdateRate() const;
164 
166  PlusStatus GetDataSource(const char* aSourceId, vtkPlusDataSource*& aSource);
167  PlusStatus GetDataSource(const std::string& aSourceId, vtkPlusDataSource*& aSource);
168 
170  PlusStatus GetTool(const char* aToolSourceId, vtkPlusDataSource*& aTool) const;
171  PlusStatus GetTool(const std::string& aToolSourceId, vtkPlusDataSource*& aTool) const;
172 
174  PlusStatus GetFirstActiveTool(vtkPlusDataSource*& aTool) const;
175 
177  PlusStatus GetToolByPortName(const char* aPortName, vtkPlusDataSource*& aSource);
178  PlusStatus GetToolByPortName(const std::string& aPortName, vtkPlusDataSource*& aSource);
180  PlusStatus GetVideoSourcesByPortName(const char* aPortName, std::vector<vtkPlusDataSource*>& sources);
181  PlusStatus GetVideoSourcesByPortName(const std::string& aPortName, std::vector<vtkPlusDataSource*>& sources);
182 
184  DataSourceContainerConstIterator GetToolIteratorBegin() const;
185 
187  DataSourceContainerConstIterator GetToolIteratorEnd() const;
188 
190  PlusStatus AddTool(vtkPlusDataSource* tool, bool requireUniquePortName = true);
191 
193  virtual int GetNumberOfTools() const;
194 
196  PlusStatus GetVideoSource(const char* aSourceId, vtkPlusDataSource*& aVideoSource);
197 
199  std::vector<vtkPlusDataSource*> GetVideoSources() const;
200 
202  PlusStatus GetVideoSourceByIndex(const unsigned int index, vtkPlusDataSource*& aVideoSource);
203 
205  PlusStatus GetFirstVideoSource(vtkPlusDataSource*& anImage);
206 
208  DataSourceContainerConstIterator GetVideoSourceIteratorBegin() const;
209 
211  DataSourceContainerConstIterator GetVideoSourceIteratorEnd() const;
212 
214  PlusStatus AddVideoSource(vtkPlusDataSource* anImage);
215 
217  virtual int GetNumberOfVideoSources() const;
218 
220  PlusStatus GetFieldDataSource(const char* aSourceId, vtkPlusDataSource*& aSource) const;
221  PlusStatus GetFieldDataSource(const std::string& aSourceId, vtkPlusDataSource*& aSource) const;
222 
224  DataSourceContainerConstIterator GetFieldDataSourcessIteratorBegin() const;
225 
227  DataSourceContainerConstIterator GetFieldDataSourcessIteratorEnd() const;
228 
230  PlusStatus AddFieldDataSource(vtkPlusDataSource* aSource);
231 
233  virtual int GetNumberOfFieldDataSources() const;
234 
236  void SetToolReferenceFrameName(const std::string& frameName);
238  std::string GetToolReferenceFrameName() const;
239 
241  virtual bool GetCorrectlyConfigured() const;
242 
244  virtual void SetDataCollector(vtkPlusDataCollector* _arg);
245 
247  void SetToolsBufferSize(int aBufferSize);
248 
250  void SetFieldDataSourcesBufferSize(int aBufferSize);
251 
253  virtual void SetLocalTimeOffsetSec(double aTimeOffsetSec);
254  virtual double GetLocalTimeOffsetSec() const;
255 
267  virtual PlusStatus InternalUpdate();
268 
272  PlusStatus GetInputDevices(std::vector<vtkPlusDevice*>& outDeviceList) const;
276  PlusStatus GetInputDevicesRecursive(std::vector<vtkPlusDevice*>& outDeviceList) const;
277 
278  // Parameter interface
279  virtual PlusStatus SetParameter(const std::string& key, const std::string& value);
280  virtual std::string GetParameter(const std::string& key) const;
281  virtual PlusStatus GetParameter(const std::string& key, std::string& outValue) const;
282 
283  //BTX
284  // These are used by static functions in vtkPlusDevice.cxx, and since
285  // VTK doesn't generally use 'friend' functions they are public
286  // instead of protected. Do not use them anywhere except inside
287  // vtkPlusDevice.cxx.
288  vtkIGSIORecursiveCriticalSection* UpdateMutex;
289  vtkTimeStamp UpdateTime;
291  //ETX
292 
294  virtual double GetAcquisitionRate() const;
295  PlusStatus SetAcquisitionRate(double aRate);
296 
298  virtual bool IsRecording() const;
299 
300  /* Return the id of the device */
301  virtual std::string GetDeviceId() const;
302  // Set the device Id
303  void SetDeviceId(const std::string& id);
304 
309  virtual unsigned long GetFrameNumber() const;
310 
319  virtual double GetFrameTimeStamp() const;
320 
325  vtkSetMacro(UpdateWithDesiredTimestamp, int);
330  vtkGetMacro(UpdateWithDesiredTimestamp, int);
335  vtkBooleanMacro(UpdateWithDesiredTimestamp, int);
336 
341  vtkSetMacro(DesiredTimestamp, double);
343  vtkGetMacro(DesiredTimestamp, double);
344 
346  vtkGetMacro(TimestampClosestToDesired, double);
347 
349  virtual int GetConnected() const;
350  virtual bool IsConnected() const;
351 
357  virtual PlusStatus SetInputFrameSize(vtkPlusDataSource& aSource, unsigned int x, unsigned int y, unsigned int z);
358 
360  virtual PlusStatus GetInputFrameSize(vtkPlusChannel& aChannel, unsigned int& x, unsigned int& y, unsigned int& z) const;
361 
363  virtual PlusStatus GetInputFrameSize(vtkPlusChannel& aChannel, FrameSizeType& dim) const;
364 
366  virtual PlusStatus GetOutputFrameSize(vtkPlusChannel& aChannel, unsigned int& x, unsigned int& y, unsigned int& z) const;
367 
369  virtual PlusStatus GetOutputFrameSize(vtkPlusChannel& aChannel, FrameSizeType& dim) const;
370 
372  virtual PlusStatus SetPixelType(vtkPlusChannel& aChannel, igsioCommon::VTKScalarPixelType pixelType);
374  virtual igsioCommon::VTKScalarPixelType GetPixelType(vtkPlusChannel& aChannel);
375 
377  virtual PlusStatus SetImageType(vtkPlusChannel& aChannel, US_IMAGE_TYPE imageType);
379  virtual US_IMAGE_TYPE GetImageType(vtkPlusChannel& aChannel);
380 
382  PlusStatus AddOutputChannel(vtkPlusChannel* aChannel);
383 
385  PlusStatus GetFirstOutputChannel(vtkPlusChannel*& aChannel);
386  PlusStatus GetOutputChannelByName(vtkPlusChannel*& aChannel, const char* aChannelId);
387  PlusStatus GetOutputChannelByName(vtkPlusChannel*& aChannel, const std::string& aChannelId);
388 
389  virtual int OutputChannelCount() const;
390 
391  ChannelContainerConstIterator GetOutputChannelsStart() const;
392  ChannelContainerConstIterator GetOutputChannelsEnd() const;
393  ChannelContainerIterator GetOutputChannelsStart();
394  ChannelContainerIterator GetOutputChannelsEnd();
395 
397  PlusStatus AddInputChannel(vtkPlusChannel* aChannel);
398 
402  virtual PlusStatus NotifyConfigured();
403 
408  virtual int RequestInformation(vtkInformation*, vtkInformationVector**, vtkInformationVector*);
409 
414  virtual int RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector*);
415 
416  /* Accessors for the grace period value */
417  vtkSetMacro(MissingInputGracePeriodSec, double);
418  double GetMissingInputGracePeriodSec() const;
419 
424  virtual PlusStatus CreateDefaultOutputChannel(const char* channelId = NULL, bool addSource = true);
425 
427  PlusStatus GetFirstActiveOutputVideoSource(vtkPlusDataSource*& aVideoSource);
428 
430  virtual PlusStatus GetImageMetaData(igsioCommon::ImageMetaDataList& imageMetaDataItems);
431 
437  virtual PlusStatus GetImage(const std::string& requestedImageId, std::string& assignedImageId, const std::string& imageReferencFrameName, vtkImageData* imageData, vtkMatrix4x4* ijkToReferenceTransform);
438 
443  virtual PlusStatus SendText(const std::string& textToSend, std::string* textReceived = NULL);
444 
445 protected:
446  static void* vtkDataCaptureThread(vtkMultiThreader::ThreadInfo* data);
447 
449  virtual PlusStatus InternalConnect();
450 
452  virtual PlusStatus InternalDisconnect();
453 
458  virtual PlusStatus InternalStartRecording();
459 
464  virtual PlusStatus InternalStopRecording();
465 
469  virtual PlusStatus AddVideoItemToVideoSources(const std::vector<vtkPlusDataSource*>& videoSources, const igsioVideoFrame& frame, long frameNumber, double unfilteredTimestamp = UNDEFINED_TIMESTAMP,
470  double filteredTimestamp = UNDEFINED_TIMESTAMP, const igsioFieldMapType* customFields = NULL);
471 
475  virtual PlusStatus AddVideoItemToVideoSources(const std::vector<vtkPlusDataSource*>& videoSources, void* imageDataPtr, US_IMAGE_ORIENTATION usImageOrientation, const FrameSizeType& frameSizeInPx,
476  igsioCommon::VTKScalarPixelType pixelType, unsigned int numberOfScalarComponents, US_IMAGE_TYPE imageType, int numberOfBytesToSkip, long frameNumber, double unfilteredTimestamp = UNDEFINED_TIMESTAMP,
477  double filteredTimestamp = UNDEFINED_TIMESTAMP, const igsioFieldMapType* customFields = NULL);
478 
484  virtual PlusStatus ToolTimeStampedUpdate(const std::string& aToolSourceId, vtkMatrix4x4* matrix, ToolStatus status, unsigned long frameNumber, double unfilteredtimestamp, const igsioFieldMapType* customFields = NULL);
485 
492  virtual PlusStatus ToolTimeStampedUpdateWithoutFiltering(const std::string& aToolSourceId, vtkMatrix4x4* matrix, ToolStatus status, double unfilteredtimestamp, double filteredtimestamp, const igsioFieldMapType* customFields = NULL);
493 
497  vtkXMLDataElement* FindThisDeviceElement(vtkXMLDataElement* rootXMLElement);
501  vtkXMLDataElement* FindOutputChannelElement(vtkXMLDataElement* rootXMLElement, const char* aChannelId);
505  vtkXMLDataElement* FindInputChannelElement(vtkXMLDataElement* rootXMLElement, const char* aChannelId);
509  virtual void InternalWriteOutputChannels(vtkXMLDataElement* rootXMLElement);
513  virtual void InternalWriteInputChannels(vtkXMLDataElement* rootXMLElement);
514 
516  PlusStatus EnsureUniqueDataSourceId(const std::string& aSourceId);
517 
518  vtkSetMacro(CorrectlyConfigured, bool);
519 
520  vtkSetMacro(StartThreadForInternalUpdates, bool);
521  bool GetStartThreadForInternalUpdates() const;
522 
523  vtkSetMacro(RecordingStartTime, double);
524  double GetRecordingStartTime() const;
525 
526  virtual vtkPlusDataCollector* GetDataCollector();
527 
528  bool HasGracePeriodExpired();
529 
530  vtkPlusDevice();
531  virtual ~vtkPlusDevice();
532 
533 protected:
536 
537  /* Is device connected */
539 
541  vtkMultiThreader* Threader;
542 
544  int ThreadId;
545 
548 
554 
557 
559  std::string DeviceId;
560 
562 
565 
566  /* Flag whether the device is recording */
568 
573 
575  unsigned long FrameNumber;
577 
580 
583 
589 
592 
597 
603  std::set<std::string> ReportedUnknownTools;
604 
606  std::map<std::string, std::string> Parameters;
607 
608  static const int VIRTUAL_DEVICE_FRAME_RATE;
609 
610 protected:
611  /*
612  When defining a device, it may be a tracker or imaging device
613  These variables allow a device to define which commonly used attributes it is expecting
614  */
617 
618 private:
619  vtkPlusDevice(const vtkPlusDevice&); // Not implemented.
620  void operator=(const vtkPlusDevice&); // Not implemented.
621 };
622 
623 #define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement) \
624  if( Superclass::ReadConfiguration(rootConfigElement) != PLUS_SUCCESS ) \
625  { \
626  LOG_ERROR("Unable to continue reading configuration of "<<this->GetClassName()<<". Generic device configuration reading failed."); \
627  return PLUS_FAIL; \
628  } \
629  vtkXMLDataElement* deviceConfig = this->FindThisDeviceElement(rootConfigElement); \
630  if (deviceConfig == NULL) \
631  { \
632  LOG_ERROR("Unable to continue configuration of "<<this->GetClassName()<<". Could not find corresponding element."); \
633  return PLUS_FAIL; \
634  }
635 
636 #define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement) \
637  if( Superclass::WriteConfiguration(rootConfigElement) == PLUS_FAIL ) \
638  { \
639  LOG_ERROR("Unable to continue writing configuration of "<<this->GetClassName()<<". Generic device configuration writing failed."); \
640  return PLUS_FAIL; \
641  } \
642  vtkXMLDataElement* deviceConfig = this->FindThisDeviceElement(rootConfigElement); \
643  if (deviceConfig == NULL) \
644  { \
645  LOG_ERROR("Cannot find or add "<<this->GetClassName()<<" device in XML tree"); \
646  return PLUS_FAIL; \
647  }
648 
649 #endif
DataSourceContainer::const_iterator DataSourceContainerConstIterator
static const int VIRTUAL_DEVICE_FRAME_RATE
const uint32_t * data
Definition: phidget22.h:3971
std::string ToolReferenceFrameName
static const std::string RFMODE_PORT_NAME
Definition: vtkPlusDevice.h:68
vtkPlusDataCollector * DataCollector
const char * key
Definition: phidget22.h:5111
Abstract interface for tracker and video devices.
Definition: vtkPlusDevice.h:60
std::vector< vtkPlusDevice * >::const_iterator DeviceCollectionConstIterator
Definition: vtkPlusDevice.h:48
std::map< std::string, vtkPlusDataSource * > DataSourceContainer
static const std::string PARAMETER_XML_ELEMENT_TAG
Definition: vtkPlusDevice.h:70
double FrameTimeStamp
igsioStatus PlusStatus
Definition: PlusCommon.h:40
ChannelContainer InputChannels
static const std::string BMODE_PORT_NAME
Definition: vtkPlusDevice.h:67
bool RequirePortNameInDeviceSetConfiguration
StreamBufferMapContainer::iterator StreamBufferMapContainerIterator
Definition: vtkPlusDevice.h:44
std::vector< vtkPlusDevice * > DeviceCollection
Definition: vtkPlusDevice.h:46
DataSourceContainer VideoSources
std::map< std::string, std::string > Parameters
bool RequireImageOrientationInConfiguration
StreamBufferContainer::const_iterator StreamBufferContainerConstIterator
Definition: vtkPlusDevice.h:39
double AcquisitionRate
vtkTimeStamp UpdateTime
DataSourceContainer Tools
double LocalTimeOffsetSec
vtkMultiThreader * Threader
DataSourceContainer Fields
#define VTK_OVERRIDE
Definition: PlusCommon.h:49
std::vector< vtkPlusBuffer * > StreamBufferContainer
Definition: vtkPlusDevice.h:38
double MissingInputGracePeriodSec
unsigned long FrameNumber
Manages devices that record image or positional data.
double RecordingStartTime
int OutputNeedsInitialization
StreamBufferMapContainer::const_iterator StreamBufferMapContainerConstIterator
Definition: vtkPlusDevice.h:43
vtkIGSIORecursiveCriticalSection * UpdateMutex
ChannelContainer::const_iterator ChannelContainerConstIterator
Definition: vtkPlusDevice.h:35
StreamBufferItem * CurrentStreamBufferItem
double DesiredTimestamp
double InternalUpdateRate
int x
Definition: phidget22.h:4265
int UpdateWithDesiredTimestamp
std::string DeviceId
const char const char * value
Definition: phidget22.h:5111
int VTKScalarPixelType
Definition: PlusCommon.h:55
bool StartThreadForInternalUpdates
static const std::string PARAMETERS_XML_ELEMENT_TAG
Definition: vtkPlusDevice.h:69
std::vector< vtkPlusDevice * >::iterator DeviceCollectionIterator
Definition: vtkPlusDevice.h:47
Contains an optional timestamped circular buffer containing the video images and a number of timestam...
ChannelContainer OutputChannels
Direction vectors of rods y
Definition: algo3.m:15
double TimestampClosestToDesired
std::set< std::string > ReportedUnknownTools
std::vector< vtkPlusChannel * > ChannelContainer
Definition: vtkPlusDevice.h:32
StreamBufferContainer::iterator StreamBufferContainerIterator
Definition: vtkPlusDevice.h:40
class for generating basic html tags
ChannelContainer::iterator ChannelContainerIterator
Definition: vtkPlusDevice.h:36
std::map< int, vtkPlusBuffer * > StreamBufferMapContainer
Definition: vtkPlusDevice.h:42
bool CorrectlyConfigured
Interface to a 3D positioning tool, video source, or generalized data stream.