Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkContourModelToSurfaceFilter.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 ===================================================================*/
17 #include <mitkSurface.h>
18 #include <vtkCellArray.h>
19 #include <vtkPoints.h>
20 #include <vtkPolyData.h>
21 #include <vtkPolygon.h>
22 #include <vtkSmartPointer.h>
23 
25 {
26  this->SetNthOutput(0, mitk::Surface::New().GetPointer());
27 }
29 {
30 }
31 
33 {
34 }
35 
37 {
38  this->SetInput(0, input);
39 }
40 
43 {
44  if (idx + 1 > this->GetNumberOfInputs())
45  {
46  this->SetNumberOfRequiredInputs(idx + 1);
47  }
48  if (input != static_cast<InputType *>(this->ProcessObject::GetInput(idx)))
49  {
50  this->ProcessObject::SetNthInput(idx, const_cast<InputType *>(input));
51  this->Modified();
52  }
53 }
54 
56 {
57  if (this->GetNumberOfInputs() < 1)
58  return nullptr;
59  return static_cast<const mitk::ContourModelToSurfaceFilter::InputType *>(this->ProcessObject::GetInput(0));
60 }
61 
63 {
64  if (this->GetNumberOfInputs() < 1)
65  return nullptr;
66  return static_cast<const mitk::ContourModelToSurfaceFilter::InputType *>(this->ProcessObject::GetInput(idx));
67 }
68 
70 {
71  mitk::Surface *surface = this->GetOutput();
72  mitk::ContourModel *inputContour = (mitk::ContourModel *)GetInput();
73 
74  unsigned int numberOfTimeSteps = inputContour->GetTimeSteps();
75  surface->Expand(numberOfTimeSteps);
76 
77  for (unsigned int currentTimeStep = 0; currentTimeStep < numberOfTimeSteps; currentTimeStep++)
78  {
79  /* First of all convert the control points of the contourModel to vtk points
80  * and add lines in between them
81  */
82  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); // the points to draw
83  vtkSmartPointer<vtkCellArray> polygons = vtkSmartPointer<vtkCellArray>::New();
84  vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New(); // the lines to connect the points
85 
86  // if the contour has less than 3 points, set empty PolyData for current timestep
87  // polygon needs at least 3 points
88  if (inputContour->GetNumberOfVertices(currentTimeStep) <= 2)
89  {
90  vtkSmartPointer<vtkPolyData> emptyPolyData = vtkSmartPointer<vtkPolyData>::New();
91  surface->SetVtkPolyData(emptyPolyData, currentTimeStep);
92  continue;
93  }
94 
95  // iterate over all control points
96  auto current = inputContour->IteratorBegin(currentTimeStep);
97 
98  auto end = inputContour->IteratorEnd(currentTimeStep);
99 
100  vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
101  polygon->GetPointIds()->SetNumberOfIds(inputContour->GetNumberOfVertices(currentTimeStep));
102 
103  int j(0);
104  while (current != end)
105  {
106  mitk::ContourModel::VertexType *currentPoint = *current;
107 
108  vtkIdType id = points->InsertNextPoint(
109  currentPoint->Coordinates[0], currentPoint->Coordinates[1], currentPoint->Coordinates[2]);
110 
111  polygon->GetPointIds()->SetId(j, id);
112 
113  // create connections between the points
114  // no previous point for first point available (ingnore id=0)
115  if (id > 0)
116  {
117  lines->InsertNextCell(2);
118  lines->InsertCellPoint(id - 1);
119  lines->InsertCellPoint(id);
120  }
121 
122  current++;
123  j++;
124  }
125 
126  /*
127  * If the contour is closed an additional line has to be created between the first point
128  * and the last point
129  */
130  if (inputContour->IsClosed(currentTimeStep))
131  {
132  lines->InsertNextCell(2);
133  lines->InsertCellPoint(0);
134  lines->InsertCellPoint((inputContour->GetNumberOfVertices(currentTimeStep) - 1));
135  }
136 
137  polygons->InsertNextCell(polygon);
138 
139  // Create a polydata to store everything in
140  vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
141  // Add the points to the dataset
142  polyData->SetPoints(points);
143  polyData->SetPolys(polygons);
144  polyData->SetLines(lines);
145  polyData->BuildLinks();
146 
147  surface->SetVtkPolyData(polyData, currentTimeStep);
148  }
149 }
Class for storing surfaces (vtkPolyData).
Definition: mitkSurface.h:32
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
mitk::Point3D Coordinates
Coordinates in 3D space.
unsigned int GetTimeSteps() const
Get the number of time steps from the TimeGeometry As the base data has not a data vector given by it...
Definition: mitkBaseData.h:346
VertexIterator IteratorBegin(int timestep=0) const
Returns a const VertexIterator at the start element of the contour.
virtual void SetVtkPolyData(vtkPolyData *polydata, unsigned int t=0)
virtual void SetInput(const InputType *input)
bool IsClosed(int timestep=0) const
Return if the contour is closed or not.
VertexIterator IteratorEnd(int timestep=0) const
Returns a const VertexIterator at the end element of the contour.
static Pointer New()
Represents a single vertex of contour.
virtual void Expand(unsigned int timeSteps=1) override
Expands the TimeGeometry to a number of TimeSteps.
int GetNumberOfVertices(int timestep=0) const
Returns the number of vertices at a given timestep.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.