MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
OpenIGTLinkProviderExample.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
13
14// Blueberry
15#include <berryISelectionService.h>
16#include <berryIWorkbenchWindow.h>
17
18// Qmitk
19#include "QmitkRenderWindow.h"
20
21// Qt
22#include <QMessageBox>
23#include <QFileDialog>
24
25// mitk
26#include <mitkImage.h>
27#include <mitkSurface.h>
28//#include <mitkIGTLMessageToNavigationDataFilter.h>
29#include <mitkNodePredicateNot.h>
30#include <mitkNodePredicateProperty.h>
31#include <mitkIOUtil.h>
32
33// vtk
34#include <vtkSphereSource.h>
35
36//
38
39//igtl
40#include "igtlStringMessage.h"
41
42
44 "org.mitk.views.OpenIGTLinkProviderExample";
45
47{
48 this->DestroyPipeline();
49 if (m_IGTLMessageProvider.IsNotNull())
50 {
51 m_IGTLMessageProvider->UnRegisterMicroservice();
52 }
53 if (m_NavDataToIGTLMsgFilter.IsNotNull())
54 {
55 m_NavDataToIGTLMsgFilter->UnRegisterMicroservice();
56 }
57}
58
62
64{
65 // create GUI widgets from the Qt Designer's .ui file
66 m_Controls.setupUi( parent );
67
68 // connect the widget items with the methods
69 connect( m_Controls.butStart, SIGNAL(clicked()),
70 this, SLOT(Start()) );
71 connect( m_Controls.butOpenNavData, SIGNAL(clicked()),
72 this, SLOT(OnOpenFile()) );
73 connect( &m_VisualizerTimer, SIGNAL(timeout()),
74 this, SLOT(UpdateVisualization()));
75
76
77 //create a new OpenIGTLink Client
78 m_IGTLServer = mitk::IGTLServer::New(false);
79 m_IGTLServer->SetName("OIGTL Provider Example Device");
80
81 //create a new OpenIGTLink Device source
82 m_IGTLMessageProvider = mitk::IGTLMessageProvider::New();
83
84 //set the OpenIGTLink server as the source for the device source
85 m_IGTLMessageProvider->SetIGTLDevice(m_IGTLServer);
86
87 //register the provider so that it can be configured with the IGTL manager
88 //plugin. This could be hardcoded but now I already have the fancy plugin.
89 m_IGTLMessageProvider->RegisterAsMicroservice();
90}
91
93{
94 //create a navigation data player object that will play nav data from a
95 //recorded file
96 m_NavDataPlayer = mitk::NavigationDataSequentialPlayer::New();
97
98 //set the currently read navigation data set
99 m_NavDataPlayer->SetNavigationDataSet(m_NavDataSet);
100
101 //create a filter that converts navigation data into IGTL messages
102 m_NavDataToIGTLMsgFilter = mitk::NavigationDataToIGTLMessageFilter::New();
103
104 //connect the filters with each other
105 //the navigation data player reads a file with recorded navigation data,
106 //passes this data to a filter that converts it into a IGTLMessage.
107 //The provider is not connected because it will search for fitting services.
108 //Once it found the filter it will automatically connect to it.
110
111 //define the operation mode for this filter, we want to send tracking data
112 //messages
113 m_NavDataToIGTLMsgFilter->SetOperationMode(
115 // mitk::NavigationDataToIGTLMessageFilter::ModeSendTransMsg);
116
117 //set the name of this filter to identify it easier
118 m_NavDataToIGTLMsgFilter->SetName("Tracking Data Source From Example");
119
120 //register this filter as micro service. The message provider looks for
121 //provided IGTLMessageSources, once it found this microservice and someone
122 //requested this data type then the provider will connect with this filter
123 //automatically.
124 m_NavDataToIGTLMsgFilter->RegisterAsMicroservice();
125
126 //also create a visualize filter to visualize the data
127 m_NavDataVisualizer = mitk::NavigationDataObjectVisualizationFilter::New();
128
129 //create an object that will be moved respectively to the navigation data
130 for (size_t i = 0; i < m_NavDataPlayer->GetNumberOfIndexedOutputs(); i++)
131 {
132 mitk::DataNode::Pointer newNode = mitk::DataNode::New();
133 QString name("DemoNode IGTLProviderExmpl T");
134 name.append(QString::number(i));
135 newNode->SetName(name.toStdString());
136
137 //create small sphere and use it as surface
138 mitk::Surface::Pointer mySphere = mitk::Surface::New();
140 vtkData->SetRadius(2.0f);
141 vtkData->SetCenter(0.0, 0.0, 0.0);
142 vtkData->Update();
143 mySphere->SetVtkPolyData(vtkData->GetOutput());
144 newNode->SetData(mySphere);
145
146 this->GetDataStorage()->Add(newNode);
147
148 m_NavDataVisualizer->SetRepresentationObject(i, mySphere.GetPointer());
149
150 m_DemoNodes.append(newNode);
151 }
152
153 //connect the visualization with the navigation data player
155
156 //start the player
157 this->Start();
158
159 //resize the boundaries
160 this->m_NavDataVisualizer->Update();
161 this->ResizeBoundingBox();
162}
163
165{
166 if (m_NavDataPlayer.IsNotNull())
167 {
168 //m_NavDataPlayer->StopPlaying();
169 }
170 foreach(mitk::DataNode::Pointer node, m_DemoNodes)
171 {
172 this->GetDataStorage()->Remove(node);
173 }
174 this->m_DemoNodes.clear();
175}
176
178{
179 if ( this->m_Controls.butStart->text().contains("Start") )
180 {
181 m_NavDataPlayer->SetRepeat(true);
182 //m_NavDataPlayer->StartPlaying();
183 this->m_Controls.butStart->setText("Stop Playing Recorded Navigation Data ");
184
185 //start the visualization
186 double fps = m_Controls.m_updateRate->value();
187 double millisecondsPerFrame = 1 / fps * 1000;
188
189 this->m_VisualizerTimer.start(millisecondsPerFrame);
190 }
191 else
192 {
193 //m_NavDataPlayer->StopPlaying();
194 this->m_Controls.butStart->setText("Start Playing Recorded Navigation Data ");
195
196 //stop the visualization
197 this->m_VisualizerTimer.stop();
198 }
199}
200
202
203 // FIXME Filter for correct files and use correct Reader
204 QString filter = tr("NavigationData File (*.csv *.xml)");
205 QString fileName = QFileDialog::getOpenFileName(nullptr, tr("Open NavigationData Set"), "", filter);
206
207 if ( fileName.isNull() ) { return; } // user pressed cancel
208
209 try
210 {
211 m_NavDataSet = dynamic_cast<mitk::NavigationDataSet*> (mitk::IOUtil::Load(fileName.toStdString())[0].GetPointer());
212 }
213 catch ( const mitk::Exception &e )
214 {
215 MITK_WARN("NavigationDataPlayerView") << "could not open file " << fileName.toStdString();
216 QMessageBox::critical(nullptr, "Error Reading File", "The file '" + fileName
217 +"' could not be read.\n" + e.GetDescription() );
218 return;
219 }
220
221 this->m_Controls.butStart->setEnabled(true);
222
223 //Setup the pipeline
224 this->CreatePipeline();
225
226 // Update Labels
227// m_Controls->m_LblFilePath->setText(fileName);
228// m_Controls->m_LblTools->setText(QString::number(m_NavDataSet->GetNumberOfTools()));
229}
230
232{
233 this->m_NavDataPlayer->GoToNextSnapshot();
234 if (this->m_Controls.visualizeCheckBox->isChecked())
235 {
236 //update the filter
237 this->m_NavDataVisualizer->Update();
238
240 //mitk::RenderingManager::GetInstance()->InitializeViewsByBoundingObjects(this->GetDataStorage());
241
242 //update rendering
243 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
244 }
245}
246
251{
252 // get all nodes
253 mitk::DataStorage::SetOfObjects::ConstPointer rs = this->GetDataStorage()->GetAll();
254 auto bounds = this->GetDataStorage()->ComputeBoundingGeometry3D(rs)->Clone();
255
256 if (bounds.IsNull())
257 {
258 return;
259 }
260
261 //expand the bounding box in case the instruments are all at one position
262 mitk::Point3D center = bounds->GetCenterInWorld();
263 mitk::Geometry3D::BoundsArrayType extended_bounds = bounds->GetGeometryForTimeStep(0)->GetBounds();
264 for (unsigned int i = 0; i < 3; ++i)
265 {
266 if (bounds->GetExtentInWorld(i) < 500)
267 {
268 // extend the bounding box
269 extended_bounds[i * 2] = center[i] - 500 / 2.0;
270 extended_bounds[i * 2 + 1] = center[i] + 500 / 2.0;
271 }
272 }
273 //set the extended bounds
274 bounds->GetGeometryForTimeStep(0)->SetBounds(extended_bounds);
275
276 // initialize the views to the bounding geometry
277 mitk::RenderingManager::GetInstance()->InitializeViews(bounds);
278}
mitk::IGTLServer::Pointer m_IGTLServer
void ResizeBoundingBox()
To initialize the scene to the bounding box of all visible objects.
mitk::IGTLMessageProvider::Pointer m_IGTLMessageProvider
QList< mitk::DataNode::Pointer > m_DemoNodes
mitk::NavigationDataSequentialPlayer::Pointer m_NavDataPlayer
mitk::NavigationDataToIGTLMessageFilter::Pointer m_NavDataToIGTLMsgFilter
Ui::OpenIGTLinkProviderExampleControls m_Controls
void CreateQtPartControl(QWidget *parent) override
mitk::NavigationDataObjectVisualizationFilter::Pointer m_NavDataVisualizer
mitk::NavigationDataSet::Pointer m_NavDataSet
Data structure which stores streams of mitk::NavigationData for multiple tools.