vp.show([a1, pts1], at=0) a2 = a1.subdivide(method=0) # Increasing the number of points of the mesh coords2 = a2.coordinates() pts2 = vp.points(coords2, r=1, legend='#points = ' + str(len(coords2))) vp.show([a2, pts2], at=1, interactive=True) ######################################################################################## # Draw a bunch of simple objects on separate parts of the rendering window: # split window to best accomodate 9 renderers vp = Plotter(N=9, title='basic shapes') vp.sharecam = False # each object can be moved independently vp.show(at=0, actors=vp.arrow([0, 0, 0], [1, 1, 1]), legend='arrow') vp.show(at=1, actors=vp.line([0, 0, 0], [1, 1, 1]), legend='line') vp.show(at=2, actors=vp.points([[0, 0, 0], [1, 1, 1]]), legend='points') vp.show(at=3, actors=vp.text('Hello!')) vp.show(at=4, actors=vp.sphere()) vp.show(at=5, actors=vp.cube(), legend='cube') vp.show(at=6, actors=vp.ring(), legend='ring') vp.show(at=7, actors=vp.helix(), legend='helix') vp.show(at=8, actors=vp.cylinder(), legend='cylinder', interactive=1) ######################################################################################## # Draw a bunch of objects from various mesh formats. Loading is automatic. vp = Plotter(shape=(3, 3), title='mesh formats') # split window in 3 rows and 3 columns vp.sharecam = False # each object can be moved independently vp.show('data/beethoven.ply', at=0, c=0, axes=0, ruler=1) # dont show axes, add a ruler vp.show('data/cow.g', at=1, c=1, zoom=1.15) # make it 15% bigger vp.show('data/limb.pcd', at=2, c=2)
man1.addPointScalars(scals, 'mypointscalars') # add a vtkArray to actor #print(man1.scalars('mypointscalars')) # info can be retrieved this way vp.show(man1, at=0, elevation=-60) vp.addScalarBar() # add a scalarbar to last drawn actor ##################################### pointColors man2 = vp.load('data/shapes/man.vtk') scals = man2.coordinates()[:, 1] + 37 # pick y coordinates of vertices man2.pointColors(scals, cmap='bone', vmin=36.2, vmax=36.7) # right dark arm vp.show(man2, at=1, axes=0, legend='pointColors') vp.addScalarBar(horizontal=True) ##################################### cellColors man3 = vp.load('data/shapes/man.vtk') scals = man3.cellCenters()[:, 2] + 37 # pick z coordinates of cells man3.cellColors(scals, cmap='afmhot') #print(man3.scalars('cellColors_afmhot')) # info can be retrieved this way # add some oriented 3D text txt = vp.text('Floor temperature is 35C', s=.1).rotateZ(90).pos([1, -.9, -1.7]) vp.show([man3, txt], at=2, legend='cellColors') # add a fancier 3D scalar bar embedded in the scene vp.addScalarBar3D(man3, at=2, pos=(-1, 0, -1.7)) vp.show(interactive=1) # N.B. in the above example one can also do: # import matplotlib.cm as cm # man2.pointColors(scals, cmap=cm.bone)
screen = vp.add(grid(pos=[0, 0, -D], sx=0.1, sy=0.1, resx=200, resy=50)) screen.wire(False) # show it as a solid plane (not as wireframe) k = 0.0 + 1j * 2 * pi / lambda1 # complex wave number norm = len(slits) * 5e+5 amplitudes = [] for i, x in enumerate(screen.coordinates()): psi = 0 for s in slits: r = mag(x - s) psi += exp(k * r) / r psi2 = real(psi * conj(psi)) # psi squared amplitudes.append(psi2) screen.point(i, x + [0, 0, psi2 / norm]) # elevate grid in z screen.pointColors(amplitudes, cmap='hot') vp.points(array(slits) * 200, c='w') # slits scale magnified by factor 200 vp.add(grid(sx=0.1, sy=0.1, resx=6, resy=6, c='white/.1')) # add some annotation vp.add(line([0, 0, 0], [0, 0, -D], c='white/.1')) vp.text("source plane", pos=[-.05, -.053, 0], s=.002, c='gray') vp.text('detector plane D = ' + str(D) + ' m', pos=[-.05, -.053, -D], s=.002, c='gray') vp.show(zoom=1.1)
# this is one instance of the class Plotter with 5 raws and 5 columns vp1 = Plotter(shape=(5,5), title='many windows', axes=0) # having set shape=(n,m), script execution after the show() is not held # set a different background color for a specific subwindow (the last one) vp1.renderers[24].SetBackground(.8,.9,.9) # use vtk method SetBackground() # load the actors and give them a name a = vp1.load('data/shapes/airboat.vtk', legend='some legend') b = vp1.load('data/shapes/cessna.vtk', c='red') c = vp1.load('data/shapes/atc.ply') # show a text in each renderer for i in range(22): txt = vp1.text('renderer\nnr.'+str(i), c=i, s=0.5, justify='centered') vp1.show(txt, at=i) vp1.show(a, at=22) vp1.show(b, at=23) vp1.show(c, at=24) ############################################################ # declare a second independent instance of the class Plotter vp2 = Plotter(pos=(500,250), bg=(0.9,0.9,1)) # blue-ish background vp2.load('data/shapes/porsche.ply', legend='an other window') vp2.show() # show and interact with mouse and keyboard
# Show a cube for each available texture name # any jpg file can be used as texture. # from vtkplotter import Plotter from vtkplotter.utils import textures, textures_path print(textures_path) print(textures) vp = Plotter(N=len(textures), axes=0) for i, txt in enumerate(textures): cube = vp.cube(texture=txt) tname = vp.text(txt, pos=3) vp.show([cube, tname], at=i) vp.camera.Elevation(70) vp.camera.Azimuth(10) vp.show(interactive=1)
class VirtualKeyboard: def __init__(self, songname=''): self.KB = dict() self.vp = None self.rightHand = None self.leftHand = None self.vpRH = None self.vpLH = None self.playsounds = True self.verbose = True self.songname = songname self.t0 = 0 # keep track of how many seconds to play self.dt = 0.1 self.speedfactor = 1 self.engagedfingersR = [False]*6 # element 0 is dummy self.engagedfingersL = [False]*6 self.engagedkeysR = [] self.engagedkeysL = [] self.build_keyboard() ####################################################### def makeHandActor(self, f=1): a1, a2, a3, c = (10*f,0,0), (0,7*f,0), (0,0,3*f), (.7,0.3,0.3) palm = ellipsoid(pos=(0,-3,0), axis1=a1, axis2=a2, axis3=a3, alpha=0.6, c=c) wrist= box(pos=(0,-9,0), length=6*f, width=5, height=2, alpha=0.4, c=c) arm = makeAssembly([palm,wrist]) self.vp.actors.append(arm) # add actor to internal list f1 = self.vp.cylinder((-2, 1.5,0), axis=(0,1,0), height=5, r=.8*f, c=c) f2 = self.vp.cylinder((-1, 3 ,0), axis=(0,1,0), height=6, r=.7*f, c=c) f3 = self.vp.cylinder(( 0, 4 ,0), axis=(0,1,0), height=6.2, r=.75*f, c=c) f4 = self.vp.cylinder(( 1, 3.5,0), axis=(0,1,0), height=6.1, r=.7*f, c=c) f5 = self.vp.cylinder(( 2, 2 ,0), axis=(0,1,0), height=5, r=.6*f, c=c) return [arm, f1,f2,f3,f4,f5] def build_RH(self, hand): if self.verbose: print('Building Right Hand..') self.rightHand = hand f = utils.handSizeFactor(hand.size) self.vpRH = self.makeHandActor(f) for limb in self.vpRH: # initial x positions are superseded later limb.x( limb.x()* 2.5 ) limb.addpos([16.5*5+1, -7.5, 3] ) def build_LH(self, hand): ######################### if self.verbose: print('Building Left Hand..') self.leftHand = hand f = utils.handSizeFactor(hand.size) self.vpLH = self.makeHandActor(f) for limb in self.vpLH: limb.x( limb.x()* -2.5 ) #flip limb.addpos([16.5*3+1, -7.5, 3] ) ####################################################### def build_keyboard(self): if self.verbose: print('Building Keyboard..') nts = ("C","D","E","F","G","A","B") tol = 0.12 keybsize = 16.5 # in cm, span of one octave wb = keybsize/7 nr_octaves = 7 span = nr_octaves*wb*7 self.vp = Plotter(title='PianoPlayer '+__version__, axes=0, size=(1400,700), bg='lb', verbose=0) #wooden top and base self.vp.box(pos=(span/2+keybsize, 6, 1), length=span+1, height=3, width= 5, texture='wood5') #top self.vp.box(pos=(span/2+keybsize, 0, -1), length=span+1, height=1, width=17, texture='wood5') self.vp.text('PianoPlayer '+__version__, pos=(18, 5.5, 2), depth=.7) self.vp.text('https://github.com/marcomusy/pianoplayer', pos=(105,4.8,2), depth=.7, s=.8) leggio = self.vp.box(pos=(span/1.55,8,10), length=span/2, height=span/8, width=0.08, c=(1,1,0.9)) leggio.rotateX(-20) self.vp.text('Playing\n\n'+self.songname, s=1.2).rotateX(70).pos([49,11,9]) for ioct in range(nr_octaves): for ik in range(7): #white keys x = ik * wb + (ioct+1)*keybsize +wb/2 tb = self.vp.box(pos=(x,-2,0), length=wb-tol, height=1, width=12, c='white') self.KB.update({nts[ik]+str(ioct+1) : tb}) if not nts[ik] in ("E","B"): #black keys tn=self.vp.box(pos=(x+wb/2,0,1), length=wb*.6, height=1, width=8, c='black') self.KB.update({nts[ik]+"#"+str(ioct+1) : tn}) self.vp.show(interactive=0) self.vp.camera.Azimuth(4) self.vp.camera.Elevation(-30) ##################################################################### def play(self): printc('Press [0-9] to proceed by one note or for more seconds',1) printc('Press Esc to exit.',1) self.vp.keyPressFunction = runTime # enable observer if self.rightHand: self.engagedkeysR = [False]*len(self.rightHand.noteseq) self.engagedfingersR = [False]*6 # element 0 is dummy if self.leftHand: self.engagedkeysL = [False]*len(self.leftHand.noteseq) self.engagedfingersL = [False]*6 t=0.0 while True: if self.rightHand: self._moveHand( 1, t) if self.leftHand: self._moveHand(-1, t) if t > 1000: break t += self.dt # absolute time flows if self.verbose: printc('End of note sequence reached.') self.vp.keyPressFunction = None # disable observer ################################################################### def _moveHand(self, side, t):############# runs inside play() loop if side == 1: c1,c2 = 'tomato', 'orange' engagedkeys = self.engagedkeysR engagedfingers = self.engagedfingersR H = self.rightHand vpH = self.vpRH else: c1,c2 = 'purple', 'mediumpurple' engagedkeys = self.engagedkeysL engagedfingers = self.engagedfingersL H = self.leftHand vpH = self.vpLH for i, n in enumerate(H.noteseq):##################### start, stop, f = n.time, n.time+n.duration, n.fingering if isinstance(f, str): continue if f and stop <= t <= stop+self.dt and engagedkeys[i]: #release key engagedkeys[i] = False engagedfingers[f] = False name = nameof(n) krelease(self.KB[name]) frelease(vpH[f]) self.vp.interactor.Render() for i, n in enumerate(H.noteseq):##################### start, stop, f = n.time, n.time+n.duration, n.fingering if isinstance(f, str): print('Warning: cannot understand lyrics:',f, 'skip note',i) continue if f and start <= t < stop and not engagedkeys[i] and not engagedfingers[f]: #press key if i >= len(H.fingerseq): return engagedkeys[i] = True engagedfingers[f] = True name = nameof(n) if t> self.t0 + self.vp.clock: self.t0 = t self.vp.show(zoom=2, interactive=True) for g in [1,2,3,4,5]: vpH[g].x( side * H.fingerseq[i][g] ) vpH[0].x(vpH[3].x()) # index 0 is arm, put it where middle finger is fpress(vpH[f], c1) kpress(self.KB[name], c2) self.vp.show(zoom=2, interactive=False) if self.verbose: msg = 'meas.'+str(n.measure)+' t='+str(round(t,2)) if side==1: printc((msg,'\t\t\t\tRH.finger', f, 'hit', name), 'b') else: printc((msg, '\tLH.finger', f, 'hit', name), 'm') if self.playsounds: playSound(n, self.speedfactor) else: time.sleep(n.duration*self.speedfactor)
# Show a cube for each available color name # from vtkplotter import Plotter from vtkplotter.colors import colors, getColor from operator import itemgetter # sorting by hex color code: sorted_colors = sorted(colors.items(), key=itemgetter(1)) # or by name: # sorted_colors = sorted(colors.items(), key=itemgetter(0)) vp = Plotter(N=len(sorted_colors), axes=0, size='fullscreen') for i, sc in enumerate(sorted_colors): cname = sc[0] rgb = getColor(cname) cube = vp.cube(c=rgb) tname = vp.text(cname, pos=3) vp.show([cube, tname], at=i) print('click on any cube and press i') vp.show(zoom=1.1, interactive=1)