MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkIGTTutorialStep2.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#include <mitkNavigationData.h>
17
18#include <mitkStandaloneDataStorage.h>
19#include <mitkDataNode.h>
20#include <mitkCone.h>
21#include <mitkCylinder.h>
22#include <mitkRenderWindow.h>
23#include <itksys/SystemTools.hxx>
24
25//The next line starts a snippet to display this code in the documentation. If you don't revise the documentation, don't remove it!
27
28//*************************************************************************
29// What we will do...
30//*************************************************************************
31// This tutorial shows how to compose navigation datas. Therefore we render two objects.
32//The first object is a cone that is tracked. The second object is a cylinder at a fixed position
33//relative to the cone. At the end of the tracking, the cylinder is moved to its new relative position
34//according to the last output of the tracking device.
35//In addition to IGT tutorial step 1, the objects are added to a datastorage. Furthermore, a renderwindow
36//is used for visual output.
37
39int main(int, char**)
40{
41 //*************************************************************************
42 // Set up Render Window and Tracking Device
43 //*************************************************************************
45 //General code rendering the data in a renderwindow. See MITK Tutorial Step1 for more details.
46 mitk::StandaloneDataStorage::Pointer dataStorage = mitk::StandaloneDataStorage::New();
47 mitk::RenderWindow::Pointer renderWindow = mitk::RenderWindow::New();
48 mitk::DataNode::Pointer dataNode = mitk::DataNode::New();
49
50 //Here, we want a 3D renderwindow
51 renderWindow->GetRenderer()->SetMapperID(mitk::BaseRenderer::Standard3D);
52 renderWindow->GetVtkRenderWindow()->SetSize(500, 500);
53 renderWindow->GetRenderer()->Resize(500, 500);
54 //Connect datastorage and renderwindow
55 renderWindow->GetRenderer()->SetDataStorage(dataStorage);
56
58
60 //Virtual tracking device to generate random positions
61 mitk::VirtualTrackingDevice::Pointer tracker = mitk::VirtualTrackingDevice::New();
62 //Bounds (within the random numbers are generated) must be set before the tools are added
63 double bound = 10.0;
64 mitk::ScalarType bounds[] = { -bound, bound, -bound, bound, -bound, bound };
65 tracker->SetBounds(bounds);
66 tracker->AddTool("tool1");
67
68 //Tracking device source to get the data
69 mitk::TrackingDeviceSource::Pointer source = mitk::TrackingDeviceSource::New();
70 source->SetTrackingDevice(tracker);
71 source->Connect();
72
74
75 //*************************************************************************
76 // Create Objects
77 //*************************************************************************
78
80 //Cone representation for rendering of the moving object
81 mitk::Cone::Pointer cone = mitk::Cone::New();
82 dataNode->SetData(cone);
83 dataNode->SetName("My tracked object");
84 dataNode->SetColor(0.0, 1.0, 1.0);
85 dataStorage->Add(dataNode);
86
87 //Filter for rendering the cone at correct postion and orientation
88 mitk::NavigationDataObjectVisualizationFilter::Pointer visualizer = mitk::NavigationDataObjectVisualizationFilter::New();
89 visualizer->SetInput(0, source->GetOutput());
90 visualizer->SetRepresentationObject(0, cone.GetPointer());
92
94 //Cylinder representation for rendering of the fixed object
95 mitk::DataNode::Pointer cylinderNode = mitk::DataNode::New();
96 mitk::Cylinder::Pointer cylinder = mitk::Cylinder::New();
97 cylinderNode->SetData(cylinder);
98 cylinderNode->SetName("My fixed object");
99 cylinderNode->SetColor(1.0, 0.0, 0.0);
100 dataStorage->Add(cylinderNode);
101
102 //Define a rotation and a translation for the fixed object
103 mitk::Matrix3D rotationMatrix;
104 rotationMatrix.SetIdentity();
105 double alpha = 0.3;
106 rotationMatrix[1][1] = cos(alpha);
107 rotationMatrix[1][2] = -sin(alpha);
108 rotationMatrix[2][1] = sin(alpha);
109 rotationMatrix[2][2] = cos(alpha);
110
111 mitk::Vector3D offset;
112 offset.Fill(5.0);
113 //Add rotation and translation to affine transform
114 mitk::AffineTransform3D::Pointer affineTransform3D = mitk::AffineTransform3D::New();
115 affineTransform3D->SetOffset(offset);
116 affineTransform3D->SetMatrix(rotationMatrix);
117
118 //apply rotation and translation
119 mitk::NavigationData::Pointer fixedNavigationData = mitk::NavigationData::New(affineTransform3D);
120 cylinder->GetGeometry()->SetIndexToWorldTransform(fixedNavigationData->GetAffineTransform3D());
122
123 //*************************************************************************
124 // The Tracking loop
125 //*************************************************************************
126
128 // Global reinit with the bounds of the virtual tracking device
129 auto timeGeometry = dataStorage->ComputeBoundingGeometry3D(dataStorage->GetAll());
130 mitk::BaseGeometry::Pointer geometry = timeGeometry->GetGeometryForTimeStep(0);
131 geometry->SetBounds(bounds);
132
133 mitk::RenderingManager::GetInstance()->InitializeViews(geometry);
134
135 source->StartTracking();
137
139 //Generate and render 75 time steps to move the tracked object
140 for (int i = 0; i < 75; ++i)
141 {
142 //Update the cone position
143 visualizer->Update();
144
145 //Update rendering
146 renderWindow->GetVtkRenderWindow()->Render();
147 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
148
149 MITK_INFO << "Position " << source->GetOutput()->GetPosition();
150 //Slight delay for the random numbers
151 itksys::SystemTools::Delay(100);
152 }
153 //Stop the tracking device and disconnect it
154 //The tracking is done, now we want to move the fixed object to its correct relative position regarding the tracked object.
155 source->StopTracking();
156 source->Disconnect();
158
159 //*************************************************************************
160 // Final Transform
161 //*************************************************************************
162
164 //Now the tracking is finished and we can use the transformation to move
165 //the fixed object to its correct position relative to the new position
166 //of the moving/tracked object. Therefore, we compose the navigation datas.
167 fixedNavigationData->Compose(source->GetOutput(), false);
168
169 //Update the transformation matrix of the cylinder
170 cylinder->GetGeometry()->SetIndexToWorldTransform(fixedNavigationData->GetAffineTransform3D());
171
172 //Update the rendering
173 renderWindow->GetVtkRenderWindow()->Render();
174 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
175
176 //Wait a little before closing the renderwindow
177 itksys::SystemTools::Delay(2000);
179}
void main()