mesh rendering - Serbipunk/notes GitHub Wiki

import numpy as np
from scipy.interpolate import griddata

def mesh_to_ggt_map(
    image,
    input_grid_point_list,
    output_grid_point_list,
):
    # compute general geometric transformation maps
    destination_list = []
    for source_point in output_grid_point_list:
        destination_list.append([source_point[1], source_point[0]])

    width, height = image.shape[1], image.shape[0]

    grid_x, grid_y = np.mgrid[0:height - 1:height * 1j, 0:width - 1:width * 1j]
    destination = np.array(destination_list)
    source = np.array(input_grid_point_list)
    grid_z = griddata(destination, source, (grid_x, grid_y), method='cubic')

    map_x = np.append([], [ar[:, 0] for ar in grid_z]).reshape(height, width)
    map_y = np.append([], [ar[:, 1] for ar in grid_z]).reshape(height, width)
    map_x = map_x.astype('float32')
    map_y = map_y.astype('float32')

    return map_x, map_y