Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkLabelAnnotation3D.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 "mitkLabelAnnotation3D.h"
14 #include <mitkPointSet.h>
15 #include <vtkActor2D.h>
16 #include <vtkIntArray.h>
17 #include <vtkLabelPlacementMapper.h>
18 #include <vtkPointData.h>
19 #include <vtkPointSetToLabelHierarchy.h>
20 #include <vtkPolyData.h>
21 #include <vtkPolyDataMapper.h>
22 #include <vtkProperty2D.h>
23 #include <vtkStringArray.h>
24 #include <vtkTextProperty.h>
25 
26 mitk::LabelAnnotation3D::LabelAnnotation3D() : m_PointSetModifiedObserverTag(0)
27 {
28 }
29 
31 {
32  if (m_LabelCoordinates.IsNotNull())
33  m_LabelCoordinates->RemoveObserver(m_PointSetModifiedObserverTag);
34  for (BaseRenderer *renderer : m_LSH.GetRegisteredBaseRenderer())
35  {
36  if (renderer)
37  {
38  this->RemoveFromBaseRenderer(renderer);
39  }
40  }
41 }
42 
44 {
45 }
46 
48 {
49  m_Points = vtkSmartPointer<vtkPolyData>::New();
50 
51  // Add label array.
52  m_Labels = vtkSmartPointer<vtkStringArray>::New();
53  m_Labels->SetNumberOfValues(0);
54  m_Labels->SetName("labels");
55 
56  // Add priority array.
57  m_Sizes = vtkSmartPointer<vtkIntArray>::New();
58  m_Sizes->SetNumberOfValues(0);
59  m_Sizes->SetName("sizes");
60 
61  m_Points->GetPointData()->AddArray(m_Sizes);
62  m_Points->GetPointData()->AddArray(m_Labels);
63 
64  m_PointSetToLabelHierarchyFilter = vtkSmartPointer<vtkPointSetToLabelHierarchy>::New();
65  m_PointSetToLabelHierarchyFilter->SetInputData(m_Points);
66  m_PointSetToLabelHierarchyFilter->SetLabelArrayName("labels");
67  m_PointSetToLabelHierarchyFilter->SetPriorityArrayName("sizes");
68  m_PointSetToLabelHierarchyFilter->Update();
69 
70  m_LabelMapper = vtkSmartPointer<vtkLabelPlacementMapper>::New();
71  m_LabelMapper->SetInputConnection(m_PointSetToLabelHierarchyFilter->GetOutputPort());
72 
73  m_LabelsActor = vtkSmartPointer<vtkActor2D>::New();
74  m_LabelsActor->SetMapper(m_LabelMapper);
75 }
76 
78 {
79  if (m_LabelCoordinates.IsNull())
80  {
81  MITK_WARN << "No pointset defined to print labels!";
82  return;
83  }
84  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
85 
86  if (ls->IsGenerateDataRequired(renderer, this))
87  {
88  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
89 
90  auto pointsetsize = (size_t)m_LabelCoordinates->GetSize();
91  ls->m_Labels->SetNumberOfValues(pointsetsize);
92  ls->m_Sizes->SetNumberOfValues(pointsetsize);
93 
94  for (size_t i = 0; i < pointsetsize; i++)
95  {
96  mitk::Point3D coordinate = m_LabelCoordinates->GetPoint(i);
97  points->InsertNextPoint(coordinate[0] + GetOffsetVector()[0],
98  coordinate[1] + GetOffsetVector()[1],
99  coordinate[2] + GetOffsetVector()[2]);
100  if (m_LabelVector.size() > i)
101  ls->m_Labels->SetValue(i, m_LabelVector[i]);
102  else
103  ls->m_Labels->SetValue(i, "");
104 
105  if (m_PriorityVector.size() > i)
106  ls->m_Sizes->SetValue(i, m_PriorityVector[i]);
107  else
108  ls->m_Sizes->SetValue(i, 1);
109  }
110 
111  ls->m_Points->SetPoints(points);
112  ls->m_PointSetToLabelHierarchyFilter->Update();
113  ls->m_LabelMapper->Update();
114 
115  float color[3] = {1, 1, 1};
116  float opacity = 1.0;
117  GetColor(color);
118  GetOpacity(opacity);
119  ls->m_LabelsActor->GetProperty()->SetColor(color[0], color[1], color[2]);
120  ls->m_LabelsActor->GetProperty()->SetOpacity(opacity);
122  }
123 }
124 
126 {
127  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
128  return ls->m_LabelsActor;
129 }
130 
131 void mitk::LabelAnnotation3D::SetLabelVector(const std::vector<std::string> &LabelVector)
132 {
133  m_LabelVector = LabelVector;
134  this->Modified();
135 }
136 
137 void mitk::LabelAnnotation3D::SetPriorityVector(const std::vector<int> &PriorityVector)
138 {
139  m_PriorityVector = PriorityVector;
140  this->Modified();
141 }
142 
144 {
145  if (m_LabelCoordinates.IsNotNull())
146  {
147  m_LabelCoordinates->RemoveObserver(m_PointSetModifiedObserverTag);
148  m_PointSetModifiedObserverTag = 0;
149  m_LabelCoordinates = nullptr;
150  }
151  if (LabelCoordinates.IsNull())
152  {
153  return;
154  }
155  m_LabelCoordinates = LabelCoordinates;
156  itk::MemberCommand<mitk::LabelAnnotation3D>::Pointer _PropertyListModifiedCommand =
157  itk::MemberCommand<mitk::LabelAnnotation3D>::New();
158  _PropertyListModifiedCommand->SetCallbackFunction(this, &mitk::LabelAnnotation3D::PointSetModified);
159  m_PointSetModifiedObserverTag = m_LabelCoordinates->AddObserver(itk::ModifiedEvent(), _PropertyListModifiedCommand);
160  this->Modified();
161 }
162 
163 void mitk::LabelAnnotation3D::PointSetModified(const itk::Object * /*caller*/, const itk::EventObject &)
164 {
165  this->Modified();
166 }
#define ls
Definition: MitkMCxyz.cpp:57
L * GetLocalStorage(mitk::BaseRenderer *forRenderer)
Retrieves a LocalStorage for a specific BaseRenderer.
vtkSmartPointer< vtkStringArray > m_Labels
std::vector< mitk::BaseRenderer * > GetRegisteredBaseRenderer()
Organizes the rendering process.
vtkSmartPointer< vtkPointSetToLabelHierarchy > m_PointSetToLabelHierarchyFilter
bool IsGenerateDataRequired(mitk::BaseRenderer *renderer, mitk::Annotation *Annotation)
void SetPriorityVector(const std::vector< int > &PriorityVector)
Optional: Provide a vector of priorities. The labels with higher priorities will be visible in lower ...
Internal class holding the vtkActor, etc. for each of the render windows.
vtkSmartPointer< vtkIntArray > m_Sizes
vtkSmartPointer< vtkActor2D > m_LabelsActor
~LabelAnnotation3D() override
virtual destructor in order to derive from this class
vtkProp * GetVtkProp(BaseRenderer *renderer) const override
This method is implemented by the specific VTKAnnotation in order to create the element as a vtkProp...
#define MITK_WARN
Definition: mitkLogMacros.h:19
mitk::LocalStorageHandler< LocalStorage > m_LSH
The LocalStorageHandler holds all LocalStorages for the render windows.
bool GetColor(float rgb[], const std::string &propertyKey="color") const
Convenience access method for color properties (instances of ColorProperty)
void UpdateVtkAnnotation(mitk::BaseRenderer *renderer) override
void SetLabelVector(const std::vector< std::string > &LabelVector)
Set the vector of labels that are shown to each corresponding point3D. The size has to be equal to th...
void RemoveFromBaseRenderer(BaseRenderer *renderer) override
Removes the Annotation from the specified renderer. It is not visible anymore then.
vtkSmartPointer< vtkPolyData > m_Points
LabelAnnotation3D()
explicit constructor which disallows implicit conversions
~LocalStorage()
Default deconstructor of the local storage.
bool GetOpacity(float &opacity, const std::string &propertyKey="opacity") const
Convenience access method for opacity properties (instances of FloatProperty)
vtkSmartPointer< vtkLabelPlacementMapper > m_LabelMapper
Point3D GetOffsetVector() const
LocalStorage()
Default constructor of the local storage.
void SetLabelCoordinates(itk::SmartPointer< PointSet > LabelCoordinates)
Coordinates of the labels.
void PointSetModified(const itk::Object *, const itk::EventObject &)