def test_halfspace_restriction_4():
    """
    Halfspace restriction can create new events even if hyperplane is already in cd.hyperplanes.
    Case: From two vertex cones that neutralized each other in original cd only one is valid in
    restricted cd.
    Here vertex at (0,1) is not part of original cd but of restricted cd.
    """
    a_0 = Hyperplane(np.array([1, 0, ]), 0)
    a_1 = Hyperplane(np.array([0, 1]), 0)
    a_2 = Hyperplane(np.array([1, 0]), -1)
    a_3 = Hyperplane(np.array([0, 1]), -1)

    b_0 = Hyperplane(np.array([1, 0, ]), 0.5)
    b_1 = Hyperplane(np.array([0, 1]), -0.5)
    b_2 = Hyperplane(np.array([1, 0]), -0.5)
    b_3 = Hyperplane(np.array([0, 1]), -1.5)

    hyperplanes = [a_0, a_1, a_2, a_3, b_0, b_1, b_2, b_3]
    cube_1 = {(0, 1), (1, 1), (2, -1), (3, -1)}
    cube_2 = {(4, 1), (5, 1), (6, -1), (7, -1)}

    cube_2_restricted = {(4, 1), (5, 1), (6, -1), (7, -1), (3, -1)}
    cd = Cell_Decomposition(hyperplanes, [cube_1, cube_2])
    cd.restrict_to_halfspace(a_3, -1)
    s = Sweep(cd.events)
    cd2 = Cell_Decomposition(hyperplanes, [cube_1, cube_2_restricted])
    s2 = Sweep(cd2.events)
    assert round(s2.calculate_volume(), 5) == 1.25
    assert round(s.calculate_volume(), 5) == 1.25
Ejemplo n.º 2
0
def hole_4d():
    hyperplanes = OrderedSet()
    a1 = np.array([1, 0, 0, 0])
    a2 = np.array([-1, 0, 0, 0])

    a3 = np.array([0, 1, 0, 0])
    a4 = np.array([0, -1, 0, 0])

    a5 = np.array([0, 0, 1, 0])
    a6 = np.array([0, 0, -1, 0])

    a7 = np.array([0, 0, 0, 1])
    a8 = np.array([0, 0, 0, -1])

    p0 = Hyperplane(a5, -1)
    p1 = Hyperplane(a6, 0)
    p2 = Hyperplane(a7, -1)
    p3 = Hyperplane(a8, 0)

    p4 = Hyperplane(a1, 0)
    p5 = Hyperplane(a2, -1)
    p6 = Hyperplane(a3, -1)
    p7 = Hyperplane(a4, 0)

    P1 = set()
    for h in [p0, p1, p2, p3, p4, p5, p6, p7]:
        index = hyperplanes.add(h)
        P1.add((index, -1))

    q1 = Hyperplane(a1, -1)
    q2 = Hyperplane(a2, 0)
    q3 = Hyperplane(a3, 0)
    q4 = Hyperplane(a4, -1)

    P2 = set()
    for h in [p0, p1, p2, p3, q1, q2, q3, q4]:
        index = hyperplanes.add(h)
        P2.add((index, -1))

    r1 = Hyperplane(a1, -2)
    r2 = Hyperplane(a2, 1)
    r3 = Hyperplane(a3, -1)
    r4 = Hyperplane(a4, 0)
    P3 = set()
    for h in [p0, p1, p2, p3, r1, r2, r3, r4]:
        index = hyperplanes.add(h)
        P3.add((index, -1))

    s1 = Hyperplane(a1, -1)
    s2 = Hyperplane(a2, 0)
    s3 = Hyperplane(a3, -2)
    s4 = Hyperplane(a4, 1)
    P4 = set()
    for h in [p0, p1, p2, p3, s1, s2, s3, s4]:
        index = hyperplanes.add(h)
        P4.add((index, -1))

    return Cell_Decomposition(hyperplanes, [P1, P2, P3, P4])
Ejemplo n.º 3
0
def simplex():
    a_0 = Hyperplane(np.array([1, 0, 0]), 0)
    a_1 = Hyperplane(np.array([0, 1, 0]), 0)
    a_2 = Hyperplane(np.array([0, 0, 1]), 0)
    a_3 = Hyperplane(np.array([-1, -1, -1]), 1)

    hyperplanes = [a_0, a_1, a_2, a_3]

    simplex = {(0, 1), (1, 1), (2, 1), (3, 1)}
    return Cell_Decomposition(hyperplanes, [simplex])
