Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector4.c File Reference
#include <vecmat.h>

Go to the source code of this file.

Functions

vector4 vec4_zero (void)
 Returns a zero-initialized vector4.
vector4 vec4_one (void)
 Returns a vector4 with all components set to 1.0f.
vector4 vec4_x_axis (const vm_float_t x)
 Returns a vector4 along the x-axis.
vector4 vec4_y_axis (const vm_float_t y)
 Returns a vector4 along the y-axis.
vector4 vec4_z_axis (const vm_float_t z)
 Returns a vector4 along the z-axis.
vector4 vec4_w_axis (const vm_float_t w)
 Returns a vector4 along the w-axis.
vector4 vec4_x_scale (const vm_float_t x)
 Returns avector4 for scaling along the x-axis.
vector4 vec4_y_scale (const vm_float_t y)
 Returns a vector4 for scaling along the y-axis.
vector4 vec4_z_scale (const vm_float_t z)
 Returns a vector4for scaling along the z-axis.
vector4 vec4_w_scale (const vm_float_t w)
 Returns a vector4 for scaling along the w-axis.
vector4 vec4_add (const vector4 a, const vector4 b)
 Component-wise addition of two vectors.
vector4 vec4_sub (const vector4 a, const vector4 b)
 Component-wise subtraction of two vectors.
vector4 vec4_mul_scalar (const vector4 v, const vm_float_t s)
 Component-wise multiplication of vector by scalar.
vector4 vec4_div_scalar (const vector4 v, const vm_float_t s)
 Component-wise division of vector by scalar.
vector4 vec4_mul (const vector4 a, const vector4 b)
 Component-wise multiplication of two vectors.
vector4 vec4_neg (const vector4 v)
 Negation of a vector.
vector4 vec4_abs (const vector4 v)
 Computes the absolute value per component of a vector4.
vector4 vec4_normalize (const vector4 v)
 Normalizes a vector4 to unit length.
vector4 vec4_min (const vector4 a, const vector4 b)
 Computes the component-wise minimum of two vector4.
vector4 vec4_max (const vector4 a, const vector4 b)
 Computes the component-wise maximum of two vector4.
vector4 vec4_sign (const vector4 v)
 Computes the sign per component of a vector4 (-1, 0, or 1).
vector4 vec4_floor (const vector4 v)
 Applies the floor per component to a vector4.
vector4 vec4_ceil (const vector4 v)
 Applies ceil per component to a vector4.
vector4 vec4_round (const vector4 v)
 Applies round per component to a vector4.
vector4 vec4_lerp (const vector4 a, const vector4 b, const vm_float_t t)
 Linearly interpolates between two vector4.
vector4 vec4_clamp (const vector4 v, const vector4 min, const vector4 max)
 Clamps a vector4 between min and max per component.
vector4 vec4_homogenize (const vector4 v)
 Homogenizes a vector4 (divides x, y, z by w).
vm_float_t vec4_dot (const vector4 a, const vector4 b)
 Computes the dot product of two vector4.
vm_float_t vec4_length (const vector4 v)
 Computes the length (magnitude) of a vector4.
vm_float_t vec4_distance (const vector4 a, const vector4 b)
 Computes the Euclidean distance between two vector4.
vector3 vec4_to_vec3 (const vector4 v)
 Converts a vector4 to a vector3 (discards the w component).
vector4 vec4_div (const vector4 a, const vector4 b)
 Divides two vectors component-wise.
vector4 vec4_add_scalar (const vector4 v, const vm_float_t s)
 Adds a scalar to each component.
vector4 vec4_sub_scalar (const vector4 v, const vm_float_t s)
 Subtracts a scalar from each component.
vector4 vec4_clamp_scalar (const vector4 v, const vm_float_t min, const vm_float_t max)
 Clamps each component to the scalar range [min, max].
vector4 vec4_saturate (const vector4 v)
 Clamps each component to the range [0, 1].
vector4 vec4_fract (const vector4 v)
 Returns the fractional part of each component.
