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)));
157 std::string metaLabel;
158 std::string metaDataValue;
159 QStringList igtlioLabels;
161 igtlioLabels << IGTLIO_KEY_PROBE_TYPE;
162 igtlioLabels << IGTLIO_KEY_ORIGIN;
163 igtlioLabels << IGTLIO_KEY_ANGLES;
164 igtlioLabels << IGTLIO_KEY_BOUNDING_BOX;
165 igtlioLabels << IGTLIO_KEY_DEPTHS;
166 igtlioLabels << IGTLIO_KEY_LINEAR_WIDTH;
167 igtlioLabels << IGTLIO_KEY_SPACING_X;
168 igtlioLabels << IGTLIO_KEY_SPACING_Y;
169 igtlioLabels << QString(
"SpacingZ");
174 for (
int i = 0; i < igtlioLabels.size(); ++i)
176 metaLabel = igtlioLabels[i].toStdString();
177 bool gotMetaData = receivedDevice->GetMetaDataElement(metaLabel, metaDataValue);
181 CX_LOG_WARNING() <<
"Cannot get needed igtlio meta information: " << metaLabel;
200 else if(device_type == igtlioTransformConverter::GetIGTLTypeName())
202 igtlioTransformDevicePointer transformDevice = igtlioTransformDevice::SafeDownCast(receivedDevice);
203 igtlioTransformConverter::ContentData content = transformDevice->GetContent();
209 Transform3D cxtransform = Transform3D::fromVtkMatrix(content.transform);
221 std::string openigtlinktransformid;
222 bool gotTransformId = receivedDevice->GetMetaDataElement(
"equipmentId", openigtlinktransformid);
227 emit
transform(deviceName, cxtransform, timestampMS);
229 else if(device_type == igtlioStatusConverter::GetIGTLTypeName())
231 igtlioStatusDevicePointer status = igtlioStatusDevice::SafeDownCast(receivedDevice);
233 igtlioStatusConverter::ContentData content = status->GetContent();
235 CX_LOG_DEBUG() <<
"STATUS: " <<
" code: " << content.code
236 <<
" subcode: " << content.subcode
237 <<
" errorname: " << content.errorname
238 <<
" statusstring: " << content.statusstring;
241 else if(device_type == igtlioStringConverter::GetIGTLTypeName())
243 igtlioStringDevicePointer
string = igtlioStringDevice::SafeDownCast(receivedDevice);
245 igtlioStringConverter::ContentData content =
string->GetContent();
251 QString message(content.string_msg.c_str());
259 CX_LOG_WARNING() <<
"Found unhandled devicetype: " << device_type;
264 void NetworkHandler::onConnectionEvent(vtkObject* caller,
void* connector,
unsigned long event ,
void*)
268 if (event==igtlioLogic::ConnectionAddedEvent)
272 if (event==igtlioLogic::ConnectionAboutToBeRemovedEvent)
278 void NetworkHandler::onDeviceAddedOrRemoved(vtkObject* caller,
void* void_device,
unsigned long event,
void* callData)
282 if (event==igtlioLogic::NewDeviceEvent)
284 igtlioDevicePointer device(reinterpret_cast<igtlioDevice*>(void_device));
287 CX_LOG_DEBUG() <<
" NetworkHandler is listening to " << device->GetDeviceName();
288 qvtkReconnect(NULL, device, igtlioDevice::ReceiveEvent,
this, SLOT(onDeviceReceived(vtkObject*,
void*,
unsigned long,
void*)));
291 if (event==igtlioLogic::RemovedDeviceEvent)
293 CX_LOG_WARNING() <<
"TODO: on remove device event, not implemented";
297 void NetworkHandler::periodicProcess()
299 mLogic->PeriodicProcess();
304 foreach(
int eventId, QList<int>()
305 << igtlioLogic::ConnectionAddedEvent
306 << igtlioLogic::ConnectionAboutToBeRemovedEvent
309 qvtkReconnect(NULL,
mLogic, eventId,
310 this, SLOT(onConnectionEvent(vtkObject*,
void*,
unsigned long,
void*)));
316 foreach(
int eventId, QList<int>()
317 << igtlioLogic::NewDeviceEvent
318 << igtlioLogic::RemovedDeviceEvent
321 qvtkReconnect(NULL,
mLogic, eventId,
322 this, SLOT(onDeviceAddedOrRemoved(vtkObject*,
void*,
unsigned long,
void*)));
331 if (probeDefinitionHaveChanged)
369 Eigen::Array3i maskDims(
mUSMask->GetDimensions());
370 unsigned char* maskPtr =
static_cast<unsigned char*
> (
mUSMask->GetScalarPointer());
371 unsigned char* imagePtr =
static_cast<unsigned char*
> (cximage->getBaseVtkImageData()->GetScalarPointer());
372 unsigned components = cximage->getBaseVtkImageData()->GetNumberOfScalarComponents();
373 unsigned dimX = maskDims[0];
374 unsigned dimY = maskDims[1];
375 for (
unsigned x = 0; x < dimX; x++)
376 for (
unsigned y = 0; y < dimY; y++)
378 unsigned pos = x + y * dimX;
379 unsigned imagePos = pos*components;
380 unsigned char maskVal = maskPtr[pos];
381 unsigned char imageVal = imagePtr[imagePos];
382 if (maskVal != 0 && imageVal <= threshold)
384 for(
unsigned i=0; i < components; ++i)
387 imagePtr[imagePos + i] = newValue;
QString qstring_cast(const T &val)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class Image > ImagePtr
igtlioSessionPointer requestConnectToServer(std::string serverHost, int serverPort=-1, IGTLIO_SYNCHRONIZATION_TYPE sync=IGTLIO_BLOCKING, double timeout_s=5)
igtlioLogicPointer mLogic
bool convertZeroesInsideSectorToOnes(ImagePtr cximage, int threshold=0, int newValue=1)
bool emitProbeDefinitionIfChanged(QString deviceName)
void string_message(QString message)
void processImageAndEmitProbeDefinition(ImagePtr cximage, QString deviceName)
double synchronizedTimestamp(double receivedTimestampSec)
igtlioSessionPointer mSession
double mTimestampOffsetMS
ProbeDefinitionFromStringMessagesPtr mProbeDefinitionFromStringMessages
bool verifyTimestamp(double ×tampMS)
Synchronize with system clock: Calculate a fixed offset, and apply this to all timestamps.
void connectToDeviceEvents()
void transform(QString devicename, Transform3D transform, double timestamp)
void probedefinition(QString devicename, ProbeDefinitionPtr definition)
void connectToConnectionEvents()
void disconnectFromServer()
boost::shared_ptr< class ProbeDefinitionFromStringMessages > ProbeDefinitionFromStringMessagesPtr
vtkImageDataPtr getMask()
NetworkHandler(igtlioLogicPointer logic)
Utility functions for drawing an US Probe sector.
bool mGotMoreThanOneImage
boost::shared_ptr< class ProbeDefinition > ProbeDefinitionPtr
ProbeDefinitionPtr mProbeDefinition
void clearTimestampSynchronization()
void image(ImagePtr image)
void setData(ProbeDefinition data)
Namespace for all CustusX production code.