feat(vec4.c): add few functions

- vec4f_len
- vec4f_norm
- vec4f_lerp
- vec4f_angle
- vec4f_proj
- vec4f_dist
This commit is contained in:
2025-06-22 19:29:45 +02:00
parent bc39653a2a
commit a206ccce6e
3 changed files with 75 additions and 66 deletions

View File

@@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <float.h>
#if defined(__x86_64__) || defined(__i386__) #if defined(__x86_64__) || defined(__i386__)
#define SIMD_X86 #define SIMD_X86

View File

@@ -163,74 +163,77 @@ float vec4f_dot(Vec4f_t a, Vec4f_t b)
#endif #endif
} }
// float vec4_dot(Vec4_t a, Vec4_t b) Vec4f_t vec4f_norm_r(Vec4f_t *__restrict v)
// { {
// return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; float length = vec4f_len(*v);
// } if (length < FLT_EPSILON) *v = vec4f_zero();
vec4f_scale_r(v, 1.f / length);
return *v;
}
// float vec4_len(Vec4_t v) Vec4f_t vec4f_norm(Vec4f_t v)
// { {
// return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); vec4f_norm_r(&v);
// } return v;
}
// Vec4_t vec4_norm(Vec4_t v) Vec4f_t vec4f_lerp_r(Vec4f_t *__restrict a, Vec4f_t b, float t)
// { {
// float length = vec4_len(v); t = fmaxf(0.f, fminf(t, 1.f));
// if (length == 0.f) return vec4(0, 0, 0, 0);
a->x += t * (b.x - a->x);
a->y += t * (b.y - a->y);
a->z += t * (b.z - a->z);
a->w += t * (b.w - a->w);
return *a;
}
// return vec4_scale(v, 1.f / length); Vec4f_t vec4f_lerp(Vec4f_t a, Vec4f_t b, float t)
// } {
vec4f_lerp_r(&a, b, t);
return a;
}
// Vec4_t vec4_lerp(Vec4_t a, Vec4_t b, float t) float vec4f_angle(Vec4f_t a, Vec4f_t b)
// { {
// t = fmaxf(0.f, fminf(t, 1.f)); float lenA = vec4f_len(a);
float lenB = vec4f_len(b);
// return vec4( if (isnan(lenA) || isnan(lenB)
// a.x + t * (b.x - a.x), || lenA < FLT_EPSILON
// a.y + t * (b.y - a.y), || lenB < FLT_EPSILON)
// a.z + t * (b.z - a.z), return NAN;
// a.w + t * (b.w - a.w)
// );
// }
// float vec4Angle(Vec4_t a, Vec4_t b) float dot = vec4f_dot(a, b);
// { float cosTheta = dot / (lenA * lenB);
// float lenA = vec4_len(a);
// float lenB = vec4_len(b);
// if (isnan(lenA) || isnan(lenB) cosTheta = fmaxf(-1.f, fminf(cosTheta, 1.f));
// || lenA < FLT_EPSILON
// || lenB < FLT_EPSILON)
// return NAN;
// float dot = vec4_dot(a, b); return acosf(cosTheta);
// float cosTheta = dot / (lenA * lenB); }
// cosTheta = fmaxf(-1.f, fminf(cosTheta, 1.f));
// return acosf(cosTheta); Vec4f_t vec4f_proj(Vec4f_t a, Vec4f_t b)
// } {
float dotA = vec4f_dot(a, b);
float dotB = vec4f_dot(b, b);
// Vec4_t vec4_proj(Vec4_t a, Vec4_t b) float scale = dotA / dotB;
// { return vec4f_scale(b, scale);
// float dotA = vec4_dot(a, b); }
// float dotB = vec4_dot(b, b);
// float scale = dotA / dotB; Vec4f_t vec4f_refl(Vec4f_t v, Vec4f_t normal)
// return vec4_scale(b, scale); {
// } Vec4f_t proj = vec4f_proj(v, normal);
Vec4f_t scal = vec4f_scale(proj, 2.f);
Vec4f_t rlt = vec4f_sub(v, scal);
return rlt;
}
// Vec4_t vec4_refl(Vec4_t v, Vec4_t normal) float vec4f_dist(Vec4f_t a, Vec4f_t b)
// { {
// Vec4_t proj = vec4_proj(v, normal); Vec4f_t vsub = vec4f_sub(a, b);
// Vec4_t scal = vec4_scale(proj, 2.f); float rlt = vec4f_len(vsub);
// Vec4_t rlt = vec4_sub(v, scal); return rlt;
// return rlt; }
// }
// float vec4_dist(Vec4_t a, Vec4_t b)
// {
// Vec4_t vsub = vec4_sub(a, b);
// float rlt = vec4_len(vsub);
// return rlt;
// }

View File

@@ -31,20 +31,25 @@ Vec4f_t vec4f_sub(Vec4f_t a, Vec4f_t b);
Vec4f_t vec4f_scale_r(Vec4f_t *__restrict out, float scale); Vec4f_t vec4f_scale_r(Vec4f_t *__restrict out, float scale);
Vec4f_t vec4f_scale(Vec4f_t a, float scale); Vec4f_t vec4f_scale(Vec4f_t a, float scale);
float vec4_dot(Vec4f_t a, Vec4f_t b); float vec4f_dot(Vec4f_t a, Vec4f_t b);
float vec4_len(Vec4f_t v); inline static float vec4f_len(Vec4f_t v)
{
return sqrtf(vec4f_dot(v, v));
}
Vec4f_t vec4_norm(Vec4f_t v); Vec4f_t vec4f_norm_r(Vec4f_t *__restrict v);
Vec4f_t vec4f_norm(Vec4f_t v);
Vec4f_t vec4_lerp(Vec4f_t a, Vec4f_t b, float t); Vec4f_t vec4f_lerp_r(Vec4f_t *__restrict a, Vec4f_t b, float t);
Vec4f_t vec4f_lerp(Vec4f_t a, Vec4f_t b, float t);
float vec4_angle(Vec4f_t a, Vec4f_t b); float vec4f_angle(Vec4f_t a, Vec4f_t b);
Vec4f_t vec4_proj(Vec4f_t a, Vec4f_t b); Vec4f_t vec4f_proj(Vec4f_t a, Vec4f_t b);
Vec4f_t vec4_refl(Vec4f_t v, Vec4f_t normal); Vec4f_t vec4f_refl(Vec4f_t v, Vec4f_t normal);
float vec4_dist(Vec4f_t a, Vec4f_t b); float vec4f_dist(Vec4f_t a, Vec4f_t b);
#endif // VECTOR4_H #endif // VECTOR4_H