1 #ifndef GIM_MATH_H_INCLUDED 2 #define GIM_MATH_H_INCLUDED 3 4 /*! \file gim_math.h 5 \author Francisco Le�n 6 */ 7 /* 8 ----------------------------------------------------------------------------- 9 This source file is part of GIMPACT Library. 10 11 For the latest info, see http://gimpact.sourceforge.net/ 12 13 Copyright (c) 2006 Francisco Leon. C.C. 80087371. 14 email: projectileman@yahoo.com 15 16 This library is free software; you can redistribute it and/or 17 modify it under the terms of EITHER: 18 (1) The GNU Lesser General Public License as published by the Free 19 Software Foundation; either version 2.1 of the License, or (at 20 your option) any later version. The text of the GNU Lesser 21 General Public License is included with this library in the 22 file GIMPACT-LICENSE-LGPL.TXT. 23 (2) The BSD-style license that is included with this library in 24 the file GIMPACT-LICENSE-BSD.TXT. 25 26 This library is distributed in the hope that it will be useful, 27 but WITHOUT ANY WARRANTY; without even the implied warranty of 28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files 29 GIMPACT-LICENSE-LGPL.TXT and GIMPACT-LICENSE-BSD.TXT for more details. 30 31 ----------------------------------------------------------------------------- 32 */ 33 34 #include "config.h" 35 36 #include <math.h> 37 #include <float.h> 38 #if HAVE_SYS_TYPES_H 39 #include <sys/types.h> 40 #elif defined(_MSC_VER) 41 typedef __int32 int32_t; 42 typedef unsigned __int32 uint32_t; 43 #elif defined(__GNUC__) 44 #include <inttypes.h> 45 #else 46 #error "GIMPACT: Must define int32_t and uint32_t" 47 #endif 48 49 50 /*! \defgroup BASIC_TYPES 51 Basic types and constants 52 Conventions: 53 Types starting with G 54 Constants starting with G_ 55 */ 56 //! @{ 57 /*! Types */ 58 #define GREAL float 59 #define GINT32 int32_t 60 #define GUINT32 uint32_t 61 62 #define GPTR void* 63 64 /*! Constants for integers*/ 65 #define GUINT32_BIT_COUNT 32 66 #define GUINT32_EXPONENT 5 67 68 #define G_FASTMATH 1 69 #define G_PI 3.14159265358979f 70 #define G_HALF_PI 1.5707963f 71 //267948966 72 #define G_TWO_PI 6.28318530f 73 //71795864 74 #define G_ROOT3 1.73205f 75 #define G_ROOT2 1.41421f 76 #define G_UINT_INFINITY 65534 77 #define G_REAL_INFINITY FLT_MAX 78 #define G_SIGN_BITMASK 0x80000000 79 #define G_USE_EPSILON_TEST 80 #define G_EPSILON 0.0000001f 81 //! @} 82 83 /*! \defgroup MATH_FUNCTIONS 84 mathematical functions 85 */ 86 //! @{ 87 #define G_DEGTORAD(X) ((X)*3.1415926f/180.0f) 88 #define G_RADTODEG(X) ((X)*180.0f/3.1415926f) 89 90 //! Integer representation of a floating-point value. 91 #define IR(x) ((GUINT32&)(x)) 92 93 //! Signed integer representation of a floating-point value. 94 #define SIR(x) ((GINT32&)(x)) 95 96 //! Absolute integer representation of a floating-point value 97 #define AIR(x) (IR(x)&0x7fffffff) 98 99 //! Floating-point representation of an integer value. 100 #define FR(x) ((GREAL&)(x)) 101 102 #define MAX(a,b) ((a)<(b)?(b):(a)) 103 #define MIN(a,b) ((a)>(b)?(b):(a)) 104 105 #define MAX3(a,b,c) MAX(a,MAX(b,c)) 106 #define MIN3(a,b,c) MIN(a,MIN(b,c)) 107 108 #define IS_ZERO(value) ((value) < G_EPSILON && (value) > -G_EPSILON) 109 110 #define IS_NEGATIVE(value) ((value) <= -G_EPSILON) 111 112 #define IS_POSISITVE(value) ((value) >= G_EPSILON) 113 114 ///returns a clamped number 115 #define CLAMP(number,minval,maxval) ((number)<(minval)?(minval):((number)>(maxval)?(maxval):(number))) 116 117 ///Swap numbers 118 #define SWAP_NUMBERS(a,b){ \ 119 (a) = (a)+(b); \ 120 (b) = (a)-(b); \ 121 (a) = (a)-(b); \ 122 }\ 123 124 #define GIM_INV_SQRT(va,isva)\ 125 {\ 126 if((va)<=0.0000001f)\ 127 {\ 128 (isva) = G_REAL_INFINITY;\ 129 }\ 130 else\ 131 {\ 132 GREAL _x = (va) * 0.5f;\ 133 GUINT32 _y = 0x5f3759df - ( IR(va) >> 1);\ 134 (isva) = FR(_y);\ 135 (isva) = (isva) * ( 1.5f - ( _x * (isva) * (isva) ) );\ 136 }\ 137 }\ 138 139 #define GIM_SQRT(va,sva)\ 140 {\ 141 GIM_INV_SQRT(va,sva);\ 142 (sva) = 1.0f/(sva);\ 143 }\ 144 145 //! Computes 1.0f / sqrtf(x). Comes from Quake3. See http://www.magic-software.com/3DGEDInvSqrt.html 146 GREAL gim_inv_sqrt(GREAL f); 147 148 //! Computes sqrtf(x) faster. 149 /*! 150 \sa gim_inv_sqrt 151 */ 152 GREAL gim_sqrt(GREAL f); 153 154 //!Initializes mathematical functions 155 void gim_init_math(); 156 157 //! Generates an unit random 158 GREAL gim_unit_random(); 159 //! @} 160 161 #endif // GIM_MATH_H_INCLUDED 162