pyquaternion: ValueError: Неожиданное количество элементов в последовательности - PullRequest
0 голосов
/ 09 мая 2019

Используя модуль pyquaternion Я хочу получить кватернионное представление матрицы вращения 3x3. При выполнении возвращается ValueError: Unexpected number of elements in sequence. Got: 3, Expected: 4.. Согласно документации, создание кватерниона с матрицей 3х3 должно работать, или, может быть, я что-то неправильно понимаю.

Чтобы проверить это, я нашел этот метод для генерации матрицы случайного вращения. Как указано выше, я получаю только это сообщение об ошибке.

import numpy as np
from pyquaternion import Quaternion

def rand_rotation_matrix(deflection=1.0, randnums=None):
    """
    Creates a random rotation matrix.

    deflection: the magnitude of the rotation. For 0, no rotation; for 1, competely random
    rotation. Small deflection => small perturbation.
    randnums: 3 random numbers in the range [0, 1]. If `None`, they will be auto-generated.
    """
    # from http://www.realtimerendering.com/resources/GraphicsGems/gemsiii/rand_rotation.c

    if randnums is None:
        randnums = np.random.uniform(size=(3,))

    theta, phi, z = randnums

    theta = theta * 2.0*deflection*np.pi  # Rotation about the pole (Z).
    phi = phi * 2.0*np.pi  # For direction of pole deflection.
    z = z * 2.0*deflection  # For magnitude of pole deflection.

    # Compute a vector V used for distributing points over the sphere
    # via the reflection I - V Transpose(V).  This formulation of V
    # will guarantee that if x[1] and x[2] are uniformly distributed,
    # the reflected points will be uniform on the sphere.  Note that V
    # has length sqrt(2) to eliminate the 2 in the Householder matrix.

    r = np.sqrt(z)
    Vx, Vy, Vz = V = (
        np.sin(phi) * r,
        np.cos(phi) * r,
        np.sqrt(2.0 - z)
        )

    st = np.sin(theta)
    ct = np.cos(theta)

    R = np.array(((ct, st, 0), (-st, ct, 0), (0, 0, 1)))

    # Construct the rotation matrix  ( V Transpose(V) - I ) R.

    M = (np.outer(V, V) - np.eye(3)).dot(R)
    return M

rotation1 = rand_rotation_matrix()
rotation2 = rand_rotation_matrix()

print(Quaternion(rotation1.dot(rotation2.T)))

1 Ответ

1 голос
/ 09 мая 2019

Согласно разделу Инициализация объекта в документации (прокрутить вниз), для инициализации из матрицы вращения необходимо использовать аргумент ключевого слова matrix=, например, Quaternion(matrix=R).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...