PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
ChrDataItem.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 __ChrDataItem_h
8 #define __ChrDataItem_h
9 
10 #include "vtkXMLDataElement.h"
11 #include "ChrSerialPacket.h"
12 
14 {
15 public:
17  {
18  m_Address = 0;
19  m_Bits = 0;
20  m_StartBit = 0;
21  m_DataType=CHR_INT16;
22  m_ScaleFactor=1.0;
23 
24  m_Value=0.0;
25  }
26 
27  PlusStatus ReadDescriptionFromXml(vtkXMLDataElement* dataItemElem)
28  {
29  PlusStatus status=PLUS_SUCCESS;
30 
31  if (GetStringValueFromChildElement("Name",m_Name,dataItemElem)!=PLUS_SUCCESS)
32  {
33  status=PLUS_FAIL;
34  }
35  if (GetUnsignedCharValueFromChildElement("Address",m_Address,dataItemElem)!=PLUS_SUCCESS)
36  {
37  status=PLUS_FAIL;
38  }
39  if (GetUnsignedCharValueFromChildElement("Bits",m_Bits,dataItemElem)!=PLUS_SUCCESS)
40  {
41  status=PLUS_FAIL;
42  }
43  if (GetUnsignedCharValueFromChildElement("Start",m_StartBit,dataItemElem)!=PLUS_SUCCESS)
44  {
45  status=PLUS_FAIL;
46  }
47  if (GetDoubleValueFromChildElement("ScaleFactor",m_ScaleFactor,dataItemElem)!=PLUS_SUCCESS)
48  {
49  status=PLUS_FAIL;
50  }
51  std::string typeStr;
52  if (GetStringValueFromChildElement("DataType",typeStr,dataItemElem)!=PLUS_SUCCESS)
53  {
54  status=PLUS_FAIL;
55  }
56  else
57  {
58  if (typeStr.compare("int16")==0)
59  {
60  m_DataType=CHR_INT16;
61  }
62  else if (typeStr.compare("uint16")==0)
63  {
64  m_DataType=CHR_UINT16;
65  }
66  else if (typeStr.compare("float")==0)
67  {
68  m_DataType=CHR_FLOAT;
69  }
70  else if (typeStr.compare("en/dis")==0)
71  {
72  m_DataType=CHR_EN_DIS;
73  }
74  else if (typeStr.compare("binary")==0)
75  {
76  m_DataType=CHR_BINARY;
77  }
78  else
79  {
80  LOG_ERROR("Unknown data type: "<<typeStr);
81  status=PLUS_FAIL;
82  }
83  }
84 
85  return status;
86  }
87 
88  int GetByteAddressFromBitAddress(unsigned int bitAddress)
89  {
90  // Data is stored in 32-bit registers.
91  // As usual on little endian systems, the high word (16-31 bits) are stored on the first two bytes in memory (dwordAddress 0-1),
92  // and low word (0-15 bits) are stored on the second two bytes in memory (dwordAddress 2-3),
93  unsigned int dwordAddress=(bitAddress/32)*4;
94  unsigned int remainingOffsetBit=bitAddress%32;
95  if (remainingOffsetBit<8)
96  {
97  return dwordAddress+2;
98  }
99  if (remainingOffsetBit<16)
100  {
101  return dwordAddress+3;
102  }
103  if (remainingOffsetBit<24)
104  {
105  return dwordAddress+0;
106  }
107  // remainingOffsetBit<32
108  return dwordAddress+1;
109  }
110 
112  {
113  switch (this->m_DataType)
114  {
115  case CHR_INT16:
116  {
117  int dataStartByteAddress=m_Address*4+GetByteAddressFromBitAddress(m_StartBit);
118  int dataEndByteAddress=m_Address*4+GetByteAddressFromBitAddress(m_StartBit+m_Bits-1);
119  int packetStartByteAddress=packet.GetAddress()*4;
120  int packetEndByteAddress=packet.GetAddress()*4+packet.GetDataLength();
121  if (dataStartByteAddress<packetStartByteAddress
122  || dataEndByteAddress>packetEndByteAddress)
123  {
124  // data is not in the packet
125  return false;
126  }
127  int intValue=int(packet.GetDataByte(dataStartByteAddress-packetStartByteAddress))*256
128  +int(packet.GetDataByte(dataStartByteAddress-packetStartByteAddress+1));
129  m_Value=intValue*m_ScaleFactor;
130  return true;
131  }
132  default:
133  LOG_ERROR("Unsupported data type: "<<this->m_DataType);
134  return false;
135  }
136  }
137 
138  double GetValue() { return m_Value; }
139 
141  {
147  };
148 
149 private:
150 
151  PlusStatus GetUnsignedCharValueFromChildElement(const std::string &childElemName, unsigned char &childElemValue, vtkXMLDataElement* dataElem)
152  {
153  vtkXMLDataElement* childElem=dataElem->FindNestedElementWithName(childElemName.c_str());
154  if (childElem==NULL)
155  {
156  LOG_ERROR("Child element name "<<childElemName<<" not found");
157  return PLUS_FAIL;
158  }
159  if (igsioCommon::StringToInt<unsigned char>(childElem->GetCharacterData(), childElemValue)!=PLUS_SUCCESS)
160  {
161  LOG_ERROR("Failed to read integer value from element "<<childElemName);
162  return PLUS_FAIL;
163  }
164  return PLUS_SUCCESS;
165  }
166 
167  PlusStatus GetDoubleValueFromChildElement(const std::string &childElemName, double &childElemValue, vtkXMLDataElement* dataElem)
168  {
169  vtkXMLDataElement* childElem=dataElem->FindNestedElementWithName(childElemName.c_str());
170  if (childElem==NULL)
171  {
172  LOG_ERROR("Child element name "<<childElemName<<" not found");
173  return PLUS_FAIL;
174  }
175  if (igsioCommon::StringToDouble(childElem->GetCharacterData(), childElemValue)!=PLUS_SUCCESS)
176  {
177  LOG_ERROR("Failed to read double value from element "<<childElemName);
178  return PLUS_FAIL;
179  }
180  return PLUS_SUCCESS;
181  }
182 
183  PlusStatus GetStringValueFromChildElement(const std::string &childElemName, std::string &childElemValue, vtkXMLDataElement* dataElem)
184  {
185  vtkXMLDataElement* childElem=dataElem->FindNestedElementWithName(childElemName.c_str());
186  if (childElem==NULL)
187  {
188  LOG_ERROR("Child element name "<<childElemName<<" not found");
189  return PLUS_FAIL;
190  }
191  const char* s=childElem->GetCharacterData();
192  if (s==NULL)
193  {
194  LOG_ERROR("Failed to string value from element "<<childElemName);
195  return PLUS_FAIL;
196  }
197  childElemValue=s;
198  return PLUS_SUCCESS;
199  }
200 
201  std::string m_Name;
202  unsigned char m_Address;
203  unsigned char m_Bits;
204  unsigned char m_StartBit;
205  CHR_DATA_TYPE m_DataType;
206  double m_ScaleFactor;
207 
208  double m_Value;
209 };
210 
211 #endif
unsigned char GetDataLength()
bool ReadValueFromPacket(ChrSerialPacket &packet)
Definition: ChrDataItem.h:111
int
Definition: phidget22.h:3069
igsioStatus PlusStatus
Definition: PlusCommon.h:40
PlusStatus ReadDescriptionFromXml(vtkXMLDataElement *dataItemElem)
Definition: ChrDataItem.h:27
#define PLUS_FAIL
Definition: PlusCommon.h:43
int GetByteAddressFromBitAddress(unsigned int bitAddress)
Definition: ChrDataItem.h:88
unsigned char GetDataByte(int index)
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
unsigned char GetAddress()
double GetValue()
Definition: ChrDataItem.h:138