1 #ifndef DEF_BASIC 2 #define DEF_BASIC 3 4 /* ---------------------------------------------------------------------------- 5 @COPYRIGHT : 6 Copyright 1993,1994,1995 David MacDonald, 7 McConnell Brain Imaging Centre, 8 Montreal Neurological Institute, McGill University. 9 Permission to use, copy, modify, and distribute this 10 software and its documentation for any purpose and without 11 fee is hereby granted, provided that the above copyright 12 notice appear in all copies. The author and McGill University 13 make no representations about the suitability of this 14 software for any purpose. It is provided "as is" without 15 express or implied warranty. 16 @VERSION : $Header: /private-cvsroot/minc/volume_io/Include/volume_io/basic.h,v 1.35 2005-05-19 21:19:27 bert Exp $ 17 ---------------------------------------------------------------------------- */ 18 19 /* ----------------------------- MNI Header ----------------------------------- 20 @NAME : basic.h 21 @INPUT : 22 @OUTPUT : 23 @RETURNS : 24 @DESCRIPTION: A set of macros and definitions useful for all MNI programs. 25 @METHOD : 26 @GLOBALS : 27 @CALLS : 28 @CREATED : July 15, 1991 David MacDonald 29 @MODIFIED : 30 ---------------------------------------------------------------------------- */ 31 32 #include <math.h> 33 #include <stdlib.h> 34 #include <stdio.h> 35 36 #ifdef __sgi 37 #include <string.h> /* --- for memcpy, etc. */ 38 #else 39 #include <memory.h> /* --- for memcpy, etc. */ 40 #endif 41 42 #include <volume_io/def_math.h> 43 #include <volume_io/system_dependent.h> 44 45 /* --------- define TRUE and FALSE ------------------------ */ 46 47 #ifndef FALSE 48 #define FALSE 0 49 #endif 50 51 #ifndef TRUE 52 #define TRUE 1 53 #endif 54 55 /* These are the internal typedefs, which are aliased to their "classic" 56 * volume_io names below, if the new VIO_PREFIX_NAMES macro is set to 57 * zero. 58 */ 59 typedef char *VIO_STR; 60 typedef int VIO_BOOL; 61 typedef double VIO_Real; 62 typedef signed char VIO_SCHAR; 63 typedef unsigned char VIO_UCHAR; 64 typedef enum { 65 OK, 66 ERROR, 67 INTERNAL_ERROR, 68 END_OF_FILE, 69 QUIT 70 } VIO_Status; 71 72 73 #if !VIO_PREFIX_NAMES /* Play nice with others */ 74 75 #ifndef __cplusplus 76 #ifndef private 77 #define private static 78 #endif /* private */ 79 #ifndef public 80 #define public 81 #endif /* public */ 82 #ifndef semiprivate 83 #define semiprivate 84 #endif /* semiprivate */ 85 #endif /* __cplusplus */ 86 87 #define OFF FALSE 88 #define ON TRUE 89 90 /* --------- macro to determine the size of a static array, 91 e.g., int array[] = { 1, 3, 9, 5 }; ------------ */ 92 93 #define SIZEOF_STATIC_ARRAY( array ) \ 94 (int) ( sizeof(array) / sizeof((array)[0])) 95 96 /* --------- interpolate between a and b ------------------- */ 97 98 #define INTERPOLATE( alpha, a, b ) ((a) + (alpha) * ((b) - (a))) 99 100 /* --------- PI, and angles -------------------------------- */ 101 102 #define PI M_PI /* from math.h */ 103 #define DEG_TO_RAD (PI / 180.0) 104 #define RAD_TO_DEG (180.0 / PI) 105 106 /* --------- Absolute value, min, and max. Bear in mind that these 107 may evaluate an expression multiple times, i.e., ABS( x - y ), 108 and therefore may be inefficient, or incorrect, 109 i.e, ABS( ++x ); ------------------ */ 110 111 #define ABS( x ) ( ((x) > 0) ? (x) : (-(x)) ) 112 #define FABS( x ) fabs( (double) x ) 113 #define SIGN( x ) ( ((x) > 0) ? 1 : (((x) < 0) ? -1 : 0) ) 114 #define FSIGN( x ) ( ((x) > 0.0) ? 1.0 : (((x) < 0.0) ? -1.0 : 0.0) ) 115 116 #ifdef MAX 117 #undef MAX 118 #endif 119 #define MAX( x, y ) ( ((x) >= (y)) ? (x) : (y) ) 120 121 #define MAX3( x, y, z ) ( ((x) >= (y)) ? MAX( x, z ) : MAX( y, z ) ) 122 123 #ifdef MIN 124 #undef MIN 125 #endif 126 #define MIN( x, y ) ( ((x) <= (y)) ? (x) : (y) ) 127 128 #define MIN3( x, y, z ) ( ((x) <= (y)) ? MIN( x, z ) : MIN( y, z ) ) 129 130 /* --------- gets the address of a 2-d array element in a 1-d array ----- */ 131 132 #define IJ( i, j, nj ) ( (i) * (nj) + (j) ) 133 134 /* --------- gets the address of a 3-d array element in a 1-d array ----- */ 135 136 #define IJK( i, j, k, nj, nk ) ( (k) + (nk) * ((j) + (nj) * (i)) ) 137 138 /* --------- environment variables -------------------------- */ 139 140 #define ENV_EXISTS( env ) ( getenv(env) != (char *) 0 ) 141 142 /* --------- C and LINT stuff -------------------------- */ 143 144 #ifdef __STDC__ 145 #define GLUE(x,y) x##y 146 #define GLUE3(x,y,z) x##y##z 147 #define CREATE_STRING(x) #x 148 #else 149 #define GLUE(x,y) x/**/y 150 #define GLUE3(x,y,z) x/**/y/**/z 151 #define CREATE_STRING(x) "x" 152 #endif 153 154 /* Basic types */ 155 156 typedef VIO_SCHAR Smallest_int; 157 typedef VIO_UCHAR unsigned_byte; 158 typedef VIO_BOOL BOOLEAN; 159 typedef VIO_Real Real; 160 typedef VIO_STR STRING; 161 typedef VIO_Status Status; 162 163 #define REAL_MAX DBL_MAX 164 165 166 /* --------------- */ 167 168 #define IS_INT( x ) ((double) (x) == (double) ((int) (x))) 169 170 #define FLOOR( x ) ((int) floor(x)) 171 172 #define ROUND( x ) FLOOR( (double) (x) + 0.5 ) 173 174 #define CEILING( x ) ((int) ceil(x)) 175 176 #define FRACTION( x ) ((double) (x) - (double) FLOOR(x)) 177 178 /* for loops */ 179 180 #define for_less( i, start, end ) for( (i) = (start); (i) < (end); ++(i) ) 181 182 #define for_down( i, start, end ) for( (i) = (start); (i) >= (end); --(i)) 183 184 #define for_inclusive( i, start, end ) \ 185 for( (i) = (start); (i) <= (end); ++(i) ) 186 187 #define for_enum( e, max, type ) \ 188 for( (e) = (type) 0; (e) < (max); (e) = (type) ((int) (e)+1) ) 189 190 #define CONVERT_INTEGER_RANGE( x1, min1, max1, min2, max2 ) \ 191 ((min2) + (2 * (x1) + 1 - 2 * (min1)) * ((max2) - (min2) + 1) / \ 192 ((max1) - (min1) + 1) / 2) 193 194 #define HANDLE_INTERNAL_ERROR( X ) \ 195 handle_internal_error( X ) 196 197 #endif /* !VIO_PREFIX_NAMES */ 198 199 #endif /* DEF_BASIC */ 200