1 #ifndef VOL_IO_BASIC_H
2 #define VOL_IO_BASIC_H
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 
65 typedef enum { VIO_OK=0,
66                VIO_ERROR,
67                VIO_INTERNAL_ERROR,
68                VIO_END_OF_FILE,
69                VIO_QUIT
70              } VIO_Status;
71 
72 
73 
74 /* --------- gets the address of a 2-d array element in a 1-d array ----- */
75 
76 #define  VIO_IJ( i, j, nj )          ( (i) * (nj) + (j) )
77 
78 /* --------- gets the address of a 3-d array element in a 1-d array ----- */
79 
80 #define  VIO_IJK( i, j, k, nj, nk )  ( (k) + (nk) * ((j) + (nj) * (i)) )
81 
82 
83 /* --------- Absolute value, min, and max.  Bear in mind that these
84              may evaluate an expression multiple times, i.e., ABS( x - y ),
85              and therefore may be inefficient, or incorrect,
86              i.e, ABS( ++x );                          ------------------ */
87 
88 #define  VIO_ABS( x )   ( ((x) > 0) ? (x) : (-(x)) )
89 #define  VIO_FABS( x )  fabs( (double) x )
90 #define  VIO_SIGN( x )  ( ((x) > 0) ? 1 : (((x) < 0) ? -1 : 0) )
91 #define  VIO_FSIGN( x ) ( ((x) > 0.0) ? 1.0 : (((x) < 0.0) ? -1.0 : 0.0) )
92 
93 #ifdef   MAX
94 #undef   MAX
95 #endif
96 #define  MAX( x, y )  ( ((x) >= (y)) ? (x) : (y) )
97 
98 #define  MAX3( x, y, z )  ( ((x) >= (y)) ? MAX( x, z ) : MAX( y, z ) )
99 
100 #ifdef   MIN
101 #undef   MIN
102 #endif
103 #define  MIN( x, y )  ( ((x) <= (y)) ? (x) : (y) )
104 
105 #define  MIN3( x, y, z )  ( ((x) <= (y)) ? MIN( x, z ) : MIN( y, z ) )
106 
107 
108 #define  VIO_IS_INT( x )    ((double) (x) == (double) ((int) (x)))
109 
110 #define  VIO_FLOOR( x )     ((long) floor(x))
111 
112 #define  VIO_ROUND( x )     VIO_FLOOR( (double) (x) + 0.5 )
113 
114 #define  VIO_CEILING( x )   ((long) ceil(x))
115 
116 #define  VIO_FRACTION( x )  ((double) (x) - (double) VIO_FLOOR(x))
117 
118 #define  VIO_ENV_EXISTS( env ) ( getenv(env) != (char *) 0 )
119 
120 /* --------- macro to determine the size of a static array,
121              e.g.,   int  array[] = { 1, 3, 9, 5 };           ------------ */
122 
123 #define  VIO_SIZEOF_STATIC_ARRAY( array ) \
124          (int) ( sizeof(array) / sizeof((array)[0]))
125 
126 /* --------- interpolate between a and b ------------------- */
127 
128 #define  VIO_INTERPOLATE( alpha, a, b ) ((a) + (alpha) * ((b) - (a)))
129 
130 
131 #define  VIO_DEG_TO_RAD   (M_PI / 180.0)
132 #define  VIO_RAD_TO_DEG   (180.0 / M_PI)
133 
134 
135 /* for loops */
136 
137 #define  for_less( i, start, end )  for( (i) = (start);  (i) < (end);  ++(i) )
138 
139 #define  for_down( i, start, end )  for( (i) = (start);  (i) >= (end); --(i))
140 
141 #define  for_inclusive( i, start, end )  \
142                    for( (i) = (start);  (i) <= (end);  ++(i) )
143 
144 #define  for_enum( e, max, type )  \
145                 for( (e) = (type) 0;  (e) < (max);  (e) = (type) ((int) (e)+1) )
146 
147 #define  CONVERT_INTEGER_RANGE( x1, min1, max1, min2, max2 )                  \
148               ((min2) + (2 * (x1) + 1 - 2 * (min1)) * ((max2) - (min2) + 1) / \
149                                                       ((max1) - (min1) + 1) / 2)
150 
151 #define  HANDLE_INTERNAL_ERROR( X )                                           \
152          handle_internal_error( X )
153 
154 #endif /* VOL_IO_BASIC_H */
155