function grid = align_edge_vertices2edge(grid, thr)
s = double(grid.shape);
for i=1:size(grid.control_pts,1)
    p =  grid.control_pts(i,:);
    d = sqrt(sum((s - ones(size(s,1),1) * p).^2,2));
    [val, pos] = min(d);
    p = double(p);
    in = inpolygon(p(1), p(2), s(:,1), s(:,2));
    if (val<thr) || (in == 0)
        grid.control_pts(i,:) = s(pos,:);
    end
end