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