Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkLabelOverlay3D.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 "mitkLabelOverlay3D.h"
18 #include <mitkPointSet.h>
19 #include <vtkActor2D.h>
20 #include <vtkIntArray.h>
21 #include <vtkLabelPlacementMapper.h>
22 #include <vtkPointData.h>
23 #include <vtkPointSetToLabelHierarchy.h>
24 #include <vtkPolyData.h>
25 #include <vtkPolyDataMapper.h>
26 #include <vtkProperty2D.h>
27 #include <vtkStringArray.h>
28 #include <vtkTextProperty.h>
29 
30 mitk::LabelOverlay3D::LabelOverlay3D() : m_PointSetModifiedObserverTag(0)
31 {
32 }
33 
35 {
36  if (m_LabelCoordinates.IsNotNull())
37  m_LabelCoordinates->RemoveObserver(m_PointSetModifiedObserverTag);
38 }
39 
41 {
42 }
43 
45 {
47 
48  // Add label array.
50  m_Labels->SetNumberOfValues(0);
51  m_Labels->SetName("labels");
52 
53  // Add priority array.
55  m_Sizes->SetNumberOfValues(0);
56  m_Sizes->SetName("sizes");
57 
58  m_Points->GetPointData()->AddArray(m_Sizes);
59  m_Points->GetPointData()->AddArray(m_Labels);
60 
61  m_PointSetToLabelHierarchyFilter = vtkSmartPointer<vtkPointSetToLabelHierarchy>::New();
62  m_PointSetToLabelHierarchyFilter->SetInputData(m_Points);
63  m_PointSetToLabelHierarchyFilter->SetLabelArrayName("labels");
64  m_PointSetToLabelHierarchyFilter->SetPriorityArrayName("sizes");
65  m_PointSetToLabelHierarchyFilter->Update();
66 
68  m_LabelMapper->SetInputConnection(m_PointSetToLabelHierarchyFilter->GetOutputPort());
69 
70  m_LabelsActor = vtkSmartPointer<vtkActor2D>::New();
71  m_LabelsActor->SetMapper(m_LabelMapper);
72 }
73 
75 {
76  if (m_LabelCoordinates.IsNull())
77  {
78  MITK_WARN << "No pointset defined to print labels!";
79  return;
80  }
81  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
82 
83  if (ls->IsGenerateDataRequired(renderer, this))
84  {
85  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
86 
87  size_t pointsetsize = (size_t)m_LabelCoordinates->GetSize();
88  ls->m_Labels->SetNumberOfValues(pointsetsize);
89  ls->m_Sizes->SetNumberOfValues(pointsetsize);
90 
91  for (size_t i = 0; i < pointsetsize; i++)
92  {
93  mitk::Point3D coordinate = m_LabelCoordinates->GetPoint(i);
94  points->InsertNextPoint(coordinate[0] + GetOffsetVector(renderer)[0],
95  coordinate[1] + GetOffsetVector(renderer)[1],
96  coordinate[2] + GetOffsetVector(renderer)[2]);
97  if (m_LabelVector.size() > i)
98  ls->m_Labels->SetValue(i, m_LabelVector[i]);
99  else
100  ls->m_Labels->SetValue(i, "");
101 
102  if (m_PriorityVector.size() > i)
103  ls->m_Sizes->SetValue(i, m_PriorityVector[i]);
104  else
105  ls->m_Sizes->SetValue(i, 1);
106  }
107 
108  ls->m_Points->SetPoints(points);
109  ls->m_PointSetToLabelHierarchyFilter->Update();
110  ls->m_LabelMapper->Update();
111 
112  float color[3] = {1, 1, 1};
113  float opacity = 1.0;
114  GetColor(color, renderer);
115  GetOpacity(opacity, renderer);
116  ls->m_LabelsActor->GetProperty()->SetColor(color[0], color[1], color[2]);
117  ls->m_LabelsActor->GetProperty()->SetOpacity(opacity);
119  }
120 }
121 
123 {
124  LocalStorage *ls = this->m_LSH.GetLocalStorage(renderer);
125  return ls->m_LabelsActor;
126 }
127 
128 void mitk::LabelOverlay3D::SetLabelVector(const std::vector<std::string> &LabelVector)
129 {
130  m_LabelVector = LabelVector;
131  this->Modified();
132 }
133 
134 void mitk::LabelOverlay3D::SetPriorityVector(const std::vector<int> &PriorityVector)
135 {
136  m_PriorityVector = PriorityVector;
137  this->Modified();
138 }
139 
141 {
142  if (m_LabelCoordinates.IsNotNull())
143  {
144  m_LabelCoordinates->RemoveObserver(m_PointSetModifiedObserverTag);
145  m_PointSetModifiedObserverTag = 0;
146  m_LabelCoordinates = NULL;
147  }
148  if (LabelCoordinates.IsNull())
149  {
150  return;
151  }
152  m_LabelCoordinates = LabelCoordinates;
153  itk::MemberCommand<mitk::LabelOverlay3D>::Pointer _PropertyListModifiedCommand =
155  _PropertyListModifiedCommand->SetCallbackFunction(this, &mitk::LabelOverlay3D::PointSetModified);
156  m_PointSetModifiedObserverTag = m_LabelCoordinates->AddObserver(itk::ModifiedEvent(), _PropertyListModifiedCommand);
157  this->Modified();
158 }
159 
160 void mitk::LabelOverlay3D::PointSetModified(const itk::Object * /*caller*/, const itk::EventObject &)
161 {
162  this->Modified();
163 }
virtual vtkProp * GetVtkProp(BaseRenderer *renderer) const override
This method is implemented by the specific VTKOverlays in order to create the element as a vtkProp...
virtual ~LabelOverlay3D()
virtual destructor in order to derive from this class
~LocalStorage()
Default deconstructor of the local storage.
itk::SmartPointer< Self > Pointer
LocalStorage()
Default constructor of the local storage.
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...
vtkSmartPointer< vtkPolyData > m_Points
Organizes the rendering process.
void UpdateVtkOverlay(mitk::BaseRenderer *renderer) override
vtkSmartPointer< vtkPointSetToLabelHierarchy > m_PointSetToLabelHierarchyFilter
vtkSmartPointer< vtkActor2D > m_LabelsActor
#define MITK_WARN
Definition: mitkLogMacros.h:23
void SetLabelCoordinates(itk::SmartPointer< PointSet > LabelCoordinates)
Coordinates of the labels.
vtkSmartPointer< vtkStringArray > m_Labels
Internal class holding the vtkActor, etc. for each of the render windows.
void SetPriorityVector(const std::vector< int > &PriorityVector)
Optional: Provide a vector of priorities. The labels with higher priorities will be visible in lower ...
bool IsGenerateDataRequired(mitk::BaseRenderer *renderer, mitk::Overlay *overlay)
vtkSmartPointer< vtkLabelPlacementMapper > m_LabelMapper
LabelOverlay3D()
explicit constructor which disallows implicit conversions
vtkSmartPointer< vtkIntArray > m_Sizes
void PointSetModified(const itk::Object *, const itk::EventObject &)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.