Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
vec.h
Go to the documentation of this file.
1
46#ifndef BJ_VEC_H
47#define BJ_VEC_H
48
49#include <banjo/api.h>
50#include <banjo/math.h>
51
61
63#define BJ_VEC2_ZERO ((struct bj_vec2){BJ_FZERO, BJ_FZERO})
64
75
77#define BJ_VEC3_ZERO ((struct bj_vec3){BJ_FZERO, BJ_FZERO, BJ_FZERO})
78
91
93#define BJ_VEC4_ZERO ((struct bj_vec4){BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO})
94
108 struct bj_vec2 a,
109 bj_real(*f)(bj_real)
110) {
111 return (struct bj_vec2){ .x = f(a.x), .y = f(a.y), };
112}
113
121static BJ_INLINE struct bj_vec2 bj_vec2_add(struct bj_vec2 lhs, struct bj_vec2 rhs) {
122 return (struct bj_vec2){ .x = lhs.x + rhs.x, .y = lhs.y + rhs.y, };
123}
124
138 struct bj_vec2 lhs,
139 struct bj_vec2 rhs,
140 bj_real s
141) {
142 return (struct bj_vec2){ .x = lhs.x + rhs.x * s, .y = lhs.y + rhs.y * s, };
143}
144
153 const struct bj_vec2 lhs,
154 const struct bj_vec2 rhs
155) {
156 return (struct bj_vec2){ .x = lhs.x - rhs.x, .y = lhs.y - rhs.y, };
157}
158
166static BJ_INLINE struct bj_vec2 bj_vec2_scale(struct bj_vec2 v, bj_real s) {
167 return (struct bj_vec2){ .x = v.x * s, .y = v.y * s, };
168}
169
182 const struct bj_vec2 v,
183 const struct bj_vec2 s
184) {
185 return (struct bj_vec2){ .x = v.x * s.x, .y = v.y * s.y, };
186}
187
195static BJ_INLINE bj_real bj_vec2_dot(struct bj_vec2 a, struct bj_vec2 b) {
196 return a.x * b.x + a.y * b.y;
197}
198
218 return a.x*b.y - a.y*b.x;
219}
220
228 return bj_sqrt(v.x * v.x + v.y * v.y);
229}
230
244 const bj_real l = bj_vec2_len(v);
245 if (bj_real_is_zero(l)){
246 return (struct bj_vec2){BJ_FZERO,BJ_FZERO};
247 }
248 return bj_vec2_scale(v, L / l);
249}
250
264 const bj_real dx = a.x - b.x;
265 const bj_real dy = a.y - b.y;
266 return dx * dx + dy * dy;
267}
268
276static BJ_INLINE bj_real bj_vec2_distance(const struct bj_vec2 a, const struct bj_vec2 b) {
277 return bj_sqrt(bj_vec2_distance_sq(a, b));
278}
279
296 const bj_real l2 = v.x * v.x + v.y * v.y;
297 // A length of at most BJ_EPSILON is zero, as for *_scale_to_len
298 if (l2 <= BJ_EPSILON * BJ_EPSILON) {
299 return (struct bj_vec2){ BJ_FZERO, BJ_FZERO };
300 }
301 const bj_real inv = BJ_F(1.0) / bj_sqrt(l2);
302 return (struct bj_vec2){ v.x * inv, v.y * inv };
303}
304
321 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y);
322 return (struct bj_vec2){ v.x * inv, v.y * inv };
323}
324
332static BJ_INLINE struct bj_vec2 bj_vec2_min(struct bj_vec2 a, struct bj_vec2 b) {
333 return (struct bj_vec2){a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, };
334}
335
343static BJ_INLINE struct bj_vec2 bj_vec2_max(struct bj_vec2 a, struct bj_vec2 b) {
344 return (struct bj_vec2){a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, };
345}
346
355 struct bj_vec3 a,
356 bj_real(*f)(bj_real)
357) {
358 return (struct bj_vec3){ .x = f(a.x), .y = f(a.y), .z = f(a.z), };
359}
360
368static BJ_INLINE struct bj_vec3 bj_vec3_add(struct bj_vec3 lhs, struct bj_vec3 rhs) {
369 return (struct bj_vec3){
370 .x = lhs.x + rhs.x,
371 .y = lhs.y + rhs.y,
372 .z = lhs.z + rhs.z,
373 };
374}
375
385 struct bj_vec3 lhs,
386 struct bj_vec3 rhs,
387 bj_real s
388) {
389 return (struct bj_vec3){
390 .x = lhs.x + rhs.x * s,
391 .y = lhs.y + rhs.y * s,
392 .z = lhs.z + rhs.z * s,
393 };
394}
395
404 struct bj_vec3 lhs,
405 struct bj_vec3 rhs
406) {
407 return (struct bj_vec3){
408 .x = lhs.x - rhs.x,
409 .y = lhs.y - rhs.y,
410 .z = lhs.z - rhs.z,
411 };
412}
413
421static BJ_INLINE struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s) {
422 return (struct bj_vec3){
423 .x = v.x * s,
424 .y = v.y * s,
425 .z = v.z * s,
426 };
427}
428
441 const struct bj_vec3 v,
442 const struct bj_vec3 s
443) {
444 return (struct bj_vec3){ .x = v.x * s.x, .y = v.y * s.y, .z = v.z * s.z, };
445}
446
454static BJ_INLINE bj_real bj_vec3_dot(struct bj_vec3 a, struct bj_vec3 b) {
455 return a.x * b.x + a.y * b.y + a.z * b.z;
456}
457
465 return bj_sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
466}
467
480 const bj_real l = bj_vec3_len(v);
481 if (bj_real_is_zero(l)) {
482 return (struct bj_vec3){BJ_FZERO,BJ_FZERO,BJ_FZERO};
483 }
484 return bj_vec3_scale(v, L / l);
485}
486
500 const bj_real dx = a.x - b.x;
501 const bj_real dy = a.y - b.y;
502 const bj_real dz = a.z - b.z;
503 return dx * dx + dy * dy + dz * dz;
504}
505
514 return bj_sqrt(bj_vec3_distance_sq(a, b));
515}
516
532 const bj_real l2 = v.x * v.x + v.y * v.y + v.z * v.z;
533 // A length of at most BJ_EPSILON is zero, as for *_scale_to_len
534 if (l2 <= BJ_EPSILON * BJ_EPSILON) {
535 return (struct bj_vec3){ BJ_FZERO, BJ_FZERO, BJ_FZERO };
536 }
537 const bj_real inv = BJ_F(1.0) / bj_sqrt(l2);
538 return (struct bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
539}
540
555 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
556 return (struct bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
557}
558
566static BJ_INLINE struct bj_vec3 bj_vec3_min(struct bj_vec3 a, struct bj_vec3 b) {
567 return (struct bj_vec3){
568 a.x < b.x ? a.x : b.x,
569 a.y < b.y ? a.y : b.y,
570 a.z < b.z ? a.z : b.z,
571 };
572}
573
581static BJ_INLINE struct bj_vec3 bj_vec3_max(struct bj_vec3 a, struct bj_vec3 b) {
582 return (struct bj_vec3){a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, a.z > b.z ? a.z : b.z,};
583}
584
597static BJ_INLINE struct bj_vec3 bj_vec3_cross(struct bj_vec3 l, struct bj_vec3 r)
598{
599 return (struct bj_vec3) {
600 .x = l.y * r.z - l.z * r.y,
601 .y = l.z * r.x - l.x * r.z,
602 .z = l.x * r.y - l.y * r.x,
603 };
604}
605
618static BJ_INLINE struct bj_vec3 bj_vec3_reflect(struct bj_vec3 v, struct bj_vec3 n)
619{
620 const bj_real p = BJ_F(2.) * bj_vec3_dot(v, n);
621 return (struct bj_vec3) {
622 .x = v.x - p * n.x,
623 .y = v.y - p * n.y,
624 .z = v.z - p * n.z,
625 };
626}
627
636 struct bj_vec4 a,
637 bj_real(*f)(bj_real)
638) {
639 return (struct bj_vec4){ .x = f(a.x), .y = f(a.y), .z = f(a.z), .w = f(a.w), };
640}
641
649static BJ_INLINE struct bj_vec4 bj_vec4_add(struct bj_vec4 lhs, struct bj_vec4 rhs) {
650 return (struct bj_vec4){
651 .x = lhs.x + rhs.x,
652 .y = lhs.y + rhs.y,
653 .z = lhs.z + rhs.z,
654 .w = lhs.w + rhs.w,
655 };
656}
657
667 struct bj_vec4 lhs,
668 struct bj_vec4 rhs,
669 bj_real s
670) {
671 return (struct bj_vec4){
672 .x = lhs.x + rhs.x * s,
673 .y = lhs.y + rhs.y * s,
674 .z = lhs.z + rhs.z * s,
675 .w = lhs.w + rhs.w * s,
676 };
677}
678
687 struct bj_vec4 lhs,
688 struct bj_vec4 rhs
689) {
690 return (struct bj_vec4){
691 .x = lhs.x - rhs.x,
692 .y = lhs.y - rhs.y,
693 .z = lhs.z - rhs.z,
694 .w = lhs.w - rhs.w,
695 };
696}
697
705static BJ_INLINE struct bj_vec4 bj_vec4_scale(struct bj_vec4 v, bj_real s) {
706 return (struct bj_vec4){
707 .x = v.x * s,
708 .y = v.y * s,
709 .z = v.z * s,
710 .w = v.w * s,
711 };
712}
713
721static BJ_INLINE bj_real bj_vec4_dot(struct bj_vec4 a, struct bj_vec4 b) {
722 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
723}
724
732 return bj_sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
733}
734
750 const bj_real len2 = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
751 // A length of at most BJ_EPSILON is zero, as for *_scale_to_len
752 if (len2 <= BJ_EPSILON * BJ_EPSILON) {
753 return (struct bj_vec4){ BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO };
754 }
755 const bj_real inv = BJ_F(1.0) / bj_sqrt(len2);
756 return (struct bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
757}
758
773 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w);
774 return (struct bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
775}
776
789 const struct bj_vec4 v,
790 const struct bj_vec4 s
791) {
792 return (struct bj_vec4){
793 .x = v.x * s.x, .y = v.y * s.y, .z = v.z * s.z, .w = v.w * s.w,
794 };
795}
796
809 const bj_real l = bj_vec4_len(v);
810 if (bj_real_is_zero(l)) {
811 return (struct bj_vec4){ BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO };
812 }
813 return bj_vec4_scale(v, len / l);
814}
815
829 const bj_real dx = a.x - b.x;
830 const bj_real dy = a.y - b.y;
831 const bj_real dz = a.z - b.z;
832 const bj_real dw = a.w - b.w;
833 return dx * dx + dy * dy + dz * dz + dw * dw;
834}
835
844 return bj_sqrt(bj_vec4_distance_sq(a, b));
845}
846
854static BJ_INLINE struct bj_vec4 bj_vec4_min(struct bj_vec4 a, struct bj_vec4 b) {
855 return (struct bj_vec4){
856 a.x < b.x ? a.x : b.x,
857 a.y < b.y ? a.y : b.y,
858 a.z < b.z ? a.z : b.z,
859 a.w < b.w ? a.w : b.w,
860 };
861}
862
870static BJ_INLINE struct bj_vec4 bj_vec4_max(struct bj_vec4 a, struct bj_vec4 b) {
871 return (struct bj_vec4){
872 a.x > b.x ? a.x : b.x,
873 a.y > b.y ? a.y : b.y,
874 a.z > b.z ? a.z : b.z,
875 a.w > b.w ? a.w : b.w,
876 };
877}
878
891static BJ_INLINE struct bj_vec4 bj_vec4_cross_xyz(struct bj_vec4 l, struct bj_vec4 r){
892 return (struct bj_vec4){
893 l.y*r.z - l.z*r.y,
894 l.z*r.x - l.x*r.z,
895 l.x*r.y - l.y*r.x,
897 };
898}
899
911static BJ_INLINE struct bj_vec4 bj_vec4_reflect(struct bj_vec4 v, struct bj_vec4 n)
912{
913 bj_real p = BJ_F(2.0) * bj_vec4_dot(v, n);
914 return (struct bj_vec4) {
915 .x = v.x - p * n.x,
916 .y = v.y - p * n.y,
917 .z = v.z - p * n.z,
918 .w = v.w - p * n.w,
919 };
920}
921
922
923#endif
924
General-purpose definitions for Banjo API.
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
Definition api.h:248
bj_real z
Z component.
Definition vec.h:88
bj_real x
X component.
Definition vec.h:58
bj_real x
X component.
Definition vec.h:71
bj_real y
Y component.
Definition vec.h:59
bj_real w
W component (scalar part for quaternions).
Definition vec.h:89
bj_real z
Z component.
Definition vec.h:73
bj_real x
X component.
Definition vec.h:86
bj_real y
Y component.
Definition vec.h:72
bj_real y
Y component.
Definition vec.h:87
static struct bj_vec3 bj_vec3_map(struct bj_vec3 a, bj_real(*f)(bj_real))
Apply a scalar function component-wise: (f(x), f(y), f(z)).
Definition vec.h:354
static struct bj_vec4 bj_vec4_sub(struct bj_vec4 lhs, struct bj_vec4 rhs)
Component-wise subtraction: lhs - rhs.
Definition vec.h:686
static bj_real bj_vec2_len(struct bj_vec2 v)
Euclidean length: sqrt(x^2 + y^2).
Definition vec.h:227
static struct bj_vec2 bj_vec2_max(struct bj_vec2 a, struct bj_vec2 b)
Component-wise maximum: (max(a.x,b.x), max(a.y,b.y)).
Definition vec.h:343
static struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s)
Uniform scaling by a scalar: v * s.
Definition vec.h:421
static struct bj_vec3 bj_vec3_sub(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise subtraction: lhs - rhs.
Definition vec.h:403
static struct bj_vec2 bj_vec2_add(struct bj_vec2 lhs, struct bj_vec2 rhs)
Component-wise addition: lhs + rhs.
Definition vec.h:121
static struct bj_vec2 bj_vec2_map(struct bj_vec2 a, bj_real(*f)(bj_real))
Apply a scalar function component-wise: (f(x), f(y)).
Definition vec.h:107
static struct bj_vec3 bj_vec3_max(struct bj_vec3 a, struct bj_vec3 b)
Component-wise maximum.
Definition vec.h:581
static bj_real bj_vec2_dot(struct bj_vec2 a, struct bj_vec2 b)
Dot product: a.x*b.x + a.y*b.y.
Definition vec.h:195
static struct bj_vec4 bj_vec4_mul_comp(const struct bj_vec4 v, const struct bj_vec4 s)
Component-wise (Hadamard) product: (v.x*s.x, ..., v.w*s.w).
Definition vec.h:788
static struct bj_vec4 bj_vec4_map(struct bj_vec4 a, bj_real(*f)(bj_real))
Apply a scalar function component-wise to all four components.
Definition vec.h:635
static bj_real bj_vec4_dot(struct bj_vec4 a, struct bj_vec4 b)
Dot product over all four components.
Definition vec.h:721
static struct bj_vec2 bj_vec2_min(struct bj_vec2 a, struct bj_vec2 b)
Component-wise minimum: (min(a.x,b.x), min(a.y,b.y)).
Definition vec.h:332
static bj_real bj_vec3_distance_sq(struct bj_vec3 a, struct bj_vec3 b)
Squared Euclidean distance: ||a - b||^2.
Definition vec.h:499
static bj_real bj_vec2_perp_dot(struct bj_vec2 a, struct bj_vec2 b)
2D perp-dot product (signed scalar): a.x*b.y - a.y*b.x.
Definition vec.h:217
static struct bj_vec4 bj_vec4_normalize_unsafe(struct bj_vec4 v)
Normalise to unit length (unsafe).
Definition vec.h:772
static bj_real bj_vec3_len(struct bj_vec3 v)
Euclidean length: sqrt(x^2 + y^2 + z^2).
Definition vec.h:464
static struct bj_vec2 bj_vec2_sub(const struct bj_vec2 lhs, const struct bj_vec2 rhs)
Component-wise subtraction: lhs - rhs.
Definition vec.h:152
static struct bj_vec4 bj_vec4_cross_xyz(struct bj_vec4 l, struct bj_vec4 r)
Cross product of the XYZ components, with w = 0.
Definition vec.h:891
static struct bj_vec2 bj_vec2_add_scaled(struct bj_vec2 lhs, struct bj_vec2 rhs, bj_real s)
Fused scaled add: lhs + s * rhs.
Definition vec.h:137
static struct bj_vec4 bj_vec4_max(struct bj_vec4 a, struct bj_vec4 b)
Component-wise maximum across all four components.
Definition vec.h:870
static struct bj_vec4 bj_vec4_scale(struct bj_vec4 v, bj_real s)
Uniform scaling by a scalar: v * s.
Definition vec.h:705
static struct bj_vec3 bj_vec3_normalize_unsafe(struct bj_vec3 v)
Normalise to unit length (unsafe).
Definition vec.h:554
static struct bj_vec2 bj_vec2_mul_comp(const struct bj_vec2 v, const struct bj_vec2 s)
Component-wise (Hadamard) product: (v.x*s.x, v.y*s.y).
Definition vec.h:181
static struct bj_vec4 bj_vec4_add(struct bj_vec4 lhs, struct bj_vec4 rhs)
Component-wise addition: lhs + rhs.
Definition vec.h:649
static struct bj_vec4 bj_vec4_reflect(struct bj_vec4 v, struct bj_vec4 n)
Reflect a vector about a surface normal: v - 2*dot(v, n)*n.
Definition vec.h:911
static struct bj_vec4 bj_vec4_min(struct bj_vec4 a, struct bj_vec4 b)
Component-wise minimum across all four components.
Definition vec.h:854
static struct bj_vec3 bj_vec3_reflect(struct bj_vec3 v, struct bj_vec3 n)
Reflect a vector about a surface normal: v - 2*dot(v, n)*n.
Definition vec.h:618
static bj_real bj_vec4_distance(struct bj_vec4 a, struct bj_vec4 b)
Euclidean distance: ||a - b||.
Definition vec.h:843
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:93
static bj_real bj_vec3_dot(struct bj_vec3 a, struct bj_vec3 b)
Dot product: a.x*b.x + a.y*b.y + a.z*b.z.
Definition vec.h:454
static struct bj_vec3 bj_vec3_add(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise addition: lhs + rhs.
Definition vec.h:368
static struct bj_vec2 bj_vec2_normalize(struct bj_vec2 v)
Normalise to unit length (safe).
Definition vec.h:295
static bj_real bj_vec2_distance_sq(struct bj_vec2 a, struct bj_vec2 b)
Squared Euclidean distance: ||a - b||^2.
Definition vec.h:263
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
Definition math.h:483
static bj_real bj_vec2_distance(const struct bj_vec2 a, const struct bj_vec2 b)
Euclidean distance: ||a - b||.
Definition vec.h:276
static struct bj_vec4 bj_vec4_normalize(struct bj_vec4 v)
Normalise to unit length (safe).
Definition vec.h:749
static struct bj_vec3 bj_vec3_cross(struct bj_vec3 l, struct bj_vec3 r)
3D cross product: l × r (right-hand rule).
Definition vec.h:597
static struct bj_vec2 bj_vec2_scale_to_len(struct bj_vec2 v, bj_real L)
Rescale a vector to a chosen length.
Definition vec.h:243
static struct bj_vec2 bj_vec2_scale(struct bj_vec2 v, bj_real s)
Uniform scaling by a scalar: v * s.
Definition vec.h:166
static struct bj_vec3 bj_vec3_add_scaled(struct bj_vec3 lhs, struct bj_vec3 rhs, bj_real s)
Fused scaled add: lhs + s * rhs.
Definition vec.h:384
static struct bj_vec3 bj_vec3_mul_comp(const struct bj_vec3 v, const struct bj_vec3 s)
Component-wise (Hadamard) product: (v.x*s.x, v.y*s.y, v.z*s.z).
Definition vec.h:440
static struct bj_vec3 bj_vec3_scale_to_len(struct bj_vec3 v, bj_real L)
Rescale a vector to a chosen length.
Definition vec.h:479
#define BJ_EPSILON
Machine epsilon for bj_real when float is selected.
Definition math.h:84
static bj_real bj_vec4_len(struct bj_vec4 v)
Euclidean length: sqrt(x^2 + y^2 + z^2 + w^2).
Definition vec.h:731
static bj_real bj_vec4_distance_sq(struct bj_vec4 a, struct bj_vec4 b)
Squared Euclidean distance: ||a - b||^2.
Definition vec.h:828
static struct bj_vec4 bj_vec4_add_scaled(struct bj_vec4 lhs, struct bj_vec4 rhs, bj_real s)
Fused scaled add: lhs + s * rhs.
Definition vec.h:666
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:82
static struct bj_vec3 bj_vec3_normalize(struct bj_vec3 v)
Normalise to unit length (safe).
Definition vec.h:531
static struct bj_vec3 bj_vec3_min(struct bj_vec3 a, struct bj_vec3 b)
Component-wise minimum.
Definition vec.h:566
#define bj_sqrt
Square root.
Definition math.h:251
float bj_real
Selected real type for float configuration.
Definition math.h:80
static bj_real bj_vec3_distance(struct bj_vec3 a, struct bj_vec3 b)
Euclidean distance: ||a - b||.
Definition vec.h:513
static struct bj_vec2 bj_vec2_normalize_unsafe(struct bj_vec2 v)
Normalise to unit length (unsafe).
Definition vec.h:320
static struct bj_vec4 bj_vec4_scale_to_len(struct bj_vec4 v, bj_real len)
Rescale a vector to a chosen length.
Definition vec.h:808
2D vector of bj_real components.
Definition vec.h:57
3D vector of bj_real components.
Definition vec.h:70
4D vector of bj_real components.
Definition vec.h:85
C99 math shim with bj_real precision type and scalar utilities.