Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
itkFiberCurvatureFilter.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 
18 #define _USE_MATH_DEFINES
19 #include <math.h>
20 #include <vtkDoubleArray.h>
21 #include <vtkPointData.h>
22 #include <boost/progress.hpp>
23 
24 namespace itk{
25 
27  : m_AngularDeviation(30)
28  , m_Distance(5.0)
29  , m_RemoveFibers(false)
30 {
31 
32 }
33 
35 {
36 
37 }
38 
40 {
41  vtkSmartPointer<vtkPolyData> inputPoly = m_InputFiberBundle->GetFiberPolyData();
42  vtkSmartPointer<vtkPoints> vtkNewPoints = vtkSmartPointer<vtkPoints>::New();
43  vtkSmartPointer<vtkCellArray> vtkNewCells = vtkSmartPointer<vtkCellArray>::New();
44 
45  MITK_INFO << "Applying curvature threshold";
46  boost::progress_display disp(inputPoly->GetNumberOfCells());
47  for (int i=0; i<inputPoly->GetNumberOfCells(); i++)
48  {
49  ++disp;
50  vtkCell* cell = inputPoly->GetCell(i);
51  int numPoints = cell->GetNumberOfPoints();
52  vtkPoints* points = cell->GetPoints();
53 
54  // calculate curvatures
55  vtkSmartPointer<vtkPolyLine> container = vtkSmartPointer<vtkPolyLine>::New();
56  for (int j=0; j<numPoints; j++)
57  {
58  double dist = 0;
59  int c = j;
60  std::vector< vnl_vector_fixed< float, 3 > > vectors;
61  vnl_vector_fixed< float, 3 > meanV; meanV.fill(0.0);
62  while(dist<m_Distance/2 && c>1)
63  {
64  double p1[3];
65  points->GetPoint(c-1, p1);
66  double p2[3];
67  points->GetPoint(c, p2);
68 
69  vnl_vector_fixed< float, 3 > v;
70  v[0] = p2[0]-p1[0];
71  v[1] = p2[1]-p1[1];
72  v[2] = p2[2]-p1[2];
73  dist += v.magnitude();
74  v.normalize();
75  vectors.push_back(v);
76  if (c==j)
77  meanV += v;
78  c--;
79  }
80  c = j;
81  dist = 0;
82  while(dist<m_Distance/2 && c<numPoints-1)
83  {
84  double p1[3];
85  points->GetPoint(c, p1);
86  double p2[3];
87  points->GetPoint(c+1, p2);
88 
89  vnl_vector_fixed< float, 3 > v;
90  v[0] = p2[0]-p1[0];
91  v[1] = p2[1]-p1[1];
92  v[2] = p2[2]-p1[2];
93  dist += v.magnitude();
94  v.normalize();
95  vectors.push_back(v);
96  if (c==j)
97  meanV += v;
98  c++;
99  }
100  meanV.normalize();
101 
102  double dev = 0;
103  for (auto vec : vectors)
104  {
105  double angle = dot_product(meanV, vec);
106  if (angle>1.0)
107  angle = 1.0;
108  if (angle<-1.0)
109  angle = -1.0;
110  dev += acos(angle)*180/M_PI;
111  }
112  if (vectors.size()>0)
113  dev /= vectors.size();
114 
115  if (dev<m_AngularDeviation)
116  {
117  double p[3];
118  points->GetPoint(j, p);
119  vtkIdType id = vtkNewPoints->InsertNextPoint(p);
120  container->GetPointIds()->InsertNextId(id);
121  }
122  else
123  {
124  if (m_RemoveFibers)
125  {
126  container = vtkSmartPointer<vtkPolyLine>::New();
127  break;
128  }
129 
130  if (container->GetNumberOfPoints()>0)
131  vtkNewCells->InsertNextCell(container);
132  container = vtkSmartPointer<vtkPolyLine>::New();
133  }
134  }
135  if (container->GetNumberOfPoints()>0)
136  vtkNewCells->InsertNextCell(container);
137  }
138 
139  vtkSmartPointer<vtkPolyData> outputPoly = vtkSmartPointer<vtkPolyData>::New();
140  outputPoly->SetPoints(vtkNewPoints);
141  outputPoly->SetLines(vtkNewCells);
143 }
144 
145 }
146 
147 
148 
149 
#define MITK_INFO
Definition: mitkLogMacros.h:22
FiberBundle::Pointer m_InputFiberBundle
FiberBundle::Pointer m_OutputFiberBundle
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.