Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector3.c
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025-2026 Igal Alkon
2// SPDX-FileCopyrightText: 2026 ALKONTEK <git@alkontek.com>
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <vecmat.h>
6
13{
14 return (vector3){.x = 0.0f, .y = 0.0f, .z = 0.0f};
15}
16
23{
24 return (vector3){.x = 1.0f, .y = 1.0f, .z = 1.0f};
25}
26
34{
35 return (vector3){.x = x, .y = 0.0f, .z = 0.0f};
36}
37
45{
46 return (vector3){.x = 0.0f, .y = y, .z = 0.0f};
47}
48
56{
57 return (vector3){.x = 0.0f, .y = 0.0f, .z = z};
58}
59
67{
68 return (vector3){.x = x, .y = 1.0f, .z = 1.0f};
69}
70
78{
79 return (vector3){.x = 1.0f, .y = y, .z = 1.0f};
80}
81
89{
90 return (vector3){.x = 1.0f, .y = 1.0f, .z = z};
91}
92
103{
104 vector3 res;
105 vec3_add_ptr(&res, &a, &b);
106 return res;
107}
108
119{
120 vector3 res;
121 vec3_sub_ptr(&res, &a, &b);
122 return res;
123}
124
135{
136 vector3 res;
137 vec3_mul_scalar_ptr(&res, &v, s);
138 return res;
139}
140
151{
152 vector3 res;
153 vec3_div_scalar_ptr(&res, &v, s);
154 return res;
155}
156
167{
168 vector3 res;
169 vec3_mul_ptr(&res, &a, &b);
170 return res;
171}
172
182{
183 vector3 res;
184 vec3_neg_ptr(&res, &v);
185 return res;
186}
187
197{
198 vector3 res;
199 vec3_abs_ptr(&res, &v);
200 return res;
201}
202
213{
214 vector3 res;
215 vec3_cross_ptr(&res, &a, &b);
216 return res;
217}
218
228{
229 vector3 res;
230 vec3_normalize_ptr(&res, &v);
231 return res;
232}
233
244{
245 vector3 res;
246 vec3_min_ptr(&res, &a, &b);
247 return res;
248}
249
260{
261 vector3 res;
262 vec3_max_ptr(&res, &a, &b);
263 return res;
264}
265
275{
276 vector3 res;
277 vec3_sign_ptr(&res, &v);
278 return res;
279}
280
290{
291 vector3 res;
292 vec3_floor_ptr(&res, &v);
293 return res;
294}
295
305{
306 vector3 res;
307 vec3_ceil_ptr(&res, &v);
308 return res;
309}
310
320{
321 vector3 res;
322 vec3_round_ptr(&res, &v);
323 return res;
324}
325
335vector3 vec3_reflect(const vector3 incident, const vector3 normal)
336{
337 vector3 res;
338 vec3_reflect_ptr(&res, &incident, &normal);
339 return res;
340}
341
353vector3 vec3_refract(const vector3 incident, const vector3 normal, const vm_float_t eta)
354{
355 vector3 res;
356 vec3_refract_ptr(&res, &incident, &normal, eta);
357 return res;
358}
359
370vector3 vec3_lerp(const vector3 a, const vector3 b, const vm_float_t t)
371{
372 vector3 res;
373 vec3_lerp_ptr(&res, &a, &b, t);
374 return res;
375}
376
387vector3 vec3_clamp(const vector3 v, const vector3 min, const vector3 max)
388{
389 vector3 res;
390 vec3_clamp_ptr(&res, &v, &min, &max);
391 return res;
392}
393
402{
403 return vec3_length(vec3_sub(a, b));
404}
405
414{
415 const vm_float_t dot = vec3_dot(a, b);
416 const vm_float_t len_a = vec3_length(a);
417 const vm_float_t len_b = vec3_length(b);
418 return VECMAT_ACOS(dot / (len_a * len_b));
419}
420
429{
430 return a.x * b.x + a.y * b.y + a.z * b.z;
431}
432
440{
441 return VECMAT_SQRT(vec3_dot(v, v));
442}
443
454{
455 vector3 res;
456 vec3_scale_ptr(&res, &v, s);
457 return res;
458}
459
470{
471 vector3 res;
472 vec3_div_ptr(&res, &a, &b);
473 return res;
474}
475
486{
487 vector3 res;
488 vec3_add_scalar_ptr(&res, &v, s);
489 return res;
490}
491
502{
503 vector3 res;
504 vec3_sub_scalar_ptr(&res, &v, s);
505 return res;
506}
507
519{
520 vector3 res;
521 vec3_clamp_scalar_ptr(&res, &v, min, max);
522 return res;
523}
524
534{
535 vector3 res;
536 vec3_saturate_ptr(&res, &v);
537 return res;
538}
539
549{
550 vector3 res;
551 vec3_fract_ptr(&res, &v);
552 return res;
553}
554
565{
566 vector3 res;
567 vec3_project_ptr(&res, &a, &b);
568 return res;
569}
570
580vector3 vec3_slide(const vector3 v, const vector3 normal)
581{
582 vector3 res;
583 vec3_slide_ptr(&res, &v, &normal);
584 return res;
585}
586
597{
598 vector3 res;
599 vec3_reject_ptr(&res, &a, &b);
600 return res;
601}
602
613vector3 vec3_rotate_axis(const vector3 v, const vector3 axis, const vm_float_t angle)
614{
615 vector3 res;
616 vec3_rotate_axis_ptr(&res, &v, &axis, angle);
617 return res;
618}
619
629{
630 return (vector3){.x = s, .y = s, .z = s};
631}
632
643{
644 vector3 res;
645 vec3_from_vec2_ptr(&res, &v, z);
646 return res;
647}
648
659vector3 vec3_move_toward(const vector3 current, const vector3 target, const vm_float_t max_delta)
660{
661 vector3 res;
662 vec3_move_toward_ptr(&res, &current, &target, max_delta);
663 return res;
664}
665
676{
677 vector3 res;
678 vec3_limit_length_ptr(&res, &v, max_len);
679 return res;
680}
681
691{
692 vector2 res;
693 vec3_xy_ptr(&res, &v);
694 return res;
695}
696
705{
707}
708
716{
717 return vec3_dot(v, v);
718}
719
727{
728 return VECMAT_FABS(v.x) + VECMAT_FABS(v.y) + VECMAT_FABS(v.z);
729}
730
738{
739 return VECMAT_FMAX(VECMAT_FABS(v.x), fmaxf(VECMAT_FABS(v.y), VECMAT_FABS(v.z)));
740}
741
750{
751 const vm_float_t dx = a.x - b.x;
752 const vm_float_t dy = a.y - b.y;
753 const vm_float_t dz = a.z - b.z;
754 return dx * dx + dy * dy + dz * dz;
755}
756
766{
767 const vector3 c = vec3_cross(a, b);
768 return VECMAT_ATAN2(vec3_dot(axis, c), vec3_dot(a, b));
769}
770
778{
779 return VECMAT_FMIN(v.x, VECMAT_FMIN(v.y, v.z));
780}
781
789{
790 return VECMAT_FMAX(v.x, VECMAT_FMAX(v.y, v.z));
791}
792
800{
801 return v.x + v.y + v.z;
802}
803
811{
812 return VECMAT_FABS(v.x) < VECMAT_EPSILON
815}
816
824{
826}
827
836bool vec3_near(const vector3 a, const vector3 b, const vm_float_t eps)
837{
838 return VECMAT_FABS(a.x - b.x) < eps
839 && VECMAT_FABS(a.y - b.y) < eps
840 && VECMAT_FABS(a.z - b.z) < eps;
841}
vm_float_t z
Definition vecmat.h:139
vm_float_t y
Definition vecmat.h:138
vm_float_t x
Definition vecmat.h:137
void vec3_add_scalar_ptr(vector3 *res, const vector3 *v, vm_float_t s)
Adds a scalar to each component.
#define VECMAT_FMAX(a, b)
Definition vecmat.h:423
#define VECMAT_EPSILON
Definition vecmat.h:377
void vec3_mul_ptr(vector3 *res, const vector3 *a, const vector3 *b)
Component-wise multiplication of two vectors.
Definition vector3_ptr.c:74
void vec3_abs_ptr(vector3 *res, const vector3 *v)
Computes the absolute value per component of a vector3.
void vec3_min_ptr(vector3 *res, const vector3 *a, const vector3 *b)
Component-wise minimum of two vectors.
void vec3_sign_ptr(vector3 *res, const vector3 *v)
Component-wise sign of a vector.
void vec3_clamp_ptr(vector3 *res, const vector3 *v, const vector3 *min, const vector3 *max)
Clamp vector components between min and max.
void vec3_scale_ptr(vector3 *res, const vector3 *v, vm_float_t s)
Scales the vector by a scalar.
void vec3_div_ptr(vector3 *res, const vector3 *a, const vector3 *b)
Divides two vectors component-wise.
void vec3_saturate_ptr(vector3 *res, const vector3 *v)
Clamps each component to the range [0, 1].
void vec3_sub_scalar_ptr(vector3 *res, const vector3 *v, vm_float_t s)
Subtracts a scalar from each component.
#define VECMAT_FMIN(a, b)
Definition vecmat.h:422
void vec3_from_vec2_ptr(vector3 *res, const vector2 *v, vm_float_t z)
Builds a higher-dimension vector from a vector2.
void vec3_move_toward_ptr(vector3 *res, const vector3 *current, const vector3 *target, vm_float_t max_delta)
Moves current toward target by at most max_delta.
void vec3_xy_ptr(vector2 *res, const vector3 *v)
Returns the x and y components as a 2D vector.
void vec3_clamp_scalar_ptr(vector3 *res, const vector3 *v, vm_float_t min, vm_float_t max)
Clamps each component to the scalar range [min, max].
void vec3_rotate_axis_ptr(vector3 *res, const vector3 *v, const vector3 *axis, vm_float_t angle)
Rotates v around axis by angle radians.
#define VECMAT_ACOS(x)
Definition vecmat.h:419
void vec3_add_ptr(vector3 *res, const vector3 *a, const vector3 *b)
Component-wise addition of two vectors.
Definition vector3_ptr.c:14
void vec3_sub_ptr(vector3 *res, const vector3 *a, const vector3 *b)
Component-wise subtraction of two vectors.
Definition vector3_ptr.c:28
void vec3_round_ptr(vector3 *res, const vector3 *v)
Component-wise round of a vector.
void vec3_lerp_ptr(vector3 *res, const vector3 *a, const vector3 *b, vm_float_t t)
Linear interpolation between two vectors.
void vec3_max_ptr(vector3 *res, const vector3 *a, const vector3 *b)
Component-wise maximum of two vectors.
void vec3_fract_ptr(vector3 *res, const vector3 *v)
Returns the fractional part of each component.
void vec3_slide_ptr(vector3 *res, const vector3 *v, const vector3 *normal)
Removes the component of v along normal.
void vec3_cross_ptr(vector3 *res, const vector3 *a, const vector3 *b)
Compute cross-product of two vectors.
void vec3_normalize_ptr(vector3 *res, const vector3 *v)
Normalize a vector to unit length.
void vec3_ceil_ptr(vector3 *res, const vector3 *v)
Component-wise ceil of a vector.
double vm_float_t
Definition vecmat.h:79
void vec3_reject_ptr(vector3 *res, const vector3 *a, const vector3 *b)
Returns the component of a orthogonal to b.
#define VECMAT_FABS(x)
Definition vecmat.h:413
#define VECMAT_SQRT(x)
Definition vecmat.h:414
void vec3_orthonormal_basis_ptr(const vector3 *n, vector3 *t, vector3 *b)
Builds a tangent and bitangent orthonormal to n.
void vec3_div_scalar_ptr(vector3 *res, const vector3 *v, vm_float_t s)
Component-wise division of vector by scalar.
Definition vector3_ptr.c:56
void vec3_mul_scalar_ptr(vector3 *res, const vector3 *v, vm_float_t s)
Component-wise multiplication of vector by scalar.
Definition vector3_ptr.c:42
void vec3_project_ptr(vector3 *res, const vector3 *a, const vector3 *b)
Projects a onto b.
void vec3_floor_ptr(vector3 *res, const vector3 *v)
Component-wise floor of a vector.
void vec3_limit_length_ptr(vector3 *res, const vector3 *v, vm_float_t max_len)
Clamps the vector length to max_len.
#define VECMAT_ATAN2(y, x)
Definition vecmat.h:421
void vec3_refract_ptr(vector3 *res, const vector3 *incident, const vector3 *normal, vm_float_t eta)
Compute refraction of the incident vector through normal with eta.
void vec3_neg_ptr(vector3 *res, const vector3 *v)
Negation of a vector.
Definition vector3_ptr.c:87
void vec3_reflect_ptr(vector3 *res, const vector3 *incident, const vector3 *normal)
Compute reflection of the incident vector over normal.
vector3 vec3_slide(const vector3 v, const vector3 normal)
Removes the component of v along normal.
Definition vector3.c:580
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.
Definition vector3.c:836
void vec3_orthonormal_basis(const vector3 n, vector3 *t, vector3 *b)
Builds a tangent and bitangent orthonormal to n.
Definition vector3.c:704
vm_float_t vec3_length(const vector3 v)
Computes the length (magnitude) of a vector3.
Definition vector3.c:439
vector2 vec3_xy(const vector3 v)
Returns the x and y components as a 2D vector.
Definition vector3.c:690
vector3 vec3_div(const vector3 a, const vector3 b)
Divides two vectors component-wise.
Definition vector3.c:469
vector3 vec3_sign(const vector3 v)
Computes the sign per component of a vector3 (-1, 0, or 1).
Definition vector3.c:274
vector3 vec3_floor(const vector3 v)
Applies the floor per component to a vector3.
Definition vector3.c:289
vector3 vec3_reflect(const vector3 incident, const vector3 normal)
Reflects an incident vector over normal.
Definition vector3.c:335
vector3 vec3_splat(const vm_float_t s)
Returns a vector with every component set to s.
Definition vector3.c:628
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].
Definition vector3.c:518
vector3 vec3_max(const vector3 a, const vector3 b)
Computes the component-wise maximum of two vector3.
Definition vector3.c:259
vm_float_t vec3_dot(const vector3 a, const vector3 b)
Computes the dot product of two vector3.
Definition vector3.c:428
vector3 vec3_one(void)
Returns a vector3 with all components set to 1.0f.
Definition vector3.c:22
vector3 vec3_add(const vector3 a, const vector3 b)
Component-wise addition of two vectors.
Definition vector3.c:102
vector3 vec3_y_scale(const vm_float_t y)
Returns a vector3 for scaling along the y-axis.
Definition vector3.c:77
vm_float_t vec3_sum(const vector3 v)
Returns the sum of all components.
Definition vector3.c:799
vector3 vec3_project(const vector3 a, const vector3 b)
Projects a onto b.
Definition vector3.c:564
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.
Definition vector3.c:765
vector3 vec3_x_scale(const vm_float_t x)
Returns a vector3 for scaling along the x-axis.
Definition vector3.c:66
vector3 vec3_mul_scalar(const vector3 v, const vm_float_t s)
Component-wise multiplication of vector by scalar.
Definition vector3.c:134
vector3 vec3_zero(void)
Returns a zero-initialized vector3.
Definition vector3.c:12
vector3 vec3_min(const vector3 a, const vector3 b)
Computes the component-wise minimum of two vector3.
Definition vector3.c:243
vector3 vec3_scale(const vector3 v, const vm_float_t s)
Scales the vector by a scalar.
Definition vector3.c:453
vector3 vec3_add_scalar(const vector3 v, const vm_float_t s)
Adds a scalar to each component.
Definition vector3.c:485
vector3 vec3_y_axis(const vm_float_t y)
Returns a vector3 along the y-axis.
Definition vector3.c:44
vector3 vec3_sub_scalar(const vector3 v, const vm_float_t s)
Subtracts a scalar from each component.
Definition vector3.c:501
vector3 vec3_z_axis(const vm_float_t z)
Returns a vector3 along the z-axis.
Definition vector3.c:55
vm_float_t vec3_min_component(const vector3 v)
Returns the smallest component.
Definition vector3.c:777
vector3 vec3_clamp(const vector3 v, const vector3 min, const vector3 max)
Clamps a vector3 between min and max per component.
Definition vector3.c:387
vector3 vec3_abs(const vector3 v)
Computes the absolute value per component of a vector3.
Definition vector3.c:196
vector3 vec3_mul(const vector3 a, const vector3 b)
Component-wise multiplication of two vectors.
Definition vector3.c:166
bool vec3_is_zero(const vector3 v)
Returns true if every component is zero.
Definition vector3.c:810
vector3 vec3_cross(const vector3 a, const vector3 b)
Computes the cross-product of two vector3.
Definition vector3.c:212
vm_float_t vec3_distance_squared(const vector3 a, const vector3 b)
Returns the squared Euclidean distance between a and b.
Definition vector3.c:749
vector3 vec3_neg(const vector3 v)
Negation of a vector.
Definition vector3.c:181
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.
Definition vector3.c:659
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.
Definition vector3.c:353
vector3 vec3_from_vec2(const vector2 v, const vm_float_t z)
Builds a vector3 from a vector2 and z.
Definition vector3.c:642
vector3 vec3_round(const vector3 v)
Applies round per component to a vector3.
Definition vector3.c:319
vector3 vec3_sub(const vector3 a, const vector3 b)
Component-wise subtraction of two vectors.
Definition vector3.c:118
vm_float_t vec3_length_squared(const vector3 v)
Returns the squared Euclidean length.
Definition vector3.c:715
vector3 vec3_fract(const vector3 v)
Returns the fractional part of each component.
Definition vector3.c:548
vector3 vec3_rotate_axis(const vector3 v, const vector3 axis, const vm_float_t angle)
Rotates v around axis by angle radians.
Definition vector3.c:613
vector3 vec3_limit_length(const vector3 v, const vm_float_t max_len)
Clamps the vector length to max_len.
Definition vector3.c:675
vm_float_t vec3_angle(const vector3 a, const vector3 b)
Computes the angle between two non-zero vector3 in radians.
Definition vector3.c:413
vm_float_t vec3_length_manhattan(const vector3 v)
Returns the Manhattan (L1) length.
Definition vector3.c:726
vm_float_t vec3_distance(const vector3 a, const vector3 b)
Computes the Euclidean distance between two vector3.
Definition vector3.c:401
vector3 vec3_normalize(const vector3 v)
Normalizes a vector3 to unit length.
Definition vector3.c:227
vector3 vec3_x_axis(const vm_float_t x)
Returns a vector3 along the x-axis.
Definition vector3.c:33
vector3 vec3_div_scalar(const vector3 v, const vm_float_t s)
Component-wise division of vector by scalar.
Definition vector3.c:150
vm_float_t vec3_length_chebyshev(const vector3 v)
Returns the Chebyshev (L-inf) length.
Definition vector3.c:737
vector3 vec3_saturate(const vector3 v)
Clamps each component to the range [0, 1].
Definition vector3.c:533
bool vec3_is_normalized(const vector3 v)
Returns true if the vector has unit length.
Definition vector3.c:823
vm_float_t vec3_max_component(const vector3 v)
Returns the largest component.
Definition vector3.c:788
vector3 vec3_z_scale(const vm_float_t z)
Returns a vector3 for scaling along the z-axis.
Definition vector3.c:88
vector3 vec3_reject(const vector3 a, const vector3 b)
Returns the component of a orthogonal to b.
Definition vector3.c:596
vector3 vec3_lerp(const vector3 a, const vector3 b, const vm_float_t t)
Linearly interpolates between two vector3.
Definition vector3.c:370
vector3 vec3_ceil(const vector3 v)
Applies ceil per component to a vector3.
Definition vector3.c:304