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  

suma_niml.c

Go to the documentation of this file.
00001 #include "niml.h"
00002 #include "suma_types.h"
00003 
00004 /*------------------------------------------------------------------*/
00005 /*! Make a NIML data element from an array of SUMA_ixyz structs.
00006     Return value is NULL if you input stupid values.  Otherwise,
00007     will return a populated element. Example:
00008      - int    num_ixyz=999 ;
00009      - SUMA_ixyz *ixyz=malloc(sizeof(SUMA_ixyz)*num_ixyz) ;
00010      - NI_element *nel ;
00011      - NI_stream *ns ;
00012      - ** fill ixyz[0..998].stuff somehow **
00013      - nel = SUMA_ixyz_to_NIML( num_ixyz , ixyz ) ;
00014      - NI_write_element( ns , nel , NI_TEXT_MODE ) ;
00015      - NI_free_element( nel ) ;
00016 --------------------------------------------------------------------*/
00017 
00018 NI_element * SUMA_ixyz_to_NIML( int num_ixyz , SUMA_ixyz *ixyz )
00019 {
00020    NI_element *nel ;
00021    int ii , *ic ;
00022    float *xc,*yc,*zc ;
00023 
00024    /* check inputs for sanity */
00025 
00026    if( num_ixyz < 1 || ixyz == NULL ) return NULL ;  /* stupid caller */
00027 
00028    /* make a new data element, to be filled by columns */
00029 
00030    nel = NI_new_data_element( "SUMA_ixyz" , num_ixyz ) ;
00031 
00032    /* make the temp columns to be put in the element */
00033 
00034    ic = NI_malloc(int, sizeof(int)   * num_ixyz ) ;
00035    xc = NI_malloc(float, sizeof(float) * num_ixyz ) ;
00036    yc = NI_malloc(float, sizeof(float) * num_ixyz ) ;
00037    zc = NI_malloc(float, sizeof(float) * num_ixyz ) ;
00038 
00039    /* load these columns from the struct array */
00040 
00041    for( ii=0 ; ii < num_ixyz ; ii++ ){
00042       ic[ii] = ixyz[ii].id ;
00043       xc[ii] = ixyz[ii].x ;
00044       yc[ii] = ixyz[ii].y ;
00045       zc[ii] = ixyz[ii].z ;
00046    }
00047 
00048    /* copy columns into NI_element, freeing them when done */
00049 
00050    NI_add_column( nel , NI_INT   , ic ) ; free(ic) ;
00051    NI_add_column( nel , NI_FLOAT , xc ) ; free(xc) ;
00052    NI_add_column( nel , NI_FLOAT , yc ) ; free(yc) ;
00053    NI_add_column( nel , NI_FLOAT , zc ) ; free(zc) ;
00054 
00055    return nel ;
00056 }
00057 
00058 /*------------------------------------------------------------------*/
00059 /*! Unload a <SUMA_ixyz> NI_element into an array of newly
00060     malloc()-ed SUMA_ixyz structs.
00061     Return value is number of structs (will be 0 inputs are bad).
00062     *ixyz will point to the output array if the number of structs is
00063     positive.  This array will be newly malloc()-ed.  Example:
00064      - SUMA_ixyz *ixyz ;
00065      - int    num_ixyz ;
00066      - NI_element *nel ;  ** get this from somewhere **
00067      - num_ixyz = NIML_to_SUMA_ixyz( nel , &ixyz ) ;
00068      - if( num_ixyz == 0 ){ ** error error error ** }
00069      - else               { ** good good good ** }
00070      - NI_free_element(nel) ;
00071 --------------------------------------------------------------------*/
00072 
00073 int NIML_to_SUMA_ixyz( NI_element *nel , SUMA_ixyz **ixyz )
00074 {
00075    int   num_ixyz ;       /* return value */
00076    SUMA_ixyz *myixyz ;    /* output array of structs */
00077    int   *ic , ii ;
00078    float *xc, *yc, *zc ;
00079 
00080    /* check element for correctness */
00081 
00082    if( nel             == 0        ||   /* no data element?          */
00083        nel->vec_len    <  1        ||   /* empty element?             */
00084        nel->vec_filled <  1        ||   /* no data was filled in?      */
00085        nel->vec_num    <  4        ||   /* less than 4 columns?         */
00086        nel->vec_typ[0] != NI_INT   ||   /* must be int,float,float,float */
00087        nel->vec_typ[1] != NI_FLOAT ||
00088        nel->vec_typ[2] != NI_FLOAT ||
00089        nel->vec_typ[3] != NI_FLOAT   ) return 0 ;  /* bad bad bad */
00090 
00091    /* number of structs is number of completely filled rows */
00092 
00093    num_ixyz = nel->vec_filled ;
00094 
00095    /* make space for the new structs */
00096 
00097    myixyz = NI_malloc(SUMA_ixyz, sizeof(SUMA_ixyz) * num_ixyz ) ;
00098 
00099    /* pointers to the data columns in the NI_element */
00100 
00101    ic = (int *)   nel->vec[0] ;
00102    xc = (float *) nel->vec[1] ;
00103    yc = (float *) nel->vec[2] ;
00104    zc = (float *) nel->vec[3] ;
00105 
00106    /* load the values from the element */
00107 
00108    for( ii=0 ; ii < num_ixyz ; ii++ ){
00109       myixyz[ii].id = ic[ii] ;
00110       myixyz[ii].x  = xc[ii] ;
00111       myixyz[ii].y  = yc[ii] ;
00112       myixyz[ii].z  = zc[ii] ;
00113    }
00114 
00115    /* we is done */
00116 
00117    *ixyz = myixyz ; return num_ixyz ;
00118 }
 

Powered by Plone

This site conforms to the following standards: