Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
plug_second_dataset.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "afni.h"
00008
00009 #ifndef ALLOW_PLUGINS
00010 # error "Plugins not properly set up -- see machdep.h"
00011 #endif
00012
00013 #define DSET2_VERSION "Version 1.1 <October, 2002>"
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 char * DSET2_main( PLUGIN_interface *) ;
00029 void DSET2_func( int num , double to,double dt, float * vec ) ;
00030 void DSET2_dset_recv( int why, int np, int * ijk, void * aux ) ;
00031
00032 static char helpstring[] =
00033 " Purpose: Control the 'Dataset#2' 1D timeseries function\n"
00034 "\n"
00035 " Dataset = a 3D+time dataset from which to extract data\n"
00036 "\n"
00037 " Justify = Left means to put timeseries data at start of output array\n"
00038 " Right means to put timeseries data at end of output array\n"
00039 "\n"
00040 " Fill = Extend means to copy the last value\n"
00041 " Zero means to fill with zeros\n"
00042 "\n"
00043 " Justify and Fill are only used when the number of points in the input\n"
00044 " dataset doesn't match the number of points in the timeseries being replaced.\n"
00045 " Let M = length of dataset, N = length of timeseries,\n"
00046 " ts[] = timeseries array, ds[] = dataset array.\n"
00047 "\n"
00048 " Case: M < N (too little data to fill timeseries)\n"
00049 " Left: ts[i] = ds[i] i=0..M-1 Data goes to left\n"
00050 " = ds[M-1] i=M..N-1 if Extend edge of timeseries\n"
00051 " = 0.0 i=M..N-1 if Zero\n"
00052 " Right: ts[i] = ds[0] i=0..J-1 if Extend (with J=N-M) Data goes to right\n"
00053 " = 0.0 i=0..J-1 if Zero edge of timeseries\n"
00054 " = ds[i-J] i=J..N-1\n"
00055 " Case: M > N (too much data for timeseries)\n"
00056 " Left: ts[i] = ds[i] i=0..N-1 Left data to ts[]\n"
00057 " Right: ts[i] = ds[i+J] i=0..N-1 (with J=M-N) Right data to ts[]\n"
00058 "\n"
00059 " -- RWCox - 18 May 2000\n"
00060 ;
00061
00062 static THD_3dim_dataset * dset = NULL ;
00063 static MCW_idcode g_id;
00064 static int g_dset_recv = -1 ;
00065 static int justify = 0 ;
00066 static int fill = 0 ;
00067
00068
00069
00070
00071
00072 static char *lr[2] = { "Left" , "Right" } ;
00073 static char *ez[2] = { "Extend" , "Zero" } ;
00074
00075 static PLUGIN_interface * plint=NULL ;
00076
00077 static void DSET2_func_init(void)
00078 {
00079 PLUG_startup_plugin_CB( NULL , (XtPointer)plint , NULL ) ;
00080 }
00081
00082
00083 DEFINE_PLUGIN_PROTOTYPE
00084
00085 PLUGIN_interface * PLUGIN_init( int ncall )
00086 {
00087
00088 ENTRY("PLUGIN_init - Dataset#2") ;
00089
00090 if( ncall > 0 ) RETURN( NULL ) ;
00091
00092 AFNI_register_nD_function ( 1 , "Dataset#2" , (generic_func *)DSET2_func ,
00093 NEEDS_DSET_INDEX ) ;
00094 AFNI_register_nD_func_init( 1 , (generic_func *)DSET2_func_init ) ;
00095
00096 plint = PLUTO_new_interface( "Dataset#2" , "Controls 1D function Dataset#2" , helpstring ,
00097 PLUGIN_CALL_VIA_MENU , DSET2_main ) ;
00098
00099 PLUTO_add_hint( plint , "Controls 1D function Dataset#2" ) ;
00100
00101 PLUTO_set_runlabels( plint , "Set+Keep" , "Set+Close" ) ;
00102
00103 PLUTO_set_sequence( plint , "A:funcs:dataset#2" ) ;
00104
00105 PLUTO_add_option( plint , "Input" , "Input" , TRUE ) ;
00106 PLUTO_add_dataset(plint , "Dataset" ,
00107 ANAT_ALL_MASK , FUNC_ALL_MASK ,
00108 DIMEN_4D_MASK | BRICK_ALLREAL_MASK ) ;
00109
00110 PLUTO_add_option( plint , "Where" , "Where" , TRUE ) ;
00111 PLUTO_add_string( plint, "Justify", 2, lr, justify ) ;
00112
00113 PLUTO_add_option( plint , "How" , "How" , TRUE ) ;
00114 PLUTO_add_string( plint, "Fill", 2, ez, fill ) ;
00115
00116 RETURN( plint ) ;
00117 }
00118
00119
00120
00121
00122
00123 char * DSET2_main( PLUGIN_interface * plint )
00124 {
00125 MCW_idcode * idc ;
00126 char * str ;
00127
00128 ENTRY("DSET2_main") ;
00129
00130 if( plint == NULL )
00131 RETURN("***********************\n"
00132 "DSET2_main: NULL input\n"
00133 "***********************") ;
00134
00135 PLUTO_next_option(plint) ;
00136 idc = PLUTO_get_idcode(plint) ;
00137 dset = PLUTO_find_dset(idc) ;
00138 if( dset == NULL )
00139 RETURN("******************************\n"
00140 "DSET2_main: bad input dataset\n"
00141 "******************************") ;
00142
00143 g_id = *idc ;
00144
00145 PLUTO_next_option(plint) ;
00146 str = PLUTO_get_string(plint) ;
00147 justify = (strcmp(str,lr[0]) != 0) ;
00148
00149 PLUTO_next_option(plint) ;
00150 str = PLUTO_get_string(plint) ;
00151 fill = (strcmp(str,ez[0]) != 0) ;
00152
00153 if ( g_dset_recv < 0 )
00154 g_dset_recv = AFNI_receive_init( plint->im3d, RECEIVE_DSETCHANGE_MASK,
00155 DSET2_dset_recv, (void *)plint ,
00156 "DSET2_dset_recv" ) ;
00157
00158 if ( g_dset_recv < 0 )
00159 RETURN("*************************************\n"
00160 "DSET2_main: failed AFNI_receive_init\n"
00161 "*************************************") ;
00162
00163 RETURN(NULL) ;
00164 }
00165
00166
00167
00168 void DSET2_dset_recv( int why, int np, int * ijk, void * aux )
00169 {
00170 PLUGIN_interface * plint = (PLUGIN_interface *)aux;
00171
00172 ENTRY( "DSET2_dset_recv" );
00173
00174 switch ( why )
00175 {
00176 default:
00177 {
00178 fprintf( stderr, "warning: DSET2_dset_recv() called with invalid "
00179 "why code, %d\n", why );
00180 EXRETURN;
00181 }
00182
00183 case RECEIVE_ALTERATION:
00184 case RECEIVE_DSETCHANGE:
00185 {
00186
00187 if ( ! ISZERO_IDCODE( g_id ) )
00188 {
00189 dset = PLUTO_find_dset( &g_id );
00190
00191 if( !ISVALID_DSET(dset) )
00192 {
00193 ZERO_IDCODE( g_id );
00194 dset = NULL;
00195 }
00196 }
00197 else
00198 dset = NULL;
00199
00200 if ( dset == NULL )
00201 {
00202 AFNI_receive_control( plint->im3d, g_dset_recv,
00203 EVERYTHING_SHUTDOWN, NULL );
00204 g_dset_recv = -1;
00205 PLUTO_popup_worker( plint,
00206 "Warning: plugin 'Dataset#2'\n"
00207 "has lost its dataset link.\n"
00208 "To plot a 1-D overlay, please\n"
00209 "re-run the plugin.",
00210 MCW_USER_KILL | MCW_TIMER_KILL ) ;
00211 }
00212 }
00213 }
00214
00215 EXRETURN;
00216 }
00217
00218
00219
00220 void DSET2_func( int num , double to,double dt, float * vec )
00221 {
00222 int ii , ijk , jj ;
00223 MRI_IMAGE * tsim ;
00224 float val ;
00225
00226 ENTRY("DSET2_func") ;
00227
00228 if( !ISVALID_DSET(dset) ) EXRETURN ;
00229
00230 DSET_load(dset) ;
00231
00232 ijk = AFNI_needs_dset_ijk() ;
00233
00234 tsim = THD_extract_series( ijk , dset , 0 ) ;
00235
00236 if( tsim == NULL ) EXRETURN ;
00237
00238 if( tsim->nx == num ){
00239
00240 memcpy(vec,MRI_FLOAT_PTR(tsim),sizeof(float)*num) ;
00241
00242 } else if( tsim->nx < num ){
00243
00244 if( justify == 0 ){
00245 memcpy(vec,MRI_FLOAT_PTR(tsim),sizeof(float)*tsim->nx) ;
00246
00247 val = (fill == 0) ? vec[tsim->nx-1]
00248 : 0.0 ;
00249
00250 for( ii=tsim->nx ; ii < num ; ii++ ) vec[ii] = val ;
00251
00252 } else {
00253 jj = num - tsim->nx ;
00254 memcpy(vec+jj,MRI_FLOAT_PTR(tsim),sizeof(float)*tsim->nx) ;
00255
00256 val = (fill == 0) ? vec[jj]
00257 : 0.0 ;
00258
00259 for( ii=0 ; ii < jj ; ii++ ) vec[ii] = val ;
00260 }
00261
00262 } else {
00263
00264 if( justify == 0 ){
00265 memcpy(vec,MRI_FLOAT_PTR(tsim),sizeof(float)*num) ;
00266 } else {
00267 jj = tsim->nx - num ;
00268 memcpy(vec,MRI_FLOAT_PTR(tsim)+jj,sizeof(float)*num) ;
00269 }
00270 }
00271
00272 mri_free(tsim) ;
00273 EXRETURN ;
00274 }