/* CCOPYRIGHT */ /* 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. */ #ifdef MEX #include "mex.h" #endif #include #include #include #include #include #include #include "splines.h" /* This is used to ensure that memory allocated C-style is also freed C-style. This is intended to use when this module is called from C++. */ void please_free(void *ptr) { if (ptr) {my_free(ptr);} } /* Calculates a 2- or 3D cubic B-spline given an array of 1D splines (obtained by get_1D_spline). */ int spline_kron(/* Input */ int ndim, /* Dimensionality of spline. */ int dim[3], /* Size of spline in the different directions. */ double *sp1d[3], /* Set of 1D splines. */ /* Output */ double *spline) /* nD (n>0 & n<4) spline. */ { int i=0, j=0, k=0; int ldim[3]; int n=1; double tmp = 1.0; double *lsp1d[3]; for (i=0; i 1) { zoom_field_by2(3,tksp,idim,dim,tmpc1,&tmpc2); tksp[dim] /= 2; sf /= 2; if (tmpc1 != oc) { my_free(tmpc1); } tmpc1 = tmpc2; } if (sf != 1) { printf("\nzoom_field: zooming must be by power of 2."); return(-1); } } for (i=0, sz=1; i<3; i++) {sz *= no_of_knots(nksp[i],idim[i]);} memcpy(nc,tmpc2,sz*sizeof(double)); my_free(tmpc2); return(1); } /* Calculates the new set of spline-coefficients resulting when the knot-spacing of an existing field is cut by half in one direction. */ int zoom_field_by2(/* Input */ int ndim, int ksp[3], int idim[3], int zdim, double *oc, /* Output */ double **nc) { int i=0, j=0, k=0; int nsz=0; int ocdim[3], ncdim[3]; double a0=1.0/8.0, a4=a0; double a1=1.0/2.0, a3=a1; double a2=3.0/4.0; for (i=ndim; i<3; i++) {ocdim[i] = 1; idim[i] = 1; ksp[i] = 1;} for (i=0, nsz=1; i<3; i++) { ocdim[i] = no_of_knots(ksp[i],idim[i]); ncdim[i] = (i==zdim) ? no_of_knots(ksp[i]/2,idim[i]) : ocdim[i]; nsz *= ncdim[i]; } *nc = (double *) my_calloc(nsz,sizeof(double)); if (zdim==0) { for (k=0; k fsz) {(*fe) = fsz;} return(1); } int no_of_knots(int ksp, int msz) { if (msz == 1) return(1); /* Collapsed dimension. */ else return(((int) ceil((((double) msz) + 1.0) / ((double) ksp))) + 2); } int make_A(/* Input. */ int ndim, int kdim[3], int sdim[3], double *spl, int idim[3], double *ima, /* Output. */ int *irp, int *jcp, double *sp) { int cntr = 0; int i=0; int ci=0; int kk=0, kj=0, ki=0; int ik=0, ij=0, ii=0; int sk=0, sj=0, si=0; int iks=0, ijs=0, iis=0; int ike=0, ije=0, iie=0; int sks=0, sjs=0, sis=0; for (i=ndim; i<3; i++) {kdim[i]=1; sdim[i]=1; idim[i]=1;} for (kk=0,ci=0; kkA*b, 1->A'*A*b */ /* Output. */ int *sz, /* Length of output vector */ double **ovec) /* Output vector */ { int x=0, y=0, z=0; int xs=0, ys=0, zs=0; int xoff=0, yoff=0, zoff=0; int c=0, i=0; int offs=0; int ksz = 1; /* Total size of spline kernel. */ int eidim[3]; /* Size of full field. */ int eisz = 1; /* Total size of field. */ int csz = 1; /* Total size of coefficient matrix. */ int *indx = NULL; /* Indicies into field for first spline. */ double *Ab = NULL; double *AtAb = NULL; for (i=0, eisz=1, ksz=1; i2 ? 4*ksp[2]-1 : 1); z++) { for (y=0; y<(ndim>1 ? 4*ksp[1]-1 : 1); y++) { for (x=0; x<4*ksp[0]-1; x++, i++) { indx[i] = index(x,y,z,eidim); } } } /* Then calculate the index offset as we go one step in the x-, y- and z-direction respectively on the coefficient space. */ xs = ksp[0]; ys = ksp[1]*eidim[0]; zs = ksp[2]*eidim[0]*eidim[1]; /* Then build A*b as weighted sum of the columns of A. */ for (z=0, zoff=0, c=0; z a[n-1]) {return(0.0);} while (jup-jlo > 1) { j = (jlo+jup) >> 1; if (key >= a[j]) {jlo = j;} else {jup = j;} } if (a[jlo] == key) {return(val[jlo]);} else return(0.0); } int make_AtB(/* Input. */ int ndim, /* Actual dimensionality of problem (1, 2 or 3). */ int cdim[3], /* # of knots in the three dimensions. */ int sdim[3], /* Size of spline kernel in the three dimensions. */ double *splB, /* Spline kernel for B. */ double *splA, /* Spline kernel for A. */ int idim[3], /* Size of image matrix. */ double *imaB, /* Image for B. */ double *imaA, /* Image for A. */ /* Output. */ int *irp, /* Array of row-indicies. */ int *jcp, /* Array of pointers into column-starts in irp. */ double *sp) /* Array of non-zero values in sparse matric. */ { int i = 0; int ci = 0; /* Column index for AtB. */ int ri = 0; /* Row index for AtB. */ int cntr = 0; int kns=0, jns=0, ins=0; /* Start of neighbours in k, j and i directions. */ int kne=0, jne=0, ine=0; /* End of neighbours in k, j and i directions. */ int s1k=0, s1j=0, s1i=0; /* index for "first" spline */ int s2k=0, s2j=0, s2i=0; /* index for "second" spline */ int is[3]={0,0,0}; /* Start indicies in image for first spline. */ int ie[3]={0,0,0}; /* End indicies in image for first spline. */ int ss[3]={0,0,0}; /* Offset for first spline. */ double *s_by_i = NULL; /* Spline multiplied by appurtenant image intensities. */ double tmp = 0.0; for (i=ndim; i<3; i++) {cdim[i]=1; sdim[i]=1; idim[i]=1;} s_by_i = (double *) my_calloc(sdim[0]*sdim[1]*sdim[2],sizeof(double)); /* First three loops over splines determine the column of AtB. */ ci = 0; cntr = 0; for (s1k=0; s1kis1[0]) {ss1i = ss1[0] + (is2i-is1[0]);} else if (is1[0]>is2i) {ss1i = ss1[0]; ss2i += (is1[0]-is2i); is2i = is1[0];} else {ss1i = ss1[0];} ie2i = MIN(ie2i,ie1[0]); if (is2j>is1[1]) {ss1j = ss1[1] + (is2j-is1[1]);} else if (is1[1]>is2j) {ss1j = ss1[1]; ss2j += (is1[1]-is2j); is2j = is1[1];} else {ss1j = ss1[1];} ie2j = MIN(ie2j,ie1[1]); if (is2k>is1[2]) {ss1k = ss1[2] + (is2k-is1[2]);} else if (is1[2]>is2k) {ss1k = ss1[2]; ss2k += (is1[2]-is2k); is2k = is1[2];} else {ss1k = ss1[2];} ie2k = MIN(ie2k,ie1[2]); dp = 0.0; for (i2k=is2k,s2k=ss2k,s1k=ss1k; i2k csz) {*ne = csz;} return(*ns); /* As an extra courtesy. */ } int get_A_nzmax(/* Input. */ int ndim, int kdim[3], int sdim[3], int idim[3]) { int nzmax = 0; int i=0; int kk=0, kj=0, ki=0; int iks=0, ijs=0, iis=0; int ike=0, ije=0, iie=0; int sks=0, sjs=0, sis=0; for (i=ndim; i<3; i++) {kdim[i]=1; sdim[i]=1; idim[i]=1;} for (kk=0; kk nzmax) { nzmax = ((int) 1.1 * nzmax); /* Increase memory by 10% */ ir_out = (int *) my_realloc(ir_out,nzmax*sizeof(int)); s_out = (double *) my_realloc(s_out,nzmax*sizeof(double)); printf("\nWarning, non-optimal nzmax passed to AtranspA."); } /* Put values in ir_out and s_out. */ memcpy(&(ir_out[cnt]),ir_tmp,nc*sizeof(int)); for (i=cnt; i<(cnt+nc); i++) { ndx = ir_out[i]; s_out[i] = full_s[ndx]; full_s[ndx] = 0.0; full_ir[ndx] = 0; } cnt += nc; } jc_out[ci] = cnt; *ir_out_orig = ir_out; *s_out_orig = s_out; my_free(full_s); my_free(full_ir); my_free(ir_tmp); return(nzmax); } int cmpf(const void *el1, const void *el2) { if (*((int *)el1) < *((int *)el2)) return(-1); else if (*((int *)el1) > *((int *)el2)) return(1); else return(0); } int AtranspA(/* Input. */ int *ir_in, int *jc_in, double *s_in, int m, int n, int nzmax, /* Output. */ int **ir_out_orig,/* These have to be pointers */ int *jc_out, /* to pointers to allow for */ double **s_out_orig) /* realloc. */ { int i=0, j=0; int ri=0; /* Row index of AtA. */ int ci=0; /* Column index of AtA. */ int cnt=0; /* Count of non-zero elements in AtA. */ int si=0, ei=0; int si2=0, ei2=0; double *s_tmp = NULL; /* Temprary non-sparse column of A. */ int *flags = NULL; /* Temprary non-sparse column of A. */ int *ir_out = NULL; double *s_out = NULL; /* We are working with copies of the input pointers to avoid double dereferencing at each access. */ ir_out = *ir_out_orig; s_out = *s_out_orig; s_tmp = s_in - 1; flags = (int *) my_calloc(m,sizeof(int)); for (ci=0,cnt=0; ci si) /* If anything non-zero at all in this column. */ { /* Fill in dense copy of column of A. */ for (i=si; i= nzmax) /* Oh oh, didn't really want that. */ { nzmax = ((int) 1.1 * nzmax); /* Increase memory by 10% */ ir_out = (int *) my_realloc(ir_out,nzmax*sizeof(int)); s_out = (double *) my_realloc(s_out,nzmax*sizeof(double)); printf("\nWarning, non-optimal nzmax passed to AtranspA."); } /* Divide into diagonal, sub-diagonal and super-diagonal cases. */ if (ri==ci) /* If diagonal. */ { for (i=si; i ci) /* If subdiagonal, calculate it. */ { si2 = jc_in[ri]; ei2 = jc_in[ri+1]; for (i=si2; i si) /* If anything non-zero at all in this column. */ { /* Fill in dense copy of column of B. */ for (i=si; i= nzmax) /* Oh oh, didn't really want that. */ { nzmax = ((int) 1.1 * nzmax); /* Increase memory by 10% */ ir_out = (int *) my_realloc(ir_out,nzmax*sizeof(int)); s_out = (double *) my_realloc(s_out,nzmax*sizeof(double)); printf("\nWarning, non-optimal nzmax passed to AtranspA."); } si2 = jc_A[ri]; ei2 = jc_A[ri+1]; if ((ir_B[si] <= ir_A[si2] && ir_B[ei-1] >= ir_A[si2]) || (ir_A[si2] <= ir_B[si] && ir_A[ei2-1] >= ir_B[si])) { for (i=si2; i 1) { fnirt_zoom_field_by2(3,tksp,tcdim,idim,dim,tmpc1,&tmpc2); tksp[dim] /= 2; tcdim[dim] = no_of_knots(tksp[dim],idim[dim]); sf /= 2; if (tmpc1 != oc) { my_free(tmpc1); } tmpc1 = tmpc2; } if (sf != 1) { printf("\nzoom_field: zooming must be by power of 2."); return(-1); } } for (i=0, sz=1; i<3; i++) {sz *= no_of_knots(nksp[i],idim[i]);} memcpy(nc,tmpc2,sz*sizeof(double)); my_free(tmpc2); return(1); } /* Calculates the new set of spline-coefficients resulting when the knot-spacing of an existing field is cut by half in one direction. */ int fnirt_zoom_field_by2(/* Input */ int ndim, int ksp[3], int ocdim[3], int idim[3], int zdim, double *oc, /* Output */ double **nc) { int i=0, j=0, k=0; int nsz=0; int ncdim[3]; double a0=1.0/8.0, a4=a0; double a1=1.0/2.0, a3=a1; double a2=3.0/4.0; for (i=ndim; i<3; i++) {ocdim[i] = 1; idim[i] = 1; ksp[i] = 1;} for (i=0, nsz=1; i<3; i++) { ncdim[i] = (i==zdim) ? no_of_knots(ksp[i]/2,idim[i]) : ocdim[i]; nsz *= ncdim[i]; } *nc = (double *) my_calloc(nsz,sizeof(double)); if (zdim==0) { for (k=0; k