def addLegend(): vp = settings.plotter_instance if not utils.isSequence(vp._legend): return # remove old legend if present on current renderer: acs = vp.renderer.GetActors2D() acs.InitTraversal() for i in range(acs.GetNumberOfItems()): a = acs.GetNextItem() if isinstance(a, vtk.vtkLegendBoxActor): vp.renderer.RemoveActor(a) actors = vp.getActors() acts, texts = [], [] for i in range(len(actors)): a = actors[i] if i < len(vp._legend) and vp._legend[i] != "": if isinstance(vp._legend[i], str): texts.append(vp._legend[i]) acts.append(a) elif hasattr(a, "_legend") and a._legend: if isinstance(a._legend, str): texts.append(a._legend) acts.append(a) NT = len(texts) if NT > 20: NT = 20 vtklegend = vtk.vtkLegendBoxActor() vtklegend.SetNumberOfEntries(NT) vtklegend.ScalarVisibilityOff() for i in range(NT): ti = texts[i] if not ti: continue a = acts[i] c = a.GetProperty().GetColor() if c == (1, 1, 1): c = (0.2, 0.2, 0.2) vtklegend.SetEntry(i, a.polydata(), " " + ti, c) pos = vp.legendPos width = vp.legendSize vtklegend.SetWidth(width) vtklegend.SetHeight(width / 5.0 * NT) sx, sy = 1 - width, 1 - width / 5.0 * NT if pos == 1: vtklegend.GetPositionCoordinate().SetValue(0, sy) elif pos == 2: vtklegend.GetPositionCoordinate().SetValue(sx, sy) # default elif pos == 3: vtklegend.GetPositionCoordinate().SetValue(0, 0) elif pos == 4: vtklegend.GetPositionCoordinate().SetValue(sx, 0) vtklegend.UseBackgroundOn() vtklegend.SetBackgroundColor(vp.legendBC) vtklegend.SetBackgroundOpacity(0.6) vtklegend.LockBorderOn() vp.renderer.AddActor(vtklegend)
def __create_time_legend(self): time_legend = vtk.vtkLegendBoxActor() # Create legend actor time_legend.SetNumberOfEntries(1) time_legend.SetPosition( self.__get_legend_position( self.settings.time_legend_location, self.settings.time_legend_height, self.settings.time_legend_width, self.settings.time_legend_offset, ) ) time_legend.SetWidth(self.settings.time_legend_width) time_legend.SetHeight(self.settings.time_legend_height) tprop = vtk.vtkTextProperty() tprop.SetColor(rgb_colors.RGB_WHITE) tprop.SetVerticalJustificationToCentered() time_legend.SetEntryTextProperty(tprop) if self.settings.time_legend_border_display: time_legend.BorderOn() else: time_legend.BorderOff() return time_legend
def set_legend(self, legend_items): if self.legend is not None: self.renderer.RemoveActor(self.legend) if len(legend_items) > 0: self.longest_name = 0 for entry in legend_items: if len(entry[0]) > self.longest_name: self.longest_name = len(entry[0]) self.n_legend_items = len(legend_items) legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(len(legend_items)) for i, entry in enumerate(legend_items): legend.SetEntryString(i, entry[0]) legend.SetEntryColor(i, entry[1]) legend.UseBackgroundOn() legend.SetBackgroundColor((0.1, 0.1, 0.1)) legend.SetPadding(9) self.legend = legend self.renderer.AddActor(self.legend) self.on_resize()
def _create_species_legend(self): species_legend = vtk.vtkLegendBoxActor() # Get number of lines legend_line_numbers = len(self._mapped_species_idset) \ + len(domain_kind_constants.DOMAIN_KIND_NAME) # Create legend actor species_legend.SetNumberOfEntries(legend_line_numbers) species_legend.SetPosition( self._get_legend_position( self.settings.species_legend_location, self.settings.species_legend_height, self.settings.species_legend_width, self.settings.species_legend_offset)) species_legend.SetWidth(self.settings.species_legend_width) species_legend.SetHeight(self.settings.species_legend_height) tprop = vtk.vtkTextProperty() tprop.SetColor(rgb_colors.RGB_WHITE) tprop.SetVerticalJustificationToCentered() species_legend.SetEntryTextProperty(tprop) if self.settings.species_legend_border_display: species_legend.BorderOn() else: species_legend.BorderOff() # Entry legend string to the actor sphere = vtk.vtkSphereSource() # Create legends of particle speices count = 0 for species_id in self._mapped_species_idset: species_legend.SetEntryColor \ (count, self._pattrs[species_id]['color']) species_legend.SetEntryString \ (count, self._pattrs[species_id]['name']) species_legend.SetEntrySymbol(count, sphere.GetOutput()) count += 1 # Create legends of shell spesies offset = count count = 0 for kind, name in domain_kind_constants.DOMAIN_KIND_NAME.items(): species_legend.SetEntryColor \ (offset + count, self._get_domain_color(kind)) species_legend.SetEntrySymbol \ (offset + count, sphere.GetOutput()) species_legend.SetEntryString(offset + count, name) count += 1 return species_legend
def make_legend(entries): legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(len(entries)) legendBox = vtk.vtkCubeSource() legendBox.Update() for i, (color, label) in enumerate(entries): legend.SetEntry(i, legendBox.GetOutput(), label, color) legend.GetPositionCoordinate().SetCoordinateSystemToDisplay() legend.GetPositionCoordinate().SetValue(0, 0) legend.GetPosition2Coordinate().SetCoordinateSystemToDisplay() legend.GetPosition2Coordinate().SetValue(LEGEND_WIDTH, LEGEND_HEIGHT) return legend, make_legend_locations(entries)
def AddLegend(self, entries, bcolor=[0.5, 0.5, 0.5], border=False, pos=None): """ Adds a legend to render window. Entries must be a list containing one string and color entry for each item pos : list Two float list, each float between 0 and 1. For example [0.5, 0.5] would put the legend in the middle of the figure. Example ------- legend_entries = [] legend_entries.append(['Label', 'w']) plobj = PlotClass() plobj.AddMesh(mesh) plobj.AddLegend(legend_entries) plobj.Plot() """ legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(len(entries)) if pos: legend.SetPosition2(pos[0], pos[1]) c = 0 legendface = MakeLegendPoly() for entry in entries: color = ParseColor(entry[1]) legend.SetEntry(c, legendface, entry[0], color) c += 1 legend.UseBackgroundOn() legend.SetBackgroundColor(bcolor) if border: legend.BorderOn() else: legend.BorderOff() # Add to renderer self.renderer.AddActor(legend) return legend
def add_legend(self): hackData = vtk.vtkPolyData() # it is hack to pass value to legend hackData.SetPoints(vtk.vtkPoints()) self.legend = vtk.vtkLegendBoxActor() self.legend.SetNumberOfEntries(3) self.legend.GetEntryTextProperty().SetFontSize(15) self.legend.GetPositionCoordinate( ).SetCoordinateSystemToNormalizedViewport() self.legend.GetPositionCoordinate().SetValue(0, 0) self.legend.GetPosition2Coordinate().SetCoordinateSystemToDisplay() self.legend.GetPosition2Coordinate().SetValue(290, 3 * 30) self.legend.SetEntry(0, hackData, "rotate - left mouse button", [1, 1, 1]) self.legend.SetEntry(1, hackData, "move - middle mouse button (or shift+left)", [1, 1, 1]) self.legend.SetEntry(2, hackData, "scale - right mouse button", [1, 1, 1]) self.render.AddActor(self.legend)
def AddLegend(self, entries, bcolor=[0.5, 0.5, 0.5], border=False): """ Adds a legend to render window. Entries must be a list containing one string and color entry for each item """ legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(len(entries)) c = 0 legendface = MakeLegendPoly() for entry in entries: legend.SetEntry(c, legendface, entry[0], entry[1]) c += 1 legend.UseBackgroundOn() legend.SetBackgroundColor(bcolor) if border: legend.BorderOn() else: legend.BorderOff() # Add to renderer self.ren.AddActor(legend)
def _create_time_legend(self): time_legend = vtk.vtkLegendBoxActor() # Create legend actor time_legend.SetNumberOfEntries(1) time_legend.SetPosition( self._get_legend_position(self.settings.time_legend_location, self.settings.time_legend_height, self.settings.time_legend_width, self.settings.time_legend_offset)) time_legend.SetWidth(self.settings.time_legend_width) time_legend.SetHeight(self.settings.time_legend_height) tprop = vtk.vtkTextProperty() tprop.SetColor(rgb_colors.RGB_WHITE) tprop.SetVerticalJustificationToCentered() time_legend.SetEntryTextProperty(tprop) if self.settings.time_legend_border_display: time_legend.BorderOn() else: time_legend.BorderOff() return time_legend
def render_actors(self, actors, legends=[]): for actor in actors: self.ren.AddActor(actor) if len(legends) > 0: legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(len(legends)) legend.GetEntryTextProperty().SetFontSize(25) legend.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport() legend.GetPositionCoordinate().SetValue(0, 0) legend.GetPosition2Coordinate().SetCoordinateSystemToDisplay() legend.GetPosition2Coordinate().SetValue(250, len(legends)*30) for i, l in enumerate(legends): legend.SetEntry(i, l[2], l[0], l[1]) self.ren.AddActor(legend) self.ren.AddActor(self.init_scale()) self.ren.ResetCamera() self.showMaximized() self.show() self.activateWindow() self.iren.Initialize()
def main(argv): colors = vtk.vtkNamedColors() backgroundColor = colors.GetColor3d("DarkSlateGray") legendBackgroundColor = colors.GetColor3d("SlateGray") originColor = colors.GetColor3d("OrangeRed") centerColor = colors.GetColor3d("Gold") point1Color = colors.GetColor3d("MediumSpringGreen") point2Color = colors.GetColor3d("Brown") xAxisColor = colors.GetColor3d("lime") yAxisColor = colors.GetColor3d("orange") normalColor = colors.GetColor3d("Raspberry") # Create actors planeSource = vtk.vtkPlaneSource() planeSource.SetOrigin(0.0, 0.0, 0.0) planeSource.SetPoint1(1, 0, 0) planeSource.SetPoint2(0, 1.0, 0) planeSource.Update() bounds = planeSource.GetOutput().GetBounds() length = max(bounds[1] - bounds[0], bounds[3] - bounds[2]) planeMapper = vtk.vtkPolyDataMapper() planeMapper.SetInputConnection(planeSource.GetOutputPort()) planeActor = vtk.vtkActor() planeActor.SetMapper(planeMapper) sphereSource = vtk.vtkSphereSource() sphereSource.SetRadius(length * .04) originMapper = vtk.vtkPolyDataMapper() originMapper.SetInputConnection(sphereSource.GetOutputPort()) originActor = vtk.vtkActor() originActor.SetPosition(planeSource.GetOrigin()) originActor.SetMapper(originMapper) originActor.GetProperty().SetDiffuseColor(originColor) centerMapper = vtk.vtkPolyDataMapper() centerMapper.SetInputConnection(sphereSource.GetOutputPort()) centerActor = vtk.vtkActor() centerActor.SetPosition(planeSource.GetCenter()) centerActor.SetMapper(centerMapper) centerActor.GetProperty().SetDiffuseColor(centerColor) point1Mapper = vtk.vtkPolyDataMapper() point1Mapper.SetInputConnection(sphereSource.GetOutputPort()) point1Actor = vtk.vtkActor() point1Actor.SetPosition(planeSource.GetPoint1()) point1Actor.SetMapper(point1Mapper) point1Actor.GetProperty().SetDiffuseColor(point1Color) point2Mapper = vtk.vtkPolyDataMapper() point2Mapper.SetInputConnection(sphereSource.GetOutputPort()) point2Actor = vtk.vtkActor() point2Actor.SetPosition(planeSource.GetPoint2()) point2Actor.SetMapper(point2Mapper) point2Actor.GetProperty().SetDiffuseColor(point2Color) center = vtk.vtkVector3d() origin = vtk.vtkVector3d() normal = vtk.vtkVector3d() point1 = vtk.vtkVector3d() point2 = vtk.vtkVector3d() for i in range(3): point1[i] = planeSource.GetPoint1()[i] point2[i] = planeSource.GetPoint2()[i] origin[i] = planeSource.GetOrigin()[i] center[i] = planeSource.GetCenter()[i] normal[i] = planeSource.GetNormal()[i] * length xAxisPolyData = CreateArrow(length, origin, point1) xAxisMapper = vtk.vtkPolyDataMapper() xAxisMapper.SetInputData(xAxisPolyData) xAxisActor = vtk.vtkActor() xAxisActor.SetMapper(xAxisMapper) xAxisActor.GetProperty().SetDiffuseColor(xAxisColor) yAxisPolyData = CreateArrow(length, origin, point2) yAxisMapper = vtk.vtkPolyDataMapper() yAxisMapper.SetInputData(yAxisPolyData) yAxisActor = vtk.vtkActor() yAxisActor.SetMapper(yAxisMapper) yAxisActor.GetProperty().SetDiffuseColor(yAxisColor) normalPolyData = CreateArrow(length, origin, normal) normalMapper = vtk.vtkPolyDataMapper() normalMapper.SetInputData(normalPolyData) normalActor = vtk.vtkActor() normalActor.SetMapper(normalMapper) normalActor.GetProperty().SetDiffuseColor(normalColor) # Create the RenderWindow, Renderer renderer = vtk.vtkRenderer() legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(7) legend.UseBackgroundOn() legend.SetBackgroundColor(legendBackgroundColor) legend.GetPositionCoordinate().SetValue(.7, .7) legend.GetPosition2Coordinate().SetValue(.3, .3) entry = 0 legend.SetEntry(entry, sphereSource.GetOutput(), "center", centerColor) entry = entry + 1 legend.SetEntry(entry, sphereSource.GetOutput(), "origin", originColor) entry = entry + 1 legend.SetEntry(entry, sphereSource.GetOutput(), "point1", point1Color) entry = entry + 1 legend.SetEntry(entry, sphereSource.GetOutput(), "point2", point2Color) entry = entry + 1 legend.SetEntry(entry, xAxisPolyData, "xAxis", xAxisColor) entry = entry + 1 legend.SetEntry(entry, xAxisPolyData, "yAxis", yAxisColor) entry = entry + 1 legend.SetEntry(entry, xAxisPolyData, "normal", normalColor) renderWindow = vtk.vtkRenderWindow() renderWindow.AddRenderer(renderer) renderWindow.SetWindowName("PlaneSourceDemo") renderWindow.SetSize(640, 480) interactor = vtk.vtkRenderWindowInteractor() interactor.SetRenderWindow(renderWindow) renderer.SetBackground(backgroundColor) renderer.AddActor(planeActor) renderer.AddActor(originActor) renderer.AddActor(centerActor) renderer.AddActor(point1Actor) renderer.AddActor(point2Actor) renderer.AddActor(xAxisActor) renderer.AddActor(yAxisActor) renderer.AddActor(normalActor) renderer.AddActor(legend) renderWindow.Render() renderer.GetActiveCamera().SetPosition(1, 0, 0) renderer.GetActiveCamera().SetFocalPoint(0, 1, 0) renderer.GetActiveCamera().SetViewUp(0, 0, 1) renderer.GetActiveCamera().Azimuth(30) renderer.GetActiveCamera().Elevation(30) renderer.ResetCamera() renderWindow.Render() interactor.Start() return 0
def plot(self, struct): # creates self. data1, legend, filename, fieldname, dim, has_field, tracker, revision if not self.call_config(struct): return self.update_field_name('element_ref') self.update_field_type('scalar') self.update_field_domain('cell') self.update_legend_data() # creates self.src if not self.call_src(): return self.add_outline_2(self.src) scalarrange = self.src.GetOutput().GetScalarRange() #print 'src scalarrange', scalarrange #code prior version 0.0.1 logging.debug(scalarrange) self.get_materials() scalarrange = [0.0, float(self.max) + 1 ] # +1 por si hay otros valores ponerlos a negro #print 'src scalarrange ->', scalarrange #code prior version 0.0.1 logging.debug(scalarrange) self.set_colors() self.wireM = vtk.vtkDataSetMapper() self.wireM.SetInputConnection(self.src.GetOutputPort()) self.wireM.SetScalarRange(scalarrange) self.look = self.wireM.GetLookupTable() self.look.SetTableRange(scalarrange) self.set_lookup() self.lbA = vtk.vtkLegendBoxActor() self.set_legendbox() # print 'pos1', self.lbA.GetPosition() # print 'pos2', self.lbA.GetPosition2() self.lbA.SetPosition(0.8, 0.1) self.lbA.SetPosition2(0.2, 0.8) # self.lbA.LockBorderOn() #nada # self.lbA.GetEntryTextProperty().SetVerticalJustificationToTop() #nada self.wireA = vtk.vtkActor() self.wireA.SetMapper(self.wireM) self.wireA.GetProperty().SetRepresentationToSurface() self.add_sw_2(self.wireA) self.sbA = vtk.vtkScalarBarActor() self.sbA.SetLookupTable(self.look) self.sbA.SetNumberOfLabels(self.look.GetNumberOfTableValues()) self.sbA.SetPosition(0.0, 0.1) self.sbA.SetPosition2(0.2, 0.8) # podese descomentar, está preparado # self.rens[0].AddActor(self.sbA) self.add_opacity_2([self.wireA]) # Opacity: 100%/75%/50%/25%/0% self.rens[0].AddActor(self.lbA) self.rens[0].AddActor(self.wireA) self.done = True
tubeMapper.SetInputConnection(tubeFilter.GetOutputPort()) tubeMapper.ScalarVisibilityOff() tubeActor = vtk.vtkActor() tubeActor.SetMapper(tubeMapper) tubeActor.GetProperty().SetColor(0.8, 0.8, 0.8) tubeActor.GetProperty().SetSpecularColor(1, 1, 1) tubeActor.GetProperty().SetSpecular(0.3) tubeActor.GetProperty().SetSpecularPower(20) tubeActor.GetProperty().SetAmbient(0.2) tubeActor.GetProperty().SetDiffuse(0.8) #Legend legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(6) legend.BoxOn() legend.SetPosition(0, 0.8) #legend.SetEntrySymbol(0, sphere.GetOutput()) #legend.SetEntryString(0,"0.37") #legend.SetEntryColor(0, 0.545098, 0.0, 0.545098) legend.SetEntry(0, sphere.GetOutput(), "0.37", (0.5, 0.0, 0.5)) legend.SetEntry(1, sphere.GetOutput(), "0.68", (1.0, 1.0, 0.0)) legend.SetEntry(2, sphere.GetOutput(), "0.73", (0.0, 0.0, 1.0)) legend.SetEntry(3, sphere.GetOutput(), "0.74", (1.0, 0.0, 0.0)) legend.SetEntry(4, sphere.GetOutput(), "0.77", (0.0, 1.0, 1.0)) legend.SetEntry(5, sphere.GetOutput(), "2.0", (0.0, 1.0, 0.0))
def main(): sphere_source = vtk.vtkSphereSource() sphere_source.SetCenter(0.0, 0.0, 0.0) sphere_source.SetRadius(5000.0) sphere_source.Update() polydata = sphere_source.GetOutput() # Create a mapper mapper = vtk.vtkPolyDataMapper() #if VTK_MAJOR_VERSION <= 5 #mapper.SetInput(polydata) #else mapper.SetInputData(polydata) #endif # Create an actor actor = vtk.vtkActor() actor.SetMapper(mapper) legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(2) colors = vtk.vtkNamedColors() legend_box = vtk.vtkCubeSource() legend_box.Update() color = [0., 0., 0., 0.] colors.GetColor('tomato', color) legend.SetEntry(0, legend_box.GetOutput(), 'Box', color[:3]) color = [0., 0., 0., 0.] colors.GetColor('banana', color) legend.SetEntry(1, sphere_source.GetOutput(), 'Ball', color[:3]) # place legend in lower right legend.GetPositionCoordinate().SetCoordinateSystemToView() legend.GetPositionCoordinate().SetValue(.5, -1.0) legend.GetPosition2Coordinate().SetCoordinateSystemToView() legend.GetPosition2Coordinate().SetValue(1.0, -0.5) legend.UseBackgroundOn() background = [0., 0., 0., 0.] colors.GetColor('warm_grey', background) legend.SetBackgroundColor(background[:3]) # A renderer and render window renderer = vtk.vtkRenderer() render_window = vtk.vtkRenderWindow() render_window.AddRenderer(renderer) # Add the actors to the scene renderer.AddActor(actor) renderer.AddActor(legend) renderer.SetBackground(0,1,1) # Background color cyan # Render an image (lights and cameras are created automatically) render_window.Render() # An interactor render_window_interactor = vtk.vtkRenderWindowInteractor() render_window_interactor.SetRenderWindow(render_window) # Begin mouse interaction render_window_interactor.Start()
def computeContactPointsWithGUI(mesh): print "Computing contact points..." rand = random.Random() rand.seed(time()) ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) segments = mesh.segments components = mesh.components adjacency_info = numpy.zeros (( len(components.keys()) , len(components.keys()) ), 'd') contact_slots = {} adjacency_info_2 = collections.defaultdict(dict) contact_slots_2 = collections.defaultdict(dict) for seg in components.keys(): contact_slots[seg] = [None, ] * len(components[seg]) contact_slots_2[seg] = collections.defaultdict(list) adjacency_info_2[seg] = collections.defaultdict(int) polys = {} for seg in components.keys(): for c in components[seg]: if not c.segmentId in polys: polys[c.segmentId] = [] pts, tris = arrayToPolydata(c.vertices, c.faces) poly = vtk.vtkPolyData() poly.SetPoints(pts) poly.SetPolys(tris) poly.Update() polys[c.segmentId].append(poly) legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries( len(segments.keys()) ) legendSphereSource = vtk.vtkSphereSource() legendSphere = legendSphereSource.GetOutput() leg = 0 for segIdx in segments.keys(): segColor = [rand.random(),0.2,rand.random()] for c in polys[segIdx]: dataMapper = vtk.vtkPolyDataMapper() dataMapper.SetInput(c) model = vtk.vtkActor() model.SetMapper(dataMapper) model.GetProperty().SetColor(segColor) ren.AddActor(model) print "%d: V%d - P%d" % (segIdx, c.GetNumberOfPoints(), c.GetNumberOfPolys() ) legend.SetEntry(leg, legendSphere, "Segment #" + str(segIdx), segColor) leg += 1 s_keys = polys.keys() vertices = vtk.vtkCellArray() points = vtk.vtkPoints() for x in range(len(s_keys) - 1): for y in range(x+1, len(s_keys)): for id_x, c_x in enumerate(polys[s_keys[x]]): for id_y, c_y in enumerate(polys[s_keys[y]]): b = vtk.vtkBooleanOperationPolyDataFilter() b.SetOperationToIntersection() b.SetTolerance(0.1) b.SetInput(0, c_x) b.SetInput(1, c_y) b.Update() p = b.GetOutput(1).GetPoints() if (p.GetNumberOfPoints() > 0): print "Found %d contact points between segment #%d-%d and segment #%d-%d" % (p.GetNumberOfPoints(), s_keys[x], id_x, s_keys[y], id_y) for k in range(p.GetNumberOfPoints()): id = points.InsertNextPoint(p.GetPoint(k)) vertices.InsertNextCell(1) vertices.InsertCellPoint(id) res_points = numpy.array(polydataToArray(p)) contact_slots[s_keys[x]][id_x] = res_points contact_slots[s_keys[y]][id_y] = res_points d_x = dict() d_x['points'] = res_points d_x['seg'] = s_keys[y] d_x['comp'] = id_y contact_slots_2[s_keys[x]][id_x].append(d_x) d_y = dict() d_y['points'] = res_points d_y['seg'] = s_keys[x] d_y['comp'] = id_x contact_slots_2[s_keys[y]][id_y].append(d_y) adjacency_info[x,y] += 1 adjacency_info[y,x] += 1 adjacency_info_2[s_keys[x]][s_keys[y]] += 1 adjacency_info_2[s_keys[y]][s_keys[x]] += 1 points.Modified() vertices.Modified() print "Adjacency info" for k in s_keys: s = "\t %d | " % k for k_2 in s_keys: s += " %d |" % (adjacency_info_2[k][k_2]) print s for s_k, s_v in contact_slots_2.items(): print "Segment %d" % s_k for c_k, c_v in s_v.items(): print "\t Component %d" % c_k for data in c_v: print "\t\t Seg: %d Comp: %d Points: %d" % (data['seg'], data['comp'], len(data['points'])) data = vtk.vtkPolyData() data.SetPoints(points) data.SetVerts(vertices) dataMapper = vtk.vtkPolyDataMapper() dataMapper.SetInput(data) model = vtk.vtkActor() model.SetMapper(dataMapper) model.GetProperty().SetColor(0.0, 1.0, 0.0) model.GetProperty().SetPointSize(8.0) ren.AddActor(model) ren.AddActor(legend) ren.SetBackground(0.1,0.2,0.4) renWin.SetSize(500,500) iren.Initialize() iren.Start()
tube_mapper = vtk.vtkPolyDataMapper() tube_mapper.SetInputConnection(tube_filter.GetOutputPort()) tube_mapper.ScalarVisibilityOff() tube_actor = vtk.vtkActor() tube_actor.SetMapper(tube_mapper) tube_actor.GetProperty().SetColor(0.8, 0.8, 0.8) tube_actor.GetProperty().SetSpecularColor(1, 1, 1) tube_actor.GetProperty().SetSpecular(0.3) tube_actor.GetProperty().SetSpecularPower(20) tube_actor.GetProperty().SetAmbient(0.2) tube_actor.GetProperty().SetDiffuse(0.8) # Add keyboard interaction for changing the isovalue interactively. # Nice addition but still too cluttered colorbar = vtk.vtkLegendBoxActor() colorbar.SetNumberOfEntries(6) colorbar.BoxOn() colorbar.SetPosition(0, 0.8) colorbar.SetEntrySymbol(0, sphere.GetOutput()) colorbar.SetEntryString(0, "0.37") colorbar.SetEntryColor(0, 0.545098, 0.0, 0.545098) colorbar.SetEntry(0, sphere.GetOutput(), "0.37", (0.5, 0.0, 0.5)) colorbar.SetEntry(1, sphere.GetOutput(), "0.68", (1.0, 1.0, 0.0)) colorbar.SetEntry(2, sphere.GetOutput(), "0.73", (0.0, 0.0, 1.0)) colorbar.SetEntry(3, sphere.GetOutput(), "0.74", (1.0, 0.0, 0.0)) colorbar.SetEntry(4, sphere.GetOutput(), "0.77", (0.0, 1.0, 1.0)) colorbar.SetEntry(5, sphere.GetOutput(), "2.0", (0.0, 1.0, 0.0))
colorTransferFunction.AddRGBPoint(15, 0.4, 0.4, 0)#stomach volumeProp= vtk.vtkVolumeProperty() volumeProp.SetColor(colorTransferFunction) volumeProp.SetScalarOpacity(opacityTransferFunction) volumeProp.SetInterpolationTypeToLinear() volume = vtk.vtkVolume() volume.SetMapper(volumeMapper) volume.SetProperty(volumeProp) #Legend legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(15) legend.UseBackgroundOn() legend.SetBackgroundColor([1, 1, 1]) legend.GetPositionCoordinate().SetCoordinateSystemToDisplay() legend.GetPositionCoordinate().SetValue(0, 0) legend.GetPosition2Coordinate().SetCoordinateSystemToDisplay() legend.GetPosition2Coordinate().SetValue(150, 300) legendBox = vtk.vtkSphereSource() legendBox.Update() organList = ["Blood", "Brain", "Duodenum", "Eye retina", "Eye white", "Heart", "Ileum", "Kidney", "Large intestine", "Liver", "Lung", "Nerve", "Skeleton", "Spleen", "Stomach"]
def main(): colors = vtk.vtkNamedColors() background_color = colors.GetColor3d("SlateGray") legend_background_color = colors.GetColor3d("Black") origin_color = colors.GetColor3d("Tomato") center_color = colors.GetColor3d("Banana") point1_color = colors.GetColor3d("Peru") point2_color = colors.GetColor3d("Bisque") x_axis_color = colors.GetColor3d("lime") y_axis_color = colors.GetColor3d("orange") normal_color = colors.GetColor3d("Raspberry") # Create actors plane_source = vtk.vtkPlaneSource() plane_source.SetOrigin(0.0, 0.0, 0.0) plane_source.SetPoint1(1, 0, 0) plane_source.SetPoint2(0, 1.0, 0) plane_source.Update() bounds = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] plane_source.GetOutput().GetBounds(bounds) length = max(bounds[1] - bounds[0], bounds[3] - bounds[2]) plane_mapper = vtk.vtkPolyDataMapper() plane_mapper.SetInputConnection(plane_source.GetOutputPort()) plane_actor = vtk.vtkActor() plane_actor.SetMapper(plane_mapper) sphere_source = vtk.vtkSphereSource() sphere_source.SetRadius(length * .04) origin_mapper = vtk.vtkPolyDataMapper() origin_mapper.SetInputConnection(sphere_source.GetOutputPort()) origin_actor = vtk.vtkActor() origin_actor.SetPosition(plane_source.GetOrigin()) origin_actor.SetMapper(origin_mapper) origin_actor.GetProperty().SetDiffuseColor(origin_color) center_mapper = vtk.vtkPolyDataMapper() center_mapper.SetInputConnection(sphere_source.GetOutputPort()) center_actor = vtk.vtkActor() center_actor.SetPosition(plane_source.GetCenter()) center_actor.SetMapper(center_mapper) center_actor.GetProperty().SetDiffuseColor(center_color) point1_mapper = vtk. vtkPolyDataMapper() point1_mapper.SetInputConnection(sphere_source.GetOutputPort()) point1_actor = vtk.vtkActor() point1_actor.SetPosition(plane_source.GetPoint1()) point1_actor.SetMapper(point1_mapper) point1_actor.GetProperty().SetDiffuseColor(point1_color) point2_mapper = vtk.vtkPolyDataMapper() point2_mapper.SetInputConnection(sphere_source.GetOutputPort()) point2_actor = vtk.vtkActor() point2_actor.SetPosition(plane_source.GetPoint2()) point2_actor.SetMapper(point2_mapper) point2_actor.GetProperty().SetDiffuseColor(point2_color) center = [0.0, 0.0, 0.0] origin = [0.0, 0.0, 0.0] normal = [0.0, 0.0, 0.0] point1 = [0.0, 0.0, 0.0] point2 = [0.0, 0.0, 0.0] for i in range(0, 3): point1[i] = plane_source.GetPoint1()[i] point2[i] = plane_source.GetPoint2()[i] origin[i] = plane_source.GetOrigin()[i] center[i] = plane_source.GetCenter()[i] normal[i] = plane_source.GetNormal()[i] * length x_axis_poly_data = create_arrow(length, origin, point1) x_axis_mapper = vtk.vtkPolyDataMapper() x_axis_mapper.SetInputData(x_axis_poly_data) x_axis_actor = vtk.vtkActor() x_axis_actor.SetMapper(x_axis_mapper) x_axis_actor.GetProperty().SetDiffuseColor(x_axis_color) y_axis_poly_data = create_arrow(length, origin, point2) y_axis_mapper = vtk.vtkPolyDataMapper() y_axis_mapper.SetInputData(y_axis_poly_data) y_axis_actor = vtk.vtkActor() y_axis_actor.SetMapper(y_axis_mapper) y_axis_actor.GetProperty().SetDiffuseColor(y_axis_color) normal_poly_data = create_arrow(length, origin, normal) normal_mapper = vtk.vtkPolyDataMapper() normal_mapper.SetInputData(normal_poly_data) normal_actor = vtk.vtkActor() normal_actor.SetMapper(normal_mapper) normal_actor.GetProperty().SetDiffuseColor(normal_color) # Create the render_window, Renderer renderer = vtk.vtkRenderer() legend = vtk.vtkLegendBoxActor() legend.SetNumberOfEntries(7) legend.UseBackgroundOn() legend.SetBackgroundColor(legend_background_color) legend.GetPositionCoordinate().SetValue(.7, .7) legend.GetPosition2Coordinate().SetValue(.3, .3) entry = 0 legend.SetEntry(entry, sphere_source.GetOutput(), "center", center_color) entry += 1 legend.SetEntry(entry, sphere_source.GetOutput(), "origin", origin_color) entry += 1 legend.SetEntry(entry, sphere_source.GetOutput(), "point1", point1_color) entry += 1 legend.SetEntry(entry, sphere_source.GetOutput(), "point2", point2_color) entry += 1 legend.SetEntry(entry, x_axis_poly_data, "xAxis", x_axis_color) entry += 1 legend.SetEntry(entry, x_axis_poly_data, "yAxis", y_axis_color) entry += 1 legend.SetEntry(entry, x_axis_poly_data, "normal", normal_color) entry += 1 render_window = vtk.vtkRenderWindow() render_window.AddRenderer(renderer) render_window.SetSize(640, 480) interactor = vtk.vtkRenderWindowInteractor() interactor.SetRenderWindow(render_window) renderer.SetBackground(background_color) renderer.AddActor(plane_actor) renderer.AddActor(origin_actor) renderer.AddActor(center_actor) renderer.AddActor(point1_actor) renderer.AddActor(point2_actor) renderer.AddActor(x_axis_actor) renderer.AddActor(y_axis_actor) renderer.AddActor(normal_actor) renderer.AddActor(legend) render_window.Render() renderer.GetActiveCamera().SetPosition(1, 0, 0) renderer.GetActiveCamera().SetFocalPoint(0, 1, 0) renderer.GetActiveCamera().SetViewUp(0, 0, 1) renderer.GetActiveCamera().Azimuth(30) renderer.GetActiveCamera().Elevation(30) renderer.ResetCamera() render_window.Render() interactor.Start()