Maths - Quaternion Interpolation (SLERP) - forum discussion

By: Nobody/Anonymous - nobody
file What exactly is a SLERP?  
2006-01-14 21:18

Case : We have 2 quaternions. 
 
Event: Someone mentions a SLERP as a means of extrapolating between the two / gettng from one to the other really smoothly, like. This person then takes some more THC and lapses into ignorant silence. 
 
Simply put, what does a SLERP do, why does it exist, and any simple practical knock-up code using GL, GLU or GLUT would be great to see;) 
 
Cheers, 
 
Anthony (Prospero)

By: Martin Baker - martinbakerProject Admin
file RE: What exactly is a SLERP?  
2006-01-15 11:03

Anthony, 
Yes, I take it your referring to this page: 
https://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/ 
 
Given two orientations (represented by two quaternoins) then this will allow interpolation between them. Provided the orientations are not 180 degrees apart then there will always be a shortest distance between them and we could divide this up into a number of intermediate orientations. 
 
To take a simpler but different example. Imagine two points on the spherical surface of the earth, we could work out the shortest route between these points, which would be an arc, and work out some equally spaced stopping points along the route. 
 
The functions built into OpenGL tend to use matrices rather than quaternions or axis angle, I dont know a way to do SLERP with matrices rather than quaternions. If anyone does let me know and Ill include it on the web page.  
 
Martin 

By: Nobody/Anonymous - nobody
file RE: What exactly is a SLERP?  
2006-01-15 18:10

Hi Martin, 
 
I should be in bed but I just have to ask a few more questions - especially regarding the Shoemkakr calculation: 
 
1) How is one expected to know what the value of theta is before one does the slerp. After all, all one's got is two sets of four numbers which men noehting to anyone in the real world:. 
 
2) 

By: Nobody/Anonymous - nobody
file RE: What exactly is a SLERP?  
2006-01-15 18:11

Hi Martin, 
 
I should be in bed but I just have to ask a few more questions - especially regarding the Shoemake calculation: 
 
1) How is one expected to know what the value of theta is before one does the slerp? After all, all one's got is two sets of four numbers which meann nothing to anyone in the real world. 
 
2) If we take the first part of the numerator of his equation: " 
 
2) 

By: Nobody/Anonymous - nobody
file RE: What exactly is a SLERP?  
2006-01-15 18:16

Hi Martin,  
 
I should be in bed but I just have to ask a few more questions - especially regarding the Shoemake calculation:  
 
1) How is one expected to know what the value of theta is before one does the slerp? After all, all one's got is two sets of four numbers which meann nothing to anyone in the real world.  
 
2) If we take the first part of the numerator of his equation: "qasin(1-t)theta) - this looks to me like mmultiplying a quat by a scalar. I am missing something in the notation here... 
 
Sorry about the earlier repeat posts. I accidentelly fell asleep as I was writing this;) 
 
Cheers, Anthony. 
 
PS: Thanks for that Quat -> Axis/Angle stuff. Worked a treat in GLUT once I'd read the GLUT manual 100 times;) 

By: Martin Baker - martinbakerProject Admin
file RE: What exactly is a SLERP?  
2006-01-16 07:23

"How is one expected to know what the value of theta is before one does the slerp?"
this page: 
https://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/ 
has the formula: 
cos(theta/2) = qa.w*qb.w + qa.x*qb.x + qa.y*qb.y+ qa.z*qb.z 
 
"all one's got is two sets of four numbers which mean nothing to anyone in the real world."
If the quaternion does not mean much to you its very easy to relate to axis angle which is more intuitive 
https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/ 
 
"If we take the first part of the numerator of his equation: "qasin(1-t)theta) - this looks to me like mmultiplying a quat by a scalar. I am missing something in the notation here... 
I dont claim to fully understand Shoemake myself, but it looks like multiplying a quaternion by a scalar to me also. 
 
Martin

 

 

By: Nobody/Anonymous - nobody
file RE: What exactly is a SLERP?  
2006-01-18 19:00

Try as as I might (and Martin, I ALMOST have SLERP working with the info you've kindly posted), nothing seems quite right. Surely there is someone on the board who could write some pseudo-code (bugger structures - keep it simple - and so on) to show how this works, and which users of any language would find easy to implement. 
 

 
Cheers, 
 
Antony (Prospero)

By: Martin Baker - martinbakerProject Admin
file RE: What exactly is a SLERP?  
2006-01-16 07:23

"How is one expected to know what the value of theta is before one does the slerp?2 
this page: 
https://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/ 
has the formula: 
cos(theta/2) = qa.w*qb.w + qa.x*qb.x + qa.y*qb.y+ qa.z*qb.z 
 
"all one's got is two sets of four numbers which mean nothing to anyone in the real world." 
If the quaternion does not mean much to you its very easy to relate to axis angle which is more intuitive 
https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/ 
 
"If we take the first part of the numerator of his equation: "qasin(1-t)theta) - this looks to me like mmultiplying a quat by a scalar. I am missing something in the notation here... 
I dont claim to fully understand Shoemake myself, but it looks like multiplying a quaternion by a scalar to me also. 
 
