class TetVisual(CompoundVisual): """ display a 3D mesh """ def __init__(self, vertices=None, simplices=None, vertex_colors=None, edge_color=None, edge_width=1, markers=None, marker_colors=None, marker_size=1, **kwargs): """ a mesh visualization toolkit that can also plot edges or markers Parameters ---------- Notes ----- """ self._mesh = MeshVisual() self._edge = LineVisual() self._edge_color = Color(edge_color) self._marker = MarkersVisual() # self._vertex_colors = vertex_colors self._update() # initialize visuals CompoundVisual.__init__(self, [self._mesh, self._edge, self._marker], **kwargs) # set default state, 'opaque', 'translucent' or 'additive' self._mesh.set_gl_state( preset="translucent", blend=True, depth_test=False, cull_face=False, polygon_offset_fill=True, polygon_offset=(1, 1), ) # end self.freeze() def _update(self): """ update parameters to visuals """ raise NotImplementedError() @property def edge_color(self): """ get """ return self._edge_color @edge_color.setter def edge_color(self, edge_color): """ set """ self._edge_color = Color(edge_color) self._update()
def __init__(self, vertices, faces, uniform_colour=(0.5, 0.5, 1.0), uniform_opacity=1.0, vertex_colours=None, wireframe=False, wireframe_offset=None): """ Creates a primitive visual based on :class:`vispy.visuals.mesh.MeshVisual` class. Parameters ---------- vertices : array_like Vertices data. faces : array_like Faces data. uniform_colour : array_like, optional Uniform mesh colour. uniform_opacity : numeric, optional Uniform mesh opacity. vertex_colours : array_like, optional Per vertex varying colour. wireframe : bool, optional Use wireframe display. wireframe_offset : array_like, optional Wireframe offset. Notes ----- - `vertex_colours` argument takes precedence over `uniform_colour` if provided. - `uniform_opacity` argument will be stacked to `vertex_colours` argument if the latter last dimension is equal to 3. """ self._wireframe = wireframe self._wireframe_offset = wireframe_offset mode = 'lines' if self._wireframe else 'triangles' uniform_colour = ColorArray(uniform_colour, alpha=uniform_opacity).rgba if vertex_colours is not None: if vertex_colours.shape[-1] == 3: vertex_colours = np.hstack( (vertex_colours, np.full((vertex_colours.shape[0], 1), uniform_opacity, DEFAULT_FLOAT_DTYPE))) else: vertex_colours[..., 3] = uniform_opacity MeshVisual.__init__( self, vertices, faces, vertex_colours, None, uniform_colour, mode=mode)
def test_empty_mesh_wireframe(): """Test that an empty mesh does not cause issues with wireframe filter""" mesh = MeshVisual() wf = WireframeFilter() mesh.attach(wf) # trigger update of filter wf.enabled = True
class TetVisual(CompoundVisual): """ display a 3D mesh """ def __init__(self, vertices=None, simplices=None, vertex_colors=None, edge_color=None, edge_width=1, markers=None, marker_colors=None, marker_size=1, **kwargs): """ a mesh visualization toolkit that can also plot edges or markers Parameters ---------- Notes ----- """ self._mesh = MeshVisual() self._edge = LineVisual() self._edge_color = Color(edge_color) self._marker = MarkersVisual() # self._vertex_colors = vertex_colors self._update() # initialize visuals CompoundVisual.__init__(self, [self._mesh, self._edge, self._marker], **kwargs) # set default state, 'opaque', 'translucent' or 'additive' self._mesh.set_gl_state(preset='translucent', blend=True, depth_test=False, cull_face=False, polygon_offset_fill=True, polygon_offset=(1, 1)) # end self.freeze() def _update(self): """ update parameters to visuals """ pass @property def edge_color(self): """ get """ return self._edge_color @edge_color.setter def edge_color(self, edge_color): """ set """ self._edge_color = Color(edge_color) self._update()
def __init__(self, mu, cov, std=2., cols=15, rows=15, depth=15, subdivisions=3, method='latitude', vertex_colors=None, face_colors=None, color=(0.5, 0.5, 1, 1), edge_color=None, **kwargs): mesh = create_sphere(rows, cols, depth, radius=std, subdivisions=subdivisions, method=method) # Take the spherical mesh and project through the covariance, like you do # when sampling a multivariate gaussian. # https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Drawing_values_from_the_distribution A = np.linalg.cholesky(cov) verts = mesh.get_vertices() verts2 = np.matmul(A[None, :, :], verts[:, :, None]) verts3 = verts2 + mu[None, :, None] mesh.set_vertices(np.squeeze(verts3)) self._mesh = MeshVisual(vertices=mesh.get_vertices(), faces=mesh.get_faces(), vertex_colors=vertex_colors, face_colors=face_colors, color=color) if edge_color: self._border = MeshVisual(vertices=mesh.get_vertices(), faces=mesh.get_edges(), color=edge_color, mode='lines') else: self._border = MeshVisual() CompoundVisual.__init__(self, [self._mesh, self._border], **kwargs) self.mesh.set_gl_state(polygon_offset_fill=True, polygon_offset=(1, 1), depth_test=True)
def __init__(self, data=None, level=None, vertex_colors=None, face_colors=None, color=(0.5, 0.5, 1, 1), **kwargs): self._data = None self._level = level self._vertex_colors = vertex_colors self._face_colors = face_colors self._color = Color(color) # We distinguish between recomputing and just changing the visual # properties - in the latter case we don't recompute the faces. self._vertices_cache = None self._faces_cache = None self._recompute = True self._update_meshvisual = True MeshVisual.__init__(self, **kwargs) if data is not None: self.set_data(data)
def _update_mesh_visual(self): if self._data is None or self._level is None: return False if self._recompute: self._vertices_cache, self._faces_cache = isosurface(np.ascontiguousarray(self._data), self._level) self._recompute = False self._update_meshvisual = True if self._update_meshvisual: MeshVisual.set_data(self, vertices=self._vertices_cache, faces=self._faces_cache, vertex_colors=self._vertex_colors, face_colors=self._face_colors, color=self._color) self._update_meshvisual = False
def _update_mesh_visual(self): if self._data is None or self._level is None: return False if self._recompute: self._vertices_cache, self._faces_cache = isosurface( np.ascontiguousarray(self._data), self._level) self._recompute = False self._update_meshvisual = True if self._update_meshvisual: MeshVisual.set_data(self, vertices=self._vertices_cache, faces=self._faces_cache, vertex_colors=self._vertex_colors, face_colors=self._face_colors, color=self._color) self._update_meshvisual = False
def __init__(self, radius=1.0, directions=None, colors=None): # Convert spherical to cartesian points = np.array([util.tp2xyz(*x) for x in directions]) # Create mesh import scipy.spatial ch = scipy.spatial.ConvexHull(points) mesh = MeshData(vertices=ch.points, faces=ch.simplices) self._mesh = MeshVisual(vertices=mesh.get_vertices(), faces=mesh.get_faces(), vertex_colors=colors) CompoundVisual.__init__(self, [self._mesh]) self.mesh.set_gl_state(depth_test=True)
def draw(self): MeshVisual.draw(self) if self._wireframe and self._wireframe_offset: set_state(polygon_offset=self._wireframe_offset, polygon_offset_fill=True)
def draw(self, transforms): self._update_mesh_visual() return MeshVisual.draw(self, transforms)
def _prepare_draw(self, view): self._update_mesh_visual() return MeshVisual._prepare_draw(self, view)
def draw(self, transforms): MeshVisual.draw(self, transforms) if self.__wireframe and self.__wireframe_offset: set_state(polygon_offset=self.__wireframe_offset, polygon_offset_fill=True)