1 /*
2 Copyright (C) 1996-2001 Id Software, Inc.
3 Copyright (C) 2002-2009 John Fitzgibbons and others
4 Copyright (C) 2007-2008 Kristian Duske
5 Copyright (C) 2010-2014 QuakeSpasm developers
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but 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
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 
22 */
23 
24 #ifndef __MATHLIB_H
25 #define __MATHLIB_H
26 
27 // mathlib.h
28 
29 #include <math.h>
30 
31 #ifndef M_PI
32 #define M_PI		3.14159265358979323846	// matches value in gcc v2 math.h
33 #endif
34 
35 #define M_PI_DIV_180 (M_PI / 180.0) //johnfitz
36 
37 struct mplane_s;
38 
39 extern vec3_t vec3_origin;
40 
41 #define	nanmask		(255 << 23)	/* 7F800000 */
42 #if 0	/* macro is violating strict aliasing rules */
43 #define	IS_NAN(x)	(((*(int *) (char *) &x) & nanmask) == nanmask)
44 #else
IS_NAN(float x)45 static inline int IS_NAN (float x) {
46 	union { float f; int i; } num;
47 	num.f = x;
48 	return ((num.i & nanmask) == nanmask);
49 }
50 #endif
51 
52 #define Q_rint(x) ((x) > 0 ? (int)((x) + 0.5) : (int)((x) - 0.5)) //johnfitz -- from joequake
53 
54 #define DotProduct(x,y) ((x)[0]*(y)[0]+(x)[1]*(y)[1]+(x)[2]*(y)[2])
55 #define DotProduct2(x,y) ((x)[0]*(y)[0]+(x)[1]*(y)[1])
56 #define DoublePrecisionDotProduct(x,y) ((double)(x)[0]*(y)[0]+(double)(x)[1]*(y)[1]+(double)(x)[2]*(y)[2])
57 #define VectorSubtract(a,b,c) {(c)[0]=(a)[0]-(b)[0];(c)[1]=(a)[1]-(b)[1];(c)[2]=(a)[2]-(b)[2];}
58 #define VectorAdd(a,b,c) {(c)[0]=(a)[0]+(b)[0];(c)[1]=(a)[1]+(b)[1];(c)[2]=(a)[2]+(b)[2];}
59 #define VectorCopy(a,b) {(b)[0]=(a)[0];(b)[1]=(a)[1];(b)[2]=(a)[2];}
60 
61 //johnfitz -- courtesy of lordhavoc
62 // QuakeSpasm: To avoid strict aliasing violations, use a float/int union instead of type punning.
63 #define VectorNormalizeFast(_v)\
64 {\
65 	union { float f; int i; } _y, _number;\
66 	_number.f = DotProduct((_v), (_v));\
67 	if (_number.f != 0.0)\
68 	{\
69 		_y.i = 0x5f3759df - (_number.i >> 1);\
70 		_y.f = _y.f * (1.5f - (_number.f * 0.5f * _y.f * _y.f));\
71 		VectorScale((_v), _y.f, (_v));\
72 	}\
73 }
74 
75 void TurnVector (vec3_t out, const vec3_t forward, const vec3_t side, float angle); //johnfitz
76 void VectorAngles (const vec3_t forward, float *up, vec3_t angles); //johnfitz, spike(up is optional)
77 
78 void VectorMA (vec3_t veca, float scale, vec3_t vecb, vec3_t vecc);
79 
80 vec_t _DotProduct (vec3_t v1, vec3_t v2);
81 void _VectorSubtract (vec3_t veca, vec3_t vecb, vec3_t out);
82 void _VectorAdd (vec3_t veca, vec3_t vecb, vec3_t out);
83 void _VectorCopy (vec3_t in, vec3_t out);
84 
85 int VectorCompare (vec3_t v1, vec3_t v2);
86 vec_t VectorLength (vec3_t v);
87 void CrossProduct (const vec3_t v1, const vec3_t v2, vec3_t cross);
88 float VectorNormalize (vec3_t v);		// returns vector length
89 void VectorInverse (vec3_t v);
90 void VectorScale (vec3_t in, vec_t scale, vec3_t out);
91 int Q_log2(int val);
92 int Q_nextPow2(int val);
93 
94 void R_ConcatRotations (float in1[3][3], float in2[3][3], float out[3][3]);
95 void R_ConcatTransforms (float in1[3][4], float in2[3][4], float out[3][4]);
96 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
97 
98 void FloorDivMod (double numer, double denom, int *quotient,
99 		int *rem);
100 fixed16_t Invert24To16(fixed16_t val);
101 int GreatestCommonDivisor (int i1, int i2);
102 
103 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
104 int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct mplane_s *plane);
105 float	anglemod(float a);
106 
107 
108 void MatrixMultiply(float left[16], float right[16]);
109 void RotationMatrix(float matrix[16], float angle, float x, float y, float z);
110 void TranslationMatrix(float matrix[16], float x, float y, float z);
111 void ScaleMatrix(float matrix[16], float x, float y, float z);
112 void IdentityMatrix(float matrix[16]);
113 
114 qboolean IsAxisAlignedDeg(vec3_t angle);
115 qboolean IsOriginWithinMinMax(vec3_t origin, vec3_t mins, vec3_t maxs);
116 
117 #define BOX_ON_PLANE_SIDE(emins, emaxs, p)	\
118 	(((p)->type < 3)?						\
119 	(										\
120 		((p)->dist <= (emins)[(p)->type])?	\
121 			1								\
122 		:									\
123 		(									\
124 			((p)->dist >= (emaxs)[(p)->type])?\
125 				2							\
126 			:								\
127 				3							\
128 		)									\
129 	)										\
130 	:										\
131 		BoxOnPlaneSide( (emins), (emaxs), (p)))
132 
133 /*==========================================================================*/
134 
135 /* SIMD */
136 #if (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))) || (defined(__GNUC__) && defined(__SSE__) && defined(__SSE2__))
137 	#define USE_SIMD
138 	#define USE_SSE2
139 	#include <emmintrin.h>
140 #endif
141 
142 /*==========================================================================*/
143 
144 #endif	/* __MATHLIB_H */
145 
146