/* run_mesh_utils.cc Brian Patenaude and Mark Jenkinson Copyright (C) 2006-2009 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 #include #include #include #include #include #include #include #include "math.h" #include "miscmaths/f2z.h" #include "libprob.h" #include "meshUtils.h" #include "first_lib/first_mesh.h" #include "first_lib/first_newmat_vec.h" #include "fslvtkio/fslvtkio.h" // #include "shapeModel/shapeModel.h" #include "miscmaths/miscprob.h" #include "utils/options.h" #include "newimage/newimageall.h" #include "meshclass/meshclass.h" #include "warpfns/fnirt_file_reader.h" #include "basisfield/basisfield.h" #include "MVdisc/MVdisc.h" #include "shapeModel/shapeModel.h" using namespace std; using namespace NEWIMAGE; using namespace MISCMATHS; using namespace SHAPE_MODEL_NAME; using namespace Utilities; using namespace mesh; using namespace fslvtkio; using namespace meshutils; // using namespace SHAPE_MODEL_NAME; using namespace FIRST_LIB; using namespace mvdisc; string title="run_mesh_utils\nCopyright(c) 2009, University of Oxford (Brian Patenaude)"; string examples="run_mesh_utils [options] "; Option verbose(string("-v,--verbose"), false, string("switch on diagnostic messages"), false, no_argument); Option help(string("-h,--help"), false, string("display this message"), false, no_argument); Option sampNN(string("--sampNN,--help"), false, string("display this message"), false, no_argument); Option toggle(string("--toggle,--toggle"), false, string("toggle a feature"), false, no_argument); Option myindex(string("-a,--myindex"), 0, string("degrees of freedom"), false, requires_argument); Option dof(string("-d,--dof"), 6, string("degrees of freedom (for fsl4.1.2 or before)"), false, requires_argument); Option dof2(string("-e,--dof2"), 6, string("degrees of freedom2 (for fsl4.1.2 or before)"), false, requires_argument); Option inname(string("-i,--in"), string(""), string("filename of input image"), false, requires_argument); Option flirtmatname(string("-f"), string(""), string("filename of flirt matrix"), false, requires_argument); Option inname2(string("-j"), string(""), string("filename ofsecond input image"), false, requires_argument); Option label(string("-l,--label"),1, string("mesh label"), false, requires_argument); Option inmeshname(string("-m"), string(""), string("filename of base mesh"), true, requires_argument); Option inmeshname2(string("-n"), string(""), string("filename of base mesh2"), false, requires_argument); Option outname(string("-o,--out"), string(""), string("filename of output image"), true, requires_argument); Option thresh(string("-t,--thresh"), 0, string("threshold"), false, requires_argument); Option shiftx(string("-x,--xshift"), 0, string("filename of input image"), false, requires_argument); Option shifty(string("-y,--yshift"), 0, string("filename of input image"), false, requires_argument); Option shiftz(string("-z,--zshift"), 0, string("filename of input image"), false, requires_argument); Option w_im(string("-p,--w_im"), 0.175, string("weighting image force"), false, requires_argument); Option w_tan(string("-r,--w_tan"), 0, string("weighting image force"), false, requires_argument); Option w_tri(string("-q,--w_tri"), 0.01, string("weighting image force"), false, requires_argument); Option w_norm(string("-s,--w_norm"), 0, string("weighting image force"), false, requires_argument); Option fullLDAoutput(string("--fullLDAoutput"), false, string("output full classification information"), false, no_argument); Option doMeshReg(string("--doMeshReg,--meshreg"), false, string("display this message"), false, no_argument); Option doFillMesh(string("--doFillMesh,--fillmesh"), false, string("display this message"), false, no_argument); Option doMeshToContours(string("--doMeshToContours,--meshToCont"), false, string("convert mesh to a series of 2D contours"), false, no_argument); Option doCombineMeshes(string("--doCombineMeshes,--combMeshes"), false, string("combine mesh results including vectors and scalars"), false, no_argument); Option doUnCentreMesh(string("--doUnCentreMesh,doUnCentreMesh"), false, string("remove centre form ASCII vtk mesh (remove old IBIM format)"), false, no_argument); Option doLabelAndCombineSB(string("--doLabelAndCombineSB, doLabelAndCombineSB"), false, string("label shared vertices and combine meshes"), false, no_argument); Option doAddMeshes(string("--doAddMeshes, doAddMeshes"), false, string("doAddMeshes"), false, no_argument); Option doSubtractMeshes(string("--doSubtractMeshes, doSubtractMeshes"), false, string("doSubtractMeshes"), false, no_argument); Option doAppendSBmask(string("--doAppendSBmask, doAppendSBmask"), false, string("doAppendSBmask"), false, no_argument); Option doAlterVertsBySBmask(string("--doAlterVertsBySBmask, doAlterVertsBySBmask"), false, string("doAlterVertsBySBmask"), false, no_argument); Option doAppendIndexedSBmask(string("--doAppendIndexedSBmask, doAppendIndexedSBmask"), false, string("doAppendIndexedSBmask"), false, no_argument); Option useSc2(string("--useSc2, useSc2"), false, string("useSc2"), false, no_argument); Option inverse(string("--inverse, inverse"), false, string("inverse"), false, no_argument); Option doWarpMesh(string("--doWarpMesh, doWarpMesh"), false, string(" doWarpMesh"), false, no_argument); Option doApplyFlirtThenSBmask(string("--doApplyFlirtThenSBmask, doApplyFlirtThenSBmask"), false, string("doApplyFlirtThenSBmask"), false, no_argument); Option doSurfDistMap(string("--doSurfDistMap, doSurfDistMap"), false, string("doSurfDistMap"), false, no_argument); Option doSurfMeanAndStDev(string("--doSurfMeanAndStDev, doSurfMeanAndStDev"), false, string("doSurfMeanAndStDev"), false, no_argument); Option doLQSurfReg(string("--doLQSurfReg, doLQSurfReg"), false, string("doLQSurfReg"), false, no_argument); Option doCartToSphere(string("--doCartToSphere, doCartToSphere"), false, string("doCartToSphere"), false, no_argument); Option doSphereToCart(string("--doSphereToCart, doSphereToCart"), false, string("doSphereToCart"), false, no_argument); Option doFindMidMidPoint(string("--doFindMidMidPoint, doFindMidMidPoint"), false, string("doFindMidMidPoint"), false, no_argument); Option doWarpGrid(string("--doWarpGrid, doWarpGrid"), false, string("doWarpGrid"), false, no_argument); Option doSampleGrid(string("--doSampleGrid, doSampleGrid"), false, string("doSampleGrid"), false, no_argument); Option doSampleMesh(string("--doSampleMesh, doSampleMesh"), false, string("doSampleMesh"), false, no_argument); Option doRandMesh(string("--doRandMesh, doRandMesh"), false, string("doRandMesh"), false, no_argument); Option doMeshToBvars(string("--doMeshToBvars, doMeshToBvars"), false, string("doMeshToBvars"), false, no_argument); Option doAddModesUsingScalars(string("--doAddModesUsingScalars, doAddModesUsingScalars"), false, string("doAddModesUsingScalars"), false, no_argument); Option doRandMatrices(string("--doRandMatrices, doRandMatrices"), false, string("doRandMatrices"), false, no_argument); Option doWriteConditionalIntensity(string("--doWriteConditionalIntensity, doWriteConditionalIntensity"), false, string("doWriteConditionalIntensity"), false, no_argument); Option doUgridToImage(string("--doUgridToImage, doUgridToImage"), false, string("doUgridToImage"), false, no_argument); Option doReplaceVertsWithCoef(string("--doReplaceVertsWithCoef, doReplaceVertsWithCoef"), false, string("doReplaceVertsWithCoef"), false, no_argument); Option doCoefModelToImage(string("--doCoefModelToImage, doCoefModelToImage"), false, string("doCoefModelToImage"), false, no_argument); Option doFieldModelToImage(string("--doFieldModelToImage,doFieldModelToImage"), false, string("doFieldModelToImage"), false, no_argument); Option doSubSampleGrid(string("--doSubSampleGrid,doSubSampleGrid"), false, string("doSubSampleGrid"), false, no_argument); Option doScalarsToVolume(string("--doScalarsToVolume"), false, string("doScalarsToVolume"), false, no_argument); Option doVertexScalarsToImageVolume(string("--doVertexScalarsToImageVolume"), false, string("doVertexScalarsToImageVolume"), false, no_argument); Option doVectorsToVolume(string("--doVectorsToVolume"), false, string("doVectorsToVolume"), false, no_argument); Option doPointsToVolume(string("--doPointsToVolume"), false, string("doPointsToVolume"), false, no_argument); Option doVolumeToScalars(string("--doVolumeToScalars"), false, string("doVolumeToScalars"), false, no_argument); Option doVolumeToVectors(string("--doVolumeToVectors"), false, string("doVolumeToVectors"), false, no_argument); Option doFtoP(string("--doFtoP"), false, string("doFtoP"), false, no_argument); Option doGetMeans(string("--doGetMeans"), false, string("doGetMeans"), false, no_argument); Option doFlipMesh(string("--doFlipMesh"), false, string("doFlipMesh"), false, no_argument); Option doAppendConstScalar(string("--doAppendConstScalar"), false, string("doAppendConstScalar"), false, no_argument); Option doShiftGrid(string("--doShiftGrid"), false, string("doShiftGrid"), false, no_argument); Option doConcatIntensityGrid(string("--doConcatIntensityGrid"), false, string("doConcatIntensityGrid"), false, no_argument); Option doDeMeanIntensities(string("--doDeMeanIntensities"), false, string("doDeMeanIntensities"), false, no_argument); Option doConvert_ASCII_To_Binary(string("--doConvert_ASCII_To_Binary"), false, string("doConvert_ASCII_To_Binary"), false, no_argument); Option doConvert_Binary_To_ASCII(string("--doConvert_Binary_To_ASCII"), false, string("doConvert_Binary_To_ASCII"), false, no_argument); Option doUnCentreModel(string("--doUnCentreModel"), false, string(" doUnCentreModel"), false, no_argument); //Option doSampleAndNormalizeIntensities(string("--doSampleAndNormalizeIntensities"), false, // string(" doSampleAndNormalizeIntensities"), // false, no_argument); Option doSampleProfiles(string("--doSampleProfiles"), false, string(" doSampleProfiles"), false, no_argument); Option doDeformSurface(string("--doDeformSurface"), false, string(" doDeformSurface"), false, no_argument); Option doMeshReg_LeastSq(string("--doMeshReg_LeastSq"), false, string(" doMeshReg_LeastSq"), false, no_argument); Option doGetMeshFromModel(string("--doGetMeshFromModel"), false, string("doGetMeshFromModel"), false, no_argument); Option doVertexLDA_LOO(string("--doVertexLDA_LOO"), false, string("doVertexLDA_LOO"), false, no_argument); Option doVertexLDA_save(string("--doVertexLDA_save"), false, string("doVertexLDA_save"), false, no_argument); Option doVertexLDA_loadAndApply(string("--doVertexLDA_loadAndApply"), false, string("doVertexLDA_loadAndApply"), false, no_argument); Option doGetMaxScalar(string("--doGetMaxScalar"), false, string("doGetMaxScalar"), false, no_argument); Option doGetMeanScalar(string("--doGetMeanScalar"), false, string("doGetMeanScalar"), false, no_argument); Option doAddScalars(string("--doAddScalars"), false, string("doAddScalars"), false, no_argument); Option doDivideScalarsByScalar(string("--doDivideScalarsByScalar"), false, string("doDivideScalarsByScalar"), false, no_argument); Option doSubtractConstantFromScalars(string("--doSubtractConstantFromScalars"), false, string("doSubtractConstantFromScalars"), false, no_argument); Option doDisplayNumericFieldNames(string("--doDisplayNumericFieldNames"), false, string("doDisplayNumericFieldNames"), false, no_argument); Option doDisplayNumericField(string("--doDisplayNumericField"), false, string("doDisplayNumericField"), false, no_argument); Option doAddVertices(string("--doAddVertices"), false, string("doAddVertices"), false, no_argument); Option doDivideVerticesByScalar(string("--doDivideVerticesByScalar"), false, string("doDivideVerticesByScalar"), false, no_argument); Option doReplaceScalarsByScalars(string("--doReplaceScalarsByScalars"), false, string("doReplaceScalarsByScalars"), false, no_argument); Option doDrawMeshScalars(string("--doDrawMeshScalars"), false, string("doDrawMeshScalars"), false, no_argument); Option doAverageSurfaces(string("--doAverageSurfaces"), false, string("doAverageSurfaces"), false, no_argument); int nonoptarg; //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// int main(int argc,char *argv[]) { Tracer tr("main"); OptionParser options(title, examples); try { // must include all wanted options here (the order determines how // the help message is printed) options.add(doUnCentreModel); options.add(inname); options.add(inname2); options.add(doSubtractConstantFromScalars); options.add(w_im); options.add(w_tan); options.add(w_norm); options.add(w_tri); options.add(doVertexScalarsToImageVolume); options.add(inmeshname); options.add(inmeshname2); options.add(useSc2); options.add(flirtmatname); options.add(doMeshReg); options.add(doFillMesh); options.add(toggle); options.add(label); options.add(doMeshToContours); options.add(outname); options.add(sampNN); options.add(doCombineMeshes); options.add(doUnCentreMesh); options.add(doLabelAndCombineSB); options.add(doAddMeshes); options.add(doSubtractMeshes); options.add(doAppendSBmask); options.add(doAppendIndexedSBmask); options.add(doApplyFlirtThenSBmask); options.add(inverse); options.add(doWarpMesh); options.add(shiftx); options.add(shifty); options.add(shiftz); options.add(doSurfDistMap); options.add(doSurfMeanAndStDev); options.add(doLQSurfReg); options.add(doAlterVertsBySBmask); options.add(doSphereToCart); options.add(doCartToSphere); options.add(doFindMidMidPoint); options.add(doWarpGrid); options.add(doSampleGrid); options.add(doSampleMesh); options.add(doRandMesh); options.add(doMeshToBvars); options.add(doAddModesUsingScalars); options.add(doRandMatrices); options.add(thresh); options.add(doWriteConditionalIntensity); options.add(doUgridToImage); options.add(doReplaceVertsWithCoef); options.add(doCoefModelToImage); options.add(myindex); options.add(doFieldModelToImage); options.add(doSubSampleGrid); options.add(doScalarsToVolume); options.add(doVectorsToVolume); options.add(doPointsToVolume); options.add(doVolumeToScalars); options.add(doVolumeToVectors); options.add(doFtoP); options.add(doGetMeans); options.add(doFlipMesh); options.add(doAppendConstScalar); options.add(doShiftGrid); options.add(doConcatIntensityGrid); options.add(doDeMeanIntensities); options.add(doConvert_ASCII_To_Binary); options.add(doConvert_Binary_To_ASCII); options.add(doSampleProfiles); options.add(doDeformSurface); options.add(doMeshReg_LeastSq); options.add(doGetMeshFromModel); options.add(doVertexLDA_LOO); options.add(doVertexLDA_save); options.add(doVertexLDA_loadAndApply); options.add(fullLDAoutput); options.add(doGetMeanScalar); options.add(doGetMaxScalar); options.add(doAddScalars); options.add(doDivideScalarsByScalar); options.add(doDisplayNumericFieldNames); options.add(doDisplayNumericField); options.add(dof); options.add(dof2); options.add(doAddVertices); options.add(doDivideVerticesByScalar); options.add(doReplaceScalarsByScalars); options.add(doDrawMeshScalars); options.add(doAverageSurfaces); options.add(verbose); options.add(help); nonoptarg = options.parse_command_line(argc, argv); // line below stops the program if the help was requested or // a compulsory option was not set if ( (help.value()) || (!options.check_compulsory_arguments(true)) ) { options.usage(); exit(EXIT_FAILURE); } // Call the local functions try{ if (doConvert_ASCII_To_Binary.value()) { // meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); fslvtkIO* m = new fslvtkIO();//inmeshname.value(),static_cast(0)); m->setSwitchRowsCols(toggle.value()); m->readPolyData(inmeshname.value()); cout<<"toggle "<setBinaryWrite(true); m->save(outname.value()); }else if (doConvert_Binary_To_ASCII.value()) { // meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); fslvtkIO* m = new fslvtkIO();//inmeshname.value(),static_cast(0)); m->setSwitchRowsCols(toggle.value()); m->readPolyData(inmeshname.value()); cout<<"toggle "<setBinaryWrite(false); m->save(outname.value()); }else if (doUnCentreModel.value()){ meshUtils* m = new meshUtils(); m->setSwitchRowsCols(toggle.value()); volume im; read_volume(im,inname.value());//load label image float tx = (im.xsize()-1)/2.0*abs(im.xdim()); float ty = (im.ysize()-1)/2.0*abs(im.ydim()); float tz = (im.zsize()-1)/2.0*abs(im.zdim()); cout<<"shift "<readPolyData(inmeshname.value()); m->shiftPoints(tx,ty,tz ); m->setBinaryWrite(true); m->save(outname.value()); }else if (doFillMesh.value()) { meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); volume im; read_volume(im,inname.value()); int bounds[6]={0,0,0,0,0,0}; first_mesh::getBounds(m->getPointsAsVector(),bounds,im.xdim(),im.ydim(),im.zdim()); volume mask=first_mesh::make_mask_from_mesh(im, m->getPointsAsVector(), first_newmat_vector::matrixToVector(m->getPolygons().t()), 1, bounds); save_volume(mask,outname.value()); delete m; }else if (doSurfDistMap.value()) { volume imlb; read_volume(imlb,inname.value());//load label image meshUtils* m = new meshUtils; cout<<"load mesh"<loadMesh(inmeshname.value()); cout<<"done load mesh"< vdist; cout<<"surfdist"<SurfDistToLabels(vdist,imlb,static_cast(label.value())); m->setScalars(vdist); m->save(outname.value()); delete m; }else if(doSurfMeanAndStDev.value()) { meshUtils* m = new meshUtils; Matrix MeanPoints; Matrix MeanScalars; Matrix StDevScalars; m->SurfScalarsMeanAndStdev(meshUtils::fileToVector(inmeshname.value()),MeanPoints, MeanScalars,StDevScalars); m->setScalars(MeanScalars); m->addFieldData(StDevScalars,"std_dev","float"); m->save(outname.value()); delete m; }else if(doMeshReg.value()) { meshUtils* m = new meshUtils; m->loadMesh(inmeshname.value()); Matrix fmat=meshUtils::readFlirtMat(flirtmatname.value()); if (inverse.value()) fmat=fmat.i(); m->meshReg(fmat); m->save(outname.value()); delete m; }else if (doShiftGrid.value()) { meshUtils* m = new meshUtils(inmeshname.value(),static_cast(1)); m->shiftPoints(shiftx.value(),shifty.value(),shiftz.value()); m->save(outname.value()); }else if (doLQSurfReg.value()) { meshUtils* m = new meshUtils; m->loadMesh(inmeshname.value()); meshUtils* mRef = new meshUtils; mRef->loadMesh(inmeshname2.value()); Matrix fmat; m->LQSurfaceReg(mRef->getPointsAsMatrix(),fmat, dof.value()); cout<<"final "<meshReg(fmat); m->save(outname.value()+".vtk"); delete m; delete mRef; }else if (doCombineMeshes.value()) { meshUtils m; m.combineMeshesWithVectorsAndScalars(meshUtils::fileToVector(inmeshname.value())); m.save(outname.value()); //delete m; }else if (doMeshToContours.value()) { meshUtils* m ; try { m=new meshUtils(inmeshname.value(),static_cast(0)); cout<<"load Mesh "< > contours; cout<<"minmaxY "<getMinY()<<" "<getMaxY()<getMinY()+res;y<=m->getMaxY();y+=res) { cout<<"slice "<sliceMesh(y)); cout<<"end slice "< >::iterator verts=contours.begin();verts!=contours.end();verts++) { float area=0; // for (vector::iterator i=verts->begin();i!=verts->end();i=i) // { for ( unsigned int j=0;jsize()-3;j+=3) { //cout<<*verts<<" "<<*(verts+1)<<" "<<*(verts+2)<<" "<<*(verts+3)<at(j)*verts->at(j+5) -verts->at(j+3)*verts->at(j+2); } area+=verts->at(verts->size()-3)*verts->at(2) - verts->at(verts->size()-1)*verts->at(0); area=abs(area)/2; // } volume+=area*res; cout<<"area "< >::iterator verts=contours.begin();verts!=contours.end();verts++) { for (vector::iterator i=verts->begin();i!=verts->end();i++) { fout<<*i<<" "; if (count==2) { fout< >::iterator verts=contours.begin();verts!=contours.end();verts++) { for (unsigned int i=0;isize()/3-1;i++) { fout<<"2 "<size()/3-1<<" "<size()/3; } // for (int i =0; iloadMesh(inmeshname.value()); meshUtils* mRef = new meshUtils; mRef->loadMesh(inmeshname2.value()); //replace the avgvertcies with the defored meshUtils* mnew = new meshUtils; mnew->loadMesh(inname.value()); Matrix Sc=m->appendSharedBoundaryMask(mRef->getPointsAsMatrix()); m->setPoints(mnew->getPointsAsMatrix()); m->setScalars(Sc); m->save(outname.value()); delete m; delete mRef; delete mnew; }else if (doAlterVertsBySBmask.value()) { meshUtils* m = new meshUtils; m->loadMesh(inmeshname.value()); meshUtils* mRef = new meshUtils; mRef->loadMesh(inmeshname2.value()); m->sampleSharedBoundaryByMask(mRef->getPointsAsMatrix()); m->save(outname.value()); delete m; }else if (doFindMidMidPoint.value()) { meshUtils* m = new meshUtils; m->loadMesh(inmeshname.value()); volume im; read_volume(im,inname.value()); float cx=0, cy=0, cz=0; m->findMidPointOfMidSlice(im,meshUtils::readFlirtMat(flirtmatname.value()),cx,cy,cz); cout<<"foudn verts"<(1)); volume4D warpField; read_volume4D(warpField,inname.value()); m->warpGridWithDefField(warpField, shiftx.value(),shifty.value(),shiftz.value()); m->save(outname.value()); }catch(exception& e){ cout<(0)); volume4D warpField; read_volume4D(warpField,inname.value()); m->warpGridWithDefField(warpField, shiftx.value(),shifty.value(),shiftz.value()); m->save(outname.value()); }catch(exception& e){ cout<(1)); // Matrix fmat=meshUtils::readFlirtMat(flirtmatname.value()); // m->meshReg(fmat.i()); volume image; vector vsamples; read_volume(image,inname.value()); m->sampleImageAtPoints(image, vsamples); delete m; meshUtils* mout = new meshUtils(inmeshname.value(),static_cast(1)); mout->setScalars(vsamples); mout->save(outname.value()); delete mout; }else if (doSampleMesh.value()) { cout<<"sample grid"<(0)); Matrix pts=m->getPointsAsMatrix(); volume image; vector vsamples; read_volume(image,inname.value()); m->sampleImageAtPoints(image, vsamples); m->setScalars(vsamples); m->save(outname.value()); delete m; }else if (doRandMesh.value()) { cout<<"sample grid"<(0)); Matrix sc=m->getScalars(); vector vsc; for (int i=0;ithresh.value()){ vsc.push_back(true); // vsc.push_back(false); }else vsc.push_back(false); } Mesh m1; m1.load(inmeshname.value()); Matrix points=m->getPointsAsMatrix(); int count=0; for (vector::iterator i = m1._points.begin(); i!=m1._points.end(); i++ , count++) { (*i)->_update_coord = Pt(points.element(count,0), points.element(count,1),points.element(count,2)); } m1.update(); meshUtils::generateRandomMeshUsingScalar(m1,outname.value(), vsc,40); delete m; }else if (doRandMatrices.value()) { meshUtils::generateRandom6DOFMatrices( outname.value(), 40); }else if (doUgridToImage.value()) { cout<<"convert grid to image"<(1)); volume image; read_volume(image,inname.value()); m->ugridToImage(image); save_volume(image,outname.value()); delete m; }else if (doReplaceVertsWithCoef.value()){//used to play around cout<<"replace verts"<(1)); // MJ: WHAT IS GOING ON HERE?? APPARENTLY NOTHING, BUT WHY? //getcoefrep disappear // FnirtFileReader fr(inname.value()); // cout<<"number of ceofficient fields "<GetCoef()->Nrows()<<" "<CoefSz()<GetCoef()) & *(fr.GetCoefRep().at(1)->GetCoef()) & *(fr.GetCoefRep().at(2)->GetCoef()); // cout<<"Coefs "<setPoints(*(fr.GetCoefRep().at(0)->GetCoef()) | *(fr.GetCoefRep().at(1)->GetCoef()) | *(fr.GetCoefRep().at(2)->GetCoef())); // m->save(outname.value()); }else if (doScalarsToVolume.value()){ meshUtils *m = new meshUtils(inmeshname.value(),static_cast(0)); Matrix Msc=m->getScalars(); volume Scvol(Msc.Nrows(),1,1); for (unsigned int i=0; i(Msc.Nrows());i++) Scvol.value(i,0,0)=Msc.element(i,0); save_volume(Scvol,outname.value()); }else if (doVertexScalarsToImageVolume.value()){ meshUtils *m = new meshUtils(inmeshname.value(),static_cast(0)); volume image; read_volume(image,inname.value()); image=0; // all vertices should be arranged along the x-axis int count=0; vector pts=m->getPointsAsVector(); Matrix sc=m->getScalars(); for (vector::iterator i=pts.begin();i!=pts.end();i=i+3,count++) { cout<<"verts "<<*i<<" "<<*(i+1)<<" "<<*(i+2)<(*i/image.xdim() + 0.5),static_cast(*(i+1)/image.ydim() + 0.5),static_cast(*(i+2)/image.zdim() + 0.5))=sc.element(count,0); } save_volume(image,outname.value()); }else if (doVectorsToVolume.value()){ meshUtils *m = new meshUtils(inmeshname.value(),static_cast(0)); Matrix Msc=m->getVectors(); volume4D Scvol(Msc.Nrows(),1,1,Msc.Ncols()); for (unsigned int i=0; i(Msc.Nrows());i++) { for (unsigned int j=0; j(Msc.Ncols());j++) { Scvol.value(i,0,0,j)=Msc.element(i,j); } } save_volume4D(Scvol,outname.value()); }else if (doVolumeToVectors.value()){ meshUtils *m = new meshUtils(inmeshname.value(),static_cast(0)); volume4D Scvol; read_volume4D(Scvol,inname.value()); // all vertices should be arranged along the x-axis Matrix Msc(Scvol.xsize(),Scvol.tsize()); for (int i=0; isetVectors(Msc); m->save(outname.value()); }else if (doPointsToVolume.value()){ meshUtils *m = new meshUtils(inmeshname.value(),static_cast(0)); Matrix Msc=m->getPointsAsMatrix(); volume Scvol(Msc.Nrows(),3,1); for (unsigned int i=0; i(Msc.Nrows());i++) { for (unsigned int j=0; j<3; j++) { Scvol.value(i,j,0)=Msc.element(i,j); } } save_volume(Scvol,outname.value()); }else if (doFtoP.value()){ meshUtils *m = new meshUtils(inmeshname.value(),static_cast(0)); Matrix Msc=m->getScalars(); m->readPolyData(inmeshname.value()); if (verbose.value()) { cout<<"Msc "<displayNumericFieldDataNames(); } Matrix fdofs=m->getField("FStatDOFs"); df1=MISCMATHS::round(fdofs(1,1)); df2=MISCMATHS::round(fdofs(2,1)); // override dofs with user-specified values (not generally needed) if (dof.set()) { df1=dof.value(); } if (dof2.set()) { df2=dof2.value(); } if (verbose.value()) { cout << "FStat DOFs = " << df1 << " and " << df2 << endl; } for (int i=0; i3) cout<<"F "<setScalars(Msc); m->save(outname.value()); }else if (doGetMeans.value()){ meshUtils *m = new meshUtils(inmeshname.value(),static_cast(0)); Matrix pts=m->getPointsAsMatrix(); Matrix Vecs=m->getVectors(); ColumnVector labels(pts.Nrows()); labels=0; meshUtils *mout = new meshUtils(); mout->setPoints(pts); mout->setPolygons(m->getPolygons()); Matrix dist=Vecs; for (int i =0;isetScalars(labels); mout->save(outname.value()+"_mean0.vtk"); labels=1; mout->setPoints(pts+Vecs); mout->setScalars(labels); mout->save(outname.value()+"_mean1.vtk"); }else if (doCoefModelToImage.value()){//used to play around int mode=myindex.value(); cout<<"replace verts"<(1)); Matrix CoefMean=m->getPointsAsMatrix(); cout<<"Smodes"<getField("Smodes"); cout<<"SCondEigs"<getField("SCondEigs").t(); ColumnVector ie = m->getField("ICondEigs").t(); Matrix IModes=m->getField("Imodes"); ColumnVector Vimean=m->getField("Imean"); cout<<"i mean "< imean; volume4D ivar; volume4D fmean; volume imtemp, imY,imZ; read_volume(imtemp,inname.value()); imY=imtemp; imZ=imtemp; int count=0; for (int k=0;k(1)); mout->warpGridWithDefField(fmean,0,0,0); // mout->setPoints(CoefMean); mout->setScalars(Vimean); //cout<<"vimena "<save("ugrid_int_mean.vtk"); vector vars; for (unsigned int i=0;i<=static_cast(mode);i++) vars.push_back(0); volume4D Vmode; volume4D VmodeVar; /* Matrix Mivar=m->getField("ICondMat"); ColumnVector ieigs=m->getField("ICondEigs").t(); ColumnVector Var(Mivar.Nrows()); cout<SetCoef(NewCoefX); // fr.GetCoefRep().at(1)->SetCoef(NewCoefY); // fr.GetCoefRep().at(2)->SetCoef(NewCoefZ); volume4D field=fr.FieldAsNewimageVolume4D(); meshUtils* mout2= new meshUtils(inmeshname2.value(),static_cast(1)); mout2->warpGridWithDefField(field,0,0,0); mout2->setScalars(inew); cout<<"inew "< image; read_volume(image,inname2.value()); mout2->ugridToImage(image); Vmode.addvolume(image); /* mout2->setScalars(Var*sc); mout2->ugridToImage(image); VmodeVar.addvolume(image); */ //mout2->save("ugrid_int_min3mde0.vtk"); delete mout2; cout<<"end of loop"<GetCoef()->Nrows()<<" "<CoefSz()<GetCoef()) & *(fr.GetCoefRep().at(1)->GetCoef()) & *(fr.GetCoefRep().at(2)->GetCoef()); // cout<<"Coefs "<setPoints(*(fr.GetCoefRep().at(0)->GetCoef()) | *(fr.GetCoefRep().at(1)->GetCoef()) | *(fr.GetCoefRep().at(2)->GetCoef())); // m->save(outname.value()); }else if (doFieldModelToImage.value()){//used to play around int mode=myindex.value(); cout<<"replace verts"<(1)); Matrix CoefMean=m->getPointsAsMatrix(); cout<<"Smodes"<getField("Smodes"); cout<<"SCondEigs"<getField("SCondEigs").t(); ColumnVector ie = m->getField("ICondEigs").t(); Matrix IModes=m->getField("Imodes"); ColumnVector Vimean=m->getField("Imean"); cout<<"i mean "< vars; for (unsigned int i=0;i<=static_cast(mode);i++) vars.push_back(0); volume4D Vmode; volume4D VmodeVar; Matrix Mivar=m->getField("ICondMat"); ColumnVector ieigs=m->getField("ICondEigs").t(); ColumnVector Var(Mivar.Nrows()); cout<(Var.Nrows());i++) { float vartemp=0; for (unsigned int j=0; j(Mivar.Ncols());j++) vartemp+=Mivar.element(i,j)*Mivar.element(i,j)*ieigs.element(j); Var.element(i)=vartemp; } meshUtils* mout2= new meshUtils(inmeshname.value(),static_cast(1)); for (float i=-3;i<=3;i+=0.5) // for (float i=-25;i<=59;i+=10) { cout<<"Iter i "<getPointsAsMatrix(); cout<(1)); mout2->setPoints(CoefMean); mout2->setScalars(inew); cout<<"points set "< image; read_volume(image,inname.value()); mout2->ugridToImage(image); Vmode.addvolume(image); cout<<"now do var "<setScalars(Var*sc); cout<<"now do var2 "<ugridToImage(image); VmodeVar.addvolume(image); cout<<"now do var added "<save("ugrid_int_min3mde0.vtk"); // delete mout2; cout<<"end of loop"<GetCoef()->Nrows()<<" "<CoefSz()<GetCoef()) & *(fr.GetCoefRep().at(1)->GetCoef()) & *(fr.GetCoefRep().at(2)->GetCoef()); // cout<<"Coefs "<setPoints(*(fr.GetCoefRep().at(0)->GetCoef()) | *(fr.GetCoefRep().at(1)->GetCoef()) | *(fr.GetCoefRep().at(2)->GetCoef())); // m->save(outname.value()); }else if (doSubSampleGrid.value()){ cout<<"sub sample grid"<(1)); cout<<"get Points"<getPointsAsMatrix(); cout<<"opoints got "< image; vector vsamples; read_volume(image,inname.value()); m->sampleImageAtPoints(image, vsamples); cout<<"crreate mask"< vmask; int N=0; for (vector::iterator i=vsamples.begin();i!=vsamples.end();i++) { if ((*i) >0 ) { vmask.push_back(true); N++; cout<<"true "<<*i<setDataType(static_cast(1)); cout<<"Pointss"<setPoints(meshUtils::subSampleMatrix(m->getPointsAsMatrix(),vmask)); cout<<"Smodes"<addFieldData(meshUtils::subSample_Nby1_3D_Matrix(m->getField("Smodes"),vmask),"Smodes","float"); // mout->addFieldData(meshUtils::subSampleMatrix(m->getField("Imodes"),vmask),"Imodes","float"); /* mout->addFieldData(meshUtils::subSampleMatrix(m->getField("Imean"),vmask),"Imean","float"); mout->addFieldData(meshUtils::subSampleMatrix(m->getField("ICondMat"),vmask),"ICondMat","float"); mout->addFieldData(m->getField("Errs"),"Errs","float"); mout->addFieldData(m->getField("Labels"),"Labels","float"); mout->addFieldData(m->getField("NumberOfSubjects"),"NumberOfSubjects","float"); mout->addFieldData(m->getField("SCondEigs"),"SCondEigs","float"); mout->addFieldData(m->getField("ICondEigs"),"ICondEigs","float"); */ cout<<"saving"<save(outname.value()); delete m; delete mout; }else if (doFlipMesh.value()){ volume im; read_volume(im,inname.value()); float tx=im.xsize()*im.xdim(); meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); m->shiftPoints(-tx,0,0); m->scalePoints(-1,1,1); // m->shiftPoints(tx,ty,tz); m->save(outname.value()); }else if (doAppendConstScalar.value()){ meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); ColumnVector Sc(m->getPointsAsMatrix().Nrows()); Sc=label.value(); m->setScalars(Sc); m->save(outname.value()); }else if (doConcatIntensityGrid.value()){ meshUtils* m = new meshUtils(inmeshname.value(),static_cast(1)); meshUtils* m2 = new meshUtils(inmeshname2.value(),static_cast(1)); m->setPoints(m->getPointsAsMatrix() & m2->getPointsAsMatrix() ); m->setScalars(m->getScalars() & m2->getScalars() ); m->save(outname.value()); delete m; delete m2; }else if (doDeMeanIntensities.value()){ meshUtils* m = new meshUtils(inmeshname.value(),static_cast(1)); Matrix intensity=m->getScalars(); cout<(intensity.Nrows())<(intensity.Nrows());i++) avg+=intensity.element(i,0);//sum values avg/=intensity.Nrows();//normalize for (unsigned int i=0; i(intensity.Nrows());i++) intensity.element(i,0)-=avg;//demean m->setScalars(intensity); m->save(outname.value()); delete m; }else if (doDeformSurface.value()) { meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); volume image; read_volume(image, inname.value()); m->deformSurface(image, dof.value(), w_im.value(), w_tan.value(), w_tri.value(), w_norm.value(),thresh.value(),100,toggle.value(),inmeshname.value()); m->save(outname.value()); delete m; }else if (doMeshReg_LeastSq.value()){ meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); meshUtils* mtarg = new meshUtils(inmeshname2.value(),static_cast(0)); Matrix Points_src=m->getPointsAsMatrix(); Matrix fmat=mtarg->reg_leastsq(Points_src, dof.value()); delete mtarg; m->meshReg(fmat); m->save((outname.value()+".vtk").c_str()); ofstream fmatout; fmatout.open((outname.value()+".mat").c_str()); for (int i=0;i<4;i++) { for (int j=0;j<4;j++) fmatout<(0)); meshUtils* mout = new meshUtils(); mout->setPoints(m->getPointsAsMatrix()); mout->setPolygons(m->getPolygons()); mout->save(outname.value()); delete m; delete mout; }else if (doVertexLDA_LOO.value()) { //load target ifstream ftarg; ftarg.open(inname.value().c_str()); int temp; unsigned int count=0; while (ftarg>>temp) count++; ftarg.close(); ColumnVector target(count); ftarg.open(inname.value().c_str()); count=0; while (ftarg>>temp) { target.element(count)=temp; cout<<"target "<(0)); //creates mean target mesh from data // Matrix alignedPoints=mbase->alignSurfaces(inmeshname2.value(),dof.value(),"nosave"); // for (int i=0;i<5;i++) // { // // if (i>0) // alignedPoints=mbase->alignSurfaces(inmeshname2.value(),dof.value(),"nosave"); // // // Matrix meanPts(mbase->getPointsAsMatrix().Nrows(),3); // for (unsigned int i=1;i(alignedPoints.Nrows());i+=3) // { // MVdisc* vertDisc = new MVdisc(); // Matrix m=(vertDisc->getGroupMeans(alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()),target)).Column(1); // //cout<<"i "<getGroupMeans(alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()),target)).Column(1).t(); // delete vertDisc; // } // mbase->setPoints(meanPts); // // } Matrix alignedPoints=mbase->alignSurfaces(inmeshname2.value(),dof.value(),outname.value()+"_aligned.vtk"); //do for each vertex vector v_accuracy; unsigned int Npoints = static_cast(alignedPoints.Nrows())/3; unsigned int Nc=alignedPoints.Ncols()+5; unsigned int Nc0=alignedPoints.Ncols(); Matrix v_results(Npoints,Nc); ColumnVector LDAres; for (unsigned int i=1;iestimateLDAParams(alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()),target); Matrix discData=alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()); if (toggle.value()) //use neighbouring vertices as well { vector< vector > neigh = first_mesh::findNeighbours(mbase->getPolygonsAsVectorOfVectors(),alignedPoints.Nrows()/3); for (unsigned int j=0; jrun_LOO_LDA(discData,target); float accuracy = LDAres(discData.Ncols()+1); cout<<"accuracy "<setScalars(v_results.SubMatrix(1,Npoints,nn,nn)); if (nn==Nc0+1) { outputname = basename + "_accuracy.vtk" ; } else if (nn==tpidx) { outputname = basename + "_TP.vtk"; } else if (nn==tnidx) { outputname = basename + "_TN.vtk"; } else if (nn==fpidx) { outputname = basename + "_FP.vtk"; } else if (nn==fnidx) { outputname = basename + "_FN.vtk"; } else { outputname = basename + "_trueclass_" + num2str(nn) + ".vtk"; } mbase->save(outputname); } // Calculate sensitivity and specificity: sens=TP/(TP+FN), spec=TN/(TN+FP) Matrix sens(Npoints,1), spec(Npoints,1); for (unsigned int mm=1; mm<=Npoints; mm++) { // sens = TP/(TP+FN) sens(mm,1)=v_results(mm,tpidx)/(v_results(mm,tpidx)+v_results(mm,fnidx)); // spec = TN/(TN+FP) spec(mm,1)=v_results(mm,tnidx)/(v_results(mm,tnidx)+v_results(mm,fpidx)); } mbase->setScalars(sens); mbase->save(basename + "_sens.vtk"); mbase->setScalars(spec); mbase->save(basename + "_spec.vtk"); } else { mbase->setScalars(v_accuracy); mbase->save(outname.value()); } } else if (doVertexLDA_save.value()) { //load target ifstream ftarg; ftarg.open(inname.value().c_str()); int temp; unsigned int count=0; while (ftarg>>temp) count++; ftarg.close(); ColumnVector target(count); ftarg.open(inname.value().c_str()); count=0; while (ftarg>>temp) { target.element(count)=temp; cout<<"target "<(0)); // Matrix alignedPoints=mbase->alignSurfaces(inmeshname2.value(),dof.value(),"nosave"); // for (int i=0;i<5;i++) // { // // if (i>0) // alignedPoints=mbase->alignSurfaces(inmeshname2.value(),dof.value(),"nosave"); // // // Matrix meanPts(mbase->getPointsAsMatrix().Nrows(),3); // for (unsigned int i=1;i(alignedPoints.Nrows());i+=3) // { // MVdisc* vertDisc = new MVdisc(); // Matrix m=(vertDisc->getGroupMeans(alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()),target)).Column(1); // //cout<<"i "<getGroupMeans(alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()),target)).Column(1).t(); // delete vertDisc; // } // mbase->setPoints(meanPts); // // } // cout<<"done looping align"<alignSurfaces(inmeshname2.value(),dof.value(),outname.value()+"_aligned.vtk"); //do for each vertex // vector v_accuracy; MVdisc* vertDisc = new MVdisc(); for (unsigned int i=1;i(alignedPoints.Nrows());i+=3) { //Matrix test=alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()); // vertDisc->estimateLDAParams(alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()),target); Matrix discData=alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()); if (toggle.value()) //use neighbouring vertices as well { vector< vector > neigh = first_mesh::findNeighbours(mbase->getPolygonsAsVectorOfVectors(),alignedPoints.Nrows()/3); for (unsigned int j=0; jestimateLDAParams(discData,target); else vertDisc->estimateAndAppendLDAParams(discData,target); // cout<<"accuracy "<setScalars(v_accuracy); vertDisc->saveLDAParams(outname.value(),mbase->getPolygons()); delete vertDisc; delete mbase; }else if (doVertexLDA_loadAndApply.value()) { meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); Matrix Mean1_all=m->getPointsAsMatrix(); Matrix Mean2_all=m->getField("mean2"); Matrix Cov_vecs_all=m->getField("Covariance_EigVecs"); Matrix Cov_vals_all=m->getField("Covariance_EigVals"); vector nsub_all=first_newmat_vector::vectorToVector(m->getField("number_of_subjects")); Matrix alignedPoints=m->alignSurfaces(inmeshname2.value(),dof.value(),outname.value()+"_aligned.vtk"); //unsigned int subject=1; vector v_class; fslvtkIO* fout = new fslvtkIO(); for (unsigned int subject=1; subject<=(unsigned)alignedPoints.Ncols() ;subject++) { //do for each vertex int p_count=1; for (unsigned int i=1;i(alignedPoints.Nrows());i+=3,p_count++) { MVdisc* vertDisc = new MVdisc(); //Matrix test=alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()); // vertDisc->estimateLDAParams(alignedPoints.SubMatrix(i,i+2,1,alignedPoints.Ncols()),target); //cout<<"i apply "<set_LDA_Params( ( Mean1_all.SubMatrix(p_count,p_count,1,3).t() | Mean2_all.SubMatrix(p_count,p_count,1,3).t() ) ,\ Cov_vecs_all.SubMatrix(i,i+2,1,3), \ first_newmat_vector::vectorToVector( Cov_vals_all.SubMatrix(i,i+2,1,1) ), \ nsub_all ); ColumnVector discData=alignedPoints.SubMatrix(i,i+2,subject,subject); v_class.push_back( static_cast(vertDisc->applyLDA(discData, 0.0 )) ); delete vertDisc; } if (subject==1) { fout->setPoints(alignedPoints.Column(subject)); fout->setPolygons(m->getPolygons()); }else { fout->appendPointsAndPolygons( first_newmat_vector::wrapMatrix(alignedPoints.Column(subject)), m->getPolygons()); } } fout->setScalars(v_class); fout->save(outname.value()); delete fout; delete m; // mbase->setScalars(v_accuracy); //vertDisc->saveLDAParams(outname.value()); }else if (doGetMaxScalar.value()) { meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); cout<maxScalar()<(0)); cout<meanScalar()< image; read_volume(image,inname.value()); meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); m->sampleMeshProfilesFromImage(image,0.5,dof.value()); vector ipp; ipp.push_back(dof.value()); m->addFieldData(ipp,"ipp","int"); vector samp_inter; samp_inter.push_back(0.5); m->addFieldData(samp_inter,"samplingInterval","float"); m->save(outname.value()); }else if (doDisplayNumericField.value()) { meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); m->displayNumericField(inname.value()); delete m; }else if (doDisplayNumericFieldNames.value()) { meshUtils* m = new meshUtils(inmeshname.value(),static_cast(0)); m->displayNumericFieldDataNames(); delete m; }else if (doAverageSurfaces.value()) { ifstream fin(inmeshname.value().c_str()); string filename; Matrix Points_all; int count=0; while (fin>>filename) { meshUtils* m = new meshUtils(filename,static_cast(0)); if (count==0) Points_all=m->getPointsAsMatrix(); else Points_all+=m->getPointsAsMatrix(); delete m; count++; } Points_all/=count; meshUtils* m = new meshUtils(filename,static_cast(0)); m->setPoints(Points_all); m->save(outname.value()); delete m; }else if(doAddScalars.value()) { cout<<"sample grid"<(0)); meshUtils* m2 = new meshUtils(inmeshname2.value(),static_cast(0)); m1->setScalars(m1->getScalars()+m2->getScalars()); m1->save(outname.value()); delete m1; delete m2; }else if(doDivideScalarsByScalar.value()) { cout<<"sample grid"<(0)); m1->setScalars(m1->getScalars()/thresh.value()); m1->save(outname.value()); delete m1; }else if(doReplaceScalarsByScalars.value()) { cout<<"sample grid"<(0)); meshUtils* m2 = new meshUtils(inmeshname2.value(),static_cast(0)); m1->setScalars(m2->getScalars()); m1->save(outname.value()); delete m1; delete m2; }else if(doAddVertices.value()) { cout<<"sample grid"<(0)); meshUtils* m2 = new meshUtils(inmeshname2.value(),static_cast(0)); m1->setPoints(m1->getPointsAsMatrix()+m2->getPointsAsMatrix()); m1->save(outname.value()); delete m1; delete m2; }else if(doDivideVerticesByScalar.value()) { meshUtils* m1 = new meshUtils(inmeshname.value(),static_cast(0)); m1->setPoints(m1->getPointsAsMatrix()/thresh.value()); m1->save(outname.value()); delete m1; }else if (doSubtractConstantFromScalars.value()){ meshUtils* m1 = new meshUtils(inmeshname.value(),static_cast(0)); m1->setScalars(m1->getScalars()-thresh.value()); m1->save(outname.value()); delete m1; }else if (doAddMeshes.value()) { meshUtils* m1 = new meshUtils(inmeshname.value(),static_cast(0)); meshUtils* m2 = new meshUtils(inmeshname2.value(),static_cast(0)); m1->setPoints(m1->getPointsAsMatrix() + m2->getPointsAsMatrix()); m1->save(outname.value()); delete m1; delete m2; }else if (doSubtractMeshes.value()) { meshUtils* m1 = new meshUtils(inmeshname.value(),static_cast(0)); meshUtils* m2 = new meshUtils(inmeshname2.value(),static_cast(0)); m1->setPoints(m1->getPointsAsMatrix() - m2->getPointsAsMatrix()); m1->save(outname.value()); delete m1; delete m2; }else if (doMeshToBvars.value()){ shapeModel* model ; model=shapeModel::loadAndCreateShapeModel(inname.value(),verbose.value()); meshUtils* m1 = new meshUtils(inmeshname.value(),static_cast(0)); Matrix fmatM(4,4); ifstream ifmat; ifmat.open(flirtmatname.value().c_str()); for (int i=0; i<4 ; i++) { for (int j=0; j<4 ; j++) { float ftemp; ifmat>>ftemp; if (verbose.value()) cout< > fmatv2; fmatv2=first_newmat_vector::matrixToVector(fmatM.t()); model->registerModel(fmatv2); vector mean_mesh=model->smean; vector mesh=m1->getPointsAsVector(); vector sqrtseigs= model->sqrtseigs; vector< vector > modes=model->smodes; //subtract off mean vector::iterator i_mean=mean_mesh.begin(); for (vector::iterator i_m1=mesh.begin();i_m1!=mesh.end();i_m1++,i_mean++) { cout<<*i_m1<<" "<<*i_mean< bvars; vector::iterator i_eig=sqrtseigs.begin(); for (vector< vector >::iterator n_mode=modes.begin();n_mode!=modes.end();i_eig++,n_mode++) { cout<<"eigs "<<*i_eig<::iterator i_m1=mesh.begin(); double dot=0; for (vector::iterator i=n_mode->begin();i!=n_mode->end();i++,i_m1++) { // cout<<"dot "<::iterator i=n_mode->begin();i!=n_mode->end();i++,i_m1++) { //cout<<"dot "<getOrigSpaceBvars(bvars); ofstream fout; // string name=outname+".bvars"; fout.open(outname.value().c_str()); fout.precision(10); fout<<"this is a bvars file"<::iterator i=bvars.begin();i!=bvars.end();i++){ fout.write(reinterpret_cast(&(*i)),sizeof(*i)); #ifdef PPC64 if ((n++ % 50) == 0) fout.flush(); #endif } //-----------------read flirt matrix-------------------//////// vector< vector > fmatv; ifstream flirt_in(flirtmatname.value().c_str()); for (int i=0;i<4;i++) { vector row; for (int j=0;j<4;j++) { float temp; flirt_in>>temp; row.push_back(temp); } fmatv.push_back(row); } for (vector< vector >::const_iterator i=fmatv.begin();i!=fmatv.end();i++) for (vector::const_iterator i2=i->begin();i2!=i->end();i2++) fout.write(reinterpret_cast(&(*i2)),sizeof(*i2)); fout<(0)); volume image; volume count; read_volume(image,inname.value()); image=0; copyconvert(image,count); for (int i=0;igetNumberOfPolygons();i++) { m->drawTriangleScalars(image,count,i); } for (int i=0;i0) image.value(i,j,k)/=count.value(i,j,k); save_volume(image,outname.value()); save_volume(count,"count"); delete m; }else{ //load target ifstream ftarg; ftarg.open(inname.value().c_str()); int temp; unsigned int count=0; while (ftarg>>temp) count++; ftarg.close(); ColumnVector target(count); ftarg.open(inname.value().c_str()); count=0; while (ftarg>>temp) { target.element(count)=temp; cout<<"target "<(0)); Matrix Sc=m->getScalars(); unsigned int npts=642; Matrix Sc_rate=Sc.SubMatrix(1,npts,1,1); Sc_rate=0; for (unsigned int j=0;j<(Sc.Nrows()/npts) ; j++) { float frac=0; for (unsigned int i=0; igetPointsAsMatrix().SubMatrix(1,npts,1,3); m->setPoints(tempM); m->setScalars(Sc_rate/(Sc.Nrows()/npts)); m->save(outname.value()); delete m; } }catch(exception& e){ cout<