Ejemplo n.º 4
0
def triangles_meeting_in_one_point():
    # geometry: |><|
    h0 = Hyperplane(np.array([1, 0]), 0)
    h1 = Hyperplane(np.array([1, 0]), -1)
    h2 = Hyperplane(np.array([1, -1]), 0)
    h3 = Hyperplane(np.array([1, 1]), -1)
    hyperplanes = [h0, h1, h2, h3]
    poly1 = {(0, 1), (2, -1), (3, -1)}
    poly2 = {(1, -1), (2, 1), (3, 1)}
    return Cell_Decomposition(hyperplanes, [poly1, poly2])
Ejemplo n.º 5
0
def non_regular_pyramid_3d():
    f_0 = Hyperplane(np.array([-1, 0, -1]), 0)
    f_1 = Hyperplane(np.array([0, -1, -1]), 0)
    f_2 = Hyperplane(np.array([0, 1, -1]), 0)
    f_3 = Hyperplane(np.array([1, 0, -1]), 0)
    f_4 = Hyperplane(np.array([0, 0, 1]), -1)

    hyperplanes = [f_0, f_1, f_2, f_3, f_4]
    p_0 = {(0, -1), (1, -1), (2, -1), (3, -1), (4, -1)}
    return Cell_Decomposition(hyperplanes, [p_0])
Ejemplo n.º 6
0
def unbounded_non_regular():
    a_0 = Hyperplane(np.array([-1, 1]), 0)
    a_1 = Hyperplane(np.array([1, 1]), 0)
    a_2 = Hyperplane(np.array([-(1 / 2), -1]), 0)
    a_3 = Hyperplane(np.array([1 / 2, -1]), 0)

    hyperplanes = [a_0, a_1, a_2, a_3]

    left_poly = {(1, -1), (2, -1)}
    right_poly = {(0, -1), (3, -1)}
    return Cell_Decomposition(hyperplanes, [left_poly, right_poly])
def test_halfspace_restriction_2():
    a_0 = Hyperplane(np.array([1, 0, ]), 0)
    a_1 = Hyperplane(np.array([0, 1]), 0)
    a_2 = Hyperplane(np.array([1, 1]), -1)
    a_3 = Hyperplane(np.array([1, 0]), -1)
    a_4 = Hyperplane(np.array([0, 1]), -1)
    hyperplanes_start = [a_0, a_1, a_2, a_3]
    hyperplanes_final = [a_0, a_1, a_2, a_3, a_4]

    simplex = {(0, 1), (1, 1), (2, -1)}
    unit_cube_start = {(0, 1), (1, 1), (3, -1)}
    unit_cube = {(0, 1), (1, 1), (3, -1), (4, -1)}

    cd = Cell_Decomposition(hyperplanes_final, [simplex, unit_cube])
    cd1 = Cell_Decomposition(hyperplanes_start, [simplex, unit_cube_start])
    cd1.restrict_to_halfspace(a_4, -1)

    assert set(cd.events).issubset(set(cd1.events))
    assert set(cd1.events).issubset(set(cd.events))
    assert round(Sweep(cd.events).calculate_volume(), 8) == round(
        Sweep(cd1.events).calculate_volume(), 8)
Ejemplo n.º 8
0
def unit_cube():
    a_0 = Hyperplane(np.array([1, 0, 0]), 0)
    a_1 = Hyperplane(np.array([0, 1, 0]), 0)
    a_2 = Hyperplane(np.array([0, 0, 1]), 0)
    a_3 = Hyperplane(np.array([1, 0, 0]), -1)
    a_4 = Hyperplane(np.array([0, 1, 0]), -1)
    a_5 = Hyperplane(np.array([0, 0, 1]), -1)

    hyperplanes = [a_0, a_1, a_2, a_3, a_4, a_5]

    unit_cube = {(0, 1), (1, 1), (2, 1), (3, -1), (4, -1), (5, -1)}
    return Cell_Decomposition(hyperplanes, [unit_cube])
Ejemplo n.º 9
0
def translated_triangles():
    # geometry: <\<\ two congruent triangles that meet in 0
    h0 = Hyperplane(np.array([0, 1]), 0)
    h1 = Hyperplane(np.array([-1, 1]), -1)
    h2 = Hyperplane(np.array([1, 1]), 0)
    h3 = Hyperplane(np.array([-1, 1]), 0)
    h4 = Hyperplane(np.array([1, 1]), -1)

    hyperplanes = [h0, h1, h2, h3, h4]
    poly1 = {(0, 1), (1, -1), (2, -1)}
    poly2 = {(0, 1), (3, -1), (4, -1)}
    return Cell_Decomposition(hyperplanes, [poly1, poly2])
