So I was implementing my own Quaternion Class and I ran into a problem I wasn't expecting when calling one of my operator (static) functions from a member function. See function rotateByVector()
;
class Quaternion
{
public:
double x, y, z, w;
Quaternion(double w, double xi, double yj, double zk)
{
w = w;
x = xi;
y = yj;
z = zk;
}
Quaternion operator +(Quaternion q1, Quaternion q2)
{
return Quaternion(q1.w + q2.w, q1.x + q2.x, q1.y + q2.y, q1.z + q2.z);
}
Quaternion operator*(Quaternion q1, Quaternion q2)
{
return Quaternion(q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z
, q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y
, q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z
, q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x);
}
void rotateByVector(double x1, double y1, double z1, double[3] res)
{
// want to calculate Hamilton Product
// res_quaternion = this_quaternion * quaternion_vector(0, x1, y1, z1) * this_conj;
// return double[3] {res_quaternion.x, res_quaternion.y, res_quaternion.z}
Quaternion q = Quaternion(0.0f, x1, y1, z1);
Quaternion r = (*this).operator*() q; //doesn't like it
Quaternion r = this->operator*(q); //doesn't like it either
...
r = r * this.conj();
res[0] = r.x;
res[1] = r.y;
res[2] = r.z;
}
}
How should I implement the multiplication of this quaternion
times the one from the parameters times the conjugate of this quaternion
I know I know close, but I am definitely missing something.