四元数转旋转矩阵

目录

[gsplat 四元数转旋转矩阵等同代码实现](#gsplat 四元数转旋转矩阵等同代码实现)

[scipy 四元数转旋转矩阵替换代码](#scipy 四元数转旋转矩阵替换代码)


gsplat 四元数转旋转矩阵等同代码实现

python 复制代码
import torch
import torch.nn.functional as F


def quat_act(x: torch.Tensor) -> torch.Tensor:
    return x / x.norm(dim=-1, keepdim=True)

def normalized_quat_to_rotmat(quat: torch.Tensor) -> torch.Tensor:
    # 源码来自: from gsplat.utils import normalized_quat_to_rotmat
    """Convert normalized quaternion to rotation matrix.

    Args:
        quat: Normalized quaternion in wxyz convension. (..., 4)

    Returns:
        Rotation matrix (..., 3, 3)
    """
    assert quat.shape[-1] == 4, quat.shape
    w, x, y, z = torch.unbind(quat, dim=-1)
    mat = torch.stack(
        [
            1 - 2 * (y**2 + z**2),
            2 * (x * y - w * z),
            2 * (x * z + w * y),
            2 * (x * y + w * z),
            1 - 2 * (x**2 + z**2),
            2 * (y * z - w * x),
            2 * (x * z - w * y),
            2 * (y * z + w * x),
            1 - 2 * (x**2 + y**2),
        ],
        dim=-1,
    )
    return mat.reshape(quat.shape[:-1] + (3, 3))

def quat2mat(quat):
    qw, qx, qy, qz = torch.unbind(quat, dim=-1)  # 原为wxyz
    quat_xyzw = torch.stack([qx, qy, qz, qw], dim=-1)  # 转为xyzw顺序
    # 后续代码保持原逻辑
    qx, qy, qz, qw = torch.unbind(quat_xyzw, dim=-1)

    # 计算旋转矩阵
    R00 = 1 - 2 * (qy ** 2 + qz ** 2)
    R01 = 2 * (qx * qy - qw * qz)
    R02 = 2 * (qx * qz + qw * qy)

    R10 = 2 * (qx * qy + qw * qz)
    R11 = 1 - 2 * (qx ** 2 + qz ** 2)
    R12 = 2 * (qy * qz - qw * qx)

    R20 = 2 * (qx * qz - qw * qy)
    R21 = 2 * (qy * qz + qw * qx)
    R22 = 1 - 2 * (qx ** 2 + qy ** 2)

    # 将旋转矩阵堆叠在一起
    matrix = torch.stack([R00, R01, R02, R10, R11, R12, R20, R21, R22], dim=-1)

    # 变换为 3x3 的矩阵
    return matrix.view(-1, 3, 3)

x=torch.range(0,3*4-1)

x=x.reshape(-1,4)

print(x)

# 调用 quat_act 函数进行归一化
normalized_x = quat_act(x)

aa=F.normalize(x, dim=-1)

print('diff',(normalized_x-aa).sum(dim=-1))
print("\nNormalized x:")
print(aa)  # 应该返回一个全为 1 的张量


if 1:
    mat= normalized_quat_to_rotmat(aa)
    print(mat)

    mat2=quat2mat(aa)

    print('diff2', (mat2 - mat).sum(dim=-1))

scipy 四元数转旋转矩阵替换代码

python 复制代码
import torch
from scipy.spatial.transform import Rotation as R
import torch.nn.functional as F
def quat2mat_scipy(quat):
    # 从四元数中提取 qx, qy, qz, qw
    qx, qy, qz, qw = torch.unbind(quat, dim=-1)

    # 计算旋转矩阵
    R00 = 1 - 2 * (qy ** 2 + qz ** 2)
    R01 = 2 * (qx * qy - qw * qz)
    R02 = 2 * (qx * qz + qw * qy)

    R10 = 2 * (qx * qy + qw * qz)
    R11 = 1 - 2 * (qx ** 2 + qz ** 2)
    R12 = 2 * (qy * qz - qw * qx)

    R20 = 2 * (qx * qz - qw * qy)
    R21 = 2 * (qy * qz + qw * qx)
    R22 = 1 - 2 * (qx ** 2 + qy ** 2)

    # 将旋转矩阵堆叠在一起
    matrix = torch.stack([R00, R01, R02, R10, R11, R12, R20, R21, R22], dim=-1)

    # 变换为 3x3 的矩阵
    return matrix.view(-1, 3, 3)

if 1:
    x = torch.range(0, 3 * 4 - 1)

    x = x.reshape(-1, 4)

    aa = F.normalize(x, dim=-1)
    r = R.from_quat(aa.numpy())
    mat3= r.as_matrix()

    mat4=quat2mat_scipy(aa)
    print('diff3', (mat4.numpy() - mat3).sum(axis=-1))
相关推荐
bitbrowser1 小时前
哪些指纹浏览器支持模拟SSL指纹
网络·python·网络协议·ssl
说私域1 小时前
从“高密度占有”到“点状渗透”:论“开源AI智能名片链动2+1模式”在S2B2C商城小程序中的渠道革新
人工智能·小程序
limenga1022 小时前
TensorFlow Keras:快速搭建神经网络模型
人工智能·python·深度学习·神经网络·机器学习·tensorflow
心软小念2 小时前
用Python requests库玩转接口自动化测试!测试工程师的实战秘籍
java·开发语言·python
sanggou4 小时前
【Python爬虫】手把手教你从零开始写爬虫,小白也能轻松学会!(附完整源码)
开发语言·爬虫·python
KG_LLM图谱增强大模型4 小时前
Vgent:基于图的多模态检索推理增强生成框架GraphRAG,突破长视频理解瓶颈
大数据·人工智能·算法·大模型·知识图谱·多模态
AKAMAI4 小时前
企业如何平衡AI创新与风险
人工智能·云原生·云计算
geng_zhaoying4 小时前
在VPython中使用向量计算3D物体移动
python·3d·vpython
半tour费5 小时前
TextCNN-NPU移植与性能优化实战
python·深度学习·分类·cnn·华为云
普通网友5 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python