Ejemplo n.º 10
0
def paper_example():
    f_0 = Hyperplane(np.array([6, 11]), -100)
    f_1 = Hyperplane(np.array([2, 1]), -28)
    f_2 = Hyperplane(np.array([2, -3]), 4)
    f_3 = Hyperplane(np.array([-2, 7]), -52)

    hyperplanes = [f_0, f_1, f_2, f_3]

    p_0 = set([(0, 1), (1, -1), (3, -1)])
    p_1 = set([(0, 1), (2, -1), (3, -1)])

    return Cell_Decomposition(hyperplanes, [p_0, p_1])
Ejemplo n.º 11
0
def simplex_cube_disconnected():
    s_0 = Hyperplane(np.array([1, 0]), 0)
    s_1 = Hyperplane(np.array([0, 1]), 0)
    s_2 = Hyperplane(np.array([1, 1]), -1)

    c_0 = Hyperplane(np.array([1, 0]), -2)
    c_1 = Hyperplane(np.array([0, 1]), -2)
    c_2 = Hyperplane(np.array([1, 0]), -3)
    c_3 = Hyperplane(np.array([0, 1]), -3)
    hyperplanes = [s_0, s_1, s_2, c_0, c_1, c_2, c_3]
    simplex = {(0, 1), (1, 1), (2, -1)}
    cube = {(3, 1), (4, 1), (5, -1), (6, -1)}
    return Cell_Decomposition(hyperplanes, [simplex, cube])
Ejemplo n.º 12
0
def simplex_in_cube_3d():
    a_0 = Hyperplane(np.array([1, 0, 0]), 0)
    a_1 = Hyperplane(np.array([0, 1, 0]), 0)
    a_2 = Hyperplane(np.array([0, 0, 1]), 0)
    a_3 = Hyperplane(np.array([-1, -1, -1]), 1)
    a_4 = Hyperplane(np.array([1, 0, 0]), -1)
    a_5 = Hyperplane(np.array([0, 1, 0]), -1)
    a_6 = Hyperplane(np.array([0, 0, 1]), -1)

    hyperplanes = [a_0, a_1, a_2, a_3, a_4, a_5, a_6]

    simplex = {(0, 1), (1, 1), (2, 1), (3, 1)}
    unit_cube = {(0, 1), (1, 1), (2, 1), (4, -1), (5, -1), (6, -1)}
    return Cell_Decomposition(hyperplanes, [unit_cube, simplex])
Ejemplo n.º 13
0
def cube_simplex_overlapping_2d():
    c_0 = Hyperplane(np.array([1, 0]), 0)
    c_1 = Hyperplane(np.array([0, 1]), 0)
    c_2 = Hyperplane(np.array([1, 0]), -1)
    c_3 = Hyperplane(np.array([0, 1]), -1)

    s_0 = Hyperplane(np.array([1, 0]), -0.5)
    s_1 = Hyperplane(np.array([0, 1]), -0.5)
    s_2 = Hyperplane(np.array([1, 1]), -1.5)
    hyperplanes = [c_0, c_1, c_2, c_3, s_0, s_1, s_2]

    simplex = {(4, 1), (5, 1), (6, -1)}
    unit_cube = {(0, 1), (1, 1), (2, -1), (3, -1)}
    return Cell_Decomposition(hyperplanes, [unit_cube, simplex])
Ejemplo n.º 14
0
def get_cell_decompositions(filepath,
                            reduce_hyperplanes=True,
                            description=PolytopeDescription.INNER_DESCRIPTION):
    polytopes = polytopes_from_json(filepath, description=description)
    hyperplanes, polytope_vectors = hyperplanes_and_polytope_vectors(polytopes)
    if reduce_hyperplanes:
        hyperplanes, polytope_vectors = ana.drop_facets(
            hyperplanes, polytope_vectors)
    logging.info("Computing cell decomposition for union of polytopes"
                 " from {} hyperplanes.".format(len(hyperplanes)))
    bbox = bounding_box(polytopes)
    union_cd = Cell_Decomposition(hyperplanes,
                                  polytope_vectors,
                                  bounding_box=bbox)
    conv_cd = ana.conv_hull_cell_decomposition(union_cd)
    return [union_cd, conv_cd]
