Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
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 "mitkContourVtkMapper3D.h"
18 #include "mitkColorProperty.h"
19 #include "mitkContour.h"
20 #include "mitkDataNode.h"
21 #include "mitkProperties.h"
22 #include "mitkVtkPropRenderer.h"
23 
24 #include <vtkActor.h>
25 #include <vtkAppendPolyData.h>
26 #include <vtkAssembly.h>
27 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
28 #include <vtkCellArray.h>
29 #pragma GCC diagnostic warning "-Wstrict-aliasing"
30 #include <vtkFollower.h>
31 #include <vtkLinearTransform.h>
32 #include <vtkPolyData.h>
33 #include <vtkPolyDataMapper.h>
34 #include <vtkPolygon.h>
35 #include <vtkProp3DCollection.h>
36 #include <vtkProperty.h>
37 #include <vtkRenderer.h>
38 #include <vtkSphereSource.h>
39 #include <vtkTubeFilter.h>
40 
42 {
46  m_Actor->SetMapper(m_VtkPolyDataMapper);
47 
49 }
50 
52 {
53  if (m_VtkPolyDataMapper)
54  m_VtkPolyDataMapper->Delete();
55 
56  if (m_TubeFilter)
57  m_TubeFilter->Delete();
58 
59  if (m_VtkPointList)
60  m_VtkPointList->Delete();
61 
62  if (m_Contour)
63  m_Contour->Delete();
64 
65  if (m_Actor)
66  m_Actor->Delete();
67 }
68 
70 {
71  return m_Actor;
72 }
73 
75 {
76  bool visible = true;
77  GetDataNode()->GetVisibility(visible, renderer, "visible");
78 
79  if (!visible)
80  {
81  m_Actor->VisibilityOff();
82  return;
83  }
84  m_Actor->VisibilityOn();
85 
86  m_Contour = vtkPolyData::New();
87 
88  mitk::Contour::Pointer input = const_cast<mitk::Contour *>(this->GetInput());
89  bool makeContour = true;
90 
91  if (makeContour)
92  {
93  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
94  vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
95 
96  int numPts = input->GetNumberOfPoints();
97  if (numPts > 200000)
98  numPts = 200000;
99  mitk::Contour::PathPointer path = input->GetContourPath();
100  mitk::Contour::PathType::InputType cstart = path->StartOfInput();
101  mitk::Contour::PathType::InputType cend = path->EndOfInput();
102  mitk::Contour::PathType::InputType cstep = (cend - cstart + 1) / numPts;
103  mitk::Contour::PathType::InputType ccur;
104 
105  vtkIdType ptIndex = 0;
106  vtkIdType lastPointIndex = 0;
107 
108  mitk::Contour::PointsContainerPointer contourPoints = input->GetPoints();
109 
110  double vtkpoint[3];
111 
112  int i;
113  float pointSize = 2;
114  this->GetDataNode()->GetFloatProperty("spheres size", pointSize);
115 
116  bool showPoints = true;
117  this->GetDataNode()->GetBoolProperty("show points", showPoints);
118  if (showPoints)
119  {
120  m_VtkPointList = vtkAppendPolyData::New();
121  }
122  for (i = 0, ccur = cstart; i < numPts; ++i, ccur += cstep)
123  {
124  itk2vtk(path->Evaluate(ccur), vtkpoint);
125  points->InsertPoint(ptIndex, vtkpoint);
126  if (ptIndex > 0)
127  {
128  vtkIdType cell[2] = {ptIndex - 1, ptIndex};
129  lines->InsertNextCell((vtkIdType)2, cell);
130  }
131  lastPointIndex = ptIndex;
132  ++ptIndex;
133 
134  if (showPoints)
135  {
136  vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
137 
138  sphere->SetRadius(pointSize);
139  sphere->SetCenter(vtkpoint);
140 
141  m_VtkPointList->AddInputConnection(sphere->GetOutputPort());
142  sphere->Update();
143  }
144  }
145 
146  if (input->GetClosed())
147  {
148  vtkIdType cell[2] = {lastPointIndex, 0};
149  lines->InsertNextCell((vtkIdType)2, cell);
150  }
151 
152  m_Contour->SetPoints(points);
153  m_Contour->SetLines(lines);
154 
155  m_TubeFilter->SetInputData(m_Contour);
156  m_TubeFilter->SetRadius(pointSize / 2.0f);
157  m_TubeFilter->SetNumberOfSides(8);
158  m_TubeFilter->Update();
159 
160  if (showPoints)
161  {
162  m_VtkPointList->AddInputConnection(m_TubeFilter->GetOutputPort());
163  m_VtkPolyDataMapper->SetInputConnection(m_VtkPointList->GetOutputPort());
164  }
165  else
166  {
167  m_VtkPolyDataMapper->SetInputConnection(m_TubeFilter->GetOutputPort());
168  }
169  double rgba[4] = {0.0f, 1.0f, 0.0f, 0.6f};
170  m_Actor->GetProperty()->SetColor(rgba);
171  m_Actor->SetMapper(m_VtkPolyDataMapper);
172  }
173 
174  SetVtkMapperImmediateModeRendering(m_VtkPolyDataMapper);
175 }
176 
178 {
179  return static_cast<const mitk::Contour *>(GetDataNode()->GetData());
180 }
Organizes the rendering process.
vtkSmartPointer< vtkPolyDataMapper > m_VtkPolyDataMapper
vtkSmartPointer< vtkActor > m_Actor
vtkSmartPointer< vtkAppendPolyData > m_VtkPointList
virtual vtkProp * GetVtkProp(mitk::BaseRenderer *renderer) override
Stores vertices for drawing a contour.
Definition: mitkContour.h:35
BoundingBoxType::PointsContainer::Pointer PointsContainerPointer
Definition: mitkContour.h:52
PathType::Pointer PathPointer
Definition: mitkContour.h:43
vtkSmartPointer< vtkTubeFilter > m_TubeFilter
void itk2vtk(const Tin &in, Tout &out)
virtual const mitk::Contour * GetInput()
virtual void GenerateDataForRenderer(mitk::BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.