Debian Codegraph @

lib/vecmat.h

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/lib/vecmat.h $
21 * $Revision: 21 $
22 * $Date: 1/21/99 11:16p $
23 * $Author: Jeff $
24 *
25 * Vector/Matrix routines
26 *
27 * $Log: /DescentIII/Main/lib/vecmat.h $
28 *
29 * 21 1/21/99 11:16p Jeff
30 * pulled out some structs and defines from header files and moved them
31 * into seperate header files so that multiplayer dlls don't require major
32 * game headers, just those new headers. Side effect is a shorter build
33 * time. Also cleaned up some header file #includes that weren't needed.
34 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
35 * manage.h and multi.h
36 *
37 * 20 1/11/99 4:45p Jason
38 * added first pass at katmai support
39 *
40 * 19 1/01/99 4:10p Chris
41 * Added some const parameters, improved ray cast object collide/rejection
42 * code
43 *
44 * 18 5/25/98 3:45p Jason
45 * added vm_GetCentroidFast
46 *
47 * 17 3/12/98 7:30p Chris
48 * Added ObjSetOrient
49 *
50 * 16 2/08/98 6:01p Matt
51 * Added functions to multiply by a transposed matrix, and simplified some
52 * other code a bit.
53 *
54 * 15 2/06/98 11:02a Matt
55 * Added missing include
56 *
57 * 14 2/06/98 10:43a Matt
58 * Made vm_VectorToMatrix() take any one or two vectors, & not require
59 * forward vec.
60 * Also, made the uvec and rvec parameters default to NULL if not
61 * specified.
62 *
63 * 13 2/02/98 8:17p Chris
64 * Added a != operator and a Zero_vector constant
65 *
66 * 12 1/20/98 4:04p Matt
67 * Made vm_GetNormalizedDir() and vm_GetNormalizeDirFast() return the
68 * distance between the two input points.
69 *
70 * 11 1/13/98 1:30p Jason
71 * changed vm_GetCentroid to also return the size of the area
72 *
73 * 10 11/04/97 6:21p Chris
74 * Allowed other files to use the vm_DeltaAngVecNorm function
75 *
76 * 9 10/25/97 7:15p Jason
77 * implemented vm_ComputeBoundingSphere
78 *
79 * 8 10/14/97 4:35p Samir
80 * Added vm_MakeRandomVector.
81 *
82 * 7 9/23/97 2:26p Matt
83 * Made vm_GetNormal() return the magnitude of the normal (before it was
84 * normalized)
85 *
86 * 6 8/28/97 4:56p Jason
87 * implemented vm_GetCentroid
88 *
89 * 5 8/18/97 6:39p Matt
90 * Added vm_VectorAngleToMatrix()
91 *
92 * 4 8/04/97 12:36p Chris
93 * Added an == operator for vectors
94 *
95 * 3 7/17/97 3:56p Matt
96 * Added vm_Orthogonalize()
97 *
98 * 2 7/16/97 5:15p Chris
99 * Moved the XYZ() macro to vecmat.h
100 *
101 * 1 6/23/97 9:25p Samir
102 * added because source safe sucks
103 *
104 * 31 4/18/97 2:14p Samir
105 * Added vm_DeltaAngVec.
106 *
107 * 30 2/27/97 6:16p Chris
108 * Added the vector_array type
109 *
110 * 29 2/27/97 6:08 PM Jeremy
111 * added prototypes for vm_MakeInverseMatrix and vm_SinCosToMatrix
112 *
113 * 28 2/27/97 5:23p Chris
114 * Removed the remainding extern inline
115 *
116 * 27 2/27/97 4:56p Samir
117 * took out ifndef MAC stuff
118 *
119 * 26 2/27/97 1:40p Chris
120 * Added a function to compute the determinate --
121 * BTW on the last rev. I moved all inline functions
122 * to the header. (So they will be inlined)
123 *
124 * 25 2/26/97 7:33p Chris
125 *
126 * 24 2/26/97 6:17 PM Jeremy
127 * put #pragma warning inside #ifndef macintosh
128 *
129 * 23 2/20/97 11:41a Chris
130 * Added a negate unary operator for vectors
131 *
132 * 22 2/12/97 5:28p Jason
133 * implemented ExtractAnglesFromMatrix function
134 *
135 * 21 2/11/97 6:49p Matt
136 * Added vm_VectorToMatrix()
137 * Made vm_NormalizeVector() return the old vector mag
138 * Fixed bug in inline version of crossprod
139 *
140 * 20 2/11/97 11:54a Jason
141 *
142 * 19 2/10/97 3:36p Matt
143 * Fixed (added) IDENTITY_MATRIX define
144 *
145 * 18 2/07/97 5:38p Matt
146 * Moved fixed-point math funcs to fix.lib
147 *
148 * $NoKeywords: $
149 */
150
151 #ifndef VECMAT_H
152 #define VECMAT_Hcxx . . $ `lib/vecmat.h:152:9`!
153
154 #include <cmath>
155
156 #include "fix.h"cxx . . $ `<file>/fix/fix.h`/
157
158 // All structs, defines and inline functions are located in vecmat_external.h
159 // vecmat_external.h is where anything that can be used by DLLs should be.
160 #include "vecmat_external.h"cxx . . $ `<file>/lib/vecmat_external.h`/
161
162 extern const vectorcxx . . $ vector# Zero_vectorcxx . . $ Zero_vector.;
163 extern const matrixcxx . . $ matrix# Identity_matrixcxx . . $ Identity_matrix.;
164
165 // Used for debugging. It is used in printf's so we do not have to write out the structure 3 times
166 // to print all the coordinates.
167 #define XYZcxx . . $ `lib/vecmat.h:167:9`!(v) (v)->x, (v)->y, (v)->z
168
169 // Given a matrix, makes it an identity matrix
170 extern void vm_MakeIdentitycxx . . $ vm_MakeIdentity(dca75c48c9ab5b54).(matrixcxx . . $ matrix# *);
171
172 // Set a vector to {0,0,0}
173 extern void vm_MakeZerocxx . . $ vm_MakeZero(ec8cf6451c36d470).(vectorcxx . . $ vector# *v);
174
175 // Set an angvec to {0,0,0}
176 extern void vm_MakeZerocxx . . $ vm_MakeZero(7b076424e81684e7).(angveccxx . . $ angvec# *a);
177
178 // Rotates a vector thru a matrix
179 extern void vm_MatrixMulVectorcxx . . $ vm_MatrixMulVector(76352432a16beb64).(vectorcxx . . $ vector# *, vectorcxx . . $ vector# *, matrixcxx . . $ matrix# *);
180
181 // Multiply a vector times the transpose of a matrix
182 void vm_VectorMulTMatrixcxx . . $ vm_VectorMulTMatrix(76352432a16beb64).(vectorcxx . . $ vector# *result, vectorcxx . . $ vector# *v, matrixcxx . . $ matrix# *m);
183
184 // Multiplies 2 3x3 matrixes, returning the result in first argument
185 extern void vm_MatrixMulcxx . . $ vm_MatrixMul(3d6e518f7ec8c77c).(matrixcxx . . $ matrix# *, matrixcxx . . $ matrix# *, matrixcxx . . $ matrix# *);
186
187 // Multiply a matrix times the transpose of a matrix
188 void vm_MatrixMulTMatrixcxx . . $ vm_MatrixMulTMatrix(3d6e518f7ec8c77c).(matrixcxx . . $ matrix# *dest, matrixcxx . . $ matrix# *src0, matrixcxx . . $ matrix# *src1);
189
190 // Computes all math look up tables, must be called before any vector stuff is used
191 extern void vm_InitMathTablescxx . . $ vm_InitMathTables(49f6e7a06ebc5aa8).();
192
193 // Given a vector, returns the magnitude. Uses sqrt so it's slow
194 extern float vm_GetMagnitudecxx . . $ vm_GetMagnitude(4939a7e530383cd4).(vectorcxx . . $ vector# *);
195
196 // Given a vector, returns an approximation of the magnitude
197 extern float vm_GetMagnitudeFastcxx . . $ vm_GetMagnitudeFast(4939a7e530383cd4).(vectorcxx . . $ vector# *);
198
199 // Returns the dot product of the two given vectors
200 extern float vm_DotProductcxx . . $ vm_DotProduct(e65d563e2b53cc54).(const vectorcxx . . $ vector# *, const vectorcxx . . $ vector# *);
201
202 // Returns a perpendicular vector to the two given vectors
203 extern void vm_CrossProductcxx . . $ vm_CrossProduct(af83aca25ba5b41c).(vectorcxx . . $ vector# *, vectorcxx . . $ vector# *, vectorcxx . . $ vector# *);
204
205 // Returns the difference between two vectors
206 extern void vm_SubVectorscxx . . $ vm_SubVectors(24e6685d32847ac5).(vectorcxx . . $ vector# *, const vectorcxx . . $ vector# *, const vectorcxx . . $ vector# *);
207
208 // Returns adds two vectors, returns result in first arg
209 extern void vm_AddVectorscxx . . $ vm_AddVectors(af83aca25ba5b41c).(vectorcxx . . $ vector# *, vectorcxx . . $ vector# *, vectorcxx . . $ vector# *);
210
211 // Inits vector to 0,0,0
212 extern void vm_CenterVectorcxx . . $ vm_CenterVector(ec8cf6451c36d470).(vectorcxx . . $ vector# *);
213
214 // Given a vector, divides second arg by vector components
215 extern void vm_AverageVectorcxx . . $ vm_AverageVector(86b7faebfcd459cf).(vectorcxx . . $ vector# *, int);
216
217 // Normalizes a vector
218 // Returns the magnitude before normalization
219 extern float vm_NormalizeVectorcxx . . $ vm_NormalizeVector(4939a7e530383cd4).(vectorcxx . . $ vector# *);
220
221 // Scales second arg vector by 3rd arg, placing result in first arg
222 extern void vm_ScaleVectorcxx . . $ vm_ScaleVector(cfd0f9d897ccca3c).(vectorcxx . . $ vector# *, vectorcxx . . $ vector# *, float);
223
224 // Scales all components of vector v by value s adds the result to p and stores result in vector d
225 extern void vm_ScaleAddVectorcxx . . $ vm_ScaleAddVector(6918b8b6b5338ad3).(vectorcxx . . $ vector# *d, vectorcxx . . $ vector# *p, vectorcxx . . $ vector# *v, float s);
226
227 // Divides second vector components by 3rd arg, placing result in first arg. Useful for parametric lines
228 extern void vm_DivVectorcxx . . $ vm_DivVector(cfd0f9d897ccca3c).(vectorcxx . . $ vector# *, vectorcxx . . $ vector# *, float);
229
230 // Same as NormalizeVector, but uses approximation
231 extern float vm_NormalizeVectorFastcxx . . $ vm_NormalizeVectorFast(4939a7e530383cd4).(vectorcxx . . $ vector# *);
232
233 // Clears a matrix to zero
234 extern void vm_ClearMatrixcxx . . $ vm_ClearMatrix(dca75c48c9ab5b54).(matrixcxx . . $ matrix# *);
235
236 // Transposes a matrix in place
237 extern void vm_TransposeMatrixcxx . . $ vm_TransposeMatrix(dca75c48c9ab5b54).(matrixcxx . . $ matrix# *);
238
239 // Given 3 angles (p,h,b), makes a rotation matrix out of them
240 extern void vm_AnglesToMatrixcxx . . $ vm_AnglesToMatrix(654fa9e3c9c3be99).(matrixcxx . . $ matrix# *, anglecxx . . $ angle# p, anglecxx . . $ angle# h, anglecxx . . $ angle# b);
241
242 // Ensure that a matrix is orthogonal
243 void vm_Orthogonalizecxx . . $ vm_Orthogonalize(dca75c48c9ab5b54).(matrixcxx . . $ matrix# *m);
244
245 // Compute a matrix from one or two vectors. At least one and at most two vectors must/can be specified.
246 // Parameters: m - filled in with the orienation matrix
247 // fvec,uvec,rvec - pointers to vectors that determine the matrix.
248 // One or two of these must be specified, with the other(s) set to NULL.
249 void vm_VectorToMatrixcxx . . $ vm_VectorToMatrix(543233b88555916d).(matrixcxx . . $ matrix# *m, vectorcxx . . $ vector# *fvec, vectorcxx . . $ vector# *uvec = nullptr, vectorcxx . . $ vector# *rvec = nullptr);
250
251 // Computes a matrix from a vector and and angle of rotation around that vector
252 // Parameters: m - filled in with the computed matrix
253 // v - the forward vector of the new matrix
254 // a - the angle of rotation around the forward vector
255 void vm_VectorAngleToMatrixcxx . . $ vm_VectorAngleToMatrix(1c1ac24c6c4d340f).(matrixcxx . . $ matrix# *m, vectorcxx . . $ vector# *v, anglecxx . . $ angle# a);
256
257 // Given an angle, places sin in 2nd arg, cos in 3rd. Either can be null
258 extern void vm_SinCoscxx . . $ vm_SinCos(5f75934053e2a042).(anglecxx . . $ angle#, float *, float *);
259
260 // Given x1,y1,x2,y2, returns the slope
261 extern float vm_GetSlopecxx . . $ vm_GetSlope(95d5df56d1097f86).(float, float, float, float);
262
263 // Calculates the perpendicular vector given three points
264 // Parms: n - the computed perp vector (filled in)
265 // v0,v1,v2 - three clockwise vertices
266 void vm_GetPerpcxx . . $ vm_GetPerp(f7eb4a62c434525d).(vectorcxx . . $ vector# *n, vectorcxx . . $ vector# *a, vectorcxx . . $ vector# *b, vectorcxx . . $ vector# *c);
267
268 // Calculates the (normalized) surface normal give three points
269 // Parms: n - the computed surface normal (filled in)
270 // v0,v1,v2 - three clockwise vertices
271 // Returns the magnitude of the normal before it was normalized.
272 // The bigger this value, the better the normal.
273 float vm_GetNormalcxx . . $ vm_GetNormal(f84d76fdbff5e570).(vectorcxx . . $ vector# *n, vectorcxx . . $ vector# *v0, vectorcxx . . $ vector# *v1, vectorcxx . . $ vector# *v2);
274
275 // Gets the distances (magnitude) between two vectors. Slow.
276 extern float vm_VectorDistancecxx . . $ vm_VectorDistance(e65d563e2b53cc54).(const vectorcxx . . $ vector# *a, const vectorcxx . . $ vector# *b);
277
278 // Gets the approx distances (magnitude) between two vectors. Faster.
279 extern float vm_VectorDistanceQuickcxx . . $ vm_VectorDistanceQuick(a0b9c8659ecc16b8).(vectorcxx . . $ vector# *a, vectorcxx . . $ vector# *b);
280
281 // Computes a normalized direction vector between two points
282 // Parameters: dest - filled in with the normalized direction vector
283 // start,end - the start and end points used to calculate the vector
284 // Returns: the distance between the two input points
285 float vm_GetNormalizedDircxx . . $ vm_GetNormalizedDir(94d642bbc45232c7).(vectorcxx . . $ vector# *dest, vectorcxx . . $ vector# *end, vectorcxx . . $ vector# *start);
286
287 // Returns a normalized direction vector between two points
288 // Uses sloppier magnitude, less precise
289 float vm_GetNormalizedDirFastcxx . . $ vm_GetNormalizedDirFast(94d642bbc45232c7).(vectorcxx . . $ vector# *dest, vectorcxx . . $ vector# *end, vectorcxx . . $ vector# *start);
290
291 // extract angles from a matrix
292 angveccxx . . $ angvec# *vm_ExtractAnglesFromMatrixcxx . . $ vm_ExtractAnglesFromMatrix(5f9a6d6f613ae256).(angveccxx . . $ angvec# *a, matrixcxx . . $ matrix# *m);
293
294 // returns the angle between two vectors and a forward vector
295 anglecxx . . $ angle# vm_DeltaAngVeccxx . . $ vm_DeltaAngVec(2b95d4afcc0f9481).(vectorcxx . . $ vector# *v0, vectorcxx . . $ vector# *v1, vectorcxx . . $ vector# *fvec);
296
297 // returns the angle between two normalized vectors and a forward vector
298 anglecxx . . $ angle# vm_DeltaAngVecNormcxx . . $ vm_DeltaAngVecNorm(2b95d4afcc0f9481).(vectorcxx . . $ vector# *v0, vectorcxx . . $ vector# *v1, vectorcxx . . $ vector# *fvec);
299
300 // Computes the distance from a point to a plane.
301 // Parms: checkp - the point to check
302 // Parms: norm - the (normalized) surface normal of the plane
303 // planep - a point on the plane
304 // Returns: The signed distance from the plane; negative dist is on the back of the plane
305 float vm_DistToPlanecxx . . $ vm_DistToPlane(94d642bbc45232c7).(vectorcxx . . $ vector# *checkp, vectorcxx . . $ vector# *norm, vectorcxx . . $ vector# *planep);
306
307 // returns the value of a determinant
308 float calc_det_valuecxx . . $ calc_det_value(36a185a7f71524b4).(matrixcxx . . $ matrix# *det);
309
310 void vm_MakeInverseMatrixcxx . . $ vm_MakeInverseMatrix(dca75c48c9ab5b54).(matrixcxx . . $ matrix# *dest);
311 void vm_SinCosToMatrixcxx . . $ vm_SinCosToMatrix(b60d58d1e647107).(matrixcxx . . $ matrix# *m, float sinp, float cosp, float sinb, float cosb, float sinh, float cosh);
312
313 // Gets the real center of a polygon
314 float vm_GetCentroidcxx . . $ vm_GetCentroid(9f2e9a768aacd0cc).(vectorcxx . . $ vector# *centroid, vectorcxx . . $ vector# *src, int nv);
315
316 // retrieves a random vector in values -RAND_MAX/2 to RAND_MAX/2
317 void vm_MakeRandomVectorcxx . . $ vm_MakeRandomVector(ec8cf6451c36d470).(vectorcxx . . $ vector# *vec);
318
319 // Given a set of points, computes the minimum bounding sphere of those points
320 float vm_ComputeBoundingSpherecxx . . $ vm_ComputeBoundingSphere(9f2e9a768aacd0cc).(vectorcxx . . $ vector# *center, vectorcxx . . $ vector# *vecs, int num_verts);
321
322 // Gets the real center of a polygon, but uses fast magnitude calculation
323 // Returns the size of the passed in stuff
324 float vm_GetCentroidFastcxx . . $ vm_GetCentroidFast(9f2e9a768aacd0cc).(vectorcxx . . $ vector# *centroid, vectorcxx . . $ vector# *src, int nv);
325
326 // Here are the C++ operator overloads -- they do as expected
327 extern matrixcxx . . $ matrix# operatorcxx . . $ `operator*`(683f8e040c904cb).*(matrixcxx . . $ matrix# src0, matrixcxx . . $ matrix# src1);
328 extern matrixcxx . . $ matrix# operatorcxx . . $ `operator*=`(3149f8f1fe1d09b5).*=(matrixcxx . . $ matrix# &src0, matrixcxx . . $ matrix# src1);
329
330 #endif