DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkSamplingSchemeQSpace.hxx
Go to the documentation of this file.
1 
18 #ifndef __itkSamplingSchemeQSpace_hxx
19 #define __itkSamplingSchemeQSpace_hxx
20 
22 
23 namespace itk
24 {
25 
26 template <class TPixelType>
29  m_BVector(new STDVectorType())
30 {
31  m_BThresholdSingleShell = 1e-4;
32 }
33 
34 template <class TPixelType>
35 typename LightObject::Pointer
38 {
39  typename LightObject::Pointer loPtr = Superclass::InternalClone();
40 
41  typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
42  if(rval.IsNull())
43  {
44  itkExceptionMacro(<< "downcast to type " << this->GetNameOfClass()<< " failed.");
45  }
46 
47  rval->m_BThresholdSingleShell = m_BThresholdSingleShell;
48  rval->m_BVector = m_BVector;
49 
50  return loPtr;
51 }
52 
53 template <class TPixelType>
54 void
56 ::PrintSelf(std::ostream& os, Indent indent) const
57 {
58  Superclass::PrintSelf(os, indent);
59  utl::PrintVector(*m_BVector, "m_BVector", " ", os<<indent);
60 }
61 
62 template <class TPixelType>
63 void
66 {
67  if (m_BVector != bVec)
68  {
69  m_BVector = bVec;
70  ConvertBVectorToQVector();
71  if (m_BThresholdSingleShell>0)
72  GroupBValues();
73  this->Modified();
74  }
75 }
76 
77 template <class TPixelType>
80 ::GetBVectorInShell(unsigned int shellIndex)
81 {
82  unsigned int num = this->GetNumberOfShells();
83  STDVectorPointer bVec(new STDVectorType());
84  if (shellIndex<num && m_BVector->size()>num)
85  {
86  IndexVectorType vecTmp = (*this->m_IndicesInShells)[shellIndex];
87  for ( unsigned int i = 0; i < vecTmp.size(); i += 1 )
88  {
89  bVec->push_back( (*m_BVector)[vecTmp[i] ] );
90  }
91  }
92  return bVec;
93 }
94 
95 template <class TPixelType>
96 void
98 ::SetSamplingScheme3D(typename Superclass::Pointer scheme)
99 {
100  this->clear();
101  for ( int i = 0; i < scheme->size(); i += 1 )
102  this->push_back((*scheme)[i]);
103 
104  this->m_DeltaSmall = scheme->GetDeltaSmall();
105  this->m_DeltaBig = scheme->GetDeltaBig();
106  this->m_Tau = scheme->GetTau();
107  this->m_RadiusThresholdSingleShell = scheme->GetRadiusThresholdSingleShell();
108 
109  *this->m_RadiusVector = *scheme->GetRadiusVector();
110  *this->m_IndicesInShells = *scheme->GetIndicesInShells();
111 }
112 
113 
114 template <class TPixelType>
115 void
118 {
119  Superclass::Clear();
120  m_BVector=STDVectorPointer(new STDVectorType());
121  this->Modified();
122 }
123 
124 template <class TPixelType>
125 void
128 {
129  // utlShowPosition(true);
130  // std::cout << "this->m_Tau = " << this->m_Tau << std::endl << std::flush;
131  this->m_RadiusVector=STDVectorPointer(new STDVectorType(m_BVector->size()));
132  double firstTerm = 4.0*M_PI*M_PI*this->m_Tau;
133  for ( int i = 0; i < m_BVector->size(); i += 1 )
134  (*this->m_RadiusVector)[i] = std::sqrt((*m_BVector)[i]/(firstTerm));
135  this->Modified();
136 }
137 
138 template <class TPixelType>
139 void
142 {
143  this->m_BVector=STDVectorPointer(new STDVectorType(this->m_RadiusVector->size()));
144  double firstTerm = 4.0*M_PI*M_PI*this->m_Tau;
145  for ( int i = 0; i < m_BVector->size(); i += 1 )
146  (*m_BVector)[i] = firstTerm * (*this->m_RadiusVector)[i] * (*this->m_RadiusVector)[i];
147  this->Modified();
148 }
149 
150 template <class TPixelType>
151 std::vector<typename SamplingSchemeQSpace<TPixelType>::STDVectorType>
154 {
155  // utlGlobalException(this->m_RadiusVector->size()==0 && m_BVector->size()==0, "need to set bVector or qVector");
156  // if (this->m_RadiusVector->size()==0 && m_BVector->size()>0)
157  // ConvertBVectorToQVector();
158 
159  std::vector<STDVectorType> bVectors;
160  if (this->m_IndicesInShells->size()==0)
161  {
162  this->m_IndicesInShells = Index2DVectorPointer(new Index2DVectorType());
163  STDVectorType bMax, bMin;
164  for ( int i = 0; i < m_BVector->size(); i += 1 )
165  {
166  double b = (*m_BVector)[i];
167  int j=0;
168  for ( j = 0; j < bVectors.size(); j += 1 )
169  {
170  if (b>=bMin[j]-m_BThresholdSingleShell && b<=bMax[j]+m_BThresholdSingleShell)
171  {
172  bVectors[j].push_back(b);
173  (*this->m_IndicesInShells)[j].push_back(i);
174  if (b<bMin[j])
175  bMin[j] = b;
176  else if (b>bMax[j])
177  bMax[j] = b;
178  break;
179  }
180  }
181  if (j==bVectors.size())
182  {
183  STDVectorType bVecTemp;
184  bVecTemp.push_back(b);
185  bVectors.push_back(bVecTemp);
186  IndexVectorType bIndexTemp;
187  bIndexTemp.push_back(i);
188  this->m_IndicesInShells->push_back(bIndexTemp);
189  bMin.push_back(b);
190  bMax.push_back(b);
191  }
192  }
193 
194  this->Modified();
195  for ( int j = 0; j < bVectors.size(); j += 1 )
196  {
197  utlSAGlobalException(bMax[j]-bMin[j]>2*m_BThresholdSingleShell)
198  (j)(bMax[j])(bMin[j])(m_BThresholdSingleShell).msg("the range of b values is larger than 100, which can not be in the same shell");
199  }
200  }
201  else
202  {
203  for ( int i = 0; i < this->m_IndicesInShells->size(); i += 1 )
204  {
205  STDVectorType bVecTemp;
206  IndexVectorType indexTemp = (*this->m_IndicesInShells)[i];
207  for ( int j = 0; j < indexTemp.size(); j += 1 )
208  {
209  bVecTemp.push_back((*m_BVector)[ indexTemp[j] ]);
210  }
211  bVectors.push_back(bVecTemp);
212  }
213  }
214  return bVectors;
215 }
216 
217 template <class TPixelType>
218 void
221 {
222  utlGlobalException(m_BVector->size()==0, "no b values");
223  std::vector<STDVectorType> bVectors = GroupBValues();
224 
225  STDVectorPointer bVec (new STDVectorType(m_BVector->size()));
226  for ( int j = 0; j < bVectors.size(); j += 1 )
227  {
228  double bMean=0;
229  for ( int k = 0; k < bVectors[j].size(); k += 1 )
230  bMean += bVectors[j][k];
231  bMean /= bVectors[j].size();
232  for ( int k = 0; k < bVectors[j].size(); k += 1 )
233  {
234  (*bVec)[(*this->m_IndicesInShells)[j][k] ] = bMean;
235  }
236  }
237  m_BVector = bVec;
238  ConvertBVectorToQVector();
239  this->Modified();
240 }
241 
242 template <class TPixelType>
243 void
246 {
247  utlGlobalException(this->m_RadiusVector->size()==0 && m_BVector->size()==0, "need to set bVector or qVector");
248  if (this->m_RadiusVector->size()==0 && m_BVector->size()>0)
249  ConvertBVectorToQVector();
250  Superclass::CorrectRadiusValues();
251  ConvertQVectorToBVector();
252 }
253 
254 template <class TPixelType>
255 void
258 {
259  if (this->size()==0)
260  return;
261  utlGlobalException(this->m_IndicesInShells->size()==0, "need to set m_IndicesInShells first");
262 
263  if (m_BVector->size()>0)
264  {
265  utlGlobalException(m_BVector->size()!=this->size(), "different size of bVec and gradients");
266  STDVectorType bVec = *m_BVector;
267  m_BVector->clear();
268  for ( int i = 0; i < this->GetNumberOfShells(); ++i )
269  {
270  IndexVectorType indices = (*this->m_IndicesInShells)[i];
271  for ( int j = 0; j < indices.size(); ++j )
272  {
273  m_BVector->push_back(bVec[indices[j]]);
274  }
275  }
276  }
277 
278  Superclass::RemoveSamplesNotIndexed();
279 }
280 
281 }
282 
283 
284 #endif
285 
286 
287 
288 
std::vector< STDVectorType > GroupBValues()
#define utlSAGlobalException(expr)
Definition: utlCoreMacro.h:362
Superclass::STDVectorPointer STDVectorPointer
std::vector< IndexVectorType > Index2DVectorType
void SetSamplingScheme3D(typename Superclass::Pointer scheme3D)
STDVectorPointer GetBVectorInShell(unsigned int shellIndex)
#define M_PI
Definition: utlCoreMacro.h:57
#define utlGlobalException(cond, expout)
Definition: utlCoreMacro.h:372
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
std::vector< double > STDVectorType
std::vector< int > IndexVectorType
this class describes sampling in a 3D space (Q space or R space).
SmartPointer< Self > Pointer
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
VectorContainer< IdentifierType, Point< TPixelType, 3 > > Superclass
utl_shared_ptr< STDVectorType > STDVectorPointer
utl_shared_ptr< Index2DVectorType > Index2DVectorPointer
LightObject::Pointer InternalClone() const ITK_OVERRIDE
void SetBVector(const STDVectorPointer bVec)