Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkTextOverlay3D.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 "mitkTextOverlay3D.h"
18 #include <vtkCamera.h>
19 #include <vtkFollower.h>
20 #include <vtkMath.h>
21 #include <vtkPolyDataMapper.h>
22 #include <vtkProperty.h>
23 #include <vtkTextActor3D.h>
24 #include <vtkTextProperty.h>
25 #include <vtkVectorText.h>
26 
28 {
29  mitk::Point3D position;
30  position.Fill(0);
31  this->SetPosition3D(position);
32  this->SetOffsetVector(position);
33  this->SetText("");
34  this->SetFontSize(20);
35  this->SetColor(1.0, 1.0, 1.0);
36 }
37 
39 {
40 }
41 
43 {
44 }
45 
47 {
48  // Create some text
49  m_textSource = vtkSmartPointer<vtkVectorText>::New();
50 
51  // Create a mapper
52  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
53  mapper->SetInputConnection(m_textSource->GetOutputPort());
54 
55  // Create a subclass of vtkActor: a vtkFollower that remains facing the camera
56  m_follower = vtkSmartPointer<vtkFollower>::New();
57  m_follower->SetMapper(mapper);
58  m_follower->GetProperty()->SetColor(1, 0, 0); // red
59  m_follower->SetScale(1);
60 }
61 
63 {
64  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
65  if (ls->IsGenerateDataRequired(renderer, this))
66  {
67  Point3D pos3d = GetPosition3D(renderer);
68  vtkRenderer *vtkRender = renderer->GetVtkRenderer();
69  if (vtkRender)
70  {
71  vtkCamera *camera = vtkRender->GetActiveCamera();
72  ls->m_follower->SetCamera(camera);
73  if (camera != nullptr)
74  {
75  // calculate the offset relative to the camera's view direction
76  Point3D offset = GetOffsetVector(renderer);
77 
78  Vector3D viewUp;
79  camera->GetViewUp(viewUp.GetDataPointer());
80  Vector3D cameraDirection;
81  camera->GetDirectionOfProjection(cameraDirection.GetDataPointer());
82  Vector3D viewRight;
83  vtkMath::Cross(cameraDirection.GetDataPointer(), viewUp.GetDataPointer(), viewRight.GetDataPointer());
84 
85  pos3d = pos3d + viewRight * offset[0] + viewUp * offset[1] + cameraDirection * offset[2];
86  }
87  }
88  ls->m_follower->SetPosition(pos3d.GetDataPointer());
89  ls->m_textSource->SetText(GetText().c_str());
90  float color[3] = {1, 1, 1};
91  float opacity = 1.0;
92  GetColor(color, renderer);
93  GetOpacity(opacity, renderer);
94  ls->m_follower->GetProperty()->SetColor(color[0], color[1], color[2]);
95  ls->m_follower->GetProperty()->SetOpacity(opacity);
96  ls->m_follower->SetScale(this->GetFontSize());
98  }
99 }
100 
102 {
103  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
104  return ls->m_follower;
105 }
virtual vtkProp * GetVtkProp(BaseRenderer *renderer) const override
This method is implemented by the specific VTKOverlays in order to create the element as a vtkProp...
Organizes the rendering process.
void SetPosition3D(const Point3D &position3D, mitk::BaseRenderer *renderer=NULL)
Internal class holding the mapper, actor, etc. for each of the render windows.
void SetColor(const mitk::Color &color, mitk::BaseRenderer *renderer=nullptr, const std::string &propertyKey="color")
Convenience method for setting color properties (instances of ColorProperty)
~LocalStorage()
Default deconstructor of the local storage.
static Vector3D offset
vtkSmartPointer< vtkVectorText > m_textSource
void UpdateVtkOverlay(mitk::BaseRenderer *renderer) override
void SetFontSize(int fontSize, mitk::BaseRenderer *renderer=nullptr)
vtkSmartPointer< vtkFollower > m_follower
Actor of a 2D render window.
void SetOffsetVector(const Point3D &OffsetVector, mitk::BaseRenderer *renderer=NULL)
TextOverlay3D()
explicit constructor which disallows implicit conversions
bool IsGenerateDataRequired(mitk::BaseRenderer *renderer, mitk::Overlay *overlay)
void SetText(std::string text, mitk::BaseRenderer *renderer=nullptr)
vtkRenderer * GetVtkRenderer() const
LocalStorage()
Default constructor of the local storage.
virtual ~TextOverlay3D()
virtual destructor in order to derive from this class
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.