/* * shapeModel.cpp * * * Created by Brian Patenaude on 23/06/2008. * Copyright 2008 University of Oxford All rights reserved. * */ /* 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 "shapeModel.h" #include #include #include #include #include //#include #include "newmat.h" #include "newmatap.h" #include "fslvtkio/fslvtkio.h" #include "first_lib/first_newmat_vec.h" #include #include #include "math.h" using namespace std; using namespace NEWMAT; using namespace FIRST_LIB; using namespace fslvtkio; namespace SHAPE_MODEL_NAME{ shapeModel::shapeModel() { } shapeModel::shapeModel( const vector & mshape, const vector< vector > & modesshape, const vector & se, \ const vector & ishape, const vector< vector > & modesint, const vector & ie, \ const int & M, const vector & errs) { smean=mshape; smodes=modesshape; seigs=se; sqrtseigs=se; for (vector::iterator i=sqrtseigs.begin();i!=sqrtseigs.end();i++) *i = sqrt(*i); sqrtseigsi=sqrtseigs; imean=ishape; imodes=modesint; ieigs=ie; NumberOfSubjects=M; Errs=errs; USE_COND=false; MODE_FOUND=false; mode=0; STORE_REG_XFM=true; } shapeModel::shapeModel( const vector & mshape, const vector< vector > & modesshape, const vector & se, \ const vector & ishape, const vector< vector > & modesint, const vector< vector > & Iprec, const vector & ie,\ const int & M, const vector & errs, const vector< vector > & cellsin, const vector & vlabels ) { labels=vlabels; smean=mshape; smodes=modesshape; seigs=se; sqrtseigs=se; for (vector::iterator i=sqrtseigs.begin();i!=sqrtseigs.end();i++) *i = sqrt(*i); sqrtseigsi=sqrtseigs; imean=ishape; imodes=modesint; i_precision=Iprec; ieigs=ie; NumberOfSubjects=M; Errs=errs; cells=cellsin; USE_COND=false; MODE_FOUND=false; mode=0; STORE_REG_XFM=true; //keeps tracks of neighbourin triangles for (unsigned int i=0; i(smean.size()/3); i++) { vector inds; int count=0; for ( vector< vector >::iterator j=cells.begin(); j!=cells.end();j++,count++) { for ( vector::iterator k= j->begin(); k!=j->end();k++) { if ((*k)==i) { inds.push_back(count); break; } } } localTri.push_back(inds); } } shapeModel::shapeModel( const vector & mshape, const vector< vector > & modesshape, const vector & se, \ const vector & ishape, const vector< vector > & modesint, const vector & ie, \ const int & M, const vector & errs, const vector & vmaskin) { smean=mshape; smodes=modesshape; seigs=se; imean=ishape; imodes=modesint; ieigs=ie; NumberOfSubjects=M; Errs=errs; stmask=vmaskin; MODE_FOUND=false; mode=0; STORE_REG_XFM=true; } void shapeModel::printLabel( const unsigned int & i) const { cout<<"get labvel "<< labels.at(i) < shapeModel::getDeformedGrid( const std::vector & vars ) const { std::vector newshape=smean; std::vector::const_iterator sqrtseigs_i=sqrtseigs.begin(); std::vector< std::vector >::const_iterator smodes_i = smodes.begin(); for (std::vector::const_iterator vars_i=vars.begin(); vars_i!=vars.end(); vars_i++,sqrtseigs_i++, smodes_i++) { std::vector::iterator new_i=newshape.begin(); for (std::vector::const_iterator smodes_j= smodes_i->begin(); smodes_j!= smodes_i->end(); smodes_j++,new_i++) *new_i += (*vars_i) * (*sqrtseigs_i) * (*smodes_j);//* } return newshape; } vector shapeModel::getDeformedIGrid( const vector & vars) const { vector varsnew=vars;//getOrigSpaceBvars(vars); vector newigrid=imean; for (unsigned int i=0; i< varsnew.size();i++){ for (unsigned int j=0; j< imean.size(); j++){ newigrid.at(j)+=varsnew.at(i)*sqrtseigs.at(i)*imodes.at(i).at(j); } } return newigrid; } vector< vector > shapeModel::registerModeVectors( const vector< vector >& vmodes, const vector< vector >& flirtmat ) { vector< vector > reg_modes = vmodes; for (unsigned int i=0; i > & flirtmat) { //start by registering mesh float f_11= flirtmat.at(0).at(0); float f_12= flirtmat.at(0).at(1); float f_13= flirtmat.at(0).at(2); float f_14= flirtmat.at(0).at(3); float f_21= flirtmat.at(1).at(0); float f_22= flirtmat.at(1).at(1); float f_23= flirtmat.at(1).at(2); float f_24= flirtmat.at(1).at(3); float f_31= flirtmat.at(2).at(0); float f_32= flirtmat.at(2).at(1); float f_33= flirtmat.at(2).at(2); float f_34= flirtmat.at(2).at(3); //assumes last line is 0 0 0 1 for (vector::iterator i=smean.begin();i!=smean.end();i+=3) { float x= f_11 * (*i) + f_12 * (*(i+1)) + f_13 * (*(i+2)) + f_14; float y= f_21 * (*i) + f_22 * (*(i+1)) + f_23 * (*(i+2)) + f_24; float z= f_31 * (*i) + f_32 * (*(i+1)) + f_33 * (*(i+2)) + f_34; (*i)=x; (*(i+1))=y; (*(i+2))=z; } vector< vector > smodesAinv; //Matrix M_smodes_mni= first_newmat_vector::vectorOfVectorsToMatrix(smodes); Matrix M_smodesPre= first_newmat_vector::vectorOfVectorsToMatrix(smodes).t(); smodes=registerModeVectors(smodes,flirtmat); Matrix M_smodes= first_newmat_vector::vectorOfVectorsToMatrix(smodes).t(); Matrix U,U2,V; DiagonalMatrix D,D2; Matrix Mfmat(3,3); for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { Mfmat.element(i,j)=flirtmat.at(i).at(j); cout<(M_smodesPre.t()*A); d_xfm=sqrtseigs; } SVD(M_smodes,D,U,V); DiagonalMatrix Eigs= first_newmat_vector::vectorToDiagonalMatrix(seigs); //DiagonalMatrix SqrtEigs= first_newmat_vector::vectorToDiagonalMatrix(sqrtseigs); SVD(D*V.t()*Eigs*V*D,D2,U2); // cout<<"st ore reg xfm "<((U*U2).SubMatrix(1,U.Nrows(),1,D2.Nrows())); vector veigs, vsqrt_eigs; for (unsigned int i=0;i(D2.Nrows());i++)//static_cast(D2.Nrows());i++) { veigs.push_back(D2.element(i)); vsqrt_eigs.push_back(sqrt(D2.element(i))); } seigs=veigs; sqrtseigs=vsqrt_eigs; Matrix M_imodes= first_newmat_vector::vectorOfVectorsToMatrix(imodes).t(); // cout<<"Transform imodes "<((M_imodes*C*U2)); cout<<"NEw done imodes transform"< shapeModel::getOrigSpaceBvars(const std::vector & bvars ) const { vector bvars_orig; if (STORE_REG_XFM) { DiagonalMatrix D2=first_newmat_vector::vectorToDiagonalMatrix(sqrtseigs); DiagonalMatrix D=first_newmat_vector::vectorToDiagonalMatrix(d_xfm); Matrix UU2 = first_newmat_vector::vectorOfVectorsToMatrix(u_xfm); Matrix M_smodes= first_newmat_vector::vectorOfVectorsToMatrix(smodes).t(); /* cout<<"Smodes2 "<::const_iterator i=bvars.begin();i!=bvars.end();i++,count++) Mbvars.element(count)=*i; cout<<"D "<(0)); if (verbose) cout<<"done reading model"<getPointsAsMatrix().Nrows(); if (verbose) cout<<"setting up shape/appearance model"< Smean; Matrix* Pts = new Matrix; *Pts=fmodel->getPointsAsMatrix(); if (verbose) cout<<"The shape has "<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")); unsigned int M = static_cast(fmodel->getField("numSubjects").element(0,0)); if (verbose) cout<<"The model was constructed from "<>mode; SmodesM=SmodesM | first_newmat_vector::unwrapMatrix(fmodel->getField("mode"+mode)); ImodesM=ImodesM | first_newmat_vector::unwrapMatrix(fmodel->getField("Imode"+mode)); } if (verbose) cout< > 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"), AllModes); 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; cout<<"create shapeModel "<