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