function [R, vertex, edge, edge_petal, N, grid_xy, centroids, REGS] = layers2grid(R, corners, reg_x, reg_y, shape, adjust_corners_flag)

if adjust_corners_flag == 1
    corners = adjust_corners(R, corners);
end
[edge, vertex, REGS] = layers2vertex_edge(R, double(shape), reg_x, reg_y, corners, 0);
edge = sort_all_region_pts(edge);
fprintf('Now finding the neighbours for each region...\n');
N = find_neighbours(R);
fprintf('done.\n');
grid_xy = calc_grid_outline(R);
centroids = get_centroids(R);
centroids = centroids(2:end, :);
edge_petal = make_edge_petal(R, shape);