예제 #1
0
def write_to_pov(P):
    """
    Write the data of a polytope to a POV-Ray include file for rendering.

    :param P: a polytope instance.
    """
    if isinstance(P, Catalan3D):
        vert_macros = "\n".join(VERT_MACRO.format(i, v + sum(len(vlist) for vlist in P.vertex_coords[:i]))
                                for i, vlist in enumerate(P.vertex_coords)
                                for v in range(len(vlist)))
        face_macros = "\n".join(FACE_MACRO.format(0, len(face), helpers.pov_array(face))
                                for face in P.face_indices)
        vertex_coords = helpers.pov_vector_list(P.vertex_coords_flatten)
    else:
        vert_macros = "\n".join(VERT_MACRO.format(0, i) for i in range(P.num_vertices))
        face_macros = "\n".join(FACE_MACRO.format(i, len(face), helpers.pov_array(face))
                                for i, flist in enumerate(P.face_indices)
                                for face in flist)
        vertex_coords = helpers.pov_vector_list(P.vertex_coords)

    edge_macros = "\n".join(EDGE_MACRO.format(i, e[0], e[1])
                            for i, elist in enumerate(P.edge_indices)
                            for e in elist)

    with open("./povray/polyhedra-data.inc", "w") as f:
        f.write(POV_TEMPLATE.format(
            P.num_vertices,
            P.num_vertices,
            vertex_coords,
            vert_macros,
            edge_macros,
            face_macros))
예제 #2
0
    def render(self, output, image_size):
        """Need to find out how to draw 3d plots in SVG format.
        """
        import subprocess

        pov_string = """
#declare num_vertices = {};
#declare vertices = array[{}]{{{}}};

{}
        """
        EDGE_MACRO = "Edge({}, {}, {})\n"
        FACE_MACRO = "Face({}, {}, {})\n"
        with open("./povray/polyhedra-data.inc", "w") as f:
            f.write(
                pov_string.format(
                    self.num_vertices,
                    self.num_vertices,
                    helpers.pov_vector_list(self.vertices_coords),
                    "".join(
                        EDGE_MACRO.format(k, i1, i2)
                        for k, elist in self.edge_indices.items()
                        for i1, i2 in elist
                    ),
                )
            )
            for (i, j), flist in self.face_indices.items():
                k = self.vertex_at_mirrors(i, j)
                for face in flist:
                    domain1, domain2 = face.get_alternative_domains()
                    for D in domain1:
                        f.write(
                            FACE_MACRO.format(
                                k,
                                0,
                                "array[{}]{{{}}}".format(
                                    len(D), helpers.pov_vector_list(D)
                                ),
                            )
                        )
                    for D in domain2:
                        f.write(
                            FACE_MACRO.format(
                                k,
                                1,
                                "array[{}]{{{}}}".format(
                                    len(D), helpers.pov_vector_list(D)
                                ),
                            )
                        )
        subprocess.check_call(
            "cd povray && "
            + "povray polyhedra.pov "
            + "+W{} +H{} +A0.001 +R4 ".format(image_size, image_size)
            + "+O../{}".format(output),
            shell=True,
        )
예제 #3
0
    def generate_povray_data(
            self,
            depth=100,
            maxcount=50000,
            cell_depth=None,
            cell_edges=10000,
            filename="./povray/honeycomb-data.inc",
            eye=(0, 0, 0.5),
            lookat=(0, 0, 0),
    ):
        self.G.init()
        self.word_generator = partial(self.G.traverse,
                                      depth=depth,
                                      maxcount=maxcount)
        self.fundamental_cells = self.get_fundamental_cells(
            cell_depth, cell_edges)
        init_edges = self.collect_fundamental_cell_edges()
        bar = tqdm.tqdm(desc="processing edges", total=maxcount)
        vertices = set()
        eye = np.array(eye)
        lookat = np.array(lookat)
        viewdir = helpers.normalize(lookat - eye)

        def add_new_edge(edge):
            p1 = self.project(edge[0])
            p2 = self.project(edge[1])
            if np.dot(p1 - eye, viewdir) > 0.5 or np.dot(p2 - eye,
                                                         viewdir) > 0.5:
                self.export_edge(f, p1, p2)
                self.num_edges += 1
                for v in [p1, p2]:
                    v = vround(v)
                    if v not in vertices:
                        vertices.add(v)
                        self.num_vertices += 1

        with open(filename, "w") as f:
            f.write("#declare camera_loc = {};\n".format(
                helpers.pov_vector(eye)))
            f.write("#declare lookat = {};\n".format(
                helpers.pov_vector(lookat)))
            for edge in init_edges:
                add_new_edge(edge)

            for word in self.word_generator():
                for edge in init_edges:
                    edge = [self.transform(word, v) for v in edge]
                    if self.is_new_edge(edge):
                        add_new_edge(edge)

                bar.update(1)
            bar.close()
            verts = "#declare num_vertices = {};\n"
            verts_coords = "#declare vertices = array[{}]{{{}}};\n"
            print("{} vertices and {} edges generated".format(
                self.num_vertices, self.num_edges))
            f.write(verts.format(self.num_vertices))
            f.write(
                verts_coords.format(self.num_vertices,
                                    helpers.pov_vector_list(vertices)))
