Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxNetworkDataTransfer.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 #include "cxNetworkDataTransfer.h"
33 
34 #include "cxNetworkConnection.h"
35 #include "cxBoolProperty.h"
36 #include "cxProfile.h"
37 #include "cxHelperWidgets.h"
39 #include "cxViewServiceProxy.h"
41 #include "cxStringProperty.h"
42 #include "cxMesh.h"
44 #include "cxVideoServiceProxy.h"
45 #include "cxVideoSource.h"
47 #include "boost/bind.hpp"
48 
49 namespace cx {
50 
51 NetworkDataTransfer::NetworkDataTransfer(ctkPluginContext *context, NetworkConnectionHandlePtr connection, QObject* parent) :
52  QObject(parent),
53  mContext(context),
54  mOpenIGTLink(connection)
55 {
56  mOptions = profile()->getXmlSettings().descend(this->getConfigUid());
57 
58  mPatientModelService = PatientModelServiceProxy::create(context);
59  mViewService = ViewServiceProxy::create(context);
60  mVideoService = VideoServiceProxy::create(context);
61 
62 // mOpenIGTLink.reset(new NetworkConnectionHandle(this->getConfigUid()));
63 
64  connect(mOpenIGTLink->getNetworkConnection(), &NetworkConnection::image, this, &NetworkDataTransfer::onImageReceived);
65  connect(mOpenIGTLink->getNetworkConnection(), &NetworkConnection::mesh, this, &NetworkDataTransfer::onMeshReceived);
66 
67  mDataToSend = StringPropertySelectData::New(mPatientModelService);
68 
69  mAcceptIncomingData = BoolProperty::initialize("acceptIncoming", "Accept Incoming",
70  "Accept incoming data and add to Patient",
71  true, mOptions.getElement());
72 
73  mStreamActiveVideoSource = BoolProperty::initialize("stream", "Stream Video",
74  "Stream the active Video Source over the connection",
75  false, QDomNode());
76  connect(mStreamActiveVideoSource.get(), &BoolProperty::changed,
77  this, &NetworkDataTransfer::onStreamActiveVideoSourceChanged);
78 
79 }
80 
82 {
83  mOpenIGTLink.reset(); //
84 }
85 
87 {
88  return mOpenIGTLink;
89 }
90 
91 QString NetworkDataTransfer::getConfigUid() const
92 {
93  return "org.custusx.core.openigtlink.datatransfer";
94 }
95 
96 void NetworkDataTransfer::onImageReceived(ImagePtr image)
97 {
98  this->onDataReceived(image);
99 }
100 
101 void NetworkDataTransfer::onMeshReceived(MeshPtr mesh)
102 {
103  this->onDataReceived(mesh);
104 }
105 
106 void NetworkDataTransfer::onDataReceived(DataPtr data)
107 {
108  QString actionText = mAcceptIncomingData->getValue() ? "inserting" : "ignoring";
109  QString nameText = data ? data->getName() : "NULL";
110  CX_LOG_CHANNEL_INFO(this->getConfigUid()) << QString("Received image [%1] over IGTLink, %2")
111  .arg(nameText)
112  .arg(actionText);
113 
114  if (mAcceptIncomingData->getValue())
115  {
116  mPatientModelService->insertData(data);
117  mViewService->autoShowData(data);
118  }
119 }
120 
121 
123 {
124  DataPtr data = mDataToSend->getData();
125  ImagePtr image = boost::dynamic_pointer_cast<Image>(data);
126  if (image)
127  {
128  boost::function<void()> message = boost::bind(&NetworkConnection::sendImage, mOpenIGTLink->getNetworkConnection(), image);
129  mOpenIGTLink->getNetworkConnection()->invoke(message);
130  return;
131  }
132  MeshPtr mesh = boost::dynamic_pointer_cast<Mesh>(data);
133  if (mesh)
134  {
135 // // test begin
136 // IGTLinkConversionPolyData polyConverter;
137 // igtl::PolyDataMessage::Pointer msg = polyConverter.encode(mesh, pcsRAS);
138 // CX_LOG_CHANNEL_DEBUG(CX_OPENIGTLINK_CHANNEL_NAME) << "Debbuging mesh: " << data->getName();
139 
140 // MeshPtr retval = polyConverter.decode(msg, pcsRAS);
141 // mPatientModelService->insertData(retval);
142 // return;
143 // // test end
144 
145  boost::function<void()> message = boost::bind(&NetworkConnection::sendMesh, mOpenIGTLink->getNetworkConnection(), mesh);
146  mOpenIGTLink->getNetworkConnection()->invoke(message);
147  return;
148  }
149 
150  QString name = data ? data->getName() : "NULL";
151  CX_LOG_CHANNEL_INFO(this->getConfigUid()) << QString("Failed to send data %1 over igtl: Unsupported type")
152  .arg(name);
153 }
154 
155 void NetworkDataTransfer::onStreamActiveVideoSourceChanged()
156 {
157  if (mStreamActiveVideoSource->getValue())
158  {
159  this->startStream();
160  }
161  else
162  {
163  this->stopStream();
164  }
165 
166 }
167 
168 void NetworkDataTransfer::startStream()
169 {
170  if (mStreamingVideoSource)
171  {
172  CX_LOG_WARNING() << QString("Already emitting VideoSource %1 on igtl").arg(mStreamingVideoSource->getName());
173  return;
174  }
175 
176  mStreamingVideoSource = mVideoService->getActiveVideoSource();
177  connect(mStreamingVideoSource.get(), &VideoSource::newFrame,
178  this, &NetworkDataTransfer::onNewStreamFrame);
179  CX_LOG_INFO() << QString("Started emitting VideoSource %1 on igtl").arg(mStreamingVideoSource->getName());
180 }
181 
182 void NetworkDataTransfer::stopStream()
183 {
184  if (!mStreamingVideoSource)
185  return;
186 
187  CX_LOG_INFO() << QString("Stopped emitting VideoSource %1 on igtl").arg(mStreamingVideoSource->getName());
188  disconnect(mStreamingVideoSource.get(), &VideoSource::newFrame,
189  this, &NetworkDataTransfer::onNewStreamFrame);
190  mStreamingVideoSource.reset();
191 }
192 
193 void NetworkDataTransfer::onNewStreamFrame()
194 {
195  vtkImageDataPtr data = mStreamingVideoSource->getVtkImageData();
196 
197  ImagePtr image(new Image(mStreamingVideoSource->getUid()+"_snapshot",
198  data,
199  mStreamingVideoSource->getName()));
200 
201  boost::function<void()> message = boost::bind(&NetworkConnection::streamImage, mOpenIGTLink->getNetworkConnection(), image);
202  mOpenIGTLink->getNetworkConnection()->invoke(message);
203 }
204 
205 } // namespace cx
#define CX_LOG_CHANNEL_INFO(channel)
Definition: cxLogger.h:123
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:176
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
A mesh data set.
Definition: cxMesh.h:61
void image(ImagePtr image)
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService, QString typeRegexp=".*")
#define CX_LOG_INFO
Definition: cxLogger.h:111
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
NetworkDataTransfer(ctkPluginContext *context, NetworkConnectionHandlePtr connection, QObject *parent=NULL)
static VideoServicePtr create(ctkPluginContext *pluginContext)
QDomElement getElement()
return the current element
NetworkConnectionHandlePtr getOpenIGTLink()
void sendMesh(MeshPtr image)
not thread-safe: use invoke
static PatientModelServicePtr create(ctkPluginContext *pluginContext)
boost::shared_ptr< class Data > DataPtr
A volumetric data set.
Definition: cxImage.h:66
void sendImage(ImagePtr image)
not thread-safe: use invoke
void changed()
emit when the underlying data value is changed: The user interface will be updated.
boost::shared_ptr< class NetworkConnectionHandle > NetworkConnectionHandlePtr
#define CX_LOG_WARNING
Definition: cxLogger.h:113
void streamImage(ImagePtr image)
not thread-safe: use invoke
static ViewServicePtr create(ctkPluginContext *pluginContext)
boost::shared_ptr< class Mesh > MeshPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
void newFrame()
emitted when a new frame has arrived (getVtkImageData() returns something new). info/status/name/vali...
void mesh(MeshPtr image)