vector4 vec4_project (const vector4 a, const vector4 b)
 Projects a onto b.
vector4 vec4_reject (const vector4 a, const vector4 b)
 Returns the component of a orthogonal to b.
vector4 vec4_slide (const vector4 v, const vector4 normal)
 Removes the component of v along normal.
vector4 vec4_splat (const vm_float_t s)
 Returns a vector with every component set to s.
vm_float_t vec4_length_squared (const vector4 v)
 Returns the squared Euclidean length.
vm_float_t vec4_distance_squared (const vector4 a, const vector4 b)
 Returns the squared Euclidean distance between a and b.
bool vec4_is_zero (const vector4 v)
 Returns true if every component is zero.
bool vec4_is_normalized (const vector4 v)
 Returns true if the vector has unit length.
bool vec4_near (const vector4 a, const vector4 b, const vm_float_t eps)
 Returns true if a and b are within eps of each other.

Function Documentation

◆ vec4_abs()

vector4 vec4_abs ( const vector4 v)

Computes the absolute value per component of a vector4.

See vec4_abs_ptr for the out-parameter version.

Parameters
vThe vector.
Returns
The absolute vector4.

Definition at line 218 of file vector4.c.

References vec4_abs_ptr().

◆ vec4_add()

vector4 vec4_add ( const vector4 a,
const vector4 b )

Component-wise addition of two vectors.

See vec4_add_ptr for the out-parameter version.

Parameters
aFirst vector.
bSecond vector.
Returns
Result vector.

Definition at line 124 of file vector4.c.

References vec4_add_ptr().

◆ vec4_add_scalar()

vector4 vec4_add_scalar ( const vector4 v,
const vm_float_t s )

Adds a scalar to each component.

See vec4_add_scalar_ptr instead.

Parameters
vInput vector.
sScalar value.
Returns
The resulting vector4.

Definition at line 455 of file vector4.c.

References vec4_add_scalar_ptr().

◆ vec4_ceil()

vector4 vec4_ceil ( const vector4 v)

Applies ceil per component to a vector4.

See vec4_ceil_ptr for the out-parameter version.

Parameters
vThe vector.
Returns
The ceiling vector.

Definition at line 310 of file vector4.c.

References vec4_ceil_ptr().

◆ vec4_clamp()

vector4 vec4_clamp ( const vector4 v,
const vector4 min,
const vector4 max )

Clamps a vector4 between min and max per component.

See vec4_clamp_ptr for the out-parameter version.

Parameters
vThe vector.
minMinimum bounds.
maxMaximum bounds.
Returns
The clamped vector.

Definition at line 359 of file vector4.c.

References vec4_clamp_ptr().

◆ vec4_clamp_scalar()

vector4 vec4_clamp_scalar ( const vector4 v,
const vm_float_t min,
const vm_float_t max )

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

See vec4_clamp_scalar_ptr instead.

Parameters
vInput vector.
minLower bound.
maxUpper bound.
Returns
The resulting vector4.

Definition at line 488 of file vector4.c.

References vec4_clamp_scalar_ptr().

◆ vec4_distance()

vm_float_t vec4_distance ( const vector4 a,
const vector4 b )

Computes the Euclidean distance between two vector4.

Parameters
aFirst vector.
bSecond vector.
Returns
The distance between a and b.

Definition at line 411 of file vector4.c.

References vec4_length(), and vec4_sub().

◆ vec4_distance_squared()

vm_float_t vec4_distance_squared ( const vector4 a,
const vector4 b )

Returns the squared Euclidean distance between a and b.

Parameters
aFirst input vector.
bSecond input vector.
Returns
The resulting scalar.

Definition at line 604 of file vector4.c.

References vec4_length_squared(), and vec4_sub().

◆ vec4_div()

vector4 vec4_div ( const vector4 a,
const vector4 b )

Divides two vectors component-wise.

See vec4_div_ptr instead.

Parameters
aFirst input vector.
bSecond input vector.
Returns
The resulting vector4.

