def make_cylinder_mesh(ndiv=7): """ Return a Mesh object representing triangulated cylinder along the z-axis. The radius and the height of the cylinder is 1. """ # First create the circle at the bottom of the cylinder for reference. angle = np.linspace(0, 2 * math.pi, num=ndiv, endpoint=False) xs = np.cos(angle) ys = np.sin(angle) zs = np.zeros(len(xs)) cross_section = np.transpose((xs, ys, zs)) axis = np.array((0, 0, 1)) # Then triangulate the side of the cylinder by connecting bottom and top # circles by series of right triangles. vertices = [] normals = [] for vert in cross_section: vertices.append(vert) vertices.append(vert + axis) normal = normalize(vert) normals.append(normal) normals.append(normal) faces = [] n = len(vertices) for i in range(0, n, 2): faces.append((i, i + 1, (i + 2) % n)) faces.append((i + 1, (i + 2) % n, (i + 3) % n)) vertices = as_float_array(vertices) faces = as_index_array(faces) normals = as_float_array(normals) return Mesh(vertices, faces, normals)
def __init__(self, points, texture, colors=None, color=None, sizes=None, size=None): # Color is white by default. if colors is None: colors = np.empty((len(points), 3), dtype=np.float32) colors[:] = (1, 1, 1) if color is None else color # Size is 1.0 by default. if sizes is None: sizes = np.empty((len(points), ), dtype=np.float32) sizes[:] = 1.0 if size is None else size self._shader = gloo.Program(self.VERT_SHADER, self.FRAG_SHADER) self._shader['position'] = as_float_array(points) self._shader['color'] = as_float_array(colors) self._shader['size'] = as_float_array(sizes) self._shader['u_texture'] = texture I = np.eye(4, dtype=np.float32) self.set_view_matrix(I) self.set_projection_matrix(I)
def __init__(self, mesh, opacity=1): self._shader = gloo.Program(self.VERT_SHADER, self.FRAG_SHADER) self._shader['u_opacity'] = opacity self._shader['position'] = as_float_array(mesh.vertices) self._shader['color'] = as_float_array(mesh.colors) self._edges = gloo.IndexBuffer(extract_edges(mesh.faces)) I = np.eye(4, dtype=np.float32) self.set_model_matrix(I) self.set_view_matrix(I) self.set_projection_matrix(I)
def __init__(self, points, colors=None, color=None, linewidth=1): # Color is white by default. if colors is None: colors = np.empty((len(points), 3), dtype=np.float32) colors[:] = color if color else (1, 1, 1) self._shader = gloo.Program(self.VERT_SHADER, self.FRAG_SHADER) self._shader['position'] = as_float_array(points) self._shader['color'] = as_float_array(colors) self._linewidth = linewidth I = np.eye(4, dtype=np.float32) self.set_model_matrix(I) self.set_view_matrix(I) self.set_projection_matrix(I)
def __init__(self, mesh): self._mesh = mesh self._shader = gloo.Program(self.VERT_SHADER, self.FRAG_SHADER) self._shader['position'] = as_float_array(mesh.vertices) self._shader['normal'] = as_float_array(mesh.normals) self._shader['color'] = as_float_array(mesh.colors) self._faces = gloo.IndexBuffer(mesh.faces) self.set_light(self.DEFAULT_LIGHT) self.set_shininess(self.DEFAULT_SHININESS) I = np.eye(4, dtype=np.float32) self.set_model_matrix(I) self.set_view_matrix(I) self.set_projection_matrix(I)
def __init__(self, texture): # (x,y) coordinates of the quad on which the texture is rendered quad_vertices = as_float_array([(-1, -1), (1, -1), (-1, 1), (1, 1)]) self._shader = gloo.Program(TextureDisplayShader.VERT_SHADER, TextureDisplayShader.FRAG_SHADER) self._shader['u_texture'] = texture self._shader['position'] = quad_vertices
def make_sphere_mesh(ndiv=1): """ Return a Mesh object representing triangulated unit sphere. """ # Subdivide each face of icosahedron mesh ndiv times to get a model of # the unit sphere. icosahedron = make_icosahedron_mesh() vertices = list(icosahedron.vertices) faces = list(icosahedron.faces) for _ in range(ndiv): prev_faces = faces faces = list() used_indices = dict() for face in prev_faces: # Add vertices at the midpoint of each edge of the face: # # f2 f2 # /\ /\ # / \ / \ # / \ ---> i1 /----\ i0 # / \ / \ / \ # /________\ /___\/___\ # f0 f1 f0 i2 f1 # # The midpoint may already be added earlier in processing of one # of the adjacent faces. In that case we reuse the previously- # added vertex and its index saved in used_indices. edges = [(face[1], face[2]), (face[2], face[0]), (face[0], face[1])] mid_indices = [] for s, t in edges: key = tuple(sorted((s, t))) if key in used_indices: index = used_indices[key] else: index = len(vertices) vertex = midpoint_on_sphere(vertices[s], vertices[t]) vertices.append(vertex) used_indices[key] = index mid_indices.append(index) # Build new faces. See the figure above. index_0, index_1, index_2 = mid_indices faces.append((index_0, index_1, index_2)) # i0-i1-i2 faces.append((face[0], index_2, index_1)) # f0-i2-i1 faces.append((index_2, face[1], index_0)) # i2-f1-i0 faces.append((index_1, index_0, face[2])) # i1-i0-f2 vertices = as_float_array(vertices) faces = as_index_array(faces) return Mesh(vertices, faces)
def find_any_normal(vec): """ Return a unit vector perpendicular to the given nonzero vector. """ x, y, z = vec candidates = [(y + z, -x, -x), (-y, z + x, -y), (-z, -z, x + y), (y - z, -x, x), (y, z - x, -y), (-z, z, x - y)] for n in candidates: norm = length(n) if norm > 0: return as_float_array(n) / norm raise Exception('cannot find a normal')
def __init__(self, points, colors=None, color=None): # Color is white by default. if colors is None: colors = np.empty((len(points), 3), dtype=np.float32) colors[:] = (1.0, 1.0, 1.0) if color is None else color self._shader = gloo.Program(self.VERT_SHADER, self.FRAG_SHADER) self._shader['position'] = as_float_array(points) self.set_colors(colors) I = np.eye(4, dtype=np.float32) self.set_view_matrix(I) self.set_projection_matrix(I)
def make_icosahedron_mesh(): """ Return a Mesh object representing triangulated icosahedron. """ t = (1 + math.sqrt(5)) / 2 a = t / math.hypot(1, t) b = 1 / math.hypot(1, t) vertices = as_float_array([(a, b, 0), (-a, b, 0), (a, -b, 0), (-a, -b, 0), (b, 0, a), (b, 0, -a), (-b, 0, a), (-b, 0, -a), (0, a, b), (0, -a, b), (0, a, -b), (0, -a, -b)]) faces = as_index_array([(0, 8, 4), (0, 5, 10), (2, 4, 9), (2, 11, 5), (1, 6, 8), (1, 10, 7), (3, 9, 6), (3, 7, 11), (0, 10, 8), (1, 8, 10), (2, 9, 11), (3, 9, 11), (4, 2, 0), (5, 0, 2), (6, 1, 3), (7, 3, 1), (8, 6, 4), (9, 4, 6), (10, 5, 7), (11, 7, 5)]) return Mesh(vertices, faces)
def create_scene(args): modellers = load_chain_modellers(args.scheme) center = (0, 0, 0) scene_radius = 0.0 shaders = [] records = parse_input(args.coords) def extract_group_tag(record): return record[0] for tag, subrecords in itertools.groupby(records, key=extract_group_tag): # Interpret a part of the tag as the name of the modelling scheme to # use for this subrecords. modeller_name = tag.split(':')[0] if modeller_name not in modellers: raise Exception('unknown modelling scheme: ' + modeller_name) modeller = modellers[modeller_name] points = [] values = [] for _, x, y, z, value in subrecords: points.append((x, y, z)) values.append(value) points = as_float_array(points) shaders.append(modeller.make_shader(points, values)) scene_radius = max(scene_radius, length(points - center).max()) # Add wireframe spherical wall. if args.wall_radius > 0: color, opacity = args.wall_color.split(',', 1) color = vispy.color.Color(color).rgb opacity = float(opacity) wall = make_sphere_mesh(ndiv=WALL_SPHERE_NDIV) wall.vertices *= args.wall_radius wall.colors[:] = color shaders.append(WireframeShader(wall, opacity)) scene_radius = max(scene_radius, args.wall_radius) return TranslucentScene(shaders, args.bgcolor), scene_radius
def copy(self): return Mesh(np.copy(as_float_array(self.vertices)), np.copy(as_index_array(self.faces)), np.copy(as_float_array(self.normals)), np.copy(as_float_array(self.colors)))
def set_light(self, light): self._shader['u_light'] = as_float_array(light)
def set_colors(self, colors): self._shader['color'] = as_float_array(colors)
def set_view_matrix(self, view): self._shader['u_view'] = as_float_array(view)
def set_model_matrix(self, mmat): self._shader['u_model'] = as_float_array(mmat)
def set_projection_matrix(self, proj): self._shader['u_projection'] = as_float_array(proj)