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
mitkFiberBuilder.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 "mitkFiberBuilder.h"
18 
19 using namespace mitk;
20 
22 {
23  m_Grid = grid;
24  m_Image = image;
25  m_FiberLength = 0;
26 }
27 
29 {
30 
31 }
32 
33 vtkSmartPointer<vtkPolyData> FiberBuilder::iterate(int minFiberLength)
34 {
37 
38  int cur_label = 1;
39  int numFibers = 0;
40  m_FiberLength = 0;
41  for (int k = 0; k < m_Grid->m_NumParticles;k++)
42  {
43  Particle *dp = m_Grid->GetParticle(k);
44  if (dp->label == 0)
45  {
46  vtkSmartPointer<vtkPolyLine> container = vtkSmartPointer<vtkPolyLine>::New();
47  dp->label = cur_label;
48  LabelPredecessors(dp, -1, container);
49  LabelSuccessors(dp, 1, container);
50  cur_label++;
51  if(m_FiberLength >= minFiberLength)
52  {
53  m_VtkCellArray->InsertNextCell(container);
54  numFibers++;
55  }
56  m_FiberLength = 0;
57  }
58  }
59  for (int k = 0; k < m_Grid->m_NumParticles;k++)
60  {
61  Particle *dp = m_Grid->GetParticle(k);
62  dp->label = 0;
63  }
64 
65  vtkSmartPointer<vtkPolyData> fiberPolyData = vtkSmartPointer<vtkPolyData>::New();
66  fiberPolyData->SetPoints(m_VtkPoints);
67  fiberPolyData->SetLines(m_VtkCellArray);
68  return fiberPolyData;
69 }
70 
71 void FiberBuilder::LabelPredecessors(Particle* p, int ep, vtkPolyLine* container)
72 {
73  Particle* p2 = nullptr;
74  if (ep==1)
75  p2 = m_Grid->GetParticle(p->pID);
76  else
77  p2 = m_Grid->GetParticle(p->mID);
78 
79  if (p2!=nullptr && p2->label==0)
80  {
81  p2->label = 1; // assign particle to current fiber
82 
83  if (p2->pID==p->ID)
84  LabelPredecessors(p2, -1, container);
85  else if (p2->mID==p->ID)
86  LabelPredecessors(p2, 1, container);
87  else
88  std::cout << "FiberBuilder: connection inconsistent (LabelPredecessors)" << std::endl;
89  }
90 
91  AddPoint(p, container);
92 }
93 
94 
95 void FiberBuilder::LabelSuccessors(Particle* p, int ep, vtkPolyLine* container)
96 {
97  AddPoint(p, container);
98 
99  Particle* p2 = nullptr;
100  if (ep==1)
101  p2 = m_Grid->GetParticle(p->pID);
102  else
103  p2 = m_Grid->GetParticle(p->mID);
104 
105  if (p2!=nullptr && p2->label==0)
106  {
107  p2->label = 1; // assign particle to current fiber
108 
109  if (p2->pID==p->ID)
110  LabelSuccessors(p2, -1, container);
111  else if (p2->mID==p->ID)
112  LabelSuccessors(p2, 1, container);
113  else
114  std::cout << "FiberBuilder: connection inconsistent (LabelPredecessors)" << std::endl;
115  }
116 }
117 
118 void FiberBuilder::AddPoint(Particle *dp, vtkSmartPointer<vtkPolyLine> container)
119 {
120  if (dp->label!=1)
121  return;
122 
123  dp->label = 2;
124 
125  itk::ContinuousIndex<float, 3> index;
126  index[0] = dp->GetPos()[0]/m_Image->GetSpacing()[0]-0.5;
127  index[1] = dp->GetPos()[1]/m_Image->GetSpacing()[1]-0.5;
128  index[2] = dp->GetPos()[2]/m_Image->GetSpacing()[2]-0.5;
129  itk::Point<float> point;
130  m_Image->TransformContinuousIndexToPhysicalPoint( index, point );
131  vtkIdType id = m_VtkPoints->InsertNextPoint(point.GetDataPointer());
132  container->GetPointIds()->InsertNextId(id);
133 
134  if(container->GetNumberOfPoints()>1)
135  m_FiberLength += m_LastPoint.EuclideanDistanceTo(point);
136 
137  m_LastPoint = point;
138 }
vtkSmartPointer< vtkCellArray > m_VtkCellArray
A particle is the basic element of the Gibbs fiber tractography method.
Definition: mitkParticle.h:30
Contains and manages particles.
ItkFloatImageType::Pointer m_Image
vtkSmartPointer< vtkPoints > m_VtkPoints
void AddPoint(Particle *dp, vtkSmartPointer< vtkPolyLine > container)
DataCollection - Class to facilitate loading/accessing structured data.
void LabelPredecessors(Particle *p, int ep, vtkPolyLine *container)
itk::Point< float > m_LastPoint
void LabelSuccessors(Particle *p, int ep, vtkPolyLine *container)
vtkSmartPointer< vtkPolyData > iterate(int minFiberLength)
vnl_vector_fixed< float, 3 > & GetPos()
Definition: mitkParticle.h:51
Particle * GetParticle(int ID)
FiberBuilder(ParticleGrid *grid, ItkFloatImageType *image)
itk::Image< float, 3 > ItkFloatImageType
ParticleGrid * m_Grid
unsigned char label
Definition: mitkParticle.h:49
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.