Definition at line 439 of file vector4.c.

References vec4_div_ptr().

◆ vec4_div_scalar()

vector4 vec4_div_scalar ( const vector4 v,
const vm_float_t s )

Component-wise division of vector by scalar.

See vec4_div_scalar_ptr for the out-parameter version.

Parameters
vThe vector.
sThe scalar (non-zero).
Returns
Result vector.

Definition at line 172 of file vector4.c.

References vec4_div_scalar_ptr().

◆ vec4_dot()

vm_float_t vec4_dot ( const vector4 a,
const vector4 b )

Computes the dot product of two vector4.

Parameters
aFirst vector.
bSecond vector.
Returns
The dot product (a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w).

Definition at line 388 of file vector4.c.

References vector4::w, vector4::x, vector4::y, and vector4::z.

Referenced by vec4_length(), vec4_length_squared(), vec4_project_ptr(), and vec4_slide_ptr().

◆ vec4_floor()

vector4 vec4_floor ( const vector4 v)

Applies the floor per component to a vector4.

See vec4_floor_ptr for the out-parameter version.

Parameters
vThe vector.
Returns
The floored vector.

Definition at line 295 of file vector4.c.

References vec4_floor_ptr().

◆ vec4_fract()

vector4 vec4_fract ( const vector4 v)

Returns the fractional part of each component.

See vec4_fract_ptr instead.

Parameters
vInput vector.
Returns
The resulting vector4.

Definition at line 518 of file vector4.c.

References vec4_fract_ptr().

◆ vec4_homogenize()

vector4 vec4_homogenize ( const vector4 v)

Homogenizes a vector4 (divides x, y, z by w).

See vec4_homogenize_ptr for the out-parameter version.

Parameters
vThe homogeneous vector (w != 0.0f).
Returns
The normalized cartesian vector4.

Definition at line 374 of file vector4.c.

References vec4_homogenize_ptr().

◆ vec4_is_normalized()

bool vec4_is_normalized ( const vector4 v)

Returns true if the vector has unit length.

Parameters
vInput vector.
Returns
True if the vector has unit length.

Definition at line 626 of file vector4.c.

References vec4_length_squared(), VECMAT_EPSILON, and VECMAT_FABS.

◆ vec4_is_zero()

bool vec4_is_zero ( const vector4 v)

Returns true if every component is zero.

Parameters
vInput vector.
Returns
True if every component is zero.

Definition at line 615 of file vector4.c.

References vec4_length_squared(), and VECMAT_EPSILON.

◆ vec4_length()

vm_float_t vec4_length ( const vector4 v)

Computes the length (magnitude) of a vector4.

Parameters
vThe vector.
Returns
The Euclidean length of v.

Definition at line 399 of file vector4.c.

References vec4_dot(), and VECMAT_SQRT.

Referenced by vec4_distance(), and vec4_normalize_ptr_scalar().

◆ vec4_length_squared()

vm_float_t vec4_length_squared ( const vector4 v)

Returns the squared Euclidean length.

Parameters
vInput vector.
Returns
The resulting scalar.

Definition at line 592 of file vector4.c.

References vec4_dot().

Referenced by vec4_distance_squared(), vec4_is_normalized(), and vec4_is_zero().

◆ vec4_lerp()

vector4 vec4_lerp ( const vector4 a,
const vector4 b,
const vm_float_t t )

Linearly interpolates between two vector4.

See vec4_lerp_ptr for the out-parameter version.

Parameters
aStart vector.
bEnd vector.
tInterpolation factor [0, 1].
Returns
The interpolated vector (a * (1 - t) + b * t).

Definition at line 342 of file vector4.c.

References vec4_lerp_ptr().

◆ vec4_max()

vector4 vec4_max ( const vector4 a,
const vector4 b )

Computes the component-wise maximum of two vector4.

See vec4_max_ptr for the out-parameter version.

Parameters
aFirst vector.
bSecond vector.
Returns
Per-component max(a, b).

Definition at line 265 of file vector4.c.

