# Uniform Random Orientation

When generating random positions and orientations that are to be used to test an image registration algorithm, it is very important that the distribution of the orientations is truly uniform, i.e. all orientations should be equally represented by the distribution. If this is not the case, then some orientations will be under-represented in the simulation while others are over-represented.

## Concepts

To start, we have to choose a way to express the orientation. The most convenient is to express the orientation as a rotation about a unit vector, since it is easy to convert an orientation that is expressed in this way into either a quaternion or into three Euler angles. In order for this vector/rotation representation to provide uniform orientations, two things must be true:

1. The vector orientation must be such that, if the vector is placed in a unit sphere, it must be equally likely to point to any location on the surface of one hemisphere of the unit sphere.
2. The rotation around the vector must be uniformly distributed over the range $[-pi,+ pi]$.

The reason that we only need one hemisphere is that any orientation can be equally represented either as a rotation about a vector, or as a rotation in the opposite direction about a vector that points in the opposite direction. In other words, since the rotation around the vector to be positive or negative, we don’t need vectors that point in the opposite direction.

## Specifics, With Rotation Angles

$Uniform Orientation$

The diagram on the right shows both the unit sphere and a set of roll, pitch, yaw $(phi, theta, psi)$ angles that represent an orientation. The angles $phi$ and $theta$ are used to set the orientation of the vector, while $psi$ is the rotation around the vector (specifically, it is around the new $z$ axis after the other two rotations have taken place).

The probability of any particular vector orientation must be uniformly distributed, in other words, the probability of the vector intersecting a unit area $dA$ at a point on the sphere must be the same everywhere on the sphere. In order to achieve this, the probability of the vector intersecting a ring at elevation $theta$ (see diagram) must be proportional to the circumference of that ring. Noting that the circumference is proportional to the radius $r = cos theta$ of the ring, we can show that the probability must be proportional to $cos theta$.

In other words, we want $cos theta$, rather than $theta$ itself, to follow a uniform distribution. If we were to simply generate $theta$ as a uniform random number, then the probability of orientations near the pole would be much higher than near the equator.

So we can state the probability distributions for angles $phi$ and $theta$ as follows:

$p(phi) = a qquadqquad mbox{where} qquad phi in [0,2pi]$

$p(theta) = b,cos(theta), quad mbox{where} qquad theta in [0,pi/2]$

The actual magnitude of the rotation is determined solely by the third angle $psi$, so if we want a uniform distribution of rotations up to magnitude $psi_{max}$.

$p(psi) = c qquadqquad mbox{where} qquad psi in [-psi_{max},psi_{max}]$

The random number generators that we use will generate uniform random number in the range [0,1] which we must then use to generate the probability distributions shown above. We will need three such random numbers: $x, y$ and $z$.

Generating random values for $phi$ and $psi$ is easy since these are uniformly distributed, we can just use the following:

$phi = 2pi, x qquadqquadqquad mbox{where} qquad x in [0,1]$

$psi = psi_{max}, (2, z - 1) qquad mbox{where} qquad z in [0,1]$

Generating random values for $theta$ requires what is called the "inverse transform method," which states that the relation between a probability distribution $p(theta)$ and a uniform random variable $y$ is given by the following:

$int_{theta_{min}}^{theta} p(theta') , dtheta' = y qquad mbox{where} qquad y in [0,1]$

All we have to do is solve the above equation for $theta$, and we have the answer we need. Substituting our probability function gives

$int_{0}^{theta} b cos theta' , dtheta' = y$

where $b$ is just a constant that ensures that the integral of the probability over all values is unity, i.e.

$b = frac{1}{int_{0}^{pi/2} cos theta' , dtheta'} = frac{1}{sin(pi/2) - sin(0)} = 1$

Substituting $b = 1$ and solving the integral, we get

$sintheta = y,$

Solving for $theta$, we have our final answer:

$theta = arcsin y,$

## Final Result

To generate a uniformly random orientation, use the orientation angles that are specified above and use the following equations to generate the angles from three uniform random numbers $x$, $y$ and $z$:

$phi = 2pi x,$

$theta = arcsin(y),$

$psi = psi_{max}, (2, z - 1),$

Note that $psi$ does not necessarily have to follow a uniform distribution $[-psi_{max}, +psi_{max}]$, we could also use a gaussian distribution or whatever other distribution we may desire.

Given the orientation angles, the next step is generate a rotation matrix:

$R = R_z,R_y,R_x$

where $R_x$ is a rotation by $phi$ about the $x$ axis, $R_y$ is a rotation by $theta$ about the $y$ axis, and $R_z$ is a rotation by $psi$ about the $z$ axis.

If you use a different convention for expressing orientations, then you can perform a similar derivation by utilizing the concept of uniform coverage of a unit sphere. The answer you get should be very similar to what is shown here, where two of the angles follow uniform distributions and one angle follows either a sin or cos distribution, depending on how the rotations are defined.