PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusGetUsParameterCommand.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"
9 
10 #include "vtkPlusDataCollector.h"
11 #include "vtkObjectFactory.h"
12 #include "vtkPlusChannel.h"
15 #include "vtkPlusUsDevice.h"
16 
17 #include <vtkVariant.h>
18 #include <vtkSmartPointer.h>
19 
20 #include <limits>
21 #include <iterator>
22 #include <string>
23 
24 namespace
25 {
26  static const std::string GET_US_PARAMETER_CMD = "GetUsParameter";
27 }
28 
30 
31 //----------------------------------------------------------------------------
33 {
34  this->UsDeviceId = "";
35  this->RequestedParameters.clear();
36 }
37 
38 //----------------------------------------------------------------------------
40 {
41  this->RequestedParameters.clear();
42 }
43 
44 //----------------------------------------------------------------------------
46 {
47  SetName(GET_US_PARAMETER_CMD);
48 }
49 
50 //----------------------------------------------------------------------------
51 void vtkPlusGetUsParameterCommand::PrintSelf(ostream& os, vtkIndent indent)
52 {
53  this->Superclass::PrintSelf(os, indent);
54 }
55 
56 //----------------------------------------------------------------------------
57 void vtkPlusGetUsParameterCommand::GetCommandNames(std::list<std::string>& cmdNames)
58 {
59  cmdNames.clear();
60  cmdNames.push_back(GET_US_PARAMETER_CMD);
61 }
62 
63 //----------------------------------------------------------------------------
64 std::string vtkPlusGetUsParameterCommand::GetDescription(const std::string& commandName)
65 {
66  std::string desc;
67  if (commandName.empty() || igsioCommon::IsEqualInsensitive(commandName, GET_US_PARAMETER_CMD))
68  {
69  desc += GET_US_PARAMETER_CMD;
70  //TODO:
71  desc += ": Get ultrasound image parameter. Attributes: UsDeviceId: ID of the ultrasound device.";
72  }
73 
74  return desc;
75 }
76 
77 //----------------------------------------------------------------------------
79 {
80  this->RequestedParameters.clear();
82  {
83  return PLUS_FAIL;
84  }
85 
86  this->SetUsDeviceId(aConfig->GetAttribute("UsDeviceId"));
87 
88  // Parse nested elements and store requested parameter changes
89  for (int elemIndex = 0; elemIndex < aConfig->GetNumberOfNestedElements(); ++elemIndex)
90  {
91  vtkXMLDataElement* currentElem = aConfig->GetNestedElement(elemIndex);
92  if (igsioCommon::IsEqualInsensitive(currentElem->GetName(), "Parameter"))
93  {
94  const char* parameterName = currentElem->GetAttribute("Name");
95  if (!parameterName)
96  {
97  LOG_ERROR("Unable to find required Name attribute in " << (currentElem->GetName() ? currentElem->GetName() : "(undefined)") << " element in GetUsParameter command");
98  continue;
99  }
100 
101  this->RequestedParameters.push_back(parameterName);
102  }
103  }
104 
105  return PLUS_SUCCESS;
106 }
107 
108 //----------------------------------------------------------------------------
110 {
112  {
113  return PLUS_FAIL;
114  }
115 
116  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(UsDeviceId, aConfig);
117 
118  // Write parameters as nested elements
119  std::vector<std::string>::iterator paramIt;
120  for (paramIt = this->RequestedParameters.begin(); paramIt != this->RequestedParameters.end(); ++paramIt)
121  {
122  vtkSmartPointer<vtkXMLDataElement> paramElem = vtkSmartPointer<vtkXMLDataElement>::New();
123  paramElem->SetName("Parameter");
124  paramElem->SetAttribute("Name", paramIt->c_str());
125  aConfig->AddNestedElement(paramElem);
126  }
127 
128  return PLUS_SUCCESS;
129 }
130 
131 //----------------------------------------------------------------------------
133 {
134  LOG_DEBUG("vtkPlusGetUsParameterCommand::Execute: " << (!this->Name.empty() ? this->Name : "(undefined)")
135  << ", device: " << (this->UsDeviceId.empty() ? "(undefined)" : this->UsDeviceId));
136 
137  if (this->Name.empty())
138  {
139  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "No command name specified.");
140  return PLUS_FAIL;
141  }
142  else if (!igsioCommon::IsEqualInsensitive(this->Name, GET_US_PARAMETER_CMD))
143  {
144  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "Unknown command name: " + this->Name + ".");
145  return PLUS_FAIL;
146  }
147 
148  vtkPlusUsDevice* usDevice = GetUsDevice();
149  if (usDevice == NULL)
150  {
151  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", std::string("Device ")
152  + (this->UsDeviceId.empty() ? "(undefined)" : this->UsDeviceId) + std::string(" is not found."));
153  return PLUS_FAIL;
154  }
155 
156  std::map < std::string, std::pair<IANA_ENCODING_TYPE, std::string> > metaData;
157 
158  vtkPlusUsImagingParameters* imagingParameters = usDevice->GetImagingParameters();
159  std::string resultString = "<CommandReply>";
160  std::string error = "";
161  PlusStatus status = PLUS_SUCCESS;
162 
163  std::vector<std::string>::iterator paramIt;
164  for (paramIt = this->RequestedParameters.begin(); paramIt != this->RequestedParameters.end(); ++paramIt)
165  {
166  std::string parameterName = *paramIt;
167  resultString += "<Parameter Name=\"" + parameterName + "\"";
168 
169  if (parameterName == vtkPlusUsImagingParameters::KEY_TGC)
170  {
171  if (imagingParameters->IsSet(vtkPlusUsImagingParameters::KEY_TGC))
172  {
173  std::stringstream ss;
174  std::vector<double> numbers = imagingParameters->GetTimeGainCompensation();
175  for (std::vector<double>::iterator numberIt = numbers.begin(); numberIt != numbers.end(); numberIt++)
176  {
177  ss << *numberIt << " ";
178  }
179  resultString += " Success=\"true\"";
180  resultString += " Value=\"" + ss.str() + "\"";
181  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, ss.str());
182  }
183  else
184  {
185  resultString += " Success=\"false\"";
186  error += parameterName + " is not set. ";
187  status = PLUS_FAIL;
188  }
189  }
190  else if (parameterName == vtkPlusUsImagingParameters::KEY_IMAGESIZE)
191  {
192  if (imagingParameters->IsSet(vtkPlusUsImagingParameters::KEY_IMAGESIZE))
193  {
194  std::stringstream ss;
195  FrameSizeType imageSize = imagingParameters->GetImageSize();
196  for (FrameSizeType::iterator imageSizeIt = imageSize.begin(); imageSizeIt != imageSize.end(); ++imageSizeIt)
197  {
198  ss << *imageSizeIt << " ";
199  }
200  resultString += " Success=\"true\"";
201  resultString += " Value=\"" + ss.str() + "\"";
202  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, ss.str());
203  }
204  else
205  {
206  resultString += " Success=\"false\"";
207  error += parameterName + " is not set. ";
208  status = PLUS_FAIL;
209  }
210  }
211  else if (parameterName == vtkPlusUsImagingParameters::KEY_FREQUENCY
212  || parameterName == vtkPlusUsImagingParameters::KEY_DEPTH
214  || parameterName == vtkPlusUsImagingParameters::KEY_SECTOR
215  || parameterName == vtkPlusUsImagingParameters::KEY_GAIN
217  || parameterName == vtkPlusUsImagingParameters::KEY_CONTRAST
218  || parameterName == vtkPlusUsImagingParameters::KEY_POWER
219  || parameterName == vtkPlusUsImagingParameters::KEY_DYNRANGE
220  || parameterName == vtkPlusUsImagingParameters::KEY_ZOOM
222  || parameterName == vtkPlusUsImagingParameters::KEY_VOLTAGE)
223  {
224  if (imagingParameters->IsSet(parameterName))
225  {
226  // double type parameter
227  double value = 0;
228  if (imagingParameters->GetValue<double>(parameterName, value) == PLUS_SUCCESS)
229  {
230  std::stringstream ss;
231  ss << value;
232  resultString += " Success=\"true\"";
233  resultString += " Value=\"" + ss.str() + "\"";
234  metaData[parameterName] = std::make_pair(IANA_TYPE_US_ASCII, ss.str());
235  }
236  }
237  else
238  {
239  resultString += " Success=\"false\"";
240  error += parameterName + " is not set. ";
241  status = PLUS_FAIL;
242  }
243  }
244  else
245  {
246  error += "Invalid parameter " + parameterName + ". ";
247  resultString += " Success=\"false\"";
248  status = PLUS_FAIL;
249  }
250  resultString += "/>";
251 
252  } // For each parameter
253  resultString += "</CommandReply>";
254 
255  if (status != PLUS_SUCCESS)
256  {
257  LOG_WARNING("Failed to get US parameter, result string was: " << resultString);
258  }
259 
260  vtkSmartPointer<vtkPlusCommandRTSCommandResponse> commandResponse = vtkSmartPointer<vtkPlusCommandRTSCommandResponse>::New();
261  commandResponse->UseDefaultFormatOff();
262  commandResponse->SetClientId(this->ClientId);
263  commandResponse->SetOriginalId(this->Id);
264  commandResponse->SetDeviceName(this->DeviceName);
265  commandResponse->SetCommandName(this->GetName());
266  commandResponse->SetStatus(status);
267  commandResponse->SetRespondWithCommandMessage(this->RespondWithCommandMessage);
268  commandResponse->SetErrorString(error);
269  commandResponse->SetResultString(resultString);
270  commandResponse->SetParameters(metaData);
271  this->CommandResponseQueue.push_back(commandResponse);
272 
273  return status;
274 }
275 
276 //----------------------------------------------------------------------------
278 {
279  vtkPlusDataCollector* dataCollector = GetDataCollector();
280  if (dataCollector == NULL)
281  {
282  LOG_ERROR("Data collector is invalid");
283  return NULL;
284  }
285  vtkPlusUsDevice* usDevice = NULL;
286  if (!this->UsDeviceId.empty())
287  {
288  // Ultrasound device ID is specified
289  vtkPlusDevice* device = NULL;
290  if (dataCollector->GetDevice(device, this->UsDeviceId) != PLUS_SUCCESS)
291  {
292  LOG_ERROR("No ultrasound device has been found by the name " << this->UsDeviceId);
293  return NULL;
294  }
295  // device found
296  usDevice = dynamic_cast<vtkPlusUsDevice*>(device);
297  if (usDevice == NULL)
298  {
299  // wrong type
300  LOG_ERROR("The specified device " << this->UsDeviceId << " is not UsDevice");
301  return NULL;
302  }
303  }
304  else
305  {
306  // No ultrasound device id is specified, auto-detect the first one and use that
307  for (DeviceCollectionConstIterator it = dataCollector->GetDeviceConstIteratorBegin(); it != dataCollector->GetDeviceConstIteratorEnd(); ++it)
308  {
309  usDevice = dynamic_cast<vtkPlusUsDevice*>(*it);
310  if (usDevice != NULL)
311  {
312  // found an ultrasound device
313  break;
314  }
315  }
316  if (usDevice == NULL)
317  {
318  LOG_ERROR("No UsDevice has been found");
319  return NULL;
320  }
321  }
322  return usDevice;
323 }
virtual vtkPlusUsImagingParameters * GetImagingParameters()
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 std::string GetDescription(const std::string &commandName)
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *aConfig)
This class is used to store a configuration of the imaging parameters of an ultrasound video device....
PlusStatus GetDevice(vtkPlusDevice *&aDevice, const std::string &aDeviceId) const
std::string Name
This command requests ultrasound parameter change in the client.
igsioStatus PlusStatus
Definition: PlusCommon.h:40
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
PlusStatus GetValue(const std::string &paramName, T &outputValue) const
#define PLUS_FAIL
Definition: PlusCommon.h:43
virtual void PrintSelf(ostream &os, vtkIndent indent)
DeviceCollectionConstIterator GetDeviceConstIteratorBegin() const
PlusStatus GetTimeGainCompensation(std::vector< double > &tgc) const
DeviceCollectionConstIterator GetDeviceConstIteratorEnd() const
virtual void GetCommandNames(std::list< std::string > &cmdNames)
Manages devices that record image or positional data.
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
bool RespondWithCommandMessage
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *aConfig)
void QueueCommandResponse(PlusStatus status, const std::string &message, const std::string &error="", const igtl::MessageBase::MetaDataMap *metaData=nullptr)
virtual vtkPlusDataCollector * GetDataCollector()
PlusStatus GetImageSize(FrameSizeType &imageSize) const
std::string DeviceName
const char const char * value
Definition: phidget22.h:5111
vtkStandardNewMacro(vtkPlusGetUsParameterCommand)
PlusCommandResponseList CommandResponseQueue
bool IsSet(const std::string &paramName) const
Abstract interface for ultrasound video devices.
std::vector< std::string > RequestedParameters