Debian Codegraph @

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