Debian Codegraph @

vecmat/vector.cpp

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