Medical Imaging Interaction Toolkit  2018.4.99-b7f3afaa
Medical Imaging Interaction Toolkit
mitkContourVtkMapper3D.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 "mitkContourVtkMapper3D.h"
14 #include "mitkColorProperty.h"
15 #include "mitkContour.h"
16 #include "mitkDataNode.h"
17 #include "mitkProperties.h"
18 #include "mitkVtkPropRenderer.h"
19 
20 #include <vtkActor.h>
21 #include <vtkAppendPolyData.h>
22 #include <vtkAssembly.h>
23 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
24 #include <vtkCellArray.h>
25 #pragma GCC diagnostic warning "-Wstrict-aliasing"
26 #include <vtkFollower.h>
27 #include <vtkLinearTransform.h>
28 #include <vtkPolyData.h>
29 #include <vtkPolyDataMapper.h>
30 #include <vtkPolygon.h>
31 #include <vtkProp3DCollection.h>
32 #include <vtkProperty.h>
33 #include <vtkRenderer.h>
34 #include <vtkSphereSource.h>
35 #include <vtkTubeFilter.h>
36 
38 {
39  m_VtkPolyDataMapper = vtkPolyDataMapper::New();
40  m_VtkPointList = vtkAppendPolyData::New();
41  m_Actor = vtkActor::New();
42  m_Actor->SetMapper(m_VtkPolyDataMapper);
43 
44  m_TubeFilter = vtkTubeFilter::New();
45 }
46 
48 {
50  m_VtkPolyDataMapper->Delete();
51 
52  if (m_TubeFilter)
53  m_TubeFilter->Delete();
54 
55  if (m_VtkPointList)
56  m_VtkPointList->Delete();
57 
58  if (m_Contour)
59  m_Contour->Delete();
60 
61  if (m_Actor)
62  m_Actor->Delete();
63 }
64 
66 {
67  return m_Actor;
68 }
69 
71 {
72  bool visible = true;
73  GetDataNode()->GetVisibility(visible, renderer, "visible");
74 
75  if (!visible)
76  {
77  m_Actor->VisibilityOff();
78  return;
79  }
80  m_Actor->VisibilityOn();
81 
82  m_Contour = vtkPolyData::New();
83 
84  mitk::Contour::Pointer input = const_cast<mitk::Contour *>(this->GetInput());
85  bool makeContour = true;
86 
87  if (makeContour)
88  {
89  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
90  vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
91 
92  int numPts = input->GetNumberOfPoints();
93  if (numPts > 200000)
94  numPts = 200000;
95  mitk::Contour::PathPointer path = input->GetContourPath();
96  mitk::Contour::PathType::InputType cstart = path->StartOfInput();
97  mitk::Contour::PathType::InputType cend = path->EndOfInput();
98  mitk::Contour::PathType::InputType cstep = (cend - cstart + 1) / numPts;
99  mitk::Contour::PathType::InputType ccur;
100 
101  vtkIdType ptIndex = 0;
102  vtkIdType lastPointIndex = 0;
103 
104  mitk::Contour::PointsContainerPointer contourPoints = input->GetPoints();
105 
106  double vtkpoint[3];
107 
108  int i;
109  float pointSize = 2;
110  this->GetDataNode()->GetFloatProperty("spheres size", pointSize);
111 
112  bool showPoints = true;
113  this->GetDataNode()->GetBoolProperty("show points", showPoints);
114  if (showPoints)
115  {
116  m_VtkPointList = vtkAppendPolyData::New();
117  }
118  for (i = 0, ccur = cstart; i < numPts; ++i, ccur += cstep)
119  {
120  itk2vtk(path->Evaluate(ccur), vtkpoint);
121  points->InsertPoint(ptIndex, vtkpoint);
122  if (ptIndex > 0)
123  {
124  vtkIdType cell[2] = {ptIndex - 1, ptIndex};
125  lines->InsertNextCell((vtkIdType)2, cell);
126  }
127  lastPointIndex = ptIndex;
128  ++ptIndex;
129 
130  if (showPoints)
131  {
132  vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
133 
134  sphere->SetRadius(pointSize);
135  sphere->SetCenter(vtkpoint);
136 
137  m_VtkPointList->AddInputConnection(sphere->GetOutputPort());
138  sphere->Update();
139  }
140  }
141 
142  if (input->GetClosed())
143  {
144  vtkIdType cell[2] = {lastPointIndex, 0};
145  lines->InsertNextCell((vtkIdType)2, cell);
146  }
147 
148  m_Contour->SetPoints(points);
149  m_Contour->SetLines(lines);
150 
151  m_TubeFilter->SetInputData(m_Contour);
152  m_TubeFilter->SetRadius(pointSize / 2.0f);
153  m_TubeFilter->SetNumberOfSides(8);
154  m_TubeFilter->Update();
155 
156  if (showPoints)
157  {
158  m_VtkPointList->AddInputConnection(m_TubeFilter->GetOutputPort());
159  m_VtkPolyDataMapper->SetInputConnection(m_VtkPointList->GetOutputPort());
160  }
161  else
162  {
163  m_VtkPolyDataMapper->SetInputConnection(m_TubeFilter->GetOutputPort());
164  }
165  double rgba[4] = {0.0f, 1.0f, 0.0f, 0.6f};
166  m_Actor->GetProperty()->SetColor(rgba);
167  m_Actor->SetMapper(m_VtkPolyDataMapper);
168  }
169 }
170 
172 {
173  return static_cast<const mitk::Contour *>(GetDataNode()->GetData());
174 }
virtual DataNode * GetDataNode() const
Get the DataNode containing the data to map. Method only returns valid DataNode Pointer if the mapper...
Definition: mitkMapper.cpp:31
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.
vtkSmartPointer< vtkPolyDataMapper > m_VtkPolyDataMapper
vtkSmartPointer< vtkActor > m_Actor
bool GetBoolProperty(const char *propertyKey, bool &boolValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for bool properties (instances of BoolProperty)
vtkSmartPointer< vtkAppendPolyData > m_VtkPointList
vtkProp * GetVtkProp(mitk::BaseRenderer *renderer) override
Stores vertices for drawing a contour.
Definition: mitkContour.h:31
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
bool GetVisibility(bool &visible, const mitk::BaseRenderer *renderer, const char *propertyKey="visible") const
Convenience access method for visibility properties (instances of BoolProperty with property-key "vis...
Definition: mitkDataNode.h:422
BoundingBoxType::PointsContainer::Pointer PointsContainerPointer
Definition: mitkContour.h:50
PathType::Pointer PathPointer
Definition: mitkContour.h:41
vtkSmartPointer< vtkTubeFilter > m_TubeFilter
void itk2vtk(const Tin &in, Tout &out)
vtkSmartPointer< vtkPolyData > m_Contour
virtual const mitk::Contour * GetInput()
void GenerateDataForRenderer(mitk::BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.