forked from felselva/mathc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathc.c
More file actions
1902 lines (1672 loc) · 42.1 KB
/
Copy pathmathc.c
File metadata and controls
1902 lines (1672 loc) · 42.1 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) 2016 Felipe Ferreira da Silva
This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use of
this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <math.h>
#include <float.h>
#include "mathc.h"
/* Check C standard */
#ifdef __STDC__
#define PREDEF_STANDARD_C89
#ifdef __STDC_VERSION__
#if __STDC_VERSION__ >= 199901L
#define PREDEF_STANDARD_C99
#endif
#endif
#endif
/* Use `extern inline` for C99 or later */
#ifdef PREDEF_STANDARD_C99
#define MATHC_EXTERN_INLINE extern inline
#else
#define MATHC_EXTERN_INLINE
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* Utils */
int nearly_equal(float a, float b, float epsilon)
{
int result = FALSE;
float abs_a = fabsf(a);
float abs_b = fabsf(b);
float diff = fabsf(a - b);
if (a == b) {
result = TRUE;
} else if (a == 0.0f || b == 0.0f || diff < FLT_EPSILON) {
result = diff < epsilon;
} else {
result = diff / fminf(abs_a + abs_b, FLT_MAX) < epsilon;
}
return result;
}
float to_radians(float degrees)
{
return degrees * M_PIF / 180.0f;
}
float to_degrees(float radians)
{
return radians * 180.0f / M_PIF;
}
/* Vector 2D */
void to_pvector2(float x, float y, struct vec *result)
{
result->x = x;
result->y = y;
result->z = 0.0f;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec to_vector2(float x, float y)
{
struct vec result;
to_pvector2(x, y, &result);
return result;
}
void pvector2_add(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x + b->x;
result->y = a->y + b->y;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_add(struct vec a, struct vec b)
{
struct vec result;
pvector2_add(&a, &b, &result);
return result;
}
void pvector2_subtract(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x - b->x;
result->y = a->y - b->y;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_subtract(struct vec a, struct vec b)
{
struct vec result;
pvector2_subtract(&a, &b, &result);
return result;
}
void pvector2_scale(struct vec *a, float scale, struct vec *result)
{
result->x = a->x * scale;
result->y = a->y * scale;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_scale(struct vec a, float scale)
{
struct vec result;
pvector2_scale(&a, scale, &result);
return result;
}
void pvector2_multiply(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x * b->x;
result->y = a->y * b->y;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_multiply(struct vec a, struct vec b)
{
struct vec result;
pvector2_multiply(&a, &b, &result);
return result;
}
void pvector2_divide(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x / b->x;
result->y = a->y / b->y;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_divide(struct vec a, struct vec b)
{
struct vec result;
pvector2_divide(&a, &b, &result);
return result;
}
void pvector2_negative(struct vec *a, struct vec *result)
{
result->x = -a->x;
result->y = -a->y;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_negative(struct vec a)
{
struct vec result;
pvector2_negative(&a, &result);
return result;
}
void pvector2_inverse(struct vec *a, struct vec *result)
{
if (a->x != 0.0f) {
result->x = 1.0f / a->x;
} else {
result->x = 0.0f;
}
if (a->y != 0.0f) {
result->y = 1.0f / a->y;
} else {
result->y = 0.0f;
}
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_inverse(struct vec a)
{
struct vec result;
pvector2_inverse(&a, &result);
return result;
}
void pvector2_abs(struct vec *a, struct vec *result)
{
result->x = fabsf(a->x);
result->y = fabsf(a->y);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_abs(struct vec a)
{
struct vec result;
pvector2_abs(&a, &result);
return result;
}
void pvector2_floor(struct vec *a, struct vec *result)
{
result->x = floorf(a->x);
result->y = floorf(a->y);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_floor(struct vec a)
{
struct vec result;
pvector2_floor(&a, &result);
return result;
}
void pvector2_ceil(struct vec *a, struct vec *result)
{
result->x = ceilf(a->x);
result->y = ceilf(a->y);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_ceil(struct vec a)
{
struct vec result;
pvector2_ceil(&a, &result);
return result;
}
void pvector2_round(struct vec *a, struct vec *result)
{
result->x = roundf(a->x);
result->y = roundf(a->y);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_round(struct vec a)
{
struct vec result;
pvector2_round(&a, &result);
return result;
}
void pvector2_max(struct vec *a, struct vec *b, struct vec *result)
{
result->x = fmaxf(a->x, b->x);
result->y = fmaxf(a->y, b->y);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_max(struct vec a, struct vec b)
{
struct vec result;
pvector2_max(&a, &b, &result);
return result;
}
void pvector2_min(struct vec *a, struct vec *b, struct vec *result)
{
result->x = fminf(a->x, b->x);
result->y = fminf(a->y, b->y);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_min(struct vec a, struct vec b)
{
struct vec result;
pvector2_min(&a, &b, &result);
return result;
}
float pvector2_dot(struct vec *a, struct vec *b)
{
return a->x * b->x + a->y * b->y;
}
MATHC_EXTERN_INLINE float vector2_dot(struct vec a, struct vec b)
{
return pvector2_dot(&a, &b);
}
float pvector2_angle(struct vec *a)
{
return atan2f(a->y, a->x);
}
MATHC_EXTERN_INLINE float vector2_angle(struct vec a)
{
return pvector2_angle(&a);
}
float pvector2_length_squared(struct vec *a)
{
return a->x * a->x + a->y * a->y;
}
MATHC_EXTERN_INLINE float vector2_length_squared(struct vec a)
{
return pvector2_length_squared(&a);
}
float pvector2_length(struct vec *a)
{
return sqrtf(a->x * a->x + a->y * a->y);
}
MATHC_EXTERN_INLINE float vector2_length(struct vec a)
{
return pvector2_length(&a);
}
void pvector2_normalize(struct vec *a, struct vec *result)
{
float length = a->x * a->x + a->y * a->y;
if (length != 0.0f) {
length = sqrtf(length);
result->x = a->x / length;
result->y = a->y / length;
} else {
result->x = 0.0f;
result->y = 0.0f;
}
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_normalize(struct vec a)
{
struct vec result;
pvector2_normalize(&a, &result);
return result;
}
void pvector2_slide(struct vec *a, struct vec *normal, struct vec *result)
{
float d = pvector2_dot(a, normal);
result->x = a->x - normal->x * d;
result->y = a->y - normal->y * d;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_slide(struct vec a, struct vec normal)
{
struct vec result;
pvector2_slide(&a, &normal, &result);
return result;
}
void pvector2_reflect(struct vec *a, struct vec *normal, struct vec *result)
{
float d = 2.0f * pvector2_dot(a, normal);
result->x = a->x - normal->x * d;
result->y = a->y - normal->y * d;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_reflect(struct vec a, struct vec normal)
{
struct vec result;
pvector2_reflect(&a, &normal, &result);
return result;
}
void pvector2_tangent(struct vec *a, struct vec *result)
{
result->x = a->y;
result->y = -a->x;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_tangent(struct vec a)
{
struct vec result;
pvector2_tangent(&a, &result);
return result;
}
void pvector2_rotate(struct vec *a, float angle, struct vec *result)
{
float cs = cosf(angle);
float sn = sinf(angle);
float x = a->x;
float y = a->y;
result->x = x * cs - y * sn;
result->y = x * sn + y * cs;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_rotate(struct vec a, float angle)
{
struct vec result;
pvector2_rotate(&a, angle, &result);
return result;
}
float pvector2_distance_to(struct vec *a, struct vec *b)
{
return sqrtf((a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y));
}
MATHC_EXTERN_INLINE float vector2_distance_to(struct vec a, struct vec b)
{
return pvector2_distance_to(&a, &b);
}
float pvector2_distance_squared_to(struct vec *a, struct vec *b)
{
return (a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y);
}
MATHC_EXTERN_INLINE float vector2_distance_squared_to(struct vec a, struct vec b)
{
return pvector2_distance_squared_to(&a, &b);
}
void pvector2_linear_interpolation(struct vec *a, struct vec *b, float p, struct vec *result)
{
result->x = a->x + (b->x - a->x) * p;
result->y = a->y + (b->y - a->y) * p;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector2_linear_interpolation(struct vec a, struct vec b, float p)
{
struct vec result;
pvector2_linear_interpolation(&a, &b, p, &result);
return result;
}
/* Vector 3D */
void to_pvector3(float x, float y, float z, struct vec *result)
{
result->x = x;
result->y = y;
result->z = z;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec to_vector3(float x, float y, float z)
{
struct vec result;
to_pvector3(x, y, z, &result);
return result;
}
void pvector3_add(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x + b->x;
result->y = a->y + b->y;
result->z = a->z + b->z;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_add(struct vec a, struct vec b)
{
struct vec result;
pvector3_add(&a, &b, &result);
return result;
}
void pvector3_subtract(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x - b->x;
result->y = a->y - b->y;
result->z = a->z - b->z;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_subtract(struct vec a, struct vec b)
{
struct vec result;
pvector3_subtract(&a, &b, &result);
return result;
}
void pvector3_scale(struct vec *a, float scale, struct vec *result)
{
result->x = a->x * scale;
result->y = a->y * scale;
result->z = a->z * scale;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_scale(struct vec a, float scale)
{
struct vec result;
pvector3_scale(&a, scale, &result);
return result;
}
void pvector3_multiply(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x * b->x;
result->y = a->y * b->y;
result->z = a->z * b->z;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_multiply(struct vec a, struct vec b)
{
struct vec result;
pvector3_multiply(&a, &b, &result);
return result;
}
void pvector3_divide(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x / b->x;
result->y = a->y / b->y;
result->z = a->z / b->z;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_divide(struct vec a, struct vec b)
{
struct vec result;
pvector3_divide(&a, &b, &result);
return result;
}
void pvector3_negative(struct vec *a, struct vec *result)
{
result->x = -a->x;
result->y = -a->y;
result->z = -a->z;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_negative(struct vec a)
{
struct vec result;
pvector3_negative(&a, &result);
return result;
}
void pvector3_inverse(struct vec *a, struct vec *result)
{
if (a->x != 0.0f) {
result->x = 1.0f / a->x;
} else {
result->x = 0.0f;
}
if (a->y != 0.0f) {
result->y = 1.0f / a->y;
} else {
result->y = 0.0f;
}
if (a->z != 0.0f) {
result->z = 1.0f / a->z;
} else {
result->z = 0.0f;
}
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_inverse(struct vec a)
{
struct vec result;
pvector3_inverse(&a, &result);
return result;
}
void pvector3_abs(struct vec *a, struct vec *result)
{
result->x = fabsf(a->x);
result->y = fabsf(a->y);
result->z = fabsf(a->z);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_abs(struct vec a)
{
struct vec result;
pvector3_abs(&a, &result);
return result;
}
void pvector3_floor(struct vec *a, struct vec *result)
{
result->x = floorf(a->x);
result->y = floorf(a->y);
result->z = floorf(a->z);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_floor(struct vec a)
{
struct vec result;
pvector3_floor(&a, &result);
return result;
}
void pvector3_ceil(struct vec *a, struct vec *result)
{
result->x = ceilf(a->x);
result->y = ceilf(a->y);
result->z = ceilf(a->z);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_ceil(struct vec a)
{
struct vec result;
pvector3_ceil(&a, &result);
return result;
}
void pvector3_round(struct vec *a, struct vec *result)
{
result->x = roundf(a->x);
result->y = roundf(a->y);
result->z = roundf(a->z);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_round(struct vec a)
{
struct vec result;
pvector3_round(&a, &result);
return result;
}
void pvector3_max(struct vec *a, struct vec *b, struct vec *result)
{
result->x = fmaxf(a->x, b->x);
result->y = fmaxf(a->y, b->y);
result->z = fmaxf(a->z, b->z);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_max(struct vec a, struct vec b)
{
struct vec result;
pvector3_max(&a, &b, &result);
return result;
}
void pvector3_min(struct vec *a, struct vec *b, struct vec *result)
{
result->x = fminf(a->x, b->x);
result->y = fminf(a->y, b->y);
result->z = fminf(a->z, b->z);
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_min(struct vec a, struct vec b)
{
struct vec result;
pvector3_min(&a, &b, &result);
return result;
}
float pvector3_dot(struct vec *a, struct vec *b)
{
return a->x * b->x + a->y * b->y + a->z * b->z;
}
MATHC_EXTERN_INLINE float vector3_dot(struct vec a, struct vec b)
{
return pvector3_dot(&a, &b);
}
void pvector3_cross(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->y * b->z - a->z * b->y;
result->y = a->z * b->x - a->x * b->z;
result->z = a->x * b->y - a->y * b->x;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_cross(struct vec a, struct vec b)
{
struct vec result;
pvector3_cross(&a, &b, &result);
return result;
}
float pvector3_length_squared(struct vec *a)
{
return a->x * a->x + a->y * a->y + a->z * a->z;
}
MATHC_EXTERN_INLINE float vector3_length_squared(struct vec a)
{
return pvector3_length_squared(&a);
}
float pvector3_length(struct vec *a)
{
return sqrtf(a->x * a->x + a->y * a->y + a->z * a->z);
}
MATHC_EXTERN_INLINE float vector3_length(struct vec a)
{
return pvector3_length(&a);
}
void pvector3_normalize(struct vec *a, struct vec *result)
{
float length = a->x * a->x + a->y * a->y + a->z * a->z;
if (length != 0.0f) {
length = sqrtf(length);
result->x = a->x / length;
result->y = a->y / length;
result->z = a->z / length;
} else {
result->x = 0.0f;
result->y = 0.0f;
result->z = 0.0f;
}
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_normalize(struct vec a)
{
struct vec result;
pvector3_normalize(&a, &result);
return result;
}
void pvector3_slide(struct vec *a, struct vec *normal, struct vec *result)
{
float d = pvector3_dot(a, normal);
result->x = a->x - normal->x * d;
result->y = a->y - normal->y * d;
result->y = a->z - normal->z * d;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_slide(struct vec a, struct vec normal)
{
struct vec result;
pvector3_slide(&a, &normal, &result);
return result;
}
void pvector3_reflect(struct vec *a, struct vec *normal, struct vec *result)
{
float d = 2.0f * pvector3_dot(a, normal);
result->x = a->x - normal->x * d;
result->y = a->y - normal->y * d;
result->z = a->z - normal->z * d;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_reflect(struct vec a, struct vec normal)
{
struct vec result;
pvector3_reflect(&a, &normal, &result);
return result;
}
float pvector3_distance_to(struct vec *a, struct vec *b)
{
return sqrtf((a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y) + (a->z - b->z) * (a->z - b->z));
}
MATHC_EXTERN_INLINE float vector3_distance_to(struct vec a, struct vec b)
{
return pvector3_distance_to(&a, &b);
}
float pvector3_distance_squared_to(struct vec *a, struct vec *b)
{
return (a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y) + (a->z - b->z) * (a->z - b->z);
}
MATHC_EXTERN_INLINE float vector3_distance_squared_to(struct vec a, struct vec b)
{
return pvector3_distance_squared_to(&a, &b);
}
void pvector3_linear_interpolation(struct vec *a, struct vec *b, float p, struct vec *result)
{
result->x = a->x + (b->x - a->x) * p;
result->y = a->y + (b->y - a->y) * p;
result->z = a->z + (b->z - a->z) * p;
result->w = 1.0f;
}
MATHC_EXTERN_INLINE struct vec vector3_linear_interpolation(struct vec a, struct vec b, float p)
{
struct vec result;
pvector3_linear_interpolation(&a, &b, p, &result);
return result;
}
/* Quaternion */
void to_pquaternion(float x, float y, float z, float w, struct vec *result)
{
result->x = x;
result->y = y;
result->z = z;
result->w = w;
}
struct vec to_quaternion(float x, float y, float z, float w)
{
struct vec result;
to_pquaternion(x, y, z, w, &result);
return result;
}
void pquaternion_add(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x + b->x;
result->y = a->y + b->y;
result->z = a->z + b->z;
result->w = a->w + b->w;
}
MATHC_EXTERN_INLINE struct vec quaternion_add(struct vec a, struct vec b)
{
struct vec result;
pquaternion_add(&a, &b, &result);
return result;
}
void pquaternion_subtract(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->x - b->x;
result->y = a->y - b->y;
result->y = a->z - b->z;
result->w = a->w - b->w;
}
MATHC_EXTERN_INLINE struct vec quaternion_subtract(struct vec a, struct vec b)
{
struct vec result;
pquaternion_subtract(&a, &b, &result);
return result;
}
void pquaternion_scale(struct vec *a, float scale, struct vec *result)
{
result->x = a->x * scale;
result->y = a->y * scale;
result->z = a->z * scale;
result->w = a->w * scale;
}
MATHC_EXTERN_INLINE struct vec quaternion_scale(struct vec a, float scale)
{
struct vec result;
pquaternion_scale(&a, scale, &result);
return result;
}
void pquaternion_multiply(struct vec *a, struct vec *b, struct vec *result)
{
result->x = a->w * b->x + a->x * b->w + a->y * b->z - a->z * b->y;
result->y = a->w * b->y + a->y * b->w + a->z * b->x - a->x * b->z;
result->z = a->w * b->z + a->z * b->w + a->x * b->y - a->y * b->x;
result->w = a->w * b->w - a->x * b->x - a->y * b->y - a->z * b->z;
}
MATHC_EXTERN_INLINE struct vec quaternion_multiply(struct vec a, struct vec b)
{
struct vec result;
pquaternion_multiply(&a, &b, &result);
return result;
}
void pquaternion_divide(struct vec *a, struct vec *b, struct vec *result)
{
float x = a->x;
float y = a->y;
float z = a->z;
float w = a->w;
float n1 = b->x * b->x + b->y * b->y + b->z * b->z + b->w * b->w;
float n2 = 1.0f / n1;
float n3 = -b->x * n2;
float n4 = -b->y * n2;
float n5 = -b->z * n2;
float n6 = b->w * n2;
float n7 = y * n5 - z * n4;
float n8 = z * n3 - x * n5;
float n9 = x * n4 - y * n3;
float n10 = x * n3 + y * n4 + z * n5;
result->x = x * n6 + n3 * w + n7;
result->y = y * n6 + n4 * w + n8;
result->z = z * n6 + n5 * w + n9;
result->w = w * n6 - n10;
}
MATHC_EXTERN_INLINE struct vec quaternion_divide(struct vec a, struct vec b)
{
struct vec result;
pquaternion_divide(&a, &b, &result);
return result;
}
void pquaternion_negative(struct vec *a, struct vec *result)
{
result->x = -a->x;
result->y = -a->y;
result->z = -a->z;
result->w = -a->w;
}
MATHC_EXTERN_INLINE struct vec quaternion_negative(struct vec a)
{
struct vec result;
pquaternion_negative(&a, &result);
return result;
}
void pquaternion_conjugate(struct vec *a, struct vec *result)
{
result->x = -a->x;
result->y = -a->y;
result->z = -a->z;
result->w = a->w;
}
MATHC_EXTERN_INLINE struct vec quaternion_conjugate(struct vec a)
{
struct vec result;
pquaternion_conjugate(&a, &result);
return result;
}
void pquaternion_inverse(struct vec *a, struct vec *result)
{
float n1 = sqrtf(a->x * a->x + a->y * a->y + a->z * a->z + a->w * a->w);
float n2 = 1.0f / n1;
result->x = -a->x * n2;
result->y = -a->y * n2;
result->z = -a->z * n2;
result->w = a->w * n2;
}
MATHC_EXTERN_INLINE struct vec quaternion_inverse(struct vec a)
{
struct vec result;
pquaternion_inverse(&a, &result);
return result;
}
void pquaternion_abs(struct vec *a, struct vec *result)
{
result->x = fabsf(a->x);
result->y = fabsf(a->y);
result->z = fabsf(a->z);
result->w = fabsf(a->w);
}
MATHC_EXTERN_INLINE struct vec quaternion_abs(struct vec a)
{
struct vec result;
pquaternion_abs(&a, &result);
return result;
}
void pquaternion_floor(struct vec *a, struct vec *result)
{
result->x = floorf(a->x);
result->y = floorf(a->y);
result->z = floorf(a->z);
result->w = floorf(a->w);
}
MATHC_EXTERN_INLINE struct vec quaternion_floor(struct vec a)
{
struct vec result;
pquaternion_floor(&a, &result);
return result;
}
void pquaternion_ceil(struct vec *a, struct vec *result)
{
result->x = ceilf(a->x);
result->y = ceilf(a->y);
result->z = ceilf(a->z);
result->w = ceilf(a->w);
}
MATHC_EXTERN_INLINE struct vec quaternion_ceil(struct vec a)
{
struct vec result;
pquaternion_ceil(&a, &result);
return result;
}
void pquaternion_round(struct vec *a, struct vec *result)
{
result->x = roundf(a->x);
result->y = roundf(a->y);
result->z = roundf(a->z);
result->w = roundf(a->w);
}