1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*=========================================================================
Program: The OpenIGTLink Library
Language: C++
Web page: http://openigtlink.org/
Copyright (c) Insight Software Consortium. All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "igtlPointMessage.h"
#include "igtl_header.h"
#include "igtl_point.h"
// Disable warning C4996 (strncpy() may be unsafe) in Windows.
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <vector>
namespace igtl {
//----------------------------------------------------------------------
// igtl::PointElement class
PointElement::PointElement() : Object()
{
this->m_Name = "";
this->m_GroupName = "";
this->m_RGBA[0] = 0;
this->m_RGBA[1] = 0;
this->m_RGBA[2] = 0;
this->m_RGBA[3] = 0;
this->m_Position[0] = 0.0;
this->m_Position[1] = 0.0;
this->m_Position[2] = 0.0;
this->m_Radius = 0.0;
this->m_Owner = "";
}
PointElement::~PointElement()
{
}
int PointElement::SetName(const char* name)
{
if (strlen(name) <= IGTL_POINT_LEN_NAME)
{
this->m_Name = name;
return 1;
}
else
{
return 0;
}
}
int PointElement::SetGroupName(const char* grpname)
{
if (strlen(grpname) <= IGTL_POINT_LEN_GROUP_NAME)
{
this->m_GroupName = grpname;
return 1;
}
else
{
return 0;
}
}
void PointElement::SetRGBA(igtlUint8 rgba[4])
{
this->m_RGBA[0] = rgba[0];
this->m_RGBA[1] = rgba[1];
this->m_RGBA[2] = rgba[2];
this->m_RGBA[3] = rgba[3];
}
void PointElement::SetRGBA(igtlUint8 r, igtlUint8 g, igtlUint8 b, igtlUint8 a)
{
this->m_RGBA[0] = r;
this->m_RGBA[1] = g;
this->m_RGBA[2] = b;
this->m_RGBA[3] = a;
}
void PointElement::GetRGBA(igtlUint8* rgba)
{
rgba[0] = this->m_RGBA[0];
rgba[1] = this->m_RGBA[1];
rgba[2] = this->m_RGBA[2];
rgba[3] = this->m_RGBA[3];
}
void PointElement::GetRGBA(igtlUint8& r, igtlUint8& g, igtlUint8& b, igtlUint8& a)
{
r = this->m_RGBA[0];
g = this->m_RGBA[1];
b = this->m_RGBA[2];
a = this->m_RGBA[3];
}
void PointElement::SetPosition(igtlFloat32 position[3])
{
this->m_Position[0] = position[0];
this->m_Position[1] = position[1];
this->m_Position[2] = position[2];
}
void PointElement::SetPosition(igtlFloat32 x, igtlFloat32 y, igtlFloat32 z)
{
this->m_Position[0] = x;
this->m_Position[1] = y;
this->m_Position[2] = z;
}
void PointElement::GetPosition(igtlFloat32* position)
{
position[0] = this->m_Position[0];
position[1] = this->m_Position[1];
position[2] = this->m_Position[2];
}
void PointElement::GetPosition(igtlFloat32& x, igtlFloat32& y, igtlFloat32& z)
{
x = this->m_Position[0];
y = this->m_Position[1];
z = this->m_Position[2];
}
int PointElement::SetOwner(const char* owner)
{
if (strlen(owner) <= IGTL_POINT_LEN_OWNER)
{
this->m_Owner = owner;
return 1;
}
else
{
return 0;
}
}
//----------------------------------------------------------------------
// igtl::PointMessage class
PointMessage::PointMessage()
{
this->m_SendMessageType = "POINT";
this->m_PointList.clear();
}
PointMessage::~PointMessage()
{
}
int PointMessage::AddPointElement(PointElement::Pointer& elem)
{
this->m_PointList.push_back(elem);
return this->m_PointList.size();
}
void PointMessage::ClearPointElement()
{
this->m_PointList.clear();
}
int PointMessage::GetNumberOfPointElement()
{
return this->m_PointList.size();
}
void PointMessage::GetPointElement(int index, PointElement::Pointer& elem)
{
if (index >= 0 && index < (int)this->m_PointList.size())
{
elem = this->m_PointList[index];
}
}
int PointMessage::CalculateContentBufferSize()
{
// The content size is the sum of the header size and status message size.
return IGTL_POINT_ELEMENT_SIZE * this->m_PointList.size();
}
int PointMessage::PackContent()
{
// Allocate buffer
AllocateBuffer();
igtl_point_element* element;
element = (igtl_point_element*)(this->m_Content);
igtl_point_element* elementHolder = element;
std::vector<PointElement::Pointer>::iterator iter;
for (iter = this->m_PointList.begin(); iter != this->m_PointList.end(); iter ++)
{
strncpy((char*)element->name, (*iter)->GetName(), IGTL_POINT_LEN_NAME);
strncpy((char*)element->group_name, (*iter)->GetGroupName(), IGTL_POINT_LEN_GROUP_NAME);
igtlUint8 rgba[4];
(*iter)->GetRGBA(rgba);
element->rgba[0] = rgba[0];
element->rgba[1] = rgba[1];
element->rgba[2] = rgba[2];
element->rgba[3] = rgba[3];
igtlFloat32 position[3];
(*iter)->GetPosition(position);
element->position[0] = position[0];
element->position[1] = position[1];
element->position[2] = position[2];
element->radius = (*iter)->GetRadius();
strncpy((char*)element->owner, (*iter)->GetOwner(), IGTL_POINT_LEN_OWNER);
element ++;
}
igtl_point_convert_byte_order(elementHolder, this->m_PointList.size());
return 1;
}
int PointMessage::UnpackContent()
{
this->m_PointList.clear();
igtl_point_element* element = NULL;
int nElement = 0;
#if OpenIGTLink_HEADER_VERSION >= 2
element = (igtl_point_element*) (this->m_Content);
nElement = igtl_point_get_data_n(CalculateReceiveContentSize());
#elif OpenIGTLink_PROTOCOL_VERSION <=2
element = (igtl_point_element*) this->m_Body;
nElement = igtl_point_get_data_n(this->m_BodySizeToRead);
#endif
igtl_point_convert_byte_order(element, nElement);
char strbuf[128];
for (int i = 0; i < nElement; i ++)
{
PointElement::Pointer elemClass = PointElement::New();
// Add '\n' at the end of each string
// (necessary for a case, where a string reaches the maximum length.)
strbuf[IGTL_POINT_LEN_NAME] = '\n';
strncpy(strbuf, (char*)element->name, IGTL_POINT_LEN_NAME);
elemClass->SetName((const char*)strbuf);
strbuf[IGTL_POINT_LEN_GROUP_NAME] = '\n';
strncpy(strbuf, (char*)element->group_name, IGTL_POINT_LEN_GROUP_NAME);
elemClass->SetGroupName(strbuf);
elemClass->SetRGBA(element->rgba);
elemClass->SetPosition(element->position);
elemClass->SetRadius(element->radius);
strbuf[IGTL_POINT_LEN_OWNER] = '\n';
strncpy(strbuf, (char*)element->owner, IGTL_POINT_LEN_OWNER);
elemClass->SetOwner(strbuf);
this->m_PointList.push_back(elemClass);
element ++;
}
return 1;
}
} // namespace igtl
|