旋转向量转bvh旋转角度

本文介绍了如何将旋转向量转换为四元素表示(如四元数),以及如何在BHV格式中处理旋转角度。内容包括Euler角的计算方法和从轴角转换到四元数的函数实现。
摘要由CSDN通过智能技术生成

目录

旋转向量转四元素

旋转向量转bvh旋转角度:

子节点从1  1 0 抓挠 0 1  1的例子:


答案不对

旋转向量转四元素

import numpy as np


def to_euler(x, order='zyx',face_front=False):
    
    q0 = x[...,0:1]
    q1 = x[...,1:2]
    q2 = x[...,2:3]
    q3 = x[...,3:4]
    
    if order == 'zyx':
    
        return np.concatenate([
            np.arctan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3)),
            np.arcsin((2 * (q0 * q2 - q3 * q1)).clip(-1,1)),
            np.arctan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2))], axis=-1)
            
    elif order == 'yzx':
    
        return np.concatenate([
            np.arctan2(2 * (q2 * q0 - q1 * q3),  q1 * q1 - q2 * q2 - q3 * q3 + q0 * q0),
            np.arcsin((2 * (q1 * q2 + q3 * q0)).clip(-1,1)), 
            np.arctan2(2 * (q1 * q0 - q2 * q3), -q1 * q1 + q2 * q2 - q3 * q3 + q0 * q0)],axis=-1)
            
    elif order == 'zxy':

        return np.concatenate([
            np.arctan2(2 * (q0 * q3 - q1 * q2), q0 * q0 - q1 * q1 + q2 * q2 - q3 * q3),
            np.arcsin((2 * (q0 * q1 + q2 * q3)).clip(-1,1)),
            np.arctan2(2 * (q0 * q2 - q1 * q3), q0 * q0 - q1 * q1 - q2 * q2 + q3 * q3)], axis=-1)

    elif order == 'yxz':
        
        data= np.concatenate([
            np.arctan2(2 * (q1 * q3 + q0 * q2), q0 * q0 - q1 * q1 - q2 * q2 + q3 * q3),
            np.arcsin((2 * (q0 * q1 - q2 * q3)).clip(-1,1)),
            np.arctan2(2 * (q1 * q2 + q0 * q3), q0 * q0 - q1 * q1 + q2 * q2 - q3 * q3)], axis=-1)

        data2 = from_euler(data)
        return data
        
    else:
        raise NotImplementedError('Cannot convert from ordering %s' % order)

def from_angle_axis(angle, axis):
    c = np.cos(angle / 2.0)[..., None]
    s = np.sin(angle / 2.0)[..., None]
    q = np.concatenate([c, s * axis], axis=-1)
    return q

def from_axis_angle(rots):
    angle = np.linalg.norm(rots, axis=-1)
    axis = rots / angle[..., None]
    result = np.nan_to_num(axis)
    return from_angle_axis(angle, result)

# 绕 z 轴旋转 90 度的旋转向量
rot_vector = np.array([0, 0, np.pi / 2])

# 转换为四元数
quat = from_axis_angle(rot_vector)
print(quat)

旋转向量转bvh旋转角度:

子节点从1  1 0 旋转到 0 1 1

答案是90 90 0 ,但是算出来不对。

    rots = rots_o.reshape(rots_o.shape[0], -1, 3)
    rots = quat.from_axis_angle(rots)

    positions[:, 0] = trans#*scale_v

    order = "zyx"  # rotation顺序
    # order = "zxy"  # rotation顺序
    rotations = np.degrees(quat.to_euler(rots, order=order))

子节点从1  1 0 抓挠 0 1  1的例子:

import numpy as np

def from_angle_axis(angle, axis):
    c = np.cos(angle / 2.0)[..., None]
    s = np.sin(angle / 2.0)[..., None]
    q = np.concatenate([c, s * axis], axis=-1)
    return q

def from_axis_angle(rots):
    angle = np.linalg.norm(rots, axis=-1)
    axis = rots / angle[..., None]
    result = np.nan_to_num(axis)
    return from_angle_axis(angle, result)

# 定义初始位置和目标位置
initial_position = np.array([1, 1, 0])
target_position = np.array([0, 1, 1])

# 计算旋转轴和旋转角度
rotation_axis = np.cross(initial_position, target_position)
rotation_angle = np.arccos(np.dot(initial_position, target_position) / (np.linalg.norm(initial_position) * np.linalg.norm(target_position)))

# 使用 from_axis_angle 函数计算四元数
quat = from_axis_angle(rotation_axis * rotation_angle)
print(f'四元数: {quat}')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值