[216] | 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 | |
---|
| 35 | #include <math.h> |
---|
| 36 | #include <float.h> |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | /*! \defgroup BASIC_TYPES |
---|
| 40 | Basic types and constants |
---|
| 41 | Conventions: |
---|
| 42 | Types starting with G |
---|
| 43 | Constants starting with G_ |
---|
| 44 | */ |
---|
| 45 | //! @{ |
---|
| 46 | /*! Types */ |
---|
| 47 | #define GREAL float |
---|
| 48 | #define GINT long |
---|
| 49 | #define GUINT unsigned long |
---|
| 50 | /*! Constants for integers*/ |
---|
| 51 | #define GUINT_BIT_COUNT 32 |
---|
| 52 | #define GUINT_EXPONENT 5 |
---|
| 53 | |
---|
| 54 | #define G_FASTMATH 1 |
---|
| 55 | #define G_PI 3.14159265358979f |
---|
| 56 | #define G_HALF_PI 1.5707963f |
---|
| 57 | //267948966 |
---|
| 58 | #define G_TWO_PI 6.28318530f |
---|
| 59 | //71795864 |
---|
| 60 | #define G_ROOT3 1.73205f |
---|
| 61 | #define G_ROOT2 1.41421f |
---|
| 62 | #define G_UINT_INFINITY 65534 |
---|
| 63 | #define G_REAL_INFINITY FLT_MAX |
---|
| 64 | #define G_SIGN_BITMASK 0x80000000 |
---|
| 65 | #define G_USE_EPSILON_TEST |
---|
| 66 | #define G_EPSILON 0.0000001f |
---|
| 67 | //! @} |
---|
| 68 | |
---|
| 69 | /*! \defgroup MATH_FUNCTIONS |
---|
| 70 | mathematical functions |
---|
| 71 | */ |
---|
| 72 | //! @{ |
---|
| 73 | #define G_DEGTORAD(X) ((X)*3.1415926f/180.0f) |
---|
| 74 | #define G_RADTODEG(X) ((X)*180.0f/3.1415926f) |
---|
| 75 | |
---|
| 76 | //! Integer representation of a floating-point value. |
---|
| 77 | #define IR(x) ((GUINT&)(x)) |
---|
| 78 | |
---|
| 79 | //! Signed integer representation of a floating-point value. |
---|
| 80 | #define SIR(x) ((GINT&)(x)) |
---|
| 81 | |
---|
| 82 | //! Absolute integer representation of a floating-point value |
---|
| 83 | #define AIR(x) (IR(x)&0x7fffffff) |
---|
| 84 | |
---|
| 85 | //! Floating-point representation of an integer value. |
---|
| 86 | #define FR(x) ((GREAL&)(x)) |
---|
| 87 | |
---|
| 88 | #define MAX(a,b) (a<b?b:a) |
---|
| 89 | #define MIN(a,b) (a>b?b:a) |
---|
| 90 | |
---|
| 91 | #define MAX3(a,b,c) MAX(a,MAX(b,c)) |
---|
| 92 | #define MIN3(a,b,c) MIN(a,MIN(b,c)) |
---|
| 93 | |
---|
| 94 | #define IS_ZERO(value) (value < G_EPSILON && value > -G_EPSILON) |
---|
| 95 | |
---|
| 96 | #define IS_NEGATIVE(value) (value <= -G_EPSILON) |
---|
| 97 | |
---|
| 98 | #define IS_POSISITVE(value) (value >= G_EPSILON) |
---|
| 99 | |
---|
| 100 | ///returns a clamped number |
---|
| 101 | #define CLAMP(number,minval,maxval) (number<minval?minval:(number>maxval?maxval:number)) |
---|
| 102 | |
---|
| 103 | ///Swap numbers |
---|
| 104 | #define SWAP_NUMBERS(a,b){ \ |
---|
| 105 | a = a+b; \ |
---|
| 106 | b = a-b; \ |
---|
| 107 | a = a-b; \ |
---|
| 108 | }\ |
---|
| 109 | |
---|
| 110 | #define GIM_INV_SQRT(va,isva)\ |
---|
| 111 | {\ |
---|
| 112 | if(va<=0.0000001f)\ |
---|
| 113 | {\ |
---|
| 114 | isva = G_REAL_INFINITY;\ |
---|
| 115 | }\ |
---|
| 116 | else\ |
---|
| 117 | {\ |
---|
| 118 | GREAL _x = va * 0.5f;\ |
---|
| 119 | GUINT _y = 0x5f3759df - ( IR(va) >> 1);\ |
---|
| 120 | isva = FR(_y);\ |
---|
| 121 | isva = isva * ( 1.5f - ( _x * isva * isva ) );\ |
---|
| 122 | }\ |
---|
| 123 | }\ |
---|
| 124 | |
---|
| 125 | #define GIM_SQRT(va,sva)\ |
---|
| 126 | {\ |
---|
| 127 | GIM_INV_SQRT(va,sva);\ |
---|
| 128 | sva = 1.0f/sva;\ |
---|
| 129 | }\ |
---|
| 130 | |
---|
| 131 | //! Computes 1.0f / sqrtf(x). Comes from Quake3. See http://www.magic-software.com/3DGEDInvSqrt.html |
---|
| 132 | GREAL gim_inv_sqrt(GREAL f); |
---|
| 133 | |
---|
| 134 | //! Computes sqrtf(x) faster. |
---|
| 135 | /*! |
---|
| 136 | \sa gim_inv_sqrt |
---|
| 137 | */ |
---|
| 138 | GREAL gim_sqrt(GREAL f); |
---|
| 139 | |
---|
| 140 | //!Initializes mathematical functions |
---|
| 141 | void gim_init_math(); |
---|
| 142 | |
---|
| 143 | //! Generates an unit random |
---|
| 144 | GREAL gim_unit_random(); |
---|
| 145 | //! @} |
---|
| 146 | |
---|
| 147 | #endif // GIM_MATH_H_INCLUDED |
---|