Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkIGTTutorialStep2.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include <mitkNavigationData.h>
17 
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 
39 int 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.
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
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);
92 
94  //Cylinder representation for rendering of the fixed object
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 
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 
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();
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();
175 
176  //Wait a little before closing the renderwindow
177  itksys::SystemTools::Delay(2000);
179 }
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
static Pointer New()
#define MITK_INFO
Definition: mitkLogMacros.h:18
double ScalarType
static Pointer New()
int main(int, char **)
[What we will do]
static Pointer New()
static Vector3D offset
static Pointer New()
static RenderingManager * GetInstance()
static Pointer New()
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)