Ejemplo n.º 1
0
def draw_3d_graph(graph: Graph,
                  pov_coords: Coords,
                  fname: str = 'graph.png',
                  verbose: bool = True):
    """Draw a projection of given graph"""
    amplitudes, center = graph.amplitudes, graph.center
    pov_coords = Coords(*pov_coords)
    pov = projection.create_pov_toward(center, pov_coords)
    print('CENTER PROJECTION:',
          projection.projection(center, pov, verbose=verbose))
    # exit()
    if verbose:
        print('POV:', pov)
        # print('ANGLES OF CENTER:', geometry.angles_from_coords(geometry.coords_centered_on(center, pov.coords)))
    nodes_projections = {
        node: projection.projection(Coords(*node), pov, verbose=verbose)
        for node in graph.nodes
    }
    # draw_map(pov, nodes_projections, center, center)
    if verbose:
        print('NODES PROJECTIONS:', nodes_projections)
    graph_2d = frozenset(
        (nodes_projections[source], nodes_projections[target])
        for source, target in graph.links
        if nodes_projections[source] and nodes_projections[target])
    if verbose:
        print('2D GRAPH:', graph_2d)
    draw_2d_graph(graph_2d,
                  fname=fname,
                  center=projection.projection(center, pov, verbose=verbose))
Ejemplo n.º 2
0
def test_coords_from_angles():
    coords_from_angles = geometry.coords_from_angles
    assert tuple(
        map(round, coords_from_angles(Coords(90, 45, 45),
                                      math.sqrt(2)))) == (0, 1, 1)
    assert tuple(
        map(round, coords_from_angles(Coords(45, 90, 45),
                                      math.sqrt(2)))) == (1, 0, 1)
    assert tuple(
        map(round, coords_from_angles(Coords(45, 45, 90),
                                      math.sqrt(2)))) == (1, 1, 0)
Ejemplo n.º 3
0
def graph_from_edges(
        graph: [(float, float, float), (float, float, float)]) -> Graph:
    nodes = frozenset(itertools.chain.from_iterable(graph))
    print('NODES:', nodes)
    center_ = Coords(*center(nodes))
    print('CENTER:', center_)
    return Graph(graph, center_, nodes, dimensions(nodes), amplitudes(nodes))
Ejemplo n.º 4
0
def test_origin_switch():
    dist = geometry.distance_between
    a, b = Coords(0, 0, 0), Coords(1, 1, 1)
    c, d, e = Coords(1, 0, 0), Coords(0, 1, 0), Coords(0, 0, 1)
    assert geometry.coords_centered_on(a, b) == Coords(-1, -1, -1)
    assert geometry.coords_centered_on(c, a) == c
    assert geometry.coords_centered_on(d, a) == d
    assert geometry.coords_centered_on(e, a) == e
Ejemplo n.º 5
0
def run_things(graph,
               nb_point=100,
               distance_to_object_factor: float = 4.7,
               fname_template: str = 'output/graph_{num:03d}.png',
               verbose: bool = True):
    dist_to_center = (max(graph.amplitudes[0], graph.amplitudes[2]) /
                      2) * distance_to_object_factor
    points = points_on_circle((graph.center.x, graph.center.z),
                              dist_to_center,
                              nb_point=nb_point)
    for n, (x, z) in enumerate(points, start=1):
        print('CIRCLING BY:', x, z)
        fname = fname_template.format(num=n)
        draw_3d_graph(graph,
                      pov_coords=Coords(x, graph.center.y, z),
                      fname=fname,
                      verbose=verbose)
        yield fname
Ejemplo n.º 6
0
def test_angles_from_coords():
    angles_from_coords = geometry.angles_from_coords
    for x in range(1, 100, 10):
        assert angles_from_coords(Coords(x, 0, 0)) == (0, 90, 90)
    for x in range(1, 100, 10):
        assert angles_from_coords(Coords(0, x, 0)) == (90, 0, 90)
    for x in range(1, 100, 10):
        assert angles_from_coords(Coords(0, 0, x)) == (90, 90, 0)
    assert tuple(map(round, angles_from_coords(Coords(0, 1,
                                                      1)))) == (90, 45, 45)
    assert tuple(map(round, angles_from_coords(Coords(1, 0,
                                                      1)))) == (45, 90, 45)
    assert tuple(map(round, angles_from_coords(Coords(1, 1,
                                                      0)))) == (45, 45, 90)
Ejemplo n.º 7
0
def test_distance():
    dist = geometry.distance_between
    odist = geometry.distance_to_origin

    a, b = Coords(0, 0, 0), Coords(1, 1, 1)
    assert geometry.distance_to_origin(a) == 0.
    assert dist(a, a) == dist(b, b) == 0.
    assert dist(a, b) == odist(b) == math.sqrt(3)

    c, d, e = Coords(1, 0, 0), Coords(0, 1, 0), Coords(0, 0, 1)
    assert dist(a, c) == dist(a, d) == dist(a, e) == 1.
    assert odist(c) == odist(d) == odist(e) == 1.
    assert dist(d, c) == dist(d, e) == dist(e, c) == math.sqrt(2)

    f = Coords(-7.7, 0, 2.4)
    assert round(odist(f), 2) == round(dist(a, f), 2) == 8.07
Ejemplo n.º 8
0
def test_coords_in_system():
    ROUNDING = 4
    rounded = lambda c: Coords(round(c.x, ROUNDING), round(c.y, ROUNDING),
                               round(c.z, ROUNDING))
    coords_in_system = lambda a, b, c: rounded(
        geometry.coords_in_system(a, b, c))
    a, null = Coords(1, 1, 0), Coords(0, 0, 0)
    assert coords_in_system(a, a, null) == null
    assert coords_in_system(a, null, null) == a
    assert coords_in_system(null, null, null) == null
    assert coords_in_system(null, a, null) == Coords(-1, -1, 0)

    # now, work with angle
    assert rounded(coords_in_system(Coords(1, 1, 0), null,
                                    Coords(0, 0, -45))) == rounded(
                                        Coords(math.sqrt(2), 0, 0))
    assert rounded(coords_in_system(Coords(1, 1, 0), null,
                                    Coords(0, 0,
                                           -90))) == rounded(Coords(1, -1, 0))

    # now, rotation and translation !
    for x in range(-90, 90, 20):
        assert rounded(coords_in_system(Coords(1, 1, 10), a,
                                        Coords(0, 0, x))) == rounded(
                                            Coords(0, 0, 10))
    assert rounded(coords_in_system(a, null,
                                    Coords(0, -90,
                                           0))) == rounded(Coords(0, 1, -1))