Quaternion Quaternion::operator* (const Quaternion& other) const // quaternion multiplication { return Quaternion( w * other.x + x * other.w + y * other.z - z * other.y, w * other.y - x * other.z + y * other.w + z * other.x, w * other.z + x * other.y - y * other.x + z * other.w, w * other.w - x * other.x - y * other.y - z * other.z); }
Quaternion Quaternion::unitpow(float exponent) const // power of unit quaternion, use with caution { assert(std::abs(x*x + y*y + z*z + w*w-1)<0.01); float thetadiv2 = acos(w); float vec_factor = sin(exponent*thetadiv2)/sin(thetadiv2); return Quaternion( vec_factor * x, vec_factor * y, vec_factor * z, cos(exponent*thetadiv2)); }
Quaternion Quaternion::quatpow(float exponent) const // power { float vmagnitude_squared = x*x + y*y + z*z; float magnitudepow = pow(sqrt(w*w + vmagnitude_squared),exponent); float vmagnitude = sqrt(vmagnitude_squared); float thetadiv2 = atan2(w, vmagnitude); float vec_factor = magnitudepow * sin(exponent*thetadiv2)/vmagnitude; return Quaternion( vec_factor * x, vec_factor * y, vec_factor * z, magnitudepow * cos(exponent*thetadiv2)); }
Quaternion Quaternion::unit() const // unit quaternion { float magnitude = sqrt(w*w + x*x + y*y + z*z); return Quaternion(x/magnitude, y/magnitude, z/magnitude, w/magnitude); }
Quaternion Quaternion::inverse() const // inverse/conjugate quaternion { return Quaternion(-x,-y,-z,w); }