/* b0sim.cc Mark Jenkinson, FMRIB Image Analysis Group Copyright (C) 2003 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. */ // Simulator for static B0 - made specifically for HBM2004 #define _GNU_SOURCE 1 #define POSIX_SOURCE 1 #include "newimage/newimageall.h" #include "miscmaths/miscmaths.h" #include "utils/options.h" using namespace MISCMATHS; using namespace NEWIMAGE; using namespace Utilities; // The two strings below specify the title and example usage that is // printed out as the help or usage message string title="b0sim \nCopyright(c) 2003, University of Oxford (Mark Jenkinson)"; string examples="b0sim [options] -i -b -o "; 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 debug(string("-d,--debug"), false, string("turn debugging outputs (files) on"), false, no_argument); Option zerogradients(string("--zerogradients"), false, string("use mean b0 in each voxel but have zero b0 gradients"), false, no_argument); Option inname(string("-i,--in"), string(""), string("input filename for object model"), true, requires_argument); Option b0name(string("-b,--b0"), string(""), string("filename for object b0 (in Tesla)"), false, requires_argument); Option outname(string("-o,--out"), string(""), string("filename for output image (complex)"), true, requires_argument); int nonoptarg; //////////////////////////////////////////////////////////////////////////// // Local functions int calc_gradients(const volume& im, volume& gx, volume& gy, volume& gz) { // Input image, im, must be in uT : output gradients in uT/mm // NB: sets all gradients at the edge of the volume to zero for // simplicity, as normally just need to know inside the brain // which doesn't extend this far! gx = im*0; gy = gx; gz = gx; for (int z=1; z rho, b0, gx, gy, gz; read_volume(rho,inname.value()); int inx=rho.xsize(); int iny=rho.ysize(); int inz=rho.zsize(); double lx=rho.xdim(), ly=rho.ydim(), lz=rho.zdim(); // input voxel dims in mm double x0=MISCMATHS::round(inx/2)*lx; // centre of input image in mm double y0=MISCMATHS::round(iny/2)*ly; double z0=MISCMATHS::round(inz/2)*lz; volume xpos(inx,iny,1), ypos(inx,iny,1); // both in mm xpos = rho*0.0; ypos = xpos; for (int x=0; x dummy(nx,ny,nz); dummy.setdims(dx,dy,dz); dummy = 0; complexvolume finalimage(dummy,dummy); // calculate signal s(t) Matrix sig(2,nx*ny); double twopi = 2.0*M_PI; for (int oz=0; oz=inz) zend=inz-1; if ( (zstart>=inz) || (zend<0) ) { zstart=1; zend=0; // do not want the following loop to run! } else if (verbose.value()) { cout << "Output slice " << oz << " ; input slice range : "; } for (int z=zstart; z<=zend; z++) { // slice number of input if (verbose.value()) { if (z==zend) cout << z << endl; else { cout << z << " , "; cout.flush(); } } double zpos=lz*(z+1)-z0; // NB z+1 for MATLAB equivalence for (int m=1; m<=nx*ny; m++) { // time point index double gbart = gammabar*t(m); for (int y=0; y0) { double kxx=kx(m) + gbart*gx(x,y,z); double kyy=ky(m) + gbart*gy(x,y,z); double kzz=gbart*gz(x,y,z); double phi = twopi*(gbart*b0(x,y,z) + kx(m)*xpos(x,y,0) + ky(m)*ypos(x,y,0)); double term = rho(x,y,z)*MISCMATHS::Sinc(lx*kxx)* MISCMATHS::Sinc(ly*kyy)*MISCMATHS::Sinc(lz*kzz); sig(1,m) += cos(phi)*term; sig(2,m) += sin(phi)*term; } } } } } sig *= (lx*ly*lz); if (debug.value()) write_ascii_matrix(sig,fname+"debug_signal"); // reconstruct slice volume dummy(nx,ny,1); dummy = 0.0; complexvolume kslice(dummy,dummy); for (int n=1; n<=(ny/2); n++) { int tidx = (n-1)*2*nx + 1; for (int m=1; m<=nx; m++) { kslice.re(m-1,2*n-2,0) = (double) sig(1,tidx + m - 1); kslice.im(m-1,2*n-2,0) = (double) sig(2,tidx + m - 1); kslice.re(m-1,2*n-1,0) = (double) sig(1,tidx+2*nx-m); kslice.im(m-1,2*n-1,0) = (double) sig(2,tidx+2*nx-m); } } if (debug.value()) save_complexvolume(kslice,fname+"debug_kspace"); fft2(kslice); fftshift(kslice); // copy transformed slice into final image for (int oy=0; oy