forked from scp-fs2open/fs2open.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvecmat.cpp
More file actions
3168 lines (2593 loc) · 104 KB
/
Copy pathvecmat.cpp
File metadata and controls
3168 lines (2593 loc) · 104 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include <cstdio>
#include <numeric>
#if _M_IX86_FP >= 1
#include <xmmintrin.h>
#endif
#include "math/vecmat.h"
#include "utils/RandomRange.h"
#define SMALL_NUM 1e-7
#define SMALLER_NUM 1e-20
#define CONVERT_RADIANS 0.017453 // conversion factor from degrees to radians
vec3d vmd_zero_vector = ZERO_VECTOR;
vec3d vmd_scale_identity_vector = SCALE_IDENTITY_VECTOR;
vec3d vmd_x_vector = { { { 1.0f, 0.0f, 0.0f } } };
vec3d vmd_y_vector = { { { 0.0f, 1.0f, 0.0f } } };
vec3d vmd_z_vector = { { { 0.0f, 0.0f, 1.0f } } };
matrix vmd_zero_matrix = ZERO_MATRIX;
matrix4 vmd_zero_matrix4 = ZERO_MATRIX4;
matrix vmd_identity_matrix = IDENTITY_MATRIX;
angles vmd_zero_angles = { 0.0f, 0.0f, 0.0f };
#define UNINITIALIZED_VALUE -12345678.9f
bool vm_vec_equal(const vec4 &self, const vec4 &other)
{
return fl_equal(self.a1d[0], other.a1d[0]) && fl_equal(self.a1d[1], other.a1d[1]) && fl_equal(self.a1d[2], other.a1d[2]) && fl_equal(self.a1d[3], other.a1d[3]);
}
bool vm_vec_equal(const vec3d &self, const vec3d &other)
{
return fl_equal(self.a1d[0], other.a1d[0]) && fl_equal(self.a1d[1], other.a1d[1]) && fl_equal(self.a1d[2], other.a1d[2]);
}
bool vm_vec_equal(const vec2d &self, const vec2d &other)
{
return fl_equal(self.x, other.x) && fl_equal(self.y, other.y);
}
bool vm_matrix_equal(const matrix &self, const matrix &other)
{
return vm_vec_equal(self.vec.fvec, other.vec.fvec) && vm_vec_equal(self.vec.uvec, other.vec.uvec) && vm_vec_equal(self.vec.rvec, other.vec.rvec);
}
bool vm_matrix_equal(const matrix4 &self, const matrix4 &other)
{
return vm_vec_equal(self.vec.fvec, other.vec.fvec) &&
vm_vec_equal(self.vec.rvec, other.vec.rvec) &&
vm_vec_equal(self.vec.uvec, other.vec.uvec) &&
vm_vec_equal(self.vec.pos, other.vec.pos);
}
// -----------------------------------------------------------
// atan2_safe()
//
// Wrapper around atan2() that handles the special case of x == 0 and y == 0.
//
float atan2_safe(float y, float x)
{
// special case
if ( x == 0.0f && y == 0.0f )
return 0.0f;
return atan2(y, x);
}
// ---------------------------------------------------------------------
// vm_vec_component()
//
// finds projection of a vector along a unit (normalized) vector
//
float vm_vec_projection_parallel(vec3d *component, const vec3d *src, const vec3d *unit_vec)
{
float mag;
Assertion(vm_vec_is_normalized(unit_vec), "unit_vec must be normalized!");
mag = vm_vec_dot(src, unit_vec);
vm_vec_copy_scale(component, unit_vec, mag);
return mag;
}
// ---------------------------------------------------------------------
// vm_vec_projection_onto_plane()
//
// finds projection of a vector onto a plane specified by a unit normal vector
//
void vm_vec_projection_onto_plane(vec3d *projection, const vec3d *src, const vec3d *unit_normal)
{
float mag;
Assertion(vm_vec_is_normalized(unit_normal), "unit_normal must be normalized!");
mag = vm_vec_dot(src, unit_normal);
*projection = *src;
vm_vec_scale_add2(projection, unit_normal, -mag);
}
// ---------------------------------------------------------------------
// vm_vec_project_point_onto_plane()
//
// finds the point on a plane closest to a given point
// moves the point in the direction of the plane normal until it is on the plane
//
void vm_project_point_onto_plane(vec3d *new_point, const vec3d *point, const vec3d *plane_normal, const vec3d *plane_point)
{
float D; // plane constant in Ax+By+Cz+D = 0 or dot(X,n) - dot(Xp,n) = 0, so D = -dot(Xp,n)
float dist;
Assertion(vm_vec_is_normalized(plane_normal), "plane_normal must be normalized!");
D = -vm_vec_dot(plane_point, plane_normal);
dist = vm_vec_dot(point, plane_normal) + D;
*new_point = *point;
vm_vec_scale_add2(new_point, plane_normal, -dist);
}
// Take abs(x), then sqrt. Could insert warning message if desired.
float asqrt(float x)
{
if (x < 0.0f)
return fl_sqrt(-x);
else
return fl_sqrt(x);
}
void vm_set_identity(matrix *m)
{
m->vec.rvec.xyz.x = 1.0f; m->vec.rvec.xyz.y = 0.0f; m->vec.rvec.xyz.z = 0.0f;
m->vec.uvec.xyz.x = 0.0f; m->vec.uvec.xyz.y = 1.0f; m->vec.uvec.xyz.z = 0.0f;
m->vec.fvec.xyz.x = 0.0f; m->vec.fvec.xyz.y = 0.0f; m->vec.fvec.xyz.z = 1.0f;
}
angles vm_angles_new(float p, float b, float h)
{
angles ang;
ang.p = p;
ang.b = b;
ang.h = h;
return ang;
}
vec3d vm_vec_new(float x, float y, float z)
{
vec3d vec;
vec.xyz.x = x;
vec.xyz.y = y;
vec.xyz.z = z;
return vec;
}
vec4 vm_vec4_new(float x, float y, float z, float w)
{
vec4 vec;
vec.xyzw.x = x;
vec.xyzw.y = y;
vec.xyzw.z = z;
vec.xyzw.w = w;
return vec;
}
matrix vm_matrix_new(float a0, float a1, float a2, float a3, float a4, float a5, float a6, float a7, float a8)
{
matrix m;
m.a1d[0] = a0;
m.a1d[1] = a1;
m.a1d[2] = a2;
m.a1d[3] = a3;
m.a1d[4] = a4;
m.a1d[5] = a5;
m.a1d[6] = a6;
m.a1d[7] = a7;
m.a1d[8] = a8;
return m;
}
matrix vm_matrix_new(vec3d rvec, vec3d uvec, vec3d fvec)
{
matrix m;
m.vec.rvec = rvec;
m.vec.uvec = uvec;
m.vec.fvec = fvec;
return m;
}
//adds two vectors, fills in dest, returns ptr to dest
//ok for dest to equal either source, but should use vm_vec_add2() if so
//dest = src0 + src1
void vm_vec_add(vec3d *dest, const vec3d *src0, const vec3d *src1)
{
dest->xyz.x = src0->xyz.x + src1->xyz.x;
dest->xyz.y = src0->xyz.y + src1->xyz.y;
dest->xyz.z = src0->xyz.z + src1->xyz.z;
}
//Component-wise multiplication of two vectors
void vm_vec_cmult(vec3d* dest, const vec3d* src0, const vec3d* src1) {
dest->xyz.x = src0->xyz.x * src1->xyz.x;
dest->xyz.y = src0->xyz.y * src1->xyz.y;
dest->xyz.z = src0->xyz.z * src1->xyz.z;
}
void vm_vec_cmult2(vec3d* dest, const vec3d* src) {
dest->xyz.x *= src->xyz.x;
dest->xyz.y *= src->xyz.y;
dest->xyz.z *= src->xyz.z;
}
//Component-wise division of two vectors
void vm_vec_cdiv(vec3d* dest, const vec3d* src0, const vec3d* src1) {
dest->xyz.x = src0->xyz.x / src1->xyz.x;
dest->xyz.y = src0->xyz.y / src1->xyz.y;
dest->xyz.z = src0->xyz.z / src1->xyz.z;
}
void vm_vec_cdiv2(vec3d* dest, const vec3d* src) {
dest->xyz.x /= src->xyz.x;
dest->xyz.y /= src->xyz.y;
dest->xyz.z /= src->xyz.z;
}
//subs two vectors, fills in dest, returns ptr to dest
//ok for dest to equal either source, but should use vm_vec_sub2() if so
//dest = src0 - src1
void vm_vec_sub(vec3d *dest, const vec3d *src0, const vec3d *src1)
{
dest->xyz.x = src0->xyz.x - src1->xyz.x;
dest->xyz.y = src0->xyz.y - src1->xyz.y;
dest->xyz.z = src0->xyz.z - src1->xyz.z;
}
//adds one vector to another. returns ptr to dest
//dest can equal source
//dest += src
void vm_vec_add2(vec3d *dest, const vec3d *src)
{
dest->xyz.x += src->xyz.x;
dest->xyz.y += src->xyz.y;
dest->xyz.z += src->xyz.z;
}
//subs one vector from another, returns ptr to dest
//dest can equal source
//dest -= src
void vm_vec_sub2(vec3d *dest, const vec3d *src)
{
dest->xyz.x -= src->xyz.x;
dest->xyz.y -= src->xyz.y;
dest->xyz.z -= src->xyz.z;
}
//averages n vectors. returns ptr to dest
//dest can equal any vector in src[]
//dest = sum(src[]) / n
vec3d *vm_vec_avg_n(vec3d *dest, int n, const vec3d src[])
{
float x = 0.0f, y = 0.0f, z = 0.0f;
float inv_n = 1.0f / (float) n;;
for(int i = 0; i<n; i++){
x += src[i].xyz.x;
y += src[i].xyz.y;
z += src[i].xyz.z;
}
dest->xyz.x = x * inv_n;
dest->xyz.y = y * inv_n;
dest->xyz.z = z * inv_n;
return dest;
}
//Calculates the componentwise minimum of the two vectors
void vm_vec_min(vec3d* dest, const vec3d* src0, const vec3d* src1) {
dest->xyz.x = std::min(src0->xyz.x, src1->xyz.x);
dest->xyz.y = std::min(src0->xyz.y, src1->xyz.y);
dest->xyz.z = std::min(src0->xyz.z, src1->xyz.z);
}
//Calculates the componentwise maximum of the two vectors
void vm_vec_max(vec3d* dest, const vec3d* src0, const vec3d* src1) {
dest->xyz.x = std::max(src0->xyz.x, src1->xyz.x);
dest->xyz.y = std::max(src0->xyz.y, src1->xyz.y);
dest->xyz.z = std::max(src0->xyz.z, src1->xyz.z);
}
//averages two vectors. returns ptr to dest
//dest can equal either source
//dest = (src0 + src1) * 0.5
vec3d *vm_vec_avg(vec3d *dest, const vec3d *src0, const vec3d *src1)
{
dest->xyz.x = (src0->xyz.x + src1->xyz.x) * 0.5f;
dest->xyz.y = (src0->xyz.y + src1->xyz.y) * 0.5f;
dest->xyz.z = (src0->xyz.z + src1->xyz.z) * 0.5f;
return dest;
}
//averages three vectors. returns ptr to dest
//dest can equal any source
//dest = (src0 + src1 + src2) *0.33
vec3d *vm_vec_avg3(vec3d *dest, const vec3d *src0, const vec3d *src1, const vec3d *src2)
{
dest->xyz.x = (src0->xyz.x + src1->xyz.x + src2->xyz.x) * 0.333333333f;
dest->xyz.y = (src0->xyz.y + src1->xyz.y + src2->xyz.y) * 0.333333333f;
dest->xyz.z = (src0->xyz.z + src1->xyz.z + src2->xyz.z) * 0.333333333f;
return dest;
}
//averages four vectors. returns ptr to dest
//dest can equal any source
//dest = (src0 + src1 + src2 + src3) * 0.25
vec3d *vm_vec_avg4(vec3d *dest, const vec3d *src0, const vec3d *src1, const vec3d *src2, const vec3d *src3)
{
dest->xyz.x = (src0->xyz.x + src1->xyz.x + src2->xyz.x + src3->xyz.x) * 0.25f;
dest->xyz.y = (src0->xyz.y + src1->xyz.y + src2->xyz.y + src3->xyz.y) * 0.25f;
dest->xyz.z = (src0->xyz.z + src1->xyz.z + src2->xyz.z + src3->xyz.z) * 0.25f;
return dest;
}
//scales a vector in place.
//dest *= s
void vm_vec_scale(vec3d *dest, float s)
{
dest->xyz.x = dest->xyz.x * s;
dest->xyz.y = dest->xyz.y * s;
dest->xyz.z = dest->xyz.z * s;
}
//scales a 4-component vector in place.
// dest *= s
void vm_vec_scale(vec4 *dest, float s)
{
dest->xyzw.x = dest->xyzw.x * s;
dest->xyzw.y = dest->xyzw.y * s;
dest->xyzw.z = dest->xyzw.z * s;
dest->xyzw.w = dest->xyzw.w * s;
}
//scales and copies a vector.
// dest = src * s
void vm_vec_copy_scale(vec3d *dest, const vec3d *src, float s)
{
dest->xyz.x = src->xyz.x*s;
dest->xyz.y = src->xyz.y*s;
dest->xyz.z = src->xyz.z*s;
}
//scales a vector, adds it to another, and stores in a 3rd vector
//dest = src1 + k * src2
void vm_vec_scale_add(vec3d *dest, const vec3d *src1, const vec3d *src2, float k)
{
dest->xyz.x = src1->xyz.x + src2->xyz.x*k;
dest->xyz.y = src1->xyz.y + src2->xyz.y*k;
dest->xyz.z = src1->xyz.z + src2->xyz.z*k;
}
//scales a vector, subtracts it from another, and stores in a 3rd vector
//dest = src1 - (k * src2)
void vm_vec_scale_sub(vec3d *dest, const vec3d *src1, const vec3d *src2, float k)
{
dest->xyz.x = src1->xyz.x - src2->xyz.x*k;
dest->xyz.y = src1->xyz.y - src2->xyz.y*k;
dest->xyz.z = src1->xyz.z - src2->xyz.z*k;
}
//scales a vector and adds it to another
//dest += k * src
void vm_vec_scale_add2(vec3d *dest, const vec3d *src, float k)
{
dest->xyz.x += src->xyz.x*k;
dest->xyz.y += src->xyz.y*k;
dest->xyz.z += src->xyz.z*k;
}
//scales a vector and subtracts it from another
//dest -= k * src
void vm_vec_scale_sub2(vec3d *dest, const vec3d *src, float k)
{
dest->xyz.x -= src->xyz.x*k;
dest->xyz.y -= src->xyz.y*k;
dest->xyz.z -= src->xyz.z*k;
}
//scales a vector in place, taking n/d for scale.
//dest *= n/d
void vm_vec_scale2(vec3d *dest, float n, float d)
{
d = 1.0f/d;
dest->xyz.x = dest->xyz.x* n * d;
dest->xyz.y = dest->xyz.y* n * d;
dest->xyz.z = dest->xyz.z* n * d;
}
// interpolate between two vectors
// dest = src0 + (k * (src1 - src0))
// Might be helpful to think of vec0 as the before, and vec1 as the after
void vm_vec_linear_interpolate(vec3d* dest, const vec3d* src0, const vec3d* src1, const float k)
{
dest->xyz.x = ((src1->xyz.x - src0->xyz.x) * k) + src0->xyz.x;
dest->xyz.y = ((src1->xyz.y - src0->xyz.y) * k) + src0->xyz.y;
dest->xyz.z = ((src1->xyz.z - src0->xyz.z) * k) + src0->xyz.z;
}
//returns dot product of 2 vectors
float vm_vec_dot(const vec3d *v0, const vec3d *v1)
{
return (v1->xyz.x*v0->xyz.x)+(v1->xyz.y*v0->xyz.y)+(v1->xyz.z*v0->xyz.z);
}
//returns dot product of <x,y,z> and vector
float vm_vec_dot3(float x, float y, float z, const vec3d *v)
{
return (x*v->xyz.x)+(y*v->xyz.y)+(z*v->xyz.z);
}
//returns magnitude of a vector
float vm_vec_mag(const vec3d *v)
{
float mag1;
mag1 = (v->xyz.x * v->xyz.x) + (v->xyz.y * v->xyz.y) + (v->xyz.z * v->xyz.z);
if (mag1 <= 0.0f) {
return 0.0f;
}
return fl_sqrt(mag1);
}
//returns squared magnitude of a vector, useful if you want to compare distances
float vm_vec_mag_squared(const vec3d *v)
{
return ((v->xyz.x * v->xyz.x) + (v->xyz.y * v->xyz.y) + (v->xyz.z * v->xyz.z));
}
//returns the square of the difference between v0 and v1 (the distance, squared)
//just like vm_vec_mag_squared, but the distance between two points instead.
float vm_vec_dist_squared(const vec3d *v0, const vec3d *v1)
{
float dx, dy, dz;
dx = v0->xyz.x - v1->xyz.x;
dy = v0->xyz.y - v1->xyz.y;
dz = v0->xyz.z - v1->xyz.z;
return dx*dx + dy*dy + dz*dz;
}
//computes the distance between two points. (does sub and mag)
float vm_vec_dist(const vec3d *v0, const vec3d *v1)
{
float t1;
vec3d t;
vm_vec_sub(&t,v0,v1);
t1 = vm_vec_mag(&t);
return t1;
}
bool vm_vec_is_normalized(const vec3d *v)
{
// By the standards of FSO, it is sufficient to check that the magnitude is close to 1.
float mag = vm_vec_mag(v);
return mag > 0.999f && mag < 1.001f;
}
//normalize a vector. returns mag of source vec (always greater than zero)
float vm_vec_copy_normalize(vec3d *dest, const vec3d *src)
{
float m;
m = vm_vec_mag(src);
// Mainly here to trap attempts to normalize a null vector.
if (fl_near_zero(m)) {
// Since vectors are not expected to be null in this function, this really ought to be a Warning, as in the original release.
nprintf(("Network", "Null vec3d in vec3d normalize.\n"
"Trace out of vecmat.cpp and find offending code.\n"));
dest->xyz.x = 1.0f;
dest->xyz.y = 0.0f;
dest->xyz.z = 0.0f;
return 1.0f;
}
float im = 1.0f / m;
dest->xyz.x = src->xyz.x * im;
dest->xyz.y = src->xyz.y * im;
dest->xyz.z = src->xyz.z * im;
return m;
}
//normalize a vector. returns mag of source vec (always greater than zero)
float vm_vec_normalize(vec3d *v)
{
return vm_vec_copy_normalize(v,v);
}
// Normalize a vector.
// If vector is 0,0,0, return 1.0f, and change v to 1,0,0.
// Otherwise return the magnitude.
// No warning() generated for null vector, as it is expected that some vectors may be null.
float vm_vec_copy_normalize_safe(vec3d *dest, const vec3d *src, bool fallbackToZeroVec)
{
float m;
m = vm_vec_mag(src);
// Mainly here to trap attempts to normalize a null vector.
if (fl_near_zero(m)) {
if (fallbackToZeroVec) {
dest->xyz.x = 0.0f;
dest->xyz.y = 0.0f;
dest->xyz.z = 0.0f;
return 0.0f;
}
else {
dest->xyz.x = 1.0f;
dest->xyz.y = 0.0f;
dest->xyz.z = 0.0f;
return 1.0f;
}
}
float im = 1.0f / m;
dest->xyz.x = src->xyz.x * im;
dest->xyz.y = src->xyz.y * im;
dest->xyz.z = src->xyz.z * im;
return m;
}
// Normalize a vector.
// If vector is 0,0,0, return 1.0f, and change v to 1,0,0.
// Otherwise return the magnitude.
// No warning() generated for null vector.
float vm_vec_normalize_safe(vec3d *v, bool fallbackToZeroVec)
{
return vm_vec_copy_normalize_safe(v,v, fallbackToZeroVec);
}
//return the normalized direction vector between two points
//dest = normalized(end - start). Returns mag of direction vector
//NOTE: the order of the parameters matches the vector subtraction
float vm_vec_normalized_dir(vec3d *dest, const vec3d *end, const vec3d *start)
{
float t;
vm_vec_sub(dest,end,start);
// VECMAT-ERROR: NULL VEC3D (end == start)
t = vm_vec_normalize_safe(dest);
return t;
}
//computes surface normal from three points. result is normalized
//returns ptr to dest
//dest CANNOT equal either source
vec3d *vm_vec_normal(vec3d *dest, const vec3d *p0, const vec3d *p1, const vec3d *p2)
{
Assert(dest != p0);
Assert(dest != p1);
Assert(dest != p2);
vm_vec_perp(dest,p0,p1,p2);
vm_vec_normalize(dest);
return dest;
}
//computes cross product of two vectors.
//Note: this magnitude of the resultant vector is the
//product of the magnitudes of the two source vectors. This means it is
//quite easy for this routine to overflow and underflow. Be careful that
//your inputs are ok.
vec3d *vm_vec_cross(vec3d *dest, const vec3d *src0, const vec3d *src1)
{
dest->xyz.x = (src0->xyz.y * src1->xyz.z) - (src0->xyz.z * src1->xyz.y);
dest->xyz.y = (src0->xyz.z * src1->xyz.x) - (src0->xyz.x * src1->xyz.z);
dest->xyz.z = (src0->xyz.x * src1->xyz.y) - (src0->xyz.y * src1->xyz.x);
return dest;
}
int vm_test_parallel(const vec3d *src0, const vec3d *src1)
{
vec3d partial1;
vec3d partial2;
/*
* To test if two vectors are parallel, calculate their cross product.
* If the result is zero, then the vectors are parallel. It is better
* to compare the two cross product "partials" (for lack of a better
* word) against each other instead of the final cross product against
* zero.
*/
partial1.xyz.x = (src0->xyz.y * src1->xyz.z);
partial1.xyz.y = (src0->xyz.z * src1->xyz.x);
partial1.xyz.z = (src0->xyz.x * src1->xyz.y);
partial2.xyz.x = (src0->xyz.z * src1->xyz.y);
partial2.xyz.y = (src0->xyz.x * src1->xyz.z);
partial2.xyz.z = (src0->xyz.y * src1->xyz.x);
return vm_vec_equal(partial1, partial2);
}
//computes non-normalized surface normal from three points.
//returns ptr to dest
//dest CANNOT equal either source
vec3d *vm_vec_perp(vec3d *dest, const vec3d *p0, const vec3d *p1,const vec3d *p2)
{
Assert(dest != p0);
Assert(dest != p1);
Assert(dest != p2);
vec3d t0,t1;
vm_vec_sub(&t0,p1,p0);
vm_vec_sub(&t1,p2,p1);
return vm_vec_cross(dest,&t0,&t1);
}
//computes the delta angle between two vectors.
//vectors need not be normalized. if they are, call vm_vec_delta_ang_norm()
//the up vector (third parameter) can be NULL, in which case the absolute
//value of the angle in returned.
//Otherwise, the delta ang will be positive if the v0 -> v1 direction from the
//point of view of uvec is clockwise, negative if counterclockwise.
//This vector should be orthogonal to v0 and v1
float vm_vec_delta_ang(const vec3d *v0, const vec3d *v1, const vec3d *uvec)
{
float t;
vec3d t0,t1,t2;
vm_vec_copy_normalize(&t0,v0);
vm_vec_copy_normalize(&t1,v1);
if (uvec == nullptr) {
t = vm_vec_delta_ang_norm(&t0, &t1, NULL);
} else {
vm_vec_copy_normalize(&t2,uvec);
t = vm_vec_delta_ang_norm(&t0,&t1,&t2);
}
return t;
}
//computes the delta angle between two normalized vectors.
float vm_vec_delta_ang_norm(const vec3d *v0, const vec3d *v1, const vec3d *uvec)
{
float a;
vec3d t;
a = acosf_safe(vm_vec_dot(v0,v1));
if (uvec) {
vm_vec_cross(&t,v0,v1);
if ( vm_vec_dot(&t,uvec) < 0.0 ) {
a = -a;
}
}
return a;
}
// helper function that fills in matrix m based on provided sine and cosine values.
static matrix *sincos_2_matrix(matrix *m, float sinp, float cosp, float sinb, float cosb, float sinh, float cosh)
{
// This is the transpose of the Y1*X2*Z3 convention on wikipedia:
// https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
// [(rotate h on Y) * (rotate p on X) * (rotate b on Z)]^T
//
// [( ch 0 sh ) ( 1 0 0 ) ( cb -sb 0 )]^T
// = [( 0 1 0 ) * ( 0 cp -sp ) * ( sb cb 0 )]
// [(-sh 0 ch ) ( 0 sp cp ) ( 0 0 1 )]
//
// (cb*ch+sp*sb*sh sp*cb*sh-sb*ch sh*cp)^T (cb*ch+sp*sb*sh sb*cp sp*sb*ch-cb*sh) <- rvec
// = ( sb*cp cb*cp -sp ) = (sp*cb*sh-sb*ch cb*cp sb*sh+sp*cb*ch) <- uvec
// (sp*sb*ch-cb*sh sb*sh+sp*cb*ch ch*cp) ( sh*cp -sp ch*cp ) <- fvec
//
// Alternatively we can write it as
// [(rotate h on Y) * (rotate p on X) * (rotate b on Z)]^T
// = (rotate b on Z)^T * (rotate p on X)^T * (rotate h on Y)^T
// = (rotate -b on Z) * (rotate -p on X) * (rotate -h on Y)
// which is the "most common" Z1*X2*Y3 convention on wikipedia (but with negative angles).
float sbsh,cbch,cbsh,sbch;
sbsh = sinb*sinh;
cbch = cosb*cosh;
cbsh = cosb*sinh;
sbch = sinb*cosh;
m->vec.rvec.xyz.x = cbch + sinp*sbsh; //m1
m->vec.uvec.xyz.z = sbsh + sinp*cbch; //m8
m->vec.uvec.xyz.x = sinp*cbsh - sbch; //m2
m->vec.rvec.xyz.z = sinp*sbch - cbsh; //m7
m->vec.fvec.xyz.x = sinh*cosp; //m3
m->vec.rvec.xyz.y = sinb*cosp; //m4
m->vec.uvec.xyz.y = cosb*cosp; //m5
m->vec.fvec.xyz.z = cosh*cosp; //m9
m->vec.fvec.xyz.y = -sinp; //m6
return m;
}
//computes a matrix from a set of three angles. returns ptr to matrix
matrix *vm_angles_2_matrix(matrix *m, const angles *a)
{
matrix * t;
float sinp,cosp,sinb,cosb,sinh,cosh;
sinp = sinf(a->p); cosp = cosf(a->p);
sinb = sinf(a->b); cosb = cosf(a->b);
sinh = sinf(a->h); cosh = cosf(a->h);
t = sincos_2_matrix(m,sinp,cosp,sinb,cosb,sinh,cosh);
return t;
}
//computes a matrix from one angle.
// angle_index = 0,1,2 for p,b,h
matrix *vm_angle_2_matrix(matrix *m, float a, int angle_index)
{
matrix * t;
float sinp,cosp,sinb,cosb,sinh,cosh;
/*
* Initialize sin and cos variables using an initial angle of
* zero degrees. Recall that sin(0) = 0 and cos(0) = 1.
*/
sinp = 0.0f; cosp = 1.0f;
sinb = 0.0f; cosb = 1.0f;
sinh = 0.0f; cosh = 1.0f;
switch (angle_index) {
case 0:
sinp = sinf(a); cosp = cosf(a);
break;
case 1:
sinb = sinf(a); cosb = cosf(a);
break;
case 2:
sinh = sinf(a); cosh = cosf(a);
break;
}
t = sincos_2_matrix(m,sinp,cosp,sinb,cosb,sinh,cosh);
return t;
}
//computes a matrix from a forward vector and an angle
matrix *vm_vec_ang_2_matrix(matrix *m, const vec3d *v, float a)
{
matrix * t;
float sinb,cosb,sinp,cosp,sinh,cosh;
sinb = sinf(a); cosb = cosf(a);
sinp = -v->xyz.y;
cosp = fl_sqrt(1.0f - sinp*sinp);
sinh = v->xyz.x / cosp;
cosh = v->xyz.z / cosp;
t = sincos_2_matrix(m,sinp,cosp,sinb,cosb,sinh,cosh);
return t;
}
/**
* @brief Generates a matrix from a normalized fvec.
*
* @param[in,out] matrix The matrix to generate
*
* @details The matrix's fvec is used to generate the uvec and rvec
*
* @sa vm_vector_2_matrix(), vm_vector_2_matrix_norm()
*/
void vm_vector_2_matrix_gen_vectors(matrix *m)
{
vec3d *xvec=&m->vec.rvec;
vec3d *yvec=&m->vec.uvec;
vec3d *zvec=&m->vec.fvec;
if ((zvec->xyz.x==0.0f) && (zvec->xyz.z==0.0f)) { //forward vec is straight up or down
m->vec.rvec.xyz.x = 1.0f;
m->vec.uvec.xyz.z = (zvec->xyz.y<0.0f)?1.0f:-1.0f;
m->vec.rvec.xyz.y = m->vec.rvec.xyz.z = m->vec.uvec.xyz.x = m->vec.uvec.xyz.y = 0.0f;
}
else { //not straight up or down
xvec->xyz.x = zvec->xyz.z;
xvec->xyz.y = 0.0f;
xvec->xyz.z = -zvec->xyz.x;
vm_vec_normalize(xvec);
vm_vec_cross(yvec,zvec,xvec);
}
}
/**
* @brief Generates a matrix from a normalized uvec.
*
* @param[in,out] matrix The matrix to generate
*
* @details The matrix's uvec is used to generate the fvec and rvec
*
* @sa vm_vector_2_matrix_uvec(), vm_vector_2_matrix_uvec_norm()
*/
void vm_vector_2_matrix_gen_vectors_uvec(matrix* m)
{
vec3d* xvec = &m->vec.rvec;
vec3d* yvec = &m->vec.uvec;
vec3d* zvec = &m->vec.fvec;
if ((yvec->xyz.x == 0.0f) && (yvec->xyz.z == 0.0f)) { // vec is straight up or down
m->vec.rvec.xyz.x = 1.0f;
m->vec.fvec.xyz.z = (yvec->xyz.y > 0.0f) ? 1.0f : -1.0f;
m->vec.rvec.xyz.y = m->vec.rvec.xyz.z = m->vec.fvec.xyz.x = m->vec.fvec.xyz.y = 0.0f;
}
else { //not straight up or down
xvec->xyz.x = yvec->xyz.z;
xvec->xyz.y = 0.0f;
xvec->xyz.z = -yvec->xyz.x;
vm_vec_normalize(xvec);
vm_vec_cross(zvec, xvec, yvec);
}
}
matrix *vm_vector_2_matrix_norm(matrix *m, const vec3d *fvec, const vec3d *uvec, const vec3d *rvec)
{
// sanity
Assertion(fvec == nullptr || vm_vec_is_normalized(fvec), "if fvec is provided, it must be normalized!");
Assertion(uvec == nullptr || vm_vec_is_normalized(uvec), "if uvec is provided, it must be normalized!");
Assertion(rvec == nullptr || vm_vec_is_normalized(rvec), "if rvec is provided, it must be normalized!");
matrix temp = vmd_identity_matrix;
vec3d *xvec=&temp.vec.rvec;
vec3d *yvec=&temp.vec.uvec;
vec3d *zvec=&temp.vec.fvec;
Assert(fvec != NULL);
*zvec = *fvec;
if (uvec == NULL) {
if (rvec == NULL) { //just forward vec
vm_vector_2_matrix_gen_vectors(&temp);
}
else { //use right vec
*xvec = *rvec;
vm_vec_cross(yvec,zvec,xvec);
//normalize new perpendicular vector
vm_vec_normalize(yvec);
//now recompute right vector, in case it wasn't entirely perpendiclar
vm_vec_cross(xvec,yvec,zvec);
}
}
else { //use up vec
*yvec = *uvec;
vm_vec_cross(xvec,yvec,zvec);
if (vm_vec_equal(*xvec, vmd_zero_vector)) {
// uvec was bogus (either same as fvec or -fvec)
// Reset temp to the original values and do the setup again
temp = *m;
temp.vec.fvec = *fvec;
vm_vector_2_matrix_gen_vectors(&temp);
}
else {
//normalize new perpendicular vector
vm_vec_normalize(xvec);
//now recompute up vector, in case it wasn't entirely perpendiclar
vm_vec_cross(yvec,zvec,xvec);
}
}
// Copy the computed values into the output parameter
*m = temp;
return m;
}
matrix* vm_vector_2_matrix_uvec_norm(matrix* m, const vec3d* fvec, const vec3d* uvec, const vec3d* rvec)
{
// sanity
Assertion(fvec == nullptr || vm_vec_is_normalized(fvec), "if fvec is provided, it must be normalized!");
Assertion(uvec == nullptr || vm_vec_is_normalized(uvec), "if uvec is provided, it must be normalized!");
Assertion(rvec == nullptr || vm_vec_is_normalized(rvec), "if rvec is provided, it must be normalized!");
Assertion(fvec != nullptr || uvec != nullptr, "vm_vector_2_matrix_uvec_norm requires one of fvec or uvec!");
matrix temp = vmd_identity_matrix;
vec3d* xvec = &temp.vec.rvec;
vec3d* yvec = &temp.vec.uvec;
vec3d* zvec = &temp.vec.fvec;
if (uvec != nullptr) { // use up vec as the 'primary'
*yvec = *uvec;
if (rvec == nullptr && fvec == nullptr) { //only up vec
vm_vector_2_matrix_gen_vectors_uvec(&temp);
}
else if (fvec != nullptr){ //use fwd vec
*zvec = *fvec;
vm_vec_cross(xvec, yvec, zvec);
//normalize new perpendicular vector
vm_vec_normalize(xvec);
//now recompute fwd vector, in case it wasn't entirely perpendiclar
vm_vec_cross(zvec, xvec, yvec);
} else { // else use rvec
*xvec = *rvec;
vm_vec_cross(zvec, xvec, yvec);
//normalize new perpendicular vector
vm_vec_normalize(zvec);
//now recompute right vector, in case it wasn't entirely perpendiclar
vm_vec_cross(xvec, yvec, zvec);
}
}
else if (fvec != nullptr) { //use fwd vec as the 'primary'
*zvec = *fvec;
if (rvec != nullptr) { // use rvec
*xvec = *rvec;
vm_vec_cross(yvec, zvec, xvec);
//normalize new perpendicular vector
vm_vec_normalize(yvec);
//now recompute right vector, in case it wasn't entirely perpendiclar
vm_vec_cross(xvec, yvec, zvec);
} else { // only fwd vec
vm_vector_2_matrix_gen_vectors(&temp);