Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
vec.h
Go to the documentation of this file.
1
40#ifndef BJ_VEC_H
41#define BJ_VEC_H
42
43#include <banjo/api.h>
44#include <banjo/math.h>
45
55
57#define BJ_VEC2_ZERO ((struct bj_vec2){BJ_FZERO, BJ_FZERO})
58
69
71#define BJ_VEC3_ZERO ((struct bj_vec3){BJ_FZERO, BJ_FZERO, BJ_FZERO})
72
85
87#define BJ_VEC4_ZERO ((struct bj_vec4){BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO})
88
102 struct bj_vec2 a,
103 bj_real(*f)(bj_real)
104) {
105 return (struct bj_vec2){ .x = f(a.x), .y = f(a.y), };
106}
107
115static BJ_INLINE struct bj_vec2 bj_vec2_add(struct bj_vec2 lhs, struct bj_vec2 rhs) {
116 return (struct bj_vec2){ .x = lhs.x + rhs.x, .y = lhs.y + rhs.y, };
117}
118
132 struct bj_vec2 lhs,
133 struct bj_vec2 rhs,
134 bj_real s
135) {
136 return (struct bj_vec2){ .x = lhs.x + rhs.x * s, .y = lhs.y + rhs.y * s, };
137}
138
147 const struct bj_vec2 lhs,
148 const struct bj_vec2 rhs
149) {
150 return (struct bj_vec2){ .x = lhs.x - rhs.x, .y = lhs.y - rhs.y, };
151}
152
160static BJ_INLINE struct bj_vec2 bj_vec2_scale(struct bj_vec2 v, bj_real s) {
161 return (struct bj_vec2){ .x = v.x * s, .y = v.y * s, };
162}
163
176 const struct bj_vec2 v,
177 const struct bj_vec2 s
178) {
179 return (struct bj_vec2){ .x = v.x * s.x, .y = v.y * s.y, };
180}
181
189static BJ_INLINE bj_real bj_vec2_dot(struct bj_vec2 a, struct bj_vec2 b) {
190 return a.x * b.x + a.y * b.y;
191}
192
210 return a.x*b.y - a.y*b.x;
211}
212
220 return bj_sqrt(v.x * v.x + v.y * v.y);
221}
222
236 const bj_real l = bj_vec2_len(v);
237 if (bj_real_is_zero(l)){
238 return (struct bj_vec2){BJ_FZERO,BJ_FZERO};
239 }
240 return bj_vec2_scale(v, L / l);
241}
242
256 const bj_real dx = a.x - b.x;
257 const bj_real dy = a.y - b.y;
258 return dx * dx + dy * dy;
259}
260
268static BJ_INLINE bj_real bj_vec2_distance(const struct bj_vec2 a, const struct bj_vec2 b) {
269 return bj_sqrt(bj_vec2_distance_sq(a, b));
270}
271
287 const bj_real l2 = v.x * v.x + v.y * v.y;
288 if (bj_real_is_zero(l2)) {
289 return (struct bj_vec2){ BJ_FZERO, BJ_FZERO };
290 }
291 const bj_real inv = BJ_F(1.0) / bj_sqrt(l2);
292 return (struct bj_vec2){ v.x * inv, v.y * inv };
293}
294
311 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y);
312 return (struct bj_vec2){ v.x * inv, v.y * inv };
313}
314
322static BJ_INLINE struct bj_vec2 bj_vec2_min(struct bj_vec2 a, struct bj_vec2 b) {
323 return (struct bj_vec2){a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, };
324}
325
333static BJ_INLINE struct bj_vec2 bj_vec2_max(struct bj_vec2 a, struct bj_vec2 b) {
334 return (struct bj_vec2){a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, };
335}
336
345 struct bj_vec3 a,
346 bj_real(*f)(bj_real)
347) {
348 return (struct bj_vec3){ .x = f(a.x), .y = f(a.y), .z = f(a.z), };
349}
350
358static BJ_INLINE struct bj_vec3 bj_vec3_add(struct bj_vec3 lhs, struct bj_vec3 rhs) {
359 return (struct bj_vec3){
360 .x = lhs.x + rhs.x,
361 .y = lhs.y + rhs.y,
362 .z = lhs.z + rhs.z,
363 };
364}
365
375 struct bj_vec3 lhs,
376 struct bj_vec3 rhs,
377 bj_real s
378) {
379 return (struct bj_vec3){
380 .x = lhs.x + rhs.x * s,
381 .y = lhs.y + rhs.y * s,
382 .z = lhs.z + rhs.z * s,
383 };
384}
385
394 struct bj_vec3 lhs,
395 struct bj_vec3 rhs
396) {
397 return (struct bj_vec3){
398 .x = lhs.x - rhs.x,
399 .y = lhs.y - rhs.y,
400 .z = lhs.z - rhs.z,
401 };
402}
403
411static BJ_INLINE struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s) {
412 return (struct bj_vec3){
413 .x = v.x * s,
414 .y = v.y * s,
415 .z = v.z * s,
416 };
417}
418
426static BJ_INLINE bj_real bj_vec3_dot(struct bj_vec3 a, struct bj_vec3 b) {
427 return a.x * b.x + a.y * b.y + a.z * b.z;
428}
429
437 return bj_sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
438}
439
452 const bj_real l = bj_vec3_len(v);
453 if (bj_real_is_zero(l)) {
454 return (struct bj_vec3){BJ_FZERO,BJ_FZERO,BJ_FZERO};
455 }
456 return bj_vec3_scale(v, L / l);
457}
458
472 const bj_real dx = a.x - b.x;
473 const bj_real dy = a.y - b.y;
474 const bj_real dz = a.z - b.z;
475 return dx * dx + dy * dy + dz * dz;
476}
477
486 return bj_sqrt(bj_vec3_distance_sq(a, b));
487}
488
503 const bj_real l2 = v.x * v.x + v.y * v.y + v.z * v.z;
504 if (bj_real_is_zero(l2)) {
505 return (struct bj_vec3){ BJ_FZERO, BJ_FZERO, BJ_FZERO };
506 }
507 const bj_real inv = BJ_F(1.0) / bj_sqrt(l2);
508 return (struct bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
509}
510
525 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
526 return (struct bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
527}
528
536static BJ_INLINE struct bj_vec3 bj_vec3_min(struct bj_vec3 a, struct bj_vec3 b) {
537 return (struct bj_vec3){
538 a.x < b.x ? a.x : b.x,
539 a.y < b.y ? a.y : b.y,
540 a.z < b.z ? a.z : b.z,
541 };
542}
543
551static BJ_INLINE struct bj_vec3 bj_vec3_max(struct bj_vec3 a, struct bj_vec3 b) {
552 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,};
553}
554
567static BJ_INLINE struct bj_vec3 bj_vec3_cross(struct bj_vec3 l, struct bj_vec3 r)
568{
569 return (struct bj_vec3) {
570 .x = l.y * r.z - l.z * r.y,
571 .y = l.z * r.x - l.x * r.z,
572 .z = l.x * r.y - l.y * r.x,
573 };
574}
575
588static BJ_INLINE struct bj_vec3 bj_vec3_reflect(struct bj_vec3 v, struct bj_vec3 n)
589{
590 const bj_real p = BJ_F(2.) * bj_vec3_dot(v, n);
591 return (struct bj_vec3) {
592 .x = v.x - p * n.x,
593 .y = v.y - p * n.y,
594 .z = v.z - p * n.z,
595 };
596}
597
606 struct bj_vec4 a,
607 bj_real(*f)(bj_real)
608) {
609 return (struct bj_vec4){ .x = f(a.x), .y = f(a.y), .z = f(a.z), .w = f(a.w), };
610}
611
619static BJ_INLINE struct bj_vec4 bj_vec4_add(struct bj_vec4 lhs, struct bj_vec4 rhs) {
620 return (struct bj_vec4){
621 .x = lhs.x + rhs.x,
622 .y = lhs.y + rhs.y,
623 .z = lhs.z + rhs.z,
624 .w = lhs.w + rhs.w,
625 };
626}
627
637 struct bj_vec4 lhs,
638 struct bj_vec4 rhs,
639 bj_real s
640) {
641 return (struct bj_vec4){
642 .x = lhs.x + rhs.x * s,
643 .y = lhs.y + rhs.y * s,
644 .z = lhs.z + rhs.z * s,
645 .w = lhs.w + rhs.w * s,
646 };
647}
648
657 struct bj_vec4 lhs,
658 struct bj_vec4 rhs
659) {
660 return (struct bj_vec4){
661 .x = lhs.x - rhs.x,
662 .y = lhs.y - rhs.y,
663 .z = lhs.z - rhs.z,
664 .w = lhs.w - rhs.w,
665 };
666}
667
675static BJ_INLINE struct bj_vec4 bj_vec4_scale(struct bj_vec4 v, bj_real s) {
676 return (struct bj_vec4){
677 .x = v.x * s,
678 .y = v.y * s,
679 .z = v.z * s,
680 .w = v.w * s,
681 };
682}
683
691static BJ_INLINE bj_real bj_vec4_dot(struct bj_vec4 a, struct bj_vec4 b) {
692 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
693}
694
702 return bj_sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
703}
704
719 const bj_real len2 = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
720 if (bj_real_is_zero(len2)) {
721 return (struct bj_vec4){ BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO };
722 }
723 const bj_real inv = BJ_F(1.0) / bj_sqrt(len2);
724 return (struct bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
725}
726
741 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);
742 return (struct bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
743}
744
752static BJ_INLINE struct bj_vec4 bj_vec4_min(struct bj_vec4 a, struct bj_vec4 b) {
753 return (struct bj_vec4){
754 a.x < b.x ? a.x : b.x,
755 a.y < b.y ? a.y : b.y,
756 a.z < b.z ? a.z : b.z,
757 a.w < b.w ? a.w : b.w,
758 };
759}
760
768static BJ_INLINE struct bj_vec4 bj_vec4_max(struct bj_vec4 a, struct bj_vec4 b) {
769 return (struct bj_vec4){
770 a.x > b.x ? a.x : b.x,
771 a.y > b.y ? a.y : b.y,
772 a.z > b.z ? a.z : b.z,
773 a.w > b.w ? a.w : b.w,
774 };
775}
776
789static BJ_INLINE struct bj_vec4 bj_vec4_cross_xyz(struct bj_vec4 l, struct bj_vec4 r){
790 return (struct bj_vec4){
791 l.y*r.z - l.z*r.y,
792 l.z*r.x - l.x*r.z,
793 l.x*r.y - l.y*r.x,
795 };
796}
797
809static BJ_INLINE struct bj_vec4 bj_vec4_reflect(struct bj_vec4 v, struct bj_vec4 n)
810{
811 bj_real p = BJ_F(2.0) * bj_vec4_dot(v, n);
812 return (struct bj_vec4) {
813 .x = v.x - p * n.x,
814 .y = v.y - p * n.y,
815 .z = v.z - p * n.z,
816 .w = v.w - p * n.w,
817 };
818}
819
820
821#endif
822
General-purpose definitions for Banjo API.
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
Definition api.h:241
bj_real z
Z component.
Definition vec.h:82
bj_real x
X component.
Definition vec.h:52
bj_real x
X component.
Definition vec.h:65
bj_real y
Y component.
Definition vec.h:53
bj_real w
W component (scalar part for quaternions).
Definition vec.h:83
bj_real z
Z component.
Definition vec.h:67
bj_real x
X component.
Definition vec.h:80
bj_real y
Y component.
Definition vec.h:66
bj_real y
Y component.
Definition vec.h:81
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:344
static struct bj_vec4 bj_vec4_sub(struct bj_vec4 lhs, struct bj_vec4 rhs)
Component-wise subtraction: lhs - rhs.
Definition vec.h:656
static bj_real bj_vec2_len(struct bj_vec2 v)
Euclidean length: sqrt(x^2 + y^2).
Definition vec.h:219
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:333
static struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s)
Uniform scaling by a scalar: v * s.
Definition vec.h:411
static struct bj_vec3 bj_vec3_sub(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise subtraction: lhs - rhs.
Definition vec.h:393
static struct bj_vec2 bj_vec2_add(struct bj_vec2 lhs, struct bj_vec2 rhs)
Component-wise addition: lhs + rhs.
Definition vec.h:115
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:101
static struct bj_vec3 bj_vec3_max(struct bj_vec3 a, struct bj_vec3 b)
Component-wise maximum.
Definition vec.h:551
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:189
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:605
static bj_real bj_vec4_dot(struct bj_vec4 a, struct bj_vec4 b)
Dot product over all four components.
Definition vec.h:691
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:322
static bj_real bj_vec3_distance_sq(struct bj_vec3 a, struct bj_vec3 b)
Squared Euclidean distance: ||a - b||^2.
Definition vec.h:471
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:209
static struct bj_vec4 bj_vec4_normalize_unsafe(struct bj_vec4 v)
Normalise to unit length (unsafe).
Definition vec.h:740
static bj_real bj_vec3_len(struct bj_vec3 v)
Euclidean length: sqrt(x^2 + y^2 + z^2).
Definition vec.h:436
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:146
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:789
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:131
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:768
static struct bj_vec4 bj_vec4_scale(struct bj_vec4 v, bj_real s)
Uniform scaling by a scalar: v * s.
Definition vec.h:675
static struct bj_vec3 bj_vec3_normalize_unsafe(struct bj_vec3 v)
Normalise to unit length (unsafe).
Definition vec.h:524
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:175
static struct bj_vec4 bj_vec4_add(struct bj_vec4 lhs, struct bj_vec4 rhs)
Component-wise addition: lhs + rhs.
Definition vec.h:619
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:809
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:752
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:588
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:89
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:426
static struct bj_vec3 bj_vec3_add(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise addition: lhs + rhs.
Definition vec.h:358
static struct bj_vec2 bj_vec2_normalize(struct bj_vec2 v)
Normalise to unit length (safe).
Definition vec.h:286
static bj_real bj_vec2_distance_sq(struct bj_vec2 a, struct bj_vec2 b)
Squared Euclidean distance: ||a - b||^2.
Definition vec.h:255
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
Definition math.h:479
static bj_real bj_vec2_distance(const struct bj_vec2 a, const struct bj_vec2 b)
Euclidean distance: ||a - b||.
Definition vec.h:268
static struct bj_vec4 bj_vec4_normalize(struct bj_vec4 v)
Normalise to unit length (safe).
Definition vec.h:718
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:567
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:235
static struct bj_vec2 bj_vec2_scale(struct bj_vec2 v, bj_real s)
Uniform scaling by a scalar: v * s.
Definition vec.h:160
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:374
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:451
static bj_real bj_vec4_len(struct bj_vec4 v)
Euclidean length: sqrt(x^2 + y^2 + z^2 + w^2).
Definition vec.h:701
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:636
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:78
static struct bj_vec3 bj_vec3_normalize(struct bj_vec3 v)
Normalise to unit length (safe).
Definition vec.h:502
static struct bj_vec3 bj_vec3_min(struct bj_vec3 a, struct bj_vec3 b)
Component-wise minimum.
Definition vec.h:536
#define bj_sqrt
Square root.
Definition math.h:247
float bj_real
Selected real type for float configuration.
Definition math.h:76
static bj_real bj_vec3_distance(struct bj_vec3 a, struct bj_vec3 b)
Euclidean distance: ||a - b||.
Definition vec.h:485
static struct bj_vec2 bj_vec2_normalize_unsafe(struct bj_vec2 v)
Normalise to unit length (unsafe).
Definition vec.h:310
2D vector of bj_real components.
Definition vec.h:51
3D vector of bj_real components.
Definition vec.h:64
4D vector of bj_real components.
Definition vec.h:79
C99 math shim with bj_real precision type and scalar utilities.