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
|
/*=========================================================================
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 "igtlSensorMessage.h"
#include "igtl_header.h"
#include "igtl_sensor.h"
// Disable warning C4996 (strncpy() may be unsafe) in Windows.
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
namespace igtl {
SensorMessage::SensorMessage():
MessageBase()
{
m_SendMessageType = "SENSOR";
this->m_Length = 0;
this->m_Status = 0;
this->m_Unit = 0;
this->m_Array.clear();
}
SensorMessage::~SensorMessage()
{
}
int SensorMessage::SetLength(unsigned int n)
{
if (n <= 256)
{
this->m_Length = n;
this->m_Array.resize(n);
}
return (int) this->m_Length;
}
unsigned int SensorMessage::GetLength()
{
return this->m_Length;
}
int SensorMessage::SetUnit(igtlUnit unit)
{
this->m_Unit = unit;
return 1;
}
int SensorMessage::SetUnit(igtl::Unit * unit)
{
this->m_Unit = unit->Pack();
return 1;
}
igtlUnit SensorMessage::GetUnit()
{
return this->m_Unit;
}
int SensorMessage::GetUnit(igtl::Unit * unit)
{
return unit->Unpack(this->m_Unit);
}
int SensorMessage::SetValue(igtlFloat64 * data)
{
for (int i = 0; i < this->m_Length; i ++)
{
this->m_Array[i] = data[i];
}
return 1;
}
int SensorMessage::SetValue(unsigned int i, igtlFloat64 value)
{
if (i < this->m_Length)
{
this->m_Array[i] = value;
return 1;
}
return 0;
}
igtlFloat64 SensorMessage::GetValue(unsigned int i)
{
if (i < this->m_Length)
{
return this->m_Array[i];
}
else
{
return 0.0;
}
}
int SensorMessage::CalculateContentBufferSize()
{
// Body pack size is the sum of LARRAY, STATUS, UNIT and DATA
return sizeof(igtlUint8)*2 + sizeof(igtlUnit) + sizeof(igtlFloat64)*this->m_Length;
}
int SensorMessage::PackContent()
{
// Allocate buffer
AllocateBuffer();
// Set pointers
igtl_sensor_header * sensor_header;
igtl_float64 * data;
sensor_header = (igtl_sensor_header *) (this->m_Content);
data = (igtl_float64 *) (this->m_Content + sizeof(igtl_sensor_header));
// Copy data
sensor_header->larray = this->m_Length;
sensor_header->status = this->m_Status;
sensor_header->unit = this->m_Unit;
for (int i = 0; i < this->m_Length; i ++)
{
data[i] = this->m_Array[i];
}
// Convert byte order from local to network
igtl_sensor_convert_byte_order(sensor_header, data);
return 1;
}
int SensorMessage::UnpackContent()
{
// Set pointers
igtl_sensor_header * sensor_header;
igtl_float64 * data;
sensor_header = (igtl_sensor_header *) (this->m_Content);
data = (igtl_float64 *) (this->m_Content + sizeof(igtl_sensor_header));
// Convert byte order from local to network
igtl_sensor_convert_byte_order(sensor_header, data);
// Copy data
this->m_Length = sensor_header->larray;
this->m_Status = sensor_header->status;
this->m_Unit = sensor_header->unit;
this->m_Array.resize(this->m_Length);
for (int i = 0; i < this->m_Length; i ++)
{
this->m_Array[i] = data[i];
}
return 1;
}
} // namespace igtl
|