Ejemplo n.º 15
0
def overlapping_simplices_2():
    s_0 = Hyperplane(np.array([1, 0, 0]), 0)
    s_1 = Hyperplane(np.array([0, 1, 0]), 0)
    s_2 = Hyperplane(np.array([0, 0, 1]), 0)
    s_3 = Hyperplane(np.array([1, 1, 1]), -8)

    s_4 = Hyperplane(np.array([0, 0, 1]), -1)
    s_5 = Hyperplane(np.array([0, 1, 0]), -1)
    s_6 = Hyperplane(np.array([1, 0, 0]), -1)
    s_7 = Hyperplane(np.array([1, 1, -1]), -3)

    hyperplanes = [s_0, s_1, s_2, s_3, s_4, s_5, s_6, s_7]
    simplex_1 = {(0, 1), (1, 1), (2, 1), (3, -1)}
    simplex_2 = {(4, -1), (5, 1), (6, 1), (7, -1)}
    polytopes = [simplex_1, simplex_2]
    return Cell_Decomposition(hyperplanes, polytopes)
Ejemplo n.º 16
0
def cube_simplex_overlapping_3d_2():
    c_0 = Hyperplane(np.array([1, 0, 0]), 0)
    c_1 = Hyperplane(np.array([0, 1, 0]), 0)
    c_2 = Hyperplane(np.array([0, 0, 1]), 0)
    c_3 = Hyperplane(np.array([1, 0, 0]), -1)
    c_4 = Hyperplane(np.array([0, 1, 0]), -1)
    c_5 = Hyperplane(np.array([0, 0, 1]), -1)

    s_0 = Hyperplane(np.array([1, 1, 1]), -4)
    s_1 = Hyperplane(np.array([1, 0, 0]), -0.5)
    s_2 = Hyperplane(np.array([0, 1, 0]), -0.5)
    s_3 = Hyperplane(np.array([0, 0, 1]), -0.5)

    hyperplanes = [c_0, c_1, c_2, c_3, c_4, c_5, s_0, s_1, s_2, s_3]

    simplex = {(6, -1), (7, 1), (8, 1), (9, 1)}
    unit_cube = {(0, 1), (1, 1), (2, 1), (3, -1), (4, -1), (5, -1)}
    return Cell_Decomposition(hyperplanes, [unit_cube, simplex])
def test_halfspace_restriction():
    h0 = Hyperplane(np.array([0, 1]), -1)
    h1 = Hyperplane(np.array([0, 1]), 0)
    h2 = Hyperplane(np.array([1, 0]), 0)
    h3 = Hyperplane(np.array([1, 0]), -1)
    h4 = Hyperplane(np.array([1, 1]), 0)

    hyperplanes_final = (h0, h1, h2, h3, h4)
    polytope = {(0, -1), (1, 1), (2, 1)}
    polytope_final = {(0, -1), (1, 1), (2, 1), (3, -1), (4, 1)}
    cd = Cell_Decomposition(hyperplanes_final, [polytope_final])
    cd1 = Cell_Decomposition((h0, h1, h2), [polytope])
    cd1.restrict_to_halfspace(h3, -1)
    cd1.restrict_to_halfspace(h4, 1)
    assert set(cd1.events).issubset(set(cd.events))
    assert set(cd.events).issubset(set(cd1.events))
    assert np.isclose(Sweep(cd.events).calculate_volume(), Sweep(cd1.events).calculate_volume())
Ejemplo n.º 18
0
def cube_simplex_overlapping_3d_imprecise():
    k = 1e+12
    c_0 = Hyperplane(np.array([k, 0, 0]), 0)
    c_1 = Hyperplane(np.array([0, k, 0]), 0)
    c_2 = Hyperplane(np.array([0, 0, k]), 0)
    c_3 = Hyperplane(np.array([k, 0, 0]), -k)
    c_4 = Hyperplane(np.array([0, k, 0]), -k)
    c_5 = Hyperplane(np.array([0, 0, k]), -k)

    s_0 = Hyperplane(np.array([k, k, k]), -3.5 * k)
    s_1 = Hyperplane(np.array([k, 0, 0]), -0.5 * k)
    s_2 = Hyperplane(np.array([0, k, 0]), -0.5 * k)
    s_3 = Hyperplane(np.array([0, 0, k]), -0.5 * k)

    hyperplanes = [c_0, c_1, c_2, c_3, c_4, c_5, s_0, s_1, s_2, s_3]

    simplex = {(6, -1), (7, 1), (8, 1), (9, 1)}
    unit_cube = {(0, 1), (1, 1), (2, 1), (3, -1), (4, -1), (5, -1)}
    return Cell_Decomposition(hyperplanes, [unit_cube, simplex])
Ejemplo n.º 19
0
def hole_2d():
    hyperplanes = OrderedSet()
    a1 = np.array([1, 0])
    a2 = np.array([-1, 0])

    a3 = np.array([0, 1])
    a4 = np.array([0, -1])

    p1 = Hyperplane(a1, 0)
    p2 = Hyperplane(a2, -1)
    p3 = Hyperplane(a3, -1)
    p4 = Hyperplane(a4, 0)
    P1 = set(zip(range(4), [-1] * 4))

    q1 = Hyperplane(a1, -1)
    q2 = Hyperplane(a2, 0)
    q3 = Hyperplane(a3, 0)
    q4 = Hyperplane(a4, -1)
    P2 = set()
    for h in [q1, q2, q3, q4]:
        ind = hyperplanes.add(h)
        P2.add((ind, -1))

    r1 = Hyperplane(a1, -2)
    r2 = Hyperplane(a2, 1)
    r3 = Hyperplane(a3, -1)
    r4 = Hyperplane(a4, 0)
    P3 = set()
    for h in [r1, r2, r3, r4]:
        ind = hyperplanes.add(h)
        P3.add((ind, -1))

    s1 = Hyperplane(a1, -1)
    s2 = Hyperplane(a2, 0)
    s3 = Hyperplane(a3, -2)
    s4 = Hyperplane(a4, 1)
    P4 = set()
    for h in [s1, s2, s3, s4]:
        ind = hyperplanes.add(h)
        P4.add((ind, -1))

    return Cell_Decomposition(hyperplanes, [P1, P2, P3, P4])
Ejemplo n.º 20
0
def conv_hull_cell_decomposition(cell_decomposition, reduce_hyperplanes=True):
    """
    Method computes Cell Decomposition for convex hull of events of input cell decomposition
    :param cell_decomposition: CellDecomposition object
    :param reduce_hyperplanes: boolean if close hyperplanes should be removed from convex hull
    :return: cell decomposition object for convex hull
    """

    p = Polytope(vertices=set([e.vertex for e in cell_decomposition.events]))
    hyperplanes = p.hyperplanes
    pos_vec = [zip(range(len(hyperplanes)), [1] * len(hyperplanes))]
    if reduce_hyperplanes:
        hyperplanes, pos_vec = drop_facets(hyperplanes, pos_vec)
    hyperplanes = [h.pertubate() for h in hyperplanes]

    logging.info(
        'create convex hull cell decomposition from {} hyperplanes'.format(
            len(hyperplanes), len(cell_decomposition.events)))
    return Cell_Decomposition(hyperplanes,
                              pos_vec,
                              bounding_box=cell_decomposition.bbox)
Ejemplo n.º 21
0
def hole_3d():
    a1 = np.array([1, 0, 0])
    a2 = np.array([-1, 0, 0])

    a3 = np.array([0, 1, 0])
    a4 = np.array([0, -1, 0])

    a5 = np.array([0, 0, 1])
    a6 = np.array([0, 0, -1])

    p0 = Hyperplane(a5, -1)
    p1 = Hyperplane(a6, 0)

    p2 = Hyperplane(a1, 0)
    p3 = Hyperplane(a2, -1)
    p4 = Hyperplane(a3, -1)
    p5 = Hyperplane(a4, 0)
    P1 = set(zip(range(6), [-1] * 6))

    q1 = Hyperplane(a1, -1)
    q2 = Hyperplane(a2, 0)
    q3 = Hyperplane(a3, 0)
    q4 = Hyperplane(a4, -1)
    P2 = set(zip(range(2), [-1] * 2) + zip(range(6, 10), [-1] * 4))

    r1 = Hyperplane(a1, -2)
    r2 = Hyperplane(a2, 1)
    r3 = Hyperplane(a3, -1)
    r4 = Hyperplane(a4, 0)
    P3 = set(zip(range(2), [-1] * 2) + zip(range(10, 14), [-1] * 4))

    s1 = Hyperplane(a1, -1)
    s2 = Hyperplane(a2, 0)
    s3 = Hyperplane(a3, -2)
    s4 = Hyperplane(a4, 1)
    P4 = set(zip(range(2), [-1] * 2) + zip(range(14, 18), [-1] * 4))
    hyperplanes = [p0, p1, p2, p3, p4, p5, q1, q2, q3, q4, r1, r2, r3, r4, s1, s2, s3, s4]

    return Cell_Decomposition(hyperplanes, [P1, P2, P3, P4])