fix(mat3.c): rename mat3_Det to mat3_det

- renamed all structs with _t suffix
- remove most comments (might continue later)
This commit is contained in:
2025-06-08 23:32:56 +02:00
parent 19e57fb1ae
commit d456cbc3a7
6 changed files with 99 additions and 267 deletions

View File

@@ -1,30 +1,30 @@
#include "mat3.h"
#include <string.h>
Mat3 mat3(const float arr[9])
Mat3_t mat3(const float arr[9])
{
Mat3 mat;
Mat3_t mat;
memcpy(mat.m, arr, 9*sizeof(float));
return mat;
}
Mat3 mat3_zro()
Mat3_t mat3_zro()
{
return (Mat3){0};
return (Mat3_t){0};
}
Mat3 mat3_ity()
Mat3_t mat3_ity()
{
return (Mat3) {{
return (Mat3_t) {{
1, 0, 0,
0, 1, 0,
0, 0, 1
}};
}
Mat3 mat3_add(const Mat3* m1, const Mat3* m2)
Mat3_t mat3_add(const Mat3_t* m1, const Mat3_t* m2)
{
Mat3 mat;
Mat3_t mat;
for(int i = 0; i<9; i++) {
mat.m[i] = m1->m[i] + m2->m[i];
@@ -33,9 +33,9 @@ Mat3 mat3_add(const Mat3* m1, const Mat3* m2)
return mat;
}
Mat3 mat3_sub(const Mat3* m1, const Mat3* m2)
Mat3_t mat3_sub(const Mat3_t* m1, const Mat3_t* m2)
{
Mat3 mat;
Mat3_t mat;
for(int i = 0; i<9; i++) {
mat.m[i] = m1->m[i] - m2->m[i];
@@ -44,9 +44,9 @@ Mat3 mat3_sub(const Mat3* m1, const Mat3* m2)
return mat;
}
Mat3 mat3_scl(const Mat3* m, float scalar)
Mat3_t mat3_scl(const Mat3_t* m, float scalar)
{
Mat3 mat;
Mat3_t mat;
for(int i = 0; i<9; i++) {
mat.m[i] = m->m[i] * scalar;
@@ -55,9 +55,9 @@ Mat3 mat3_scl(const Mat3* m, float scalar)
return mat;
}
Mat3 mat3_mul(const Mat3* m1, const Mat3* m2)
Mat3_t mat3_mul(const Mat3_t* m1, const Mat3_t* m2)
{
Mat3 mat;
Mat3_t mat;
for(int i = 0; i<3; i++) {
int i3 = i * 3;
@@ -75,9 +75,9 @@ Mat3 mat3_mul(const Mat3* m1, const Mat3* m2)
return mat;
}
Mat3 mat3_tpo(const Mat3* m)
Mat3_t mat3_tpo(const Mat3_t* m)
{
Mat3 mat;
Mat3_t mat;
for(int i = 0; i<3; i++) {
int i3 = i * 3;
@@ -90,21 +90,20 @@ Mat3 mat3_tpo(const Mat3* m)
return mat;
}
float mat3_Det(const Mat3* m)
float mat3_det(const Mat3_t* m)
{
return m->m[0] * (m->m[4] * m->m[8] - m->m[5] * m->m[7]) -
m->m[1] * (m->m[3] * m->m[8] - m->m[5] * m->m[6]) +
m->m[2] * (m->m[3] * m->m[7] - m->m[4] * m->m[6]);
}
Mat3 mat3_inv(const Mat3* m) {
Mat3 inv;
Mat3_t mat3_inv(const Mat3_t* m) {
Mat3_t inv;
float det = mat3_det(m);
if (det == 0) return mat3_ity();
float invDet = 1.0f / det;
// ???
inv.m[0] = (m->m[4] * m->m[8] - m->m[5] * m->m[7]) * invDet;
inv.m[1] = -(m->m[1] * m->m[8] - m->m[2] * m->m[7]) * invDet;
inv.m[2] = (m->m[1] * m->m[5] - m->m[2] * m->m[4]) * invDet;

View File

@@ -4,19 +4,26 @@
typedef struct
{
float m[9];
} Mat3;
} Mat3_t;
Mat3 mat3(const float arr[9]);
Mat3 mat3_zro();
Mat3 mat3_ity();
Mat3_t mat3(const float arr[9]);
Mat3 mat3_add(const Mat3* m1, const Mat3* m2);
Mat3 mat3_sub(const Mat3* m1, const Mat3* m2);
Mat3 mat3_scl(const Mat3* m, float scalar);
Mat3 mat3_mul(const Mat3* m1, const Mat3* m2);
Mat3_t mat3_zro();
Mat3 mat3_tpo(const Mat3* m);
float mat3_det(const Mat3* m);
Mat3 mat3_inv(const Mat3* m);
Mat3_t mat3_ity();
Mat3_t mat3_add(const Mat3_t* m1, const Mat3_t* m2);
Mat3_t mat3_sub(const Mat3_t* m1, const Mat3_t* m2);
Mat3_t mat3_scl(const Mat3_t* m, float scalar);
Mat3_t mat3_mul(const Mat3_t* m1, const Mat3_t* m2);
Mat3_t mat3_tpo(const Mat3_t* m);
float mat3_det(const Mat3_t* m);
Mat3_t mat3_inv(const Mat3_t* m);
#endif // MATRIX3_H

View File

@@ -4,37 +4,37 @@
#include "vec3.h"
inline Vec3 vec3(float x, float y, float z)
inline Vec3_t vec3(float x, float y, float z)
{
return (Vec3) {x, y, z};
return (Vec3_t) {x, y, z};
}
Vec3 vec3_add(Vec3 v1, Vec3 v2)
Vec3_t vec3_add(Vec3_t v1, Vec3_t v2)
{
return vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
Vec3 vec3_sub(Vec3 v1, Vec3 v2)
Vec3_t vec3_sub(Vec3_t v1, Vec3_t v2)
{
return vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
Vec3 vec3_scale(Vec3 v, float scalar)
Vec3_t vec3_scale(Vec3_t v, float scalar)
{
return vec3(v.x * scalar, v.y * scalar, v.z * scalar);
}
float vec3_dot(Vec3 a, Vec3 b)
float vec3_dot(Vec3_t a, Vec3_t b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
float vec3_len(Vec3 v)
float vec3_len(Vec3_t v)
{
return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
}
Vec3 vec3_norm(Vec3 v)
Vec3_t vec3_norm(Vec3_t v)
{
float length = vec3_len(v);
@@ -43,7 +43,7 @@ Vec3 vec3_norm(Vec3 v)
return vec3_scale(v, 1.f / length);
}
Vec3 vec3_lerp(Vec3 a, Vec3 b, float t)
Vec3_t vec3_lerp(Vec3_t a, Vec3_t b, float t)
{
t = fmaxf(0.f, fminf(t, 1.f));
return vec3(
@@ -53,7 +53,7 @@ Vec3 vec3_lerp(Vec3 a, Vec3 b, float t)
);
}
Vec3 vec3_cross(Vec3 a, Vec3 b)
Vec3_t vec3_cross(Vec3_t a, Vec3_t b)
{
return vec3(
a.y * b.z - a.z * b.y,
@@ -62,7 +62,7 @@ Vec3 vec3_cross(Vec3 a, Vec3 b)
);
}
float vec3_angle(Vec3 a, Vec3 b)
float vec3_angle(Vec3_t a, Vec3_t b)
{
float lenA = vec3_len(a);
float lenB = vec3_len(b);
@@ -74,27 +74,27 @@ float vec3_angle(Vec3 a, Vec3 b)
fminf(vec3_dot(a, b) / (lenA * lenB), 1.f)));
}
Vec3 vec3_proj(Vec3 a, Vec3 b)
Vec3_t vec3_proj(Vec3_t a, Vec3_t b)
{
return vec3_scale(b,
vec3_dot(a, b) / vec3_dot(b, b));
}
Vec3 vec3_refl(Vec3 v, Vec3 normal)
Vec3_t vec3_refl(Vec3_t v, Vec3_t normal)
{
return vec3_sub(v, vec3_scale(vec3_proj(v, normal), 2.f));
}
float vec3_dist(Vec3 a, Vec3 b)
float vec3_dist(Vec3_t a, Vec3_t b)
{
return vec3_len(vec3_sub(a, b));
}
Vec3 vec3_rotate(Vec3 v, Vec3 axis, float angle)
Vec3_t vec3_rotate(Vec3_t v, Vec3_t axis, float angle)
{
Vec3 normAxis = vec3_norm(axis);
Vec3_t normAxis = vec3_norm(axis);
Vec3 rlt = vec3_add(
Vec3_t rlt = vec3_add(
vec3_add(
vec3_scale(v, cosf(angle)),
vec3_scale(vec3_cross(normAxis, v), sinf(angle))

View File

@@ -4,128 +4,34 @@
typedef struct
{
float x, y, z;
} Vec3;
} Vec3_t;
/**
* @brief Creates a new 3D vector.
* @param x X-axis coordinate.
* @param y Y-axis coordinate.
* @param z Z-axis coordinate.
* @return A 3D vector with the specified coordinates.
*/
Vec3 vec3(float x, float y, float z);
Vec3_t vec3(float x, float y, float z);
/**
* @brief Adds two 3D vectors and returns a new 3D vector.
* @param v1 First vector.
* @param v2 Second vector.
* @return A 3D vector representing the sum of v1 and v2.
*/
Vec3 vec3_add(Vec3 v1, Vec3 v2);
Vec3_t vec3_add(Vec3_t v1, Vec3_t v2);
/**
* @brief Subtracts two 3D vectors and returns a new 3D vector.
* @param v1 First vector.
* @param v2 Second vector.
* @return A 3D vector representing the result of v1 minus v2.
*/
Vec3 vec3_sub(Vec3 v1, Vec3 v2);
Vec3_t vec3_sub(Vec3_t v1, Vec3_t v2);
/**
* @brief Scales a 3D vector by a constant scalar and returns a new 3D vector.
* @param v 3D vector.
* @param scalar Scalar value.
* @return A 3D vector representing the multiplication of v by the scalar.
*/
Vec3 vec3_scale(Vec3 v, float scalar);
Vec3_t vec3_scale(Vec3_t v, float scalar);
/**
* @brief Computes the dot product of two 3D vectors.
* @param a First vector.
* @param b Second vector.
* @return A scalar value representing the dot product of a and b.
* - scalar > 0: Both vectors have the same orientation (the angle between them is acute).
* - scalar = 0: Vectors are orthogonal (the angle between them is 90 degrees).
* - scalar < 0: Vectors have opposite orientations (the angle between them is obtuse).
*/
float vec3_dot(Vec3 a, Vec3 b);
float vec3_dot(Vec3_t a, Vec3_t b);
/**
* @brief Computes the length (magnitude) of a 3D vector.
* @param v 3D vector.
* @return A scalar value representing the length (magnitude) of the vector v.
*/
float vec3_len(Vec3 v);
float vec3_len(Vec3_t v);
/**
* @brief Normalizes a 3D vector (scales it to unit length).
* @param v 3D vector.
* @return A 3D vector representing the normalized version of v.
* Returns a zero vector (0, 0, 0) if the input vector is a zero vector.
*/
Vec3 vec3_norm(Vec3 v);
Vec3_t vec3_norm(Vec3_t v);
/**
* @brief Performs linear interpolation between two 3D vectors.
* @param a Start vector.
* @param b End vector.
* @param t Interpolation factor (0.0 to 1.0).
* - t = 0 returns the vector a.
* - t = 1 returns the vector b.
* - t between 0 and 1 returns a point between a and b.
* @return A 3D vector representing the interpolated result between a and b.
*/
Vec3 vec3_lerp(Vec3 a, Vec3 b, float t);
Vec3_t vec3_lerp(Vec3_t a, Vec3_t b, float t);
/**
* @brief Computes the cross product of two 3D vectors.
* The cross product produces a new vector that is orthogonal (perpendicular) to both input vectors.
* The direction of the resulting vector follows the right-hand rule.
* @param a First vector.
* @param b Second vector.
* @return A 3D vector representing the cross product of vectors a and b.
*/
Vec3 vec3_cross(Vec3 a, Vec3 b);
Vec3_t vec3_cross(Vec3_t a, Vec3_t b);
/**
* @brief Computes the angle between two 3D vectors.
* @param a First vector.
* @param b Second vector.
* @return The angle between vectors a and b in radians.
*/
float vec3_angle(Vec3 a, Vec3 b);
float vec3_angle(Vec3_t a, Vec3_t b);
/**
* @brief Computes the projection of vector a onto vector b.
* @param a The vector to be projected.
* @param b The vector onto which a is projected.
* @return A 3D vector representing the projection of a onto b.
*/
Vec3 vec3_proj(Vec3 a, Vec3 b);
Vec3_t vec3_proj(Vec3_t a, Vec3_t b);
/**
* @brief Computes the reflection of a vector v against a normal.
* @param v The incident vector.
* @param normal The normal vector of the surface.
* @return A 3D vector representing the reflection of v across normal.
*/
Vec3 vec3_refl(Vec3 v, Vec3 normal);
Vec3_t vec3_refl(Vec3_t v, Vec3_t normal);
/**
* @brief Computes the Euclidean distance between two 3D vectors.
* @param a The first vector.
* @param b The second vector.
* @return The scalar distance between a and b.
*/
float vec3_rist(Vec3 a, Vec3 b);
float vec3_rist(Vec3_t a, Vec3_t b);
/**
* @brief Rotates a 3D vector around a given axis by a specified angle.
* @param v The vector to rotate.
* @param axis The rotation axis (must be normalized).
* @param angle Rotation angle in radians.
* @return A 3D vector representing the rotated vector.
*/
Vec3 vec3_rotate(Vec3 v, Vec3 axis, float angle);
Vec3_t vec3_rotate(Vec3_t v, Vec3_t axis, float angle);
#endif // VEC3__H

View File

@@ -4,37 +4,37 @@
#include "vec4.h"
inline Vec4 vec4(float x, float y, float z, float w)
inline Vec4_t vec4(float x, float y, float z, float w)
{
return (Vec4) {x, y, z, w};
return (Vec4_t) {x, y, z, w};
}
Vec4 vec4_add(Vec4 v1, Vec4 v2)
Vec4_t vec4_add(Vec4_t v1, Vec4_t v2)
{
return vec4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
}
Vec4 vec4_sub(Vec4 v1, Vec4 v2)
Vec4_t vec4_sub(Vec4_t v1, Vec4_t v2)
{
return vec4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
}
Vec4 vec4_scale(Vec4 v, float scalar)
Vec4_t vec4_scale(Vec4_t v, float scalar)
{
return vec4(v.x * scalar, v.y * scalar, v.z * scalar, v.w * scalar);
}
float vec4_dot(Vec4 a, Vec4 b)
float vec4_dot(Vec4_t a, Vec4_t b)
{
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
float vec4_len(Vec4 v)
float vec4_len(Vec4_t v)
{
return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
}
Vec4 vec4_norm(Vec4 v)
Vec4_t vec4_norm(Vec4_t v)
{
float length = vec4_len(v);
if (length == 0.f) return vec4(0, 0, 0, 0);
@@ -42,7 +42,7 @@ Vec4 vec4_norm(Vec4 v)
return vec4_scale(v, 1.f / length);
}
Vec4 vec4_lerp(Vec4 a, Vec4 b, float t)
Vec4_t vec4_lerp(Vec4_t a, Vec4_t b, float t)
{
t = fmaxf(0.f, fminf(t, 1.f));
@@ -54,7 +54,7 @@ Vec4 vec4_lerp(Vec4 a, Vec4 b, float t)
);
}
float vec4Angle(Vec4 a, Vec4 b)
float vec4Angle(Vec4_t a, Vec4_t b)
{
float lenA = vec4_len(a);
float lenB = vec4_len(b);
@@ -72,7 +72,7 @@ float vec4Angle(Vec4 a, Vec4 b)
return acosf(cosTheta);
}
Vec4 vec4_proj(Vec4 a, Vec4 b)
Vec4_t vec4_proj(Vec4_t a, Vec4_t b)
{
float dotA = vec4_dot(a, b);
float dotB = vec4_dot(b, b);
@@ -81,17 +81,17 @@ Vec4 vec4_proj(Vec4 a, Vec4 b)
return vec4_scale(b, scale);
}
Vec4 vec4_refl(Vec4 v, Vec4 normal)
Vec4_t vec4_refl(Vec4_t v, Vec4_t normal)
{
Vec4 proj = vec4_proj(v, normal);
Vec4 scal = vec4_scale(proj, 2.f);
Vec4 rlt = vec4_sub(v, scal);
Vec4_t proj = vec4_proj(v, normal);
Vec4_t scal = vec4_scale(proj, 2.f);
Vec4_t rlt = vec4_sub(v, scal);
return rlt;
}
float vec4_dist(Vec4 a, Vec4 b)
float vec4_dist(Vec4_t a, Vec4_t b)
{
Vec4 vsub = vec4_sub(a, b);
Vec4_t vsub = vec4_sub(a, b);
float rlt = vec4_len(vsub);
return rlt;
}

View File

@@ -4,110 +4,30 @@
typedef struct
{
float x, y, z, w;
} Vec4;
} Vec4_t;
/**
* @brief Creates a new 4D vector.
* @param x X-axis coordinate.
* @param y Y-axis coordinate.
* @param z Z-axis coordinate.
* @param w W-axis coordinate.
* @return A 4D vector with the specified coordinates.
*/
Vec4 vec4(float x, float y, float z, float w);
Vec4_t vec4(float x, float y, float z, float w);
/**
* @brief Adds two 4D vectors and returns a new 4D vector.
* @param v1 First vector.
* @param v2 Second vector.
* @return A 4D vector representing the sum of v1 and v2.
*/
Vec4 vec4_add(Vec4 v1, Vec4 v2);
Vec4_t vec4_add(Vec4_t v1, Vec4_t v2);
/**
* @brief Subtracts two 4D vectors and returns a new 4D vector.
* @param v1 First vector.
* @param v2 Second vector.
* @return A 4D vector representing the result of v1 minus v2.
*/
Vec4 vec4_sub(Vec4 v1, Vec4 v2);
Vec4_t vec4_sub(Vec4_t v1, Vec4_t v2);
/**
* @brief Scales a 4D vector by a constant scalar and returns a new 4D vector.
* @param v 4D vector.
* @param scalar Scalar value.
* @return A 4D vector representing the multiplication of v by the scalar.
*/
Vec4 vec4_scale(Vec4 v, float scalar);
Vec4_t vec4_scale(Vec4_t v, float scalar);
/**
* @brief Computes the dot product of two 4D vectors.
* @param a First vector.
* @param b Second vector.
* @return A scalar value representing the dot product of a and b.
* - scalar > 0: Both vectors have the same orientation (the angle between them is acute).
* - scalar = 0: Vectors are orthogonal (the angle between them is 90 degrees).
* - scalar < 0: Vectors have opposite orientations (the angle between them is obtuse).
*/
float vec4_dot(Vec4 a, Vec4 b);
float vec4_dot(Vec4_t a, Vec4_t b);
/**
* @brief Computes the length (magnitude) of a 4D vector.
* @param v 4D vector.
* @return A scalar value representing the length (magnitude) of the vector v.
*/
float vec4_len(Vec4 v);
float vec4_len(Vec4_t v);
/**
* @brief Normalizes a 4D vector (scales it to unit length).
* @param v 4D vector.
* @return A 4D vector representing the normalized version of v.
* Returns a zero vector (0, 0, 0) if the input vector is a zero vector.
*/
Vec4 vec4_norm(Vec4 v);
Vec4_t vec4_norm(Vec4_t v);
/**
* @brief Performs linear interpolation between two 4D vectors.
* @param a Start vector.
* @param b End vector.
* @param t Interpolation factor (0.0 to 1.0).
* - t = 0 returns the vector a.
* - t = 1 returns the vector b.
* - t between 0 and 1 returns a point between a and b.
* @return A 4D vector representing the interpolated result between a and b.
*/
Vec4 vec4_lerp(Vec4 a, Vec4 b, float t);
Vec4_t vec4_lerp(Vec4_t a, Vec4_t b, float t);
/**
* @brief Computes the angle between two 4D vectors.
* @param a First vector.
* @param b Second vector.
* @return The angle between vectors a and b in radians.
*/
float vec4_angle(Vec4 a, Vec4 b);
float vec4_angle(Vec4_t a, Vec4_t b);
/**
* @brief Computes the projection of vector a onto vector b.
* @param a The vector to be projected.
* @param b The vector onto which a is projected.
* @return A 4D vector representing the projection of a onto b.
*/
Vec4 vec4_proj(Vec4 a, Vec4 b);
Vec4_t vec4_proj(Vec4_t a, Vec4_t b);
/**
* @brief Computes the reflection of a vector v against a normal.
* @param v The incident vector.
* @param normal The normal vector of the surface.
* @return A 4D vector representing the reflection of v across normal.
*/
Vec4 vec4_refl(Vec4 v, Vec4 normal);
Vec4_t vec4_refl(Vec4_t v, Vec4_t normal);
/**
* @brief Computes the Euclidean distance between two 4D vectors.
* @param a The first vector.
* @param b The second vector.
* @return The scalar distance between a and b.
*/
float vec4_dist(Vec4 a, Vec4 b);
float vec4_dist(Vec4_t a, Vec4_t b);
#endif // VECTOR4_H