DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkFiberTracts.hxx
Go to the documentation of this file.
1 
12 #ifndef __itkFiberTracts_hxx
13 #define __itkFiberTracts_hxx
14 
15 #include "itkFiberTracts.h"
16 
17 namespace itk
18 {
19 
20 template< class TValue >
21 typename LightObject::Pointer
24 {
25  typename LightObject::Pointer loPtr = Superclass::InternalClone();
26 
27  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
28  if(rval.IsNull())
29  {
30  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
31  }
32 
33  rval->m_Header = m_Header;
34  rval->m_Fibers = m_Fibers;
35  return loPtr;
36 }
37 
38 template< class TValue >
41 ::DeepClone() const
42 {
43  Pointer out = Self::New();
44  itk::CopyTrackvisHeader(*m_Header, *out->m_Header);
45  auto fibers = out->GetFibers();
46  int num = GetNumberOfFibers();
47  for ( int i = 0; i < num; ++i )
48  {
49  auto fiber = this->GetFiber(i)->DeepClone();
50  fibers->InsertElement( fibers->Size(), fiber);
51  }
52  return out;
53 }
54 
55 template< class TValue >
56 int
59 {
60  int num=0;
61  for ( int i = 0; i < GetNumberOfFibers(); ++i )
62  num += GetFiber(i)->GetNumberOfPoints();
63  return num;
64 }
65 
66 template< class TValue >
67 void
69 ::GetPointStats(std::vector<double>& numPointsStats, std::vector<double>& pointDistStats) const
70 {
71  std::vector<double> numVec, distVec;
72  for ( int i = 0; i < GetNumberOfFibers(); ++i )
73  {
74  auto fiber = GetFiber(i);
75  numVec.push_back(fiber->GetNumberOfPoints());
76  auto vec = fiber->GetPointDistanceStats();
77  distVec.push_back(vec[2]); // mean dist
78  }
79  numPointsStats = utl::GetContainerStats(numVec.begin(), numVec.end());
80  pointDistStats = utl::GetContainerStats(distVec.begin(), distVec.end());
81 }
82 
83 template< class TValue >
84 void
86 ::RemoveScalarsByName(const std::string& name)
87 {
88  std::vector<std::string> scalarNames = utl::CovertChar2DArrayToStringArray(GetHeader()->scalar_name, 10);
89  for ( int i = 0; i < GetNumberOfFibers(); ++i )
90  this->GetFiber(i)->RemoveScalarsByName(name, scalarNames);
91 
92  itk::RemoveScalarName(*m_Header, name);
93 }
94 
95 template< class TValue >
96 void
98 ::RemovePropertiesByName(const std::string& name)
99 {
100  std::vector<std::string> scalarNames = utl::CovertChar2DArrayToStringArray(GetHeader()->property_name, 10);
101  for ( int i = 0; i < GetNumberOfFibers(); ++i )
102  this->GetFiber(i)->RemovePropertiesByName(name, scalarNames);
103 
104  itk::RemovePropertyName(*m_Header, name);
105 }
106 
107 template< class TValue >
110 ::SelectByIndicesOfTracts(const std::vector<int>& indices)
111 {
112  Pointer result = Self::New();
113  itk::CopyTrackvisHeader(*m_Header, *result->m_Header);
114  result->m_Header->n_count = indices.size();
115 
116  auto fibers = result->GetFibers();
117  for ( int i = 0; i < indices.size(); ++i )
118  fibers->InsertElement(i, GetFiber(indices[i]));
119 
120  return result;
121 }
122 
123 template< class TValue >
126 ::SelectByBallROI(double x, double y, double z, double radius)
127 {
128  Pointer result = Self::New();
129  itk::CopyTrackvisHeader(*m_Header, *result->m_Header);
130 
131  int num=0;
132  auto fibers = result->GetFibers();
133  for ( int i = 0; i < GetNumberOfFibers(); ++i )
134  {
135  auto fiber_i = GetFiber(i);
136  double dist = fiber_i->DistanceToPoint(x,y,z);
137  if (dist < radius)
138  {
139  fibers->InsertElement(fibers->Size(), fiber_i);
140  num++;
141  }
142  }
143  result->m_Header->n_count=num;
144 
145  return result;
146 }
147 
148 template< class TValue >
149 void
152 {
153  m_Fibers->InsertElement(m_Fibers->Size(), fiber);
154  m_Header->n_count++;
155 }
156 
157 template< class TValue >
158 void
160 ::AppendFibers(const Pointer fibers)
161 {
162  utlGlobalException(!itk::IsSameStructure(*m_Header, *fibers->m_Header), "two headers do not have the same structure. Cannot merge.");
163  for ( int i = 0; i < fibers->GetNumberOfFibers(); ++i )
164  AppendFiber(fibers->GetFiber(i));
165 }
166 
167 template< class TValue >
168 void
170 ::PrintSelf(std::ostream & os, Indent indent) const
171 {
172  Superclass::PrintSelf(os, indent);
173  PrintFibersHeader(os, indent);
174 
175  std::vector<double> numPointsStats, pointDistStats;
176  GetPointStats(numPointsStats, pointDistStats);
177 
178  utlSAGlobalException(m_Header->n_count!=m_Fibers->Size())
179  (m_Header->n_count)(m_Fibers->Size()).msg("the number of tracts are not consistent in m_Header and m_Fibers");
180 
181  os << indent << std::endl;
182  os << indent << "The number of fibers: " << GetNumberOfFibers() << std::endl;
183  os << indent << "Total number of points: " << GetNumberOfPoints() << std::endl << std::flush;
184  utl::PrintVector(numPointsStats, "stats of the number of points (min,max,mean,std): ", " ", os << indent, false);
185  utl::PrintVector(pointDistStats, "stats of distances between points (min,max,mean,std): ", " ", os << indent, false);
186  for ( int i = 0; i < m_Fibers->Size(); ++i )
187  {
188  FiberPointer fiber = (*m_Fibers)[i];
189  os << indent << std::endl;
190  os << indent << "Fiber #" << i << " : number of points = " << fiber->GetNumberOfPoints() << std::endl;
191  fiber->Print(os, indent);
192  }
193  os << indent<< std::endl;
194 }
195 
196 }
197 
198 
199 #endif
bool IsSameStructure(const TrackVisHeaderType &h1, const TrackVisHeaderType &h2)
void RemoveScalarName(TrackVisHeaderType &header, const std::string &name)
FiberTracts< TValue >::Pointer DeepClone() const
FiberType::Pointer FiberPointer
void RemovePropertyName(TrackVisHeaderType &header, const std::string &name)
std::vector< double > GetContainerStats(IteratorType v1, IteratorType v2)
Definition: utlCore.h:921
#define utlSAGlobalException(expr)
Definition: utlCoreMacro.h:362
void AppendFiber(const FiberPointer fiber)
void CopyTrackvisHeader(const TrackVisHeaderType &hFrom, TrackVisHeaderType &hTo)
virtual void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
void GetPointStats(std::vector< double > &numPointsStats, std::vector< double > &pointDistStats) const
std::vector< std::string > CovertChar2DArrayToStringArray(const char arr[][M], int N)
Definition: utlCore.h:605
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
void AppendFibers(const Pointer fibers)
Pointer SelectByBallROI(double x, double y, double z, double radius)
void PrintVector(const std::vector< T > &vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
Definition: utlCore.h:1002
void RemovePropertiesByName(const std::string &name)
Pointer SelectByIndicesOfTracts(const std::vector< int > &indices)
Created "07-22-2017.
SmartPointer< Self > Pointer
LightObject::Pointer InternalClone() const ITK_OVERRIDE
int GetNumberOfPoints() const
void RemoveScalarsByName(const std::string &name)