src/vector3.c file

Functions

auto vec3_zero(void) -> vector3
Returns a zero-initialized vector3.
auto vec3_one(void) -> vector3
Returns a vector3 with all components set to 1.0f.
auto vec3_x_axis(const vm_float_t x) -> vector3
Returns a vector3 along the x-axis.
auto vec3_y_axis(const vm_float_t y) -> vector3
Returns a vector3 along the y-axis.
auto vec3_z_axis(const vm_float_t z) -> vector3
Returns a vector3 along the z-axis.
auto vec3_x_scale(const vm_float_t x) -> vector3
Returns a vector3 for scaling along the x-axis.
auto vec3_y_scale(const vm_float_t y) -> vector3
Returns a vector3 for scaling along the y-axis.
auto vec3_z_scale(const vm_float_t z) -> vector3
Returns a vector3 for scaling along the z-axis.
auto vec3_add(const vector3 a, const vector3 b) -> vector3
Component-wise addition of two vectors.
auto vec3_sub(const vector3 a, const vector3 b) -> vector3
Component-wise subtraction of two vectors.
auto vec3_mul_scalar(const vector3 v, const vm_float_t s) -> vector3
Component-wise multiplication of vector by scalar.
auto vec3_div_scalar(const vector3 v, const vm_float_t s) -> vector3
Component-wise division of vector by scalar.
auto vec3_mul(const vector3 a, const vector3 b) -> vector3
Component-wise multiplication of two vectors.
auto vec3_neg(const vector3 v) -> vector3
Negation of a vector.
auto vec3_abs(const vector3 v) -> vector3
Computes the absolute value per component of a vector3.
auto vec3_cross(const vector3 a, const vector3 b) -> vector3
Computes the cross-product of two vector3.
auto vec3_normalize(const vector3 v) -> vector3
Normalizes a vector3 to unit length.
auto vec3_min(const vector3 a, const vector3 b) -> vector3
Computes the component-wise minimum of two vector3.
auto vec3_max(const vector3 a, const vector3 b) -> vector3
Computes the component-wise maximum of two vector3.
auto vec3_sign(const vector3 v) -> vector3
Computes the sign per component of a vector3 (-1, 0, or 1).
auto vec3_floor(const vector3 v) -> vector3
Applies the floor per component to a vector3.
auto vec3_ceil(const vector3 v) -> vector3
Applies ceil per component to a vector3.
auto vec3_round(const vector3 v) -> vector3
Applies round per component to a vector3.
auto vec3_reflect(const vector3 incident, const vector3 normal) -> vector3
Reflects an incident vector over normal.
auto vec3_refract(const vector3 incident, const vector3 normal, const vm_float_t eta) -> vector3
Refracts an incident vector across an interface with a given normal and ratio of refraction eta.
auto vec3_lerp(const vector3 a, const vector3 b, const vm_float_t t) -> vector3
Linearly interpolates between two vector3.
auto vec3_clamp(const vector3 v, const vector3 min, const vector3 max) -> vector3
Clamps a vector3 between min and max per component.
auto vec3_distance(const vector3 a, const vector3 b) -> vm_float_t
Computes the Euclidean distance between two vector3.
auto vec3_angle(const vector3 a, const vector3 b) -> vm_float_t
Computes the angle between two non-zero vector3 in radians.
auto vec3_dot(const vector3 a, const vector3 b) -> vm_float_t
Computes the dot product of two vector3.
auto vec3_length(const vector3 v) -> vm_float_t
Computes the length (magnitude) of a vector3.
auto vec3_scale(const vector3 v, const vm_float_t s) -> vector3
Scales the vector by a scalar.
auto vec3_div(const vector3 a, const vector3 b) -> vector3
Divides two vectors component-wise.
auto vec3_add_scalar(const vector3 v, const vm_float_t s) -> vector3
Adds a scalar to each component.
auto vec3_sub_scalar(const vector3 v, const vm_float_t s) -> vector3
Subtracts a scalar from each component.
auto vec3_clamp_scalar(const vector3 v, const vm_float_t min, const vm_float_t max) -> vector3
Clamps each component to the scalar range [min, max].
auto vec3_saturate(const vector3 v) -> vector3
Clamps each component to the range [0, 1].
auto vec3_fract(const vector3 v) -> vector3
Returns the fractional part of each component.
auto vec3_project(const vector3 a, const vector3 b) -> vector3
Projects a onto b.
auto vec3_slide(const vector3 v, const vector3 normal) -> vector3
Removes the component of v along normal.
auto vec3_reject(const vector3 a, const vector3 b) -> vector3
Returns the component of a orthogonal to b.
auto vec3_rotate_axis(const vector3 v, const vector3 axis, const vm_float_t radians) -> vector3
Rotates v around axis by angle radians.
auto vec3_rotate_axis_deg(const vector3 v, const vector3 axis, const vm_float_t degrees) -> vector3
Rotates a vector around an arbitrary axis by a given angle in degrees.
auto vec3_splat(const vm_float_t s) -> vector3
Returns a vector with every component set to s.
auto vec3_from_vec2(const vector2 v, const vm_float_t z) -> vector3
Builds a vector3 from a vector2 and z.
auto vec3_move_toward(const vector3 current, const vector3 target, const vm_float_t max_delta) -> vector3
Moves current toward target by at most max_delta.
auto vec3_limit_length(const vector3 v, const vm_float_t max_len) -> vector3
Clamps the vector length to max_len.
auto vec3_xy(const vector3 v) -> vector2
Returns the x and y components as a 2D vector.
void vec3_orthonormal_basis(const vector3 n, vector3* t, vector3* b)
Builds a tangent and bitangent orthonormal to n.
auto vec3_length_squared(const vector3 v) -> vm_float_t
Returns the squared Euclidean length.
auto vec3_length_manhattan(const vector3 v) -> vm_float_t
Returns the Manhattan (L1) length.
auto vec3_length_chebyshev(const vector3 v) -> vm_float_t
Returns the Chebyshev (L-inf) length.
auto vec3_distance_squared(const vector3 a, const vector3 b) -> vm_float_t
Returns the squared Euclidean distance between a and b.
auto vec3_signed_angle(const vector3 a, const vector3 b, const vector3 axis) -> vm_float_t
Returns the signed angle from a to b around axis.
auto vec3_min_component(const vector3 v) -> vm_float_t
Returns the smallest component.
auto vec3_max_component(const vector3 v) -> vm_float_t
Returns the largest component.
auto vec3_sum(const vector3 v) -> vm_float_t
Returns the sum of all components.
auto vec3_is_zero(const vector3 v) -> bool
Returns true if every component is zero.
auto vec3_is_normalized(const vector3 v) -> bool
Returns true if the vector has unit length.
auto vec3_near(const vector3 a, const vector3 b, const vm_float_t eps) -> bool
Returns true if a and b are within eps of each other.

