PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusParameters.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 "vtkPlusParameters.h"
9 
10 #include <iterator>
11 
12 //----------------------------------------------------------------------------
14 
15 //----------------------------------------------------------------------------
16 
17 const char* vtkPlusParameters::XML_ELEMENT_TAG = "Parameters";
18 const char* vtkPlusParameters::PARAMETER_TAG = "Parameter";
19 const char* vtkPlusParameters::NAME_TAG = "Name";
20 const char* vtkPlusParameters::VALUE_TAG = "Value";
21 
22 //----------------------------------------------------------------------------
24  : vtkObject()
25 {
26 }
27 
28 //----------------------------------------------------------------------------
30 {
31  LOG_TRACE("vtkPlusParameters::~vtkPlusParameters()");
32 }
33 
34 // Check
35 //----------------------------------------------------------------------------
37 {
38  return this->Parameters.begin();
39 }
40 
41 //----------------------------------------------------------------------------
43 {
44  return this->Parameters.end();
45 }
46 
47 //----------------------------------------------------------------------------
48 void vtkPlusParameters::PrintSelf(ostream& os, vtkIndent indent)
49 {
50  Superclass::PrintSelf(os, indent);
51 
52  for (ParameterMap::iterator it = this->Parameters.begin(); it != this->Parameters.end(); ++it)
53  {
54  if (it->second.Set == true)
55  {
56  os << indent << it->first << ": " << it->second.Value
57  << (it->second.Pending ? " (pending)" : "") << std::endl;
58  }
59  }
60 }
61 
62 //-----------------------------------------------------------------------------
63 PlusStatus vtkPlusParameters::ReadConfiguration(vtkXMLDataElement* deviceConfig)
64 {
65  vtkXMLDataElement* parameterList(NULL);
66  for (int i = 0; i < deviceConfig->GetNumberOfNestedElements(); ++i)
67  {
68  vtkXMLDataElement* element = deviceConfig->GetNestedElement(i);
69  if (STRCASECMP(element->GetName(), this->GetXMLElementName()) == 0)
70  {
71  parameterList = element;
72  break;
73  }
74  }
75 
76  if (parameterList == NULL)
77  {
78  LOG_ERROR("Unable to locate " << this->GetXMLElementName() << " tag in device config.Unable to read imaging parameters.Device defaults will probably be used.");
79  return PLUS_FAIL;
80  }
81 
82  for (int i = 0; i < parameterList->GetNumberOfNestedElements(); ++i)
83  {
84  vtkXMLDataElement* element = parameterList->GetNestedElement(i);
85  std::string name = element->GetAttribute(NAME_TAG) ? element->GetAttribute(NAME_TAG) : "";
86  std::string value = element->GetAttribute(VALUE_TAG) ? element->GetAttribute(VALUE_TAG) : "";
87  if (name.empty())
88  {
89  continue;
90  }
91 
92  if (this->Parameters[name].Value != value)
93  {
94  // If the value changed, then mark it pending
95  this->Parameters[name].Pending = true;
96  }
97  this->Parameters[name].Value = value;
98  this->Parameters[name].Set = true;
99  }
100 
101  return PLUS_SUCCESS;
102 }
103 
104 //-----------------------------------------------------------------------------
105 PlusStatus vtkPlusParameters::WriteConfiguration(vtkXMLDataElement* deviceConfig)
106 {
107  /* Create a sub node, populate it with entries of the form
108  <device ...>
109  <Parameters>
110  <Parameter name="DepthMm" value="55"/>
111  <Parameter name="FrequencyMhz" value="12.5"/>
112  </Parameters>
113  ...
114  </device>
115  */
116 
117  XML_FIND_NESTED_ELEMENT_CREATE_IF_MISSING(parameterList, deviceConfig, this->GetXMLElementName());
118 
119  // Clear the list before writing new elements
120  parameterList->RemoveAllNestedElements();
121 
122  for (ParameterMap::iterator it = this->Parameters.begin(); it != this->Parameters.end(); ++it)
123  {
124  if (it->second.Set == false)
125  {
126  // Don't write out parameters that are defaults
127  continue;
128  }
129 
130  vtkSmartPointer<vtkXMLDataElement> parameter = vtkSmartPointer<vtkXMLDataElement>::New();
131  parameter->SetName(PARAMETER_TAG);
132  parameter->SetAttribute(NAME_TAG, it->first.c_str());
133  parameter->SetAttribute(VALUE_TAG, it->second.Value.c_str());
134 
135  parameterList->AddNestedElement(parameter);
136  }
137 
138  return PLUS_SUCCESS;
139 }
140 
141 //-----------------------------------------------------------------------------
142 bool vtkPlusParameters::IsSet(const std::string& paramName) const
143 {
144  ParameterMapConstIterator keyIt = this->Parameters.find(paramName);
145  if (keyIt != this->Parameters.end())
146  {
147  return keyIt->second.Set;
148  }
149 
150  LOG_ERROR("Invalid key request sent to vtkPlusParameters::IsSet -- " << paramName);
151  return false;
152 }
153 
154 //-----------------------------------------------------------------------------
155 bool vtkPlusParameters::IsPending(const std::string& paramName) const
156 {
157  ParameterMapConstIterator keyIt = this->Parameters.find(paramName);
158  if (keyIt != this->Parameters.end())
159  {
160  return keyIt->second.Pending;
161  }
162 
163  LOG_ERROR("Invalid key request sent to vtkPlusParameters::IsPending -- " << paramName);
164  return false;
165 }
166 
167 //-----------------------------------------------------------------------------
168 PlusStatus vtkPlusParameters::SetPending(const std::string& paramName, bool pending)
169 {
170  ParameterMapIterator keyIt = this->Parameters.find(paramName);
171  if (keyIt != this->Parameters.end())
172  {
173  if (keyIt->second.Set)
174  {
175  keyIt->second.Pending = pending;
176  return PLUS_SUCCESS;
177  }
178  else
179  {
180  LOG_ERROR("Pending status cannot be set for unset parameter -- " << paramName);
181  return PLUS_FAIL;
182  }
183  }
184 
185  LOG_ERROR("Invalid key request sent to vtkPlusParameters::SetPending -- " << paramName);
186  return PLUS_FAIL;
187 }
188 
189 //-----------------------------------------------------------------------------
191 {
192  for (ParameterMapConstIterator it = otherParameters.Parameters.begin(); it != otherParameters.Parameters.end(); ++it)
193  {
194  if (this->Parameters[it->first].Value != it->second.Value)
195  {
196  // If the value changed, then mark it pending
197  this->Parameters[it->first].Pending = true;
198  }
199  this->Parameters[it->first].Value = it->second.Value;
200  this->Parameters[it->first].Set = it->second.Set;
201  }
202 
203  return PLUS_SUCCESS;
204 }
ParameterMapConstIterator begin() const
ParameterMap::iterator ParameterMapIterator
virtual PlusStatus DeepCopy(const vtkPlusParameters &otherParameters)
bool IsSet(const std::string &paramName) const
This class is used as the basis to store a configuration of generic parameters for any device....
igsioStatus PlusStatus
Definition: PlusCommon.h:40
static const char * NAME_TAG
for i
#define PLUS_FAIL
Definition: PlusCommon.h:43
static const char * XML_ELEMENT_TAG
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *deviceConfig)
ParameterMap Parameters
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
ParameterMap::const_iterator ParameterMapConstIterator
bool IsPending(const std::string &paramName) const
const char const char * value
Definition: phidget22.h:5111
ParameterMapConstIterator end() const
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *deviceConfig)
static const char * VALUE_TAG
static const char * PARAMETER_TAG
virtual const char * GetXMLElementName()
vtkStandardNewMacro(vtkPlusParameters)
PlusStatus SetPending(const std::string &paramName, bool pending)