Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
vec.h
Go to the documentation of this file.
1
16#ifndef BJ_VEC_H
17#define BJ_VEC_H
18
19#include <banjo/api.h>
20#include <banjo/math.h>
21
30struct bj_vec2;
31
32#define BJ_VEC2_ZERO ((struct bj_vec2){BJ_FZERO, BJ_FZERO})
33
43struct bj_vec3;
44
45#define BJ_VEC3_ZERO ((struct bj_vec3){BJ_FZERO, BJ_FZERO, BJ_FZERO})
46
57struct bj_vec4;
58
59#define BJ_VEC4_ZERO ((struct bj_vec4){BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO})
60
62 struct bj_vec2 a,
63 bj_real(*f)(bj_real)
64) {
65 return (struct bj_vec2){ .x = f(a.x), .y = f(a.y), };
66}
67
76static BJ_INLINE struct bj_vec2 bj_vec2_add(struct bj_vec2 lhs, struct bj_vec2 rhs) {
77 return (struct bj_vec2){ .x = lhs.x + rhs.x, .y = lhs.y + rhs.y, };
78}
79
88 struct bj_vec2 lhs,
89 struct bj_vec2 rhs,
90 bj_real s
91) {
92 return (struct bj_vec2){ .x = lhs.x + rhs.x * s, .y = lhs.y + rhs.y * s, };
93}
94
102 const struct bj_vec2 lhs,
103 const struct bj_vec2 rhs
104) {
105 return (struct bj_vec2){ .x = lhs.x - rhs.x, .y = lhs.y - rhs.y, };
106}
107
114static BJ_INLINE struct bj_vec2 bj_vec2_scale(struct bj_vec2 v, bj_real s) {
115 return (struct bj_vec2){ .x = v.x * s, .y = v.y * s, };
116}
117
125 const struct bj_vec2 v,
126 const struct bj_vec2 s
127) {
128 return (struct bj_vec2){ .x = v.x * s.x, .y = v.y * s.y, };
129}
130
137static BJ_INLINE bj_real bj_vec2_dot(struct bj_vec2 a, struct bj_vec2 b) {
138 return a.x * b.x + a.y * b.y;
139}
140
146 return a.x*b.y - a.y*b.x;
147}
148
155 return bj_sqrt(v.x * v.x + v.y * v.y);
156}
157
165 const bj_real l = bj_vec2_len(v);
166 if (bj_real_is_zero(l)){
167 return (struct bj_vec2){BJ_FZERO,BJ_FZERO};
168 }
169 return bj_vec2_scale(v, L / l);
170}
171
179 const bj_real dx = a.x - b.x;
180 const bj_real dy = a.y - b.y;
181 return dx * dx + dy * dy;
182}
183
190static BJ_INLINE bj_real bj_vec2_distance(const struct bj_vec2 a, const struct bj_vec2 b) {
191 return bj_sqrt(bj_vec2_distance_sq(a, b));
192}
193
208 const bj_real l2 = v.x * v.x + v.y * v.y;
209 if (bj_real_is_zero(l2)) {
210 return (struct bj_vec2){ BJ_FZERO, BJ_FZERO };
211 }
212 const bj_real inv = BJ_F(1.0) / bj_sqrt(l2);
213 return (struct bj_vec2){ v.x * inv, v.y * inv };
214}
215
230 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y);
231 return (struct bj_vec2){ v.x * inv, v.y * inv };
232}
233
234
240static BJ_INLINE struct bj_vec2 bj_vec2_min(struct bj_vec2 a, struct bj_vec2 b) {
241 return (struct bj_vec2){a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, };
242}
243
249static BJ_INLINE struct bj_vec2 bj_vec2_max(struct bj_vec2 a, struct bj_vec2 b) {
250 return (struct bj_vec2){a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, };
251}
252
254 struct bj_vec3 a,
255 bj_real(*f)(bj_real)
256) {
257 return (struct bj_vec3){ .x = f(a.x), .y = f(a.y), .z = f(a.z), };
258}
259
265static BJ_INLINE struct bj_vec3 bj_vec3_add(struct bj_vec3 lhs, struct bj_vec3 rhs) {
266 return (struct bj_vec3){
267 .x = lhs.x + rhs.x,
268 .y = lhs.y + rhs.y,
269 .z = lhs.z + rhs.z,
270 };
271}
272
281 struct bj_vec3 lhs,
282 struct bj_vec3 rhs,
283 bj_real s
284) {
285 return (struct bj_vec3){
286 .x = lhs.x + rhs.x * s,
287 .y = lhs.y + rhs.y * s,
288 .z = lhs.z + rhs.z * s,
289 };
290}
291
292
300 struct bj_vec3 lhs,
301 struct bj_vec3 rhs
302) {
303 return (struct bj_vec3){
304 .x = lhs.x - rhs.x,
305 .y = lhs.y - rhs.y,
306 .z = lhs.z - rhs.z,
307 };
308}
309
316static BJ_INLINE struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s) {
317 return (struct bj_vec3){
318 .x = v.x * s,
319 .y = v.y * s,
320 .z = v.z * s,
321 };
322}
323
330static BJ_INLINE bj_real bj_vec3_dot(struct bj_vec3 a, struct bj_vec3 b) {
331 return a.x * b.x + a.y * b.y + a.z * b.z;
332}
333
340 return bj_sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
341}
342
350 const bj_real l = bj_vec3_len(v);
351 if (bj_real_is_zero(l)) {
352 return (struct bj_vec3){BJ_FZERO,BJ_FZERO,BJ_FZERO};
353 }
354 return bj_vec3_scale(v, L / l);
355}
356
364 const bj_real dx = a.x - b.x;
365 const bj_real dy = a.y - b.y;
366 const bj_real dz = a.z - b.z;
367 return dx * dx + dy * dy + dz * dz;
368}
369
377 return bj_sqrt(bj_vec3_distance_sq(a, b));
378}
379
393 const bj_real l2 = v.x * v.x + v.y * v.y + v.z * v.z;
394 if (bj_real_is_zero(l2)) {
395 return (struct bj_vec3){ BJ_FZERO, BJ_FZERO, BJ_FZERO };
396 }
397 const bj_real inv = BJ_F(1.0) / bj_sqrt(l2);
398 return (struct bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
399}
400
414 const bj_real inv = BJ_F(1.0) / bj_sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
415 return (struct bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
416}
417
423static BJ_INLINE struct bj_vec3 bj_vec3_min(struct bj_vec3 a, struct bj_vec3 b) {
424 return (struct bj_vec3){
425 a.x < b.x ? a.x : b.x,
426 a.y < b.y ? a.y : b.y,
427 a.z < b.z ? a.z : b.z,
428 };
429}
430
436static BJ_INLINE struct bj_vec3 bj_vec3_max(struct bj_vec3 a, struct bj_vec3 b) {
437 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,};
438}
439
446static BJ_INLINE struct bj_vec3 bj_vec3_cross(struct bj_vec3 l, struct bj_vec3 r)
447{
448 return (struct bj_vec3) {
449 .x = l.y * r.z - l.z * r.y,
450 .y = l.z * r.x - l.x * r.z,
451 .z = l.x * r.y - l.y * r.x,
452 };
453}
454
462static BJ_INLINE struct bj_vec3 bj_vec3_reflect(struct bj_vec3 v, struct bj_vec3 n)
463{
464 const bj_real p = BJ_F(2.) * bj_vec3_dot(v, n);
465 return (struct bj_vec3) {
466 .x = v.x - p * n.x,
467 .y = v.y - p * n.y,
468 .z = v.z - p * n.z,
469 };
470}
471
473 struct bj_vec4 a,
474 bj_real(*f)(bj_real)
475) {
476 return (struct bj_vec4){ .x = f(a.x), .y = f(a.y), .z = f(a.z), .w = f(a.w), };
477}
478
485static BJ_INLINE struct bj_vec4 bj_vec4_add(struct bj_vec4 lhs, struct bj_vec4 rhs) {
486 return (struct bj_vec4){
487 .x = lhs.x + rhs.x,
488 .y = lhs.y + rhs.y,
489 .z = lhs.z + rhs.z,
490 .w = lhs.w + rhs.w,
491 };
492}
493
502 struct bj_vec4 lhs,
503 struct bj_vec4 rhs,
504 bj_real s
505) {
506 return (struct bj_vec4){
507 .x = lhs.x + rhs.x * s,
508 .y = lhs.y + rhs.y * s,
509 .z = lhs.z + rhs.z * s,
510 .w = lhs.w + rhs.w * s,
511 };
512}
513
521 struct bj_vec4 lhs,
522 struct bj_vec4 rhs
523) {
524 return (struct bj_vec4){
525 .x = lhs.x - rhs.x,
526 .y = lhs.y - rhs.y,
527 .z = lhs.z - rhs.z,
528 .w = lhs.w - rhs.w,
529 };
530}
531
538static BJ_INLINE struct bj_vec4 bj_vec4_scale(struct bj_vec4 v, bj_real s) {
539 return (struct bj_vec4){
540 .x = v.x * s,
541 .y = v.y * s,
542 .z = v.z * s,
543 .w = v.w * s,
544 };
545}
546
553static BJ_INLINE bj_real bj_vec4_dot(struct bj_vec4 a, struct bj_vec4 b) {
554 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
555}
556
563 return bj_sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
564}
565
578static BJ_INLINE struct bj_vec4 bj_vec4_normalize(struct bj_vec4 v) {
579 const bj_real len2 = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
580 if (bj_real_is_zero(len2)) {
581 return (struct bj_vec4){ BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO };
582 }
583 const bj_real inv = BJ_F(1.0) / bj_sqrt(len2);
584 return (struct bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
585}
586
600 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);
601 return (struct bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
602}
603
609static BJ_INLINE struct bj_vec4 bj_vec4_min(struct bj_vec4 a, struct bj_vec4 b) {
610 return (struct bj_vec4){
611 a.x < b.x ? a.x : b.x,
612 a.y < b.y ? a.y : b.y,
613 a.z < b.z ? a.z : b.z,
614 a.w < b.w ? a.w : b.w,
615 };
616}
617
623static BJ_INLINE struct bj_vec4 bj_vec4_max(struct bj_vec4 a, struct bj_vec4 b) {
624 return (struct bj_vec4){
625 a.x > b.x ? a.x : b.x,
626 a.y > b.y ? a.y : b.y,
627 a.z > b.z ? a.z : b.z,
628 a.w > b.w ? a.w : b.w,
629 };
630}
631
638static BJ_INLINE struct bj_vec4 bj_vec4_cross_xyz(struct bj_vec4 l, struct bj_vec4 r){
639 return (struct bj_vec4){
640 l.y*r.z - l.z*r.y,
641 l.z*r.x - l.x*r.z,
642 l.x*r.y - l.y*r.x,
644 };
645}
646
653static BJ_INLINE struct bj_vec4 bj_vec4_reflect(struct bj_vec4 v, struct bj_vec4 n)
654{
655 bj_real p = BJ_F(2.0) * bj_vec4_dot(v, n);
656 return (struct bj_vec4) {
657 .x = v.x - p * n.x,
658 .y = v.y - p * n.y,
659 .z = v.z - p * n.z,
660 .w = v.w - p * n.w,
661 };
662}
663
664
665#endif
666
668
General-purpose definitions for Banjo API.
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
Definition api.h:233
bj_real z
Definition vec.h:54
bj_real x
Definition vec.h:27
bj_real x
Definition vec.h:39
bj_real y
Definition vec.h:28
bj_real w
Definition vec.h:55
bj_real z
Definition vec.h:41
bj_real x
Definition vec.h:52
bj_real y
Definition vec.h:40
bj_real y
Definition vec.h:53
static struct bj_vec3 bj_vec3_map(struct bj_vec3 a, bj_real(*f)(bj_real))
Definition vec.h:253
static struct bj_vec4 bj_vec4_sub(struct bj_vec4 lhs, struct bj_vec4 rhs)
Component-wise subtraction of two 4D vectors: res = lhs - rhs.
Definition vec.h:520
static bj_real bj_vec2_len(struct bj_vec2 v)
Euclidean length (L2 norm) of a 2D vector.
Definition vec.h:154
static struct bj_vec2 bj_vec2_max(struct bj_vec2 a, struct bj_vec2 b)
Component-wise maximum of two 2D vectors.
Definition vec.h:249
static struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:316
static struct bj_vec3 bj_vec3_sub(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise subtraction of two 3D vectors: res = lhs - rhs.
Definition vec.h:299
static struct bj_vec2 bj_vec2_add(struct bj_vec2 lhs, struct bj_vec2 rhs)
Component-wise addition of two 2D vectors: res = lhs + rhs.
Definition vec.h:76
static struct bj_vec2 bj_vec2_map(struct bj_vec2 a, bj_real(*f)(bj_real))
Definition vec.h:61
static struct bj_vec3 bj_vec3_max(struct bj_vec3 a, struct bj_vec3 b)
Component-wise maximum of two 3D vectors.
Definition vec.h:436
static bj_real bj_vec2_dot(struct bj_vec2 a, struct bj_vec2 b)
Dot product of two 2D vectors.
Definition vec.h:137
static struct bj_vec4 bj_vec4_map(struct bj_vec4 a, bj_real(*f)(bj_real))
Definition vec.h:472
static bj_real bj_vec4_dot(struct bj_vec4 a, struct bj_vec4 b)
Dot product of two 4D vectors.
Definition vec.h:553
static struct bj_vec2 bj_vec2_min(struct bj_vec2 a, struct bj_vec2 b)
Component-wise minimum of two 2D vectors.
Definition vec.h:240
static bj_real bj_vec3_distance_sq(struct bj_vec3 a, struct bj_vec3 b)
Squared Euclidean distance between two 3D vectors.
Definition vec.h:363
static bj_real bj_vec2_perp_dot(struct bj_vec2 a, struct bj_vec2 b)
2D "cross product" (perp dot): returns scalar a.x*b.y - a.y*b.x.
Definition vec.h:145
static struct bj_vec4 bj_vec4_normalize_unsafe(struct bj_vec4 v)
Normalize a 4D vector to unit length (unsafe).
Definition vec.h:599
static bj_real bj_vec3_len(struct bj_vec3 v)
Euclidean length (L2 norm) of a 3D vector.
Definition vec.h:339
static struct bj_vec2 bj_vec2_sub(const struct bj_vec2 lhs, const struct bj_vec2 rhs)
Component-wise subtraction of two 2D vectors: res = lhs - rhs.
Definition vec.h:101
static struct bj_vec4 bj_vec4_cross_xyz(struct bj_vec4 l, struct bj_vec4 r)
Cross product using xyz components; w is set to 1.
Definition vec.h:638
static struct bj_vec2 bj_vec2_add_scaled(struct bj_vec2 lhs, struct bj_vec2 rhs, bj_real s)
Add a scaled vector: res = a + s * b.
Definition vec.h:87
static struct bj_vec4 bj_vec4_max(struct bj_vec4 a, struct bj_vec4 b)
Component-wise maximum of two 4D vectors.
Definition vec.h:623
static struct bj_vec4 bj_vec4_scale(struct bj_vec4 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:538
static struct bj_vec3 bj_vec3_normalize_unsafe(struct bj_vec3 v)
Normalize a 3D vector to unit length (unsafe).
Definition vec.h:413
static struct bj_vec2 bj_vec2_mul_comp(const struct bj_vec2 v, const struct bj_vec2 s)
Per-component scaling: res[i] = v[i] * s[i].
Definition vec.h:124
static struct bj_vec4 bj_vec4_add(struct bj_vec4 lhs, struct bj_vec4 rhs)
Component-wise addition of two 4D vectors: res = lhs + rhs.
Definition vec.h:485
static struct bj_vec4 bj_vec4_reflect(struct bj_vec4 v, struct bj_vec4 n)
Reflect a vector about a normal: res = v - 2*dot(v, n)*n.
Definition vec.h:653
static struct bj_vec4 bj_vec4_min(struct bj_vec4 a, struct bj_vec4 b)
Component-wise minimum of two 4D vectors.
Definition vec.h:609
static struct bj_vec3 bj_vec3_reflect(struct bj_vec3 v, struct bj_vec3 n)
Reflect a vector about a normal: res = v - 2*dot(v, n)*n.
Definition vec.h:462
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:64
static bj_real bj_vec3_dot(struct bj_vec3 a, struct bj_vec3 b)
Dot product of two 3D vectors.
Definition vec.h:330
static struct bj_vec3 bj_vec3_add(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise addition of two 3D vectors: res = lhs + rhs.
Definition vec.h:265
static struct bj_vec2 bj_vec2_normalize(struct bj_vec2 v)
Normalize a 2D vector to unit length (safe).
Definition vec.h:207
static bj_real bj_vec2_distance_sq(struct bj_vec2 a, struct bj_vec2 b)
Squared Euclidean distance between two 2D vectors.
Definition vec.h:178
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
Definition math.h:441
static bj_real bj_vec2_distance(const struct bj_vec2 a, const struct bj_vec2 b)
Euclidean distance between two 2D vectors.
Definition vec.h:190
static struct bj_vec4 bj_vec4_normalize(struct bj_vec4 v)
Normalize a 4D vector to unit length (safe).
Definition vec.h:578
static struct bj_vec3 bj_vec3_cross(struct bj_vec3 l, struct bj_vec3 r)
3D cross product: res = l × r (right-hand rule).
Definition vec.h:446
static struct bj_vec2 bj_vec2_scale_to_len(struct bj_vec2 v, bj_real L)
Scale a 2D vector to a given length.
Definition vec.h:164
static struct bj_vec2 bj_vec2_scale(struct bj_vec2 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:114
static struct bj_vec3 bj_vec3_add_scaled(struct bj_vec3 lhs, struct bj_vec3 rhs, bj_real s)
Add a scaled vector: res = a + s * b.
Definition vec.h:280
static struct bj_vec3 bj_vec3_scale_to_len(struct bj_vec3 v, bj_real L)
Scale a 3D vector to a given length.
Definition vec.h:349
static bj_real bj_vec4_len(struct bj_vec4 v)
Euclidean length (L2 norm) of a 4D vector.
Definition vec.h:562
static struct bj_vec4 bj_vec4_add_scaled(struct bj_vec4 lhs, struct bj_vec4 rhs, bj_real s)
Add a scaled vector: res = a + s * b.
Definition vec.h:501
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:53
static struct bj_vec3 bj_vec3_normalize(struct bj_vec3 v)
Normalize a 3D vector to unit length (safe).
Definition vec.h:392
static struct bj_vec3 bj_vec3_min(struct bj_vec3 a, struct bj_vec3 b)
Component-wise minimum of two 3D vectors.
Definition vec.h:423
#define bj_sqrt
Square root.
Definition math.h:222
float bj_real
Selected real type for float configuration.
Definition math.h:51
static bj_real bj_vec3_distance(struct bj_vec3 a, struct bj_vec3 b)
Euclidean distance between two 3D vectors.
Definition vec.h:376
static struct bj_vec2 bj_vec2_normalize_unsafe(struct bj_vec2 v)
Normalize a 2D vector to unit length (unsafe).
Definition vec.h:229
struct bj_vec2: 2D vector of bj_real values.
Definition vec.h:26
struct bj_vec3: 3D vector of bj_real values.
Definition vec.h:38
struct bj_vec4: 4D vector of bj_real values.
Definition vec.h:51
C99 math shim with bj_real precision type and scalar utilities.