Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
Step5.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 
13 #include "QmitkRegisterClasses.h"
14 #include "QmitkRenderWindow.h"
15 #include "QmitkSliceWidget.h"
16 
18 #include "mitkProperties.h"
19 #include "mitkRenderingManager.h"
21 
22 #include "mitkPointSet.h"
23 // NEW INCLUDE
25 
26 #include <QApplication>
27 #include <QHBoxLayout>
28 #include <itksys/SystemTools.hxx>
29 #include <mitkIOUtil.h>
30 
31 //##Documentation
32 //## @brief Interactively add points
33 //##
34 //## As in Step4, load one or more data sets (many image,
35 //## surface and other formats) and create 3 views on the data.
36 //## Additionally, we want to interactively add points. A node containing
37 //## a PointSet as data is added to the data tree and a PointSetDataInteractor
38 //## is associated with the node, which handles the interaction. The
39 //## @em interaction @em pattern is defined in a state-machine, stored in an
40 //## external XML file. Thus, we need to load a state-machine
41 //## The interaction patterns defines the @em events,
42 //## on which the interactor reacts (e.g., which mouse buttons are used to
43 //## set a point), the @em transition to the next state (e.g., the initial
44 //## may be "empty point set") and associated @a actions (e.g., add a point
45 //## at the position where the mouse-click occured).
46 int main(int argc, char *argv[])
47 {
48  QApplication qtapplication(argc, argv);
49 
50  if (argc < 2)
51  {
52  fprintf(
53  stderr, "Usage: %s [filename1] [filename2] ...\n\n", itksys::SystemTools::GetFilenameName(argv[0]).c_str());
54  return 1;
55  }
56 
57  // Register Qmitk-dependent global instances
59 
60  //*************************************************************************
61  // Part I: Basic initialization
62  //*************************************************************************
63 
64  // Create a DataStorage
66 
67  //*************************************************************************
68  // Part II: Create some data by reading files
69  //*************************************************************************
70  int i;
71  for (i = 1; i < argc; ++i)
72  {
73  // For testing
74  if (strcmp(argv[i], "-testing") == 0)
75  continue;
76 
77  // Load datanode (eg. many image formats, surface formats, etc.)
78  mitk::StandaloneDataStorage::SetOfObjects::Pointer dataNodes = mitk::IOUtil::Load(argv[i], *ds);
79 
80  //*********************************************************************
81  // Part III: Put the data into the datastorage
82  //*********************************************************************
83  // Add the node to the DataStorage
84  if (dataNodes->empty())
85  {
86  fprintf(stderr, "Could not open file %s \n\n", argv[i]);
87  exit(2);
88  }
89  }
90 
91  //*************************************************************************
92  // Part V: Create windows and pass the tree to it
93  //*************************************************************************
94 
95  // Create toplevel widget with horizontal layout
96  QWidget toplevelWidget;
97  QHBoxLayout layout;
98  layout.setSpacing(2);
99  layout.setMargin(0);
100  toplevelWidget.setLayout(&layout);
101 
102  //*************************************************************************
103  // Part Va: 3D view
104  //*************************************************************************
105 
106  // Create a renderwindow
107  QmitkRenderWindow renderWindow(&toplevelWidget);
108  layout.addWidget(&renderWindow);
109 
110  // Tell the renderwindow which (part of) the tree to render
111 
112  renderWindow.GetRenderer()->SetDataStorage(ds);
113 
114  // Use it as a 3D view
116 
117  // Reposition the camera to include all visible actors
118  renderWindow.GetRenderer()->GetVtkRenderer()->ResetCamera();
119 
120  //*************************************************************************
121  // Part Vb: 2D view for slicing axially
122  //*************************************************************************
123 
124  // Create QmitkSliceWidget, which is based on the class
125  // QmitkRenderWindow, but additionally provides sliders
126  QmitkSliceWidget view2(&toplevelWidget);
127  layout.addWidget(&view2);
128 
129  // Tell the QmitkSliceWidget which (part of) the tree to render.
130  // By default, it slices the data axially
131  view2.SetDataStorage(ds);
132  mitk::DataStorage::SetOfObjects::ConstPointer rs = ds->GetSubset(mitk::TNodePredicateDataType<mitk::Image>::New());
134  // We want to see the position of the slice in 2D and the
135  // slice itself in 3D: add it to the tree!
136  ds->Add(view2.GetRenderer()->GetCurrentWorldPlaneGeometryNode());
137 
138  //*************************************************************************
139  // Part Vc: 2D view for slicing sagitally
140  //*************************************************************************
141 
142  // Create QmitkSliceWidget, which is based on the class
143  // QmitkRenderWindow, but additionally provides sliders
144  QmitkSliceWidget view3(&toplevelWidget);
145  layout.addWidget(&view3);
146 
147  // Tell the QmitkSliceWidget which (part of) the tree to render
148  // and to slice sagitall
149  view3.SetDataStorage(ds);
151 
152  // We want to see the position of the slice in 2D and the
153  // slice itself in 3D: add it to the tree!
154  ds->Add(view3.GetRenderer()->GetCurrentWorldPlaneGeometryNode());
155 
156  // *******************************************************
157  // ****************** START OF NEW PART ******************
158  // *******************************************************
159 
160  //*************************************************************************
161  // Part VI: For allowing to interactively add points ...
162  //*************************************************************************
163 
164  // ATTENTION: It is very important that the renderer already know their DataStorage,
165  // because registerig DataInteractors with the render windows is done automatically
166  // and only works if the BaseRenderer and the DataStorage know each other.
167 
168  // Create PointSet and a node for it
171  // Store the point set in the DataNode
172  pointSetNode->SetData(pointSet);
173 
174  // Add the node to the tree
175  ds->Add(pointSetNode);
176 
177  // Create PointSetDataInteractor
179  // Set the StateMachine pattern that describes the flow of the interactions
180  interactor->LoadStateMachine("PointSet.xml");
181  // Set the configuration file, which describes the user interactions that trigger actions
182  // in this file SHIFT + LeftClick triggers add Point, but by modifying this file,
183  // it could as well be changes to any other user interaction.
184  interactor->SetEventConfig("PointSetConfig.xml");
185 
186  // Assign the pointSetNode to the interactor,
187  // alternatively one could also add the DataInteractor to the pointSetNode using the SetDataInteractor() method.
188  interactor->SetDataNode(pointSetNode);
189 
190  // *******************************************************
191  // ******************* END OF NEW PART *******************
192  // *******************************************************
193 
194  //*************************************************************************
195  // Part VII: Qt-specific initialization
196  //*************************************************************************
197  toplevelWidget.show();
198 
199  return qtapplication.exec();
200 }
virtual DataNode * GetCurrentWorldPlaneGeometryNode()
Get a DataNode pointing to a data object containing the current 2D-worldgeometry. ...
vtkRenderer * GetVtkRenderer() const
void SetDataStorage(mitk::DataStorage *storage) override
set the datastorage that will be used for rendering
int main(int argc, char *argv[])
Interactively add points.
Definition: Step5.cpp:46
static Pointer New()
MITKQTWIDGETS_EXPORT void QmitkRegisterClasses()
Tests for type compatibility (dynamic_cast).
mitk::VtkPropRenderer * GetRenderer()
static Pointer New()
MITK implementation of the QVTKWidget.
virtual mitk::VtkPropRenderer * GetRenderer()
void SetData(mitk::DataStorage::SetOfObjects::ConstIterator it)
void SetMapperID(const MapperSlotId mapperId) override
Set the MapperSlotId to use.
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file into the given DataStorage.
Definition: mitkIOUtil.cpp:489
void SetDataStorage(mitk::StandaloneDataStorage::Pointer storage)