Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
OverlayExample.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 "QmitkRegisterClasses.h"
19 #include "QmitkRenderWindow.h"
20 
21 #include <mitkDataNodeFactory.h>
22 #include <mitkPointSet.h>
24 
25 #include <QApplication>
26 #include <itksys/SystemTools.hxx>
27 #include <mitkOverlay2DLayouter.h>
28 #include <mitkOverlayManager.h>
29 #include <mitkTextOverlay2D.h>
30 #include <mitkTextOverlay3D.h>
32 
33 //##Documentation
34 //## @brief Load image (nrrd format) and display it in a 2D view
35 int main(int argc, char *argv[])
36 {
37  QApplication qtapplication(argc, argv);
38 
39  if (argc < 2)
40  {
41  fprintf(stderr, "Usage: %s [filename] \n\n", itksys::SystemTools::GetFilenameName(argv[0]).c_str());
42  return 1;
43  }
44 
45  // Register Qmitk-dependent global instances
47 
48  //*************************************************************************
49  // Part I: Basic initialization
50  //*************************************************************************
51 
52  // Create a DataStorage
53  // The DataStorage manages all data objects. It is used by the
54  // rendering mechanism to render all data objects
55  // We use the standard implementation mitk::StandaloneDataStorage.
57 
58  //*************************************************************************
59  // Part II: Create some data by reading a file
60  //*************************************************************************
61 
62  // Create a DataNodeFactory to read a data format supported
63  // by the DataNodeFactory (many image formats, surface formats, etc.)
65  const char *filename = argv[1];
66  try
67  {
68  reader->SetFileName(filename);
69  reader->Update();
70  //*************************************************************************
71  // Part III: Put the data into the datastorage
72  //*************************************************************************
73 
74  // Add the node to the DataStorage
75  dataStorage->Add(reader->GetOutput());
76  }
77  catch (...)
78  {
79  fprintf(stderr, "Could not open file %s \n\n", filename);
80  exit(2);
81  }
82 
83  //*************************************************************************
84  // Part IV: Create window and pass the datastorage to it
85  //*************************************************************************
86 
87  // Create a RenderWindow
88  QmitkRenderWindow renderWindow;
89 
90  // Tell the RenderWindow which (part of) the datastorage to render
91  renderWindow.GetRenderer()->SetDataStorage(dataStorage);
92 
93  // Initialize the RenderWindow
94  mitk::TimeGeometry::Pointer geo = dataStorage->ComputeBoundingGeometry3D(dataStorage->GetAll());
96  // mitk::RenderingManager::GetInstance()->InitializeViews();
97 
98  // Select a slice
100  if (sliceNaviController)
101  sliceNaviController->GetSlice()->SetPos(0);
102 
103  //*************************************************************************
104  // Part V: Qt-specific initialization
105  //*************************************************************************
106 
108  mitk::OverlayManager::Pointer OverlayManagerInstance = mitk::OverlayManager::New();
110  renderer->SetOverlayManager(OverlayManagerInstance);
112 
114  // The id that is passed identifies the correct mitk::OverlayManager and is '0' by default.
116  mitk::OverlayManager::Pointer overlayManager = renderer2D->GetOverlayManager();
118 
120  // This creates a 2DLayouter that is only active for the recently fetched axialRenderer and positione
121  mitk::Overlay2DLayouter::Pointer topleftLayouter =
123 
124  // Now, the created Layouter is added to the OverlayManager and can be referred to by its identification string.
125  overlayManager->AddLayouter(topleftLayouter.GetPointer());
126 
127  // Several other Layouters can be added to the overlayManager
128  mitk::Overlay2DLayouter::Pointer bottomLayouter =
130  overlayManager->AddLayouter(bottomLayouter.GetPointer());
132 
134  // Create a textOverlay2D
136 
137  textOverlay->SetText("Test!"); // set UTF-8 encoded text to render
138  textOverlay->SetFontSize(40);
139  textOverlay->SetColor(1, 0, 0); // Set text color to red
140  textOverlay->SetOpacity(1);
141 
142  // The position of the Overlay can be set to a fixed coordinate on the display.
143  mitk::Point2D pos;
144  pos[0] = 10, pos[1] = 20;
145  textOverlay->SetPosition2D(pos);
146 
147  // Add the overlay to the overlayManager. It is added to all registered renderers automaticly
148  overlayManager->AddOverlay(textOverlay.GetPointer());
149 
150  // Alternatively, a layouter can be used to manage the position of the overlay. If a layouter is set, the absolute
151  // position of the overlay is not used anymore
152  // The Standard TopLeft Layouter has to be registered to the OverlayManager first
153  overlayManager->AddLayouter(
156 
158  // Because a Layouter is specified by the identification string AND the Renderer, both have to be passed to the call.
159  overlayManager->SetLayouter(textOverlay.GetPointer(), mitk::Overlay2DLayouter::STANDARD_2D_TOPLEFT(), renderer2D);
161 
164 
165  // This vector is used to define an offset for the annotations, in order to show them with a margin to the actual
166  // coordinate.
168  offset[0] = .5;
169  offset[1] = .5;
170  offset[2] = .5;
171 
172  // Just a loop to create some points
173  for (int i = 0; i < 10; i++)
174  {
175  // To each point, a TextOverlay3D is created
177  mitk::Point3D point;
178  point[0] = i * 20;
179  point[1] = i * 30;
180  point[2] = -i * 50;
181  pointset->InsertPoint(i, point);
182  textOverlay3D->SetText("A Point");
183 
184  // The Position is set to the point coordinate to create an annotation to the point in the PointSet.
185  textOverlay3D->SetPosition3D(point);
186 
187  // move the annotation away from the actual point
188  textOverlay3D->SetOffsetVector(offset);
189 
190  overlayManager->AddOverlay(textOverlay3D.GetPointer());
191  }
192 
193  // also show the created pointset
195  datanode->SetData(pointset);
196  datanode->SetName("pointSet");
197  dataStorage->Add(datanode);
199  renderWindow.show();
200  renderWindow.resize(256, 256);
201 
202  return qtapplication.exec();
203 
204  // cleanup: Remove References to DataStorage. This will delete the object
205  dataStorage = NULL;
206 }
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
int main(int argc, char *argv[])
[includes]
itk::SmartPointer< Self > Pointer
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
virtual void SetDataStorage(mitk::DataStorage *storage) override
set the datastorage that will be used for rendering
void SetOverlayManager(itk::SmartPointer< OverlayManager > overlayManager)
Sets an OverlayManager which is used to add various Overlays to this renderer. If an OverlayManager w...
static Pointer New()
Organizes the rendering process.
static std::string STANDARD_2D_TOPLEFT()
MITKQTWIDGETS_EXPORT void QmitkRegisterClasses()
static std::string STANDARD_2D_BOTTOM()
virtual vtkRenderWindow * GetVtkRenderWindow() override
static Vector3D offset
static Pointer New()
static RenderingManager * GetInstance()
static const std::string filename
MITK implementation of the QVTKWidget.
virtual mitk::VtkPropRenderer * GetRenderer()
itk::SmartPointer< OverlayManager > GetOverlayManager()
Get the OverlayManager registered with this renderer if none was set, it will be created at this poin...
static Pointer New()
static Pointer New()
virtual mitk::SliceNavigationController * GetSliceNavigationController()
static Overlay2DLayouter::Pointer CreateLayouter(Alignment alignment, BaseRenderer *renderer)
Factory method for the different kinds of Layouters.
static Pointer New()
static Pointer New()