/* POSSUM Ivana Drobnjak & Mark Jenkinson Copyright (C) 2005-2007 University of Oxford */ /* Part of FSL - FMRIB's Software Library http://www.fmrib.ox.ac.uk/fsl fsl@fmrib.ox.ac.uk Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance Imaging of the Brain), Department of Clinical Neurology, Oxford University, Oxford, UK LICENCE FMRIB Software Library, Release 5.0 (c) 2012, The University of Oxford (the "Software") The Software remains the property of the University of Oxford ("the University"). The Software is distributed "AS IS" under this Licence solely for non-commercial use in the hope that it will be useful, but in order that the University as a charitable foundation protects its assets for the benefit of its educational and research purposes, the University makes clear that no condition is made or to be implied, nor is any warranty given or to be implied, as to the accuracy of the Software, or that it will be suitable for any particular purpose or for use under any specific conditions. Furthermore, the University disclaims all responsibility for the use which is made of the Software. It further disclaims any liability for the outcomes arising from using the Software. The Licensee agrees to indemnify the University and hold the University harmless from and against any and all claims, damages and liabilities asserted by third parties (including claims for negligence) which arise directly or indirectly from the use of the Software or the sale of any products based on the Software. No part of the Software may be reproduced, modified, transmitted or transferred in any form or by any means, electronic or mechanical, without the express permission of the University. The permission of the University is not required if the said reproduction, modification, transmission or transference is done without financial return, the conditions of this Licence are imposed upon the receiver of the product, and all original and amended source code is included in any transmitted product. You may be held legally responsible for any copyright infringement that is caused or encouraged by your failure to abide by these terms and conditions. You are not permitted under this Licence to use this Software commercially. Use for which any financial return is received shall be defined as commercial use, and includes (1) integration of all or part of the source code or the Software into a product for sale or license by or on behalf of Licensee to third parties or (2) use of the Software or any derivative of it for research with the final aim of developing software products for sale or license to a third party or (3) use of the Software or any derivative of it for research with the final aim of developing non-software products for sale or license to a third party, or (4) use of the Software to provide any service to an external organisation for which payment is received. If you are interested in using the Software commercially, please contact Isis Innovation Limited ("Isis"), the technology transfer company of the University, to negotiate a licence. Contact details are: innovation@isis.ox.ac.uk quoting reference DE/9564. */ //POSSUM-FUNCTIONS #include //standard c++ library #include //include string class #include //to read and write to the file #include //what is this? #include //for precalcuation of some variables #include "newmatap.h"//including NEWMAT library #include "newmatio.h"// #include "newimage/newimageall.h"//including NEW IMAGE library #include "possumfns.h"//including possum functions I made #include "libprob.h" #include "miscmaths/miscprob.h" #include "newimage/costfns.h" #define _GNU_SOURCE 1 #define POSIX_SOURCE 1 using namespace std; using namespace MISCMATHS; using namespace NEWIMAGE; using namespace NEWMAT; //string title="possumfns (Version 2.0)\nCopyright(c) 2003, University of Oxford (Ivana Drobnjak)"; vector gstatic; double* g1static; double* g2static; double* g3static; double* g1motion; double* g2motion; double* g3motion; double* g4motion; double* rotmotion1; double* rotmotion2; double* rotmotion3; double* transmotion; double* b0tmp111; double* b0tmp112; double* b0tmp113; double* b0tmp221; double* b0tmp222; double* b0tmp223; double* b0tmp331; double* b0tmp332; double* b0tmp333; double* b0tmp111freq; double* b0tmp112freq; double* b0tmp113freq; double* b0tmp221freq; double* b0tmp222freq; double* b0tmp223freq; double* b0tmp331freq; double* b0tmp332freq; double* b0tmp333freq; //TABLE SINC double* table_sinc;//table for SINC in range [0,Dsinc] int glo_Nsinc=5000000;//no of elements double glo_Dsinc=500.00;//domain double glo_dsinc=glo_Dsinc/glo_Nsinc;//step size for SINC double glo_idsinc=1/glo_dsinc;//inverse of step size for fster calc //TABLE SIN AND COS double* table_sin;//table for SIN in range [0,2pi] double* table_cos;//table for COS in range [0,2pi] int glo_Nsin=6000000;//no of elements double glo_Dsin=2.0*M_PI;//domain double glo_dsin=glo_Dsin/glo_Nsin;//step size for SIN and COS double glo_idsin=1/glo_dsin;//inverse of the step size double glo_twopi=2*M_PI; double glo_itwopi=1/glo_twopi; double glo_cx; double glo_cy; double glo_cz; //SOME CONSTANTS const double gammabar=42.58*1e06;//(in Hz/T) const double gama=2*M_PI*gammabar; ///////// //GENERAL /////////////////////////////// double round_ivana(const double x, const int n){ //rounds a number up to n digits of precision double xx,xxx,xxxx,nn; nn=MISCMATHS::pow(10.0f,(double) n); xx=nn*x; if (x>0) xxx=floor((float)xx+0.5); else xxx=ceil(xx-0.5); xxxx=xxx/nn; return xxxx; } ///////////////////////////// double norm(const RowVector q){ int x=q.Ncols(); double sqsum=0.0; for (int i=1;i<=x;i++){ sqsum+=q(i)*q(i); } double norm_q=sqrt(sqsum); return norm_q; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void coeff(const double xold, const double xnew, const double told, const double tnew, double& a, double& b){//needs improvement // return slope and intercept of a line if ((tnew-told)>1e-12){ a=xold-told*(xnew-xold)/(tnew-told); b=(xnew-xold)/(tnew-told); } } //////////////////////////////////////////from mj code costfns.cc in NEWIMAGE double mj_sinc(const double x){ if (fabs(x)<1e-7) { return 1.0-fabs(x); } double y=M_PI*x; return sin(y)/y; } ///////////////////////////////////////////////// Matrix rot(const double alpha,const string axes){//WE USE RIGHT HAND RULE FOR THE ROTATIONS AND ORDER IN ROTATION SEQUENCE Rz*Ry*Rx Matrix R(3,3); if (axes=="z"){ //rotation right hand rule R <1e-12) { cout<<"Warning, euler_to_quat is producing quaternions with the norm different from 1!"<<"Norm difference from one is "<1e-12) { cout<<"Warning, quaternion is not normalised !"<<"Norm_q-1 is "<0) q_n(1)=1; //the difference can be really small i.e. 1e-16, but acos gives nan for it (for 1+1e-16) if ((q_n(1)+1)<0) q_n(1)=-1; angle=acos(q_n(1))*2; //cout<1e-12) { axis(1)=axis(1)/norm_axis; axis(2)=axis(2)/norm_axis; axis(3)=axis(3)/norm_axis; } double x=axis(1)*sin(angle/2); double y=axis(2)*sin(angle/2); double z=axis(3)*sin(angle/2); double w=cos(angle/2); RowVector q(4); q<1e-12) { q_n(1)=q(1)/norm_q; q_n(2)=q(2)/norm_q; q_n(3)=q(3)/norm_q; q_n(4)=q(4)/norm_q; } double x=q_n(2); double y=q_n(3); double z=q_n(4); double w=q_n(1); Matrix R(3,3); R<<1-2*y*y-2*z*z<<2*x*y-2*z*w<<2*x*z+2*y*w <<2*x*y+2*z*w<<1-2*x*x-2*z*z<<2*y*z-2*x*w <<2*x*z-2*y*w<<2*y*z+2*x*w<<1-2*x*x-2*y*y; return R; } //////////////////////////////////////////////////////////////////////////////// RowVector matrix_to_quaternion(const Matrix M){//not using double trace = M(1,1) + M(2,2) + M(3,3) + 1; double s,x,y,z,w; if( trace > 0 ) { s = 0.5 / sqrt(trace); w = 0.25f/s; x = (M(3,2) - M(2,3))*s; y = (M(1,3) - M(3,1))*s; z = (M(2,1) - M(1,2))*s; } else { if ( M(1,1) > M(2,2) && M(1,1) > M(3,3) ) { double s = 2 * sqrt( 1 + M(1,1) - M(2,2) - M(3,3)); x = 0.25 * s; y = (M(1,2)+M(2,1))/s; z = (M(1,3)+M(3,1))/s; w = (M(2,3)-M(3,2))/s; } else if (M(2,2) > M(3,3)) { double s = 2 * sqrt( 1 + M(2,2) - M(1,1) - M(3,3)); x = (M(1,2) + M(2,1) ) / s; y = 0.25f * s; z = (M(2,3) + M(3,2) ) / s; w = (M(1,3) - M(3,1) ) / s; } else { double s = 2 * sqrt( 1 + M(3,3) - M(1,1) - M(2,2) ); x = (M(1,3) + M(3,1) ) / s; y = (M(2,3) + M(3,2) ) / s; z = 0.25 * s; w = (M(1,2) - M(2,1) ) / s; } } RowVector q(4); q<1e-12) { if (fabs(norm_axis)>1e-12){ axis(1)=axis(1)/norm_axis; axis(2)=axis(2)/norm_axis; axis(3)=axis(3)/norm_axis; } else { cout<<"Warning in axismatm: Norm of the axis is ZERO!"< 0.01"< (tx2,ty2,tz2) int d=vectortime.Nrows(); double dtt=0; Matrix inttra(d,3); double dt=vectortime(d)-vectortime(1); for (int k=1;k<=d-1;k++){ dtt=dtt+vectortime(k+1)-vectortime(k); inttra(k,1)=dtt*(tx2-tx1)/dt+tx1; inttra(k,2)=dtt*(ty2-ty1)/dt+ty1; inttra(k,3)=dtt*(tz2-tz1)/dt+tz1; } inttra(d,1)=inttra(d-1,1); inttra(d,2)=inttra(d-1,2); inttra(d,3)=inttra(d-1,3); return inttra; } ///////////////////////////////////////////////////////////////////////////////////////////// Matrix interrotation(const double a1,const double b1,const double c1, const double a2,const double b2,const double c2, const ColumnVector vectortime){ // as above but for rotations (input in euler angles, output in angle/axis) int d=vectortime.Nrows(); Matrix introt(d,8); RowVector aa(8); RowVector q1=euler_to_quaternion(a1,b1,c1); RowVector q2=euler_to_quaternion(a2,b2,c2); RowVector q1c(4); //conjugate of q1 q1c <2){ double dtt=0; double dt=vectortime(d)-vectortime(1); RowVector angle(d-2); for (int k=1;k<=d-2;k++){ dtt=dtt+vectortime(k+1)-vectortime(k); angle(k)=dtt*qa(1)/dt; aa<1e-10){ l=l+1; timevector(l)=epi(t1,1); t1=t1+1; } l=l+1; timevector(l)=motion(t2+1,1); int tmp6=0; RowVector G(8); G=0; if (fabs(motion(t2+1,1)-epi(t1,1))<=1e-10){ t1=t1+1; tmp6=0; } else{ G=interpolation_gradients(epi.Row(t1-1),epi.Row(t1),motion(t2+1,1)); tmp6=1; } Matrix R=interrotation(motion(t2,5),motion(t2,6),motion(t2,7),motion(t2+1,5),motion(t2+1,6),motion(t2+1,7),timevector.Rows(1,l)); Matrix T=intertranslation(motion(t2,2),motion(t2,3),motion(t2,4),motion(t2+1,2),motion(t2+1,3),motion(t2+1,4),timevector.Rows(1,l)); for (int tt=1;tt<=l-2;tt++){ t=t+1; RowVector tmp1(19); tmp1.Columns(1,8)=epi.Row(t1-l+tt+tmp6); tmp1.Columns(9,11)=T.Row(tt); tmp1.Columns(12,19)=R.Row(tt); mainmatrix.Row(t)=tmp1; } t=t+1; if (tmp6==1){ RowVector Gpre(8); Gpre<& b, volume& b0gx, volume& b0gy, volume& b0gz){ // b is in mT, and dimensions of voxels are in mm b0gx = b*0; b0gy = b0gx; b0gz = b0gx; for (int z=1;z& b0, volume& b0x, volume& b0y, volume& b0z, const int myid, const int Nxx,const int numprocs){ calc_gradients(b0,b0x,b0y,b0z); int Nx=b0.xsize(); int Ny=b0.ysize(); int Nz=b0.zsize(); int Ntmp=(int)((Nxx-myid-1)/numprocs)+1; if (Ntmp<1){ cout<<"WARNING:Number of processors bigger than the number of voxels in x-direction."< tmpvol1(Ntmp,Ny,Nz); volume tmpvol2(Ntmp,Ny,Nz); volume tmpvol3(Ntmp,Ny,Nz); volume tmpvol4(Ntmp,Ny,Nz); int x0=0, x1=0, y0=0, y1=0, z0=0, z1=0; for (z0=0, z1=0; z0& b, volume4D& b0gx, volume4D& b0gy, volume4D& b0gz){ // b is in mT, and dimensions of voxels are in mm b0gx = b*0; b0gy = b0gx; b0gz = b0gx; for (int t=0;t& b0, volume4D& b0x, volume4D& b0y, volume4D& b0z, const int myid, const int Nxx, const int numprocs){ calc_gradients4D(b0,b0x,b0y,b0z); int Nx=b0.xsize(); int Ny=b0.ysize(); int Nz=b0.zsize(); int Nt=b0.tsize(); volume4D tmpvol1(Nx,Ny,Nz,Nt); volume4D tmpvol2(Nx,Ny,Nz,Nt); volume4D tmpvol3(Nx,Ny,Nz,Nt); volume4D tmpvol4(Nx,Ny,Nz,Nt); int x0=0, x1=0, y0=0, y1=0, z0=0, z1=0, t0=0, t1=0; for (t0=0, t1=0; t0=timecourse[actstep] && actstep<=(Nact-2)){ coeff(activation[actstep],activation[actstep+1],timecourse[actstep],timecourse[actstep+1],dT2_1,dT2_2); dT2_1=dT2_1*iT2*iT2; dT2_2=dT2_2*iT2*iT2; actstep=actstep+1; } actint+=(dT2_1+dT2_2*(tnew+told)/2)*(tnew-told); //if (dT2_2<1e-12) actint+=(tnew-told)/(dT2_1+T2); // else actint+=log(1+(tnew-told)/(told+dT2_1/dT2_2))/dT2_2 double phase=gama*gg1*x+gama*gg2*y+gama*gg3*z+gama*b0*tt+gama*chshift*tt; if (rfangle!=0){ excitation=0; double df=H(step,3); double fc=H(step,4); double f=gammabar*(gxnew*x+gynew*y+gznew*z+b0+chshift); if (opt_test==1)cout<<"gxnew="<=0 && nf<=(Nslc-2)) { off-=nf; double ts=table_slcprof[nf]; double sx=(table_slcprof[nf+1]-ts)*off + ts; //cout<<"ts="<0){//new stuff mon dec 19 excitation=1; m=free(m,tt,tissue,phase,actint); //new stuff mon dec 19 //due to crushers or any gradient induced dephasing over the voxel a new initial magnetisation is introduced which is the average of the magnetisations over the voxel double xvalrf=fabs(glo_cx*(gg1+b0x*tt)); double yvalrf=fabs(glo_cy*(gg2+b0y*tt)); double zvalrf=fabs(glo_cz*(gg3+b0z*tt)); double xyzrf=Sinc(xvalrf)*Sinc(yvalrf)*Sinc(zvalrf); m(1)=m(1)*xyzrf; m(2)=m(2)*xyzrf; m=rot(rfangle_f,"x")*m; //new stuff m00=sqrt(m(1)*m(1)+m(2)*m(2)); if (opt_test==1 && readstep%4096==0 && v==1 ){ cout<<"Projection of the magnetisation vector into the xy plane after flipping is "<=0 && nf<=(Nslc-2)) { off-=nf; double ts=table_slcprof[nf]; double sx=(table_slcprof[nf+1]-ts)*off + ts; double rfangle_f=sx*rfangle*RFtrans;//RFtrans are values 0 to 1 to derscribe the inhomogeneity f the receive RF field. 1 is for perfectly homog; if (opt_test==1 && readstep%4096==0 && v==1 ){ cout<<"table_slcprof[nf]= "<0){//new stuff mon dec 19 excitation=1; if (opt_test==1 && readstep%4096==0 && readstep>7*4096){ cout<0) phase_2pi=phase- ((int) (phase*glo_itwopi))*glo_twopi;//one solution when phase exceedes Dsin , this is faster else phase_2pi=phase- ((int) (phase*glo_itwopi))*glo_twopi+glo_twopi; double off=phase_2pi*glo_idsin; int nphase=(int) off; off-=nphase; double ts1=table_sin[nphase], tc1=table_cos[nphase]; double wanted_cos=(table_cos[nphase+1]-tc1)*off+tc1; double wanted_sin=(table_sin[nphase+1]-ts1)*off+ts1; //CALCULATING SIGNAL sreal[readstep-1]+=den*tmp*wanted_cos; simag[readstep-1]+=den*tmp*wanted_sin; if(opt_test==1 && readstep%4096==2081 && v==1){ cout.precision(20); cout<<"readstep= "<=timecourse[actstep] && actstep<=(Nact-2)){ coeff(activation[actstep],activation[actstep+1],timecourse[actstep],timecourse[actstep+1],dT2_1,dT2_2); dT2_1=dT2_1*iT2*iT2; dT2_2=dT2_2*iT2*iT2; actstep=actstep+1; } actint+=(dT2_1+dT2_2*(tnew+told)/2)*(tnew-told); if (told>=b0timecourse[b0step] && b0step<=(Nb0-2)){ coeff(b0time[b0step],b0time[b0step+1],b0timecourse[b0step],b0timecourse[b0step+1],db0_1,db0_2); coeff(b0xtime[b0step],b0xtime[b0step+1],b0timecourse[b0step],b0timecourse[b0step+1],db0x_1,db0x_2); coeff(b0ytime[b0step],b0ytime[b0step+1],b0timecourse[b0step],b0timecourse[b0step+1],db0y_1,db0y_2); coeff(b0ztime[b0step],b0ztime[b0step+1],b0timecourse[b0step],b0timecourse[b0step+1],db0z_1,db0z_2); if (opt_test==1){ cout<<"Voxel number="<0){//new stuff mon dec 19 excitation=1; m=free(m,tt,tissue,phase,actint); //new stuff mon dec 19 //due to crushers or any gradient induced dephasing over the voxel a new initial magnetisation is introduced which is the average of the magnetisations over the voxel double xvalrf=fabs(glo_cx*(gg1+b0xint+b0x*tt)); double yvalrf=fabs(glo_cy*(gg2+b0yint+b0y*tt)); double zvalrf=fabs(glo_cz*(gg3+b0zint+b0z*tt)); double xyzrf=Sinc(xvalrf)*Sinc(yvalrf)*Sinc(zvalrf); m(1)=m(1)*xyzrf; m(2)=m(2)*xyzrf; m=rot(rfangle_f,"x")*m; //new stuff m00=sqrt(m(1)*m(1)+m(2)*m(2)); if (opt_test==1 && fabs(z)<0.0005 ){ cout<<"Projection of the magnetisation vector into the xy plane after flipping is "<