| 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_H |
| 153 | |
| 154 | #include <cmath> |
| 155 | |
| 156 | #include "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" |
| 161 | |
| 162 | extern const vector Zero_vector; |
| 163 | extern const matrix 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 XYZ(v) (v)->x, (v)->y, (v)->z |
| 168 | |
| 169 | // Given a matrix, makes it an identity matrix |
| 170 | extern void vm_MakeIdentity(matrix *); |
| 171 | |
| 172 | // Set a vector to {0,0,0} |
| 173 | extern void vm_MakeZero(vector *v); |
| 174 | |
| 175 | // Set an angvec to {0,0,0} |
| 176 | extern void vm_MakeZero(angvec *a); |
| 177 | |
| 178 | // Rotates a vector thru a matrix |
| 179 | extern void vm_MatrixMulVector(vector *, vector *, matrix *); |
| 180 | |
| 181 | // Multiply a vector times the transpose of a matrix |
| 182 | void vm_VectorMulTMatrix(vector *result, vector *v, matrix *m); |
| 183 | |
| 184 | // Multiplies 2 3x3 matrixes, returning the result in first argument |
| 185 | extern void vm_MatrixMul(matrix *, matrix *, matrix *); |
| 186 | |
| 187 | // Multiply a matrix times the transpose of a matrix |
| 188 | void vm_MatrixMulTMatrix(matrix *dest, matrix *src0, matrix *src1); |
| 189 | |
| 190 | // Computes all math look up tables, must be called before any vector stuff is used |
| 191 | extern void vm_InitMathTables(); |
| 192 | |
| 193 | // Given a vector, returns the magnitude. Uses sqrt so it's slow |
| 194 | extern float vm_GetMagnitude(vector *); |
| 195 | |
| 196 | // Given a vector, returns an approximation of the magnitude |
| 197 | extern float vm_GetMagnitudeFast(vector *); |
| 198 | |
| 199 | // Returns the dot product of the two given vectors |
| 200 | extern float vm_DotProduct(const vector *, const vector *); |
| 201 | |
| 202 | // Returns a perpendicular vector to the two given vectors |
| 203 | extern void vm_CrossProduct(vector *, vector *, vector *); |
| 204 | |
| 205 | // Returns the difference between two vectors |
| 206 | extern void vm_SubVectors(vector *, const vector *, const vector *); |
| 207 | |
| 208 | // Returns adds two vectors, returns result in first arg |
| 209 | extern void vm_AddVectors(vector *, vector *, vector *); |
| 210 | |
| 211 | // Inits vector to 0,0,0 |
| 212 | extern void vm_CenterVector(vector *); |
| 213 | |
| 214 | // Given a vector, divides second arg by vector components |
| 215 | extern void vm_AverageVector(vector *, int); |
| 216 | |
| 217 | // Normalizes a vector |
| 218 | // Returns the magnitude before normalization |
| 219 | extern float vm_NormalizeVector(vector *); |
| 220 | |
| 221 | // Scales second arg vector by 3rd arg, placing result in first arg |
| 222 | extern void vm_ScaleVector(vector *, 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_ScaleAddVector(vector *d, vector *p, 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_DivVector(vector *, vector *, float); |
| 229 | |
| 230 | // Same as NormalizeVector, but uses approximation |
| 231 | extern float vm_NormalizeVectorFast(vector *); |
| 232 | |
| 233 | // Clears a matrix to zero |
| 234 | extern void vm_ClearMatrix(matrix *); |
| 235 | |
| 236 | // Transposes a matrix in place |
| 237 | extern void vm_TransposeMatrix(matrix *); |
| 238 | |
| 239 | // Given 3 angles (p,h,b), makes a rotation matrix out of them |
| 240 | extern void vm_AnglesToMatrix(matrix *, angle p, angle h, angle b); |
| 241 | |
| 242 | // Ensure that a matrix is orthogonal |
| 243 | void vm_Orthogonalize(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_VectorToMatrix(matrix *m, vector *fvec, vector *uvec = nullptr, 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_VectorAngleToMatrix(matrix *m, vector *v, angle a); |
| 256 | |
| 257 | // Given an angle, places sin in 2nd arg, cos in 3rd. Either can be null |
| 258 | extern void vm_SinCos(angle, float *, float *); |
| 259 | |
| 260 | // Given x1,y1,x2,y2, returns the slope |
| 261 | extern float vm_GetSlope(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_GetPerp(vector *n, vector *a, vector *b, 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_GetNormal(vector *n, vector *v0, vector *v1, vector *v2); |
| 274 | |
| 275 | // Gets the distances (magnitude) between two vectors. Slow. |
| 276 | extern float vm_VectorDistance(const vector *a, const vector *b); |
| 277 | |
| 278 | // Gets the approx distances (magnitude) between two vectors. Faster. |
| 279 | extern float vm_VectorDistanceQuick(vector *a, 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_GetNormalizedDir(vector *dest, vector *end, vector *start); |
| 286 | |
| 287 | // Returns a normalized direction vector between two points |
| 288 | // Uses sloppier magnitude, less precise |
| 289 | float vm_GetNormalizedDirFast(vector *dest, vector *end, vector *start); |
| 290 | |
| 291 | // extract angles from a matrix |
| 292 | angvec *vm_ExtractAnglesFromMatrix(angvec *a, matrix *m); |
| 293 | |
| 294 | // returns the angle between two vectors and a forward vector |
| 295 | angle vm_DeltaAngVec(vector *v0, vector *v1, vector *fvec); |
| 296 | |
| 297 | // returns the angle between two normalized vectors and a forward vector |
| 298 | angle vm_DeltaAngVecNorm(vector *v0, vector *v1, 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_DistToPlane(vector *checkp, vector *norm, vector *planep); |
| 306 | |
| 307 | // returns the value of a determinant |
| 308 | float calc_det_value(matrix *det); |
| 309 | |
| 310 | void vm_MakeInverseMatrix(matrix *dest); |
| 311 | void vm_SinCosToMatrix(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_GetCentroid(vector *centroid, vector *src, int nv); |
| 315 | |
| 316 | // retrieves a random vector in values -RAND_MAX/2 to RAND_MAX/2 |
| 317 | void vm_MakeRandomVector(vector *vec); |
| 318 | |
| 319 | // Given a set of points, computes the minimum bounding sphere of those points |
| 320 | float vm_ComputeBoundingSphere(vector *center, 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_GetCentroidFast(vector *centroid, vector *src, int nv); |
| 325 | |
| 326 | // Here are the C++ operator overloads -- they do as expected |
| 327 | extern matrix operator*(matrix src0, matrix src1); |
| 328 | extern matrix operator*=(matrix &src0, matrix src1); |
| 329 | |
| 330 | #endif |