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)))
def export_polygon_face(ind, face, isplane, center, radius, facesize, facecolor): """Export the information of a face to a povray macro.""" if isplane: macro = "FlatFace({}, {}, vertices_list, {}, {})\n" return macro.format(ind, len(face), facesize, helpers.pov_vector(facecolor)) else: macro = "BubbleFace({}, {}, vertices_list, {}, {}, {}, {})\n" return macro.format(ind, len(face), helpers.pov_vector(center), radius, facesize, helpers.pov_vector(facecolor))
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) )
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))
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)))
def export_edge(self, fobj, p1, p2): """Export the data of an edge to POV-Ray .inc file.""" fobj.write("HyperbolicEdge({}, {})\n".format(helpers.pov_vector(p1), helpers.pov_vector(p2)))