Blender  V2.59
BLI_utildefines.h
Go to the documentation of this file.
00001 /*
00002  * $Id: BLI_utildefines.h 37276 2011-06-06 22:10:05Z psy-fi $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028 */
00029 
00030 #ifndef BLI_UTILDEFINES_H
00031 #define BLI_UTILDEFINES_H
00032 
00037 #ifndef FALSE
00038 #define FALSE 0
00039 #endif
00040 
00041 #ifndef TRUE
00042 #define TRUE 1
00043 #endif
00044 
00045 
00046 #define ELEM(a, b, c)           ( (a)==(b) || (a)==(c) )
00047 #define ELEM3(a, b, c, d)       ( ELEM(a, b, c) || (a)==(d) )
00048 #define ELEM4(a, b, c, d, e)    ( ELEM(a, b, c) || ELEM(a, d, e) )
00049 #define ELEM5(a, b, c, d, e, f) ( ELEM(a, b, c) || ELEM3(a, d, e, f) )
00050 #define ELEM6(a, b, c, d, e, f, g)      ( ELEM(a, b, c) || ELEM4(a, d, e, f, g) )
00051 #define ELEM7(a, b, c, d, e, f, g, h)   ( ELEM3(a, b, c, d) || ELEM4(a, e, f, g, h) )
00052 #define ELEM8(a, b, c, d, e, f, g, h, i)        ( ELEM4(a, b, c, d, e) || ELEM4(a, f, g, h, i) )
00053 #define ELEM9(a, b, c, d, e, f, g, h, i, j)        ( ELEM4(a, b, c, d, e) || ELEM5(a, f, g, h, i, j) )
00054 #define ELEM10(a, b, c, d, e, f, g, h, i, j, k)        ( ELEM4(a, b, c, d, e) || ELEM6(a, f, g, h, i, j, k) )
00055 #define ELEM11(a, b, c, d, e, f, g, h, i, j, k, l)        ( ELEM4(a, b, c, d, e) || ELEM7(a, f, g, h, i, j, k, l) )
00056 
00057 /* shift around elements */
00058 #define SHIFT3(type, a, b, c) { type tmp; tmp = a; a = c; c = b; b = tmp; }
00059 #define SHIFT4(type, a, b, c, d) { type tmp; tmp = a; a = d; d = c; c = b; b = tmp; }
00060 
00061 /* min/max */
00062 #define MIN2(x,y)               ( (x)<(y) ? (x) : (y) )
00063 #define MIN3(x,y,z)             MIN2( MIN2((x),(y)) , (z) )
00064 #define MIN4(x,y,z,a)           MIN2( MIN2((x),(y)) , MIN2((z),(a)) )
00065 
00066 #define MAX2(x,y)               ( (x)>(y) ? (x) : (y) )
00067 #define MAX3(x,y,z)             MAX2( MAX2((x),(y)) , (z) )
00068 #define MAX4(x,y,z,a)           MAX2( MAX2((x),(y)) , MAX2((z),(a)) )
00069 
00070 #define INIT_MINMAX(min, max) { (min)[0]= (min)[1]= (min)[2]= 1.0e30f; (max)[0]= (max)[1]= (max)[2]= -1.0e30f; }
00071 
00072 #define INIT_MINMAX2(min, max) { (min)[0]= (min)[1]= 1.0e30f; (max)[0]= (max)[1]= -1.0e30f; }
00073 
00074 #define DO_MIN(vec, min) { if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0];      \
00075                                                           if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1];   \
00076                                                           if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2]; } \
00077 
00078 #define DO_MAX(vec, max) { if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0];          \
00079                                                           if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1];   \
00080                                                           if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2]; } \
00081 
00082 #define DO_MINMAX(vec, min, max) { if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \
00083                                                           if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \
00084                                                           if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2]; \
00085                                                           if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \
00086                                                           if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; \
00087                                                           if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2]; } \
00088 
00089 #define DO_MINMAX2(vec, min, max) { if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \
00090                                                           if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \
00091                                                           if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \
00092                                                           if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; }
00093 
00094 /* some math and copy defines */
00095 
00096 #ifndef SWAP
00097 #define SWAP(type, a, b)        { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; }
00098 #endif
00099 
00100 #define ABS(a)                                  ( (a)<0 ? (-(a)) : (a) )
00101 
00102 #define AVG2(x, y)              ( 0.5 * ((x) + (y)) )
00103 
00104 #define FTOCHAR(val) ((val)<=0.0f)? 0 : (((val)>(1.0f-0.5f/255.0f))? 255 : (char)((255.0f*(val))+0.5f))
00105 #define FTOUSHORT(val) ((val >= 1.0f-0.5f/65535)? 65535: (val <= 0.0f)? 0: (unsigned short)(val*65535.0f + 0.5f))
00106 #define F3TOCHAR3(v2,v1) (v1)[0]=FTOCHAR((v2[0])); (v1)[1]=FTOCHAR((v2[1])); (v1)[2]=FTOCHAR((v2[2]))
00107 #define F3TOCHAR4(v2,v1) { (v1)[0]=FTOCHAR((v2[0])); (v1)[1]=FTOCHAR((v2[1])); (v1)[2]=FTOCHAR((v2[2])); \
00108                                                 (v1)[3]=FTOCHAR((v2[3])); (v1)[3] = 255; }
00109 #define F4TOCHAR4(v2,v1) { (v1)[0]=FTOCHAR((v2[0])); (v1)[1]=FTOCHAR((v2[1])); (v1)[2]=FTOCHAR((v2[2])); \
00110                                                 (v1)[3]=FTOCHAR((v2[3])); (v1)[3]=FTOCHAR((v2[3])); }
00111 
00112 
00113 #define VECCOPY(v1,v2)          {*(v1)= *(v2); *(v1+1)= *(v2+1); *(v1+2)= *(v2+2);}
00114 #define VECCOPY2D(v1,v2)          {*(v1)= *(v2); *(v1+1)= *(v2+1);}
00115 #define QUATCOPY(v1,v2)         {*(v1)= *(v2); *(v1+1)= *(v2+1); *(v1+2)= *(v2+2); *(v1+3)= *(v2+3);}
00116 #define LONGCOPY(a, b, c)       {int lcpc=c, *lcpa=(int *)a, *lcpb=(int *)b; while(lcpc-->0) *(lcpa++)= *(lcpb++);}
00117 
00118 
00119 #define VECADD(v1,v2,v3)        {*(v1)= *(v2) + *(v3); *(v1+1)= *(v2+1) + *(v3+1); *(v1+2)= *(v2+2) + *(v3+2);}
00120 #define VECSUB(v1,v2,v3)        {*(v1)= *(v2) - *(v3); *(v1+1)= *(v2+1) - *(v3+1); *(v1+2)= *(v2+2) - *(v3+2);}
00121 #define VECSUB2D(v1,v2,v3)      {*(v1)= *(v2) - *(v3); *(v1+1)= *(v2+1) - *(v3+1);}
00122 #define VECADDFAC(v1,v2,v3,fac) {*(v1)= *(v2) + *(v3)*(fac); *(v1+1)= *(v2+1) + *(v3+1)*(fac); *(v1+2)= *(v2+2) + *(v3+2)*(fac);}
00123 #define VECSUBFAC(v1,v2,v3,fac) {*(v1)= *(v2) - *(v3)*(fac); *(v1+1)= *(v2+1) - *(v3+1)*(fac); *(v1+2)= *(v2+2) - *(v3+2)*(fac);}
00124 #define QUATADDFAC(v1,v2,v3,fac) {*(v1)= *(v2) + *(v3)*(fac); *(v1+1)= *(v2+1) + *(v3+1)*(fac); *(v1+2)= *(v2+2) + *(v3+2)*(fac); *(v1+3)= *(v2+3) + *(v3+3)*(fac);}
00125 
00126 #define INPR(v1, v2)            ( (v1)[0]*(v2)[0] + (v1)[1]*(v2)[1] + (v1)[2]*(v2)[2] )
00127 
00128 /* some misc stuff.... */
00129 #define CLAMP(a, b, c)          if((a)<(b)) (a)=(b); else if((a)>(c)) (a)=(c)
00130 #define CLAMPIS(a, b, c) ((a)<(b) ? (b) : (a)>(c) ? (c) : (a))
00131 #define CLAMPTEST(a, b, c)      if((b)<(c)) {CLAMP(a, b, c);} else {CLAMP(a, c, b);}
00132 
00133 #define IS_EQ(a,b) ((fabs((double)(a)-(b)) >= (double) FLT_EPSILON) ? 0 : 1)
00134 #define IS_EQF(a,b) ((fabsf((float)(a)-(b)) >= (float) FLT_EPSILON) ? 0 : 1)
00135 
00136 #define IS_EQT(a, b, c) ((a > b)? (((a-b) <= c)? 1:0) : ((((b-a) <= c)? 1:0)))
00137 #define IN_RANGE(a, b, c) ((b < c)? ((b<a && a<c)? 1:0) : ((c<a && a<b)? 1:0))
00138 #define IN_RANGE_INCL(a, b, c) ((b < c)? ((b<=a && a<=c)? 1:0) : ((c<=a && a<=b)? 1:0))
00139 
00140 /* array helpers */
00141 #define ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot)           (arr_dtype *)((char*)arr_start + (elem_size*(tot - 1)))
00142 #define ARRAY_HAS_ITEM(item, arr_start, arr_dtype, elem_size, tot)      ((item >= arr_start) && (item <= ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot)))
00143 
00144 /* This one rotates the bytes in an int64, int (32) and short (16) */
00145 #define SWITCH_INT64(a) { \
00146         char s_i, *p_i; \
00147         p_i= (char *)&(a); \
00148         s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \
00149         s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \
00150         s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \
00151         s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; }
00152 
00153                 #define SWITCH_INT(a) { \
00154         char s_i, *p_i; \
00155         p_i= (char *)&(a); \
00156         s_i=p_i[0]; p_i[0]=p_i[3]; p_i[3]=s_i; \
00157         s_i=p_i[1]; p_i[1]=p_i[2]; p_i[2]=s_i; }
00158 
00159 #define SWITCH_SHORT(a) { \
00160         char s_i, *p_i; \
00161         p_i= (char *)&(a); \
00162         s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i; }
00163 
00164 
00165 /* Warning-free macros for storing ints in pointers. Use these _only_
00166  * for storing an int in a pointer, not a pointer in an int (64bit)! */
00167 #define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
00168 #define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
00169 
00170 /* Macro to convert a value to string in the preprocessor
00171  * STRINGIFY_ARG: gives the defined name in the string
00172  * STRINGIFY: gives the defined value. */
00173 #define STRINGIFY_ARG(x) #x
00174 #define STRINGIFY(x) STRINGIFY_ARG(x)
00175 
00176 /* useful for debugging */
00177 #define AT __FILE__ ":" STRINGIFY(__LINE__)
00178 
00179 /* UNUSED macro, for function argument */
00180 #ifdef __GNUC__
00181 #  define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
00182 #else
00183 #  define UNUSED(x) UNUSED_ ## x
00184 #endif
00185 
00186 #ifdef __GNUC__
00187 #  define WARN_UNUSED  __attribute__((warn_unused_result))
00188 #else
00189 #  define WARN_UNUSED
00190 #endif
00191 
00192 /*little macro so inline keyword works*/
00193 #if defined(_MSC_VER)
00194 #  define BM_INLINE static __forceinline
00195 #elif defined(__GNUC__)
00196 #  define BM_INLINE static inline __attribute((always_inline))
00197 #else
00198 /* #warning "MSC/GNUC defines not found, inline non-functional" */
00199 #  define BM_INLINE static
00200 #endif
00201 
00202 
00203 /* BLI_assert(), default only to print
00204  * for aborting need to define WITH_ASSERT_ABORT
00205  */
00206 #if !defined NDEBUG
00207 #  ifdef WITH_ASSERT_ABORT
00208 #    define _dummy_abort abort
00209 #  else
00210 #    define _dummy_abort() (void)0
00211 #  endif
00212 #  ifdef __GNUC__ /* just want to check if __func__ is available */
00213 #    define BLI_assert(a) \
00214 do { \
00215         if (!(a)) { \
00216                 fprintf(stderr, \
00217                         "BLI_assert failed: %s, %s(), %d at \'%s\'\n", \
00218                         __FILE__, __func__, __LINE__, STRINGIFY(a)); \
00219                 _dummy_abort(); \
00220         } \
00221 } while (0)
00222 #  else
00223 #    define BLI_assert(a) \
00224 do { \
00225         if (0 == (a)) { \
00226                 fprintf(stderr, \
00227                         "BLI_assert failed: %s, %d at \'%s\'\n", \
00228                         __FILE__, __LINE__, STRINGIFY(a)); \
00229                 _dummy_abort(); \
00230         } \
00231 } while (0)
00232 #  endif
00233 #else
00234 #  define BLI_assert(a) (void)0
00235 #endif
00236 
00237 #endif // BLI_UTILDEFINES_H