101 size_t found = name.find_last_of(
"_");
102 if (found==std::string::npos)
105 std::string dim = name.substr(found+1);
107 return utl::ConvertStringToNumber<int>(dim);
114 inline std::vector<T>
115 GetScalarsByName(
const std::vector<T>& vec,
const std::vector<std::string>& nameVec,
const std::string& name)
118 std::vector<T> result;
119 for (
int i = 0; i < nameVec.size(); ++i )
122 if (nameVec[i]==name)
124 for (
int j = 0; j < len; ++j )
125 result.push_back(vec[start+j]);
135 SetScalarsByName(
const std::vector<T>& vec,
const std::vector<std::string>& nameVec,
const std::vector<T>& scalars,
const std::string& name)
138 for (
int i = 0; i < nameVec.size(); ++i )
141 utlSAException(len!=scalars.size())(nameVec[i])(len)(scalars.size()).msg(
"the size of scalars is not consistent");
142 if (nameVec[i]==name)
144 for (
int j = 0; j < len; ++j )
145 vec[start+j] = scalars[j];
153 RemoveScalarsByName(std::vector<T>& vec,
const std::vector<std::string>& nameVec,
const std::string& name)
155 int start=0, len=0, i;
156 for ( i = 0; i < nameVec.size(); ++i )
159 if (nameVec[i]==name)
165 if (i==nameVec.size())
173 for ( i = start; i+len < vec.size(); ++i )
176 vec.resize(vec.size()-len);
184 double result = -1.5 + std::sqrt(0.25 + 2.0*dimm);
186 utlGlobalAssert(
IsInt(result) &&
IsEven(result),
"Logical ERROR! Rank of SH here must be an even integer. Wrong dimension! rank=" << result <<
", dimm = " << dimm);
199 return (shRank + 1)*(shRank + 2)/2;
206 return (l*l + l)/2 + m;
210 inline std::vector<int>
213 std::vector<int> lm(2,0);
218 int l = std::ceil(0.5*(std::sqrt(9.+8.*j)-3.0));
221 int residual = j+1 - (l-1)*l/2;
223 lm[1] = residual-1 - l;
233 template <
typename T >
237 std::vector<T> eigenValues(2);
238 double tmp = fa/std::sqrt(3.0-2.0*fa*fa);
241 eigenValues[0] = meanEigenValue*(1.0 + 2.0*tmp);
242 eigenValues[1] = meanEigenValue*(1.0 - tmp);
247 eigenValues[0] = meanEigenValue*(1.0 + tmp);
248 eigenValues[1] = meanEigenValue*(1.0 - 2.0*tmp);
255 template<
typename V1Type,
typename V2Type>
313 template<
typename V1Type,
typename V2Type>
357 template<
typename V1Type,
typename V2Type>
363 for (
int i = 0; i < 6; ++i )
368 std::vector<double> v9d(9);
401 for (
int i = 0; i < grad.
Rows(); ++i )
414 inline utl_shared_ptr<NDArray<T,2> >
418 std::vector < std::vector<std::string> > strVec;
423 for (
int i = 0; i < strVec.size(); i += 1 )
424 utlGlobalException(strVec[i].size()!=3,
"wrong dimension in gradient file! strVec[i].size()="<< strVec[i].size() <<
", grad_str="<<grad_str);
426 utl_shared_ptr<NDArray<T,2> > grad (
new NDArray<T,2>() );
428 grad->ReSize(2*strVec.size(),3);
430 grad->ReSize(strVec.size(),3);
432 std::vector<double> xyz(3);
434 for (
int i=0; i<strVec.size(); i++)
437 std::istringstream ( strVec[i][0] ) >> xyz[0];
438 std::istringstream ( strVec[i][1] ) >> xyz[1];
439 std::istringstream ( strVec[i][2] ) >> xyz[2];
461 (*grad)(jj,0)=xyz[0], (*grad)(jj,1)=xyz[1], (*grad)(jj,2)=xyz[2];
467 { xyz[0] = -xyz[0]; xyz[1] = -xyz[1]; xyz[2] = -xyz[2]; }
469 { xyz[1]=
M_PI-xyz[1]; xyz[2]=
M_PI+xyz[2]; xyz[2]=(xyz[2]>=2*
M_PI)?(xyz[2]-2*
M_PI):xyz[2]; }
470 (*grad)(jj,0)=xyz[0], (*grad)(jj,1)=xyz[1], (*grad)(jj,2)=xyz[2];
483 vec = std::vector<T>(grad.
Rows(), br);
490 utlException(vec.size()==0 || grad.
Rows()==0,
"wrong size! vec.size()="<<vec.size()<<
", grad.Rows()="<<grad.
Rows());
492 if (grad.
Rows()==vec.size())
495 std::vector<T> vec_result;
503 vec_result.resize(vec.size()* grad.
Rows());
505 for (
int i = 0; i<vec.size(); i++)
507 for (
int j = 0; j < grad.
Rows(); j += 1 )
509 vec_result[i*grad.
Rows()+j] = vec[i];
510 for (
int kk = 0; kk < 3; kk += 1 )
511 grad_result(i*grad.
Rows()+j,kk) = grad(j,kk);
522 std::string ext, fileNoExt;
539 std::string ext, fileNoExt;
543 file = fopen(filename.c_str(),
"rb");
552 fseek (file, 0, SEEK_SET);
553 size_t rsize = fread(code, 1, 6, file);
554 utlGlobalException(strcmp(code,
"TRACK")!=0,
"Wrong data format TRACTS_TRK. Magic code is wrong. It is " + std::string(code) +
". Should be 'TRACK'");
585 utlSAException(gradMat.
Rows()!=bVec.size())(gradMat.
Rows())(bVec.size()).msg(
"wrong size of gradMat and bVec");
594 for (
int i = 0; i < gradMat.
Rows(); ++i )
596 mat(i,0) = -1.0 * bVec[i] * gradMat(i,0) * gradMat(i,0);
597 mat(i,1) = -2.0 * bVec[i] * gradMat(i,0) * gradMat(i,1);
598 mat(i,2) = -2.0 * bVec[i] * gradMat(i,0) * gradMat(i,2);
599 mat(i,3) = -1.0 * bVec[i] * gradMat(i,1) * gradMat(i,1);
600 mat(i,4) = -2.0 * bVec[i] * gradMat(i,1) * gradMat(i,2);
601 mat(i,5) = -1.0 * bVec[i] * gradMat(i,2) * gradMat(i,2);
671 if (maxIdx == 0 && minIdx == 2) sextant = 0;
672 if (maxIdx == 1 && minIdx == 2) sextant = 1;
673 if (maxIdx == 1 && minIdx == 0) sextant = 2;
674 if (maxIdx == 2 && minIdx == 0) sextant = 3;
675 if (maxIdx == 2 && minIdx == 1) sextant = 4;
676 if (maxIdx == 0 && minIdx == 1) sextant = 5;
683 case 0: { index = G*offset;
break; }
684 case 1: { index = offset + (1-R)*offset;
break; }
685 case 2: { index = offset*2 + B*offset;
break; }
686 case 3: { index = offset*3 + (1-G)*offset;
break; }
687 case 4: { index = offset*4 + R*offset;
break; }
688 case 5: { index = offset*5 + (1-B)*offset;
break; }
int GetFiberTractsFormatFromFileExtension(const std::string &filename)
void NormalizeGrad(const utl::NDArray< T, 2 > &grad)
NDArray<T,1> is a vector class which uses blas mkl.
std::vector< T > GetE1E2FromFAMD(const T fa, const T meanEigenValue, const bool isE2E3Equal=true)
bool IsOdd(const int value)
std::shared_ptr< NDArray< T, 2 > > ReadGrad(const std::string &grad_str, const int NoSymmetricDuple=DIRECTION_NODUPLICATE, const int mode=CARTESIAN_TO_SPHERICAL, const int flipx=DIRECTION_NOFLIP, const int flipy=DIRECTION_NOFLIP, const int flipz=DIRECTION_NOFLIP, const bool need_normalize=true)
void GetRow(const int index, T *vec) const
int GetIndexSHj(const int l, const int m)
bool IsEven(const int value)
void MatchBVectorAndGradientMatrix(const T &br, std::vector< T > &vec, const NDArray< T, 2 > &grad)
static constexpr double SQRT1_2
void SetScalarsByName(const std::vector< T > &vec, const std::vector< std::string > &nameVec, const std::vector< T > &scalars, const std::string &name)
#define utlException(cond, expout)
void spherical2Cartesian(const T r, const T theta, const T phi, T &x, T &y, T &z)
std::vector< int > GetIndexSHlm(const int j)
#define utlSAGlobalException(expr)
NDArray<T,2> is a row-major matrix.
void ReadLinesFirstlineCheck(const std::string &filename, std::vector< std::vector< std::string > > &strVec, const char *cc=" ")
void RemoveScalarsByName(std::vector< T > &vec, const std::vector< std::string > &nameVec, const std::string &name)
utl::Matrix< T > GetDTIDesignMatrix(const utl::Matrix< T > &gradMat, const std::vector< T > &bVec, int dwi_normalize)
int DimToRankSH(const int dimm)
const T & max(const T &a, const T &b)
Return the maximum between a and b.
double GetTwoNorm() const
NDArray< T, 2 > & SetRow(const int index, T const *vec)
int GetFiberTractsFormat(const std::string &filename)
const T & min(const T &a, const T &b)
Return the minimum between a and b.
void Convert2To1Tensor(const utl::NDArray< T, 2 > &mat, utl::NDArray< T, 1 > &vec)
#define utlGlobalException(cond, expout)
#define utlGlobalAssert(cond, expout)
bool IsInt(const std::string &input, const double epss=1e-10)
#define utlSAException(expr)
int RankToDimSH(const int shRank)
void ConvertTensor6DTo9D(const V1Type &v6d, V2Type &v9d, int v6dStoreWay)
std::vector< T > GetScalarsByName(const std::vector< T > &vec, const std::vector< std::string > &nameVec, const std::string &name)
void RGBToIndex(double R, double G, double B, double &index)
int GetScalarsDimentionByName(const std::string &name)
void ConvertTensor6DTo6D(const V1Type &v6d1, V2Type &v6d2, int s1, int s2)
void cartesian2Spherical(const T x, const T y, const T z, T &r, T &theta, T &phi)
VectorType NormalizeUnitNorm(const VectorType &v, const int nSize)
void Convert1To2Tensor(const utl::NDArray< T, 1 > &vec, utl::NDArray< T, 2 > &mat)
bool ReSize(const SizeType rows, const SizeType cols)
bool ReSize(const SizeType size)
void GetFileExtension(const std::string &fileNameAbsolute, std::string &ext, std::string &fileNoExt)
void ConvertTensor9DTo6D(const V1Type &v9d, V2Type &v6d, int v6dStoreWay)
static constexpr double SQRT2