我的问题如下:我想在点A的框架中获得点B的球坐标。我可以访问点A的笛卡尔世界坐标,点B的笛卡尔世界坐标以及访问描述点A相对于世界旋转的四元数参考系(所谓的“方向四元数”或“姿态四元数”)。
到目前为止,这是我使用的那种代码(在OpenGL数学库中,glm):
#define U glm::dvec3(1.0, 0.0, 0.0)
#define V glm::dvec3(0.0, 1.0, 0.0)
#define W glm::dvec3(0.0, 0.0, 1.0)
glm::dvec3 cart_to_local(glm::dvec3 B_position) {
glm::dvec3 e1 = A_rotation_quaternion * U;
glm::dvec3 e2 = A_rotation_quaternion * V;
glm::dvec3 e3 = A_rotation_quaternion * W;
glm::mat3 transition_matrix(e1.x, e1.y, e1.z, e2.x, e2.y, e2.z, e3.x, e3.y, e3.z);
glm::mat3 reverse_transition_matrix = glm::inverse(transition_matrix);
glm::dvec3 v = B_position - A_position;
return reverse_transition_matrix * v;
}
然后我使用众所周知的公式(以acos,atan2等为特征)将笛卡尔坐标转换为球坐标。
它有效,但对我来说似乎涉及了太多步骤。是否可以通过使用我不知道的更多标准函数来使此代码“更小”?
我还没有测试过,但是您应该可以:
glm::dvec3 cart_to_local(glm::dvec3 B_position) {
glm::dvec3 v = B_position - A_position;
return glm::inverse(A_rotation_quaternion) * v;
}