Function documentation

vector3 vec3_zero(void)

Returns a zero-initialized vector3.

Returns A vector3 with all components set to 0.0f.

vector3 vec3_one(void)

Returns a vector3 with all components set to 1.0f.

Returns A vector3 with all components set to 1.0f.

vector3 vec3_x_axis(const vm_float_t x)

Returns a vector3 along the x-axis.

Parameters
x The x component value.
Returns A vector3 with (x, 0.0f, 0.0f).

vector3 vec3_y_axis(const vm_float_t y)

Returns a vector3 along the y-axis.

Parameters
y The y component value.
Returns A vector3 with (0.0f, y, 0.0f).

vector3 vec3_z_axis(const vm_float_t z)

Returns a vector3 along the z-axis.

Parameters
z The z component value.
Returns A vector3 with (0.0f, 0.0f, z).

vector3 vec3_x_scale(const vm_float_t x)

Returns a vector3 for scaling along the x-axis.

Parameters
x The x scale factor.
Returns A vector3 with (x, 1.0f, 1.0f).

vector3 vec3_y_scale(const vm_float_t y)

Returns a vector3 for scaling along the y-axis.

Parameters
y The y scale factor.
Returns A vector3 with (1.0f, y, 1.0f).

vector3 vec3_z_scale(const vm_float_t z)

Returns a vector3 for scaling along the z-axis.

Parameters
z The z scale factor.
Returns A vector3 with (1.0f, 1.0f, z).

vector3 vec3_add(const vector3 a, const vector3 b)

Component-wise addition of two vectors.

Parameters
a First vector.
b Second vector.
Returns Result vector.

vector3 vec3_sub(const vector3 a, const vector3 b)

Component-wise subtraction of two vectors.

Parameters
a First vector.
b Second vector.
Returns Result vector.

vector3 vec3_mul_scalar(const vector3 v, const vm_float_t s)

Component-wise multiplication of vector by scalar.

Parameters
v The vector.
s The scalar.
Returns Result vector.

vector3 vec3_div_scalar(const vector3 v, const vm_float_t s)

