Interpolating rotations(slerp) using Eigen
Asked Answered
P

3

6

I am trying to slerp between 2 quaternions using Eigen(thought would be the easiest).

I found two different examples

One,

for(int i = 0; i < list.size(); i++)
{
  Matrix3f m;
  Quaternion<float,0> q1 = m.toRotationMatrix();

  Quaternion<float,0> q3(q1.slerp(1,q2));
  m_node->Rotation(q3.toRotationMatrix());
}

Second,

Vec3 slerp(const Vec3& a, const Vec3& b, const Real& t)
{
 Quaternionf qa;
 Quaternionf qb;
 qa = Quaternionf::Identity();
 qb.setFromTwoVectors(a,b); 
  return (qa.slerp(t,qb)) * a; 
 }

I cant really say which one is correct. There is not many documentation about this. Can anyone tell me if I should use a different library? or how can I slerp using eigen.

Pricket answered 20/10, 2013 at 10:13 Comment(0)
C
15

Doing a SLERP between two quaternions is just a matter of calling the slerp method:

Quaterniond qa, qb, qres;
// initialize qa, qb;
qres = qa.slerp(t, qb);

where t is your interpolation parameter.

Curtain answered 20/10, 2013 at 11:59 Comment(0)
W
1

Use the second variant.

Both code snippets implement a SLERP, however the first one does something with elements in a list, which your snippet doesn't show. Also the second variant is the computationally more efficient one, as it doesn't take a detour over a rotation matrix.

Wormwood answered 20/10, 2013 at 10:54 Comment(2)
I have more than one quaternion so I was keeping them in a list. I want to move from q1 to q2 and when I come to q2 I want to move to q3Pricket
@james456: This is a classical segmented interpolation problem. You store your quaternions in a list of pairs t, q where t designates the starting "time" for the associated quaternion. Then you map the range from t_{n} … t_{n+1} to the range 0 … 1 and multiply that with the given q.Wormwood
Z
0

I think GLM is the best choice in an openGL application because the GLM functions are the same as in GLSL.

The glm slerp takes argouments as the mix function (which is the glm function for lerp). The mix function gives you, for a call like

 result = mix (first, second, alpha); // result = (1-alpha)*first + alpha*second;

The alpha parameter works the same way for slerp, so a typical example of using slerp to interpolate between quaternion over time can be

glm::quat interpolated_quaternion; //the result
std::vector<glm::quat> my_quaternion; //vector of quaternions, one per frame.
float frame_time; //the time passed since the previous frame
int frame; //the actual frame
interpolated_quaternion = slerp( my_quaternion[frame],my_quaternion[frame+1],frame_time);
Zootechnics answered 20/10, 2013 at 19:42 Comment(3)
I tried to use slerp glm. But couldnt figure out how to use it. Can you give me an example?Pricket
When I get the interpolated quaternion, does the function move the Camera itself or do I need to write a function for itPricket
@Pricket You need to move the camera yourself.Zootechnics

© 2022 - 2024 — McMap. All rights reserved.