1 #include "niml.h"
2 #include "suma_types.h"
3 
4 /*------------------------------------------------------------------*/
5 /*! Make a NIML data element from an array of SUMA_ixyz structs.
6     Return value is NULL if you input stupid values.  Otherwise,
7     will return a populated element. Example:
8      - int    num_ixyz=999 ;
9      - SUMA_ixyz *ixyz=malloc(sizeof(SUMA_ixyz)*num_ixyz) ;
10      - NI_element *nel ;
11      - NI_stream *ns ;
12      - ** fill ixyz[0..998].stuff somehow **
13      - nel = SUMA_ixyz_to_NIML( num_ixyz , ixyz ) ;
14      - NI_write_element( ns , nel , NI_TEXT_MODE ) ;
15      - NI_free_element( nel ) ;
16 --------------------------------------------------------------------*/
17 
SUMA_ixyz_to_NIML(int num_ixyz,SUMA_ixyz * ixyz)18 NI_element * SUMA_ixyz_to_NIML( int num_ixyz , SUMA_ixyz *ixyz )
19 {
20    NI_element *nel ;
21    int ii , *ic ;
22    float *xc,*yc,*zc ;
23 
24    /* check inputs for sanity */
25 
26    if( num_ixyz < 1 || ixyz == NULL ) return NULL ;  /* stupid caller */
27 
28    /* make a new data element, to be filled by columns */
29 
30    nel = NI_new_data_element( "SUMA_ixyz" , num_ixyz ) ;
31 
32    /* make the temp columns to be put in the element */
33 
34    ic = NI_malloc(int, sizeof(int)   * num_ixyz ) ;
35    xc = NI_malloc(float, sizeof(float) * num_ixyz ) ;
36    yc = NI_malloc(float, sizeof(float) * num_ixyz ) ;
37    zc = NI_malloc(float, sizeof(float) * num_ixyz ) ;
38 
39    /* load these columns from the struct array */
40 
41    for( ii=0 ; ii < num_ixyz ; ii++ ){
42       ic[ii] = ixyz[ii].id ;
43       xc[ii] = ixyz[ii].x ;
44       yc[ii] = ixyz[ii].y ;
45       zc[ii] = ixyz[ii].z ;
46    }
47 
48    /* copy columns into NI_element, freeing them when done */
49 
50    NI_add_column( nel , NI_INT   , ic ) ; free(ic) ;
51    NI_add_column( nel , NI_FLOAT , xc ) ; free(xc) ;
52    NI_add_column( nel , NI_FLOAT , yc ) ; free(yc) ;
53    NI_add_column( nel , NI_FLOAT , zc ) ; free(zc) ;
54 
55    return nel ;
56 }
57 
58 /*------------------------------------------------------------------*/
59 /*! Unload a <SUMA_ixyz> NI_element into an array of newly
60     malloc()-ed SUMA_ixyz structs.
61     Return value is number of structs (will be 0 inputs are bad).
62     *ixyz will point to the output array if the number of structs is
63     positive.  This array will be newly malloc()-ed.  Example:
64      - SUMA_ixyz *ixyz ;
65      - int    num_ixyz ;
66      - NI_element *nel ;  ** get this from somewhere **
67      - num_ixyz = NIML_to_SUMA_ixyz( nel , &ixyz ) ;
68      - if( num_ixyz == 0 ){ ** error error error ** }
69      - else               { ** good good good ** }
70      - NI_free_element(nel) ;
71 --------------------------------------------------------------------*/
72 
NIML_to_SUMA_ixyz(NI_element * nel,SUMA_ixyz ** ixyz)73 int NIML_to_SUMA_ixyz( NI_element *nel , SUMA_ixyz **ixyz )
74 {
75    int   num_ixyz ;       /* return value */
76    SUMA_ixyz *myixyz ;    /* output array of structs */
77    int   *ic , ii ;
78    float *xc, *yc, *zc ;
79 
80    /* check element for correctness */
81 
82    if( nel             == 0        ||   /* no data element?          */
83        nel->vec_len    <  1        ||   /* empty element?             */
84        nel->vec_filled <  1        ||   /* no data was filled in?      */
85        nel->vec_num    <  4        ||   /* less than 4 columns?         */
86        nel->vec_typ[0] != NI_INT   ||   /* must be int,float,float,float */
87        nel->vec_typ[1] != NI_FLOAT ||
88        nel->vec_typ[2] != NI_FLOAT ||
89        nel->vec_typ[3] != NI_FLOAT   ) return 0 ;  /* bad bad bad */
90 
91    /* number of structs is number of completely filled rows */
92 
93    num_ixyz = nel->vec_filled ;
94 
95    /* make space for the new structs */
96 
97    myixyz = NI_malloc(SUMA_ixyz, sizeof(SUMA_ixyz) * num_ixyz ) ;
98 
99    /* pointers to the data columns in the NI_element */
100 
101    ic = (int *)   nel->vec[0] ;
102    xc = (float *) nel->vec[1] ;
103    yc = (float *) nel->vec[2] ;
104    zc = (float *) nel->vec[3] ;
105 
106    /* load the values from the element */
107 
108    for( ii=0 ; ii < num_ixyz ; ii++ ){
109       myixyz[ii].id = ic[ii] ;
110       myixyz[ii].x  = xc[ii] ;
111       myixyz[ii].y  = yc[ii] ;
112       myixyz[ii].z  = zc[ii] ;
113    }
114 
115    /* we is done */
116 
117    *ixyz = myixyz ; return num_ixyz ;
118 }
119