Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

kpart.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 #include "coxplot.h"
00008 
00009 /* type for a partition set */
00010 
00011 typedef struct {
00012    int npart , nall ;
00013    int * typ ;
00014    float * x , * y ;
00015 } kpart ;
00016 
00017 #define INCKP 16
00018 
00019 /* macro to create a new partition set */
00020 
00021 #define NEWKP(kp)                             \
00022   do{ int nn = INCKP ;                        \
00023       (kp) = malloc(sizeof(kpart)) ;          \
00024       (kp)->typ = malloc(sizeof(int)  *nn ) ; \
00025       (kp)->x   = malloc(sizeof(float)*nn ) ; \
00026       (kp)->y   = malloc(sizeof(float)*nn ) ; \
00027       (kp)->nall= nn ; (kp)->npart = 1 ;      \
00028       (kp)->typ[0] = LONG_TYPE ;              \
00029       (kp)->x[0] = 0.0 ; (kp)->y[0] = 0.0 ; } while(0)
00030 
00031 /* macro to make sure a partition set has at least n components */
00032 
00033 #define ATLEAST(kp,n)                                           \
00034   do{ if( (kp)->nall < (n) ){                                   \
00035          int nn = (n)+INCKP ;                                   \
00036          (kp)->typ = realloc( (kp)->typ , sizeof(int)  *nn ) ;  \
00037          (kp)->x   = realloc( (kp)->x   , sizeof(float)*nn ) ;  \
00038          (kp)->y   = realloc( (kp)->y   , sizeof(float)*nn ) ;  \
00039          (kp)->nall= (nn) ;                                     \
00040       } } while(0)
00041 
00042 #define LONG_TYPE       1
00043 #define TRAN_TYPE       2
00044 #define ILLEGAL_TYPE -666
00045 
00046 #define DEL      0.001
00047 #define FEQ(a,b) (fabs((a)-(b)) < DEL)
00048 
00049 /* prototypes */
00050 
00051 void collapse_illegals( kpart * kp ) ;
00052 void collapse_xy( kpart * kp ) ;
00053 void move( kpart * kp , float dx , float dy ) ;
00054 void flip( kpart * kp ) ;
00055 void flip90( kpart * kp ) ;
00056 void flip180( kpart * kp ) ;
00057 void crush( kpart * kp ) ;
00058 
00059 /*----------------------------------------------------------------------*/
00060 
00061 void collapse_illegals( kpart * kp )
00062 {
00063    int ii , jj , jtop ;
00064 
00065    if( kp == NULL ) return ;
00066 
00067    /* find uppermost legal entry */
00068 
00069    for( jj=kp->npart-1 ; jj >= 0 ; jj-- )
00070       if( kp->typ[jj] >= 0 ) break ;
00071 
00072    if( jj <  0 ){ kp->npart = 0; return; }
00073    if( jj == 0 ){ kp->npart = 1; return; }
00074 
00075    jtop = jj ;
00076 
00077    /* for each entry below this, see if it is legal */
00078 
00079    for( jj=0 ; jj < jtop ; ){
00080 
00081       if( kp->typ[jj] < 0 ){                   /* illegal entry at jj   */
00082          for( ii=jj+1 ; ii <= jtop ; ii++ ){   /* => move all those     */
00083             kp->typ[ii-1] = kp->typ[ii] ;      /*    above jj down by 1 */
00084             kp->x[ii-1]   = kp->x[ii] ;
00085             kp->y[ii-1]   = kp->y[ii] ;
00086          }
00087          jtop-- ;                              /* top index is reduced */
00088       } else {
00089          jj++ ;                                /* go to next entry jj */
00090       }
00091    }
00092 
00093    return ;
00094 }
00095 
00096 /*----------------------------------------------------------------------*/
00097 
00098 void collapse_xy( kpart * kp )
00099 {
00100    int ii , jj ;
00101 
00102    if( kp == NULL || kp->npart < 2 ) return ;
00103 
00104    /* find all partitions that are at the same place */
00105 
00106    for( jj=1 ; jj < kp->npart ; jj++ ){
00107       if( kp->typ[jj] < 0 ) continue ;         /* skip illegals */
00108 
00109       for( ii=0 ; ii < jj ; ii++ ){            /* check all entries below jj */
00110 
00111          if( kp->typ[ii] >= 0         &&       /* if legal */
00112              FEQ(kp->x[ii],kp->x[jj]) &&       /* and at same place */
00113              FEQ(kp->y[ii],kp->y[jj])   ){
00114 
00115             kp->typ[jj] = ILLEGAL_TYPE ;       /* mark for demolition */
00116             break ;
00117          }
00118       }
00119    }
00120 
00121    collapse_illegals( kp ) ;                   /* demolition */
00122    return ;
00123 }
00124 
00125 /*----------------------------------------------------------------------*/
00126 
00127 void move( kpart * kp , float dx , float dy )
00128 {
00129    int ii ;
00130 
00131    if( kp == NULL || kp->npart == 0 ) return ;
00132 
00133    for( ii=0 ; ii < kp->npart ; kp++ ){
00134       if( kp->typ[ii] == TRAN_TYPE ){     /* only transverse components move */
00135          kp->x[ii] += dx ;
00136          kp->y[ii] += dy ;
00137       }
00138    }
00139    return ;
00140 }
00141 
00142 /*----------------------------------------------------------------------*/
00143 
00144 void flip( kpart * kp )
00145 {
00146    int ii , itop , jj ;
00147 
00148    if( kp == NULL || kp->npart == 0 ) return ;
00149 
00150    itop = kp->npart ;
00151    for( ii=0 ; ii < itop ; kp++ ){
00152 
00153       jj = kp->npart ;
00154 
00155       switch( kp->typ[ii] ){
00156 
00157          case TRAN_TYPE:{                   /* transverse magnetization   */
00158             ATLEAST(kp,jj+4) ;              /* component breaks into 4    */
00159                                             /* pieces: 1 at same place,   */
00160             kp->typ[jj] = TRAN_TYPE ;       /*         1 new transverse   */
00161             kp->x[jj]   = - kp->x[ii] ;     /*     and 2 new longitudinal */
00162             kp->y[jj]   = - kp->y[ii] ;
00163 
00164             kp->typ[jj+1] = LONG_TYPE ;
00165             kp->x[jj+1]   = kp->x[ii] ;
00166             kp->y[jj+1]   = kp->y[ii] ;
00167 
00168             kp->typ[jj+2] = LONG_TYPE ;
00169             kp->x[jj+2]   = - kp->x[ii] ;
00170             kp->y[jj+2]   = - kp->y[ii] ;
00171          }
00172          break ;
00173 
00174          case LONG_TYPE:{                   /* longitudinal magnetization */
00175             ATLEAST(kp,jj+1) ;              /* breaks into 2 pieces:      */
00176                                             /*   1 at same place          */
00177             kp->typ[jj] = LONG_TYPE ;       /*   1 new transverse         */
00178             kp->x[jj]   = kp->x[ii] ;
00179             kp->y[jj]   = kp->y[ii] ;
00180          }
00181          break ;
00182 
00183       }
00184    }
00185 
00186    collapse_xy(kp) ;
00187    return ;
00188 }
00189 
00190 /*----------------------------------------------------------------------*/
00191 
00192 void flip90( kpart * kp )
00193 {
00194    int ii , itop , jj ;
00195 
00196    if( kp == NULL || kp->npart == 0 ) return ;
00197 
00198    itop = kp->npart ;
00199    for( ii=0 ; ii < itop ; kp++ ){
00200 
00201       jj = kp->npart ;
00202 
00203       switch( kp->typ[ii] ){
00204 
00205          case TRAN_TYPE:{                  /* rules for transverse are */
00206             ATLEAST(kp,jj+4) ;             /* same as previous case    */
00207 
00208             kp->typ[jj] = LONG_TYPE ;
00209             kp->x[jj]   = - kp->x[ii] ;
00210             kp->y[jj]   = - kp->y[ii] ;
00211 
00212             kp->typ[jj+1] = TRAN_TYPE ;
00213             kp->x[jj+1]   = kp->x[ii] ;
00214             kp->y[jj+1]   = kp->y[ii] ;
00215 
00216             kp->typ[jj+2] = TRAN_TYPE ;
00217             kp->x[jj+2]   = - kp->x[ii] ;
00218             kp->y[jj+2]   = - kp->y[ii] ;
00219          }
00220          break ;
00221 
00222          case LONG_TYPE:{                  /* longitudinal is just         */
00223             kp->typ[ii] = LONG_TYPE ;      /* converted to pure transverse */
00224          }
00225          break ;
00226 
00227       }
00228    }
00229 
00230    collapse_xy(kp) ;
00231    return ;
00232 }
00233 
00234 /*----------------------------------------------------------------------*/
00235 
00236 void flip180( kpart * kp )
00237 {
00238    int ii , itop ;
00239 
00240    if( kp == NULL || kp->npart == 0 ) return ;
00241 
00242    itop = kp->npart ;
00243    for( ii=0 ; ii < itop ; kp++ ){
00244       if( kp->typ[ii] == TRAN_TYPE ){   /* transverse flips over */
00245          kp->x[ii] = - kp->x[ii] ;
00246          kp->y[ii] = - kp->y[ii] ;
00247       }
00248    }
00249 
00250    collapse_xy(kp) ;
00251    return ;
00252 }
00253 
00254 /*----------------------------------------------------------------------*/
00255 
00256 void crush( kpart * kp )
00257 {
00258    int ii , itop ;
00259 
00260    if( kp == NULL || kp->npart == 0 ) return ;
00261 
00262    itop = kp->npart ;
00263    for( ii=0 ; ii < itop ; kp++ ){
00264       if( kp->typ[ii] == TRAN_TYPE ){   /* transverse is killed */
00265          kp->typ[ii] = ILLEGAL_TYPE ;
00266       }
00267    }
00268 
00269    collapse_xy(kp) ;
00270    return ;
00271 }
00272 
00273 /*----------------------------------------------------------------------*/
 

Powered by Plone

This site conforms to the following standards: