def showSolution3D(S, start, goal): from vedo import Text3D, Cube, Line, Grid, merge, show pts, cubes, txts = [], [], [] pts = [(x, -y) for y, x in S[0]] for y, line in enumerate(Z): for x, c in enumerate(line): if c: cubes.append(Cube([x, -y, 0])) path = Line(pts).lw(6).c('tomato') walls = merge(cubes).clean().flat().texture('wood1') sy, sx = S[1].shape gradient = np.flip(S[1], axis=0).ravel() grd = Grid(pos=((sx - 1) / 2, -(sy - 1) / 2, -0.49), sx=sx, sy=sy, resx=sx, resy=sy) grd.lw(0).wireframe(False).cmap('gist_earth_r', gradient, on='cells') grd.addScalarBar(title='Gradient', horizontal=True, c='k', nlabels=2) txts.append(__doc__) txts.append(Text3D('Start', pos=[start[1] - 1, -start[0] + 1.5, 1], c='k')) txts.append(Text3D('Goal!', pos=[goal[1] - 2, -goal[0] - 2.7, 1], c='k')) show(path, walls, grd, txts, axes=0, zoom=1.2)
def __init__(self, parent=None): Qt.QMainWindow.__init__(self, parent) self.frame = Qt.QFrame() self.vl = Qt.QVBoxLayout() self.vtkWidget = QVTKRenderWindowInteractor(self.frame) self.vl.addWidget(self.vtkWidget) vp = Plotter(qtWidget=self.vtkWidget, axes=2, N=2) cn = Cone() cc = Cube().pos(1, 1, 1).color("pink") ss = Torus() vp.show(cn, cc, at=0) vp.show(ss, at=1, viewup="z", interactorStyle=0) self.start(vp)
# Add a trail behind the particle self.vsphere.addTrail(alpha=0.4, maxlength=1, n=50) ##################################################################################################### if __name__ == "__main__": # An example simulation of N particles scattering on a charged target. # See e.g. https://en.wikipedia.org/wiki/Rutherford_scattering vp = Plotter(title="Particle Simulator", bg="black", axes=0, interactive=False) vp += Cube(c="white").wireframe(1) # a wireframe cube sim = ParticleSim(dt=5e-6, iterations=200) sim.add_particle((-0.4, 0, 0), color="w", charge=3e-6, radius=0.01, fixed=True) # the target positions = np.random.randn(500, 3) / 60 # generate a beam of 500 particles for p in positions: p[0] = -0.5 # Fix x position. Their charge are small/negligible compared to target: sim.add_particle(p, charge=0.01e-6, mass=0.1e-6,
"""Record and playback camera movements and other events \rightarrow Move the cube around, press 1, and finally press q""" from vedo import Cube, Plotter plt1 = Plotter(axes=1, interactive=0, title="recording window") evts = plt1.show(Cube(), __doc__).record() # print("Events:", evts) # a simple string (also saved as .vedo_recorded_events.log) plt2 = Plotter(axes=1, interactive=0, title="playback window", pos=(1100,0)) plt2.show(Cube(), "...now playing!").play(evts).interactive().close()
"""Show a cube for each available color name""" print(__doc__) from vedo import Cube, Text2D, show, settings from vedo.colors import colors from operator import itemgetter settings.immediateRendering = False # faster for multi-renderers # sorting by hex color code (matplotlib colors): sorted_colors1 = sorted(colors.items(), key=itemgetter(1)) cbs=[] for sc in sorted_colors1: cname = sc[0] if cname[-1] in "123456789": continue cb = Cube().lw(1).color(cname) tname = Text2D(cname, s=0.9) cbs.append([tname, cb]) print("click on any cube and press i or I") show(cbs, N=len(cbs), azimuth=.2, size='full', title="matplotlib colors", interactive=0) # sort by name (bootstrap5 colors): sorted_colors2 = sorted(colors.items(), key=itemgetter(0)) cbs = [] for sc in sorted_colors2: cname = sc[0] if cname[-1] not in "123456789": continue cb = Cube().lw(1).lighting('off').color(cname) cbs.append([cname, cb]) show(cbs, shape=(11,9), azimuth=.2, size=(800,1000), title="bootstrap5 colors", new=True)
""" Show a cube for each available texture name. Any jpg file can be used as texture. """ from vedo import settings, Plotter, Cube, Text2D from vedo.settings import textures, textures_path print(__doc__) print('textures_path:', settings.textures_path) print('textures:', settings.textures) settings.immediateRendering = False plt = Plotter(N=len(settings.textures), axes=0) for i, name in enumerate(settings.textures): if i > 30: break cb = Cube().texture(name) plt.show(cb, name, at=i, azimuth=1) plt.show(interactive=True).close()
self.vsphere = Sphere(pos, r=radius, c=color).addTrail(alpha=0.4, maxlength=1, n=50) vp.add(self.vsphere, render=False) # Sphere representing the particle ##################################################################################################### if __name__ == "__main__": vp = Plotter(title="Particle Simulator", bg="black", axes=0, interactive=False) vp += Cube().c('w').wireframe(True).lighting('off') # a wireframe cube sim = ParticleSim(dt=5e-6, iterations=200) sim.add_particle((-0.4, 0, 0), color="w", charge=3e-6, radius=0.01, fixed=True) # the target positions = np.random.randn(500, 3) / 60 # generate a beam of 500 particles for p in positions: p[0] = -0.5 # Fix x position. Their charge are small/negligible compared to target: sim.add_particle(p, charge=0.01e-6, mass=0.1e-6,
"""Compute the (signed) distance of one mesh to another""" from vedo import Sphere, Cube, show s1 = Sphere().x(10) s2 = Cube(c='grey4').scale([2, 1, 1]).x(14) s1.distanceTo(s2, signed=False) s1.cmap('hot').addScalarBar('Signed\nDistance') # print(s1.pointdata["Distance"]) # numpy array show(s1, s2, __doc__, axes=1, size=(1000, 500), zoom=1.5).close()
""" Show a cube for each available color name """ print(__doc__) from vedo import Cube, Text2D, show from vedo.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)) cbs = [] for i, sc in enumerate(sorted_colors): cname = sc[0] rgb = getColor(cname) cb = Cube(c=rgb) tname = Text2D(cname, pos=3) cbs.append([tname, cb]) print("click on any cube and press i") show(cbs, N=len(sorted_colors), azimuth=.2, size='fullscreen')
"""Computes the signed distance from one mesh to another. """ from vedo import Sphere, Cube, show s1 = Sphere() s2 = Cube(pos=[1, 0, 0], c='white', alpha=0.4) s1.distanceToMesh(s2, signed=True, negate=False) s1.addScalarBar(title='Signed\nDistance') #print(s1.getPointArray("Distance")) show(s1, s2, __doc__)
"""Simulation of an optical system with lenses and mirrors of arbitrary shapes and orientations (points mark the exit locations of photons, many from total internal reflections)""" from vedo import Grid, Sphere, Cube, Cone, Points, show from optics_base import Lens, Ray, Mirror, Screen # see file ./optics_base.py import numpy as np # Create meshes as ordinary vedo objects sm = Sphere(r=8).z(-8.1) sp = Sphere(r=8).z(+8.1) shape1 = Sphere(r=0.9, res=53).cutWithPlane().cap().rotateY(-90).pos(0, 0, 0.5) shape2 = Cube(side=2).triangulate().boolean('-', sm).boolean("-", sp).z(3) shape3 = Cone().rotateY(-90).z(6) shape4 = Cube().scale([1.7, 1, 0.2]).rotateY(70).pos(-0.3, 0, 8) shape5 = Sphere(r=2).boolean("intersect", Sphere(r=2).z(3.5)).rotateX(10).pos(0.8, 0, 7.5) shape6 = Grid(resx=1, resy=1).rotateY(-60).rotateX(30).pos(0, -1, 11) # Build lenses (with their refractive indices), and mirrors, using those meshes lens1 = Lens(shape1, ref_index=1.35).c("blue9") # constant refr. index lens2 = Lens(shape2, ref_index="glass").c("blue7") lens3 = Lens(shape3, ref_index="glass").c("green9") lens4 = Lens(shape4, ref_index="glass").c("purple9").lineWidth(1) lens5 = Lens(shape5, ref_index="glass").c("orange9") mirror = Mirror(shape6) screen = Screen(4, 4).rotateY(20).pos(1, 0, 12) elements = [lens1, lens2, lens3, lens4, lens5, mirror, screen] # Generate photons and trace them through the optical elements lines = [] source = Grid(resx=20, resy=20).points() # a numpy array for pt in source:
"""Record and playback camera movements and other events \rightarrow Move the cube around, press 1, and finally press q""" from vedo import Cube, Plotter, interactive plt1 = Plotter(axes=1, interactive=0, title="recording window") evts = plt1.show(Cube(), __doc__).record() # print("Events:", evts) # a simple string (also saved as .vedo_recorded_events.log) plt2 = Plotter(axes=1, interactive=0, title="playback window", pos=(1100,0)) plt2.show(Cube(), "...now playing!").play(evts) interactive()
"""Histogram (or plot) in 3D. The size of each cube is proportional to the value at that point""" import numpy as np from vedo import Volume, Cube, Glyph, show # Make up some arbitrary data X, Y, Z = np.mgrid[:4, :8, :8] counts = 50 - ( (X-4)**2 + (Y-4)**2 + (Z-4)**2 ) # This is now a point cloud with an associated array of counts pcloud = Volume(counts).topoints() marker = Cube().scale(0.015) glyphed_pcl = Glyph(pcloud, marker, scaleByScalar=True) glyphed_pcl.cmap('seismic').addScalarBar('counts') show(glyphed_pcl, __doc__, axes=1)