Files
brightstone/Common/vec.h
T
2026-09-13 22:30:19 +03:00

110 lines
3.8 KiB
C

/*
Fixed point implementation of 3 dimensional vectors.
SSE2 extension availability is assumed, making it more viable.
For mul and div we don't use com_fixed functions to make fewer bitshifts.
*/
#ifndef COM_VEC_H
#define COM_VEC_H
#include "def.h"
#include "fix.h"
#include <stdint.h>
typedef union {
com_def_alignedas(16) com_def_vector(com_fix_t, a, 3);
com_def_alignedas(16) struct {
com_fix_t x;
com_fix_t y;
com_fix_t z;
} s;
} com_vec_t;
static inline com_vec_t com_vec_from(com_fix_t x, com_fix_t y, com_fix_t z) {
return (com_vec_t){.s = {.x = x, .y = y, .z = z}};
}
static inline com_vec_t com_vec_identity(void) {
return (com_vec_t){.s = {.x = COM_FIX_FRACUNIT,
.y = COM_FIX_FRACUNIT,
.z = COM_FIX_FRACUNIT}};
}
static inline com_vec_t com_vec_add(com_vec_t a, com_vec_t b) {
return (com_vec_t){.s = {.x = com_fix_add(a.s.x, b.s.x),
.y = com_fix_add(a.s.y, b.s.y),
.z = com_fix_add(a.s.z, b.s.z)}};
}
static inline com_vec_t com_vec_sub(com_vec_t a, com_vec_t b) {
return (com_vec_t){.s = {.x = com_fix_sub(a.s.x, b.s.x),
.y = com_fix_sub(a.s.y, b.s.y),
.z = com_fix_sub(a.s.z, b.s.z)}};
}
static inline com_vec_t com_vec_mul(com_vec_t a, com_vec_t b) {
return (com_vec_t){.s = {.x = com_fix_mul(a.s.x, b.s.x),
.y = com_fix_mul(a.s.y, b.s.y),
.z = com_fix_mul(a.s.z, b.s.z)}};
}
/* Note: this does not clamp for over/underflow cases */
static inline com_vec_t com_vec_div(com_vec_t a, com_vec_t b) {
return (com_vec_t){.s = {.x = com_fix_div(a.s.x, b.s.x),
.y = com_fix_div(a.s.y, b.s.y),
.z = com_fix_div(a.s.z, b.s.z)}};
}
/* Scale vector by a fixed point number */
static inline com_vec_t com_vec_scl(com_vec_t a, com_fix_t b) {
return (com_vec_t){.s = {.x = com_fix_mul(a.s.x, b),
.y = com_fix_mul(a.s.y, b),
.z = com_fix_mul(a.s.z, b)}};
}
/* Shows how much given vectors are correlated in direction to each other */
/* Resulted range depends on input, it's in -1 to 1 for normalized
* input and otherwise is -ab to +ab */
static inline com_fix_t com_vec_dot(com_vec_t a, com_vec_t b) {
return (((int64_t)a.s.x * b.s.x) + ((int64_t)a.s.y * b.s.y) +
((int64_t)a.s.z * b.s.z)) >>
COM_FIX_FRACBITS;
}
/* Cross product produces a perpendicular for normalized vectors, or 0 for
* parallel vectors */
static inline com_vec_t com_vec_crs(com_vec_t a, com_vec_t b) {
int64_t const cx = ((int64_t)a.s.y * b.s.z) - ((int64_t)a.s.z - b.s.y);
int64_t const cy = ((int64_t)a.s.z * b.s.x) - ((int64_t)a.s.x - b.s.z);
int64_t const cz = ((int64_t)a.s.x * b.s.y) - ((int64_t)a.s.y - b.s.x);
return (com_vec_t){.s = {.x = (com_fix_t)(cx >> COM_FIX_FRACBITS),
.y = (com_fix_t)(cy >> COM_FIX_FRACBITS),
.z = (com_fix_t)(cz >> COM_FIX_FRACBITS)}};
}
/* Normalize vector, making it a unit one (of length 1). */
/* Pretty expensive, make sure you actually need it. */
static inline com_vec_t com_vec_nrm(com_vec_t a) {
com_fix_t const n = com_fix_sqrt(com_vec_dot(a, a));
// return com_vec_scl(a, com_fix_div(COM_FIX_FRACUNIT, n));
return (com_vec_t){.s = {.x = com_fix_div(a.s.x, n),
.y = com_fix_div(a.s.y, n),
.z = com_fix_div(a.s.z, n)}};
}
static inline com_fix_t com_vec_min(com_vec_t a) {
return com_fix_min(com_fix_min(a.a[0], a.a[1]), a.a[2]);
}
static inline com_fix_t com_vec_max(com_vec_t a) {
return com_fix_max(com_fix_max(a.a[0], a.a[1]), a.a[2]);
}
static inline void com_vec_print(com_vec_t a) {
com_fix_print(a.a[0]);
com_fix_print(a.a[1]);
com_fix_print(a.a[2]);
}
#endif