예제 #4
0
    def export_pov(self, filename="./povray/polychora-data.inc"):
        vstr = "Vertex({})\n"
        estr = "Edge({}, {})\n"

        extent = np.max(
            [np.linalg.norm(helpers.proj3d(v)) for v in self.vertex_coords])

        with open(filename, "w") as f:
            f.write("#declare extent = {};\n".format(extent))

            for v in self.vertex_coords:
                f.write(vstr.format(helpers.pov_vector(v)))

            for i, edge_list in enumerate(self.edge_coords):
                for edge in edge_list:
                    f.write(estr.format(i, helpers.pov_vector_list(edge)))

            for i, face_list in enumerate(self.face_coords):
                for face in face_list:
                    isplane, center, radius, facesize = helpers.get_sphere_info(
                        face)
                    f.write(helpers.pov_array(face))
                    f.write(
                        helpers.export_face(i, face, isplane, center, radius,
                                            facesize))
예제 #5
0
def write_to_pov(P,
                 camera=(0, 0, 180),
                 rotation=(0, 0, 0),
                 vertex_size=0.04,
                 edge_size=0.02,
                 size_func=0,
                 face_index=[0],
                 face_max=3,
                 face_min=0.5):
    """Write the data of a polytope `P` to the include file.

    :param camera: camera location.

    :param rotation: rotation angles (in degree) of the polytope.

    :param vertex_size: controls size of the vertices.

    :param edge_size: controls size of the edges.

    :param size_func: choose which way to adjust the size of the edges.
        currently there are three choices, so it can only be 0-2.

    :param face_index: controls which type of faces are rendered,
        must be a list of integers.

    :param face_max: faces larger than this value will not be rendered.

    :param face_min: faces smaller than this value will not be rendered.
    """
    with open("./povray/polychora-data.inc", "w") as f:
        extent = max(np.linalg.norm(helpers.proj3d(v)) for v in P.vertex_coords)
        vert_macros = "\n".join(VERT_MACRO.format(k) for k in range(P.num_vertices))
        edge_macros = "\n".join(EDGE_MACRO.format(i, e[0], e[1])
                                for i, elist in enumerate(P.edge_indices)
                                    for e in elist)
        face_macros = "\n".join(helpers.export_face(i, face)
                                for i, flist in enumerate(P.face_coords)
                                    for face in flist)
        f.write(POV_TEMPLATE.format(
            vertex_size,
            edge_size,
            helpers.pov_vector(camera),
            helpers.pov_vector(rotation),
            extent,
            P.num_vertices,
            helpers.pov_vector_list(P.vertex_coords),
            size_func,
            face_max,
            face_min,
            helpers.pov_array(face_index),
            vert_macros,
            edge_macros,
            face_macros)
            )
예제 #6
0
    def export_pov(self, filename="./povray/polyhedra-data.inc"):
        vstr = "Vertex({})\n"
        estr = "Edge({}, {})\n"
        fstr = "Face({}, {}, vertices_list)\n"
        with open(filename, "w") as f:
            for v in self.vertex_coords:
                f.write(vstr.format(helpers.pov_vector(v)))

            for i, edge_list in enumerate(self.edge_coords):
                for edge in edge_list:
                    f.write(estr.format(i, helpers.pov_vector_list(edge)))

            for i, face_list in enumerate(self.face_coords):
                for face in face_list:
                    f.write(helpers.pov_array(face))
                    f.write(fstr.format(i, len(face)))
