Completed port of vec3.zig
This commit is contained in:
parent
139444ccc6
commit
16b0218556
3 changed files with 210 additions and 18 deletions
|
|
@ -2,6 +2,7 @@
|
|||
#define MATHPRIMITIVES_H
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <xmmintrin.h>
|
||||
|
||||
struct Vec3 {
|
||||
|
|
@ -44,7 +45,7 @@ struct Mat3 {
|
|||
struct Vec3 r3;
|
||||
};
|
||||
|
||||
static inline struct Mat3 mat3_identity() {
|
||||
static inline struct Mat3 mat3_identity(void) {
|
||||
return (struct Mat3){
|
||||
.r1 = {1.0, 0.0, 0.0}, .r2 = {0.0, 1.0, 0.0}, .r3 = {0.0, 0.0, 1.0}};
|
||||
}
|
||||
|
|
@ -133,4 +134,98 @@ static inline struct Vec3 mat3_mul_vec(struct Mat3 a, struct Vec3 b) {
|
|||
};
|
||||
}
|
||||
|
||||
struct Quat {
|
||||
double x;
|
||||
double y;
|
||||
double z;
|
||||
double w;
|
||||
};
|
||||
|
||||
static inline int quat_eql_tol(struct Quat a, struct Quat b, double tol) {
|
||||
return fabs(a.x - b.x) < tol && fabs(a.y - b.y) < tol &&
|
||||
fabs(a.z - b.z) < tol && fabs(a.w - b.w) < tol;
|
||||
}
|
||||
|
||||
static inline double mag_quat(struct Quat q) {
|
||||
return sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
|
||||
}
|
||||
|
||||
static inline struct Quat quat_normalized(struct Quat q) {
|
||||
double inv_mag = 1.0 / mag_quat(q);
|
||||
|
||||
return (struct Quat){
|
||||
q.x * inv_mag,
|
||||
q.y * inv_mag,
|
||||
q.z * inv_mag,
|
||||
q.w * inv_mag,
|
||||
};
|
||||
}
|
||||
|
||||
static inline struct Quat quat_conjugate(struct Quat q) {
|
||||
return (struct Quat){
|
||||
-q.x,
|
||||
-q.y,
|
||||
-q.z,
|
||||
q.w,
|
||||
};
|
||||
}
|
||||
|
||||
static inline struct Quat yaw_pitch_roll_to_quat(double yaw_radians,
|
||||
double pitch_radians,
|
||||
double roll_radians) {
|
||||
double cos_roll = cos(roll_radians / 2.0);
|
||||
double sin_roll = sin(roll_radians / 2.0);
|
||||
double cos_pitch = cos(pitch_radians / 2.0);
|
||||
double sin_pitch = sin(pitch_radians / 2.0);
|
||||
double cos_yaw = cos(yaw_radians / 2.0);
|
||||
double sin_yaw = sin(yaw_radians / 2.0);
|
||||
|
||||
struct Quat q = {
|
||||
sin_roll * cos_pitch * cos_yaw - cos_roll * sin_pitch * sin_yaw,
|
||||
cos_roll * sin_pitch * cos_yaw + sin_roll * cos_pitch * sin_yaw,
|
||||
cos_roll * cos_pitch * sin_yaw - sin_roll * sin_pitch * cos_yaw,
|
||||
cos_roll * cos_pitch * cos_yaw + sin_roll * sin_pitch * sin_yaw,
|
||||
};
|
||||
|
||||
return quat_normalized(q);
|
||||
}
|
||||
|
||||
static inline struct Quat quat_mul(struct Quat a, struct Quat b) {
|
||||
return (struct Quat){a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
|
||||
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
|
||||
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
|
||||
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z};
|
||||
}
|
||||
|
||||
static inline struct Vec3 quat_apply(struct Quat a, struct Vec3 b) {
|
||||
struct Quat b_as_quat = {b.x, b.y, b.z, 0.0};
|
||||
|
||||
struct Quat rotated_quat =
|
||||
quat_mul(quat_mul(a, b_as_quat), quat_conjugate(a));
|
||||
|
||||
return (struct Vec3){
|
||||
rotated_quat.x,
|
||||
rotated_quat.y,
|
||||
rotated_quat.z,
|
||||
};
|
||||
}
|
||||
|
||||
static inline struct Quat quat_scale(struct Quat q, double b) {
|
||||
return (struct Quat){
|
||||
q.x * b,
|
||||
q.y * b,
|
||||
q.z * b,
|
||||
q.w * b,
|
||||
};
|
||||
}
|
||||
|
||||
static inline struct Quat quat_add(struct Quat a, struct Quat b) {
|
||||
return (struct Quat){
|
||||
a.x + b.x,
|
||||
a.y + b.y,
|
||||
a.z + b.z,
|
||||
a.w + b.w,
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue