From fdeaa2423c582df8d9e9ae6b0e26d386b5531262 Mon Sep 17 00:00:00 2001 From: rodri Date: Fri, 27 Jan 2023 22:57:21 +0000 Subject: bring the slerp function, make ptinpoly public and clean some of the code. --- quaternion.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'quaternion.c') diff --git a/quaternion.c b/quaternion.c index 686c04e..ca44747 100644 --- a/quaternion.c +++ b/quaternion.c @@ -78,6 +78,24 @@ normq(Quaternion q) return sdivq(q, qlen(q)); } +/* + * based on the implementation from: + * + * Jonathan Blow, “Understanding Slerp, Then Not Using it”, + * The Inner Product, April 2004. + */ +Quaternion +slerp(Quaternion q, Quaternion r, double t) +{ + Quaternion v; + double θ, q·r; + + q·r = fclamp(dotq(q, r), -1, 1); /* stay within the domain of acos(2) */ + θ = acos(q·r)*t; + v = normq(subq(r, smulq(q, q·r))); /* v = r - (q·r)q / |v| */ + return addq(smulq(q, cos(θ)), smulq(v, sin(θ))); /* q cos(θ) + v sin(θ) */ +} + Point3 qrotate(Point3 p, Point3 axis, double θ) { -- cgit v1.2.3