/* Copyright (C) 2012 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. */ #include //FSL INCLUDES #include #include #include #include #include #include "newmatap.h" #include "libprob.h" //FSL namespaces using namespace SHAPE_MODEL_NAME; using namespace fslvtkio; using namespace NEWIMAGE; //MAY WANT TO REMOVE DEPENDENCY ON THIS LIBRARY IN THE FUTURE, we'll see using namespace FIRST_LIB; //STL using namespace std; namespace fslsurface_name { template void draw_segment(volume& image, const vec3 & p1, const vec3 & p2, const int & label) { vec3 dims = vec3(image.xdim(),image.ydim(), image.zdim()); float mininc = (dims.y < dims.z) ? dims.y : dims.z; mininc = ( ( dims.x < mininc ) ? dims.x : mininc ) * 0.5; // cout<<"dims "< n = p1 - p2; double d = n.norm(); n.normalize(); // double l = d*4; for (double i=0; i<=d; i+=mininc) { // cout<<"draw "< p = p2 + n * i; // cout<<(int) floor((p.x)/dims.x +.5)<<" "<<(int) floor((p.y)/dims.y +.5)<<" "<<(int) floor((p.z)/dims.z +.5)<<" "<(volume& image, const vec3 & p1, const vec3 & p2, const int & label); template void draw_mesh(const fslSurface & surf, volume & image, const int & lb) { vec3 dims = vec3(image.xdim(),image.ydim(), image.zdim()); //in new version of bet2 // double mininc = min(xdim,min(ydim,zdim)) * .5; float mininc = (dims.y < dims.z) ? dims.y : dims.z; mininc = ( ( dims.x < mininc ) ? dims.x : mininc ) * 0.5; typename vector< vertex >::const_iterator i_v = surf.const_vbegin(); for ( typename vector::const_iterator i_f = surf.const_facebegin(); i_f != surf.const_faceend(); i_f= i_f+3) { vec3 n = vec3( (i_v+ *(i_f+1) )->x - (i_v+ *i_f)->x, \ (i_v+ *(i_f+1) )->y - (i_v+ *i_f)->y, \ (i_v+ *(i_f+1) )->z - (i_v+ *i_f)->z ); float d = n.norm(); n.normalize(); for (float j=0; j<=d; j+=mininc) { vec3 p= n*j; p += vec3( (i_v+ *i_f)->x , (i_v+ *i_f)->y , (i_v+ *i_f)->z ); vec3 p2= vec3( (i_v+ *(i_f+2))->x,(i_v+ *(i_f+2))->y,(i_v+ *(i_f+2))->z ); //coord(); draw_segment(image, p ,p2,lb); } // cout<<"f "<<*i_f< res = image; /* for (list::const_iterator i = m._triangles.begin(); i!=m._triangles.end(); i++) { Vec n = (*(*i)->get_vertice(0) - *(*i)->get_vertice(1)); double d = n.norm(); n.normalize(); for (double j=0; j<=d ; j+=mininc) { Pt p = (*i)->get_vertice(1)->get_coord() + (double)j* n; draw_segment(res, p, (*i)->get_vertice(2)->get_coord(),label); } } */ //return res; } template void draw_mesh(const fslSurface & surf, volume& image, const int & label); template volume fillMesh( const fslSurface & surf, const volume & image, const int & label ) { vec3 dims = vec3(image.xdim(),image.ydim(), image.zdim()); volume mask; copyconvert(image,mask); mask = 0; draw_mesh(surf, mask, label+100); volume otl = mask; vector bounds = surf.getBounds(); // cout<<"got bounds "< > current; vec3 c(bounds[0]-2, bounds[2]-2, bounds[4]-2); current.push_back(c); mask.value(static_cast(c.x),static_cast(c.y),static_cast(c.z)) = label; int fillCount=0; while (!current.empty()) { vec3 pc = current.back(); int x, y, z; x=(int) pc.x; y=(int) pc.y; z=(int) pc.z; current.pop_back(); fillCount++; if (bounds[0]<=x-1 && mask.value(x-1, y, z)==0) { mask.value(x-1, y, z) = label; current.push_back(vec3(x-1, y, z)); } if (bounds[2]<=y-1 && mask.value(x, y-1, z)==0) { mask.value(x, y-1, z) = label; current.push_back(vec3(x, y-1, z)); } if (bounds[4]<=z-1 && mask.value(x, y, z-1)==0) { mask.value(x, y, z-1) = label; current.push_back(vec3(x, y, z-1)); } if (bounds[1]>=x+1 && mask.value(x+1, y, z)==0){ mask.value(x+1, y, z) = label; current.push_back(vec3(x+1, y, z)); } if (bounds[3]>=y+1 && mask.value(x, y+1, z)==0){ mask.value(x, y+1, z) = label; current.push_back(vec3(x, y+1, z)); } if (bounds[5]>=z+1 && mask.value(x, y, z+1)==0){ mask.value(x, y, z+1) = label; current.push_back(vec3(x, y, z+1)); } } for (int i=bounds[0];i fillMesh( const fslSurface & surf, const NEWIMAGE::volume & image, const int & label ) ; //template volume fillMesh( const fslSurface & surf, volume & image, const int & label ); float MVGLM_fit(Matrix G, Matrix D, Matrix contrast, int& df1, int& df2){ cout<<"enter MVGLMFIT "< X = design matrix (N_subj by N_evs) // D -> Y = data matrix (N_subj by 3) //Calculate estimated values Matrix Yhat=G*(G.t()*G).i()*G.t()*D; //caluclate E covariance matrix cout<<"enter MVGLMFIT2 "< FtoP( const std::vector & F, const int & dof1, const int & dof2) { cout<<"first FtoP "< P(F.size()); vector::iterator i_P = P.begin(); for (vector::const_iterator i_F = F.begin(); i_F != F.end(); ++i_F,++i_P) *i_P = 1-MISCMATHS::fdtr(dof1,dof2,*i_F); return P; } shapeModel* loadAndCreateShapeModel( const string & modelname) { //read in model fslvtkIO* fmodel = new fslvtkIO(modelname,static_cast(0)); //get number of points const int Npts=fmodel->getPointsAsMatrix().Nrows(); unsigned int M = static_cast(fmodel->getField("numSubjects").element(0,0)); int MaxModes=M; //Setting uo shape model and appearance model //load mean shape into vector vector Smean; Matrix* Pts = new Matrix; *Pts=fmodel->getPointsAsMatrix(); for (int i=0;i< Npts ; i++){ Smean.push_back(Pts->element(i,0)); Smean.push_back(Pts->element(i,1)); Smean.push_back(Pts->element(i,2)); } Pts->ReleaseAndDelete(); //read polygon data vector< vector > polygons = first_newmat_vector::matrixToVector(fmodel->getPolygons().t()); //process shape modes and conditional intensity mean modes Matrix SmodesM; Matrix ImodesM; SmodesM=first_newmat_vector::unwrapMatrix(fmodel->getField("mode0")); ImodesM=first_newmat_vector::unwrapMatrix(fmodel->getField("Imode0")); //only keep max modes of variation for (int i =1; i>mode; SmodesM=SmodesM | first_newmat_vector::unwrapMatrix(fmodel->getField("mode"+mode)); ImodesM=ImodesM | first_newmat_vector::unwrapMatrix(fmodel->getField("Imode"+mode)); } vector< vector > Smodes = first_newmat_vector::matrixToVector(SmodesM); vector< vector > Imodes = first_newmat_vector::matrixToVector(ImodesM); ImodesM.Release(); SmodesM.Release(); //process rest of information, including intensity variance vector< vector > Iprec = first_newmat_vector::matrixToVector(fmodel->getField("iCondPrec0").t()); vector Errs = first_newmat_vector::vectorToVector(fmodel->getField("ErrPriors0")); vector se = first_newmat_vector::vectorToVector(fmodel->getField("eigenValues"), MaxModes); vector ie = first_newmat_vector::vectorToVector(fmodel->getField("iCondEigs0")); vector Imean = first_newmat_vector::vectorToVector(first_newmat_vector::unwrapMatrix(fmodel->getField("Imean"))); vector labels = first_newmat_vector::vectorToVector(fmodel->getField("labels")); //have read in all data and store in local structures, now delete the reader. delete fmodel; //create shape model shapeModel* model1 = new shapeModel(Smean, Smodes, se, Imean, Imodes,Iprec, ie, M,Errs,polygons,labels); return model1; } void readBvars(const string & filename, const string & image_path, string & modelname, unsigned int & Nsubjects, vector & v_imagenames, vector< vector > & v_bvars, vector< vector > & v_xfms) { v_bvars.clear(); v_xfms.clear(); v_imagenames.clear(); //for now image name is not used string stemp; string imagename; unsigned int Nbvars; //lets start reading the mode parameters from the file ifstream fin; fin.open(filename.c_str()); //cout<<"open file "<>modelname; fin>>stemp>>Nsubjects; //cout<<"Nsubjects "< one_sub_bvars; vector one_sub_xfm; fin>>imagename>>Nbvars; //cout<<"imagepath "<(&blank),sizeof(char)); float bvar; // //cout<<"BVARS"<(&bvar),sizeof(float)); one_sub_bvars.push_back(bvar); // //cout<(&bvar),sizeof(float)); one_sub_xfm.push_back(bvar); // //cout< v_imagenames; vector< vector > v_bvars; vector< vector > v_xfms; Matrix xfm_mni(4,4); xfm_mni=0; //load bvars //"" is in placve of image path readBvars(filename, "", modelname, Nsubjects, v_imagenames, v_bvars, v_xfms); shapeModel* model1=loadAndCreateShapeModel(modelname); cout<<"shape model loaded"< mni; volume* immni = new volume(); char* fsldir = getenv("FSLDIR"); read_volume_hdr_only(*immni, string(fsldir) + "/data/standard/MNI152_T1_1mm"); //read_volume_hdr_only(*immni, template_name); // vector< float > csys_eye(16,0); // csys_eye[0]=1; // csys_eye[5]=1; // csys_eye[10]=1; // csys_eye[15]=1; // //cout<<"add coord system "<at(0)<newimagevox2mm_mat(); Matrix m_mm2vox_mni(4,4); m_mm2vox_mni=0; m_mm2vox_mni.element(0, 0) = 1.0/immni->xdim(); m_mm2vox_mni.element(1, 1) = 1.0/immni->ydim(); m_mm2vox_mni.element(2, 2) = 1.0/immni->zdim(); m_mm2vox_mni.element(3, 3) = 1.0; xfm_mni = xfm_mni * m_mm2vox_mni; ////cout<<"xfmmni2 "< fmat; for (int i = 0 ; i < 4; ++i) for (int j = 0 ; j < 4; ++j) fmat.push_back(xfm_mni.element(i, j)); delete immni; //-----out mean surface //save mean surface fslSurface *surf = new fslSurface(); vector bvars(1,0); surf->setVertices( model1->getDeformedGrid(bvars) ); surf->setFaces( model1->cells ); apply_xfm(*surf, fmat); // //cout<<"add coord system "<at(0)< csys_eye(16,0); csys_eye[0]=1; csys_eye[5]=1; csys_eye[10]=1; csys_eye[15]=1; surf->addCoordSystem( csys_eye, "NIFTI_XFORM_SCANNER_ANAT"); //-------------------------------------------------------- Matrix flirtmat = xfm_mni.i(); fmat.clear(); for (int i = 0 ; i < 4; ++i) for (int j = 0 ; j < 4; ++j) fmat.push_back(flirtmat.element(i, j)); surf->addCoordSystem( fmat, "FSL_XFORM_SCALEDMM"); //-------------------------------------------------------- writeGIFTI(*surf,out_base+"_modelmean.gii"); delete surf; } //------------------------------------------// cout<<"done mean"< >::iterator i_xfms = v_xfms.begin(); vector::iterator i_imnames = v_imagenames.begin(); unsigned int count = 0; //only for one set of bvars for ( vector< vector >::iterator i_bvars = v_bvars.begin(); i_bvars != v_bvars.end(); ++i_bvars, ++i_xfms, ++i_imnames, ++count ) { fslSurface *surf = new fslSurface(); volume* im = new volume(); read_volume_hdr_only(*im, *i_imnames ); Matrix m_vox2mm = im->newimagevox2mm_mat(); Matrix m_mm2vox(4,4); m_mm2vox=0; m_mm2vox.element(0, 0) = 1.0/im->xdim(); m_mm2vox.element(1, 1) = 1.0/im->ydim(); m_mm2vox.element(2, 2) = 1.0/im->zdim(); m_mm2vox.element(3, 3) = 1.0; delete im; Matrix flirtmat(4,4); vector::iterator i_one_xfm = i_xfms->begin(); for (int i=0; i<4;i++){ for (int j=0; j<4;j++, ++i_one_xfm){ flirtmat.element(i,j) = *i_one_xfm; //cout<setVertices( model1->getDeformedGrid(*i_bvars) ); surf->setFaces( model1->cells ); //----------------------------in native space---------------------------- vector< float > csys_eye(16,0); csys_eye[0]=1; csys_eye[5]=1; csys_eye[10]=1; csys_eye[15]=1; // flirtmat = m_mm2vox * m_vox2mm; vector fmat; for (int i = 0 ; i < 4; ++i) { for (int j = 0 ; j < 4; ++j) { //cout<at(0)<addCoordSystem( csys_eye, "NIFTI_XFORM_SCANNER_ANAT"); //--------------------------back to mni------------------------------ //need to transform to scaled voxels, then applyxfm, the transform to NIFTI //cout<<"setmni "<( surf, fmat ); surf->addCoordSystem( fmat, "NIFTI_XFORM_MNI_152"); //-------------------------------------------------------- flirtmat = m_vox2mm * m_mm2vox ; flirtmat = flirtmat.i(); fmat.clear(); for (int i = 0 ; i < 4; ++i) for (int j = 0 ; j < 4; ++j) fmat.push_back(flirtmat.element(i, j)); surf->addCoordSystem( fmat, "FSL_XFORM_SCALEDMM"); //-------------------------------------------------------- stringstream ss; ss<>snum; writeGIFTI(*surf,out_base+snum+".gii"); fsurf_log< & surf, const string & filename)//, const string & template_name) { string modelname; unsigned int Nsubjects; vector v_imagenames; vector< vector > v_bvars; vector< vector > v_xfms; Matrix xfm_mni(4,4); xfm_mni=0; //load bvars //"" is in placve of image path readBvars(filename, "", modelname, Nsubjects, v_imagenames, v_bvars, v_xfms); shapeModel* model1=loadAndCreateShapeModel(modelname); //this is reconstructing the models mean surface //-------------save mean mesh--------------// { // volume mni; volume* immni = new volume(); char* fsldir = getenv("FSLDIR"); read_volume_hdr_only(*immni, string(fsldir) + "/data/standard/MNI152_T1_1mm"); //read_volume_hdr_only(*immni, template_name); // vector< float > csys_eye(16,0); // csys_eye[0]=1; // csys_eye[5]=1; // csys_eye[10]=1; // csys_eye[15]=1; // //cout<<"add coord system "<at(0)<newimagevox2mm_mat(); Matrix m_mm2vox_mni(4,4); m_mm2vox_mni=0; m_mm2vox_mni.element(0, 0) = 1.0/immni->xdim(); m_mm2vox_mni.element(1, 1) = 1.0/immni->ydim(); m_mm2vox_mni.element(2, 2) = 1.0/immni->zdim(); m_mm2vox_mni.element(3, 3) = 1.0; xfm_mni = xfm_mni * m_mm2vox_mni; ////cout<<"xfmmni2 "< fmat; for (int i = 0 ; i < 4; ++i) for (int j = 0 ; j < 4; ++j) fmat.push_back(xfm_mni.element(i, j)); delete immni; } //------------------------------------------// vector< vector >::iterator i_xfms = v_xfms.begin(); vector::iterator i_imnames = v_imagenames.begin(); unsigned int count = 0; //only for one set of bvars for ( vector< vector >::iterator i_bvars = v_bvars.begin(); i_bvars != v_bvars.begin()+1; ++i_bvars, ++i_xfms, ++i_imnames, ++count ) { volume* im = new volume(); read_volume_hdr_only(*im, *i_imnames ); Matrix m_vox2mm = im->newimagevox2mm_mat(); Matrix m_mm2vox(4,4); m_mm2vox=0; m_mm2vox.element(0, 0) = 1.0/im->xdim(); m_mm2vox.element(1, 1) = 1.0/im->ydim(); m_mm2vox.element(2, 2) = 1.0/im->zdim(); m_mm2vox.element(3, 3) = 1.0; delete im; Matrix flirtmat(4,4); vector::iterator i_one_xfm = i_xfms->begin(); for (int i=0; i<4;i++){ for (int j=0; j<4;j++, ++i_one_xfm){ flirtmat.element(i,j) = *i_one_xfm; //cout<getDeformedGrid(*i_bvars) ); surf.setFaces( model1->cells ); //----------------------------in native space---------------------------- vector< float > csys_eye(16,0); csys_eye[0]=1; csys_eye[5]=1; csys_eye[10]=1; csys_eye[15]=1; // flirtmat = m_mm2vox * m_vox2mm; vector fmat; for (int i = 0 ; i < 4; ++i) { for (int j = 0 ; j < 4; ++j) { //cout<at(0)<( surf, fmat ); surf.addCoordSystem( fmat, "NIFTI_XFORM_MNI_152"); //-------------------------------------------------------- flirtmat = m_vox2mm * m_mm2vox ; flirtmat = flirtmat.i(); fmat.clear(); for (int i = 0 ; i < 4; ++i) for (int j = 0 ; j < 4; ++j) fmat.push_back(flirtmat.element(i, j)); surf.addCoordSystem( fmat, "FSL_XFORM_SCALEDMM"); //-------------------------------------------------------- } delete model1; } vector getFSLtoNIFTIxfm( const volume & imref) { Matrix m_vox2mm = imref.newimagevox2mm_mat(); Matrix m_mm2vox(4,4); m_mm2vox=0; m_mm2vox.element(0, 0) = 1.0/imref.xdim(); m_mm2vox.element(1, 1) = 1.0/imref.ydim(); m_mm2vox.element(2, 2) = 1.0/imref.zdim(); m_mm2vox.element(3, 3) = 1.0; Matrix M_fmat=m_vox2mm * m_mm2vox; vector fmat; for (int i = 0 ; i < 4; ++i) for (int j = 0 ; j < 4; ++j) fmat.push_back(M_fmat.element(i, j)); return fmat; } void reconSurfaces_from_bvars( vector< fslSurface > & v_surf, const string & filename, const string & imagepath, const string & template_name, string out_base, bool reconMNI ) { // for (vector::iterator i_surf = v_surf.begin(); v_surf.end(); //will alter object or push back a new one ofstream f_surf_list( (out_base + "_surface_list.txt").c_str() ); //-------------------mni tmeplate xfm info-------------// Matrix xfm_mni(4,4); xfm_mni=0; //-------------------mni tmeplate xfm info-------------// // //cout<<"mni "< v_imagenames; vector< vector > v_bvars; vector< vector > v_xfms; //load bvars readBvars(filename, imagepath, modelname, Nsubjects, v_imagenames, v_bvars, v_xfms); //get model name from file shapeModel* model1=loadAndCreateShapeModel(modelname); //this is reconstructing the models mean surface //-------------save mean mesh--------------// { volume* immni = new volume(); read_volume_hdr_only(*immni, template_name); fslSurface* surf_mean = new fslSurface(); surf_mean->setVertices( model1->smean ); surf_mean->setFaces( model1->cells ); vector< float > csys_eye(16,0); csys_eye[0]=1; csys_eye[5]=1; csys_eye[10]=1; csys_eye[15]=1; // //cout<<"add coord system "<at(0)<addCoordSystem( csys_eye, "NIFTI_XFORM_MNI_152"); xfm_mni = immni->newimagevox2mm_mat(); Matrix m_mm2vox_mni(4,4); m_mm2vox_mni=0; m_mm2vox_mni.element(0, 0) = 1.0/immni->xdim(); m_mm2vox_mni.element(1, 1) = 1.0/immni->ydim(); m_mm2vox_mni.element(2, 2) = 1.0/immni->zdim(); m_mm2vox_mni.element(3, 3) = 1.0; xfm_mni = xfm_mni * m_mm2vox_mni; ////cout<<"xfmmni2 "< fmat; for (int i = 0 ; i < 4; ++i) for (int j = 0 ; j < 4; ++j) fmat.push_back(xfm_mni.element(i, j)); apply_xfm( *surf_mean, fmat ); writeGIFTI(*surf_mean, out_base + "_mean_mni.gii"); delete immni; delete surf_mean; } //------------------------------------------// ////cout<<"xfmmni "< >::iterator i_xfms = v_xfms.begin(); vector::iterator i_imnames = v_imagenames.begin(); unsigned int count = 0; for ( vector< vector >::iterator i_bvars = v_bvars.begin(); i_bvars != v_bvars.end(); ++i_bvars, ++i_xfms, ++i_imnames, ++count ) { // ////cout<<"imname "<<*i_imnames<* im = new volume(); read_volume_hdr_only(*im, *i_imnames ); Matrix m_vox2mm = im->newimagevox2mm_mat(); Matrix m_mm2vox(4,4); m_mm2vox=0; m_mm2vox.element(0, 0) = 1.0/im->xdim(); m_mm2vox.element(1, 1) = 1.0/im->ydim(); m_mm2vox.element(2, 2) = 1.0/im->zdim(); m_mm2vox.element(3, 3) = 1.0; delete im; Matrix flirtmat(4,4); vector::iterator i_one_xfm = i_xfms->begin(); for (int i=0; i<4;i++){ for (int j=0; j<4;j++, ++i_one_xfm){ flirtmat.element(i,j) = *i_one_xfm; //cout< fmat; for (int i = 0 ; i < 4; ++i) for (int j = 0 ; j < 4; ++j) fmat.push_back(flirtmat.element(i, j)); //vector v_b(i_bvars->size(), 0); fslSurface* surf = new fslSurface(); // surf->setVertices( model1->getDeformedGrid(v_b) );//model1->getDeformedGrid(*i_bvars) ); surf->setVertices( model1->getDeformedGrid(*i_bvars) ); // apply_xfm( surf, fmat ); // apply_xfm( surf, *i_xfms ); //for (int j =0 ; j<16 ; ++j) //cout<(fmat,"inverse fmat"); surf->setFaces( model1->cells ); //----------------------------in native space---------------------------- vector< float > csys_eye(16,0); csys_eye[0]=1; csys_eye[5]=1; csys_eye[10]=1; csys_eye[15]=1; // //cout<<"add coord system "<at(0)<addCoordSystem( csys_eye, "NIFTI_XFORM_SCANNER_ANAT"); //--------------------------back to mni------------------------------ //cout<<"setmni "<( *surf, fmat ); surf->addCoordSystem( fmat, "NIFTI_XFORM_MNI_152"); //-------------------------------------------------------- flirtmat = m_vox2mm * m_mm2vox ; flirtmat = flirtmat.i(); fmat.clear(); for (int i = 0 ; i < 4; ++i) for (int j = 0 ; j < 4; ++j) fmat.push_back(flirtmat.element(i, j)); surf->addCoordSystem( fmat, "FSL_XFORM_SCALEDMM"); //-------------------------------------------------------- string outname; if ( out_base == "NULL" ) { // //cout<<"imname end "<substr(i_imnames->length()-3,3)<substr(i_imnames->length()-3,3) == ".gz" ) { i_imnames->erase(i_imnames->length()-3,3); } if ( i_imnames->substr(i_imnames->length()-4,4) == ".nii" ) { i_imnames->erase(i_imnames->length()-4,4); } if ( i_imnames->substr(i_imnames->length()-4,4) == ".hdr" ) { i_imnames->erase(i_imnames->length()-4,4); } if ( i_imnames->substr(i_imnames->length()-4,4) == ".img" ) { i_imnames->erase(i_imnames->length()-4,4); } }else { stringstream ss; ss<>temp; if (count< 10) temp="000"+temp; else if (count < 100) temp="00"+temp; else if (count < 1000) temp="0"+temp; outname=out_base+"_"+temp+".gii"; } f_surf_list< subjectnames; Matrix bvars; vector vN; vector< vector< vector > > fmatv; read_bvars(inname.value(),bvars,subjectnames, vN, pathname.value(),fmatv); vector vars; for (int i=0; iregisterModel(fmatv.at(0)); //cout<<"vars "<getDeformedGrid(vars),model1->cells); meshReg(m,fmatv.at(0)); m.save(outname.value(),3); */ } vector meshRegLeastSq( const fslSurface & m_src, const fslSurface & m_target, const unsigned int & dof ) { //demenad vertices //not most efficient but copied from meshutils Matrix Points(m_src.getNumberOfVertices(),3); Matrix refPoints(m_target.getNumberOfVertices(),3); //cout<<"point "<getNumberOfVertices()<<" "< >::const_iterator i_v = m_src.const_vbegin(); i_v != m_src.const_vend(); ++i_v, ++count ) { Points.element(count,0) = i_v->x; Points.element(count,1) = i_v->y; Points.element(count,2) = i_v->z; } count=0; for ( vector< vertex >::const_iterator i_v = m_target.const_vbegin(); i_v != m_target.const_vend(); ++i_v, ++count ) { refPoints.element(count,0) = i_v->x; refPoints.element(count,1) = i_v->y; refPoints.element(count,2) = i_v->z; } //cout<3) preMultiplyGlobalRotation(fmat,R); // //cout<<"fmat4 "< vfmat(16,0); for (int i = 0 ; i < 4 ; ++i) for (int j = 0; j < 4; ++j) { vfmat[4*i +j ] = fmat.element(i,j); } return vfmat; } //----------------------------This runs the vertex analysis--------------------// void vertexMVglm( fslSurface & surf_stats, const string & inname, const string & designname, const string & saveVertices , string normname ){ //**********read in bvars and models and lfirt matrices***************// Matrix MeshVerts;//this is used when placing t-stats on a mesh Matrix target; //target is only used for glm target=read_vest(designname); bool useNorm=false; if ( normname != "none" ) useNorm=true; ifstream f_mesh(inname.c_str()); string meshname; unsigned int Nverts = 0; unsigned int Nsubjects = 0; //read in surfaces concatenate into one large martrix while ( f_mesh >> meshname ) { //cout<<"read surface "< *mesh = new fslSurface(); read_surface(*mesh,meshname); if (Nsubjects==0) Nverts = mesh->getNumberOfVertices(); unsigned int vert_index=0; ColumnVector one_mesh_verts(3*Nverts); for ( vector< vertex >::iterator i_m= mesh->vbegin(); i_m != mesh->vend(); ++i_m,vert_index+=3) { // //cout<x; one_mesh_verts.element(vert_index+1) = i_m->y; one_mesh_verts.element(vert_index+2) = i_m->z; } if ( Nsubjects == 0 ) MeshVerts = one_mesh_verts; else { MeshVerts = MeshVerts | one_mesh_verts; } ++Nsubjects; delete mesh; } f_mesh.close(); //----------------------Do Design matrix stuff --------------------// bool isOne=true; //checks for mean Column as first column, if it decides there is a column of ones (first column) then //it will not demean the design matrix for (int i=0;i>normtemp; CVnorm.element(subject)=normtemp; } target=target | CVnorm ; } //****************END DEMEAN DESIGN MATRIX AND ADD ONE COLUMS*********************/ int Nrows = target.Ncols()-1; // if (isOne) // ++Nrows; Matrix contrast(Nrows,target.Ncols()); //int EVmax=1;//EV to include int EVmax=target.Ncols(); for (int EV=EVmin;EV>evnum; vector mean_verts; for ( unsigned int v_ind = 0 ; v_ind < Nverts*3; v_ind += 3 ) { // cout<<"vind "< scalarsT; //this provides the option to do stats on the vertices //create F test contrast matrix (testing each EV separately unsigned int count=0; for (int j=0;j vertices(ReshapedVerts.Nrows()/3,3,1,ReshapedVerts.Ncols()); vertices.setmatrix(ReshapedVerts.t()); save_volume4D(vertices,saveVertices); } cout<<"done sabe vertices"<smean); vector vdofs(2); vdofs[0]=dof1; vdofs[1]=dof2; cout<<"dof12 "<>snum; surf_stats.insertScalars(scalarsT, 0, "F-stats_EV_"+snum); } } } //-----------------------------Transformation Matrix Utilities Begin---------------------------// void preMultiplyGlobalRotation(Matrix & fmat, const Matrix & R) { //assume a 3by 3 rotation matrix //SubMatrix command has indices starting from 1 fmat.SubMatrix(1,3,1,3)=R*fmat.SubMatrix(1,3,1,3); fmat.SubMatrix(1,3,4,4)=R*fmat.SubMatrix(1,3,4,4); } void preMultiplyGlobalScale(Matrix & fmat, const float & s) { for (int col=0;col<4;col++) for (int row=0;row<3;row++) fmat.element(row,col)*=s; } void preMultiplyGlobalScale(Matrix & fmat, const float & sx,const float & sy, const float & sz) { for (int col=0;col<4;col++) fmat.element(0,col)*=sx; for (int col=0;col<4;col++) fmat.element(1,col)*=sy; for (int col=0;col<4;col++) fmat.element(2,col)*=sz; } void preMultiplyTranslation(Matrix & fmat, const float & tx, const float & ty, const float & tz ) { //must be a 4 by 4 matrix fmat.element(0,3)+=tx; fmat.element(1,3)+=ty; fmat.element(2,3)+=tz; } ReturnMatrix getIdentityMatrix(const short N) { Matrix fmat(N,N); fmat=0; for (int i=0;i invertMatrix( const vector & fmat) { vector fmati(16); Matrix Mfmat(4,4); // print::const_iterator i_f=fmat.begin(); for (int i=0;i<4;i++) { for (int j=0;j<4;j++,++i_f) { cout<<*i_f<<" "; Mfmat.element(i,j)=*i_f; } cout<::iterator i_fi=fmati.begin(); for (int i=0;i<4;i++) for (int j=0;j<4;j++,++i_fi) *i_fi = Mfmat.element(i,j); Mfmat.Release(); return fmati; } }