1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // $Id: m_vectors.h 4661 2014-03-21 19:14:21Z dr_sean $
5 //
6 // Copyright (C) 1997-2000 by id Software Inc.
7 // Copyright (C) 1998-2006 by Randy Heit (ZDoom).
8 // Copyright (C) 2006-2014 by The Odamex Team.
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version 2
13 // of the License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // DESCRIPTION:
21 //	Vector math routines which I took from Quake2's source since they
22 //	make more sense than the way Doom does things. :-)
23 //
24 // [SL] 2012-02-18 - Reworked all of the functions to be more uniform with
25 // the ordering of parameters, based in part on m_vectors.cpp from Eternity
26 // Engine.  Also changed from using an array of floats as the underlying type
27 // to using a struct.
28 //
29 //-----------------------------------------------------------------------------
30 
31 
32 #ifndef __M_VECTORS_H__
33 #define __M_VECTORS_H__
34 
35 #include "tables.h"
36 #include "m_fixed.h"
37 
38 class AActor;
39 
40 struct v2fixed_t
41 {
v2fixed_tv2fixed_t42 	v2fixed_t() {}
43 
v2fixed_tv2fixed_t44 	v2fixed_t(fixed_t _x, fixed_t _y) : x(_x), y(_y) { }
45 
v2fixed_tv2fixed_t46 	v2fixed_t(const v2fixed_t& other) : x(other.x), y(other.y) { }
47 
48 	fixed_t x, y;
49 };
50 
51 struct v3fixed_t
52 {
v3fixed_tv3fixed_t53 	v3fixed_t() {}
54 
v3fixed_tv3fixed_t55 	v3fixed_t(fixed_t _x, fixed_t _y, fixed_t _z) : x(_x), y(_y), z(_z) { }
56 
v3fixed_tv3fixed_t57 	v3fixed_t(const v3fixed_t& other) : x(other.x), y(other.y), z(other.z) { }
58 
59 	fixed_t x, y, z;
60 };
61 
62 struct v3float_t
63 {
v3float_tv3float_t64 	v3float_t() {}
65 
v3float_tv3float_t66 	v3float_t(float _x, float _y, float _z) : x(_x), y(_y), z(_z) { }
67 
v3float_tv3float_t68 	v3float_t(const v3float_t& other) : x(other.x), y(other.y), z(other.z) { }
69 
70 	float x, y, z;
71 };
72 
73 struct v3double_t
74 {
v3double_tv3double_t75 	v3double_t() {}
76 
v3double_tv3double_t77 	v3double_t(double _x, double _y, double _z) : x(_x), y(_y), z(_z) { }
78 
v3double_tv3double_t79 	v3double_t(const v3float_t& other) : x(other.x), y(other.y), z(other.z) { }
80 
81 	double x, y, z;
82 };
83 
84 
85 //
86 // M_SetVec3f
87 //
88 // Sets the components of dest to the values of the parameters x, y, z
89 //
90 void M_SetVec3f(v3float_t *dest, float x, float y, float z);
91 void M_SetVec3f(v3float_t *dest, fixed_t x, fixed_t y, fixed_t z);
92 void M_SetVec3(v3double_t *dest, double x, double y, double z);
93 void M_SetVec3(v3double_t *dest, fixed_t x, fixed_t y, fixed_t z);
94 void M_SetVec2Fixed(v2fixed_t *dest, double x, double y);
95 void M_SetVec2Fixed(v2fixed_t *dest, fixed_t x, fixed_t y);
96 void M_SetVec3Fixed(v3fixed_t *dest, double x, double y, double z);
97 void M_SetVec3Fixed(v3fixed_t *dest, fixed_t x, fixed_t y, fixed_t z);
98 
99 //
100 //M_ConvertVec3FixedToVec3f
101 //
102 // Converts the component values of src to floats and stores
103 // in dest
104 //
105 void M_ConvertVec3FixedToVec3f(v3float_t *dest, const v3fixed_t *src);
106 void M_ConvertVec3FixedToVec3(v3double_t *dest, const v3fixed_t *src);
107 void M_ConvertVec3fToVec3Fixed(v3fixed_t *dest, const v3float_t *src);
108 void M_ConvertVec3ToVec3Fixed(v3fixed_t *dest, const v3double_t *src);
109 
110 //
111 // M_IsZeroVec3f
112 //
113 // Returns true if the v is the zero or null vector (all components are 0)
114 //
115 bool M_IsZeroVec3f(const v3float_t *v);
116 bool M_IsZeroVec3(const v3double_t *v);
117 bool M_IsZeroVec2Fixed(const v2fixed_t *v);
118 bool M_IsZeroVec3Fixed(const v3fixed_t *v);
119 
120 //
121 // M_ZeroVec3f
122 //
123 // Sets all components of v to 0
124 //
125 void M_ZeroVec3f(v3float_t *v);
126 void M_ZeroVec3(v3double_t *v);
127 void M_ZeroVec2Fixed(v2fixed_t *v);
128 void M_ZeroVec3Fixed(v3fixed_t *v);
129 
130 //
131 // M_AddVec3f
132 //
133 // Adds v2 to v1 stores in dest
134 // [SL] 2012-02-13 - Courtesy of Eternity Engine
135 //
136 void M_AddVec3f(v3float_t *dest, const v3float_t *v1, const v3float_t *v2);
137 void M_AddVec3 (v3double_t *dest, const v3double_t *v1, const v3double_t *v2);
138 void M_AddVec2Fixed(v2fixed_t *dest, const v2fixed_t *v1, const v2fixed_t *v2);
139 void M_AddVec3Fixed(v3fixed_t *dest, const v3fixed_t *v1, const v3fixed_t *v2);
140 
141 //
142 // M_SubVec3
143 //
144 // Subtracts v2 from v1 stores in dest
145 // [SL] 2012-02-13 - Courtesy of Eternity Engine
146 //
147 void M_SubVec3f(v3float_t *dest,  const v3float_t *v1,  const v3float_t *v2);
148 void M_SubVec3 (v3double_t *dest, const v3double_t *v1, const v3double_t *v2);
149 void M_SubVec2Fixed(v2fixed_t *dest, const v2fixed_t *v1, const v2fixed_t *v2);
150 void M_SubVec3Fixed(v3fixed_t *dest, const v3fixed_t *v1, const v3fixed_t *v2);
151 
152 //
153 // M_LengthVec3f
154 //
155 // Returns the length of a given vector (relative to the origin).  Taken from
156 // Quake 2, added by CG.
157 // [SL] 2012-02-13 - Courtesy of Eternity Engine
158 //
159 float M_LengthVec3f(const v3float_t *v);
160 double M_LengthVec3(const v3double_t *v);
161 fixed_t M_LengthVec2Fixed(const v2fixed_t *v);
162 fixed_t M_LengthVec3Fixed(const v3fixed_t *v);
163 
164 //
165 // M_ScaleVec3f
166 //
167 // Multiplies each element in the vector by scalar value a
168 // and stores in dest
169 //
170 void M_ScaleVec3f(v3float_t *dest, const v3float_t *v, float a);
171 void M_ScaleVec3 (v3double_t *dest, const v3double_t *v, double a);
172 void M_ScaleVec2Fixed(v2fixed_t *dest, const v2fixed_t *v, fixed_t a);
173 void M_ScaleVec3Fixed(v3fixed_t *dest, const v3fixed_t *v, fixed_t a);
174 
175 
176 //
177 // M_ScaleVec3fToLength
178 //
179 // Scales each element in the vector such that the vector length equals a
180 // and stores the resulting vector in dest.
181 //
182 void M_ScaleVec3fToLength(v3float_t* dest, const v3float_t* v, float a);
183 void M_ScaleVec3ToLength (v3double_t* dest, const v3double_t* v, double a);
184 void M_ScaleVec2FixedToLength(v2fixed_t * dest, const v2fixed_t* v, fixed_t a);
185 void M_ScaleVec3FixedToLength(v3fixed_t * dest, const v3fixed_t* v, fixed_t a);
186 
187 //
188 // M_DotVec3
189 //
190 // Returns the dot product of v1 and v2
191 // [SL] 2012-02-13 - Courtesy of Eternity Engine
192 //
193 float  M_DotProductVec3f(const v3float_t *v1,  const v3float_t *v2);
194 double M_DotProductVec3 (const v3double_t *v1, const v3double_t *v2);
195 
196 //
197 // M_CrossProduct3f
198 //
199 // Gets the cross product of v1 and v2 and stores in dest
200 // [SL] 2012-02-13 - Courtesy of Eternity Engine
201 //
202 void M_CrossProductVec3f(v3float_t *dest,  const v3float_t *v1,  const v3float_t *v2);
203 void M_CrossProductVec3 (v3double_t *dest, const v3double_t *v1, const v3double_t *v2);
204 
205 //
206 // M_NormalizeVec3f
207 //
208 // Scales v so that its length is 1.0 and stores in dest
209 //
210 void M_NormalizeVec3f(v3float_t *dest, const v3float_t *v);
211 void M_NormalizeVec3 (v3double_t *dest, const v3double_t *v);
212 void M_NormalizeVec2Fixed(v2fixed_t *dest, const v2fixed_t *v);
213 void M_NormalizeVec3Fixed(v3fixed_t *dest, const v3fixed_t *v);
214 
215 //
216 // M_ActorToVec3f
217 //
218 // Stores thing's position in the vector dest
219 //
220 void M_ActorPositionToVec3f(v3float_t *dest, const AActor *thing);
221 void M_ActorPositionToVec3 (v3double_t *dest, const AActor *thing);
222 void M_ActorPositionToVec2Fixed(v2fixed_t *dest, const AActor *thing);
223 void M_ActorPositionToVec3Fixed(v3fixed_t *dest, const AActor *thing);
224 
225 //
226 // M_ActorMomentumToVec3f
227 //
228 // Stores thing's momentum in the vector dest
229 //
230 void M_ActorMomentumToVec3f(v3float_t *dest, const AActor *thing);
231 void M_ActorMomentumToVec3 (v3double_t *dest, const AActor *thing);
232 void M_ActorMomentumToVec2Fixed(v2fixed_t *dest, const AActor *thing);
233 void M_ActorMomentumToVec3Fixed(v3fixed_t *dest, const AActor *thing);
234 
235 //
236 // M_AngleToVec3f
237 //
238 // Calculates the normalized direction vector from ang and pitch
239 //
240 void M_AngleToVec3f(v3float_t *dest, angle_t ang, int pitch);
241 void M_AngleToVec3(v3double_t *dest, angle_t ang, int pitch);
242 
243 void M_ProjectPointOnPlane(v3double_t *dest, const v3double_t *p, const v3double_t *normal);
244 void M_PerpendicularVec3(v3double_t *dest, const v3double_t *src);
245 void M_RotatePointAroundVector(v3double_t *dest, const v3double_t *dir, const v3double_t *point, float degrees);
246 
247 
248 //
249 // M_TranslateVec3f
250 //
251 // Rotates a vector around a point of origin
252 //
253 void M_TranslateVec3f(v3float_t *vec, const v3float_t *origin, angle_t ang);
254 void M_TranslateVec3(v3double_t *vec, const v3double_t *origin, angle_t ang);
255 
256 #endif //__M_VECTORS_H__
257 
258