def single_virus_gen(): """ single_virus_gen() Generates a virus object. :return virus object """ object_texture = Texture(Pigment('color', [1, 1, 1], 'transmit', 0), Finish('phong', 0.5, 'reflection', 0.9)) helix_test = 'f_helix1(x, y, z, 1, 1 * 4 * pi, 0.07, 0.8, 1, 0.3, 0)' iso_spine = Isosurface(Function(helix_test), ContainedBy(Box(-5, 5)), 'translate', [0, 0, 0], models.default_sphere_model) # spine helix cyl = Cylinder([0, -5, 0], [0, 5, 0], 0.5, object_texture) # spine cylinder icosahedral = 'abs(x)+abs(y)+abs(z)-4.9' ico_head = Isosurface(Function(icosahedral), ContainedBy(Box(-3.5, 3.5)), 'max_gradient', 5, 'translate', [0, 7, 0], object_texture) # head object ring = Torus(1.2, 0.3, 'translate', [0, -5, 0], object_texture) # hip ring spine # berelem radian, bijv: 1.05 * math.cos(radian * 3) 3 = step, 1.05 = straal radian = (360 / 6) * (math.pi / 180) x_tail_list = [] for coordinate_offset in range(1, 7, 2): coordinate_offset += 0.05 x_tail_list.append(coordinate_offset) # [1.05, 3.05, 5.05] x_axis_tail_list = [] z_axis_tail_list = [] for tail_nr in range(1, 7): tail_steps = tail_nr for coordinate in x_tail_list: x_axis_tail_list.append(coordinate * math.cos(radian * tail_steps)) z_axis_tail_list.append(coordinate * math.sin(radian * tail_steps)) tails = [] for tail_coordinate in range(0, len(x_axis_tail_list), 3): tails.append( SphereSweep('linear_spline', 3, [ x_axis_tail_list[tail_coordinate], -5.00, z_axis_tail_list[tail_coordinate] ], 0.1, [ x_axis_tail_list[tail_coordinate + 1], -1.00, z_axis_tail_list[tail_coordinate + 1] ], 0.15, [ x_axis_tail_list[tail_coordinate + 2], -9.00, z_axis_tail_list[tail_coordinate + 2] ], 0.1, object_texture)) # 6 tails virus_object = Merge(iso_spine, ico_head, ring, cyl) for tail in tails: virus_object.args.append(tail) return virus_object
def legend(start_position, axis_length): """ Returns the objects of a legend :param start_position: the position where the legend is rendered :param axis_length: the length of each line+arrow in the x, y and z direction :return legend_objects: the objects of a legend """ # Reduce the AXIS_LENGTH by the length of the Cone (1) so that # the total length is exactly the AXIS_LENGTH axis_length -= 1 # Initialize the Cylinder END-position to a COPY of the start position cylinder_coords_end = { 'x': list(start_position), 'y': list(start_position), 'z': list(start_position) } # Add the AXIS_LENGTHs to the corresponding coordinate cylinder_coords_end['x'][0] += axis_length cylinder_coords_end['y'][1] += axis_length cylinder_coords_end['z'][2] += axis_length ''' CREATE THE CYLINDERS''' cylinders = { 'x': Cylinder(start_position, cylinder_coords_end['x'], 0.1, Texture(Pigment('color', [1, 0, 0]), Finish('reflection', 1))), 'y': Cylinder(start_position, cylinder_coords_end['y'], 0.1, Texture(Pigment('color', [0, 0, 1]), Finish('reflection', 1))), 'z': Cylinder(start_position, cylinder_coords_end['z'], 0.1, Texture(Pigment('color', [0, 1, 0]), Finish('reflection', 1))) } # Cone START is the same as the Cylinder END, so we COPY these lists cones_coords_start = { 'x': list(cylinder_coords_end['x']), 'y': list(cylinder_coords_end['y']), 'z': list(cylinder_coords_end['z']) } # Copy the START as END coordinate cones_coords_end = { 'x': list(cones_coords_start['x']), 'y': list(cones_coords_start['y']), 'z': list(cones_coords_start['z']) } # Extend the tip of the cones with length 1 cones_coords_end['x'][0] += 1 cones_coords_end['y'][1] += 1 cones_coords_end['z'][2] += 1 ''' CREATE THE CONES ''' cones = { 'x': Cone(cones_coords_start['x'], 0.3, cones_coords_end['x'], 0, Texture(Pigment('color', [1, 0, 0]), Finish('reflection', 1))), 'y': Cone(cones_coords_start['y'], 0.3, cones_coords_end['y'], 0, Texture(Pigment('color', [0, 0, 1]), Finish('reflection', 1))), 'z': Cone(cones_coords_start['z'], 0.3, cones_coords_end['z'], 0, Texture(Pigment('color', [0, 1, 0]), Finish('reflection', 1))), } # Add ALL objects to a LIST and return legend_objects = list(cylinders.values()) + list(cones.values()) return legend_objects
def _render_structure(self, change=None): """Render the structure with POVRAY.""" if not isinstance(self.structure, Atoms): return self.render_btn.disabled = True omat = np.array(self._viewer._camera_orientation).reshape( 4, 4).transpose() zfactor = norm(omat[0, 0:3]) omat[0:3, 0:3] = omat[0:3, 0:3] / zfactor bb = deepcopy(self.structure) bb.pbc = (False, False, False) for i in bb: ixyz = omat[0:3, 0:3].dot(np.array([i.x, i.y, i.z]) + omat[0:3, 3]) i.x, i.y, i.z = -ixyz[0], ixyz[1], ixyz[2] vertices = [] cell = bb.get_cell() vertices.append(np.array([0, 0, 0])) vertices.extend(cell) vertices.extend([ cell[0] + cell[1], cell[0] + cell[2], cell[1] + cell[2], cell[0] + cell[1] + cell[2], ]) for n, i in enumerate(vertices): ixyz = omat[0:3, 0:3].dot(i + omat[0:3, 3]) vertices[n] = np.array([-ixyz[0], ixyz[1], ixyz[2]]) bonds = [] cutOff = neighborlist.natural_cutoffs( bb) # Takes the cutoffs from the ASE database neighborList = neighborlist.NeighborList(cutOff, self_interaction=False, bothways=False) neighborList.update(bb) matrix = neighborList.get_connectivity_matrix() for k in matrix.keys(): i = bb[k[0]] j = bb[k[1]] v1 = np.array([i.x, i.y, i.z]) v2 = np.array([j.x, j.y, j.z]) midi = v1 + (v2 - v1) * Radius[i.symbol] / (Radius[i.symbol] + Radius[j.symbol]) bond = Cylinder( v1, midi, 0.2, Pigment("color", np.array(Colors[i.symbol])), Finish("phong", 0.8, "reflection", 0.05), ) bonds.append(bond) bond = Cylinder( v2, midi, 0.2, Pigment("color", np.array(Colors[j.symbol])), Finish("phong", 0.8, "reflection", 0.05), ) bonds.append(bond) edges = [] for x, i in enumerate(vertices): for j in vertices[x + 1:]: if (norm(np.cross(i - j, vertices[1] - vertices[0])) < 0.001 or norm(np.cross(i - j, vertices[2] - vertices[0])) < 0.001 or norm(np.cross(i - j, vertices[3] - vertices[0])) < 0.001): edge = Cylinder( i, j, 0.06, Texture( Pigment("color", [212 / 255.0, 175 / 255.0, 55 / 255.0])), Finish("phong", 0.9, "reflection", 0.01), ) edges.append(edge) camera = Camera( "perspective", "location", [0, 0, -zfactor / 1.5], "look_at", [0.0, 0.0, 0.0], ) light = LightSource([0, 0, -100.0], "color", [1.5, 1.5, 1.5]) spheres = [ Sphere( [i.x, i.y, i.z], Radius[i.symbol], Texture(Pigment("color", np.array(Colors[i.symbol]))), Finish("phong", 0.9, "reflection", 0.05), ) for i in bb ] objects = ( [light] + spheres + edges + bonds + [Background("color", np.array(to_rgb(self._viewer.background)))]) scene = Scene(camera, objects=objects) fname = bb.get_chemical_formula() + ".png" scene.render( fname, width=2560, height=1440, antialiasing=0.000, quality=11, remove_temp=False, ) with open(fname, "rb") as raw: payload = base64.b64encode(raw.read()).decode() self._download(payload=payload, filename=fname) self.render_btn.disabled = False