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