00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
#include <volume_io/internal_volume_io.h>
00016
#include <bicpl/vols.h>
00017
00018
#ifndef lint
00019
static char rcsid[] =
"$Header: /software/source//libraries/bicpl/Volumes/talairach.c,v 1.14 2000/02/06 15:30:57 stever Exp $";
00020
#endif
00021
00022 #define TALAIRACH_OFFSET 16.0
00023
00024 #define FALSE 0
00025 #define TRUE 1
00026
00027 private int nx = 128;
00028 private int ny = 128;
00029 private int nz = 80;
00030
00031 private Real
x_low = -1.28;
00032 private Real
x_high = 1.28;
00033 private Real
y_low = -1.28;
00034 private Real
y_high = 1.28;
00035 private Real
z_low = -0.50;
00036 private Real
z_high = 1.10;
00037
00038 private Real
x_dist_minus_1 = -67.0;
00039 private Real
x_dist_1 = 67.0;
00040 private Real
y_dist_minus_1 = -86.0;
00041 private Real
y_dist_1 = 86.0;
00042 private Real
z_dist_0 = 0.0;
00043 private Real
z_dist_1 = 75.0;
00044
00045 private BOOLEAN
initialized =
FALSE;
00046
00047
private void read_talairach_coordinate_system(
void );
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 private void check_initialized(
void )
00064 {
00065
if( !
initialized )
00066 {
00067
read_talairach_coordinate_system();
00068
initialized =
TRUE;
00069 }
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 private void read_talairach_coordinate_system(
void )
00087 {
00088
int i, n_lines, ch;
00089 FILE *file;
00090 STRING name =
"brain_size.dat";
00091 STRING filename;
00092
int okay;
00093
int tmp_nx, tmp_ny, tmp_nz;
00094 Real tmp_x_low, tmp_x_high, tmp_y_low, tmp_y_high, tmp_z_low;
00095 Real tmp_z_high, tmp_x_dist_minus_1, tmp_x_dist_1, tmp_y_dist_minus_1;
00096 Real tmp_y_dist_1, tmp_z_dist_0, tmp_z_dist_1;
00097
00098 okay =
TRUE;
00099
00100
if( file_exists(name) )
00101 {
00102 filename = create_string( name );
00103 }
00104
else
00105 {
00106
if( getenv(
"PET_ROI" ) == NULL )
00107 {
00108 print_error(
"You have to setenv PET_ROI.\n" );
00109 filename = NULL;
00110 okay =
FALSE;
00111 }
00112
else
00113 {
00114 filename = get_absolute_filename( name, getenv(
"PET_ROI") );
00115 }
00116 }
00117
00118
if( okay )
00119 {
00120 file = fopen( filename,
"r" );
00121
00122
if( file == (FILE *) 0 )
00123 {
00124 print_error(
"Cannot open %s\n", filename );
00125 okay =
FALSE;
00126 }
00127 }
00128
00129
if( okay )
00130 {
00131 (
void) input_int( file, &n_lines );
00132
00133
for( i = 0; i < n_lines+1; ++i )
00134 {
00135
while( (ch = fgetc(file)) != EOF && ch !=
'\n' )
00136 {}
00137 }
00138
00139
if( input_int( file, &tmp_nx ) != OK ||
00140 input_int( file, &tmp_ny ) != OK ||
00141 input_int( file, &tmp_nz ) != OK ||
00142 input_real( file, &tmp_x_low ) != OK ||
00143 input_real( file, &tmp_x_high ) != OK ||
00144 input_real( file, &tmp_y_low ) != OK ||
00145 input_real( file, &tmp_y_high ) != OK ||
00146 input_real( file, &tmp_z_low ) != OK ||
00147 input_real( file, &tmp_z_high ) != OK ||
00148 input_real( file, &tmp_x_dist_minus_1 ) != OK ||
00149 input_real( file, &tmp_x_dist_1 ) != OK ||
00150 input_real( file, &tmp_y_dist_minus_1 ) != OK ||
00151 input_real( file, &tmp_y_dist_1 ) != OK ||
00152 input_real( file, &tmp_z_dist_0 ) != OK ||
00153 input_real( file, &tmp_z_dist_1 ) != OK )
00154 {
00155 print_error(
"Error reading info from %s\n", filename );
00156 okay =
FALSE;
00157 }
00158 }
00159
00160
if( okay )
00161 {
00162
nx = tmp_nx;
00163
ny = tmp_ny;
00164
nz = tmp_nz;
00165
00166
x_low = tmp_x_low;
00167
x_high = tmp_x_high;
00168
y_low = tmp_y_low;
00169
y_high = tmp_y_high;
00170
z_low = tmp_z_low;
00171
z_high = tmp_z_high;
00172
00173
x_dist_minus_1 = tmp_x_dist_minus_1;
00174
x_dist_1 = tmp_x_dist_1;
00175
y_dist_minus_1 = tmp_y_dist_minus_1;
00176
y_dist_1 = tmp_y_dist_1;
00177
z_dist_0 = tmp_z_dist_0;
00178
z_dist_1 = tmp_z_dist_1;
00179
00180 (
void) fclose( file );
00181 }
00182
00183 delete_string( filename );
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 public void convert_voxel_to_talairach(
00207 Real x_voxel,
00208 Real y_voxel,
00209 Real z_voxel,
00210
int nx_voxels,
00211
int ny_voxels,
00212
int nz_voxels,
00213 Real *x_tal,
00214 Real *y_tal,
00215 Real *z_tal )
00216 {
00217
check_initialized();
00218
00219 *x_tal = x_voxel / (Real) nx_voxels * (Real)
nx;
00220 *y_tal = y_voxel / (Real) ny_voxels * (Real)
ny;
00221 *z_tal = z_voxel / (Real) nz_voxels * (Real)
nz;
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 public void convert_talairach_to_voxel(
00245 Real x_tal,
00246 Real y_tal,
00247 Real z_tal,
00248
int nx_voxels,
00249
int ny_voxels,
00250
int nz_voxels,
00251 Real *x_voxel,
00252 Real *y_voxel,
00253 Real *z_voxel )
00254 {
00255
check_initialized();
00256
00257 *x_voxel = x_tal / (Real)
nx * (Real) nx_voxels;
00258 *y_voxel = y_tal / (Real)
ny * (Real) ny_voxels;
00259 *z_voxel = z_tal / (Real)
nz * (Real) nz_voxels;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 private Real
convert_to_mm(
00283 Real tal,
00284
int n_planes,
00285 Real limit_low,
00286 Real limit_high,
00287 Real brain_dist_low,
00288 Real brain_dist_high,
00289 Real brain_limit_low,
00290 Real brain_limit_high )
00291 {
00292 Real mm;
00293 Real stereotactic;
00294
00295 stereotactic = limit_low + (limit_high - limit_low) * tal / (Real) n_planes;
00296
00297 mm = brain_dist_low + (brain_dist_high - brain_dist_low) *
00298 (stereotactic - brain_limit_low) / (brain_limit_high - brain_limit_low);
00299
00300
return( mm );
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 public void convert_talairach_to_mm(
00321 Real x_tal,
00322 Real y_tal,
00323 Real z_tal,
00324 Real *x_mm,
00325 Real *y_mm,
00326 Real *z_mm )
00327 {
00328
check_initialized();
00329
00330 *x_mm =
convert_to_mm( x_tal,
nx,
x_low,
x_high,
x_dist_minus_1,
x_dist_1,
00331 -1.0, 1.0 );
00332 *y_mm =
convert_to_mm( y_tal,
ny,
y_low,
y_high,
y_dist_minus_1,
y_dist_1,
00333 -1.0, 1.0 );
00334 *y_mm -=
TALAIRACH_OFFSET;
00335 *z_mm =
convert_to_mm( z_tal,
nz,
z_low,
z_high,
z_dist_0,
z_dist_1,
00336 0.0, 1.0 );
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 private Real
convert_from_mm(
00361 Real mm,
00362
int n_planes,
00363 Real limit_low,
00364 Real limit_high,
00365 Real brain_dist_low,
00366 Real brain_dist_high,
00367 Real brain_limit_low,
00368 Real brain_limit_high )
00369 {
00370 Real stereotactic;
00371
00372 stereotactic = brain_limit_low +
00373 (mm - brain_dist_low) * (brain_limit_high - brain_limit_low)
00374 / (brain_dist_high - brain_dist_low);
00375
00376
return( (stereotactic - limit_low) * (Real) n_planes /
00377 (limit_high - limit_low) );
00378 }
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 public void convert_mm_to_talairach(
00398 Real x_mm,
00399 Real y_mm,
00400 Real z_mm,
00401 Real *x_tal,
00402 Real *y_tal,
00403 Real *z_tal )
00404 {
00405
check_initialized();
00406
00407 *x_tal =
convert_from_mm( x_mm,
nx,
x_low,
x_high,
x_dist_minus_1,
x_dist_1,
00408 -1.0, 1.0 );
00409 *y_tal =
convert_from_mm( y_mm +
TALAIRACH_OFFSET,
ny,
y_low,
y_high,
00410
y_dist_minus_1,
y_dist_1, -1.0, 1.0 );
00411 *z_tal =
convert_from_mm( z_mm,
nz,
z_low,
z_high,
z_dist_0,
z_dist_1,
00412 0.0, 1.0 );
00413 }