模长,归一化,点乘
整理四元数的 dot product、length 和 normalization 公式。
模长,归一化,点乘
点乘
点乘可以计算两个四元数之间的夹角,但是夹角对于四元数来说不像向量那么有用
如果四元数是单位四元数,那么可以化简为
模长
四元数的模长并不那么有用,它一般只是用来检测四元数是否还是单位四元数,以便执行归一化
float LengthSq(Quaternion quat) {
return quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;
}
float Length(Quaternion quat) {
float lengthSq = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;
return sqrt(lengthSq);
}归一化
要标准化,只需将每个分量除以四元数的长度即可,我们用四元数表示纯旋转时总是使用单位四元数,所以归一化很重要
float Normalize(Quaternion quat) {
float lengthSq = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;
assert(lengthAq != 0);
float invLen = 1.0f / sqrt(lengthSq);
return Quaternion(
quat.x * invLen,
quat.y * invLen,
quat.z * invLen,
quat.w * intLen
)
}