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
27typedef bj_real bj_vec2[2];
28
34typedef bj_real bj_vec3[3];
35
41typedef bj_real bj_vec4[4];
42
50 res[0] = a; res[1] = b;
51}
52
53static BJ_INLINE void bj_vec2_zero(bj_vec2 res) {
54 res[0] = res[1] = BJ_FZERO;
55}
56
57static BJ_INLINE void bj_vec2_apply(bj_vec2 res, const bj_vec2 a, bj_real(*f)(bj_real)) {
58 res[0] = f(a[0]);
59 res[1] = f(a[1]);
60}
61
70static BJ_INLINE void bj_vec2_add(bj_vec2 res, const bj_vec2 lhs, const bj_vec2 rhs) {
71 res[0] = lhs[0] + rhs[0];
72 res[1] = lhs[1] + rhs[1];
73}
74
75
84 bj_vec2 res,
85 const bj_vec2 a,
86 const bj_vec2 b,
87 bj_real s
88) {
89 res[0] = a[0] + b[0] * s;
90 res[1] = a[1] + b[1] * s;
91}
92
99static BJ_INLINE void bj_vec2_sub(bj_vec2 res, const bj_vec2 lhs, const bj_vec2 rhs) {
100 res[0] = lhs[0] - rhs[0];
101 res[1] = lhs[1] - rhs[1];
102}
103
110static BJ_INLINE void bj_vec2_scale(bj_vec2 res, const bj_vec2 v, bj_real s) {
111 res[0] = v[0] * s;
112 res[1] = v[1] * s;
113}
114
121static BJ_INLINE void bj_vec2_scale_each(bj_vec2 res, const bj_vec2 v, const bj_vec2 s) {
122 res[0] = v[0] * s[0];
123 res[1] = v[1] * s[1];
124}
125
132static BJ_INLINE bj_real bj_vec2_dot(const bj_vec2 a, const bj_vec2 b) {
133 return a[0] * b[0] + a[1] * b[1];
134}
135
141 return a[0]*b[1] - a[1]*b[0];
142}
143
150 return bj_sqrt(v[0] * v[0] + v[1] * v[1]);
151}
152
161static BJ_INLINE void bj_vec2_set_len(bj_vec2 res, const bj_vec2 v, bj_real target_len) {
162 bj_real len = bj_vec2_len(v);
163 bj_real scale = target_len / len;
164 bj_vec2_scale(res, v, scale);
165}
166
175 const bj_real dx = a[0] - b[0];
176 const bj_real dy = a[1] - b[1];
177 return dx * dx + dy * dy;
178}
179
187static BJ_INLINE bj_real bj_vec2_dist(const bj_vec2 a, const bj_vec2 b) {
188 return bj_sqrt(bj_vec2_dist_squared(a, b));
189}
190
197static BJ_INLINE void bj_vec2_normalize(bj_vec2 res, const bj_vec2 v) {
198 bj_real inv_len = BJ_F(1.0) / bj_sqrt(v[0] * v[0] + v[1] * v[1]);
199 res[0] = v[0] * inv_len;
200 res[1] = v[1] * inv_len;
201}
202
209static BJ_INLINE void bj_vec2_min(bj_vec2 res, const bj_vec2 a, const bj_vec2 b) {
210 res[0] = a[0] < b[0] ? a[0] : b[0];
211 res[1] = a[1] < b[1] ? a[1] : b[1];
212}
213
220static BJ_INLINE void bj_vec2_max(bj_vec2 res, const bj_vec2 a, const bj_vec2 b) {
221 res[0] = a[0] > b[0] ? a[0] : b[0];
222 res[1] = a[1] > b[1] ? a[1] : b[1];
223}
224
230static BJ_INLINE void bj_vec2_copy(bj_vec2 res, const bj_vec2 src) {
231 res[0] = src[0];
232 res[1] = src[1];
233}
234
243 res[0] = a; res[1] = b; res[2] = c;
244}
245
247 res[0] = res[1] = res[2] = BJ_FZERO;
248}
249
250static BJ_INLINE void bj_vec3_apply(bj_vec3 res, const bj_vec3 a, bj_real(*f)(bj_real)) {
251 res[0] = f(a[0]);
252 res[1] = f(a[1]);
253 res[2] = f(a[2]);
254}
255
256
263static BJ_INLINE void bj_vec3_add(bj_vec3 res, const bj_vec3 lhs, const bj_vec3 rhs) {
264 res[0] = lhs[0] + rhs[0];
265 res[1] = lhs[1] + rhs[1];
266 res[2] = lhs[2] + rhs[2];
267}
268
276static BJ_INLINE void bj_vec3_add_scaled(bj_vec3 res, const bj_vec3 a, const bj_vec3 b, bj_real s) {
277 res[0] = a[0] + b[0] * s;
278 res[1] = a[1] + b[1] * s;
279 res[2] = a[2] + b[2] * s;
280}
281
282
289static BJ_INLINE void bj_vec3_sub(bj_vec3 res, const bj_vec3 lhs, const bj_vec3 rhs) {
290 res[0] = lhs[0] - rhs[0];
291 res[1] = lhs[1] - rhs[1];
292 res[2] = lhs[2] - rhs[2];
293}
294
301static BJ_INLINE void bj_vec3_scale(bj_vec3 res, const bj_vec3 v, bj_real s) {
302 res[0] = v[0] * s;
303 res[1] = v[1] * s;
304 res[2] = v[2] * s;
305}
306
313static BJ_INLINE bj_real bj_vec3_dot(const bj_vec3 a, const bj_vec3 b) {
314 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
315}
316
323 return bj_sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
324}
325
334static BJ_INLINE void bj_vec3_set_len(bj_vec3 res, const bj_vec3 v, bj_real target_len) {
335 bj_real len = bj_vec3_len(v);
336 bj_real scale = target_len / len;
337 bj_vec3_scale(res, v, scale);
338}
339
348 const bj_real dx = a[0] - b[0];
349 const bj_real dy = a[1] - b[1];
350 const bj_real dz = a[2] - b[2];
351 return dx * dx + dy * dy + dz * dz;
352}
353
361static BJ_INLINE bj_real bj_vec3_dist(const bj_vec3 a, const bj_vec3 b) {
362 return bj_sqrt(bj_vec3_dist_squared(a, b));
363}
364
365
372static BJ_INLINE void bj_vec3_normalize(bj_vec3 res, const bj_vec3 v) {
373 bj_real inv_len = BJ_F(1.0) / bj_sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
374 res[0] = v[0] * inv_len;
375 res[1] = v[1] * inv_len;
376 res[2] = v[2] * inv_len;
377}
378
385static BJ_INLINE void bj_vec3_min(bj_vec3 res, const bj_vec3 a, const bj_vec3 b) {
386 res[0] = a[0] < b[0] ? a[0] : b[0];
387 res[1] = a[1] < b[1] ? a[1] : b[1];
388 res[2] = a[2] < b[2] ? a[2] : b[2];
389}
390
397static BJ_INLINE void bj_vec3_max(bj_vec3 res, const bj_vec3 a, const bj_vec3 b) {
398 res[0] = a[0] > b[0] ? a[0] : b[0];
399 res[1] = a[1] > b[1] ? a[1] : b[1];
400 res[2] = a[2] > b[2] ? a[2] : b[2];
401}
402
408static BJ_INLINE void bj_vec3_copy(bj_vec3 res, const bj_vec3 src) {
409 res[0] = src[0];
410 res[1] = src[1];
411 res[2] = src[2];
412}
413
420static BJ_INLINE void bj_vec3_cross(bj_vec3 res, const bj_vec3 l, const bj_vec3 r)
421{
422 res[0] = l[1] * r[2] - l[2] * r[1];
423 res[1] = l[2] * r[0] - l[0] * r[2];
424 res[2] = l[0] * r[1] - l[1] * r[0];
425}
426
434static BJ_INLINE void bj_vec3_reflect(bj_vec3 res, const bj_vec3 v, const bj_vec3 n)
435{
436 const bj_real p = BJ_F(2.) * bj_vec3_dot(v, n);
437 for (int i = 0; i < 3; ++i) {
438 res[i] = v[i] - p * n[i];
439 }
440}
441
442
452 res[0] = a; res[1] = b; res[2] = c; res[3] = d;
453}
454
455static BJ_INLINE void bj_vec4_apply(bj_vec4 res, const bj_vec4 a, bj_real(*f)(bj_real)) {
456 res[0] = f(a[0]);
457 res[1] = f(a[1]);
458 res[2] = f(a[2]);
459 res[3] = f(a[3]);
460}
461
468static BJ_INLINE void bj_vec4_add(bj_vec4 res, const bj_vec4 lhs, const bj_vec4 rhs) {
469 res[0] = lhs[0] + rhs[0];
470 res[1] = lhs[1] + rhs[1];
471 res[2] = lhs[2] + rhs[2];
472 res[3] = lhs[3] + rhs[3];
473}
474
482static BJ_INLINE void bj_vec4_add_scaled(bj_vec4 res, const bj_vec4 a, const bj_vec4 b, bj_real s) {
483 res[0] = a[0] + b[0] * s;
484 res[1] = a[1] + b[1] * s;
485 res[2] = a[2] + b[2] * s;
486 res[3] = a[3] + b[3] * s;
487}
488
495static BJ_INLINE void bj_vec4_sub(bj_vec4 res, const bj_vec4 lhs, const bj_vec4 rhs) {
496 res[0] = lhs[0] - rhs[0];
497 res[1] = lhs[1] - rhs[1];
498 res[2] = lhs[2] - rhs[2];
499 res[3] = lhs[3] - rhs[3];
500}
501
508static BJ_INLINE void bj_vec4_scale(bj_vec4 res, const bj_vec4 v, bj_real s) {
509 res[0] = v[0] * s;
510 res[1] = v[1] * s;
511 res[2] = v[2] * s;
512 res[3] = v[3] * s;
513}
514
521static BJ_INLINE bj_real bj_vec4_dot(const bj_vec4 a, const bj_vec4 b) {
522 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
523}
524
531 return bj_sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
532}
533
540static BJ_INLINE void bj_vec4_normalize(bj_vec4 res, const bj_vec4 v) {
541 bj_real inv_len = BJ_F(1.0) / bj_sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
542 res[0] = v[0] * inv_len;
543 res[1] = v[1] * inv_len;
544 res[2] = v[2] * inv_len;
545 res[3] = v[3] * inv_len;
546}
547
554static BJ_INLINE void bj_vec4_min(bj_vec4 res, const bj_vec4 a, const bj_vec4 b) {
555 res[0] = a[0] < b[0] ? a[0] : b[0];
556 res[1] = a[1] < b[1] ? a[1] : b[1];
557 res[2] = a[2] < b[2] ? a[2] : b[2];
558 res[3] = a[3] < b[3] ? a[3] : b[3];
559}
560
567static BJ_INLINE void bj_vec4_max(bj_vec4 res, const bj_vec4 a, const bj_vec4 b) {
568 res[0] = a[0] > b[0] ? a[0] : b[0];
569 res[1] = a[1] > b[1] ? a[1] : b[1];
570 res[2] = a[2] > b[2] ? a[2] : b[2];
571 res[3] = a[3] > b[3] ? a[3] : b[3];
572}
573
579static BJ_INLINE void bj_vec4_copy(bj_vec4 res, const bj_vec4 src) {
580 res[0] = src[0];
581 res[1] = src[1];
582 res[2] = src[2];
583 res[3] = src[3];
584}
585
593static BJ_INLINE void bj_vec4_cross(bj_vec4 res, const bj_vec4 l, const bj_vec4 r)
594{
595 res[0] = l[1] * r[2] - l[2] * r[1];
596 res[1] = l[2] * r[0] - l[0] * r[2];
597 res[2] = l[0] * r[1] - l[1] * r[0];
598 res[3] = BJ_F(1.0);
599}
600
608static BJ_INLINE void bj_vec4_reflect(bj_vec4 res, const bj_vec4 v, const bj_vec4 n)
609{
610 bj_real p = BJ_F(2.0) * bj_vec4_dot(v, n);
611 for (int i = 0; i < 4; ++i) {
612 res[i] = v[i] - p * n[i];
613 }
614}
615
616
617#endif
618
620
General-purpose definitions for Banjo API.
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
Definition api.h:260
bj_real bj_vec3[3]
bj_vec3: 3D vector of bj_real values.
Definition vec.h:34
static void bj_vec4_reflect(bj_vec4 res, const bj_vec4 v, const bj_vec4 n)
Reflect a vector about a normal: res = v - 2*dot(v, n)*n.
Definition vec.h:608
static void bj_vec2_scale_each(bj_vec2 res, const bj_vec2 v, const bj_vec2 s)
Per-component scaling: res[i] = v[i] * s[i].
Definition vec.h:121
bj_real bj_vec2[2]
bj_vec2: 2D vector of bj_real values.
Definition vec.h:27
static void bj_vec3_add(bj_vec3 res, const bj_vec3 lhs, const bj_vec3 rhs)
Component-wise addition of two 3D vectors: res = lhs + rhs.
Definition vec.h:263
static void bj_vec2_scale(bj_vec2 res, const bj_vec2 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:110
static void bj_vec2_set_len(bj_vec2 res, const bj_vec2 v, bj_real target_len)
Scale a 2D vector to a given length.
Definition vec.h:161
static bj_real bj_vec2_dist(const bj_vec2 a, const bj_vec2 b)
Euclidean distance between two 2D vectors.
Definition vec.h:187
static bj_real bj_vec3_dist(const bj_vec3 a, const bj_vec3 b)
Euclidean distance between two 3D vectors.
Definition vec.h:361
static bj_real bj_vec3_dist_squared(const bj_vec3 a, const bj_vec3 b)
Squared Euclidean distance between two 3D vectors.
Definition vec.h:347
static void bj_vec2_zero(bj_vec2 res)
Definition vec.h:53
static void bj_vec2_add(bj_vec2 res, const bj_vec2 lhs, const bj_vec2 rhs)
Component-wise addition of two 2D vectors: res = lhs + rhs.
Definition vec.h:70
static bj_real bj_vec2_len(const bj_vec2 v)
Euclidean length (L2 norm) of a 2D vector.
Definition vec.h:149
bj_real bj_vec4[4]
bj_vec4: 4D vector of bj_real values.
Definition vec.h:41
static void bj_vec3_add_scaled(bj_vec3 res, const bj_vec3 a, const bj_vec3 b, bj_real s)
Add a scaled vector: res = a + s * b.
Definition vec.h:276
static void bj_vec3_scale(bj_vec3 res, const bj_vec3 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:301
static void bj_vec2_min(bj_vec2 res, const bj_vec2 a, const bj_vec2 b)
Component-wise minimum of two 2D vectors.
Definition vec.h:209
static void bj_vec3_max(bj_vec3 res, const bj_vec3 a, const bj_vec3 b)
Component-wise maximum of two 3D vectors.
Definition vec.h:397
static bj_real bj_vec3_dot(const bj_vec3 a, const bj_vec3 b)
Dot product of two 3D vectors.
Definition vec.h:313
static void bj_vec2_set(bj_vec2 res, bj_real a, bj_real b)
Set a 2D vector from components (x, y).
Definition vec.h:49
static void bj_vec2_add_scaled(bj_vec2 res, const bj_vec2 a, const bj_vec2 b, bj_real s)
Add a scaled vector: res = a + s * b.
Definition vec.h:83
static void bj_vec2_apply(bj_vec2 res, const bj_vec2 a, bj_real(*f)(bj_real))
Definition vec.h:57
static void bj_vec3_set_len(bj_vec3 res, const bj_vec3 v, bj_real target_len)
Scale a 3D vector to a given length.
Definition vec.h:334
static bj_real bj_vec4_len(const bj_vec4 v)
Euclidean length (L2 norm) of a 4D vector.
Definition vec.h:530
static void bj_vec3_min(bj_vec3 res, const bj_vec3 a, const bj_vec3 b)
Component-wise minimum of two 3D vectors.
Definition vec.h:385
static void bj_vec4_sub(bj_vec4 res, const bj_vec4 lhs, const bj_vec4 rhs)
Component-wise subtraction of two 4D vectors: res = lhs - rhs.
Definition vec.h:495
static void bj_vec2_sub(bj_vec2 res, const bj_vec2 lhs, const bj_vec2 rhs)
Component-wise subtraction of two 2D vectors: res = lhs - rhs.
Definition vec.h:99
static bj_real bj_vec3_len(const bj_vec3 v)
Euclidean length (L2 norm) of a 3D vector.
Definition vec.h:322
static void bj_vec4_cross(bj_vec4 res, const bj_vec4 l, const bj_vec4 r)
Cross product using xyz components; w is set to 1.
Definition vec.h:593
static void bj_vec3_zero(bj_vec3 res)
Definition vec.h:246
static void bj_vec4_copy(bj_vec4 res, const bj_vec4 src)
Copy a 4D vector.
Definition vec.h:579
static void bj_vec4_max(bj_vec4 res, const bj_vec4 a, const bj_vec4 b)
Component-wise maximum of two 4D vectors.
Definition vec.h:567
static void bj_vec4_set(bj_vec4 res, bj_real a, bj_real b, bj_real c, bj_real d)
Set a 4D vector from components (x, y, z, w).
Definition vec.h:451
static void bj_vec2_normalize(bj_vec2 res, const bj_vec2 v)
Normalize a 2D vector to unit length.
Definition vec.h:197
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:64
static void bj_vec4_add_scaled(bj_vec4 res, const bj_vec4 a, const bj_vec4 b, bj_real s)
Add a scaled vector: res = a + s * b.
Definition vec.h:482
static bj_real bj_vec2_dist_squared(const bj_vec2 a, const bj_vec2 b)
Squared Euclidean distance between two 2D vectors.
Definition vec.h:174
static void bj_vec3_set(bj_vec3 res, bj_real a, bj_real b, bj_real c)
Set a 3D vector from components (x, y, z).
Definition vec.h:242
static void bj_vec3_apply(bj_vec3 res, const bj_vec3 a, bj_real(*f)(bj_real))
Definition vec.h:250
static void bj_vec2_copy(bj_vec2 res, const bj_vec2 src)
Copy a 2D vector.
Definition vec.h:230
static void bj_vec2_max(bj_vec2 res, const bj_vec2 a, const bj_vec2 b)
Component-wise maximum of two 2D vectors.
Definition vec.h:220
static bj_real bj_vec2_dot(const bj_vec2 a, const bj_vec2 b)
Dot product of two 2D vectors.
Definition vec.h:132
static void bj_vec4_normalize(bj_vec4 res, const bj_vec4 v)
Normalize a 4D vector to unit length.
Definition vec.h:540
static void bj_vec4_add(bj_vec4 res, const bj_vec4 lhs, const bj_vec4 rhs)
Component-wise addition of two 4D vectors: res = lhs + rhs.
Definition vec.h:468
static bj_real bj_vec2_cross(const bj_vec2 a, const bj_vec2 b)
2D "cross product" (perp dot): returns scalar a.x*b.y - a.y*b.x.
Definition vec.h:140
static void bj_vec4_scale(bj_vec4 res, const bj_vec4 v, bj_real s)
Uniform scaling by scalar: res = v * s.
Definition vec.h:508
static void bj_vec3_copy(bj_vec3 res, const bj_vec3 src)
Copy a 3D vector.
Definition vec.h:408
static void bj_vec3_reflect(bj_vec3 res, const bj_vec3 v, const bj_vec3 n)
Reflect a vector about a normal: res = v - 2*dot(v, n)*n.
Definition vec.h:434
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:53
static void bj_vec4_min(bj_vec4 res, const bj_vec4 a, const bj_vec4 b)
Component-wise minimum of two 4D vectors.
Definition vec.h:554
#define bj_sqrt
Square root.
Definition math.h:222
float bj_real
Selected real type for float configuration.
Definition math.h:51
static void bj_vec3_sub(bj_vec3 res, const bj_vec3 lhs, const bj_vec3 rhs)
Component-wise subtraction of two 3D vectors: res = lhs - rhs.
Definition vec.h:289
static bj_real bj_vec4_dot(const bj_vec4 a, const bj_vec4 b)
Dot product of two 4D vectors.
Definition vec.h:521
static void bj_vec4_apply(bj_vec4 res, const bj_vec4 a, bj_real(*f)(bj_real))
Definition vec.h:455
static void bj_vec3_cross(bj_vec3 res, const bj_vec3 l, const bj_vec3 r)
3D cross product: res = l × r (right-hand rule).
Definition vec.h:420
static void bj_vec3_normalize(bj_vec3 res, const bj_vec3 v)
Normalize a 3D vector to unit length.
Definition vec.h:372
C99 math shim with bj_real precision type and scalar utilities.