Medical Imaging Interaction Toolkit  2018.4.99-08619e4f
Medical Imaging Interaction Toolkit
AnnotationExample.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 
17 
18 #include <QApplication>
19 #include <itksys/SystemTools.hxx>
20 
21 #include <mitkAnnotationUtils.h>
24 #include <mitkTextAnnotation2D.h>
25 #include <mitkTextAnnotation3D.h>
26 
27 #include <mitkIOUtil.h>
28 
29 //##Documentation
30 //## @brief Load image (nrrd format) and display it in a 2D view
31 int main(int argc, char *argv[])
32 {
33  QApplication qtapplication(argc, argv);
34 
35  if (argc < 2)
36  {
37  fprintf(stderr, "Usage: %s [filename] \n\n", itksys::SystemTools::GetFilenameName(argv[0]).c_str());
38  return 1;
39  }
40 
41  // Register Qmitk-dependent global instances
43 
45 
46  // Load datanode (eg. many image formats, surface formats, etc.)
47  mitk::IOUtil::Load(argv[1], *ds);
48 
49  // Create a RenderWindow
50  QmitkRenderWindow renderWindow;
51 
52  // Tell the RenderWindow which (part of) the datastorage to render
53  renderWindow.GetRenderer()->SetDataStorage(ds);
54 
55  // Initialize the RenderWindow
56  auto geo = ds->ComputeBoundingGeometry3D(ds->GetAll());
58  // mitk::RenderingManager::GetInstance()->InitializeViews();
59 
60  // Add Overlays
62  // Create a textAnnotation2D
64 
65  textAnnotation->SetText("Test!"); // set UTF-8 encoded text to render
66  textAnnotation->SetFontSize(40);
67  textAnnotation->SetColor(1, 0, 0); // Set text color to red
68  textAnnotation->SetOpacity(1);
69 
70  // The position of the Annotation can be set to a fixed coordinate on the display.
71  mitk::Point2D pos;
72  pos[0] = 10;
73  pos[1] = 20;
74  textAnnotation->SetPosition2D(pos);
75 
76  std::string rendererID = renderWindow.GetRenderer()->GetName();
77 
78  // The LayoutAnnotationRenderer can place the TextAnnotation2D at some defined corner positions
80  textAnnotation, rendererID, mitk::LayoutAnnotationRenderer::TopLeft, 5, 5, 1);
82 
85 
86  // This vector is used to define an offset for the annotations, in order to show them with a margin to the actual
87  // coordinate.
89  offset[0] = .5;
90  offset[1] = .5;
91  offset[2] = .5;
92 
93  // save references to Annotations so that they do not get deregistered
94  std::vector<mitk::TextAnnotation3D::Pointer> annotationReferences;
95 
96  // Just a loop to create some points
97  for (unsigned long i = 0; i < 10; i++)
98  {
99  // To each point, a TextAnnotation3D is created
101  mitk::Point3D point;
102  point[0] = i * 20;
103  point[1] = i * 30;
104  point[2] = i * -50;
105  pointset->InsertPoint(i, point);
106  textAnnotation3D->SetText("A Point");
107 
108  // The Position is set to the point coordinate to create an annotation to the point in the PointSet.
109  textAnnotation3D->SetPosition3D(point);
110 
111  // move the annotation away from the actual point
112  textAnnotation3D->SetOffsetVector(offset);
113 
114  annotationReferences.push_back(textAnnotation3D);
115  mitk::ManualPlacementAnnotationRenderer::AddAnnotation(textAnnotation3D, rendererID);
116  }
117 
118  // Get the MicroserviceID of the registered textAnnotation
119  std::string serviceID = textAnnotation->GetMicroserviceID();
120 
121  // The AnnotationUtils can retrieve any registered annotations by their microservice ID
123  // This way, it is possible to change the properties of Annotations without knowing their implementation
124  annotation->SetText("changed text!");
125 
126  // also show the created pointset
128  datanode->SetData(pointset);
129  datanode->SetName("pointSet");
130  ds->Add(datanode);
132  renderWindow.show();
133  renderWindow.resize(256, 256);
134 
135  renderWindow.show();
136  renderWindow.resize(256, 256);
137 
138  // cleanup: Remove References to DataStorage. This will delete the object
139  ds = nullptr;
140 }
virtual bool InitializeViews(const BaseGeometry *geometry, RequestType type=REQUEST_UPDATE_ALL, bool preserveRoughOrientationInWorldSpace=false)
void SetText(std::string text)
void SetDataStorage(mitk::DataStorage *storage) override
set the datastorage that will be used for rendering
static Pointer New()
XipAnnotationPart annotation
Definition: AnnotationP.h:115
static mitk::Annotation * GetAnnotation(const std::string &AnnotationID)
GetAnnotation returns a registered Annotation for a specified ID.
MITKQTWIDGETS_EXPORT void QmitkRegisterClasses()
static void AddAnnotation(Annotation *annotation, const std::string &rendererID, Alignment alignment=TopLeft, double marginX=5, double marginY=5, int priority=-1)
int main(int argc, char *argv[])
Load image (nrrd format) and display it in a 2D view.
static Pointer New()
static Pointer New()
static Vector3D offset
static Pointer New()
static RenderingManager * GetInstance()
MITK implementation of the QVTKWidget.
virtual mitk::VtkPropRenderer * GetRenderer()
static void AddAnnotation(Annotation *Annotation, const std::string &rendererID)
Base class for all Annotation This class is to be implemented in order to create Annotation which are...
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
const char * GetName() const
get the name of the Renderer