1 /* 2 * mathlib.h -- math primitives 3 * $Id: mathlib.h,v 1.6 2008-03-06 18:55:07 sezero Exp $ 4 * 5 * Copyright (C) 1996-1997 Id Software, Inc. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or (at 10 * your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 15 * 16 * See the GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License along 19 * with this program; if not, write to the Free Software Foundation, Inc., 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 23 #ifndef __MATHLIB_H 24 #define __MATHLIB_H 25 26 #include <math.h> 27 28 #define SIDE_FRONT 0 29 #define SIDE_ON 2 30 #define SIDE_BACK 1 31 #define SIDE_CROSS -2 32 33 #ifndef M_PI 34 #define M_PI 3.14159265358979323846 /* matches value in gcc v2 math.h */ 35 #endif 36 #define Q_PI M_PI 37 38 #define EQUAL_EPSILON 0.001 39 40 extern vec3_t vec3_origin; 41 42 qboolean VectorCompare (vec3_t v1, vec3_t v2); 43 44 #define DotProduct(x,y) ((x)[0] * (y)[0] + (x)[1] * (y)[1] + (x)[2] * (y)[2]) 45 #define VectorLength(a) sqrt(DotProduct((a),(a))) 46 47 #define CrossProduct(v1,v2,cross) \ 48 do { \ 49 (cross)[0] = (v1)[1] * (v2)[2] - (v1)[2] * (v2)[1]; \ 50 (cross)[1] = (v1)[2] * (v2)[0] - (v1)[0] * (v2)[2]; \ 51 (cross)[2] = (v1)[0] * (v2)[1] - (v1)[1] * (v2)[0]; \ 52 } while (0) 53 54 #define VectorAdd(a,b,c) \ 55 do { \ 56 (c)[0] = (a)[0] + (b)[0]; \ 57 (c)[1] = (a)[1] + (b)[1]; \ 58 (c)[2] = (a)[2] + (b)[2]; \ 59 } while (0) 60 61 #define VectorSubtract(a,b,c) \ 62 do { \ 63 (c)[0] = (a)[0] - (b)[0]; \ 64 (c)[1] = (a)[1] - (b)[1]; \ 65 (c)[2] = (a)[2] - (b)[2]; \ 66 } while (0) 67 68 #define VectorInverse(v) \ 69 do { \ 70 (v)[0] = -(v)[0]; \ 71 (v)[1] = -(v)[1]; \ 72 (v)[2] = -(v)[2]; \ 73 } while (0) 74 75 #define VectorClear(a) ((a)[2] = (a)[1] = (a)[0] = 0) 76 #if 0 77 #define VectorCopy(a,b) memcpy((b),(a),sizeof(vec3_t)) 78 #else 79 #define VectorCopy(a,b) \ 80 do { \ 81 (b)[0] = (a)[0]; \ 82 (b)[1] = (a)[1]; \ 83 (b)[2] = (a)[2]; \ 84 } while (0) 85 #endif 86 87 #define VectorSet(v,a,b,c) \ 88 do { \ 89 (v)[0] = (a); \ 90 (v)[1] = (b); \ 91 (v)[2] = (c); \ 92 } while (0) 93 94 #if 0 95 #define VectorNegate(a,b) VectorSubtract(vec3_origin,(a),(b)) 96 #else 97 #define VectorNegate(a,b) \ 98 do { \ 99 (b)[0] = -(a)[0]; \ 100 (b)[1] = -(a)[1]; \ 101 (b)[2] = -(a)[2]; \ 102 } while (0) 103 #endif 104 105 #define VectorScale(a,b,c) \ 106 do { \ 107 (c)[0] = (a)[0] * (b); \ 108 (c)[1] = (a)[1] * (b); \ 109 (c)[2] = (a)[2] * (b); \ 110 } while (0) 111 112 /* VectorMA(vec3_t,double,vec3_t,vec3_t) */ 113 #define VectorMA(a,s,b,c) \ 114 do { \ 115 (c)[0] = (vec_t)((a)[0] + (s) * (b)[0]); \ 116 (c)[1] = (vec_t)((a)[1] + (s) * (b)[1]); \ 117 (c)[2] = (vec_t)((a)[2] + (s) * (b)[2]); \ 118 } while (0) 119 120 vec_t VectorNormalize (vec3_t v); 121 122 vec_t Q_rint (vec_t in); 123 124 #endif /* __MATHLIB_H */ 125 126