def scalarbar(self, source, color="white", title=None, n_labels=4, bgcolor=None): with warnings.catch_warnings(record=True): # traits bar = self.mlab.scalarbar(source, title=title, nb_labels=n_labels) if color is not None: bar.label_text_property.color = _check_color(color) if bgcolor is not None: from tvtk.api import tvtk bgcolor = np.asarray(bgcolor) bgcolor = np.append(bgcolor, 1.0) * 255. cmap = source.module_manager.scalar_lut_manager lut = cmap.lut ctable = lut.table.to_array() cbar_lut = tvtk.LookupTable() cbar_lut.deep_copy(lut) alphas = ctable[:, -1][:, np.newaxis] / 255. use_lut = ctable.copy() use_lut[:, -1] = 255. vals = (use_lut * alphas) + bgcolor * (1 - alphas) cbar_lut.table.from_array(vals) cmap.scalar_bar.lookup_table = cbar_lut
def save(self, file_name): """Save control point set into a new file FileName. It is not checked whether the file already exists. Further writes out a VTK .lut file and a .jpg file showing the gradients.""" # Ensure that if the input file name had one of the extensions # we'll be writing out ourselves, it gets stripped out first. path_base,ext = splitext(file_name) #print(file_name) if ext.lower() in ['.lut','.jpg','.jpeg','.grad']: ext = '' file_name = path_base + ext # Create the three names for the files we'll be actually # writing out. file_name_grad = file_name + '.grad' file_name_lut = file_name + '.lut' file_name_jpg = file_name + '.jpg' # write control points set. file = open( file_name_grad, "w" ) file.write( "V 2.0 Color Gradient File\n" ) file.write( "ScalingFunction: %s\n" % (self.scaling_function_string) ) file.write( "ScalingParameter: %s\n" % (self.scaling_function_parameter) ) file.write( "ControlPoints: (pos fixed bindings h s v a)\n" ) for control_point in self.control_points: file.write( " %s %s %s %s %s %s %s\n" % ( \ control_point.pos, control_point.fixed, control_point.active_channels, control_point.color.get_hsva()[0], control_point.color.get_hsva()[1], control_point.color.get_hsva()[2], control_point.color.get_hsva()[3] ) ) file.close() # write vtk lookup table. Unfortunatelly these objects don't seem to # have any built in and exposed means of loading or saving them, so # we build the vtk file directly vtk_table = tvtk.LookupTable() self.store_to_vtk_lookup_table(vtk_table) file = open( file_name_lut, "w" ) num_colors = vtk_table.number_of_table_values file.write( "LOOKUP_TABLE UnnamedTable %s\n" % ( num_colors ) ) for idx in range(num_colors): entry = vtk_table.get_table_value(idx) file.write("%.4f %.4f %.4f %.4f\n" % (entry[0],entry[1],entry[2],entry[3])) file.close() # if the python image library is aviable, also generate a small .jpg # file showing how the gradient looks. Based on code from Arnd Baecker. try: import Image except ImportError: pass # we're ready otherwise. no jpg output tho. else: Ny=64 # vertical size of the jpeg im = Image.new("RGBA",(num_colors,Ny)) for nx in range(num_colors): (r,g,b,a) = vtk_table.get_table_value(nx) for ny in range(Ny): im.putpixel((nx,ny),(int(255*r),int(255*g),int(255*b), int(255*a))) im.save(file_name_jpg,"JPEG")
def make_test_table(lut=False): from ctf import ColorTransferFunction, PiecewiseFunction if lut: table = tvtk.LookupTable() table.table_range = (255, 355) return table, None, None else: table = tvtk.VolumeProperty() ctf = ColorTransferFunction() mins, maxs = 255, 355 ds = (maxs-mins)/4.0 try: ctf.range = (mins, maxs) except Exception: # VTK versions < 5.2 don't seem to need this. pass ctf.add_rgb_point(mins, 0.00, 0.0, 1.00) ctf.add_rgb_point(mins+ds, 0.25, 0.5, 0.75) ctf.add_rgb_point(mins+2*ds, 0.50, 1.0, 0.50) ctf.add_rgb_point(mins+3*ds, 0.75, 0.5, 0.25) ctf.add_rgb_point(maxs, 1.00, 0.0, 0.00) otf = PiecewiseFunction() otf.add_point(255, 0.0) otf.add_point(355, 0.2) table.set_color(ctf) table.set_scalar_opacity(otf) return table, ctf, otf
def set_colormap(self, c): if isinstance(c, str): lut = tvtk.LookupTable() c = gf_colormap(c) lut.number_of_table_values = c.shape[0] for i in range(c.shape[0]): lut.set_table_value(i, c[i, 0], c[i, 1], c[i, 2], 1) elif isinstance(c, tvtk.LookupTable): lut = c else: raise Exception("expected a string or a tvtk.LookupTable") self.lookup_table = lut if (self.mapper is not None): self.mapper.lookup_table = self.lookup_table if (self.scalar_bar is not None): self.scalar_bar.lookup_table = self.lookup_table
def __init__(self, Xmean, tris, components): HasTraits.__init__(self) self._components = components self._max_component_index = len(components) self._Xmean = Xmean self.pd = tvtk.PolyData(points=Xmean, polys=tris) self.pd.point_data.normals = compute_normals( self.pd) # compute normals once for rest-shape (faster) self.actor = tvtk.Actor(mapper=tvtk.PolyDataMapper( input=self.pd, immediate_mode_rendering=True)) self.actor.mapper.lookup_table = tvtk.LookupTable( hue_range=(0.45, 0.6), saturation_range=(0., 0.8), value_range=(.6, 1.), ) self.scene.add_actor(self.actor) self.timer = Timer(40, self.animate().next)
def __init__(self, Xmean, tris, components): HasTraits.__init__(self) self._components = components self._max_component_index = len(components) self._Xmean = Xmean self.pd = tvtk.PolyData(points=Xmean, polys=tris) self.normals = tvtk.PolyDataNormals(splitting=False) configure_input_data(self.normals, self.pd) mapper = tvtk.PolyDataMapper(immediate_mode_rendering=True) self.actor = tvtk.Actor(mapper=mapper) configure_input(self.actor.mapper, self.normals) self.actor.mapper.lookup_table = tvtk.LookupTable( hue_range=(0.45, 0.6), saturation_range=(0., 0.8), value_range=(.6, 1.), ) self.scene.add_actor(self.actor) self.timer = Timer(40, self.animate().next)
def scalarbar(self, source, color="white", title=None, n_labels=4, bgcolor=None): with warnings.catch_warnings(record=True): # traits bar = self.mlab.scalarbar(source, title=title, nb_labels=n_labels) if color is not None: bar.label_text_property.color = _check_color(color) if bgcolor is not None: from tvtk.api import tvtk bgcolor = np.asarray(bgcolor) bgcolor = np.append(bgcolor, 1.0) * 255. cmap = source.module_manager.scalar_lut_manager lut = cmap.lut ctable = lut.table.to_array() cbar_lut = tvtk.LookupTable() cbar_lut.deep_copy(lut) vals = _alpha_blend_background(ctable, bgcolor) cbar_lut.table.from_array(vals) cmap.scalar_bar.lookup_table = cbar_lut return bar
q_file_name=path.join(folder, "combq.bin"), scalar_function_number=100, vector_function_number=200 ) plot3d.update() return plot3d if __name__ == "__main__": plot3d = read_data() grid = plot3d.output.get_block(0) # 创建颜色映射表 import pylab as pl lut = tvtk.LookupTable() lut.table = pl.cm.cool(np.arange(0, 256)) * 255 # 显示StructuredGrid中的一个网格面 plane = tvtk.StructuredGridGeometryFilter(extent=(0, 100, 0, 100, 6, 6)) plane.set_input_data(grid) plane_mapper = tvtk.PolyDataMapper(input_connection=plane.output_port, lookup_table=lut) plane_mapper.scalar_range = grid.scalar_range plane_actor = tvtk.Actor(mapper=plane_mapper) lut2 = tvtk.LookupTable() lut2.table = pl.cm.cool(np.arange(0, 256)) * 255 cut_plane = tvtk.Plane(origin=grid.center, normal=(-0.287, 0, 0.9579)) cut = tvtk.Cutter(cut_function=cut_plane) cut.set_input_data(grid)