/*
 * Just enough 3D maths for the glasses: vectors, unit quaternions for
 * orientations and column-major 4x4 matrices as GL takes them. World
 * space is OpenGL's: +x right, +y up, -z straight ahead, metres.
 */
#ifndef C8_MATH3D_H
#define C8_MATH3D_H

#include <math.h>

struct vec3 {
	double x, y, z;
};

/* w + xi + yj + zk */
struct quat {
	double w, x, y, z;
};

/* m[column * 4 + row] */
struct mat4 {
	float m[16];
};

/* Where something is and which way it faces: room positions come with
 * 6DoF tracking; until then position is the origin */
struct pose {
	struct vec3 position;
	struct quat orientation;
};

static inline struct vec3 vec3_add(struct vec3 a, struct vec3 b) {
	return (struct vec3){ a.x + b.x, a.y + b.y, a.z + b.z };
}

static inline struct vec3 vec3_sub(struct vec3 a, struct vec3 b) {
	return (struct vec3){ a.x - b.x, a.y - b.y, a.z - b.z };
}

static inline struct vec3 vec3_scale(struct vec3 a, double s) {
	return (struct vec3){ a.x * s, a.y * s, a.z * s };
}

static inline double vec3_dot(struct vec3 a, struct vec3 b) {
	return a.x * b.x + a.y * b.y + a.z * b.z;
}

static inline struct vec3 vec3_cross(struct vec3 a, struct vec3 b) {
	return (struct 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 };
}

static inline double vec3_length(struct vec3 a) {
	return sqrt(vec3_dot(a, a));
}

static inline struct vec3 vec3_normalize(struct vec3 a) {
	double l = vec3_length(a);
	return l > 1e-12 ? vec3_scale(a, 1 / l) : a;
}

static inline struct quat quat_identity(void) {
	return (struct quat){ 1, 0, 0, 0 };
}

static inline struct quat quat_mul(struct quat a, struct quat b) {
	return (struct quat){
		a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
		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,
	};
}

static inline struct quat quat_conjugate(struct quat q) {
	return (struct quat){ q.w, -q.x, -q.y, -q.z };
}

static inline struct quat quat_normalize(struct quat q) {
	double l = sqrt(q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);
	if (l < 1e-12) {
		return quat_identity();
	}
	return (struct quat){ q.w / l, q.x / l, q.y / l, q.z / l };
}

/* Turning by angle (radians) about a unit axis */
static inline struct quat quat_axis_angle(struct vec3 axis, double angle) {
	double s = sin(angle / 2);
	return (struct quat){ cos(angle / 2), axis.x * s, axis.y * s,
		axis.z * s };
}

static inline struct vec3 quat_rotate(struct quat q, struct vec3 v) {
	/* v + 2w(u x v) + 2u x (u x v), u the vector part */
	struct vec3 u = { q.x, q.y, q.z };
	struct vec3 t = vec3_scale(vec3_cross(u, v), 2);
	return vec3_add(vec3_add(v, vec3_scale(t, q.w)), vec3_cross(u, t));
}

/* Turned by yaw about up, then pitched: level, facing back at the origin */
static inline struct quat quat_yaw_pitch(double yaw, double pitch) {
	return quat_mul(quat_axis_angle((struct vec3){ 0, 1, 0 }, yaw),
		quat_axis_angle((struct vec3){ 1, 0, 0 }, pitch));
}

/* The other way: the yaw and pitch a direction points along */
static inline void vec3_yaw_pitch(struct vec3 d, double *yaw, double *pitch) {
	d = vec3_normalize(d);
	*yaw = atan2(-d.x, -d.z);
	*pitch = asin(fmax(-1, fmin(1, d.y)));
}

/* Heading about +y, ignoring pitch and roll: for recentring */
static inline double quat_yaw(struct quat q) {
	struct vec3 f = quat_rotate(q, (struct vec3){ 0, 0, -1 });
	return atan2(-f.x, -f.z);
}

static inline struct quat quat_slerp(struct quat a, struct quat b, double t) {
	double d = a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z;
	if (d < 0) {
		b = (struct quat){ -b.w, -b.x, -b.y, -b.z };
		d = -d;
	}
	if (d > 0.9995) {
		return quat_normalize((struct quat){ a.w + (b.w - a.w) * t,
			a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t,
			a.z + (b.z - a.z) * t });
	}
	double th = acos(d), s = sin(th);
	double wa = sin((1 - t) * th) / s, wb = sin(t * th) / s;
	return (struct quat){ a.w * wa + b.w * wb, a.x * wa + b.x * wb,
		a.y * wa + b.y * wb, a.z * wa + b.z * wb };
}

/* A point given in a pose's own space (x right, y up on its plane) */
static inline struct vec3 pose_point(struct pose p, double x, double y,
		double z) {
	return vec3_add(p.position,
		quat_rotate(p.orientation, (struct vec3){ x, y, z }));
}

static inline struct mat4 mat4_mul(struct mat4 a, struct mat4 b) {
	struct mat4 r;
	for (int c = 0; c < 4; c++) {
		for (int row = 0; row < 4; row++) {
			float s = 0;
			for (int k = 0; k < 4; k++) {
				s += a.m[k * 4 + row] * b.m[c * 4 + k];
			}
			r.m[c * 4 + row] = s;
		}
	}
	return r;
}

/* Rotation, then translation: a pose as a matrix (model space to world) */
static inline struct mat4 mat4_from_pose(struct pose p) {
	struct quat q = p.orientation;
	double xx = q.x * q.x, yy = q.y * q.y, zz = q.z * q.z;
	double xy = q.x * q.y, xz = q.x * q.z, yz = q.y * q.z;
	double wx = q.w * q.x, wy = q.w * q.y, wz = q.w * q.z;
	return (struct mat4){ {
		1 - 2 * (yy + zz), 2 * (xy + wz), 2 * (xz - wy), 0,
		2 * (xy - wz), 1 - 2 * (xx + zz), 2 * (yz + wx), 0,
		2 * (xz + wy), 2 * (yz - wx), 1 - 2 * (xx + yy), 0,
		p.position.x, p.position.y, p.position.z, 1,
	} };
}

/* The inverse of a pose's matrix: world to that pose's space (a view) */
static inline struct mat4 mat4_view(struct pose p) {
	struct quat inv = quat_conjugate(p.orientation);
	struct vec3 t = quat_rotate(inv, vec3_scale(p.position, -1));
	return mat4_from_pose((struct pose){ t, inv });
}

static inline struct mat4 mat4_scale_translate(double sx, double sy,
		double tx, double ty) {
	return (struct mat4){ { sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, 1, 0,
		tx, ty, 0, 1 } };
}

/* A symmetric frustum; fov_y in radians */
static inline struct mat4 mat4_perspective(double fov_y, double aspect,
		double near, double far) {
	double f = 1 / tan(fov_y / 2);
	return (struct mat4){ {
		f / aspect, 0, 0, 0,
		0, f, 0, 0,
		0, 0, (far + near) / (near - far), -1,
		0, 0, 2 * far * near / (near - far), 0,
	} };
}

/* An asymmetric frustum, as OpenXR gives a view's (tangents of the
 * angles to its edges: left and down negative) */
static inline struct mat4 mat4_frustum(double tl, double tr, double td,
		double tu, double near, double far) {
	double w = tr - tl, h = tu - td;
	return (struct mat4){ {
		2 / w, 0, 0, 0,
		0, 2 / h, 0, 0,
		(tr + tl) / w, (tu + td) / h, (far + near) / (near - far), -1,
		0, 0, 2 * far * near / (near - far), 0,
	} };
}

#endif