Component-wise division of vector by scalar.

Parameters
v The vector.
s The scalar (non-zero).
Returns Result vector.

vector3 vec3_mul(const vector3 a, const vector3 b)

Component-wise multiplication of two vectors.

Parameters
a First vector.
b Second vector.
Returns Result vector.

vector3 vec3_neg(const vector3 v)

Negation of a vector.

Parameters
v The vector.
Returns The negated vector.

vector3 vec3_abs(const vector3 v)

Computes the absolute value per component of a vector3.

Parameters
v The vector.
Returns The absolute vector3.

vector3 vec3_cross(const vector3 a, const vector3 b)

Computes the cross-product of two vector3.

Parameters
a First vector.
b Second vector.
Returns The cross-product (a × b).

vector3 vec3_normalize(const vector3 v)

Normalizes a vector3 to unit length.

Parameters
v The vector (non-zero).
Returns The normalized vector.

vector3 vec3_min(const vector3 a, const vector3 b)

Computes the component-wise minimum of two vector3.

Parameters
a First vector.
b Second vector.
Returns Per-component min(a, b).

vector3 vec3_max(const vector3 a, const vector3 b)

Computes the component-wise maximum of two vector3.

Parameters
a First vector.
b Second vector.
Returns Per-component max(a, b).

vector3 vec3_sign(const vector3 v)

Computes the sign per component of a vector3 (-1, 0, or 1).

Parameters
v The vector.
Returns The sign vector.

vector3 vec3_floor(const vector3 v)

Applies the floor per component to a vector3.

Parameters
v The vector.
Returns The floored vector.

vector3 vec3_ceil(const vector3 v)

Applies ceil per component to a vector3.

Parameters
v The vector.
Returns The ceiling vector.

vector3 vec3_round(const vector3 v)

Applies round per component to a vector3.

Parameters
v The vector.
Returns The rounded vector.

vector3 vec3_reflect(const vector3 incident, const vector3 normal)

Reflects an incident vector over normal.

Parameters
incident The incident vector.
normal The surface normal.
Returns The reflected vector.

vector3 vec3_refract(const vector3 incident, const vector3 normal, const vm_float_t eta)

Refracts an incident vector across an interface with a given normal and ratio of refraction eta.

Parameters
incident The incident vector.
normal The surface normal.
eta The ratio of refraction (eta = n1 / n2).
Returns The refracted vector, or incident if total internal reflection.

vector3 vec3_lerp(const vector3 a, const vector3 b, const vm_float_t t)

Linearly interpolates between two vector3.

Parameters
a Start vector.
b End vector.
t Interpolation factor [0, 1].
Returns The interpolated vector (a * (1 - t) + b * t).

vector3 vec3_clamp(const vector3 v, const vector3 min, const vector3 max)

Clamps a vector3 between min and max per component.

Parameters
v The vector.
min Minimum bounds.
max Maximum bounds.
Returns The clamped vector.

vm_float_t vec3_distance(const vector3 a, const vector3 b)

Computes the Euclidean distance between two vector3.

Parameters
a First vector.
b Second vector.
Returns The distance between a and b.

vm_float_t vec3_angle(const vector3 a, const vector3 b)

Computes the angle between two non-zero vector3 in radians.

Parameters
a First vector.
b Second vector.
Returns The angle between the directions of a and b.

vm_float_t vec3_dot(const vector3 a, const vector3 b)

Computes the dot product of two vector3.

Parameters
a First vector.
b Second vector.
Returns The dot product (a.x * b.x + a.y * b.y + a.z * b.z).

vm_float_t vec3_length(const vector3 v)

Computes the length (magnitude) of a vector3.

Parameters
v The vector.
Returns The Euclidean length of v.

vector3 vec3_scale(const vector3 v, const vm_float_t s)

Scales the vector by a scalar.

Parameters
v Input vector.
s Scalar value.
Returns The resulting vector3.

vector3 vec3_div(const vector3 a, const vector3 b)

Divides two vectors component-wise.

Parameters
a First input vector.
b Second input vector.
Returns The resulting vector3.

vector3 vec3_add_scalar(const vector3 v, const vm_float_t s)

Adds a scalar to each component.

Parameters
v Input vector.
s Scalar value.
Returns The resulting vector3.

vector3 vec3_sub_scalar(const vector3 v, const vm_float_t s)

Subtracts a scalar from each component.

Parameters
v Input vector.
s Scalar value.
Returns The resulting vector3.

