Open1

Yaw, Pitch, Roll の値からベクトルを計算、あるいは、Yaw, Pitch のみからベクトルを計算

PINTOPINTO
import numpy as np

def rotation_matrix(yaw, pitch, roll):
    # Yaw
    R_yaw = np.array([
        [np.cos(yaw), -np.sin(yaw), 0],
        [np.sin(yaw), np.cos(yaw), 0],
        [0, 0, 1]
    ])

    # Pitch
    R_pitch = np.array([
        [np.cos(pitch), 0, np.sin(pitch)],
        [0, 1, 0],
        [-np.sin(pitch), 0, np.cos(pitch)]
    ])

    # Roll
    R_roll = np.array([
        [1, 0, 0],
        [0, np.cos(roll), -np.sin(roll)],
        [0, np.sin(roll), np.cos(roll)]
    ])

    R = np.dot(R_yaw, np.dot(R_pitch, R_roll))
    return R

def get_vector_from_yaw_pitch_roll(yaw, pitch, roll):
    R = rotation_matrix(yaw, pitch, roll)
    basic_vector = np.array([1, 0, 0])  # X-axis as an example
    rotated_vector = np.dot(R, basic_vector)
    return rotated_vector

# 例
yaw = np.radians(30)    # 30 degrees to radians
pitch = np.radians(45)  # 45 degrees to radians
roll = np.radians(60)   # 60 degrees to radians

vector = get_vector_from_yaw_pitch_roll(yaw, pitch, roll)
print(vector)

"""
[ 0.61237244  0.35355339 -0.70710678]
"""


def get_vector_from_yaw_pitch(yaw, pitch):
    x = np.cos(pitch) * np.cos(yaw)
    y = np.cos(pitch) * np.sin(yaw)
    z = np.sin(pitch)
    return np.array([x, y, z])

# 例
yaw = np.radians(30)    # 30 degrees to radians
pitch = np.radians(45)  # 45 degrees to radians

vector = get_vector_from_yaw_pitch(yaw, pitch)
print(vector)

"""
[0.61237244 0.35355339 0.70710678]
"""