Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPlanarFigureVtkMapper3D.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 
15 #include "mitkImage.h"
16 #include "mitkPlaneGeometry.h"
17 #include <mitkPlanarFigure.h>
18 #include <vtkCellArray.h>
19 #include <vtkIdList.h>
20 #include <vtkPoints.h>
21 #include <vtkPolyData.h>
22 #include <vtkPolyDataMapper.h>
23 #include <vtkPolyLine.h>
24 #include <vtkPolygon.h>
25 
26 mitk::PlanarFigureVtkMapper3D::LocalStorage::LocalStorage() : m_Actor(vtkSmartPointer<vtkActor>::New()), m_LastMTime(0)
27 {
28 }
29 
30 mitk::PlanarFigureVtkMapper3D::LocalStorage::~LocalStorage()
31 {
32 }
33 
35 {
36  node->AddProperty("planarfigure.3drendering", mitk::BoolProperty::New(false));
37  node->AddProperty("planarfigure.3drendering.fill", mitk::BoolProperty::New(false));
38 }
39 
40 mitk::PlanarFigureVtkMapper3D::PlanarFigureVtkMapper3D() : m_FillPf(false)
41 {
42 }
43 
44 mitk::PlanarFigureVtkMapper3D::~PlanarFigureVtkMapper3D()
45 {
46 }
47 
49 {
50  if (actor == nullptr)
51  return;
52 
53  const mitk::DataNode *dataNode = this->GetDataNode();
54 
55  if (dataNode == nullptr)
56  return;
57 
58  bool selected = false;
59  dataNode->GetBoolProperty("selected", selected, renderer);
60 
61  float color[3];
62  dataNode->GetColor(color, renderer, selected ? "planarfigure.selected.line.color" : "color");
63 
64  float opacity = 1.0f;
65  dataNode->GetOpacity(opacity, renderer);
66 
67  vtkProperty *property = actor->GetProperty();
68  property->SetColor(color[0], color[1], color[2]);
69  property->SetOpacity(opacity);
70 }
71 
73 {
74  if (actor == nullptr)
75  return;
76 
77  const mitk::DataNode *dataNode = this->GetDataNode();
78 
79  if (dataNode == nullptr)
80  return;
81 
82  bool render = false;
83  dataNode->GetBoolProperty("planarfigure.3drendering", render);
84 
85  actor->SetVisibility(render);
86 
87  float lineWidth = 1.0f;
88  dataNode->GetFloatProperty("planarfigure.line.width", lineWidth, renderer);
89 
90  vtkProperty *property = actor->GetProperty();
91  property->SetLineWidth(lineWidth);
92 }
93 
94 void mitk::PlanarFigureVtkMapper3D::GenerateDataForRenderer(BaseRenderer *renderer)
95 {
96  typedef PlanarFigure::PolyLineType PolyLine;
97 
98  const DataNode *node = this->GetDataNode();
99 
100  if (node == nullptr)
101  return;
102 
103  auto *planarFigure = dynamic_cast<PlanarFigure *>(node->GetData());
104 
105  if (planarFigure == nullptr || !planarFigure->IsPlaced())
106  return;
107 
108  LocalStorage *localStorage = m_LocalStorageHandler.GetLocalStorage(renderer);
109  unsigned long mTime = planarFigure->GetMTime();
110 
111  bool fillPf = false;
112  bool refresh = false;
113  node->GetBoolProperty("planarfigure.3drendering.fill", fillPf);
114  if (m_FillPf != fillPf)
115  {
116  m_FillPf = fillPf;
117  refresh = true;
118  }
119 
120  if (mTime > localStorage->m_LastMTime || refresh)
121  {
122  localStorage->m_LastMTime = mTime;
123 
124  const auto *planeGeometry = dynamic_cast<const PlaneGeometry *>(planarFigure->GetPlaneGeometry());
125  const auto *abstractTransformGeometry =
126  dynamic_cast<const AbstractTransformGeometry *>(planarFigure->GetPlaneGeometry());
127 
128  if (planeGeometry == nullptr && abstractTransformGeometry == nullptr)
129  return;
130 
131  const size_t numPolyLines = planarFigure->GetPolyLinesSize();
132 
133  if (numPolyLines == 0)
134  return;
135 
136  const vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
137  const vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
138  const vtkSmartPointer<vtkCellArray> polygons = vtkSmartPointer<vtkCellArray>::New();
139  vtkIdType baseIndex = 0;
140 
141  for (size_t i = 0; i < numPolyLines; ++i)
142  {
143  const PolyLine polyLine = planarFigure->GetPolyLine(i);
144  const vtkIdType numPoints = polyLine.size();
145 
146  vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
147 
148  if (numPoints < 2)
149  continue;
150 
151  auto polyLineEnd = polyLine.cend();
152 
153  for (auto polyLineIt = polyLine.cbegin(); polyLineIt != polyLineEnd; ++polyLineIt)
154  {
155  Point3D point;
156  planeGeometry->Map(*polyLineIt, point);
157  points->InsertNextPoint(point.GetDataPointer());
158 
159  const vtkIdType id = polygon->GetPoints()->InsertNextPoint(point[0], point[1], point[2]);
160  polygon->GetPointIds()->InsertNextId(id);
161  }
162 
163  vtkSmartPointer<vtkPolyLine> line = vtkSmartPointer<vtkPolyLine>::New();
164 
165  vtkIdList *pointIds = line->GetPointIds();
166 
167  if (planarFigure->IsClosed() && numPoints > 2)
168  {
169  polygons->InsertNextCell(polygon);
170  pointIds->SetNumberOfIds(numPoints + 1);
171  pointIds->SetId(numPoints, baseIndex);
172  }
173  else
174  {
175  pointIds->SetNumberOfIds(numPoints);
176  }
177 
178  for (vtkIdType j = 0; j < numPoints; ++j)
179  pointIds->SetId(j, baseIndex + j);
180 
181  cells->InsertNextCell(line);
182 
183  baseIndex += points->GetNumberOfPoints();
184  }
185 
186  vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
187  polyData->SetPoints(points);
188  polyData->SetLines(cells);
189  if (m_FillPf)
190  polyData->SetPolys(polygons);
191 
192  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
193  mapper->SetInputData(polyData);
194 
195  localStorage->m_Actor->SetMapper(mapper);
196  }
197 
198  this->ApplyColorAndOpacityProperties(renderer, localStorage->m_Actor);
199  this->ApplyPlanarFigureProperties(renderer, localStorage->m_Actor);
200 }
201 
203 {
204  return m_LocalStorageHandler.GetLocalStorage(renderer)->m_Actor;
205 }
206 
208 {
209 }
static char * line
Definition: svm.cpp:2870
bool GetFloatProperty(const char *propertyKey, float &floatValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for float properties (instances of FloatProperty)
Organizes the rendering process.
bool GetBoolProperty(const char *propertyKey, bool &boolValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for bool properties (instances of BoolProperty)
bool GetOpacity(float &opacity, const mitk::BaseRenderer *renderer, const char *propertyKey="opacity") const
Convenience access method for opacity properties (instances of FloatProperty)
static void SetDefaultProperties(DataNode *, BaseRenderer *=nullptr, bool=false)
static Pointer New()
vtkProp * GetVtkProp(BaseRenderer *renderer) override
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
void AddProperty(const char *propertyKey, BaseProperty *property, const mitk::BaseRenderer *renderer=nullptr, bool overwrite=false)
Add the property (instance of BaseProperty) if it does not exist (or always ifoverwrite istrue) with ...
bool GetColor(float rgb[3], const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="color") const
Convenience access method for color properties (instances of ColorProperty)
void ApplyPlanarFigureProperties(BaseRenderer *renderer, vtkActor *actor)
void ApplyColorAndOpacityProperties(BaseRenderer *renderer, vtkActor *actor) override
Apply color and opacity properties read from the PropertyList. Called by mapper subclasses.
Describes a geometry defined by an vtkAbstractTransform and a plane.
unsigned long GetMTime() const override
Get the modified time of the last change of the contents this data object or its geometry.
void UpdateVtkTransform(BaseRenderer *) override
Set the vtkTransform of the m_Prop3D for the current time step of renderer.
Base-class for geometric planar (2D) figures, such as lines, circles, rectangles, polygons...
std::vector< PolyLineElement > PolyLineType
Describes a two-dimensional, rectangular plane.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57