Vecmat 0.2.3
C math and linear algebra library for 2D/3D graphics, physics, and science.
Loading...
Searching...
No Matches
vector3i_ptr.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#include <stdlib.h>
7
15void vec3i_add_ptr(vector3i *res, const vector3i *a, const vector3i *b)
16{
17 res->x = a->x + b->x;
18 res->y = a->y + b->y;
19 res->z = a->z + b->z;
20}
21
29void vec3i_sub_ptr(vector3i *res, const vector3i *a, const vector3i *b)
30{
31 res->x = a->x - b->x;
32 res->y = a->y - b->y;
33 res->z = a->z - b->z;
34}
35
43void vec3i_mul_scalar_ptr(vector3i *res, const vector3i *v, const vm_int_t s)
44{
45 res->x = v->x * s;
46 res->y = v->y * s;
47 res->z = v->z * s;
48}
49
59void vec3i_div_scalar_ptr(vector3i *res, const vector3i *v, const vm_int_t s)
60{
61 if (s == 0) {
62 res->x = 0;
63 res->y = 0;
64 res->z = 0;
65 return;
66 }
67 res->x = v->x / s;
68 res->y = v->y / s;
69 res->z = v->z / s;
70}
71
79void vec3i_mul_ptr(vector3i *res, const vector3i *a, const vector3i *b)
80{
81 res->x = a->x * b->x;
82 res->y = a->y * b->y;
83 res->z = a->z * b->z;
84}
85
92void vec3i_neg_ptr(vector3i *res, const vector3i *v)
93{
94 res->x = -v->x;
95 res->y = -v->y;
96 res->z = -v->z;
97}
98
105void vec3i_abs_ptr(vector3i *res, const vector3i *v)
106{
107 res->x = abs(v->x);
108 res->y = abs(v->y);
109 res->z = abs(v->z);
110}
111
121{
122 const vm_float_t len = vec3i_length(*v);
123 if (len == 0.0f) {
124 *res = *v;
125 return;
126 }
127
128 res->x = (vm_int_t)(v->x / len);
129 res->y = (vm_int_t)(v->y / len);
130 res->z = (vm_int_t)(v->z / len);
131}
132
140void vec3i_cross_ptr(vector3i *res, const vector3i *a, const vector3i *b)
141{
142 res->x = a->y * b->z - a->z * b->y;
143 res->y = a->z * b->x - a->x * b->z;
144 res->z = a->x * b->y - a->y * b->x;
145}
146
154void vec3i_min_ptr(vector3i *res, const vector3i *a, const vector3i *b)
155{
156 res->x = a->x < b->x ? a->x : b->x;
157 res->y = a->y < b->y ? a->y : b->y;
158 res->z = a->z < b->z ? a->z : b->z;
159}
160
168void vec3i_max_ptr(vector3i *res, const vector3i *a, const vector3i *b)
169{
170 res->x = a->x > b->x ? a->x : b->x;
171 res->y = a->y > b->y ? a->y : b->y;
172 res->z = a->z > b->z ? a->z : b->z;
173}
174
181void vec3i_sign_ptr(vector3i *res, const vector3i *v)
182{
183 res->x = v->x > 0 ? 1 : v->x < 0 ? -1 : 0;
184 res->y = v->y > 0 ? 1 : v->y < 0 ? -1 : 0;
185 res->z = v->z > 0 ? 1 : v->z < 0 ? -1 : 0;
186}
187
196void vec3i_lerp_ptr(vector3i *res, const vector3i *a, const vector3i *b, const vm_float_t t)
197{
198 res->x = (vm_int_t)((1.0f - t) * (vm_float_t)a->x + t * (vm_float_t)b->x);
199 res->y = (vm_int_t)((1.0f - t) * (vm_float_t)a->y + t * (vm_float_t)b->y);
200 res->z = (vm_int_t)((1.0f - t) * (vm_float_t)a->z + t * (vm_float_t)b->z);
201}
202
212void vec3i_clamp_ptr(vector3i *res, const vector3i *v, const vector3i *min, const vector3i *max)
213{
214 res->x = v->x < min->x ? min->x : (v->x > max->x ? max->x : v->x);
215 res->y = v->y < min->y ? min->y : (v->y > max->y ? max->y : v->y);
216 res->z = v->z < min->z ? min->z : (v->z > max->z ? max->z : v->z);
217}
218
219// Floor division: toward -inf; returns 0 if b == 0.
220static vm_int_t vm_div_floor3(const vm_int_t a, const vm_int_t b)
221{
222 if (b == 0) {
223 return 0;
224 }
225 const vm_int_t q = a / b;
226 const vm_int_t r = a % b;
227 if (r != 0 && ((a < 0) != (b < 0))) {
228 return q - 1;
229 }
230 return q;
231}
232
233// Floor modulo: result in [0, b) for b > 0 (or (b, 0] for b < 0); 0 if b == 0.
234static vm_int_t vm_mod_floor3(const vm_int_t a, const vm_int_t b)
235{
236 if (b == 0) {
237 return 0;
238 }
239 const vm_int_t r = a % b;
240 if (r != 0 && ((a < 0) != (b < 0))) {
241 return r + b;
242 }
243 return r;
244}
245
253void vec3i_div_ptr(vector3i *res, const vector3i *a, const vector3i *b)
254{
255 res->x = (b->x == 0) ? 0 : a->x / b->x;
256 res->y = (b->y == 0) ? 0 : a->y / b->y;
257 res->z = (b->z == 0) ? 0 : a->z / b->z;
258}
259
267void vec3i_add_scalar_ptr(vector3i *res, const vector3i *v, const vm_int_t s)
268{
269 res->x = v->x + s;
270 res->y = v->y + s;
271 res->z = v->z + s;
272}
273
281void vec3i_sub_scalar_ptr(vector3i *res, const vector3i *v, const vm_int_t s)
282{
283 res->x = v->x - s;
284 res->y = v->y - s;
285 res->z = v->z - s;
286}
287
295void vec3i_mod_ptr(vector3i *res, const vector3i *a, const vector3i *b)
296{
297 res->x = vm_mod_floor3(a->x, b->x);
298 res->y = vm_mod_floor3(a->y, b->y);
299 res->z = vm_mod_floor3(a->z, b->z);
300}
301
309void vec3i_div_floor_ptr(vector3i *res, const vector3i *a, const vector3i *b)
310{
311 res->x = vm_div_floor3(a->x, b->x);
312 res->y = vm_div_floor3(a->y, b->y);
313 res->z = vm_div_floor3(a->z, b->z);
314}
315
323void vec3i_wrap_ptr(vector3i *res, const vector3i *v, const vector3i *period)
324{
325 res->x = vm_mod_floor3(v->x, period->x);
326 res->y = vm_mod_floor3(v->y, period->y);
327 res->z = vm_mod_floor3(v->z, period->z);
328}
329
337void vec3i_from_vec2i_ptr(vector3i *res, const vector2i *v, const vm_int_t z)
338{
339 res->x = v->x;
340 res->y = v->y;
341 res->z = z;
342}
343
350void vec3i_xy_ptr(vector2i *res, const vector3i *v)
351{
352 res->x = v->x;
353 res->y = v->y;
354}
355
363{
364 const vector3 f = {
365 .x = (vm_float_t)v->x,
366 .y = (vm_float_t)v->y,
367 .z = (vm_float_t)v->z
368 };
369 vec3_normalize_ptr(res, &f);
370}
vm_int_t y
Definition vecmat.h:165
vm_int_t x
Definition vecmat.h:164
vm_int_t x
Definition vecmat.h:174
vm_int_t y
Definition vecmat.h:175
vm_int_t z
Definition vecmat.h:176
void vec3_normalize_ptr(vector3 *res, const vector3 *v)
Normalize a vector to unit length.
vm_float_t vec3i_length(vector3i v)
Computes the length (magnitude) of a vector3i.
Definition vector3i.c:333
double vm_float_t
Definition vecmat.h:79
int8_t vm_int_t
Definition vecmat.h:103
void vec3i_cross_ptr(vector3i *res, const vector3i *a, const vector3i *b)
Computes the cross-product of two vector3i.
void vec3i_wrap_ptr(vector3i *res, const vector3i *v, const vector3i *period)
Wraps each component of v into [0, period).
void vec3i_clamp_ptr(vector3i *res, const vector3i *v, const vector3i *min, const vector3i *max)
Clamps each component of a vector3i between corresponding min and max values.
void vec3i_from_vec2i_ptr(vector3i *res, const vector2i *v, const vm_int_t z)
Builds a vector3i from a vector2i and z.
void vec3i_mod_ptr(vector3i *res, const vector3i *a, const vector3i *b)
Component-wise floor modulo of a by b.
void vec3i_add_ptr(vector3i *res, const vector3i *a, const vector3i *b)
Computes the component-wise sum of two vector3i.
void vec3i_sign_ptr(vector3i *res, const vector3i *v)
Computes the sign of each component of a vector3i (-1, 0, or 1).
void vec3i_normalize_ptr(vector3i *res, const vector3i *v)
Normalizes a vector3i to approximate unit length.
void vec3i_max_ptr(vector3i *res, const vector3i *a, const vector3i *b)
Computes the component-wise maximum of two vector3i.
void vec3i_div_floor_ptr(vector3i *res, const vector3i *a, const vector3i *b)
Component-wise floored division of a by b.
void vec3i_add_scalar_ptr(vector3i *res, const vector3i *v, const vm_int_t s)
Adds a scalar to each component.
void vec3i_sub_scalar_ptr(vector3i *res, const vector3i *v, const vm_int_t s)
Subtracts a scalar from each component.
void vec3i_lerp_ptr(vector3i *res, const vector3i *a, const vector3i *b, const vm_float_t t)
Performs linear interpolation between two vector3i.
static vm_int_t vm_div_floor3(const vm_int_t a, const vm_int_t b)
void vec3i_abs_ptr(vector3i *res, const vector3i *v)
Computes the absolute value of each component of a vector3i.
void vec3i_mul_scalar_ptr(vector3i *res, const vector3i *v, const vm_int_t s)
Scales a vector3i by an integer scalar.
void vec3i_mul_ptr(vector3i *res, const vector3i *a, const vector3i *b)
Computes the component-wise product (Hadamard) of two vector3i.
void vec3i_xy_ptr(vector2i *res, const vector3i *v)
Returns the x and y components as a 2D vector.
void vec3i_div_ptr(vector3i *res, const vector3i *a, const vector3i *b)
Divides two vectors component-wise.
static vm_int_t vm_mod_floor3(const vm_int_t a, const vm_int_t b)
void vec3i_div_scalar_ptr(vector3i *res, const vector3i *v, const vm_int_t s)
Scales a vector3i by the inverse of an integer scalar.
void vec3i_min_ptr(vector3i *res, const vector3i *a, const vector3i *b)
Computes the component-wise minimum of two vector3i.
void vec3i_neg_ptr(vector3i *res, const vector3i *v)
Negates a vector3i (multiplies by -1).
void vec3i_sub_ptr(vector3i *res, const vector3i *a, const vector3i *b)
Computes the component-wise difference of two vector3i (a minus b).
void vec3i_normalize_to_vec3_ptr(vector3 *res, const vector3i *v)
Converts to a unit-length vector3.