Medical Imaging Interaction Toolkit  2018.4.99-9a29ffc6
Medical Imaging Interaction Toolkit
QtFreeRender.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 "mitkRenderWindow.h"
14 
15 #include "mitkCameraController.h"
16 #include "mitkDisplayInteractor.h"
17 #include "mitkDisplayInteractor.h"
18 #include "mitkInteractionConst.h"
19 #include "mitkLine.h"
21 #include "mitkProperties.h"
22 #include "mitkVtkLayerController.h"
23 #include <mitkProperties.h>
24 #include <mitkRenderingManager.h>
26 #include <mitkTransferFunction.h>
28 
29 #include "mitkDataStorage.h"
30 #include "mitkIOUtil.h"
31 
32 #include "vtkAnnotatedCubeActor.h"
33 #include "vtkCornerAnnotation.h"
34 #include "vtkMitkRectangleProp.h"
35 #include "vtkOrientationMarkerWidget.h"
36 #include "vtkProperty.h"
37 #include "vtkRenderWindow.h"
38 #include "vtkRenderWindowInteractor.h"
39 #include "vtkTextProperty.h"
40 
41 // us
42 #include "usGetModuleContext.h"
43 #include "usModuleContext.h"
44 
46 
47 //##Documentation
48 //## @brief Example of a NON QT DEPENDENT MITK RENDERING APPLICATION.
49 
54 
56 vtkSmartPointer<vtkMitkRectangleProp> m_RectangleRendering1;
57 vtkSmartPointer<vtkMitkRectangleProp> m_RectangleRendering2;
58 vtkSmartPointer<vtkMitkRectangleProp> m_RectangleRendering3;
59 vtkSmartPointer<vtkMitkRectangleProp> m_RectangleRendering4;
60 
62 
68 
70 {
71  // Set default view directions for SNCs
72  mitkWidget1->GetSliceNavigationController()->SetDefaultViewDirection(mitk::SliceNavigationController::Axial);
73  mitkWidget2->GetSliceNavigationController()->SetDefaultViewDirection(mitk::SliceNavigationController::Sagittal);
74  mitkWidget3->GetSliceNavigationController()->SetDefaultViewDirection(mitk::SliceNavigationController::Frontal);
75  mitkWidget4->GetSliceNavigationController()->SetDefaultViewDirection(mitk::SliceNavigationController::Original);
76 
77  // initialize m_TimeNavigationController: send time via sliceNavigationControllers
78  m_TimeNavigationController = mitk::RenderingManager::GetInstance()->GetTimeNavigationController();
79  m_TimeNavigationController->ConnectGeometryTimeEvent(mitkWidget1->GetSliceNavigationController(), false);
80  m_TimeNavigationController->ConnectGeometryTimeEvent(mitkWidget2->GetSliceNavigationController(), false);
81  m_TimeNavigationController->ConnectGeometryTimeEvent(mitkWidget3->GetSliceNavigationController(), false);
82  m_TimeNavigationController->ConnectGeometryTimeEvent(mitkWidget4->GetSliceNavigationController(), false);
83  mitkWidget1->GetSliceNavigationController()->ConnectGeometrySendEvent(
84  mitk::BaseRenderer::GetInstance(mitkWidget4->GetVtkRenderWindow()));
85 
86  // reverse connection between sliceNavigationControllers and m_TimeNavigationController
87  mitkWidget1->GetSliceNavigationController()->ConnectGeometryTimeEvent(m_TimeNavigationController, false);
88  mitkWidget2->GetSliceNavigationController()->ConnectGeometryTimeEvent(m_TimeNavigationController, false);
89  mitkWidget3->GetSliceNavigationController()->ConnectGeometryTimeEvent(m_TimeNavigationController, false);
90  mitkWidget4->GetSliceNavigationController()->ConnectGeometryTimeEvent(m_TimeNavigationController, false);
91 
92  mitkWidget4->GetRenderer()->GetVtkRenderer()->SetBackground(0.1, 0.1, 0.1);
93  mitkWidget4->GetRenderer()->GetVtkRenderer()->SetBackground(0.5, 0.5, 0.5);
94  mitkWidget4->GetRenderer()->GetVtkRenderer()->GradientBackgroundOn();
95 
96  m_RectangleRendering1 = vtkSmartPointer<vtkMitkRectangleProp>::New();
97  m_RectangleRendering1->SetColor(1.0, 0.0, 0.0);
98  mitkWidget1->GetRenderer()->GetVtkRenderer()->AddViewProp(m_RectangleRendering1);
99 
100  m_RectangleRendering2 = vtkSmartPointer<vtkMitkRectangleProp>::New();
101  m_RectangleRendering2->SetColor(0.0, 1.0, 0.0);
102  mitkWidget2->GetRenderer()->GetVtkRenderer()->AddViewProp(m_RectangleRendering2);
103 
104  m_RectangleRendering3 = vtkSmartPointer<vtkMitkRectangleProp>::New();
105  m_RectangleRendering3->SetColor(0.0, 0.0, 1.0);
106  mitkWidget3->GetRenderer()->GetVtkRenderer()->AddViewProp(m_RectangleRendering3);
107 
108  m_RectangleRendering4 = vtkSmartPointer<vtkMitkRectangleProp>::New();
109  m_RectangleRendering4->SetColor(1.0, 1.0, 0.0);
110  mitkWidget4->GetRenderer()->GetVtkRenderer()->AddViewProp(m_RectangleRendering4);
111 }
112 
114 {
115  // add the displayed planes of the multiwidget to a node to which the subtree
116  // @a planesSubTree points ...
117 
118  float white[3] = {1.0f, 1.0f, 1.0f};
121 
122  // ... of widget 1
123  m_PlaneNode1 =
124  (mitk::BaseRenderer::GetInstance(mitkWidget1->GetVtkRenderWindow()))->GetCurrentWorldPlaneGeometryNode();
125  m_PlaneNode1->SetColor(white, mitk::BaseRenderer::GetInstance(mitkWidget4->GetVtkRenderWindow()));
126  m_PlaneNode1->SetProperty("visible", mitk::BoolProperty::New(true));
127  m_PlaneNode1->SetProperty("name", mitk::StringProperty::New("widget1Plane"));
128  m_PlaneNode1->SetProperty("includeInBoundingBox", mitk::BoolProperty::New(false));
129  m_PlaneNode1->SetProperty("helper object", mitk::BoolProperty::New(true));
130  m_PlaneNode1->SetProperty("layer", layer);
131  m_PlaneNode1->SetColor(1.0, 0.0, 0.0);
133  m_PlaneNode1->SetMapper(mitk::BaseRenderer::Standard2D, mapper);
134 
135  // ... of widget 2
136  m_PlaneNode2 =
137  (mitk::BaseRenderer::GetInstance(mitkWidget2->GetVtkRenderWindow()))->GetCurrentWorldPlaneGeometryNode();
138  m_PlaneNode2->SetColor(white, mitk::BaseRenderer::GetInstance(mitkWidget4->GetVtkRenderWindow()));
139  m_PlaneNode2->SetProperty("visible", mitk::BoolProperty::New(true));
140  m_PlaneNode2->SetProperty("name", mitk::StringProperty::New("widget2Plane"));
141  m_PlaneNode2->SetProperty("includeInBoundingBox", mitk::BoolProperty::New(false));
142  m_PlaneNode2->SetProperty("helper object", mitk::BoolProperty::New(true));
143  m_PlaneNode2->SetProperty("layer", layer);
144  m_PlaneNode2->SetColor(0.0, 1.0, 0.0);
146  m_PlaneNode2->SetMapper(mitk::BaseRenderer::Standard2D, mapper);
147 
148  // ... of widget 3
149  m_PlaneNode3 =
150  (mitk::BaseRenderer::GetInstance(mitkWidget3->GetVtkRenderWindow()))->GetCurrentWorldPlaneGeometryNode();
151  m_PlaneNode3->SetColor(white, mitk::BaseRenderer::GetInstance(mitkWidget4->GetVtkRenderWindow()));
152  m_PlaneNode3->SetProperty("visible", mitk::BoolProperty::New(true));
153  m_PlaneNode3->SetProperty("name", mitk::StringProperty::New("widget3Plane"));
154  m_PlaneNode3->SetProperty("includeInBoundingBox", mitk::BoolProperty::New(false));
155  m_PlaneNode3->SetProperty("helper object", mitk::BoolProperty::New(true));
156  m_PlaneNode3->SetProperty("layer", layer);
157  m_PlaneNode3->SetColor(0.0, 0.0, 1.0);
159  m_PlaneNode3->SetMapper(mitk::BaseRenderer::Standard2D, mapper);
160 
161  // AddPlanesToDataStorage
162  if (m_PlaneNode1.IsNotNull() && m_PlaneNode2.IsNotNull() && m_PlaneNode3.IsNotNull() && m_Node.IsNotNull())
163  {
164  if (m_DataStorage.IsNotNull())
165  {
166  m_DataStorage->Add(m_PlaneNode1);
167  m_DataStorage->Add(m_PlaneNode2);
168  m_DataStorage->Add(m_PlaneNode3);
169  }
170  }
171 }
172 
173 void Fit()
174 {
175  vtkRenderer *vtkrenderer;
176  mitk::BaseRenderer::GetInstance(mitkWidget1->GetVtkRenderWindow())->GetCameraController()->Fit();
177  mitk::BaseRenderer::GetInstance(mitkWidget2->GetVtkRenderWindow())->GetCameraController()->Fit();
178  mitk::BaseRenderer::GetInstance(mitkWidget3->GetVtkRenderWindow())->GetCameraController()->Fit();
179  mitk::BaseRenderer::GetInstance(mitkWidget4->GetVtkRenderWindow())->GetCameraController()->Fit();
180 
181  int w = vtkObject::GetGlobalWarningDisplay();
182  vtkObject::GlobalWarningDisplayOff();
183 
184  vtkrenderer = mitk::BaseRenderer::GetInstance(mitkWidget1->GetVtkRenderWindow())->GetVtkRenderer();
185  if (vtkrenderer != nullptr)
186  vtkrenderer->ResetCamera();
187 
188  vtkrenderer = mitk::BaseRenderer::GetInstance(mitkWidget2->GetVtkRenderWindow())->GetVtkRenderer();
189  if (vtkrenderer != nullptr)
190  vtkrenderer->ResetCamera();
191 
192  vtkrenderer = mitk::BaseRenderer::GetInstance(mitkWidget3->GetVtkRenderWindow())->GetVtkRenderer();
193  if (vtkrenderer != nullptr)
194  vtkrenderer->ResetCamera();
195 
196  vtkrenderer = mitk::BaseRenderer::GetInstance(mitkWidget4->GetVtkRenderWindow())->GetVtkRenderer();
197  if (vtkrenderer != nullptr)
198  vtkrenderer->ResetCamera();
199 
200  vtkObject::SetGlobalWarningDisplay(w);
201 }
202 
203 int main(int argc, char *argv[])
204 {
205  if (argc < 2)
206  {
207  fprintf(stderr, "Usage: %s [filename1] [filename2] ...\n\n", "");
208  return 1;
209  }
210 
211  // Create a DataStorage
212  m_DataStorage = mitk::StandaloneDataStorage::New();
213 
214  //*************************************************************************
215  // Part II: Create some data by reading files
216  //*************************************************************************
217  int i;
218  for (i = 1; i < argc; ++i)
219  {
220  // For testing
221  if (strcmp(argv[i], "-testing") == 0)
222  continue;
223 
224  std::string filename = argv[i];
225  try
226  {
227  // Read the file and add it as a data node to the data storage
228  mitk::DataStorage::SetOfObjects::Pointer nodes = mitk::IOUtil::Load(filename, *m_DataStorage);
229 
230  for (mitk::DataStorage::SetOfObjects::Iterator nodeIter = nodes->Begin(), nodeIterEnd = nodes->End();
231  nodeIter != nodeIterEnd;
232  ++nodeIter)
233  {
234  mitk::DataNode::Pointer node = nodeIter->Value();
235  mitk::Image::Pointer image = dynamic_cast<mitk::Image *>(node->GetData());
236  if (image.IsNotNull())
237  {
238  // Set the property "volumerendering" to the Boolean value "true"
239  node->SetProperty("volumerendering", mitk::BoolProperty::New(false));
240  node->SetProperty("name", mitk::StringProperty::New("testimage"));
241  node->SetProperty("layer", mitk::IntProperty::New(1));
242  }
243  }
244  }
245  catch (...)
246  {
247  std::cerr << "Could not open file " << filename << std::endl;
248  exit(2);
249  }
250  }
251 
252  //*************************************************************************
253  // Part V: Create window and pass the tree to it
254  //*************************************************************************
255 
256  // Create renderwindows
257  mitkWidget1 = mitk::RenderWindow::New();
258  mitkWidget2 = mitk::RenderWindow::New();
259  mitkWidget3 = mitk::RenderWindow::New();
260  mitkWidget4 = mitk::RenderWindow::New();
261 
262  mitkWidget1->GetRenderer()->PrepareRender();
263  mitkWidget2->GetRenderer()->PrepareRender();
264  mitkWidget3->GetRenderer()->PrepareRender();
265 
266  // Tell the renderwindow which (part of) the datastorage to render
267  mitkWidget1->GetRenderer()->SetDataStorage(m_DataStorage);
268  mitkWidget2->GetRenderer()->SetDataStorage(m_DataStorage);
269  mitkWidget3->GetRenderer()->SetDataStorage(m_DataStorage);
270  mitkWidget4->GetRenderer()->SetDataStorage(m_DataStorage);
271 
272  // instantiate display interactor
273  if (m_DisplayInteractor.IsNull())
274  {
275  m_DisplayInteractor = mitk::DisplayInteractor::New();
276  m_DisplayInteractor->LoadStateMachine("DisplayInteraction.xml");
277  m_DisplayInteractor->SetEventConfig("DisplayConfigMITK.xml");
278  // Register as listener via micro services
279 
280  us::ModuleContext *context = us::GetModuleContext();
281  context->RegisterService<mitk::InteractionEventObserver>(m_DisplayInteractor.GetPointer());
282  }
283  // Use it as a 2D View
284  mitkWidget1->GetRenderer()->SetMapperID(mitk::BaseRenderer::Standard2D);
285  mitkWidget2->GetRenderer()->SetMapperID(mitk::BaseRenderer::Standard2D);
286  mitkWidget3->GetRenderer()->SetMapperID(mitk::BaseRenderer::Standard2D);
287  mitkWidget4->GetRenderer()->SetMapperID(mitk::BaseRenderer::Standard3D);
288 
289  mitkWidget1->SetSize(400, 400);
290 
291  mitkWidget2->GetVtkRenderWindow()->SetPosition(mitkWidget1->GetVtkRenderWindow()->GetPosition()[0] + 420,
292  mitkWidget1->GetVtkRenderWindow()->GetPosition()[1]);
293  mitkWidget2->SetSize(400, 400);
294 
295  mitkWidget3->GetVtkRenderWindow()->SetPosition(mitkWidget1->GetVtkRenderWindow()->GetPosition()[0],
296  mitkWidget1->GetVtkRenderWindow()->GetPosition()[1] + 450);
297  mitkWidget3->SetSize(400, 400);
298 
299  mitkWidget4->GetVtkRenderWindow()->SetPosition(mitkWidget1->GetVtkRenderWindow()->GetPosition()[0] + 420,
300  mitkWidget1->GetVtkRenderWindow()->GetPosition()[1] + 450);
301  mitkWidget4->SetSize(400, 400);
302 
304 
306 
307  Fit();
308 
309  // Initialize the RenderWindows
310  auto geo = m_DataStorage->ComputeBoundingGeometry3D(m_DataStorage->GetAll());
312 
313  m_DataStorage->Print(std::cout);
315 
316  // reinit the mitkVTKEventProvider;
317  // this is only necessary once after calling
318  // ForceImmediateUpdateAll() for the first time
319  mitkWidget1->ReinitEventProvider();
320  mitkWidget2->ReinitEventProvider();
321  mitkWidget3->ReinitEventProvider();
322 
323  mitkWidget1->GetVtkRenderWindow()->Render();
324  mitkWidget2->GetVtkRenderWindow()->Render();
325  mitkWidget3->GetVtkRenderWindow()->Render();
326  mitkWidget4->GetVtkRenderWindow()->Render();
327  mitkWidget4->GetVtkRenderWindowInteractor()->Start();
328 
329  return 0;
330 }
mitk::RenderWindow::Pointer mitkWidget4
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
Base class to implement InteractionEventObservers.
mitk::DisplayInteractor::Pointer m_DisplayInteractor
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
void AddDisplayPlaneSubTree()
mitk::RenderWindow::Pointer mitkWidget2
mitk::DataNode::Pointer m_PlaneNode3
vtkSmartPointer< vtkMitkRectangleProp > m_RectangleRendering4
static Pointer New()
Constants for most interaction classes, due to the generic StateMachines.
static Pointer New()
const SliceNavigationController * GetTimeNavigationController() const
Controls the selection of the slice the associated BaseRenderer will display.
vtkSmartPointer< vtkMitkRectangleProp > m_RectangleRendering2
static Pointer New()
mitk::DataNode::Pointer m_Node
mitk::DataNode::Pointer m_PlaneNode1
void ConnectGeometryTimeEvent(T *receiver, bool connectSendEvent=true)
mitk::DataStorage::Pointer m_DataStorage
mitk::RenderWindow::Pointer mitkWidget1
Example of a NON QT DEPENDENT MITK RENDERING APPLICATION.
static RenderingManager * GetInstance()
Image class for storing images.
Definition: mitkImage.h:72
mitk::RenderWindow::Pointer mitkWidget3
vtkSmartPointer< vtkMitkRectangleProp > m_RectangleRendering1
mitk::DataNode::Pointer m_PlaneNode2
mitk::Image::Pointer image
void InitializeWindows()
void Fit()
static Pointer New()
vtkSmartPointer< vtkMitkRectangleProp > m_RectangleRendering3
mitk::SliceNavigationController * m_TimeNavigationController
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
int main(int argc, char *argv[])
static Pointer New()
void RequestUpdateAll(RequestType type=REQUEST_UPDATE_ALL)
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