MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
QmitkIGTTutorialView.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
14
15#include "mitkNDIPassiveTool.h"
18#include "mitkStandardFileLocations.h"
20#include "mitkCone.h"
21
22#include <QTimer>
23#include <QMessageBox>
24
26
27
28const std::string QmitkIGTTutorialView::VIEW_ID = "org.mitk.views.igttutorial";
29
31: QmitkAbstractView(),
32m_Controls(nullptr), m_Source(nullptr), m_Visualizer(nullptr), m_Timer(nullptr)
33{
34}
35
36
40
41
43{
44 if (!m_Controls)
45 {
46 // create GUI widget
47 m_Controls = new Ui::QmitkIGTTutorialViewControls;
48 m_Controls->setupUi(parent);
49 this->CreateConnections();
50 }
51}
52
53
54
56{
57 if ( m_Controls )
58 {
59 connect( (QObject*)(m_Controls->m_StartButton), SIGNAL(clicked()),(QObject*) this, SLOT(OnStartIGT()));
60 connect( (QObject*)(m_Controls->m_StopButton), SIGNAL(clicked()),(QObject*) this, SLOT(OnStopIGT()));
61 }
62}
63
65{
66 m_Controls->m_virtualTrackingRadioButton->setFocus();
67}
68
69//The next line starts a snippet to display this code in the documentation. If you don't revise the documentation, don't remove it!
72{
73 //This method is called when the "Start Image Guided Therapy" button is pressed. Any kind of navigation application will
74 //start with the connection to a tracking system and as we do image guided procedures we want to show
75 //something on the screen. In this tutorial we connect to the NDI Polaris tracking system pr a virtual tracking device and we will
76 //show the movement of a tool as cone in MITK.
77
79
81 try
82 {
83 if(m_Controls->m_NDITrackingRadioButton->isChecked())
84 {
85 /**************** Variant 1: Use a NDI Polaris Tracking Device ****************/
86 //Here we want to use the NDI Polaris tracking device. Therefore we instantiate a object of the class
87 //NDITrackingDevice and make some settings which are necessary for a proper connection to the device.
88 MITK_INFO << "NDI tracking";
89 QMessageBox::warning ( nullptr, "Warning", "You have to set the parameters for the NDITracking device inside the code (QmitkIGTTutorialView::OnStartIGT()) before you can use it.");
90 mitk::NDITrackingDevice::Pointer tracker = mitk::NDITrackingDevice::New(); //instantiate
91 tracker->SetPortNumber(mitk::SerialCommunication::COM4); //set the comport
92 tracker->SetBaudRate(mitk::SerialCommunication::BaudRate115200); //set the baud rate
93 tracker->SetType(mitk::NDIPolarisTypeInformation::GetTrackingDeviceName()); //set the type there you can choose between Polaris and Aurora
94
95 //The tools represent the sensors of the tracking device. In this case we have one pointer tool.
96 //The TrackingDevice object it self fills the tool with data. So we have to add the tool to the
97 //TrackingDevice object.
98 // The Polaris system needs a ".rom" file which describes the geometry of the markers related to the tool tip.
99 //NDI provides an own software (NDI architect) to generate those files.
100 tracker->AddTool("MyInstrument", "c:\\myinstrument.rom");
102 /**************** End of Variant 1 ****************/
104 //The tracking device object is used for the physical connection to the device. To use the
105 //data inside of our tracking pipeline we need a source. This source encapsulate the tracking device
106 //and provides objects of the type mitk::NavigationData as output. The NavigationData objects stores
107 //position, orientation, if the data is valid or not and special error information in a covariance
108 //matrix.
109 //
110 //Typically the start of a pipeline is a TrackingDeviceSource. To work correct we have to set a
111 //TrackingDevice object. Attention you have to set the tools before you set the whole TrackingDevice
112 //object to the TrackingDeviceSource because the source need to know how many outputs should be
113 //generated.
114 m_Source = mitk::TrackingDeviceSource::New(); //We need the filter objects to stay alive,
115 //therefore they must be members.
116 m_Source->SetTrackingDevice(tracker); //Here we set the tracking device to the source of the pipeline.
118
119 }
121 else
122 {
123 /**************** Variant 2: Emulate a Tracking Device with mitk::VirtualTrackingDevice ****************/
124 // For tests, it is useful to simulate a tracking device in software. This is what mitk::VirtualTrackingDevice does.
125 // It will produce random position, orientation and error values for each tool that is added.
126 MITK_INFO << "virtual tracking"<<endl;
127 mitk::VirtualTrackingDevice::Pointer tracker = mitk::VirtualTrackingDevice::New(); // create virtual tracker
128
129 mitk::ScalarType bounds[] = {0.0, 200.0, 0.0, 200.0, 0.0, 200.0};
130 tracker->SetBounds(bounds);
131 tracker->AddTool("MyInstrument"); // add a tool to tracker
132
133 //The tracking device object is used for the physical connection to the device. To use the
134 //data inside of our tracking pipeline we need a source. This source encapsulate the tracking device
135 //and provides objects of the type mitk::NavigationData as output. The NavigationData objects stores
136 //position, orientation, if the data is valid or not and special error information in a covariance
137 //matrix.
138 //
139 //Typically the start of a pipeline is a TrackingDeviceSource. To work correct we have to set a
140 //TrackingDevice object. Attention you have to set the tools before you set the whole TrackingDevice
141 //object to the TrackingDeviceSource because the source need to know how many outputs should be
142 //generated.
143 m_Source = mitk::TrackingDeviceSource::New(); //We need the filter objects to stay alive,
144 //therefore they must be members.
145 m_Source->SetTrackingDevice(tracker); //Here we set the tracking device to the source of the pipeline.
146 /**************** End of Variant 2 ****************/
147 }
149
151 m_Source->Connect(); //Now we connect to the tracking system.
152 //Note we do not call this on the TrackingDevice object
154
156 //As we wish to visualize our tool we need to have a PolyData which shows us the movement of our tool.
157 //Here we take a cone shaped PolyData. In MITK you have to add the PolyData as a node into the DataStorage
158 //to show it inside of the rendering windows. After that you can change the properties of the cone
159 //to manipulate rendering, e.g. the position and orientation as in our case.
160 mitk::Cone::Pointer cone = mitk::Cone::New(); //instantiate a new cone
161 double scale[] = {10.0, 10.0, 10.0};
162 cone->GetGeometry()->SetSpacing(scale); //scale it a little that so we can see something
163 mitk::DataNode::Pointer node = mitk::DataNode::New(); //generate a new node to store the cone into
164 //the DataStorage.
165 node->SetData(cone); //The data of that node is our cone.
166 node->SetName("My tracked object"); //The node has additional properties like a name
167 node->SetColor(1.0, 0.0, 0.0); //or the color. Here we make it red.
168 this->GetDataStorage()->Add(node); //After adding the Node with the cone in it to the
169 //DataStorage, MITK will show the cone in the
170 //render windows.
172
173
175 //For updating the render windows we use another filter of the MITK-IGT pipeline concept. The
176 //NavigationDataObjectVisualizationFilter needs as input a NavigationData and a
177 //PolyData. In our case the input is the source and the PolyData our cone.
178
179 //First we create a new filter for the visualization update.
180 m_Visualizer = mitk::NavigationDataObjectVisualizationFilter::New();
181 m_Visualizer->SetInput(0, m_Source->GetOutput()); //Then we connect to the pipeline.
182 m_Visualizer->SetRepresentationObject(0, cone.GetPointer()); // After that we have to assign the cone to the input
183
184 //Now this simple pipeline is ready, so we can start the tracking. Here again: We do not call the
185 //StartTracking method from the tracker object itself. Instead we call this method from our source.
186 m_Source->StartTracking();
188
190 //Now every call of m_Visualizer->Update() will show us the cone at the position and orientation
191 //given from the tracking device.
192 //We use a QTimer object to call this Update() method in a fixed interval.
193 if (m_Timer == nullptr)
194 {
195 m_Timer = new QTimer(this); //create a new timer
196 }
197 connect(m_Timer, SIGNAL(timeout()), this, SLOT(OnTimer())); //connect the timer to the method OnTimer()
198
199 m_Timer->start(100); //Every 100ms the method OnTimer() is called. -> 10fps
201
203 //disable the tracking device selection
204 this->m_Controls->m_NDITrackingRadioButton->setDisabled(true);
205 this->m_Controls->m_virtualTrackingRadioButton->setDisabled(true);
207
208 }
210 catch (std::exception& e)
211 {
212 // add cleanup
213 MITK_INFO << "Error in QmitkIGTTutorial::OnStartIGT():" << e.what();
214 }
216}
217
220{
221 //Here we call the Update() method from the Visualization Filter. Internally the filter checks if
222 //new NavigationData is available. If we have a new NavigationData the cone position and orientation
223 //will be adapted.
224 m_Visualizer->Update();
225
226 auto geo = this->GetDataStorage()->ComputeBoundingGeometry3D(this->GetDataStorage()->GetAll());
227 mitk::RenderingManager::GetInstance()->InitializeViews( geo );
228 this->RequestRenderWindowUpdate();
229}
231
234{
235 //This method is called when the Stop button is pressed. Here we disconnect the pipeline.
236 if (m_Timer == nullptr)
237 {
238 MITK_INFO << "No Timer was set yet!";
239 return;
240 }
241 //To disconnect the pipeline in a save way we first stop the timer than we disconnect the tracking device.
242 //After that we destroy all filters with changing them to nullptr.
243 m_Timer->stop();
244 disconnect(m_Timer, SIGNAL(timeout()), this, SLOT(OnTimer()));
245 m_Timer = nullptr;
246 m_Source->StopTracking();
247 m_Source->Disconnect();
248 m_Source = nullptr;
249 m_Visualizer = nullptr;
250 m_Source = nullptr;
251 this->GetDataStorage()->Remove(this->GetDataStorage()->GetNamedNode("My tracked object"));
252
253 //enable the tracking device selection
254 this->m_Controls->m_NDITrackingRadioButton->setEnabled(true);
255 this->m_Controls->m_virtualTrackingRadioButton->setEnabled(true);
256}
virtual void CreateConnections()
Creation of the connections of main and control widget.
static const std::string VIEW_ID
QTimer * m_Timer
timer for continuous tracking update
void OnStopIGT()
stop IGT scene and clean up
void OnStartIGT()
Execute MITK-IGT Tutorial.
mitk::NavigationDataObjectVisualizationFilter::Pointer m_Visualizer
visualization filter uses output from m_Source
Ui::QmitkIGTTutorialViewControls * m_Controls
void CreateQtPartControl(QWidget *parent) override
void OnTimer()
timer based update of IGT scene
mitk::TrackingDeviceSource::Pointer m_Source
source filer that connects to the tracking device