Skip to content

Instantly share code, notes, and snippets.

@stowler
Created April 16, 2013 17:20
Show Gist options
  • Save stowler/5397749 to your computer and use it in GitHub Desktop.
Save stowler/5397749 to your computer and use it in GitHub Desktop.
mm->vox integers per AFNI 3dmaskdump OUTPUT
AFNI 3dmaskdump OUTPUT:
------------------------------------------------
Appears to use implicit casting and a custom log-based algorithm to convert
coordinate floats to integers:
If user requests integer voxel coordinates (not mm), 3dmaskdump creates them
from internal float voxel coordinates (not mm) using functions in multivector.c:
MV_format_fval(), which calls MV_fval_to_char()
e.g., here 3dmaskdump calls for outputting integer voxel coordinate ijk:
666 i = DSET_index_to_ix( input_dset[0] , ii ) ; /* voxel indexes */
667 j = DSET_index_to_jy( input_dset[0] , ii ) ;
668 k = DSET_index_to_kz( input_dset[0] , ii ) ;
669
670 otemp = MV_format_fval((float)i); strcat(obuf,otemp); strcat(obuf," ");
671 otemp = MV_format_fval((float)j); strcat(obuf,otemp); strcat(obuf," ");
672 otemp = MV_format_fval((float)k); strcat(obuf,otemp); strcat(obuf," ");
...which invokes the algorithm in lines 395-457 of multivector.c:
395 void MV_fval_to_char( float qval , char *buf )
396 {
397 float aval = fabs(qval) ;
398 int lv ;
399 char lbuf[32] ;
400 int il ;
401
402 /* special case if the value is an integer */
403
404 if( qval == 0.0 ){ strcpy(buf,"0"); return; }
405
406 lv = (fabs(qval) < 99999999.0) ? (int)qval : 100000001 ;
407
408 if( qval == lv && abs(lv) < 100000000 ){
409 sprintf( buf, "%d" , lv ) ; return ;
410 }
411
412 /* macro to strip trailing zeros from output */
413
414 #undef BSTRIP
415 #define BSTRIP for( il=strlen(lbuf)-1 ; \
416 il>1 && (lbuf[il]=='0' || lbuf[il]==' ') ; \
417 il-- ) lbuf[il] = '\0'
418
419 /* noninteger: choose floating format based on magnitude */
420
421 lv = (int) (10.0001 + log10(aval)) ;
422
423 switch( lv ){
424
425 default:
426 if( qval > 0.0 ) sprintf( lbuf , "%-12.6e" , qval ) ;
427 else sprintf( lbuf , "%-12.5e" , qval ) ;
428 break ;
429
430 case 6: /* 0.0001-0.001 */
431 case 7: /* 0.001 -0.01 */
432 case 8: /* 0.01 -0.1 */
433 case 9: /* 0.1 -1 */
434 case 10: /* 1 -9.99 */
435 sprintf( lbuf , "%-9.6f" , qval ) ; BSTRIP ; break ;
436
437 case 11: /* 10-99.9 */
438 sprintf( lbuf , "%-9.5f" , qval ) ; BSTRIP ; break ;
439
440 case 12: /* 100-999.9 */
441 sprintf( lbuf , "%-9.4f" , qval ) ; BSTRIP ; break ;
442
443 case 13: /* 1000-9999.9 */
444 sprintf( lbuf , "%-9.3f" , qval ) ; BSTRIP ; break ;
445
446 case 14: /* 10000-99999.9 */
447 sprintf( lbuf , "%-9.2f" , qval ) ; BSTRIP ; break ;
448
449 case 15: /* 100000-999999.9 */
450 sprintf( lbuf , "%-9.1f" , qval ) ; BSTRIP ; break ;
451
452 case 16: /* 1000000-9999999.9 */
453 sprintf( lbuf , "%-9.0f" , qval ) ; break ;
454 }
455
456 strcpy(buf,lbuf) ; return ;
457 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment