1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 
21 #ifndef MATHLIB_H
22 #define MATHLIB_H
23 
24 #include <limits.h>
25 
26 #include "qtypes.h"
27 
28 // mathlib.h
29 
30 typedef float vec_t;
31 typedef vec_t vec3_t[3];
32 typedef vec_t vec5_t[5];
33 
34 typedef int fixed4_t;
35 typedef int fixed8_t;
36 typedef int fixed16_t;
37 #define FIXED16_MAX INT_MAX;
38 
39 /* min and max macros with type checking */
40 #define qmax(a,b) ((a > b) ? a : b)
41 #define qmin(a,b) ((a < b) ? a : b)
42 
43 /* clamp macro with type checking */
44 #define qclamp(var,min,max) qmax(qmin(var,max),min)
45 
46 #ifndef M_PI
47 #define M_PI 3.14159265358979323846	// matches value in gcc v2 math.h
48 #endif
49 
50 extern vec3_t vec3_origin;
51 extern int nanmask;
52 
53 #ifdef _MSC_VER
54 #define  IS_NAN(x) _isnan(x)
55 #else
56 #define	IS_NAN(x) (((*(int *)&x)&nanmask)==nanmask)
57 #endif
58 
59 #define DotProduct(x,y) (x[0]*y[0]+x[1]*y[1]+x[2]*y[2])
60 #define VectorSubtract(a,b,c) do {c[0]=a[0]-b[0];c[1]=a[1]-b[1];c[2]=a[2]-b[2];} while (0)
61 #define VectorAdd(a,b,c) do {c[0]=a[0]+b[0];c[1]=a[1]+b[1];c[2]=a[2]+b[2];} while (0)
62 #define VectorCopy(a,b) do {b[0]=a[0];b[1]=a[1];b[2]=a[2];} while (0)
63 
64 void VectorMA(const vec3_t veca, const float scale, const vec3_t vecb,
65 	      vec3_t vecc);
66 
67 vec_t _DotProduct(vec3_t v1, vec3_t v2);
68 void _VectorSubtract(vec3_t veca, vec3_t vecb, vec3_t out);
69 void _VectorAdd(vec3_t veca, vec3_t vecb, vec3_t out);
70 void _VectorCopy(vec3_t in, vec3_t out);
71 
72 int VectorCompare(vec3_t v1, vec3_t v2);
73 vec_t Length(vec3_t v);
74 void CrossProduct(const vec3_t v1, const vec3_t v2, vec3_t cross);
75 float VectorNormalize(vec3_t v);	// returns vector length
76 void VectorInverse(vec3_t v);
77 void VectorScale(const vec3_t in, const vec_t scale, vec3_t out);
78 int Q_log2(int val);
79 
80 void R_ConcatRotations(float in1[3][3], float in2[3][3], float out[3][3]);
81 void R_ConcatTransforms(float in1[3][4], float in2[3][4], float out[3][4]);
82 
83 void FloorDivMod(double numer, double denom, int *quotient, int *rem);
84 fixed16_t Invert24To16(fixed16_t val);
85 int GreatestCommonDivisor(int i1, int i2);
86 
87 void AngleVectors(const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
88 float anglemod(float a);
89 
90 // plane_t structure
91 // !!! if this is changed, it must be changed in asm_i386.h too !!!
92 typedef struct mplane_s {
93     vec3_t normal;
94     float dist;
95     byte type;		// for texture axis selection and fast side tests
96     byte signbits;	// signx + signy<<1 + signz<<1
97     byte pad[2];
98 } mplane_t;
99 
100 int SignbitsForPlane(const mplane_t *plane); /* sign bits for BOPS test */
101 
102 #define PSIDE_FRONT 1
103 #define PSIDE_BACK  2
104 #define PSIDE_BOTH  (PSIDE_FRONT | PSIDE_BACK)
105 
106 int BoxOnPlaneSide(const vec3_t mins, const vec3_t maxs, const mplane_t *plane);
107 #define BOX_ON_PLANE_SIDE(mins, maxs, p)			\
108 	(((p)->type < 3)?					\
109 	(							\
110 		((p)->dist <= (mins)[(p)->type])?		\
111 			PSIDE_FRONT				\
112 		:						\
113 		(						\
114 			((p)->dist >= (maxs)[(p)->type])?	\
115 				PSIDE_BACK			\
116 			:					\
117 				PSIDE_BOTH			\
118 		)						\
119 	)							\
120 	:							\
121 		BoxOnPlaneSide( (mins), (maxs), (p)))
122 
123 #ifdef QW_HACK
124 fixed16_t Mul16_30(fixed16_t multiplier, fixed16_t multiplicand);
125 #endif
126 void RotatePointAroundVector(vec3_t dst, const vec3_t dir,
127 			     const vec3_t point, float degrees);
128 
129 
130 
131 #endif /* MATHLIB_H */
132