/**
 * Distance to the nearest crease
 * @param {number} u from 0 to 1
 * @param {number} v from 0 to 1
 * @returns {number}
 */
export function octahedral_uv_crease_distance(u, v) {

    const cross_distance_u = Math.abs(u - 0.5);
    const cross_distance_v = Math.abs(v - 0.5);

    const cross_distance = Math.min(cross_distance_u, cross_distance_v);

    const n_u = u * 2 - 1;
    const n_v = v * 2 - 1;

    // for the belt of the fold
    const fold = 1 - Math.abs(n_u) - Math.abs(n_v);

    const fold_distance = Math.abs(fold);

    // distance to the edge of the map, which folds the welding crease
    const welding_crease_distance = Math.min(Math.min(u, 1 - u), Math.min(v, 1 - v));

    return Math.min(fold_distance, cross_distance, welding_crease_distance);
}