Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkTextAnnotation3D.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 "mitkTextAnnotation3D.h"
14 #include <vtkCamera.h>
15 #include <vtkFollower.h>
16 #include <vtkMath.h>
17 #include <vtkPolyDataMapper.h>
18 #include <vtkProperty.h>
19 #include <vtkTextActor3D.h>
20 #include <vtkTextProperty.h>
21 #include <vtkVectorText.h>
22 
24 {
25  mitk::Point3D position;
26  position.Fill(0);
27  this->SetPosition3D(position);
28  this->SetOffsetVector(position);
29  this->SetText("");
30  this->SetFontSize(20);
31  this->SetColor(1.0, 1.0, 1.0);
32 }
33 
35 {
36  for (BaseRenderer *renderer : m_LSH.GetRegisteredBaseRenderer())
37  {
38  if (renderer)
39  {
40  this->RemoveFromBaseRenderer(renderer);
41  }
42  }
43 }
44 
46 {
47 }
48 
50 {
51  // Create some text
52  m_textSource = vtkSmartPointer<vtkVectorText>::New();
53 
54  // Create a mapper
55  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
56  mapper->SetInputConnection(m_textSource->GetOutputPort());
57 
58  // Create a subclass of vtkActor: a vtkFollower that remains facing the camera
59  m_follower = vtkSmartPointer<vtkFollower>::New();
60  m_follower->SetMapper(mapper);
61  m_follower->GetProperty()->SetColor(1, 0, 0); // red
62  m_follower->SetScale(1);
63 }
64 
66 {
67  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
68  if (ls->IsGenerateDataRequired(renderer, this))
69  {
70  Point3D pos3d = GetPosition3D();
71  vtkRenderer *vtkRender = renderer->GetVtkRenderer();
72  if (vtkRender)
73  {
74  vtkCamera *camera = vtkRender->GetActiveCamera();
75  ls->m_follower->SetCamera(camera);
76  if (camera != nullptr)
77  {
78  // calculate the offset relative to the camera's view direction
80 
81  Vector3D viewUp;
82  camera->GetViewUp(viewUp.GetDataPointer());
83  Vector3D cameraDirection;
84  camera->GetDirectionOfProjection(cameraDirection.GetDataPointer());
85  Vector3D viewRight;
86  vtkMath::Cross(cameraDirection.GetDataPointer(), viewUp.GetDataPointer(), viewRight.GetDataPointer());
87 
88  pos3d = pos3d + viewRight * offset[0] + viewUp * offset[1] + cameraDirection * offset[2];
89  }
90  }
91  ls->m_follower->SetPosition(pos3d.GetDataPointer());
92  ls->m_textSource->SetText(GetText().c_str());
93  float color[3] = {1, 1, 1};
94  float opacity = 1.0;
95  GetColor(color);
96  GetOpacity(opacity);
97  ls->m_follower->GetProperty()->SetColor(color[0], color[1], color[2]);
98  ls->m_follower->GetProperty()->SetOpacity(opacity);
99  ls->m_follower->SetScale(this->GetFontSize());
101  }
102 }
103 
105 {
106  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
107  return ls->m_follower;
108 }
#define ls
Definition: MitkMCxyz.cpp:57
~TextAnnotation3D() override
virtual destructor in order to derive from this class
L * GetLocalStorage(mitk::BaseRenderer *forRenderer)
Retrieves a LocalStorage for a specific BaseRenderer.
void SetText(std::string text)
std::vector< mitk::BaseRenderer * > GetRegisteredBaseRenderer()
vtkRenderer * GetVtkRenderer() const
Organizes the rendering process.
bool IsGenerateDataRequired(mitk::BaseRenderer *renderer, mitk::Annotation *Annotation)
vtkSmartPointer< vtkVectorText > m_textSource
void UpdateVtkAnnotation(mitk::BaseRenderer *renderer) override
TextAnnotation3D()
explicit constructor which disallows implicit conversions
void SetColor(const mitk::Color &color, const std::string &propertyKey="color")
Convenience method for setting color properties (instances of ColorProperty)
~LocalStorage()
Default deconstructor of the local storage.
static Vector3D offset
Internal class holding the mapper, actor, etc. for each of the render windows.
vtkSmartPointer< vtkFollower > m_follower
Actor of a 2D render window.
void SetOffsetVector(const Point3D &OffsetVector)
bool GetColor(float rgb[], const std::string &propertyKey="color") const
Convenience access method for color properties (instances of ColorProperty)
LocalStorage()
Default constructor of the local storage.
int GetFontSize() const
void RemoveFromBaseRenderer(BaseRenderer *renderer) override
Removes the Annotation from the specified renderer. It is not visible anymore then.
mitk::LocalStorageHandler< LocalStorage > m_LSH
The LocalStorageHandler holds all LocalStorages for the render windows.
std::string GetText() const
void SetPosition3D(const Point3D &position3D)
bool GetOpacity(float &opacity, const std::string &propertyKey="opacity") const
Convenience access method for opacity properties (instances of FloatProperty)
Point3D GetOffsetVector() const
void SetFontSize(int fontSize)
vtkProp * GetVtkProp(BaseRenderer *renderer) const override
This method is implemented by the specific VTKAnnotation in order to create the element as a vtkProp...