def quaternion_to_transform_matrix(quaternion, translation=np.zeros(3)):
    """Convert quaternion to homogenous 4x4 transform matrix.

    Parameters
    ----------
    quaternion : 4x1 array of float
        Quaternion describing rotation after translation.
    translation : 3x1 array of float, optional
        Translation to be performed before rotation.
    """
    q = np.array(quaternion, dtype=np.float64, copy=True)
    n = np.linalg.norm(q)
    if n < 1e-12:
        return np.identity(4, dtype=np.float64)
    q /= n
    q = 2 * np.outer(q, q)
    # fmt: off
    transform_mat = np.array(
        [[1.-q[2, 2]-q[3, 3],    q[1, 2]-q[3, 0],    q[1, 3]+q[2, 0], 0.],
         [   q[1, 2]+q[3, 0], 1.-q[1, 1]-q[3, 3],    q[2, 3]-q[1, 0], 0.],
         [   q[1, 3]-q[2, 0],    q[2, 3]+q[1, 0], 1.-q[1, 1]-q[2, 2], 0.],
         [                0.,                 0.,                 0., 1.]],
        dtype=np.float64
    )
    # fmt: on
    transform_mat[:3, 3] = translation
    return transform_mat