vector3 vec3_clamp_scalar(const vector3 v, const vm_float_t min, const vm_float_t max)

Clamps each component to the scalar range [min, max].

Parameters
v Input vector.
min Lower bound.
max Upper bound.
Returns The resulting vector3.

vector3 vec3_saturate(const vector3 v)

Clamps each component to the range [0, 1].

Parameters
v Input vector.
Returns The resulting vector3.

vector3 vec3_fract(const vector3 v)

Returns the fractional part of each component.

Parameters
v Input vector.
Returns The resulting vector3.

vector3 vec3_project(const vector3 a, const vector3 b)

Projects a onto b.

Parameters
a First input vector.
b Second input vector.
Returns The resulting vector3.

vector3 vec3_slide(const vector3 v, const vector3 normal)

Removes the component of v along normal.

Parameters
v Input vector.
normal Surface normal.
Returns The resulting vector3.

vector3 vec3_reject(const vector3 a, const vector3 b)

Returns the component of a orthogonal to b.

Parameters
a First input vector.
b Second input vector.
Returns The resulting vector3.

vector3 vec3_rotate_axis(const vector3 v, const vector3 axis, const vm_float_t radians)

Rotates v around axis by angle radians.

Parameters
v Input vector.
axis Rotation axis.
radians Angle in radians.
Returns The resulting vector3.

vector3 vec3_rotate_axis_deg(const vector3 v, const vector3 axis, const vm_float_t degrees)

Rotates a vector around an arbitrary axis by a given angle in degrees.

Parameters
v The vector to rotate.
axis The axis of rotation. The axis vector is assumed to be normalized.
degrees The rotation angle in degrees.
Returns The rotated vector.

This is a convenience wrapper that converts degrees to radians and delegates to vec3_rotate_axis.

vector3 vec3_splat(const vm_float_t s)

Returns a vector with every component set to s.

Parameters
s Scalar value.
Returns The resulting vector3.

vector3 vec3_from_vec2(const vector2 v, const vm_float_t z)

Builds a vector3 from a vector2 and z.

Parameters
v Input vector.
z Z component.
Returns The resulting vector3.

vector3 vec3_move_toward(const vector3 current, const vector3 target, const vm_float_t max_delta)

Moves current toward target by at most max_delta.

Parameters
current Current position.
target Target position.
max_delta Maximum distance to move.
Returns The resulting vector3.

vector3 vec3_limit_length(const vector3 v, const vm_float_t max_len)

Clamps the vector length to max_len.

Parameters
v Input vector.
max_len Maximum length.
Returns The resulting vector3.

vector2 vec3_xy(const vector3 v)

Returns the x and y components as a 2D vector.

Parameters
v Input vector.
Returns The resulting vector2.

void vec3_orthonormal_basis(const vector3 n, vector3* t, vector3* b)

Builds a tangent and bitangent orthonormal to n.

Parameters
n Unit normal.
t Output tangent.
b Output bitangent.

vm_float_t vec3_length_squared(const vector3 v)

Returns the squared Euclidean length.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec3_length_manhattan(const vector3 v)

Returns the Manhattan (L1) length.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec3_length_chebyshev(const vector3 v)

Returns the Chebyshev (L-inf) length.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec3_distance_squared(const vector3 a, const vector3 b)

Returns the squared Euclidean distance between a and b.

Parameters
a First input vector.
b Second input vector.
Returns The resulting scalar.

vm_float_t vec3_signed_angle(const vector3 a, const vector3 b, const vector3 axis)

Returns the signed angle from a to b around axis.

Parameters
a First input vector.
b Second input vector.
axis Rotation axis.
Returns The resulting scalar.

vm_float_t vec3_min_component(const vector3 v)

Returns the smallest component.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec3_max_component(const vector3 v)

Returns the largest component.

Parameters
v Input vector.
Returns The resulting scalar.

vm_float_t vec3_sum(const vector3 v)

Returns the sum of all components.

Parameters
v Input vector.
Returns The resulting scalar.

bool vec3_is_zero(const vector3 v)

Returns true if every component is zero.

Parameters
v Input vector.
Returns True if every component is zero.

bool vec3_is_normalized(const vector3 v)

Returns true if the vector has unit length.

Parameters
v Input vector.
Returns True if the vector has unit length.

bool vec3_near(const vector3 a, const vector3 b, const vm_float_t eps)

Returns true if a and b are within eps of each other.

Parameters
a First input vector.
b Second input vector.
eps Distance tolerance.
Returns True if a and b are within eps.