Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include <mitkNavigationData.h>
21 
23 #include <mitkDataNode.h>
24 #include <mitkCone.h>
25 #include <mitkCylinder.h>
26 #include <mitkRenderWindow.h>
27 #include <itksys/SystemTools.hxx>
28 
29 //The next line starts a snippet to display this code in the documentation. If you don't revise the documentation, don't remove it!
31 
32 //*************************************************************************
33 // What we will do...
34 //*************************************************************************
35 // This tutorial shows how to compose navigation datas. Therefore we render two objects.
36 //The first object is a cone that is tracked. The second object is a cylinder at a fixed position
37 //relative to the cone. At the end of the tracking, the cylinder is moved to its new relative position
38 //according to the last output of the tracking device.
39 //In addition to IGT tutorial step 1, the objects are added to a datastorage. Furthermore, a renderwindow
40 //is used for visual output.
41 
43 int main(int argc, char* argv[])
44 {
45  //*************************************************************************
46  // Set up Render Window and Tracking Device
47  //*************************************************************************
49  //General code rendering the data in a renderwindow. See MITK Tutorial Step1 for more details.
53 
54  //Here, we want a 3D renderwindow
55  renderWindow->GetRenderer()->SetMapperID(mitk::BaseRenderer::Standard3D);
56  renderWindow->GetVtkRenderWindow()->SetSize(500, 500);
57  renderWindow->GetRenderer()->Resize(500, 500);
58  //Connect datastorage and renderwindow
59  renderWindow->GetRenderer()->SetDataStorage(dataStorage);
60 
62 
64  //Virtual tracking device to generate random positions
66  //Bounds (within the random numbers are generated) must be set before the tools are added
67  double bound = 10.0;
68  mitk::ScalarType bounds[] = { -bound, bound, -bound, bound, -bound, bound };
69  tracker->SetBounds(bounds);
70  tracker->AddTool("tool1");
71 
72  //Tracking device source to get the data
74  source->SetTrackingDevice(tracker);
75  source->Connect();
76 
78 
79  //*************************************************************************
80  // Create Objects
81  //*************************************************************************
82 
84  //Cone representation for rendering of the moving object
86  dataNode->SetData(cone);
87  dataNode->SetName("My tracked object");
88  dataNode->SetColor(0.0, 1.0, 1.0);
89  dataStorage->Add(dataNode);
90 
91  //Filter for rendering the cone at correct postion and orientation
93  visualizer->SetInput(0, source->GetOutput());
94  visualizer->SetRepresentationObject(0, cone);
96 
98  //Cylinder representation for rendering of the fixed object
101  cylinderNode->SetData(cylinder);
102  cylinderNode->SetName("My fixed object");
103  cylinderNode->SetColor(1.0, 0.0, 0.0);
104  dataStorage->Add(cylinderNode);
105 
106  //Define a rotation and a translation for the fixed object
107  mitk::Matrix3D rotationMatrix;
108  rotationMatrix.SetIdentity();
109  double alpha = 0.3;
110  rotationMatrix[1][1] = cos(alpha);
111  rotationMatrix[1][2] = -sin(alpha);
112  rotationMatrix[2][1] = sin(alpha);
113  rotationMatrix[2][2] = cos(alpha);
114 
116  offset.Fill(5.0);
117  //Add rotation and translation to affine transform
119  affineTransform3D->SetOffset(offset);
120  affineTransform3D->SetMatrix(rotationMatrix);
121 
122  //apply rotation and translation
123  mitk::NavigationData::Pointer fixedNavigationData = mitk::NavigationData::New(affineTransform3D);
124  cylinder->GetGeometry()->SetIndexToWorldTransform(fixedNavigationData->GetAffineTransform3D());
126 
127  //*************************************************************************
128  // The Tracking loop
129  //*************************************************************************
130 
132  // Global reinit with the bounds of the virtual tracking device
133  mitk::TimeGeometry::Pointer timeGeometry = dataStorage->ComputeBoundingGeometry3D(dataStorage->GetAll());
134  mitk::BaseGeometry::Pointer geometry = timeGeometry->GetGeometryForTimeStep(0);
135  geometry->SetBounds(bounds);
136 
138 
139  source->StartTracking();
141 
143  //Generate and render 75 time steps to move the tracked object
144  for (int i = 0; i < 75; ++i)
145  {
146  //Update the cone position
147  visualizer->Update();
148 
149  //Update rendering
150  renderWindow->GetVtkRenderWindow()->Render();
152 
153  MITK_INFO << "Position " << source->GetOutput()->GetPosition();
154  //Slight delay for the random numbers
155  itksys::SystemTools::Delay(100);
156  }
157  //Stop the tracking device and disconnect it
158  //The tracking is done, now we want to move the fixed object to its correct relative position regarding the tracked object.
159  source->StopTracking();
160  source->Disconnect();
162 
163  //*************************************************************************
164  // Final Transform
165  //*************************************************************************
166 
168  //Now the tracking is finished and we can use the transformation to move
169  //the fixed object to its correct position relative to the new position
170  //of the moving/tracked object. Therefore, we compose the navigation datas.
171  fixedNavigationData->Compose(source->GetOutput(), false);
172 
173  //Update the transformation matrix of the cylinder
174  cylinder->GetGeometry()->SetIndexToWorldTransform(fixedNavigationData->GetAffineTransform3D());
175 
176  //Update the rendering
177  renderWindow->GetVtkRenderWindow()->Render();
179 
180  //Wait a little before closing the renderwindow
181  itksys::SystemTools::Delay(2000);
183 }
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
static Pointer New()
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
double ScalarType
static Pointer New()
static Pointer New()
static Vector3D offset
static Pointer New()
static RenderingManager * GetInstance()
static Pointer New()
int main(int argc, char *argv[])
[What we will do]
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.