PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusGetImageCommand.cxx
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 #include "PlusConfigure.h"
8 #include "vtkPlusDataCollector.h"
10 
11 #include "vtkImageData.h"
13 
14 namespace
15 {
16  static const std::string GET_IMAGE_META_DATA = "GET_IMGMETA";
17  static const std::string GET_IMAGE = "GET_IMAGE";
18 
19  static const char DeviceNameImageIdSeparator = '-';
20 }
21 
23 
24 //----------------------------------------------------------------------------
26  : ImageMetaDatasetsCount(1)
27 {
28 
29 }
30 
31 //----------------------------------------------------------------------------
33 {
34 }
35 
36 //----------------------------------------------------------------------------
37 void vtkPlusGetImageCommand::PrintSelf(ostream& os, vtkIndent indent)
38 {
39  this->Superclass::PrintSelf(os, indent);
40 }
41 
42 //----------------------------------------------------------------------------
43 void vtkPlusGetImageCommand::GetCommandNames(std::list<std::string>& cmdNames)
44 {
45  cmdNames.clear();
46  cmdNames.push_back(GET_IMAGE_META_DATA);
47  cmdNames.push_back(GET_IMAGE);
48 }
49 
50 //----------------------------------------------------------------------------
51 std::string vtkPlusGetImageCommand::GetDescription(const std::string& commandName)
52 {
53  std::string desc;
54  if (commandName.empty() || igsioCommon::IsEqualInsensitive(commandName, GET_IMAGE_META_DATA))
55  {
56  desc += GET_IMAGE_META_DATA;
57  desc += ": Acquire the image meta data information from all the devices that are connected.";
58  }
59  if (commandName.empty() || igsioCommon::IsEqualInsensitive(commandName, GET_IMAGE))
60  {
61  desc += GET_IMAGE;
62  desc += "Acquire the volume data and the ijkToRas transformation of the data from the specified device.";
63  }
64  return desc;
65 }
66 
67 //----------------------------------------------------------------------------
69 {
70  SetName(GET_IMAGE_META_DATA);
71 }
72 
73 //----------------------------------------------------------------------------
75 {
76  SetName(GET_IMAGE);
77 }
78 
79 //----------------------------------------------------------------------------
81 {
82  std::string baseMessage = std::string("vtkPlusGetImageCommand ") + (!this->Name.empty() ? this->Name : "(undefined)")
83  + ", device: (" + (this->GetImageId().empty() ? "(undefined)" : this->GetImageId()) + ")";
84 
85  std::string errorString;
86  if (igsioCommon::IsEqualInsensitive(this->Name, GET_IMAGE_META_DATA))
87  {
88  std::string imageIdStr(this->GetImageId());
89  if (imageIdStr.size() > 0)
90  {
91  LOG_ERROR("The implementation of the GET_IMGMETA is not implemented for the case in which an id is specified. The id is supposed to be empty and then it will send the meta data from all connected devices")
92  return PLUS_FAIL;
93  }
94  if (this->ExecuteImageMetaReply(errorString) != PLUS_SUCCESS)
95  {
96  this->QueueCommandResponse(PLUS_FAIL, baseMessage + " Failed. See error message.", errorString);
97  return PLUS_FAIL;
98  }
99  return PLUS_SUCCESS;
100  }
101  else if (igsioCommon::IsEqualInsensitive(this->Name, GET_IMAGE))
102  {
103  if (this->ExecuteImageReply(errorString) != PLUS_SUCCESS)
104  {
105  this->QueueCommandResponse(PLUS_FAIL, baseMessage + " Failed. See error message.", errorString);
106  return PLUS_FAIL;
107  }
108  return PLUS_SUCCESS;
109  }
110  this->QueueCommandResponse(PLUS_FAIL, baseMessage + " Failed. See error message.", std::string("Unknown command name: ") + this->Name);
111  return PLUS_FAIL;
112 }
113 
114 //----------------------------------------------------------------------------
116 {
117  vtkPlusDataCollector* dataCollector = GetDataCollector();
118  if (dataCollector == NULL)
119  {
120  outErrorString = "The DataCollector is NULL.";
121  return PLUS_FAIL;
122  }
123 
124  vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
125  vtkSmartPointer<vtkMatrix4x4> ijkToRasTransform = vtkSmartPointer<vtkMatrix4x4>::New();
126  for (DeviceCollectionConstIterator it = dataCollector->GetDeviceConstIteratorBegin(); it != dataCollector->GetDeviceConstIteratorEnd(); ++it)
127  {
128  vtkPlusDevice* plusDevice = (*it);
129  if (plusDevice->GetDeviceId().empty())
130  {
131  outErrorString = "PLUS device has a NULL id.";
132  return PLUS_FAIL;
133  }
134 
135  std::string imageIdStr(this->GetImageId()); // SLD-001
136  size_t dashFound = imageIdStr.find_last_of(DeviceNameImageIdSeparator);
137  if (dashFound == std::string::npos)
138  {
139  outErrorString = "ImageId has to contain a dash right after the deviceId.";
140  return PLUS_FAIL;
141  }
142 
143  std::string requestedDeviceId = imageIdStr.substr(0, dashFound); //SLD
144  std::string requestedImageId = imageIdStr.substr(dashFound + 1); // 001
145  std::string deviceIdStr(plusDevice->GetDeviceId()); // SLD
146  if (requestedDeviceId.compare(deviceIdStr) == 0)
147  {
148  std::string assignedImageId("");
149  if (plusDevice->GetImage(requestedImageId, assignedImageId, std::string("Ras"), imageData, ijkToRasTransform))
150  {
151  if (assignedImageId.compare(requestedImageId) != 0)
152  {
153  outErrorString = "The assignedImageId does not match requestedImageId.";
154  return PLUS_FAIL;
155  }
156 
157  vtkSmartPointer<vtkPlusCommandImageResponse> imageResponse = vtkSmartPointer<vtkPlusCommandImageResponse>::New();
158  this->CommandResponseQueue.push_back(imageResponse);
159  imageResponse->SetClientId(this->ClientId);
160  imageResponse->SetImageName(this->GetImageId());
161  imageResponse->SetImageData(imageData);
162  imageResponse->SetRespondWithCommandMessage(this->RespondWithCommandMessage);
163  imageResponse->SetImageToReferenceTransform(ijkToRasTransform);
164 
165  return PLUS_SUCCESS;
166  }
167  }
168  }
169  outErrorString = "Could not find the image.";
170  return PLUS_FAIL;
171 }
172 
173 //----------------------------------------------------------------------------
175 {
176  vtkPlusDataCollector* dataCollector = GetDataCollector();
177  if (dataCollector == NULL)
178  {
179  outErrorString = "The DataCollector is NULL.";
180  return PLUS_FAIL;
181  }
182  vtkPlusDevice* plusDevice;
183  igsioCommon::ImageMetaDataList imageMetaDataList;
184  for (DeviceCollectionConstIterator it = dataCollector->GetDeviceConstIteratorBegin(); it != dataCollector->GetDeviceConstIteratorEnd(); ++it)
185  {
186  plusDevice = (*it);
187  if (plusDevice == NULL)
188  {
189  outErrorString = "NULL device found in data collector list.";
190  return PLUS_FAIL;
191  }
192  if (plusDevice->GetDeviceId().empty())
193  {
194  outErrorString = "PLUS device has a NULL deviceId.";
195  return PLUS_FAIL;
196  }
197  igsioCommon::ImageMetaDataList imageMetaDataListDevice;
198  imageMetaDataListDevice.clear();
199  plusDevice->GetImageMetaData(imageMetaDataListDevice);
200  for (igsioCommon::ImageMetaDataList::iterator it = imageMetaDataListDevice.begin(); it != imageMetaDataListDevice.end(); it++)
201  {
202  if (it->Id.find(DeviceNameImageIdSeparator) != std::string::npos)
203  {
204  outErrorString = "DeviceId cannot contain a dash.";
205  return PLUS_FAIL;
206  }
207  it->Id = std::string(plusDevice->GetDeviceId()) + DeviceNameImageIdSeparator + (*it).Id;
208  }
209  imageMetaDataList.splice(imageMetaDataList.end(), imageMetaDataListDevice, imageMetaDataListDevice.begin(), imageMetaDataListDevice.end());
210  }
211  if (imageMetaDataList.size() == 0)
212  {
213  LOG_INFO("There are currently no images on the devices that are connected through plus.");
214  }
215 
216  vtkSmartPointer<vtkPlusCommandImageMetaDataResponse > imageMetaDataResponse = vtkSmartPointer<vtkPlusCommandImageMetaDataResponse>::New();
217  this->CommandResponseQueue.push_back(imageMetaDataResponse);
218  imageMetaDataResponse->SetClientId(this->ClientId);
219  imageMetaDataResponse->SetRespondWithCommandMessage(this->GetRespondWithCommandMessage());
220  imageMetaDataResponse->SetImageMetaDataItems(imageMetaDataList);
221  return PLUS_SUCCESS;
222 }
virtual void PrintSelf(ostream &os, vtkIndent indent)
Abstract interface for tracker and video devices.
Definition: vtkPlusDevice.h:60
std::vector< vtkPlusDevice * >::const_iterator DeviceCollectionConstIterator
Definition: vtkPlusDevice.h:48
virtual bool GetRespondWithCommandMessage()
std::string Name
const char ** errorString
Definition: phidget22.h:1270
vtkStandardNewMacro(vtkPlusGetImageCommand)
igsioStatus PlusStatus
Definition: PlusCommon.h:40
virtual void GetCommandNames(std::list< std::string > &cmdNames)
virtual std::string GetDeviceId() const
virtual PlusStatus Execute()
#define PLUS_FAIL
Definition: PlusCommon.h:43
DeviceCollectionConstIterator GetDeviceConstIteratorBegin() const
virtual PlusStatus GetImageMetaData(igsioCommon::ImageMetaDataList &imageMetaDataItems)
PlusStatus ExecuteImageMetaReply(std::string &outErrorString)
virtual void PrintSelf(ostream &os, vtkIndent indent)
DeviceCollectionConstIterator GetDeviceConstIteratorEnd() const
Manages devices that record image or positional data.
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
bool RespondWithCommandMessage
virtual PlusStatus GetImage(const std::string &requestedImageId, std::string &assignedImageId, const std::string &imageReferencFrameName, vtkImageData *imageData, vtkMatrix4x4 *ijkToReferenceTransform)
void QueueCommandResponse(PlusStatus status, const std::string &message, const std::string &error="", const igtl::MessageBase::MetaDataMap *metaData=nullptr)
virtual vtkPlusDataCollector * GetDataCollector()
PlusStatus ExecuteImageReply(std::string &outErrorString)
This command is used to answer the OpenIGTLink messages "GET_IMGMETA" and "GET_IMAGE"....
PlusCommandResponseList CommandResponseQueue
virtual std::string GetDescription(const std::string &commandName)