| 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 | |
| 19 | #ifndef OSIRIS_VECTOR_H |
| 20 | #define OSIRIS_VECTOR_H |
| 21 | |
| 22 | #include <cmath> |
| 23 | #include <cstdint> |
| 24 | #include <ctime> |
| 25 | |
| 26 | #include "fix.h" |
| 27 | #include "vecmat_external.h" |
| 28 | |
| 29 | const vector Zero_vector = {0.0f, 0.0f, 0.0f}; |
| 30 | |
| 31 | // Disable the "possible loss of data" warning |
| 32 | #pragma warning(disable : 4244) |
| 33 | |
| 34 | // Used for debugging. It is used in printf's so we do not have to write out the structure 3 times |
| 35 | // to print all the coordinates. |
| 36 | #define XYZ(v) (v)->x, (v)->y, (v)->z |
| 37 | |
| 38 | extern const matrix Identity_matrix; |
| 39 | |
| 40 | // Given a matrix, makes it an identity matrix |
| 41 | extern void vm_MakeIdentity(matrix *); |
| 42 | |
| 43 | // Set a vector to {0,0,0} |
| 44 | extern void vm_MakeZero(vector *v); |
| 45 | |
| 46 | // Set an angvec to {0,0,0} |
| 47 | extern void vm_MakeZero(angvec *a); |
| 48 | |
| 49 | // Rotates a vector thru a matrix |
| 50 | extern void vm_MatrixMulVector(vector *, vector *, matrix *); |
| 51 | |
| 52 | // Multiply a vector times the transpose of a matrix |
| 53 | void vm_VectorMulTMatrix(vector *result, vector *v, matrix *m); |
| 54 | |
| 55 | // Multiplies 2 3x3 matrixes, returning the result in first argument |
| 56 | extern void vm_MatrixMul(matrix *, matrix *, matrix *); |
| 57 | |
| 58 | // Multiply a matrix times the transpose of a matrix |
| 59 | void vm_MatrixMulTMatrix(matrix *dest, matrix *src0, matrix *src1); |
| 60 | |
| 61 | // Computes all math look up tables, must be called before any vector stuff is used |
| 62 | extern void vm_InitMathTables(); |
| 63 | |
| 64 | // Given a vector, returns the magnitude. Uses sqrt so it's slow |
| 65 | extern float vm_GetMagnitude(vector *); |
| 66 | |
| 67 | // Given a vector, returns an approximation of the magnitude |
| 68 | extern float vm_GetMagnitudeFast(vector *); |
| 69 | |
| 70 | // Returns the dot product of the two given vectors |
| 71 | extern float vm_DotProduct(vector *, vector *); |
| 72 | |
| 73 | // Returns a perpendicular vector to the two given vectors |
| 74 | extern void vm_CrossProduct(vector *, vector *, vector *); |
| 75 | |
| 76 | // Returns the difference between two vectors |
| 77 | extern void vm_SubVectors(vector *, const vector *, const vector *); |
| 78 | |
| 79 | // Returns adds two vectors, returns result in first arg |
| 80 | extern void vm_AddVectors(vector *, vector *, vector *); |
| 81 | |
| 82 | // Inits vector to 0,0,0 |
| 83 | extern void vm_CenterVector(vector *); |
| 84 | |
| 85 | // Given a vector, divides second arg by vector components |
| 86 | extern void vm_AverageVector(vector *, int); |
| 87 | |
| 88 | // Normalizes a vector |
| 89 | // Returns the magnitude before normalization |
| 90 | extern float vm_VectorNormalize(vector *); |
| 91 | |
| 92 | // Scales second arg vector by 3rd arg, placing result in first arg |
| 93 | extern void vm_ScaleVector(vector *, vector *, float); |
| 94 | |
| 95 | // Scales all components of vector v by value s adds the result to p and stores result in vector d |
| 96 | extern void vm_ScaleAddVector(vector *d, vector *p, vector *v, float s); |
| 97 | |
| 98 | // Divides second vector components by 3rd arg, placing result in first arg. Useful for parametric lines |
| 99 | extern void vm_DivVector(vector *, vector *, float); |
| 100 | |
| 101 | // Same as VectorNormalize, but uses approximation |
| 102 | extern float vm_VectorNormalizeFast(vector *); |
| 103 | |
| 104 | // Clears a matrix to zero |
| 105 | extern void vm_ClearMatrix(matrix *); |
| 106 | |
| 107 | // Transposes a matrix in place |
| 108 | extern void vm_TransposeMatrix(matrix *); |
| 109 | |
| 110 | // Given 3 angles (p,h,b), makes a rotation matrix out of them |
| 111 | extern void vm_AnglesToMatrix(matrix *, angle p, angle h, angle b); |
| 112 | |
| 113 | // Ensure that a matrix is orthogonal |
| 114 | void vm_Orthogonalize(matrix *m); |
| 115 | |
| 116 | // Compute a matrix from one or two vectors. At least one and at most two vectors must/can be specified. |
| 117 | // Parameters: m - filled in with the orienation matrix |
| 118 | // fvec,uvec,rvec - pointers to vectors that determine the matrix. |
| 119 | // One or two of these must be specified, with the other(s) set to NULL. |
| 120 | void vm_VectorToMatrix(matrix *m, vector *fvec, vector *uvec = NULL, vector *rvec = NULL); |
| 121 | |
| 122 | // Computes a matrix from a vector and and angle of rotation around that vector |
| 123 | // Parameters: m - filled in with the computed matrix |
| 124 | // v - the forward vector of the new matrix |
| 125 | // a - the angle of rotation around the forward vector |
| 126 | void vm_VectorAngleToMatrix(matrix *m, vector *v, angle a); |
| 127 | |
| 128 | // Given an angle, places sin in 2nd arg, cos in 3rd. Either can be null |
| 129 | extern void vm_SinCos(angle, float *, float *); |
| 130 | |
| 131 | // Given x1,y1,x2,y2, returns the slope |
| 132 | extern float vm_GetSlope(float, float, float, float); |
| 133 | |
| 134 | // Calculates the perpendicular vector given three points |
| 135 | // Parms: n - the computed perp vector (filled in) |
| 136 | // v0,v1,v2 - three clockwise vertices |
| 137 | void vm_GetPerp(vector *n, vector *a, vector *b, vector *c); |
| 138 | |
| 139 | // Calculates the (normalized) surface normal give three points |
| 140 | // Parms: n - the computed surface normal (filled in) |
| 141 | // v0,v1,v2 - three clockwise vertices |
| 142 | // Returns the magnitude of the normal before it was normalized. |
| 143 | // The bigger this value, the better the normal. |
| 144 | float vm_GetNormal(vector *n, vector *v0, vector *v1, vector *v2); |
| 145 | |
| 146 | #define vm_GetSurfaceNormal vm_GetNormal |
| 147 | |
| 148 | // Gets the distances (magnitude) between two vectors. Slow. |
| 149 | extern float vm_VectorDistance(const vector *a, const vector *b); |
| 150 | |
| 151 | // Gets the approx distances (magnitude) between two vectors. Faster. |
| 152 | extern float vm_VectorDistanceQuick(vector *a, vector *b); |
| 153 | |
| 154 | // Computes a normalized direction vector between two points |
| 155 | // Parameters: dest - filled in with the normalized direction vector |
| 156 | // start,end - the start and end points used to calculate the vector |
| 157 | // Returns: the distance between the two input points |
| 158 | float vm_GetNormalizedDir(vector *dest, vector *end, vector *start); |
| 159 | |
| 160 | // Returns a normalized direction vector between two points |
| 161 | // Uses sloppier magnitude, less precise |
| 162 | float vm_GetNormalizedDirFast(vector *dest, vector *end, vector *start); |
| 163 | |
| 164 | // extract angles from a matrix |
| 165 | angvec *vm_ExtractAnglesFromMatrix(angvec *a, matrix *m); |
| 166 | |
| 167 | // returns the angle between two vectors and a forward vector |
| 168 | angle vm_DeltaAngVec(vector *v0, vector *v1, vector *fvec); |
| 169 | |
| 170 | // returns the angle between two normalized vectors and a forward vector |
| 171 | angle vm_DeltaAngVecNorm(vector *v0, vector *v1, vector *fvec); |
| 172 | |
| 173 | // Computes the distance from a point to a plane. |
| 174 | // Parms: checkp - the point to check |
| 175 | // Parms: norm - the (normalized) surface normal of the plane |
| 176 | // planep - a point on the plane |
| 177 | // Returns: The signed distance from the plane; negative dist is on the back of the plane |
| 178 | float vm_DistToPlane(vector *checkp, vector *norm, vector *planep); |
| 179 | |
| 180 | // returns the value of a determinant |
| 181 | float calc_det_value(matrix *det); |
| 182 | |
| 183 | void vm_MakeInverseMatrix(matrix *dest); |
| 184 | void vm_SinCosToMatrix(matrix *m, float sinp, float cosp, float sinb, float cosb, float sinh, float cosh); |
| 185 | |
| 186 | // Gets the real center of a polygon |
| 187 | float vm_GetCentroid(vector *centroid, vector *src, int nv); |
| 188 | |
| 189 | // retrieves a random vector in values -RAND_MAX/2 to RAND_MAX/2 |
| 190 | void vm_MakeRandomVector(vector *vec); |
| 191 | |
| 192 | // Given a set of points, computes the minimum bounding sphere of those points |
| 193 | float vm_ComputeBoundingSphere(vector *center, vector *vecs, int num_verts); |
| 194 | |
| 195 | // Gets the real center of a polygon, but uses fast magnitude calculation |
| 196 | // Returns the size of the passed in stuff |
| 197 | float vm_GetCentroidFast(vector *centroid, vector *src, int nv); |
| 198 | |
| 199 | // Here are the C++ operator overloads -- they do as expected |
| 200 | extern matrix operator*(matrix src0, matrix src1); |
| 201 | extern matrix operator*=(matrix &src0, matrix src1); |
| 202 | |
| 203 | void vm_AverageVector(vector *a, int num) { |
| 204 | // Averages a vector. ie divides each component of vector a by num |
| 205 | // assert (num!=0); |
| 206 | |
| 207 | a->x = a->x / (float)num; |
| 208 | a->y = a->y / (float)num; |
| 209 | a->z = a->z / (float)num; |
| 210 | } |
| 211 | |
| 212 | void vm_AddVectors(vector *result, vector *a, vector *b) { |
| 213 | // Adds two vectors. Either source can equal dest |
| 214 | |
| 215 | result->x = a->x + b->x; |
| 216 | result->y = a->y + b->y; |
| 217 | result->z = a->z + b->z; |
| 218 | } |
| 219 | |
| 220 | void vm_SubVectors(vector *result, const vector *a, const vector *b) { |
| 221 | // Subtracts second vector from first. Either source can equal dest |
| 222 | |
| 223 | result->x = a->x - b->x; |
| 224 | result->y = a->y - b->y; |
| 225 | result->z = a->z - b->z; |
| 226 | } |
| 227 | |
| 228 | float vm_VectorDistance(const vector *a, const vector *b) { |
| 229 | // Given two vectors, returns the distance between them |
| 230 | |
| 231 | vector dest; |
| 232 | float dist; |
| 233 | |
| 234 | vm_SubVectors(&dest, a, b); |
| 235 | dist = vm_GetMagnitude(&dest); |
| 236 | return dist; |
| 237 | } |
| 238 | float vm_VectorDistanceQuick(vector *a, vector *b) { |
| 239 | // Given two vectors, returns the distance between them |
| 240 | |
| 241 | vector dest; |
| 242 | float dist; |
| 243 | |
| 244 | vm_SubVectors(&dest, a, b); |
| 245 | dist = vm_GetMagnitudeFast(&dest); |
| 246 | return dist; |
| 247 | } |
| 248 | |
| 249 | // Calculates the perpendicular vector given three points |
| 250 | // Parms: n - the computed perp vector (filled in) |
| 251 | // v0,v1,v2 - three clockwise vertices |
| 252 | void vm_GetPerp(vector *n, vector *a, vector *b, vector *c) { |
| 253 | // Given 3 vertices, return the surface normal in n |
| 254 | // IMPORTANT: B must be the 'corner' vertex |
| 255 | |
| 256 | vector x, y; |
| 257 | |
| 258 | vm_SubVectors(&x, b, a); |
| 259 | vm_SubVectors(&y, c, b); |
| 260 | |
| 261 | vm_CrossProduct(n, &x, &y); |
| 262 | } |
| 263 | |
| 264 | // Calculates the (normalized) surface normal give three points |
| 265 | // Parms: n - the computed surface normal (filled in) |
| 266 | // v0,v1,v2 - three clockwise vertices |
| 267 | // Returns the magnitude of the normal before it was normalized. |
| 268 | // The bigger this value, the better the normal. |
| 269 | float vm_GetNormal(vector *n, vector *v0, vector *v1, vector *v2) { |
| 270 | vm_GetPerp(n, v0, v1, v2); |
| 271 | |
| 272 | return vm_VectorNormalize(n); |
| 273 | } |
| 274 | |
| 275 | // Does a simple dot product calculation |
| 276 | float vm_DotProduct(vector *u, vector *v) { return (u->x * v->x) + (u->y * v->y) + (u->z * v->z); } |
| 277 | |
| 278 | // Scales all components of vector v by value s and stores result in vector d |
| 279 | // dest can equal source |
| 280 | void vm_ScaleVector(vector *d, vector *v, float s) { |
| 281 | d->x = (v->x * s); |
| 282 | d->y = (v->y * s); |
| 283 | d->z = (v->z * s); |
| 284 | } |
| 285 | |
| 286 | void vm_ScaleAddVector(vector *d, vector *p, vector *v, float s) { |
| 287 | // Scales all components of vector v by value s |
| 288 | // adds the result to p and stores result in vector d |
| 289 | // dest can equal source |
| 290 | |
| 291 | d->x = p->x + (v->x * s); |
| 292 | d->y = p->y + (v->y * s); |
| 293 | d->z = p->z + (v->z * s); |
| 294 | } |
| 295 | |
| 296 | void vm_DivVector(vector *dest, vector *src, float n) { |
| 297 | // Divides a vector into n portions |
| 298 | // Dest can equal src |
| 299 | |
| 300 | // assert (n!=0); |
| 301 | |
| 302 | dest->x = src->x / n; |
| 303 | dest->y = src->y / n; |
| 304 | dest->z = src->z / n; |
| 305 | } |
| 306 | |
| 307 | void vm_CrossProduct(vector *dest, vector *u, vector *v) { |
| 308 | // Computes a cross product between u and v, returns the result |
| 309 | // in Normal. Dest cannot equal source. |
| 310 | |
| 311 | dest->x = (u->y * v->z) - (u->z * v->y); |
| 312 | dest->y = (u->z * v->x) - (u->x * v->z); |
| 313 | dest->z = (u->x * v->y) - (u->y * v->x); |
| 314 | } |
| 315 | |
| 316 | // Normalize a vector. |
| 317 | // Returns: the magnitude before normalization |
| 318 | float vm_VectorNormalize(vector *a) { |
| 319 | float mag; |
| 320 | |
| 321 | mag = vm_GetMagnitude(a); |
| 322 | |
| 323 | if (mag > 0) |
| 324 | *a /= mag; |
| 325 | else { |
| 326 | *a = Zero_vector; |
| 327 | a->x = 1.0; |
| 328 | mag = 0.0f; |
| 329 | } |
| 330 | |
| 331 | return mag; |
| 332 | } |
| 333 | |
| 334 | float vm_GetMagnitude(vector *a) { |
| 335 | float f; |
| 336 | |
| 337 | f = (a->x * a->x) + (a->y * a->y) + (a->z * a->z); |
| 338 | |
| 339 | return (sqrt(f)); |
| 340 | } |
| 341 | |
| 342 | void vm_ClearMatrix(matrix *dest) { memset(dest, 0, sizeof(matrix)); } |
| 343 | |
| 344 | void vm_MakeIdentity(matrix *dest) { |
| 345 | memset(dest, 0, sizeof(matrix)); |
| 346 | dest->rvec.x = dest->uvec.y = dest->fvec.z = 1.0; |
| 347 | } |
| 348 | void vm_MakeInverseMatrix(matrix *dest) { |
| 349 | memset((void *)dest, 0, sizeof(matrix)); |
| 350 | dest->rvec.x = dest->uvec.y = dest->fvec.z = -1.0; |
| 351 | } |
| 352 | |
| 353 | void vm_TransposeMatrix(matrix *m) { |
| 354 | // Transposes a matrix in place |
| 355 | |
| 356 | float t; |
| 357 | |
| 358 | t = m->uvec.x; |
| 359 | m->uvec.x = m->rvec.y; |
| 360 | m->rvec.y = t; |
| 361 | t = m->fvec.x; |
| 362 | m->fvec.x = m->rvec.z; |
| 363 | m->rvec.z = t; |
| 364 | t = m->fvec.y; |
| 365 | m->fvec.y = m->uvec.z; |
| 366 | m->uvec.z = t; |
| 367 | } |
| 368 | |
| 369 | void vm_MatrixMulVector(vector *result, vector *v, matrix *m) { |
| 370 | // Rotates a vector thru a matrix |
| 371 | |
| 372 | // assert(result != v); |
| 373 | |
| 374 | result->x = *v * m->rvec; |
| 375 | result->y = *v * m->uvec; |
| 376 | result->z = *v * m->fvec; |
| 377 | } |
| 378 | |
| 379 | // Multiply a vector times the transpose of a matrix |
| 380 | void vm_VectorMulTMatrix(vector *result, vector *v, matrix *m) { |
| 381 | // assert(result != v); |
| 382 | |
| 383 | result->x = vm_Dot3Vector(m->rvec.x, m->uvec.x, m->fvec.x, v); |
| 384 | result->y = vm_Dot3Vector(m->rvec.y, m->uvec.y, m->fvec.y, v); |
| 385 | result->z = vm_Dot3Vector(m->rvec.z, m->uvec.z, m->fvec.z, v); |
| 386 | } |
| 387 | |
| 388 | void vm_MatrixMul(matrix *dest, matrix *src0, matrix *src1) { |
| 389 | // For multiplying two 3x3 matrices together |
| 390 | |
| 391 | // assert((dest != src0) && (dest != src1)); |
| 392 | |
| 393 | dest->rvec.x = vm_Dot3Vector(src0->rvec.x, src0->uvec.x, src0->fvec.x, &src1->rvec); |
| 394 | dest->uvec.x = vm_Dot3Vector(src0->rvec.x, src0->uvec.x, src0->fvec.x, &src1->uvec); |
| 395 | dest->fvec.x = vm_Dot3Vector(src0->rvec.x, src0->uvec.x, src0->fvec.x, &src1->fvec); |
| 396 | |
| 397 | dest->rvec.y = vm_Dot3Vector(src0->rvec.y, src0->uvec.y, src0->fvec.y, &src1->rvec); |
| 398 | dest->uvec.y = vm_Dot3Vector(src0->rvec.y, src0->uvec.y, src0->fvec.y, &src1->uvec); |
| 399 | dest->fvec.y = vm_Dot3Vector(src0->rvec.y, src0->uvec.y, src0->fvec.y, &src1->fvec); |
| 400 | |
| 401 | dest->rvec.z = vm_Dot3Vector(src0->rvec.z, src0->uvec.z, src0->fvec.z, &src1->rvec); |
| 402 | dest->uvec.z = vm_Dot3Vector(src0->rvec.z, src0->uvec.z, src0->fvec.z, &src1->uvec); |
| 403 | dest->fvec.z = vm_Dot3Vector(src0->rvec.z, src0->uvec.z, src0->fvec.z, &src1->fvec); |
| 404 | } |
| 405 | |
| 406 | // Multiply a matrix times the transpose of a matrix |
| 407 | void vm_MatrixMulTMatrix(matrix *dest, matrix *src0, matrix *src1) { |
| 408 | // For multiplying two 3x3 matrices together |
| 409 | |
| 410 | // assert((dest != src0) && (dest != src1)); |
| 411 | |
| 412 | dest->rvec.x = src0->rvec.x * src1->rvec.x + src0->uvec.x * src1->uvec.x + src0->fvec.x * src1->fvec.x; |
| 413 | dest->uvec.x = src0->rvec.x * src1->rvec.y + src0->uvec.x * src1->uvec.y + src0->fvec.x * src1->fvec.y; |
| 414 | dest->fvec.x = src0->rvec.x * src1->rvec.z + src0->uvec.x * src1->uvec.z + src0->fvec.x * src1->fvec.z; |
| 415 | |
| 416 | dest->rvec.y = src0->rvec.y * src1->rvec.x + src0->uvec.y * src1->uvec.x + src0->fvec.y * src1->fvec.x; |
| 417 | dest->uvec.y = src0->rvec.y * src1->rvec.y + src0->uvec.y * src1->uvec.y + src0->fvec.y * src1->fvec.y; |
| 418 | dest->fvec.y = src0->rvec.y * src1->rvec.z + src0->uvec.y * src1->uvec.z + src0->fvec.y * src1->fvec.z; |
| 419 | |
| 420 | dest->rvec.z = src0->rvec.z * src1->rvec.x + src0->uvec.z * src1->uvec.x + src0->fvec.z * src1->fvec.x; |
| 421 | dest->uvec.z = src0->rvec.z * src1->rvec.y + src0->uvec.z * src1->uvec.y + src0->fvec.z * src1->fvec.y; |
| 422 | dest->fvec.z = src0->rvec.z * src1->rvec.z + src0->uvec.z * src1->uvec.z + src0->fvec.z * src1->fvec.z; |
| 423 | } |
| 424 | |
| 425 | matrix operator*(matrix src0, matrix src1) { |
| 426 | // For multiplying two 3x3 matrices together |
| 427 | matrix dest; |
| 428 | |
| 429 | dest.rvec.x = vm_Dot3Vector(src0.rvec.x, src0.uvec.x, src0.fvec.x, &src1.rvec); |
| 430 | dest.uvec.x = vm_Dot3Vector(src0.rvec.x, src0.uvec.x, src0.fvec.x, &src1.uvec); |
| 431 | dest.fvec.x = vm_Dot3Vector(src0.rvec.x, src0.uvec.x, src0.fvec.x, &src1.fvec); |
| 432 | |
| 433 | dest.rvec.y = vm_Dot3Vector(src0.rvec.y, src0.uvec.y, src0.fvec.y, &src1.rvec); |
| 434 | dest.uvec.y = vm_Dot3Vector(src0.rvec.y, src0.uvec.y, src0.fvec.y, &src1.uvec); |
| 435 | dest.fvec.y = vm_Dot3Vector(src0.rvec.y, src0.uvec.y, src0.fvec.y, &src1.fvec); |
| 436 | |
| 437 | dest.rvec.z = vm_Dot3Vector(src0.rvec.z, src0.uvec.z, src0.fvec.z, &src1.rvec); |
| 438 | dest.uvec.z = vm_Dot3Vector(src0.rvec.z, src0.uvec.z, src0.fvec.z, &src1.uvec); |
| 439 | dest.fvec.z = vm_Dot3Vector(src0.rvec.z, src0.uvec.z, src0.fvec.z, &src1.fvec); |
| 440 | |
| 441 | return dest; |
| 442 | } |
| 443 | |
| 444 | matrix operator*=(matrix &src0, matrix src1) { return (src0 = src0 * src1); } |
| 445 | |
| 446 | // Computes a normalized direction vector between two points |
| 447 | // Parameters: dest - filled in with the normalized direction vector |
| 448 | // start,end - the start and end points used to calculate the vector |
| 449 | // Returns: the distance between the two input points |
| 450 | float vm_GetNormalizedDir(vector *dest, vector *end, vector *start) { |
| 451 | vm_SubVectors(dest, end, start); |
| 452 | return vm_VectorNormalize(dest); |
| 453 | } |
| 454 | |
| 455 | // Returns a normalized direction vector between two points |
| 456 | // Just like vm_GetNormalizedDir(), but uses sloppier magnitude, less precise |
| 457 | // Parameters: dest - filled in with the normalized direction vector |
| 458 | // start,end - the start and end points used to calculate the vector |
| 459 | // Returns: the distance between the two input points |
| 460 | float vm_GetNormalizedDirFast(vector *dest, vector *end, vector *start) { |
| 461 | vm_SubVectors(dest, end, start); |
| 462 | return vm_VectorNormalizeFast(dest); |
| 463 | } |
| 464 | |
| 465 | float vm_GetMagnitudeFast(vector *v) { |
| 466 | float a, b, c, bc; |
| 467 | |
| 468 | a = fabs(v->x); |
| 469 | b = fabs(v->y); |
| 470 | c = fabs(v->z); |
| 471 | |
| 472 | if (a < b) { |
| 473 | float t = a; |
| 474 | a = b; |
| 475 | b = t; |
| 476 | } |
| 477 | |
| 478 | if (b < c) { |
| 479 | float t = b; |
| 480 | b = c; |
| 481 | c = t; |
| 482 | |
| 483 | if (a < b) { |
| 484 | float t = a; |
| 485 | a = b; |
| 486 | b = t; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | bc = (b / 4) + (c / 8); |
| 491 | |
| 492 | return a + bc + (bc / 2); |
| 493 | } |
| 494 | |
| 495 | // Normalize a vector using an approximation of the magnitude |
| 496 | // Returns: the magnitude before normalization |
| 497 | float vm_VectorNormalizeFast(vector *a) { |
| 498 | float mag; |
| 499 | |
| 500 | mag = vm_GetMagnitudeFast(a); |
| 501 | |
| 502 | if (mag == 0.0) { |
| 503 | a->x = a->y = a->z = 0.0; |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | a->x = (a->x / mag); |
| 508 | a->y = (a->y / mag); |
| 509 | a->z = (a->z / mag); |
| 510 | |
| 511 | return mag; |
| 512 | } |
| 513 | |
| 514 | // Computes the distance from a point to a plane. |
| 515 | // Parms: checkp - the point to check |
| 516 | // Parms: norm - the (normalized) surface normal of the plane |
| 517 | // planep - a point on the plane |
| 518 | // Returns: The signed distance from the plane; negative dist is on the back of the plane |
| 519 | float vm_DistToPlane(vector *checkp, vector *norm, vector *planep) { |
| 520 | vector t; |
| 521 | |
| 522 | t = *checkp - *planep; |
| 523 | |
| 524 | return t * *norm; |
| 525 | } |
| 526 | |
| 527 | float vm_GetSlope(float x1, float y1, float x2, float y2) { |
| 528 | // returns the slope of a line |
| 529 | float r; |
| 530 | |
| 531 | if (y2 - y1 == 0) |
| 532 | return (0.0); |
| 533 | |
| 534 | r = (x2 - x1) / (y2 - y1); |
| 535 | return (r); |
| 536 | } |
| 537 | |
| 538 | void vm_SinCosToMatrix(matrix *m, float sinp, float cosp, float sinb, float cosb, float sinh, float cosh) { |
| 539 | float sbsh, cbch, cbsh, sbch; |
| 540 | |
| 541 | sbsh = (sinb * sinh); |
| 542 | cbch = (cosb * cosh); |
| 543 | cbsh = (cosb * sinh); |
| 544 | sbch = (sinb * cosh); |
| 545 | |
| 546 | m->rvec.x = cbch + (sinp * sbsh); // m1 |
| 547 | m->uvec.z = sbsh + (sinp * cbch); // m8 |
| 548 | |
| 549 | m->uvec.x = (sinp * cbsh) - sbch; // m2 |
| 550 | m->rvec.z = (sinp * sbch) - cbsh; // m7 |
| 551 | |
| 552 | m->fvec.x = (sinh * cosp); // m3 |
| 553 | m->rvec.y = (sinb * cosp); // m4 |
| 554 | m->uvec.y = (cosb * cosp); // m5 |
| 555 | m->fvec.z = (cosh * cosp); // m9 |
| 556 | |
| 557 | m->fvec.y = -sinp; // m6 |
| 558 | } |
| 559 | |
| 560 | void vm_AnglesToMatrix(matrix *m, angle p, angle h, angle b) { |
| 561 | float sinp, cosp, sinb, cosb, sinh, cosh; |
| 562 | |
| 563 | sinp = FixSin(p); |
| 564 | cosp = FixCos(p); |
| 565 | sinb = FixSin(b); |
| 566 | cosb = FixCos(b); |
| 567 | sinh = FixSin(h); |
| 568 | cosh = FixCos(h); |
| 569 | |
| 570 | vm_SinCosToMatrix(m, sinp, cosp, sinb, cosb, sinh, cosh); |
| 571 | } |
| 572 | |
| 573 | // Computes a matrix from a vector and and angle of rotation around that vector |
| 574 | // Parameters: m - filled in with the computed matrix |
| 575 | // v - the forward vector of the new matrix |
| 576 | // a - the angle of rotation around the forward vector |
| 577 | void vm_VectorAngleToMatrix(matrix *m, vector *v, angle a) { |
| 578 | float sinb, cosb, sinp, cosp, sinh, cosh; |
| 579 | |
| 580 | sinb = FixSin(a); |
| 581 | cosb = FixCos(a); |
| 582 | |
| 583 | sinp = -v->y; |
| 584 | cosp = sqrt(1.0 - (sinp * sinp)); |
| 585 | |
| 586 | if (cosp != 0.0) { |
| 587 | sinh = v->x / cosp; |
| 588 | cosh = v->z / cosp; |
| 589 | } else { |
| 590 | sinh = 0; |
| 591 | cosh = 1.0; |
| 592 | } |
| 593 | |
| 594 | vm_SinCosToMatrix(m, sinp, cosp, sinb, cosb, sinh, cosh); |
| 595 | } |
| 596 | |
| 597 | // Ensure that a matrix is orthogonal |
| 598 | void vm_Orthogonalize(matrix *m) { |
| 599 | // Normalize forward vector |
| 600 | if (vm_VectorNormalize(&m->fvec) == 0) { |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | // Generate right vector from forward and up vectors |
| 605 | m->rvec = m->uvec ^ m->fvec; |
| 606 | |
| 607 | // Normaize new right vector |
| 608 | if (vm_VectorNormalize(&m->rvec) == 0) { |
| 609 | vm_VectorToMatrix(m, &m->fvec, NULL, NULL); // error, so generate from forward vector only |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | // Recompute up vector, in case it wasn't entirely perpendiclar |
| 614 | m->uvec = m->fvec ^ m->rvec; |
| 615 | } |
| 616 | |
| 617 | // do the math for vm_VectorToMatrix() |
| 618 | void DoVectorToMatrix(matrix *m, vector *fvec, vector *uvec, vector *rvec) { |
| 619 | vector *xvec = &m->rvec, *yvec = &m->uvec, *zvec = &m->fvec; |
| 620 | |
| 621 | // ASSERT(fvec != NULL); |
| 622 | |
| 623 | *zvec = *fvec; |
| 624 | if (vm_VectorNormalize(zvec) == 0) { |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | if (uvec == NULL) { |
| 629 | |
| 630 | if (rvec == NULL) { // just forward vec |
| 631 | |
| 632 | bad_vector2:; |
| 633 | |
| 634 | if (zvec->x == 0 && zvec->z == 0) { // forward vec is straight up or down |
| 635 | |
| 636 | m->rvec.x = 1.0; |
| 637 | m->uvec.z = (zvec->y < 0) ? 1.0 : -1.0; |
| 638 | |
| 639 | m->rvec.y = m->rvec.z = m->uvec.x = m->uvec.y = 0; |
| 640 | } else { // not straight up or down |
| 641 | |
| 642 | xvec->x = zvec->z; |
| 643 | xvec->y = 0; |
| 644 | xvec->z = -zvec->x; |
| 645 | |
| 646 | vm_VectorNormalize(xvec); |
| 647 | |
| 648 | *yvec = *zvec ^ *xvec; |
| 649 | } |
| 650 | |
| 651 | } else { // use right vec |
| 652 | |
| 653 | *xvec = *rvec; |
| 654 | if (vm_VectorNormalize(xvec) == 0) |
| 655 | goto bad_vector2; |
| 656 | |
| 657 | *yvec = *zvec ^ *xvec; |
| 658 | |
| 659 | // normalize new perpendicular vector |
| 660 | if (vm_VectorNormalize(yvec) == 0) |
| 661 | goto bad_vector2; |
| 662 | |
| 663 | // now recompute right vector, in case it wasn't entirely perpendiclar |
| 664 | *xvec = *yvec ^ *zvec; |
| 665 | } |
| 666 | } else { // use up vec |
| 667 | |
| 668 | *yvec = *uvec; |
| 669 | if (vm_VectorNormalize(yvec) == 0) |
| 670 | goto bad_vector2; |
| 671 | |
| 672 | *xvec = *yvec ^ *zvec; |
| 673 | |
| 674 | // normalize new perpendicular vector |
| 675 | if (vm_VectorNormalize(xvec) == 0) |
| 676 | goto bad_vector2; |
| 677 | |
| 678 | // now recompute up vector, in case it wasn't entirely perpendiclar |
| 679 | *yvec = *zvec ^ *xvec; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | // Compute a matrix from one or two vectors. At least one and at most two vectors must/can be specified. |
| 684 | // Parameters: m - filled in with the orienation matrix |
| 685 | // fvec,uvec,rvec - pointers to vectors that determine the matrix. |
| 686 | // One or two of these must be specified, with the other(s) set to NULL. |
| 687 | void vm_VectorToMatrix(matrix *m, vector *fvec, vector *uvec, vector *rvec) { |
| 688 | if (!fvec) { // no forward vector. Use up and/or right vectors. |
| 689 | matrix tmatrix; |
| 690 | |
| 691 | if (uvec) { // got up vector. use up and, if specified, right vectors. |
| 692 | DoVectorToMatrix(&tmatrix, uvec, NULL, rvec); |
| 693 | m->fvec = -tmatrix.uvec; |
| 694 | m->uvec = tmatrix.fvec; |
| 695 | m->rvec = tmatrix.rvec; |
| 696 | return; |
| 697 | } else { // no up vector. Use right vector only. |
| 698 | // ASSERT(rvec); |
| 699 | DoVectorToMatrix(&tmatrix, rvec, NULL, NULL); |
| 700 | m->fvec = -tmatrix.rvec; |
| 701 | m->uvec = tmatrix.uvec; |
| 702 | m->rvec = tmatrix.fvec; |
| 703 | return; |
| 704 | } |
| 705 | } else { |
| 706 | // ASSERT(! (uvec && rvec)); //can only have 1 or 2 vectors specified |
| 707 | DoVectorToMatrix(m, fvec, uvec, rvec); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | void vm_SinCos(uint16_t a, float *s, float *c) { |
| 712 | if (s) |
| 713 | *s = FixSin(a); |
| 714 | if (c) |
| 715 | *c = FixCos(a); |
| 716 | } |
| 717 | |
| 718 | // extract angles from a matrix |
| 719 | angvec *vm_ExtractAnglesFromMatrix(angvec *a, matrix *m) { |
| 720 | float sinh, cosh, cosp; |
| 721 | |
| 722 | if (m->fvec.x == 0 && m->fvec.z == 0) // zero head |
| 723 | a->h = 0; |
| 724 | else |
| 725 | a->h = FixAtan2(m->fvec.z, m->fvec.x); |
| 726 | |
| 727 | sinh = FixSin(a->h); |
| 728 | cosh = FixCos(a->h); |
| 729 | |
| 730 | if (fabs(sinh) > fabs(cosh)) // sine is larger, so use it |
| 731 | cosp = (m->fvec.x / sinh); |
| 732 | else // cosine is larger, so use it |
| 733 | cosp = (m->fvec.z / cosh); |
| 734 | |
| 735 | if (cosp == 0 && m->fvec.y == 0) |
| 736 | a->p = 0; |
| 737 | else |
| 738 | a->p = FixAtan2(cosp, -m->fvec.y); |
| 739 | |
| 740 | if (cosp == 0) // the cosine of pitch is zero. we're pitched straight up. say no bank |
| 741 | |
| 742 | a->b = 0; |
| 743 | |
| 744 | else { |
| 745 | float sinb, cosb; |
| 746 | |
| 747 | sinb = (m->rvec.y / cosp); |
| 748 | cosb = (m->uvec.y / cosp); |
| 749 | |
| 750 | if (sinb == 0 && cosb == 0) |
| 751 | a->b = 0; |
| 752 | else |
| 753 | a->b = FixAtan2(cosb, sinb); |
| 754 | } |
| 755 | |
| 756 | return a; |
| 757 | } |
| 758 | |
| 759 | // returns the value of a determinant |
| 760 | float calc_det_value(matrix *det) { |
| 761 | return det->rvec.x * det->uvec.y * det->fvec.z - det->rvec.x * det->uvec.z * det->fvec.y - |
| 762 | det->rvec.y * det->uvec.x * det->fvec.z + det->rvec.y * det->uvec.z * det->fvec.x + |
| 763 | det->rvec.z * det->uvec.x * det->fvec.y - det->rvec.z * det->uvec.y * det->fvec.x; |
| 764 | } |
| 765 | |
| 766 | // computes the delta angle between two vectors. |
| 767 | // vectors need not be normalized. if they are, call vm_vec_delta_ang_norm() |
| 768 | // the forward vector (third parameter) can be NULL, in which case the absolute |
| 769 | // value of the angle in returned. Otherwise the angle around that vector is |
| 770 | // returned. |
| 771 | |
| 772 | angle vm_DeltaAngVec(vector *v0, vector *v1, vector *fvec) { |
| 773 | vector t0, t1; |
| 774 | |
| 775 | t0 = *v0; |
| 776 | t1 = *v1; |
| 777 | |
| 778 | vm_VectorNormalize(&t0); |
| 779 | vm_VectorNormalize(&t1); |
| 780 | |
| 781 | return vm_DeltaAngVecNorm(&t0, &t1, fvec); |
| 782 | } |
| 783 | |
| 784 | // computes the delta angle between two normalized vectors. |
| 785 | angle vm_DeltaAngVecNorm(vector *v0, vector *v1, vector *fvec) { |
| 786 | angle a; |
| 787 | |
| 788 | a = FixAcos(vm_DotProduct(v0, v1)); |
| 789 | |
| 790 | if (fvec) { |
| 791 | vector t; |
| 792 | |
| 793 | vm_CrossProduct(&t, v0, v1); |
| 794 | if (vm_DotProduct(&t, fvec) < 0) |
| 795 | a = -a; |
| 796 | } |
| 797 | |
| 798 | return a; |
| 799 | } |
| 800 | |
| 801 | // Gets the real center of a polygon |
| 802 | // Returns the size of the passed in stuff |
| 803 | float vm_GetCentroid(vector *centroid, vector *src, int nv) { |
| 804 | // ASSERT (nv>2); |
| 805 | vector normal; |
| 806 | float area, total_area; |
| 807 | int i; |
| 808 | vector tmp_center; |
| 809 | |
| 810 | vm_MakeZero(centroid); |
| 811 | |
| 812 | // First figure out the total area of this polygon |
| 813 | vm_GetPerp(&normal, &src[0], &src[1], &src[2]); |
| 814 | total_area = (vm_GetMagnitude(&normal) / 2); |
| 815 | |
| 816 | for (i = 2; i < nv - 1; i++) { |
| 817 | vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]); |
| 818 | area = (vm_GetMagnitude(&normal) / 2); |
| 819 | total_area += area; |
| 820 | } |
| 821 | |
| 822 | // Now figure out how much weight each triangle represents to the overall |
| 823 | // polygon |
| 824 | vm_GetPerp(&normal, &src[0], &src[1], &src[2]); |
| 825 | area = (vm_GetMagnitude(&normal) / 2); |
| 826 | |
| 827 | // Get the center of the first polygon |
| 828 | vm_MakeZero(&tmp_center); |
| 829 | for (i = 0; i < 3; i++) { |
| 830 | tmp_center += src[i]; |
| 831 | } |
| 832 | tmp_center /= 3; |
| 833 | |
| 834 | *centroid += (tmp_center * (area / total_area)); |
| 835 | |
| 836 | // Now do the same for the rest |
| 837 | for (i = 2; i < nv - 1; i++) { |
| 838 | vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]); |
| 839 | area = (vm_GetMagnitude(&normal) / 2); |
| 840 | |
| 841 | vm_MakeZero(&tmp_center); |
| 842 | |
| 843 | tmp_center += src[0]; |
| 844 | tmp_center += src[i]; |
| 845 | tmp_center += src[i + 1]; |
| 846 | |
| 847 | tmp_center /= 3; |
| 848 | |
| 849 | *centroid += (tmp_center * (area / total_area)); |
| 850 | } |
| 851 | |
| 852 | return total_area; |
| 853 | } |
| 854 | |
| 855 | // Gets the real center of a polygon, but uses fast magnitude calculation |
| 856 | // Returns the size of the passed in stuff |
| 857 | float vm_GetCentroidFast(vector *centroid, vector *src, int nv) { |
| 858 | // ASSERT (nv>2); |
| 859 | vector normal; |
| 860 | float area, total_area; |
| 861 | int i; |
| 862 | vector tmp_center; |
| 863 | |
| 864 | vm_MakeZero(centroid); |
| 865 | |
| 866 | // First figure out the total area of this polygon |
| 867 | vm_GetPerp(&normal, &src[0], &src[1], &src[2]); |
| 868 | total_area = (vm_GetMagnitudeFast(&normal) / 2); |
| 869 | |
| 870 | for (i = 2; i < nv - 1; i++) { |
| 871 | vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]); |
| 872 | area = (vm_GetMagnitudeFast(&normal) / 2); |
| 873 | total_area += area; |
| 874 | } |
| 875 | |
| 876 | // Now figure out how much weight each triangle represents to the overall |
| 877 | // polygon |
| 878 | vm_GetPerp(&normal, &src[0], &src[1], &src[2]); |
| 879 | area = (vm_GetMagnitudeFast(&normal) / 2); |
| 880 | |
| 881 | // Get the center of the first polygon |
| 882 | vm_MakeZero(&tmp_center); |
| 883 | for (i = 0; i < 3; i++) { |
| 884 | tmp_center += src[i]; |
| 885 | } |
| 886 | tmp_center /= 3; |
| 887 | |
| 888 | *centroid += (tmp_center * (area / total_area)); |
| 889 | |
| 890 | // Now do the same for the rest |
| 891 | for (i = 2; i < nv - 1; i++) { |
| 892 | vm_GetPerp(&normal, &src[0], &src[i], &src[i + 1]); |
| 893 | area = (vm_GetMagnitudeFast(&normal) / 2); |
| 894 | |
| 895 | vm_MakeZero(&tmp_center); |
| 896 | |
| 897 | tmp_center += src[0]; |
| 898 | tmp_center += src[i]; |
| 899 | tmp_center += src[i + 1]; |
| 900 | |
| 901 | tmp_center /= 3; |
| 902 | |
| 903 | *centroid += (tmp_center * (area / total_area)); |
| 904 | } |
| 905 | |
| 906 | return total_area; |
| 907 | } |
| 908 | |
| 909 | // creates a completely random, non-normalized vector with a range of values from -1023 to +1024 values) |
| 910 | void vm_MakeRandomVector(vector *vec) { |
| 911 | vec->x = rand() - RAND_MAX / 2; |
| 912 | vec->y = rand() - RAND_MAX / 2; |
| 913 | vec->z = rand() - RAND_MAX / 2; |
| 914 | } |
| 915 | |
| 916 | // Given a set of points, computes the minimum bounding sphere of those points |
| 917 | float vm_ComputeBoundingSphere(vector *center, vector *vecs, int num_verts) { |
| 918 | // This algorithm is from Graphics Gems I. There's a better algorithm in Graphics Gems III that |
| 919 | // we should probably implement sometime. |
| 920 | |
| 921 | vector *min_x, *max_x, *min_y, *max_y, *min_z, *max_z, *vp; |
| 922 | float dx, dy, dz; |
| 923 | float rad, rad2; |
| 924 | int i; |
| 925 | |
| 926 | // Initialize min, max vars |
| 927 | min_x = max_x = min_y = max_y = min_z = max_z = &vecs[0]; |
| 928 | |
| 929 | // First, find the points with the min & max x,y, & z coordinates |
| 930 | for (i = 0, vp = vecs; i < num_verts; i++, vp++) { |
| 931 | |
| 932 | if (vp->x < min_x->x) |
| 933 | min_x = vp; |
| 934 | |
| 935 | if (vp->x > max_x->x) |
| 936 | max_x = vp; |
| 937 | |
| 938 | if (vp->y < min_y->y) |
| 939 | min_y = vp; |
| 940 | |
| 941 | if (vp->y > max_y->y) |
| 942 | max_y = vp; |
| 943 | |
| 944 | if (vp->z < min_z->z) |
| 945 | min_z = vp; |
| 946 | |
| 947 | if (vp->z > max_z->z) |
| 948 | max_z = vp; |
| 949 | } |
| 950 | |
| 951 | // Calculate initial sphere |
| 952 | |
| 953 | dx = vm_VectorDistance(min_x, max_x); |
| 954 | dy = vm_VectorDistance(min_y, max_y); |
| 955 | dz = vm_VectorDistance(min_z, max_z); |
| 956 | |
| 957 | if (dx > dy) |
| 958 | if (dx > dz) { |
| 959 | *center = (*min_x + *max_x) / 2; |
| 960 | rad = dx / 2; |
| 961 | } else { |
| 962 | *center = (*min_z + *max_z) / 2; |
| 963 | rad = dz / 2; |
| 964 | } |
| 965 | else if (dy > dz) { |
| 966 | *center = (*min_y + *max_y) / 2; |
| 967 | rad = dy / 2; |
| 968 | } else { |
| 969 | *center = (*min_z + *max_z) / 2; |
| 970 | rad = dz / 2; |
| 971 | } |
| 972 | |
| 973 | // Go through all points and look for ones that don't fit |
| 974 | rad2 = rad * rad; |
| 975 | for (i = 0, vp = vecs; i < num_verts; i++, vp++) { |
| 976 | vector delta; |
| 977 | float t2; |
| 978 | |
| 979 | delta = *vp - *center; |
| 980 | t2 = delta.x * delta.x + delta.y * delta.y + delta.z * delta.z; |
| 981 | |
| 982 | // If point outside, make the sphere bigger |
| 983 | if (t2 > rad2) { |
| 984 | float t; |
| 985 | |
| 986 | t = sqrt(t2); |
| 987 | rad = (rad + t) / 2; |
| 988 | rad2 = rad * rad; |
| 989 | *center += delta * (t - rad) / t; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | // We're done |
| 994 | return rad; |
| 995 | } |
| 996 | |
| 997 | #endif |