63#define BJ_VEC2_ZERO ((struct bj_vec2){BJ_FZERO, BJ_FZERO})
77#define BJ_VEC3_ZERO ((struct bj_vec3){BJ_FZERO, BJ_FZERO, BJ_FZERO})
93#define BJ_VEC4_ZERO ((struct bj_vec4){BJ_FZERO, BJ_FZERO, BJ_FZERO, BJ_FZERO})
111 return (
struct bj_vec2){ .x = f(a.x), .y = f(a.y), };
122 return (
struct bj_vec2){ .x = lhs.x + rhs.x, .y = lhs.y + rhs.y, };
142 return (
struct bj_vec2){ .x = lhs.x + rhs.x * s, .y = lhs.y + rhs.y * s, };
156 return (
struct bj_vec2){ .x = lhs.x - rhs.x, .y = lhs.y - rhs.y, };
167 return (
struct bj_vec2){ .x = v.x * s, .y = v.y * s, };
185 return (
struct bj_vec2){ .x = v.x * s.x, .y = v.y * s.y, };
196 return a.
x * b.
x + a.
y * b.
y;
218 return a.
x*b.
y - a.
y*b.
x;
266 return dx * dx + dy * dy;
296 const bj_real l2 = v.x * v.x + v.y * v.y;
302 return (
struct bj_vec2){ v.x * inv, v.y * inv };
322 return (
struct bj_vec2){ v.x * inv, v.y * inv };
333 return (
struct bj_vec2){a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, };
344 return (
struct bj_vec2){a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, };
358 return (
struct bj_vec3){ .x = f(a.x), .y = f(a.y), .z = f(a.z), };
390 .x = lhs.x + rhs.x * s,
391 .y = lhs.y + rhs.y * s,
392 .z = lhs.z + rhs.z * s,
444 return (
struct bj_vec3){ .x = v.x * s.x, .y = v.y * s.y, .z = v.z * s.z, };
455 return a.
x * b.
x + a.
y * b.
y + a.
z * b.
z;
503 return dx * dx + dy * dy + dz * dz;
532 const bj_real l2 = v.x * v.x + v.y * v.y + v.z * v.z;
538 return (
struct bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
556 return (
struct bj_vec3){ v.x * inv, v.y * inv, v.z * inv };
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,
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,};
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,
639 return (
struct bj_vec4){ .x = f(a.x), .y = f(a.y), .z = f(a.z), .w = f(a.w), };
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,
722 return a.
x * b.
x + a.
y * b.
y + a.
z * b.
z + a.
w * b.
w;
750 const bj_real len2 = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
756 return (
struct bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
774 return (
struct bj_vec4){ v.x * inv, v.y * inv, v.z * inv, v.w * inv };
793 .x = v.x * s.x, .y = v.y * s.y, .z = v.z * s.z, .w = v.w * s.w,
833 return dx * dx + dy * dy + dz * dz + dw * dw;
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,
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,
General-purpose definitions for Banjo API.
#define BJ_INLINE
BJ_INLINE expands to an inline specifier appropriate for the toolchain.
bj_real w
W component (scalar part for quaternions).
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)).
static struct bj_vec4 bj_vec4_sub(struct bj_vec4 lhs, struct bj_vec4 rhs)
Component-wise subtraction: lhs - rhs.
static bj_real bj_vec2_len(struct bj_vec2 v)
Euclidean length: sqrt(x^2 + y^2).
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)).
static struct bj_vec3 bj_vec3_scale(struct bj_vec3 v, bj_real s)
Uniform scaling by a scalar: v * s.
static struct bj_vec3 bj_vec3_sub(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise subtraction: lhs - rhs.
static struct bj_vec2 bj_vec2_add(struct bj_vec2 lhs, struct bj_vec2 rhs)
Component-wise addition: lhs + rhs.
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)).
static struct bj_vec3 bj_vec3_max(struct bj_vec3 a, struct bj_vec3 b)
Component-wise maximum.
static bj_real bj_vec2_dot(struct bj_vec2 a, struct bj_vec2 b)
Dot product: a.x*b.x + a.y*b.y.
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).
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.
static bj_real bj_vec4_dot(struct bj_vec4 a, struct bj_vec4 b)
Dot product over all four components.
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)).
static bj_real bj_vec3_distance_sq(struct bj_vec3 a, struct bj_vec3 b)
Squared Euclidean distance: ||a - b||^2.
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.
static struct bj_vec4 bj_vec4_normalize_unsafe(struct bj_vec4 v)
Normalise to unit length (unsafe).
static bj_real bj_vec3_len(struct bj_vec3 v)
Euclidean length: sqrt(x^2 + y^2 + z^2).
static struct bj_vec2 bj_vec2_sub(const struct bj_vec2 lhs, const struct bj_vec2 rhs)
Component-wise subtraction: lhs - rhs.
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.
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.
static struct bj_vec4 bj_vec4_max(struct bj_vec4 a, struct bj_vec4 b)
Component-wise maximum across all four components.
static struct bj_vec4 bj_vec4_scale(struct bj_vec4 v, bj_real s)
Uniform scaling by a scalar: v * s.
static struct bj_vec3 bj_vec3_normalize_unsafe(struct bj_vec3 v)
Normalise to unit length (unsafe).
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).
static struct bj_vec4 bj_vec4_add(struct bj_vec4 lhs, struct bj_vec4 rhs)
Component-wise addition: lhs + rhs.
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.
static struct bj_vec4 bj_vec4_min(struct bj_vec4 a, struct bj_vec4 b)
Component-wise minimum across all four components.
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.
static bj_real bj_vec4_distance(struct bj_vec4 a, struct bj_vec4 b)
Euclidean distance: ||a - b||.
#define BJ_FZERO
Zero constant in bj_real.
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.
static struct bj_vec3 bj_vec3_add(struct bj_vec3 lhs, struct bj_vec3 rhs)
Component-wise addition: lhs + rhs.
static struct bj_vec2 bj_vec2_normalize(struct bj_vec2 v)
Normalise to unit length (safe).
static bj_real bj_vec2_distance_sq(struct bj_vec2 a, struct bj_vec2 b)
Squared Euclidean distance: ||a - b||^2.
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
static bj_real bj_vec2_distance(const struct bj_vec2 a, const struct bj_vec2 b)
Euclidean distance: ||a - b||.
static struct bj_vec4 bj_vec4_normalize(struct bj_vec4 v)
Normalise to unit length (safe).
static struct bj_vec3 bj_vec3_cross(struct bj_vec3 l, struct bj_vec3 r)
3D cross product: l × r (right-hand rule).
static struct bj_vec2 bj_vec2_scale_to_len(struct bj_vec2 v, bj_real L)
Rescale a vector to a chosen length.
static struct bj_vec2 bj_vec2_scale(struct bj_vec2 v, bj_real s)
Uniform scaling by a scalar: v * s.
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.
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).
static struct bj_vec3 bj_vec3_scale_to_len(struct bj_vec3 v, bj_real L)
Rescale a vector to a chosen length.
#define BJ_EPSILON
Machine epsilon for bj_real when float is selected.
static bj_real bj_vec4_len(struct bj_vec4 v)
Euclidean length: sqrt(x^2 + y^2 + z^2 + w^2).
static bj_real bj_vec4_distance_sq(struct bj_vec4 a, struct bj_vec4 b)
Squared Euclidean distance: ||a - b||^2.
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.
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
static struct bj_vec3 bj_vec3_normalize(struct bj_vec3 v)
Normalise to unit length (safe).
static struct bj_vec3 bj_vec3_min(struct bj_vec3 a, struct bj_vec3 b)
Component-wise minimum.
#define bj_sqrt
Square root.
float bj_real
Selected real type for float configuration.
static bj_real bj_vec3_distance(struct bj_vec3 a, struct bj_vec3 b)
Euclidean distance: ||a - b||.
static struct bj_vec2 bj_vec2_normalize_unsafe(struct bj_vec2 v)
Normalise to unit length (unsafe).
static struct bj_vec4 bj_vec4_scale_to_len(struct bj_vec4 v, bj_real len)
Rescale a vector to a chosen length.
2D vector of bj_real components.
3D vector of bj_real components.
4D vector of bj_real components.
C99 math shim with bj_real precision type and scalar utilities.