Martin

By: Nobody/Anonymous - nobody
file RE: What exactly is a SLERP?  
2006-01-20 03:02

Hi all. Regarding some example "generic" SLERP code: 
 
 
---------- 
// First quaternion. 
qa.w = 1.0; 
qa.x = 0.0; 
qa.y = 0.0; 
qa.z = 0.0; 
 
// Second quaternion. 
qb.w = 0.7071067811; 
qb.x = 0.0; 
qb.y = 0.0; 
qb.z = 0.7071067811; 
 
// Calculate "angle beteen them". 
theta = acos(qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z) * 2.0; 
 
// Fairy dust. 
temp_a = sin((1 - t) * theta); 
temp_b = sin(t * theta); 
temp_c = sin(theta); 
 
// Quaternion at any time t, when t is specified between 0.0 and 1.0.  
qm.w = (qa.w * temp_a + qb.w * temp_b) / temp_c; 
qm.x = (qa.x * temp_a + qb.x * temp_b) / temp_c; 
qm.y = (qa.y * temp_a + qb.y * temp_b) / temp_c;  
qm.z = (qa.z * temp_a + qb.z * temp_b) / temp_c; 
---------- 
 
 
This is my interpretation of the equation given on Martin's web page. I have NO IDEA if it's correct. Any input and corrections would be GREATLY appreciated, chaps. 
 
Anthony (Prospero)

By: Nobody/Anonymous - nobody
file RE: What exactly is a SLERP?  
2006-01-22 03:30

My SLERPY maths seeem to be working (sorta) under GLUT - but there seems to be a problem with t. Sticking rigidly to the formulae on Martin's ite, I always start with t = 0 and have it rise to 1.0. This can mean that *what I actually SEE* is my SLERP occuring backwards and only as t nears 1.0 - i.e. PORKED. 
 
Surely someone must have probed this thorny issue before? 
 
Anthony (Prospero)

By: Martin Baker - martinbakerProject Admin
file RE: What exactly is a SLERP?  
2006-01-22 08:49

Anthony, 
 
As I say, Im not an expert on SLERP, but it looks OK to me. 
 
if t=0 then, 
sin(t*theta) = 0 
sin((1-t)*theta) = sin(theta) 
qm=qa 
if t=1 then, 
sin(t*theta) = sin(theta) 
sin((1-t)*theta) = 0 
qm=qb 
 
Martin 

By: Nobody/Anonymous - nobody
file RE: What exactly is a SLERP?  
2006-01-22 15:51

Hi Martin, 
 
How does this code fit in, exactly? I'm too dumb... 
 
Anthony (Prospero) 
 
Here's my entire listing: 
 
 
// SLERP Preparation. 
qa.w = 1.0; 
qa.x = 0.0; 
qa.y = 0.0; 
qa.z = 0.0; 
ep = sqrt(qa.w * qa.w + qa.x * qa.x + qa.y * qa.y + qa.z * qa.z); 
qa.w = qa.w / ep; 
qa.x = qa.x / ep; 
qa.y = qa.y / ep; 
qa.z = qa.x / ep; 
 
qb.w = "whatever"; 
qb.x = "whatever"; 
qb.y = "whatever"; 
qb.z = "whatever"; 
ep = sqrt(qb.w * qb.w + qb.x * qb.x + qb.y * qb.y + qb.z * qb.z); 
qb.w = qb.w / ep; 
qb.x = qb.x / ep; 
qb.y = qb.y / ep; 
qb.z = qb.z / ep; 
 
qtheta = acos(qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z) * 2.0;  
 
if(0 >= gt <= 1) 

gt = gt + gdt / 20; 

if(gt > 1.0) 

gt = 1.0; 

if(gt < 0.0) 

gt = 0.0; 

 
// SLERP Quat Cube using GLUT. 
glLineWidth(2.0); 
glColor3f(0.0, 1.0, 0.0); 
glPushMatrix(); 
glTranslatef(gpos_eus.x, gpos_eus.y, gpos_eus.z); 
 
temp_a = sin((1 - gt) * qtheta); 
temp_b = sin(gt * qtheta); 
temp_c = sin(qtheta); 
 
qm.w = (qa.w * temp_a + qb.w * temp_b) / temp_c; 
qm.x = (qa.x * temp_a + qb.x * temp_b) / temp_c; 
qm.y = (qa.y * temp_a + qb.y * temp_b) / temp_c;  
qm.z = (qa.z * temp_a + qb.z * temp_b) / temp_c; 
 
gsc = sqrt(qm.x * qm.x + qm.y * qm.y + qm.z * qm.z); 
glRotatef(2.0 * acos(qm.w) * RADTODEG, qm.y / gsc, -qm.z / gsc, -qm.x / gsc); 
glutWireCube(0.4); 
glPopMatrix(); 
 
 
Help!;) It ALMOST works...

By: Nobody/Anonymous - nobody
file RE: What exactly is a SLERP?  
2006-01-22 20:34

