16 #include "igtlioLogic.h"
17 #include "igtlioImageDevice.h"
18 #include "igtlioTransformDevice.h"
19 #include "igtlioStatusDevice.h"
20 #include "igtlioStringDevice.h"
22 #include "igtlioConnector.h"
24 #include "igtlioImageConverter.h"
25 #include "igtlioTransformConverter.h"
26 #include "igtlioCommandConverter.h"
27 #include "igtlioStatusConverter.h"
28 #include "igtlioStringConverter.h"
29 #include "igtlioUsSectorDefinitions.h"
39 mTimer(new QTimer(this)),
41 mGotTimeOffset(false),
42 mTimestampOffsetMS(0),
43 mGotMoreThanOneImage(false),
49 qRegisterMetaType<Transform3D>(
"Transform3D");
50 qRegisterMetaType<ImagePtr>(
"ImagePtr");
57 connect(
mTimer, SIGNAL(timeout()),
this, SLOT(periodicProcess()));
68 mSession =
mLogic->ConnectToServer(serverHost, serverPort, sync, timeout_s);
74 if (
mSession->GetConnector() &&
mSession->GetConnector()->GetState()!=igtlioConnector::STATE_OFF)
76 CX_LOG_DEBUG() <<
"NetworkHandler: Disconnecting from server" <<
mSession->GetConnector()->GetName();
77 igtlioConnectorPointer connector =
mSession->GetConnector();
79 mLogic->RemoveConnector(connector);
92 double receivedTimestampMS = receivedTimestampSec * 1000;
95 qint64 systemTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
103 return receivedTimestampMS;
109 qint64 latestSystemTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
110 double diff = timestampMS - latestSystemTime;
111 if(fabs(diff) > 1000)
117 timestampMS = latestSystemTime;
124 void NetworkHandler::onDeviceReceived(vtkObject* caller_device,
void* unknown,
unsigned long event ,
void*)
128 vtkSmartPointer<igtlioDevice> receivedDevice(
reinterpret_cast<igtlioDevice*
>(caller_device));
130 igtlioBaseConverter::HeaderData header = receivedDevice->GetHeader();
131 std::string device_type = receivedDevice->GetDeviceType();
141 QString deviceName(receivedDevice->GetDeviceName().c_str());
143 if(device_type == igtlioImageConverter::GetIGTLTypeName())
145 igtlioImageDevicePointer imageDevice = igtlioImageDevice::SafeDownCast(receivedDevice);
147 igtlioImageConverter::ContentData content = imageDevice->GetContent();
152 cximage->setAcquisitionTime( QDateTime::fromMSecsSinceEpoch(qint64(timestampMS)));
153 cximage->setModality(
imUS);
158 std::string metaLabel;
159 std::string metaDataValue;
160 QStringList igtlioLabels;
162 igtlioLabels << IGTLIO_KEY_PROBE_TYPE;
163 igtlioLabels << IGTLIO_KEY_ORIGIN;
164 igtlioLabels << IGTLIO_KEY_ANGLES;
165 igtlioLabels << IGTLIO_KEY_BOUNDING_BOX;
166 igtlioLabels << IGTLIO_KEY_DEPTHS;
167 igtlioLabels << IGTLIO_KEY_LINEAR_WIDTH;
168 igtlioLabels << IGTLIO_KEY_SPACING_X;
169 igtlioLabels << IGTLIO_KEY_SPACING_Y;
170 igtlioLabels << QString(
"SpacingZ");
175 for (
int i = 0; i < igtlioLabels.size(); ++i)
177 metaLabel = igtlioLabels[i].toStdString();
178 bool gotMetaData = receivedDevice->GetMetaDataElement(metaLabel, metaDataValue);
182 CX_LOG_WARNING() <<
"Cannot get needed igtlio meta information: " << metaLabel;
201 else if(device_type == igtlioTransformConverter::GetIGTLTypeName())
203 igtlioTransformDevicePointer transformDevice = igtlioTransformDevice::SafeDownCast(receivedDevice);
204 igtlioTransformConverter::ContentData content = transformDevice->GetContent();
210 Transform3D cxtransform = Transform3D::fromVtkMatrix(content.transform);
222 std::string openigtlinktransformid;
223 bool gotTransformId = receivedDevice->GetMetaDataElement(
"equipmentId", openigtlinktransformid);
228 emit
transform(deviceName, cxtransform, timestampMS);
230 else if(device_type == igtlioStatusConverter::GetIGTLTypeName())
232 igtlioStatusDevicePointer status = igtlioStatusDevice::SafeDownCast(receivedDevice);
234 igtlioStatusConverter::ContentData content = status->GetContent();
236 CX_LOG_DEBUG() <<
"STATUS: " <<
" code: " << content.code
237 <<
" subcode: " << content.subcode
238 <<
" errorname: " << content.errorname
239 <<
" statusstring: " << content.statusstring;
242 else if(device_type == igtlioStringConverter::GetIGTLTypeName())
244 igtlioStringDevicePointer
string = igtlioStringDevice::SafeDownCast(receivedDevice);
246 igtlioStringConverter::ContentData content =
string->GetContent();
252 QString message(content.string_msg.c_str());
260 CX_LOG_WARNING() <<
"Found unhandled devicetype: " << device_type;
265 void NetworkHandler::onConnectionEvent(vtkObject* caller,
void* connector,
unsigned long event ,
void*)
269 if (event==igtlioLogic::ConnectionAddedEvent)
273 if (event==igtlioLogic::ConnectionAboutToBeRemovedEvent)
279 void NetworkHandler::onDeviceAddedOrRemoved(vtkObject* caller,
void* void_device,
unsigned long event,
void* callData)
283 if (event==igtlioLogic::NewDeviceEvent)
285 igtlioDevicePointer device(
reinterpret_cast<igtlioDevice*
>(void_device));
288 CX_LOG_DEBUG() <<
" NetworkHandler is listening to " << device->GetDeviceName();
289 qvtkReconnect(NULL, device, igtlioDevice::ReceiveEvent,
this, SLOT(onDeviceReceived(vtkObject*,
void*,
unsigned long,
void*)));
292 if (event==igtlioLogic::RemovedDeviceEvent)
294 CX_LOG_WARNING() <<
"TODO: on remove device event, not implemented";
298 void NetworkHandler::periodicProcess()
300 mLogic->PeriodicProcess();
305 foreach(
int eventId, QList<int>()
306 << igtlioLogic::ConnectionAddedEvent
307 << igtlioLogic::ConnectionAboutToBeRemovedEvent
310 qvtkReconnect(NULL,
mLogic, eventId,
311 this, SLOT(onConnectionEvent(vtkObject*,
void*,
unsigned long,
void*)));
317 foreach(
int eventId, QList<int>()
318 << igtlioLogic::NewDeviceEvent
319 << igtlioLogic::RemovedDeviceEvent
322 qvtkReconnect(NULL,
mLogic, eventId,
323 this, SLOT(onDeviceAddedOrRemoved(vtkObject*,
void*,
unsigned long,
void*)));
332 if (probeDefinitionHaveChanged)
370 Eigen::Array3i maskDims(
mUSMask->GetDimensions());
371 unsigned char* maskPtr =
static_cast<unsigned char*
> (
mUSMask->GetScalarPointer());
372 unsigned char* imagePtr =
static_cast<unsigned char*
> (cximage->getBaseVtkImageData()->GetScalarPointer());
373 unsigned components = cximage->getBaseVtkImageData()->GetNumberOfScalarComponents();
374 unsigned dimX = maskDims[0];
375 unsigned dimY = maskDims[1];
376 for (
unsigned x = 0; x < dimX; x++)
377 for (
unsigned y = 0; y < dimY; y++)
379 unsigned pos = x + y * dimX;
380 unsigned imagePos = pos*components;
381 unsigned char maskVal = maskPtr[pos];
382 unsigned char imageVal = imagePtr[imagePos];
383 if (maskVal != 0 && imageVal <= threshold)
385 for(
unsigned i=0; i < components; ++i)
388 imagePtr[imagePos + i] = newValue;