四元数转欧拉角代码

static inline void QuaternionToAngleAxis(const float * quaternion,
                                         float* angle_axis) {
  const float q1 = quaternion[1];
  const float q2 = quaternion[2];
  const float q3 = quaternion[3];
  const float sin_squared_theta = q1 * q1 + q2 * q2 + q3 * q3;

  // For quaternions representing non-zero rotation, the conversion
  // is numerically stable.
  if (sin_squared_theta > 0.0f) {
    const float sin_theta = sqrt(sin_squared_theta);
    const float cos_theta = quaternion[0];

  
    const float two_theta =
        2.0 * ((cos_theta < 0.0)
               ? atan2(-sin_theta, -cos_theta)
               : atan2(sin_theta, cos_theta));
    const float k = two_theta / sin_theta;
    angle_axis[0] = q1 * k;
    angle_axis[1] = q2 * k;
    angle_axis[2] = q3 * k;
  } else {
    // For zero rotation, sqrt() will produce NaN in the derivative since
    // the argument is zero. By approximating with a Taylor series,
    // and truncating at one term, the value and first derivatives will be
    // computed correctly when Jets are used.
    angle_axis[0] = q1 * 2.0f;
    angle_axis[1] = q2 * 2.0f;
    angle_axis[2] = q3 * 2.0f;
  }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值