References vec4_max_ptr().

◆ vec4_min()

vector4 vec4_min ( const vector4 a,
const vector4 b )

Computes the component-wise minimum of two vector4.

See vec4_min_ptr for the out-parameter version.

Parameters
aFirst vector.
bSecond vector.
Returns
Per-component min(a, b).

Definition at line 249 of file vector4.c.

References vec4_min_ptr().

◆ vec4_mul()

vector4 vec4_mul ( const vector4 a,
const vector4 b )

Component-wise multiplication of two vectors.

See vec4_mul_ptr for the out-parameter version.

Parameters
aFirst vector.
bSecond vector.
Returns
Result vector.

Definition at line 188 of file vector4.c.

References vec4_mul_ptr().

◆ vec4_mul_scalar()

vector4 vec4_mul_scalar ( const vector4 v,
const vm_float_t s )

Component-wise multiplication of vector by scalar.

See vec4_mul_scalar_ptr for the out-parameter version.

Parameters
vThe vector.
sThe scalar.
Returns
Result vector.

Definition at line 156 of file vector4.c.

References vec4_mul_scalar_ptr().

◆ vec4_near()

bool vec4_near ( const vector4 a,
const vector4 b,
const vm_float_t eps )

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

Parameters
aFirst input vector.
bSecond input vector.
epsDistance tolerance.
Returns
True if a and b are within eps.

Definition at line 639 of file vector4.c.

References VECMAT_FABS, vector4::w, vector4::x, vector4::y, and vector4::z.

◆ vec4_neg()

vector4 vec4_neg ( const vector4 v)

Negation of a vector.

See vec4_neg_ptr for the out-parameter version.

Parameters
vThe vector.
Returns
The negated vector.

Definition at line 203 of file vector4.c.

References vec4_neg_ptr().

◆ vec4_normalize()

vector4 vec4_normalize ( const vector4 v)

Normalizes a vector4 to unit length.

See vec4_normalize_ptr for the out-parameter version.

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

Definition at line 233 of file vector4.c.

References vec4_normalize_ptr().

◆ vec4_one()

vector4 vec4_one ( void )

Returns a vector4 with all components set to 1.0f.

Returns
A vector4 with all components set to 1.0f.

Definition at line 22 of file vector4.c.

◆ vec4_project()

vector4 vec4_project ( const vector4 a,
const vector4 b )

Projects a onto b.

See vec4_project_ptr instead.

Parameters
aFirst input vector.
bSecond input vector.
Returns
The resulting vector4.

Definition at line 534 of file vector4.c.

References vec4_project_ptr().

◆ vec4_reject()

vector4 vec4_reject ( const vector4 a,
const vector4 b )

Returns the component of a orthogonal to b.

See vec4_reject_ptr instead.

Parameters
aFirst input vector.
bSecond input vector.
Returns
The resulting vector4.

Definition at line 550 of file vector4.c.

References vec4_reject_ptr().

◆ vec4_round()

vector4 vec4_round ( const vector4 v)

Applies round per component to a vector4.

See vec4_round_ptr for the out-parameter version.

Parameters
vThe vector.
Returns
The rounded vector.

Definition at line 325 of file vector4.c.

References vec4_round_ptr().

◆ vec4_saturate()

vector4 vec4_saturate ( const vector4 v)

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

See vec4_saturate_ptr instead.

Parameters
vInput vector.
Returns
The resulting vector4.

Definition at line 503 of file vector4.c.

References vec4_saturate_ptr().

◆ vec4_sign()

vector4 vec4_sign ( const vector4 v)

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

See vec4_sign_ptr for the out-parameter version.

Parameters
vThe vector.
Returns
The sign vector.

Definition at line 280 of file vector4.c.

References vec4_sign_ptr().

◆ vec4_slide()

vector4 vec4_slide ( const vector4 v,
const vector4 normal )

Removes the component of v along normal.

See vec4_slide_ptr instead.

Parameters
vInput vector.
normalSurface normal.
Returns
The resulting vector4.

