def _get_change_basis_xform(center, up, normal):
    """Return a matrix that changes from the given basis to the x-y-z
    basis. Basically, this 'straightens' out the plane described by
    the basis. The 'right' axis is calculated from the up and normal."""
    normal = normal.normalized()
    right = up.cross(normal).normalize()
    up    = normal.cross(right).normalize()

    xform = Mat4.new_translate(*center)
    xform *= Mat4.new_change_basis(right, up, normal, center).inverse()
    return xform
def _get_camera_xform(image):
    """Return the xform that maps world coordinates to image
    coordinates. Uses the Iphone's lens characteristics."""
    film_size    = 6.35 # Iphone sensor size, in mm
    focal_length = 3.85 # Iphone focal length, in mm
    width, height = image.size
    aspect = float(width) / height
    fov_y = 2 * math.atan2(film_size/aspect, 2 * focal_length)
    xform = Mat4.new_translate(width/2.0,  height/2.0, 0)
    xform.scale(-width/2.0, -height/2.0, 1)
    xform *= Mat4.new_perspective(fov_y, aspect, 1, 10)
    return xform