예제 #7
0
def write_to_pov(P):
    with open("./povray/120-cell-data.inc", "w") as f:
        extent = max(
            np.linalg.norm(helpers.proj3d(v)) for v in P.vertex_coords)
        vert_macros = "\n".join(
            VERT_MACRO.format(k) for k in range(P.num_vertices))
        edge_macros = "\n".join(
            EDGE_MACRO.format(e[0], e[1]) for elist in P.edge_indices
            for e in elist)
        face_macros = "\n".join(
            helpers.export_face(i, face)
            for i, flist in enumerate(P.face_coords) for face in flist)
        f.write(
            POV_TEMPLATE.format(extent, P.num_vertices,
                                helpers.pov_vector_list(P.vertex_coords),
                                vert_macros, edge_macros, face_macros))
def render3d(T, output="./povray/affine-data.inc", scene_file="euclid3d.pov"):
    VERT_MACRO = "Vert({})"
    EDGE_MACRO = "Edge(vertices, {}, {}, {})"
    vert_macros = "\n".join(VERT_MACRO.format(i) for i in range(T.num_vertices))
    edge_macros = "\n".join(EDGE_MACRO.format(i, e[0], e[1])
                            for i, elist in T.edge_indices.items()
                            for e in elist)
    with open(output, "w") as f:
        f.write(POV_TEMPLATE.format(
            T.num_vertices,
            T.num_vertices,
            helpers.pov_vector_list([T.project(v) for v in T.vertices_coords]),
            vert_macros,
            edge_macros,
        ))
    subprocess.call(POV_COMMAND, shell=True)
def render(T):
    vertex_coords = helpers.pov_vector_list(T.vertices_coords)
    vert_macros = "\n".join(
        VERT_MACRO.format(i) for i in range(T.num_vertices))
    edge_macros = "\n".join(
        EDGE_MACRO.format(i, e[0], e[1])
        for i, elist in T.edge_indices.items() for e in elist)
    face_macros = "\n".join(
        FACE_MACRO.format((i + j) % 3, len(face), helpers.pov_array(face))
        for (i, j), flist in T.face_indices.items() for face in flist)

    with open("./povray/polyhedra-data.inc", "w") as f:
        f.write(
            POV_TEMPLATE.format(T.num_vertices, T.num_vertices, vertex_coords,
                                vert_macros, edge_macros, face_macros))

    subprocess.call(POV_COMMAND, shell=True)
예제 #10
0
def write_to_pov(P, glass_tex, face_index, vertex_color, edge_color):
    """Write the data of a polytope to a POV-Ray include file for rendering.

    :param P: a polytope instance.

    :param glass_tex: glass texture defined in POV-Ray's "textures.inc".

    :param face_index: controls which type of faces are rendered.
        This input can be either an integer or a list/tuple of integers,
        for example if face_index=1 then the second list of faces will be
        rendered, or if face_index=(0, 1) then the first and second lists
        of faces will be rendered.

    :param vertex_color && edge_color: colors defined in POV-Ray's "colors.inc".
    """
    vert_macros = "\n".join(VERT_MACRO.format(i, vertex_color) for i in range(P.num_vertices))
    edge_macros = "\n".join(EDGE_MACRO.format(e[0], e[1], edge_color) for elist in P.edge_indices for e in elist)

    faces = []
    for i, flist in enumerate(P.face_indices):
        try:
            if (face_index == "all" or i == face_index or i in face_index):
                faces += flist
        except:
            pass

    face_macros = "\n".join(FACE_MACRO.format(len(face), helpers.pov_array(face), glass_tex) for face in faces)

    with open("./povray/polychora-data.inc", "w") as f:
        f.write(POV_TEMPLATE.format(
            vertex_color,
            edge_color,
            P.num_vertices,
            P.num_vertices,
            helpers.pov_vector_list(P.vertex_coords),
            vert_macros,
            edge_macros,
            face_macros
            )
        )
예제 #11
0
def export_pov_array(arr):
    """Export the vertices of a face to povray array."""
    declare = "#declare vertices_list = array[{}] {{ {} }};\n"
    return declare.format(len(arr), helpers.pov_vector_list(arr))