Definition at line 566 of file vector4.c.

References vec4_slide_ptr().

◆ vec4_splat()

vector4 vec4_splat ( const vm_float_t s)

Returns a vector with every component set to s.

See vec4_splat_ptr instead.

Parameters
sScalar value.
Returns
The resulting vector4.

Definition at line 581 of file vector4.c.

◆ vec4_sub()

vector4 vec4_sub ( const vector4 a,
const vector4 b )

Component-wise subtraction of two vectors.

See vec4_sub_ptr for the out-parameter version.

Parameters
aFirst vector.
bSecond vector.
Returns
Result vector.

Definition at line 140 of file vector4.c.

References vec4_sub_ptr().

Referenced by vec4_distance(), and vec4_distance_squared().

◆ vec4_sub_scalar()

vector4 vec4_sub_scalar ( const vector4 v,
const vm_float_t s )

Subtracts a scalar from each component.

See vec4_sub_scalar_ptr instead.

Parameters
vInput vector.
sScalar value.
Returns
The resulting vector4.

Definition at line 471 of file vector4.c.

References vec4_sub_scalar_ptr().

◆ vec4_to_vec3()

vector3 vec4_to_vec3 ( const vector4 v)

Converts a vector4 to a vector3 (discards the w component).

Parameters
vThe source vector4.
Returns
A vector3 {v.x, v.y, v.z}.

Definition at line 423 of file vector4.c.

References vec4_to_vec3_ptr().

◆ vec4_w_axis()

vector4 vec4_w_axis ( const vm_float_t w)

Returns a vector4 along the w-axis.

Parameters
wThe w component value.
Returns
A vector4 with (0.0f, 0.0f, 0.0f, w).

Definition at line 66 of file vector4.c.

◆ vec4_w_scale()

vector4 vec4_w_scale ( const vm_float_t w)

Returns a vector4 for scaling along the w-axis.

Parameters
wThe w scale factor.
Returns
A vector4 with (1.0f, 1.0f, 1.0f, w).

Definition at line 110 of file vector4.c.

◆ vec4_x_axis()

vector4 vec4_x_axis ( const vm_float_t x)

Returns a vector4 along the x-axis.

Parameters
xThe x component value.
Returns
A vector4 with (x, 0.0f, 0.0f, 0.0f).

Definition at line 33 of file vector4.c.

◆ vec4_x_scale()

vector4 vec4_x_scale ( const vm_float_t x)

Returns avector4 for scaling along the x-axis.

Parameters
xThe x scale factor.
Returns
A vector4 with (x, 1.0f, 1.0f, 1.0f).

Definition at line 77 of file vector4.c.

◆ vec4_y_axis()

vector4 vec4_y_axis ( const vm_float_t y)

Returns a vector4 along the y-axis.

Parameters
yThe y component value.
Returns
A vector4 with (0.0f, y, 0.0f, 0.0f).

Definition at line 44 of file vector4.c.

◆ vec4_y_scale()

vector4 vec4_y_scale ( const vm_float_t y)

Returns a vector4 for scaling along the y-axis.

Parameters
yThe y scale factor.
Returns
A vector4 with (1.0f, y, 1.0f, 1.0f).

Definition at line 88 of file vector4.c.

◆ vec4_z_axis()

vector4 vec4_z_axis ( const vm_float_t z)

Returns a vector4 along the z-axis.

Parameters
zThe z component value.
Returns
A vector4 with (0.0f, 0.0f, z, 0.0f).

Definition at line 55 of file vector4.c.

◆ vec4_z_scale()

vector4 vec4_z_scale ( const vm_float_t z)

Returns a vector4for scaling along the z-axis.

Parameters
zThe z scale factor.
Returns
A vector4 with (1.0f, 1.0f, z, 1.0f).

Definition at line 99 of file vector4.c.

◆ vec4_zero()

vector4 vec4_zero ( void )

Returns a zero-initialized vector4.

Returns
A vector4 with all components set to 0.0f.

Definition at line 12 of file vector4.c.