Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
Step3.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 
16 #include <mitkIOUtil.h>
17 #include <mitkProperties.h>
18 #include <mitkRenderingManager.h>
20 #include <mitkTransferFunction.h>
22 
23 #include <itksys/SystemTools.hxx>
24 
25 #include <QApplication>
26 
27 //##Documentation
28 //## @brief Change the type of display to 3D
29 //##
30 //## As in Step2, load one or more data sets (many image, surface
31 //## and other formats), but display it in a 3D view.
32 //## The QmitkRenderWindow is now used for displaying a 3D view, by
33 //## setting the used mapper-slot to Standard3D.
34 //## Since volume-rendering is a (rather) slow procedure, the default
35 //## is that images are not displayed in the 3D view. For this example,
36 //## we want volume-rendering, thus we switch it on by setting
37 //## the Boolean-property "volumerendering" to "true".
38 int main(int argc, char *argv[])
39 {
40  QApplication qtapplication(argc, argv);
41  if (argc < 2)
42  {
43  fprintf(
44  stderr, "Usage: %s [filename1] [filename2] ...\n\n", itksys::SystemTools::GetFilenameName(argv[0]).c_str());
45  return 1;
46  }
47 
48  // Register Qmitk-dependent global instances
50 
51  //*************************************************************************
52  // Part I: Basic initialization
53  //*************************************************************************
54 
55  // Create a DataStorage
57 
58  //*************************************************************************
59  // Part II: Create some data by reading files
60  //*************************************************************************
61  int i;
62  for (i = 1; i < argc; ++i)
63  {
64  // For testing
65  if (strcmp(argv[i], "-testing") == 0)
66  continue;
67 
68  //*********************************************************************
69  // Part III: Put the data into the datastorage
70  //*********************************************************************
71  // Load datanode (eg. many image formats, surface formats, etc.)
72  mitk::StandaloneDataStorage::SetOfObjects::Pointer dataNodes = mitk::IOUtil::Load(argv[i], *ds);
73 
74  if (dataNodes->empty())
75  {
76  fprintf(stderr, "Could not open file %s \n\n", argv[i]);
77  exit(2);
78  }
79  mitk::DataNode::Pointer node = dataNodes->at(0);
80 
81  // *********************************************************
82  // ********************* START OF NEW PART 1 (Step 3a) *****
83  // *********************************************************
84 
85  //*********************************************************************
86  // Part IV: We want all images to be volume-rendered
87  //*********************************************************************
88 
89  // Check if the data is an image by dynamic_cast-ing the data
90  // contained in the node. Warning: dynamic_cast's are rather slow,
91  // do not use it too often!
92  mitk::Image::Pointer image = dynamic_cast<mitk::Image *>(node->GetData());
93  if (image.IsNotNull())
94  {
95  // Set the property "volumerendering" to the Boolean value "true"
96  node->SetProperty("volumerendering", mitk::BoolProperty::New(true));
97 
98  // Create a transfer function to assign optical properties (color and opacity) to grey-values of the data
100  tf->InitializeByMitkImage(image);
101 
102  // Set the color transfer function AddRGBPoint(double x, double r, double g, double b)
103  tf->GetColorTransferFunction()->AddRGBPoint(tf->GetColorTransferFunction()->GetRange()[0], 1.0, 0.0, 0.0);
104  tf->GetColorTransferFunction()->AddRGBPoint(tf->GetColorTransferFunction()->GetRange()[1], 1.0, 1.0, 0.0);
105 
106  // Set the piecewise opacity transfer function AddPoint(double x, double y)
107  tf->GetScalarOpacityFunction()->AddPoint(0, 0);
108  tf->GetScalarOpacityFunction()->AddPoint(tf->GetColorTransferFunction()->GetRange()[1], 1);
109 
110  node->SetProperty("TransferFunction", mitk::TransferFunctionProperty::New(tf.GetPointer()));
111  }
112 
113  // *********************************************************
114  // ******************* END OF NEW PART 1 (Step 3a) *********
115  // *********************************************************
116  }
117 
118  //*************************************************************************
119  // Part V: Create window and pass the tree to it
120  //*************************************************************************
121 
122  // Create a renderwindow
123  QmitkRenderWindow renderWindow;
124 
125  // Tell the renderwindow which (part of) the datastorage to render
126  renderWindow.GetRenderer()->SetDataStorage(ds);
127 
128  // *********************************************************
129  // ****************** START OF NEW PART 2 (Step 3b) ********
130  // *********************************************************
131  // Use it as a 3D view!
133 
134  // Reposition the camera to include all visible actors
135  renderWindow.GetRenderer()->GetVtkRenderer()->ResetCamera();
136 
137  // *********************************************************
138  // ******************* END OF NEW PART 2 (Step 3b) *********
139  // *********************************************************
140 
141  //*************************************************************************
142  // Part VI: Qt-specific initialization
143  //*************************************************************************
144  renderWindow.show();
145  renderWindow.resize(256, 256);
146 
148 
149  return qtapplication.exec();
150 }
151 
vtkRenderer * GetVtkRenderer() const
void SetDataStorage(mitk::DataStorage *storage) override
set the datastorage that will be used for rendering
int main(int argc, char *argv[])
Change the type of display to 3D.
Definition: Step3.cpp:38
MITKQTWIDGETS_EXPORT void QmitkRegisterClasses()
static Pointer New()
static RenderingManager * GetInstance()
Image class for storing images.
Definition: mitkImage.h:72
MITK implementation of the QVTKWidget.
static Pointer New()
virtual mitk::VtkPropRenderer * GetRenderer()
mitk::Image::Pointer image
void SetMapperID(const MapperSlotId mapperId) override
Set the MapperSlotId to use.
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