I see my error here... thinking that t was somehow &quot;time&quot; rather than just a scalar, and hence I tried to control it like a shuttle on a VCR machine. But in your code snippet Martin, you seem to be able to be testing it - I understand the logic, just not how to do it. 
 
Anthony (Prospero)

By: Martin Baker - martinbakerProject Admin
file RE: What exactly is a SLERP?  
2006-01-23 02:32

I suspect that that: 
if(0 >= gt <= 1) { gt = gt + gdt / 20;} 
would not work as intended anyway. If you want to show the shape rotating between the two quaternions why not remove the above and put the procure in a for loop like this: 
 
for (gt=0;gt<=1;gt=gt+0.1){ 
SLERP procedure 

 
Martin

By: Nobody/Anonymous - nobody
file RE: Whatexactly is a SLERP?  
2006-01-23 22:12

Hi Martin, 
 
Still working on this. 
 
Do you mean that for each recursion of the SLERP, qa and qb should not be messed about with - i.e. their definitions can actually reside within the slerp loop unchanged. 
 
So qm is the important thing to calculate each loop. 
 
Heheh, hoping we're almost of here, 
 
Anthony (Prospero)

By: Martin Baker - martinbakerProject Admin
file RE: What exactly is a SLERP?  
2006-01-24 03:13

If you want to blend between two fixed orientations then I think it makes sense to setup qa and qb before the loop and display qm each time through the loop. 
 
Martin

By: Nobody/Anonymous - nobody
file RE: What exactly is a SLERP?  
2006-01-24 19:18

Hi Martin, 
 
Well, it all seems to work now, with a couple of caveats. For anyone wanting to know the code, here is a bare-bones &quot;pseudo-code&quot; C version: 
 
 
---------- 
gt = 0; 
 
:BEGIN LOOP HERE: 
 
// SLERP Quat Box. 
qa.w = 1.0; 
qa.x = 0.0; 
qa.y = 0.0; 
qa.z = 0.0; 
ep = sqrt(qa.w * qa.w + qa.x * qa.x + qa.y * qa.y + qa.z * qa.z); 
qa.w = qa.w / ep; 
qa.x = qa.x / ep; 
qa.y = qa.y / ep; 
qa.z = qa.x / ep; 
 
qb.w = <choose a second valid quaternion>; 
qb.x = <choose a second valid quaternion>; 
qb.y = <choose a second valid quaternion>; 
qb.z = <choose a second valid quaternion>; 
ep = sqrt(qb.w * qb.w + qb.x * qb.x + qb.y * qb.y + qb.z * qb.z); 
qb.w = qb.w / ep; 
qb.x = qb.x / ep; 
qb.y = qb.y / ep; 
qb.z = qb.z / ep; 
 
qtheta = acos(qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z) * 2.0;  
 
// OpenGL Stuff.  
glDisable(GL_CULL_FACE); 
glLineWidth(2.0); 
glColor4f(1.0, 0.5, 0.0, 0.5); 
glPushMatrix(); 
glTranslatef(gpos_eus.x, gpos_eus.y, gpos_eus.z); 
 
temp_a = sin((1 - gt) * qtheta); 
temp_b = sin(gt * qtheta); 
temp_c = sin(qtheta); 
 
qm.w = (qa.w * temp_a + qb.w * temp_b) / temp_c; 
qm.x = (qa.x * temp_a + qb.x * temp_b) / temp_c; 
qm.y = (qa.y * temp_a + qb.y * temp_b) / temp_c;  
qm.z = (qa.z * temp_a + qb.z * temp_b) / temp_c; 
ep = sqrt(qm.w * qm.w + qm.x * qm.x + qm.y * qm.y + qm.z * qm.z); 
qm.w = qm.w / ep; 
qm.x = qm.x / ep; 
qm.y = qm.y / ep; 
qm.z = qm.z / ep; 
 
gsc = sqrt(qm.x * qm.x + qm.y * qm.y + qm.z * qm.z); 
glRotatef(2.0 * acos(qm.w) * RADTODEG, qm.y / gsc, -qm.z / gsc, -qm.x / gsc); 
glutSolidCube(0.4); 
glPopMatrix(); 
 
glEnable(GL_CULL_FACE); 
 
gt = gt + gdt / qtheta; 
if(gt >= 1.0) gt = 1.0; 
 
:END LOOP HERE: 
---------- 
 
 
One issue becomes clear from playing with this example, however. Huge SLERP steps (either measured via the magnitude of qtheta, or simply by trying to get to turn more than 90 degrees around one particular axis in one step) can cause problems. Just how stable it is, I wonder? Oh, and be damn careful about feeding it VALID quaternions, or it vomits. 
 
Questions welcome and comments appreciated, 
 
Cheers, 
 
Prospero (Anthony)


metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

 

cover us uk de jp fr ca Quaternions and Rotation Sequences.

Terminology and Notation

Specific to this page here:

 

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2017 Martin John Baker - All rights reserved - privacy policy.