mirror of
https://github.com/guezoloic/t3dsr.git
synced 2026-01-25 09:34:24 +00:00
fix: remove all heap allocation for better performance in vector functions.
This commit is contained in:
@@ -13,7 +13,7 @@ typedef struct {
|
||||
* @return A pointer to the newly allocated 3D vector if
|
||||
* successful, or NULL if the allocation fails.
|
||||
*/
|
||||
Vec3* vec3(float x, float y, float z);
|
||||
Vec3 vec3(float x, float y, float z);
|
||||
|
||||
/**
|
||||
* @brief Adds two 3D vectors in a new 3D vector.
|
||||
@@ -23,7 +23,7 @@ Vec3* vec3(float x, float y, float z);
|
||||
* representing the sum of v1 and v2.
|
||||
* Returns NULL if the allocation fails.
|
||||
*/
|
||||
Vec3* vec3Add(Vec3* v1, Vec3* v2);
|
||||
Vec3 vec3Add(Vec3 v1, Vec3 v2);
|
||||
|
||||
/**
|
||||
* @brief Subtracts two 3D vectors in a new 3D
|
||||
@@ -34,7 +34,7 @@ Vec3* vec3Add(Vec3* v1, Vec3* v2);
|
||||
* representing the subtraction of v1 and v2.
|
||||
* Returns NULL if the allocation fails.
|
||||
*/
|
||||
Vec3* vec3Sub(Vec3* v1, Vec3* v2);
|
||||
Vec3 vec3Sub(Vec3 v1, Vec3 v2);
|
||||
|
||||
/**
|
||||
* @brief Scales a 3D vector by a constant scalar in a
|
||||
@@ -46,7 +46,7 @@ Vec3* vec3Sub(Vec3* v1, Vec3* v2);
|
||||
* scalar. Returns NULL if the allocation
|
||||
* fails.
|
||||
*/
|
||||
Vec3* vec3Scale(Vec3* v, float scalar);
|
||||
Vec3 vec3Scale(Vec3 v, float scalar);
|
||||
|
||||
/**
|
||||
* @brief Computes the dot product of two 3D vectors
|
||||
@@ -62,7 +62,7 @@ Vec3* vec3Scale(Vec3* v, float scalar);
|
||||
* Returns NAN if one of the input vectors are
|
||||
* NULL.
|
||||
*/
|
||||
float vec3Dot(Vec3* a, Vec3* b);
|
||||
float vec3Dot(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the Length (magnitude) of a 3D
|
||||
@@ -72,7 +72,7 @@ float vec3Dot(Vec3* a, Vec3* b);
|
||||
* a scalar value. Returns NAN if input vector
|
||||
* is NULL.
|
||||
*/
|
||||
float vec3Len(Vec3* v);
|
||||
float vec3Len(Vec3 v);
|
||||
|
||||
/**
|
||||
* @brief Normalizes the 3D vector.
|
||||
@@ -82,7 +82,7 @@ float vec3Len(Vec3* v);
|
||||
* if the input vector is a zero vector. Returns
|
||||
* NULL if the input pointer is invalid.
|
||||
*/
|
||||
Vec3* vec3Norm(Vec3* v);
|
||||
Vec3 vec3Norm(Vec3 v);
|
||||
|
||||
/**
|
||||
* @brief Performs linear interpolation between two 3D
|
||||
@@ -99,7 +99,7 @@ Vec3* vec3Norm(Vec3* v);
|
||||
* a and b. Returns NULL if any of the input vectors
|
||||
* is NULL.
|
||||
*/
|
||||
Vec3* vec3Lerp(Vec3* a, Vec3* b, float t);
|
||||
Vec3 vec3Lerp(Vec3 a, Vec3 b, float t);
|
||||
|
||||
/**
|
||||
* @brief Computes the cross product of two 3D vectors.
|
||||
@@ -113,7 +113,7 @@ Vec3* vec3Lerp(Vec3* a, Vec3* b, float t);
|
||||
* the cross product of vectors a and b.
|
||||
* Returns NULL if the allocation fails.
|
||||
*/
|
||||
Vec3* vec3Cross(Vec3* a, Vec3* b);
|
||||
Vec3 vec3Cross(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the angle between two 3D vectors.
|
||||
@@ -121,7 +121,7 @@ Vec3* vec3Cross(Vec3* a, Vec3* b);
|
||||
* @param b Pointer to the second vector.
|
||||
* @return The angle between a and b in radians.
|
||||
*/
|
||||
float vec3Angle(Vec3* a, Vec3* b);
|
||||
float vec3Angle(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the projection of vector a onto vector b.
|
||||
@@ -131,7 +131,7 @@ float vec3Angle(Vec3* a, Vec3* b);
|
||||
* the projection of a onto b.
|
||||
* Returns NULL if b is a zero vector.
|
||||
*/
|
||||
Vec3* vec3Proj(Vec3* a, Vec3* b);
|
||||
Vec3 vec3Proj(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the reflection of a vector v against a normal.
|
||||
@@ -141,7 +141,7 @@ Vec3* vec3Proj(Vec3* a, Vec3* b);
|
||||
* the reflection of v across normal.
|
||||
* Returns NULL if normal is a zero vector.
|
||||
*/
|
||||
Vec3* vec3Refl(Vec3* v, Vec3* normal);
|
||||
Vec3 vec3Refl(Vec3 v, Vec3 normal);
|
||||
|
||||
/**
|
||||
* @brief Computes the Euclidean distance between two 3D vectors.
|
||||
@@ -150,7 +150,7 @@ Vec3* vec3Refl(Vec3* v, Vec3* normal);
|
||||
* @return The scalar distance between a and b.
|
||||
* Returns NAN if either vector is NULL.
|
||||
*/
|
||||
float vec3Dist(Vec3* a, Vec3* b);
|
||||
float vec3Dist(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Rotates a 3D vector around a given axis by a specified angle.
|
||||
@@ -161,8 +161,8 @@ float vec3Dist(Vec3* a, Vec3* b);
|
||||
* the rotated vector.
|
||||
* Returns NULL if axis is a zero vector.
|
||||
*/
|
||||
Vec3* vec3Rotate(Vec3* v, Vec3* axis, float angle);
|
||||
Vec3 vec3Rotate(Vec3 v, Vec3 axis, float angle);
|
||||
|
||||
void vec3Free(Vec3* v);
|
||||
void vec3Free(Vec3 v);
|
||||
|
||||
#endif // VEC3_H
|
||||
@@ -14,7 +14,7 @@ typedef struct {
|
||||
* @return A pointer to the newly allocated 4D vector if
|
||||
* successful, or NULL if the allocation fails.
|
||||
*/
|
||||
Vec4* vec4(float x, float y, float z, float w);
|
||||
Vec4 vec4(float x, float y, float z, float w);
|
||||
|
||||
/**
|
||||
* @brief Adds two 4D vectors in a new 4D vector.
|
||||
@@ -24,7 +24,7 @@ Vec4* vec4(float x, float y, float z, float w);
|
||||
* representing the sum of v1 and v2.
|
||||
* Returns NULL if the allocation fails.
|
||||
*/
|
||||
Vec4* vec4Add(Vec4* v1, Vec4* v2);
|
||||
Vec4 vec4Add(Vec4 v1, Vec4 v2);
|
||||
|
||||
/**
|
||||
* @brief Subtracts two 4D vectors in a new 4D
|
||||
@@ -35,7 +35,7 @@ Vec4* vec4Add(Vec4* v1, Vec4* v2);
|
||||
* representing the subtraction of v1 and v2.
|
||||
* Returns NULL if the allocation fails.
|
||||
*/
|
||||
Vec4* vec4Sub(Vec4* v1, Vec4* v2);
|
||||
Vec4 vec4Sub(Vec4 v1, Vec4 v2);
|
||||
|
||||
/**
|
||||
* @brief Scales a 4D vector by a constant scalar in a
|
||||
@@ -47,7 +47,7 @@ Vec4* vec4Sub(Vec4* v1, Vec4* v2);
|
||||
* scalar. Returns NULL if the allocation
|
||||
* fails.
|
||||
*/
|
||||
Vec4* vec4Scale(Vec4* v, float scalar);
|
||||
Vec4 vec4Scale(Vec4 v, float scalar);
|
||||
|
||||
/**
|
||||
* @brief Computes the dot product of two 4D vectors
|
||||
@@ -63,7 +63,7 @@ Vec4* vec4Scale(Vec4* v, float scalar);
|
||||
* Returns NAN if one of the input vectors are
|
||||
* NULL.
|
||||
*/
|
||||
float vec4Dot(Vec4* a, Vec4* b);
|
||||
float vec4Dot(Vec4 a, Vec4 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the Length (magnitude) of a 4D
|
||||
@@ -73,7 +73,7 @@ float vec4Dot(Vec4* a, Vec4* b);
|
||||
* a scalar value. Returns NAN if input vector
|
||||
* is NULL.
|
||||
*/
|
||||
float vec4Len(Vec4* v);
|
||||
float vec4Len(Vec4 v);
|
||||
|
||||
/**
|
||||
* @brief Normalizes the 4D vector.
|
||||
@@ -83,7 +83,7 @@ float vec4Len(Vec4* v);
|
||||
* if the input vector is a zero vector. Returns
|
||||
* NULL if the input pointer is invalid.
|
||||
*/
|
||||
Vec4* vec4Norm(Vec4* v);
|
||||
Vec4 vec4Norm(Vec4 v);
|
||||
|
||||
/**
|
||||
* @brief Performs linear interpolation between two 4D
|
||||
@@ -100,7 +100,7 @@ Vec4* vec4Norm(Vec4* v);
|
||||
* a and b. Returns NULL if any of the input vectors
|
||||
* is NULL.
|
||||
*/
|
||||
Vec4* vec4Lerp(Vec4* a, Vec4* b, float t);
|
||||
Vec4 vec4Lerp(Vec4 a, Vec4 b, float t);
|
||||
|
||||
/**
|
||||
* @brief Computes the angle between two 4D vectors.
|
||||
@@ -108,7 +108,7 @@ Vec4* vec4Lerp(Vec4* a, Vec4* b, float t);
|
||||
* @param b Pointer to the second vector.
|
||||
* @return The angle between a and b in radians.
|
||||
*/
|
||||
float vec4Angle(Vec4* a, Vec4* b);
|
||||
float vec4Angle(Vec4 a, Vec4 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the projection of vector a onto vector b.
|
||||
@@ -118,7 +118,7 @@ float vec4Angle(Vec4* a, Vec4* b);
|
||||
* the projection of a onto b.
|
||||
* Returns NULL if b is a zero vector.
|
||||
*/
|
||||
Vec4* vec4Proj(Vec4* a, Vec4* b);
|
||||
Vec4 vec4Proj(Vec4 a, Vec4 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the reflection of a vector v against a normal.
|
||||
@@ -128,7 +128,7 @@ Vec4* vec4Proj(Vec4* a, Vec4* b);
|
||||
* the reflection of v across normal.
|
||||
* Returns NULL if normal is a zero vector.
|
||||
*/
|
||||
Vec4* vec4Refl(Vec4* v, Vec4* normal);
|
||||
Vec4 vec4Refl(Vec4 v, Vec4 normal);
|
||||
|
||||
/**
|
||||
* @brief Computes the Euclidean distance between two 4D vectors.
|
||||
@@ -137,9 +137,7 @@ Vec4* vec4Refl(Vec4* v, Vec4* normal);
|
||||
* @return The scalar distance between a and b.
|
||||
* Returns NAN if either vector is NULL.
|
||||
*/
|
||||
float vec4Dist(Vec4* a, Vec4* b);
|
||||
|
||||
void vec4Free(Vec4* v);
|
||||
float vec4Dist(Vec4 a, Vec4 b);
|
||||
|
||||
|
||||
#endif // VECTOR4_H
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
return 0;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "math/vector.h"
|
||||
|
||||
Vec3* Vec4ToVec3(Vec4* v)
|
||||
Vec3 Vec4ToVec3(Vec4 v)
|
||||
{
|
||||
return vec3(v->x, v->y, v->z);
|
||||
return vec3(v.x, v.y, v.z);
|
||||
}
|
||||
|
||||
Vec4* Vec3ToVec4(Vec3* v)
|
||||
Vec4 Vec3ToVec4(Vec3 v)
|
||||
{
|
||||
return vec4(v->x, v->y, v->z, 0);
|
||||
return vec4(v.x, v.y, v.z, 0);
|
||||
}
|
||||
|
||||
@@ -2,82 +2,67 @@
|
||||
#include <stdlib.h>
|
||||
#include <float.h>
|
||||
|
||||
#include <math/vector3.h>
|
||||
#include "math/vector3.h"
|
||||
|
||||
Vec3* vec3(float x, float y, float z)
|
||||
Vec3 vec3(float x, float y, float z)
|
||||
{
|
||||
Vec3* vec = (Vec3*)malloc(sizeof(Vec3));
|
||||
if (!vec) return NULL;
|
||||
|
||||
vec->x = x;
|
||||
vec->y = y;
|
||||
vec->z = z;
|
||||
|
||||
return vec;
|
||||
return (Vec3) {x, y, z};
|
||||
}
|
||||
|
||||
Vec3* vec3Add(Vec3* v1, Vec3* v2)
|
||||
Vec3 vec3Add(Vec3 v1, Vec3 v2)
|
||||
{
|
||||
if (!v1 || !v2) return NULL;
|
||||
return vec3(v1->x + v2->x, v1->y + v2->y, v1->z + v2->z);
|
||||
return vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
|
||||
}
|
||||
|
||||
Vec3* vec3Sub(Vec3* v1, Vec3* v2)
|
||||
Vec3 vec3Sub(Vec3 v1, Vec3 v2)
|
||||
{
|
||||
if (!v1 || !v2) return NULL;
|
||||
return vec3(v1->x - v2->x, v1->y - v2->y, v1->z - v2->z);
|
||||
return vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
|
||||
}
|
||||
|
||||
Vec3* vec3Scale(Vec3* v, float scalar)
|
||||
Vec3 vec3Scale(Vec3 v, float scalar)
|
||||
{
|
||||
if (!v) return NULL;
|
||||
return vec3(v->x * scalar, v->y * scalar, v->z * scalar);
|
||||
return vec3(v.x * scalar, v.y * scalar, v.z * scalar);
|
||||
}
|
||||
|
||||
float vec3Dot(Vec3* a, Vec3* b)
|
||||
float vec3Dot(Vec3 a, Vec3 b)
|
||||
{
|
||||
if (!a || !b) return NAN;
|
||||
return a->x * b->x + a->y * b->y + a->z * b->z;
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
float vec3Len(Vec3* v)
|
||||
float vec3Len(Vec3 v)
|
||||
{
|
||||
if (!v) return NAN;
|
||||
return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
|
||||
return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
|
||||
}
|
||||
|
||||
Vec3* vec3Norm(Vec3* v)
|
||||
Vec3 vec3Norm(Vec3 v)
|
||||
{
|
||||
if (!v) return NULL;
|
||||
|
||||
float length = vec3Len(v);
|
||||
if (length == 0.f) return NULL;
|
||||
if (length == 0.f) return vec3(0, 0, 0);
|
||||
|
||||
return vec3Scale(v, 1.f / length);
|
||||
}
|
||||
|
||||
Vec3* vec3Lerp(Vec3* a, Vec3* b, float t)
|
||||
Vec3 vec3Lerp(Vec3 a, Vec3 b, float t)
|
||||
{
|
||||
if (!a || !b) return NULL;
|
||||
t = fmaxf(0.f, fminf(t, 1.f));
|
||||
return vec3(
|
||||
a->x + t * (b->x - a->x),
|
||||
a->y + t * (b->y - a->y),
|
||||
a->z + t * (b->z - a->z)
|
||||
a.x + t * (b.x - a.x),
|
||||
a.y + t * (b.y - a.y),
|
||||
a.z + t * (b.z - a.z)
|
||||
);
|
||||
}
|
||||
|
||||
Vec3* vec3Cross(Vec3* a, Vec3* b)
|
||||
Vec3 vec3Cross(Vec3 a, Vec3 b)
|
||||
{
|
||||
if (!a || !b) return NULL;
|
||||
return vec3(
|
||||
a->y * b->z - a->z * b->y,
|
||||
a->z * b->x - a->x * b->z,
|
||||
a->x * b->y - a->y * b->x
|
||||
a.y * b.z - a.z * b.y,
|
||||
a.z * b.x - a.x * b.z,
|
||||
a.x * b.y - a.y * b.x
|
||||
);
|
||||
}
|
||||
|
||||
float vec3Angle(Vec3* a, Vec3* b)
|
||||
float vec3Angle(Vec3 a, Vec3 b)
|
||||
{
|
||||
float lenA = vec3Len(a);
|
||||
float lenB = vec3Len(b);
|
||||
@@ -95,70 +80,46 @@ float vec3Angle(Vec3* a, Vec3* b)
|
||||
return acosf(cosTheta);
|
||||
}
|
||||
|
||||
Vec3* vec3Proj(Vec3* a, Vec3* b)
|
||||
Vec3 vec3Proj(Vec3 a, Vec3 b)
|
||||
{
|
||||
if (!a || !b) return NULL;
|
||||
float dotA = vec3Dot(a, b);
|
||||
float dotB = vec3Dot(b, b);
|
||||
|
||||
if (dotB < FLT_EPSILON) return NULL;
|
||||
|
||||
float scale = dotA / dotB;
|
||||
return vec3Scale(b, scale);
|
||||
}
|
||||
|
||||
Vec3* vec3Refl(Vec3* v, Vec3* normal)
|
||||
Vec3 vec3Refl(Vec3 v, Vec3 normal)
|
||||
{
|
||||
if (!v || !normal) return NULL;
|
||||
|
||||
Vec3* proj = vec3Proj(v, normal);
|
||||
Vec3* scal = vec3Scale(proj, 2.f);
|
||||
Vec3* rlt = vec3Sub(v, scal);
|
||||
|
||||
vec3Free(proj);
|
||||
vec3Free(scal);
|
||||
Vec3 proj = vec3Proj(v, normal);
|
||||
Vec3 scal = vec3Scale(proj, 2.f);
|
||||
Vec3 rlt = vec3Sub(v, scal);
|
||||
return rlt;
|
||||
}
|
||||
|
||||
float vec3Dist(Vec3* a, Vec3* b)
|
||||
float vec3Dist(Vec3 a, Vec3 b)
|
||||
{
|
||||
if (!a || !b) return NAN;
|
||||
Vec3* vsub = vec3Sub(a, b);
|
||||
Vec3 vsub = vec3Sub(a, b);
|
||||
|
||||
float rlt = vec3Len(vsub);
|
||||
vec3Free(vsub);
|
||||
return rlt;
|
||||
}
|
||||
|
||||
Vec3* vec3Rotate(Vec3* v, Vec3* axis, float angle)
|
||||
Vec3 vec3Rotate(Vec3 v, Vec3 axis, float angle)
|
||||
{
|
||||
if (!v || !axis) return NULL;
|
||||
Vec3* normAxis = vec3Norm(axis);
|
||||
Vec3 normAxis = vec3Norm(axis);
|
||||
|
||||
float dot = vec3Dot(normAxis, v);
|
||||
Vec3* cross = vec3Cross(normAxis, v);
|
||||
Vec3 cross = vec3Cross(normAxis, v);
|
||||
|
||||
Vec3* vscal = vec3Scale(v, cosf(angle));
|
||||
Vec3* cscal = vec3Scale(cross, sinf(angle));
|
||||
Vec3 vscal = vec3Scale(v, cosf(angle));
|
||||
Vec3 cscal = vec3Scale(cross, sinf(angle));
|
||||
|
||||
Vec3* add = vec3Add(vscal, cscal);
|
||||
Vec3 add = vec3Add(vscal, cscal);
|
||||
|
||||
vec3Free(cross);
|
||||
Vec3 dscal = vec3Scale(normAxis, dot * (1 - cosf(angle)));
|
||||
|
||||
Vec3* dscal = vec3Scale(normAxis, dot * (1 - cosf(angle)));
|
||||
vec3Free(normAxis);
|
||||
vec3Free(vscal);
|
||||
vec3Free(cscal);
|
||||
|
||||
Vec3* rlt = vec3Add(add, dscal);
|
||||
|
||||
vec3Free(add);
|
||||
vec3Free(dscal);
|
||||
Vec3 rlt = vec3Add(add, dscal);
|
||||
|
||||
return rlt;
|
||||
}
|
||||
|
||||
void vec3Free(Vec3* v)
|
||||
{
|
||||
free(v);
|
||||
}
|
||||
@@ -4,73 +4,57 @@
|
||||
|
||||
#include "math/vector4.h"
|
||||
|
||||
Vec4* vec4(float x, float y, float z, float w)
|
||||
Vec4 vec4(float x, float y, float z, float w)
|
||||
{
|
||||
Vec4* vec = (Vec4*)malloc(sizeof(Vec4));
|
||||
if (!vec) return NULL;
|
||||
|
||||
vec->x = x;
|
||||
vec->y = y;
|
||||
vec->z = z;
|
||||
vec->w = w;
|
||||
|
||||
return vec;
|
||||
return (Vec4) {x, y, z, w};
|
||||
}
|
||||
|
||||
Vec4* vec4Add(Vec4* v1, Vec4* v2)
|
||||
Vec4 vec4Add(Vec4 v1, Vec4 v2)
|
||||
{
|
||||
if (!v1 || !v2) return NULL;
|
||||
return vec4(v1->x + v2->x, v1->y + v2->y, v1->z + v2->z, v1->w + v2->w);
|
||||
return vec4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
|
||||
}
|
||||
|
||||
Vec4* vec4Sub(Vec4* v1, Vec4* v2)
|
||||
Vec4 vec4Sub(Vec4 v1, Vec4 v2)
|
||||
{
|
||||
if (!v1 || !v2) return NULL;
|
||||
return vec4(v1->x - v2->x, v1->y - v2->y, v1->z - v2->z, v1->w - v2->w);
|
||||
return vec4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
|
||||
}
|
||||
|
||||
Vec4* vec4Scale(Vec4* v, float scalar)
|
||||
Vec4 vec4Scale(Vec4 v, float scalar)
|
||||
{
|
||||
if (!v) return NULL;
|
||||
return vec4(v->x * scalar, v->y * scalar, v->z * scalar, v->w * scalar);
|
||||
return vec4(v.x * scalar, v.y * scalar, v.z * scalar, v.w * scalar);
|
||||
}
|
||||
|
||||
float vec4Dot(Vec4* a, Vec4* b)
|
||||
float vec4Dot(Vec4 a, Vec4 b)
|
||||
{
|
||||
if (!a || !b) return 0;
|
||||
return a->x * b->x + a->y * b->y + a->z * b->z + a->w * b->w;
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
}
|
||||
|
||||
float vec4Len(Vec4* v)
|
||||
float vec4Len(Vec4 v)
|
||||
{
|
||||
if (!v) return NAN;
|
||||
return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
|
||||
return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
|
||||
}
|
||||
|
||||
Vec4* vec4Norm(Vec4* v)
|
||||
Vec4 vec4Norm(Vec4 v)
|
||||
{
|
||||
if (!v) return NULL;
|
||||
|
||||
float length = vec4Len(v);
|
||||
if (length == 0.f) return NULL;
|
||||
if (length == 0.f) return vec4(0, 0, 0, 0);
|
||||
|
||||
return vec4Scale(v, 1.f / length);
|
||||
}
|
||||
|
||||
Vec4* vec4Lerp(Vec4* a, Vec4* b, float t)
|
||||
Vec4 vec4Lerp(Vec4 a, Vec4 b, float t)
|
||||
{
|
||||
if (!a || !b) return NULL;
|
||||
t = fmaxf(0.f, fminf(t, 1.f));
|
||||
|
||||
return vec4(
|
||||
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)
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
float vec4Angle(Vec4* a, Vec4* b)
|
||||
float vec4Angle(Vec4 a, Vec4 b)
|
||||
{
|
||||
float lenA = vec4Len(a);
|
||||
float lenB = vec4Len(b);
|
||||
@@ -88,42 +72,26 @@ float vec4Angle(Vec4* a, Vec4* b)
|
||||
return acosf(cosTheta);
|
||||
}
|
||||
|
||||
Vec4* vec4Proj(Vec4* a, Vec4* b)
|
||||
Vec4 vec4Proj(Vec4 a, Vec4 b)
|
||||
{
|
||||
if (!a || !b) return NULL;
|
||||
float dotA = vec4Dot(a, b);
|
||||
float dotB = vec4Dot(b, b);
|
||||
|
||||
if (dotB < FLT_EPSILON) return NULL;
|
||||
|
||||
float scale = dotA / dotB;
|
||||
return vec4Scale(b, scale);
|
||||
}
|
||||
|
||||
Vec4* vec4Refl(Vec4* v, Vec4* normal)
|
||||
Vec4 vec4Refl(Vec4 v, Vec4 normal)
|
||||
{
|
||||
if (!v || !normal) return NULL;
|
||||
|
||||
Vec4* proj = vec4Proj(v, normal);
|
||||
Vec4* scal = vec4Scale(proj, 2.f);
|
||||
Vec4* rlt = vec4Sub(v, scal);
|
||||
|
||||
vec4Free(proj);
|
||||
vec4Free(scal);
|
||||
Vec4 proj = vec4Proj(v, normal);
|
||||
Vec4 scal = vec4Scale(proj, 2.f);
|
||||
Vec4 rlt = vec4Sub(v, scal);
|
||||
return rlt;
|
||||
}
|
||||
|
||||
float vec4Dist(Vec4* a, Vec4* b)
|
||||
float vec4Dist(Vec4 a, Vec4 b)
|
||||
{
|
||||
if (!a || !b) return NAN;
|
||||
Vec4* vsub = vec4Sub(a, b);
|
||||
|
||||
Vec4 vsub = vec4Sub(a, b);
|
||||
float rlt = vec4Len(vsub);
|
||||
vec4Free(vsub);
|
||||
return rlt;
|
||||
}
|
||||
|
||||
void vec4Free(Vec4* v)
|
||||
{
|
||||
if (v) free(v);
|
||||
}
|
||||
Reference in New Issue
Block a user