function beta = greedyice_choosebeta(con, im, beta)
% GREEDYICE_CHOOSEBETA Relaxes values of beta at contour corners

%% constants
threshold1 = 0.25; %% corner curvature threshold 29 degrees
threshold2 = 0.4; %% corner edge strength threshold

%% init
n = length(con);
vlast = con(n,:)';
curvatures = zeros(n,1);

%% calculate curvature at each point on contour
for i = 1:n
    %% get this and next point
    vi = con(i,:)';
    if i == n, vnext = con(1,:)';
    else vnext = con(i+1,:)'; end

    %% calculate vectors
    ui = vi - vlast;
    unext = vnext - vi;

    %% calculate curvature (avoid divide by zero)
    if unext == 0
        if ui == 0
            ci = 0;
        else
            ci = norm((ui / norm(ui)) - 0) ^ 2;
        end
    else
        if ui == 0
            ci = norm(0 - (unext / norm(unext))) ^ 2;
        else
            ci = norm((ui / norm(ui)) - (unext / norm(unext))) ^ 2;
        end
    end

    curvatures(i) = ci;
    vlast = vi;
end

%% process to determine whether to allow corners on next iteration
clast = curvatures(n);
for i = 1:n
    ci = curvatures(i);
    if i == n, cnext = curvatures(1);
    else cnext = curvatures(i+1); end
    vi = con(i,:)';
    if ci > clast && ci > cnext
        if ci > threshold1 && im(vi(1), vi(2)) < threshold2
            beta(i) = 0;
        end
    end

    clast = ci;
end