import numpy as np
from transforms3d import quaternions
[docs]def random_point_on_unit_sphere():
"""Point randomly drawn from a uniform distribution on the unit sphere.
Randomly picking a point on the unit sphere requires choosing the point
so that each solid angle on the sphere is equally likely.
Set `np.random.seed(seed)` for reproducible results.
Returns:
np.ndarray: a 3d array with the x,y,z coordinates of a random point"""
theta = 2.0*np.pi*np.random.rand()
phi = np.arccos(1 - 2.0*np.random.rand())
return np.array([np.sin(phi)*np.cos(theta), np.sin(phi)*np.sin(theta), np.cos(phi)])
[docs]def random_quaternion():
"""Random uniformly distributed quaternion
A quaternion defines an 3d axis of rotation and a corresponding angle
of rotation. A random quaternion is drawn by first drawing a random
rotation axis from the uniform random sphere and a uniform angle. Random
quaternions are used for random rotation operations on arbitrary geometries.
Returns:
transforms3d.Quaternion: a random quaternion from a uniform distribution
"""
return quaternions.axangle2quat(random_point_on_unit_sphere(),
2.0*np.pi*np.random.randn())