def remove_intersections_from_rings(rings):
    from options4rings import intersection_removal_progress_output_on
    from time import time as current_time
    from andysmod import n_choose_k, format_time
    [r.record_wasClosed() for r in rings]
    num_segments_in_ring_list = sum((len(r.path) for r in rings))
    num_seg_pairs2check = n_choose_k(num_segments_in_ring_list, 2)
    num_seg_pairs_checked = 0
    current_percent_complete = 0
    start_time = current_time()
    count = 0
    overlappingClosedRingPairs = []
    for i in range(len(rings)):
        iring = rings[i]
        ipath = iring.path
        new_ipath = ipath
        iclosed = iring.wasClosed
        iringupdated = False
        num_segs_in_ipath = len(ipath)
        for j in range((i + 1), len(rings)):
            if ((rings[j].maxR < rings[i].minR) or (rings[i].maxR < rings[j].minR)):
                continue
            jring = rings[j]
            jpath = jring.path
            new_jpath = jpath
            jclosed = jring.wasClosed
            jringupdated = False
            num_segs_in_jpath = len(jpath)
            run_again = True
            maxits = 20
            its = 0
            while (run_again and (its < maxits)):
                try:
                    args = (new_ipath, new_jpath, iclosed, jclosed)
                    res = remove_intersections(*args, iringupdated=iringupdated, jringupdated=jringupdated)
                    (new_ipath, new_jpath, iringupdated, jringupdated, run_again) = res
                except ClosedRingsOverlapError:
                    overlappingClosedRingPairs.append((i, j))
                    run_again = False
                    pass
                its += 1
            if (its >= maxits):
                raise Exception('Max iterations reached while removing intersections.  Either the above two rings have over 20 intersections or this is a bug.')
            if intersection_removal_progress_output_on.b:
                num_seg_pairs_checked += (num_segs_in_jpath * num_segs_in_ipath)
                if (((100 * num_seg_pairs_checked) / num_seg_pairs2check) > int((100 * current_percent_complete))):
                    current_percent_complete = (num_seg_pairs_checked / num_seg_pairs2check)
                    time_elapsed = (current_time() - start_time)
                    estimated_time_remaining = (((1 - current_percent_complete) * time_elapsed) / current_percent_complete)
                    stuff = (int((100 * current_percent_complete)), format_time(estimated_time_remaining), format_time(time_elapsed))
                    mes = ('[%s%% complete || Est. Remaining Time = %s || Elapsed Time = %s]\r' % stuff)
                    intersection_removal_progress_output_on.dprint(mes)
            if jringupdated:
                jring.updatePath(new_jpath)
                count += 1
        if iringupdated:
            iring.updatePath(new_ipath)
            count += 1
    return (rings, count, overlappingClosedRingPairs)
def remove_intersections_from_rings(rings):
    from options4rings import intersection_removal_progress_output_on
    from time import time as current_time
    from andysmod import n_choose_k, format_time
    [r.record_wasClosed() for r in rings]
    num_segments_in_ring_list = sum((len(r.path) for r in rings))
    num_seg_pairs2check = n_choose_k(num_segments_in_ring_list, 2)
    num_seg_pairs_checked = 0
    current_percent_complete = 0
    start_time = current_time()
    count = 0
    overlappingClosedRingPairs = []
    for i in range(len(rings)):
        iring = rings[i]
        ipath = iring.path
        new_ipath = ipath
        iclosed = iring.wasClosed
        iringupdated = False
        num_segs_in_ipath = len(ipath)
        for j in range((i + 1), len(rings)):
            if ((rings[j].maxR < rings[i].minR) or (rings[i].maxR < rings[j].minR)):
                continue
            jring = rings[j]
            jpath = jring.path
            new_jpath = jpath
            jclosed = jring.wasClosed
            jringupdated = False
            num_segs_in_jpath = len(jpath)
            run_again = True
            maxits = 20
            its = 0
            while (run_again and (its < maxits)):
                try:
                    args = (new_ipath, new_jpath, iclosed, jclosed)
                    res = remove_intersections(*args, iringupdated=iringupdated, jringupdated=jringupdated)
                    (new_ipath, new_jpath, iringupdated, jringupdated, run_again) = res
                except ClosedRingsOverlapError:
                    overlappingClosedRingPairs.append((i, j))
                    run_again = False
                    pass
                its += 1
            if (its >= maxits):
                raise Exception('Max iterations reached while removing intersections.  Either the above two rings have over 20 intersections or this is a bug.')
            if intersection_removal_progress_output_on.b:
                num_seg_pairs_checked += (num_segs_in_jpath * num_segs_in_ipath)
                if (((100 * num_seg_pairs_checked) / num_seg_pairs2check) > int((100 * current_percent_complete))):
                    current_percent_complete = (num_seg_pairs_checked / num_seg_pairs2check)
                    time_elapsed = (current_time() - start_time)
                    estimated_time_remaining = (((1 - current_percent_complete) * time_elapsed) / current_percent_complete)
                    stuff = (int((100 * current_percent_complete)), format_time(estimated_time_remaining), format_time(time_elapsed))
                    mes = ('[%s%% complete || Est. Remaining Time = %s || Elapsed Time = %s]\r' % stuff)
                    intersection_removal_progress_output_on.dprint(mes)
            if jringupdated:
                jring.updatePath(new_jpath)
                count += 1
        if iringupdated:
            iring.updatePath(new_ipath)
            count += 1
    return (rings, count, overlappingClosedRingPairs)