def __init__(self, pos, charge, mass, radius, color, vel, fixed, negligible): """ Creates a new particle with specified properties (in SI units) pos: XYZ starting position of the particle, in meters charge: charge of the particle, in Coulombs mass: mass of the particle, in kg radius: radius of the particle, in meters. No effect on simulation color: color of the particle. If None, a default color will be chosen vel: initial velocity vector, in m/s fixed: if True, particle will remain fixed in place negligible: assume charge is small wrt other charges to speed up calculation """ self.pos = vector(pos) self.radius = radius self.charge = charge self.mass = mass self.vel = vector(vel) self.fixed = fixed self.negligible = negligible self.color = color if vp: self.vtk_actor = vp.add(sphere( pos, r=radius, c=color)) # Sphere representing the particle # vp.addTrail(alpha=0.4, maxlength=1, n=50) # Add a trail behind the particle self.vtk_actor.addTrail(alpha=0.4, maxlength=1, n=50)
def myfnc(key): if not vp.clickedActor or key != 'c': printc('click an actor and press c.', c='r') return cb = sphere(pos=vp.picked3d, r=.05, c='v') vp.render(cb) printc('clicked actor :', vp.clickedActor.legend, c=4) printc('clicked 3D point:', vp.picked3d, c=4) printc('clicked renderer:', vp.clickedRenderer, c=2)
def draw_shapes(self): pos, sz = self.s_size[0], self.s_size[1] sphere0 = sphere(pos, c='gray', r=sz, alpha=.8, wire=1, res=16) sphere1 = sphere(pos, c='gray', r=sz, alpha=.2, res=16) hairsacts=[] for i in range(sphere0.N()): p = sphere0.point(i) newp = self.transform(p) sphere1.point(i, newp) hairsacts.append( arrow(p, newp, s=0.3, alpha=.5) ) zero = vp.point(pos, c='black') x1,x2, y1,y2, z1,z2 = self.target.polydata().GetBounds() tpos = [x1, y2, z1] text1 = vp.text('source vs target', tpos, s=sz/10, c='dg') text2 = vp.text('morphed vs target', tpos, s=sz/10, c='dg') text3 = vp.text('deformation', tpos, s=sz/10, c='dr') vp.show([sphere0, sphere1, zero, text3] + hairsacts, at=2) vp.show([self.msource, self.target, text2], at=1) vp.show([self.source, self.target, text1], at=0, zoom=1.2, interactive=1)
def myloop(*event): global counts # this is needed because counts is being modified: counts += 0.1 # reference 'actor' is not modified so doesnt need to be global # (internal properties of the object are, but python doesn't know..) actor.pos([0, counts / 20, 0]).color(counts) # move cube and change color rp = [u(-1, 1), u(0, 1), u(-1, 1)] # a random position s = sphere(rp, r=0.05, c='blue 0.2') vp.render(s, resetcam=True) # show() would cause exiting the loop # waste cpu time to slow down program for i in range(100000): sin(i) printc('#', end='')
vp = Plotter(title='gas in toroid', verbose=0, axes=0) vp.add(torus(c='g', r=RingRadius, thickness=RingThickness, alpha=.1, wire=1)) ### <-- Atoms = [] poslist = [] plist, mlist, rlist = [],[],[] mass = Matom*Ratom**3/Ratom**3 pavg = np.sqrt(2.*mass*1.5*k*T) # average kinetic energy p**2/(2mass) = (3/2)kT for i in range(Natoms): alpha = 2*np.pi*random() x = RingRadius*np.cos(alpha)*.9 y = RingRadius*np.sin(alpha)*.9 z = 0 Atoms = Atoms + [vp.add(sphere(pos=(x,y,z), r=Ratom, c=i))] ### <-- theta = np.pi*random() phi = 2*np.pi*random() px = pavg*np.sin(theta)*np.cos(phi) py = pavg*np.sin(theta)*np.sin(phi) pz = pavg*np.cos(theta) poslist.append((x,y,z)) plist.append((px,py,pz)) mlist.append(mass) rlist.append(Ratom) pos = np.array(poslist) poscircle = pos p = np.array(plist) m = np.array(mlist) m.shape = (Natoms, 1)
# Generate a time sequence of 3D shapes (from a sphere to a tetrahedron) # as noisy cloud points, and smooth it with Moving Least Squares (smoothMLS3D). # This make a simultaneus fit in 4D (space+time). # smoothMLS3D method returns a vtkActor where points are color coded # in bins of fitted time. # Data itself can suggest a meaningful time separation based on the spatial # distribution of points. # The nr neighbours in the local 4D fitting must be specified. # import numpy as np from vtkplotter import Plotter, sphere, smoothMLS3D vp = Plotter(N=2, axes=0) # generate uniform points on sphere (tol separates points by 2% of actor size) cc = sphere(res=200).clean(tol=0.02).coordinates() a, b, noise = .2, .4, .1 # some random warping paramenters, and noise factor for i in range(5): # generate a time sequence of 5 shapes cs = cc + a * i * cc**2 + b * i * cc**3 # warp sphere in weird ways # set absolute time of points actor, and add 1% noise on positions vp.points(cs, c=i, alpha=0.5).gaussNoise(1.0).time(0.2 * i) vp.show(at=0, zoom=1.4) # show input clouds as func(time) asse = smoothMLS3D(vp.actors, neighbours=50) vp.addScalarBar3D(asse, at=1, pos=(-2, 0, -1)) # color indicates fitted time vp.show(asse, at=1, zoom=1.4, axes=4, interactive=1)
# Example of boolean operations with actors or polydata # from vtkplotter import Plotter, booleanOperation, sphere # declare the instance of the class vp = Plotter(shape=(2, 2), interactive=0, axes=3) # build to sphere actors s1 = sphere(pos=[-.7, 0, 0], c='r', alpha=0.5) s2 = sphere(pos=[0.7, 0, 0], c='g', alpha=0.5) # make 3 different possible operations: b1 = booleanOperation(s1, 'intersect', s2, c='m') b2 = booleanOperation(s1, 'plus', s2, c='b', wire=True) b3 = booleanOperation(s1, 'minus', s2, c=None) # show the result in 4 different subwindows 0->3 vp.show([s1, s2], at=0, legend='2 spheres') vp.show(b1, at=1, legend='intersect') vp.show(b2, at=2, legend='plus') vp.show(b3, at=3, legend='minus') vp.addScalarBar() # adds a scalarbar to the last actor vp.show(interactive=1)
# Work with vtkVolume objects and surfaces. # from vtkplotter import loadImageData, Plotter, Volume, sphere vp = Plotter() # Load a 3D voxel dataset (returns a vtkImageData object): img = loadImageData('data/embryo.slc', spacing=[1, 1, 1]) # Build a vtkVolume object. # A set of transparency values - of any length - can be passed # to define the opacity transfer function in the range of the scalar. # E.g.: setting alphas=[0, 0, 0, 1, 0, 0, 0] would make visible # only voxels with value close to 98.5 (see print output). vol = Volume(img, c='green', alphas=[0, 0.4, 0.9, 1]) # vtkVolume # can relocate volume in space: #vol.scale(0.3).pos([10,100,0]).rotate(90, axis=[0,1,1]) sph = sphere(pos=[100, 100, 100], r=20) # add a dummy surface vp.show([vol, sph], zoom=1.4) # show both vtkVolume and vtkActor
ListPos.append(PossiblePos[n]) del PossiblePos[n] Pos = np.array(ListPos) # Create an array with all the radius and a list with all the masses Radius = np.concatenate((np.array([Rb]), np.array([Rs] * (Nsp - 1)))) Mass = [1.0] + [Ms] * (Nsp - 1) # Create the initial array of velocities at random with big sphere at rest ListVel = [(0., 0.)] for s in range(1, Nsp): ListVel.append((Rb * random.uniform(-1, 1), Rb * random.uniform(-1, 1))) Vel = np.array(ListVel) # Create the spheres Spheres = [vp.add(sphere(pos=(Pos[0][0], Pos[0][1], 0), r=Radius[0], c='red'))] for s in range(1, Nsp): a = vp.add(sphere(pos=(Pos[s][0], Pos[s][1], 0), r=Radius[s], c='blue')) Spheres.append(a) vp.add(grid(sx=screen_w, sy=screen_w)) # Auxiliary variables Id = np.identity(Nsp) Dij = (Radius + Radius[:, np.newaxis])**2 # Matrix Dij=(Ri+Rj)**2 # The main loop pb = ProgressBar(0, 2000, c='r') for i in pb.range(): # Update all positions np.add(Pos, Vel * Dt, Pos) # Fast version of Pos = Pos + Vel*Dt
# Shrink the triangulation of a mesh to make the inside visible # from vtkplotter import Plotter, sphere vp = Plotter() vp.load('data/shapes/teapot.vtk').shrink(0.75) vp.add(sphere(r=0.2).pos([0,0,-0.5])) vp.show(viewup='z')
from vtkplotter import Plotter, sphere from vtkplotter.analysis import surfaceIntersection vp = Plotter() # alpha value (opacity) can be put in color string separated by space,/ car = vp.load('data/shapes/porsche.ply', c='gold, 0.1') s = sphere(r=4, c='v/0.1', wire=1) # color is violet with alpha=0.1 # Intersect car with sphere, c=black, lw=line width contour = surfaceIntersection(car, s, c='k', lw=4) vp.show([car, contour, s], zoom=1.3)
# Example usage of addTrail() # from vtkplotter import Plotter, sin, sphere vp = Plotter(axes=6) s = sphere(c='green', res=24) vp.cutPlane(s, [-0.9, 0, 0], showcut=True) #cut left part of sphere p = vp.point([1, 1, 1], r=12) # add a trail to point p with maximum length 0.5 and 50 segments p.addTrail(c='k', lw=3, maxlength=0.5, n=50) for i in range(200): p.pos([-2 + i / 100., sin(i / 5.) / 15, 0]) vp.render() vp.camera.Azimuth(-0.2) vp.show(resetcam=0)
for k in range(1, N + 1): alpha = np.pi / 5 * k / 10 bob_x.append(bob_x[k - 1] + np.cos(alpha) + np.random.normal(0, .1)) bob_y.append(bob_y[k - 1] + np.sin(alpha) + np.random.normal(0, .1)) # Create the bobs vp = Plotter(title="Multiple Pendulum", axes=0, verbose=0) vp.add( box(pos=(bob_x[0], bob_y[0], 0), length=12, width=12, height=.7, c='k', wire=1)) bob = [vp.add(sphere(pos=(bob_x[0], bob_y[0], 0), r=R / 2, c='gray'))] for k in range(1, N + 1): bob.append( vp.add(cylinder(pos=(bob_x[k], bob_y[k], 0), r=R, height=.3, c=k))) # Create the springs out of N links link = [0] * N for k in range(N): p0 = bob[k].pos() p1 = bob[k + 1].pos() link[k] = vp.add(helix(p0, p1, thickness=.015, r=R / 3, c='gray')) # Create some auxiliary variables x_dot_m = [0] * (N + 1) y_dot_m = [0] * (N + 1) dij = [0] * (N + 1) # array with distances to previous bob
pts = [] for i, longs in enumerate(agrid_reco): ilat = grid_reco.lats()[i] for j, value in enumerate(longs): ilong = grid_reco.lons()[j] th = (90 - ilat) / 57.3 ph = ilong / 57.3 r = value + rbias p = np.array([sin(th) * cos(ph), sin(th) * sin(ph), cos(th)]) * r pts.append(p) return pts vp = Plotter(shape=[2, 2], verbose=0, axes=3, interactive=0) shape1 = sphere(alpha=0.2) shape2 = vp.load('data/shapes/icosahedron.vtk').normalize().lineWidth(1) agrid1, actorpts1 = makegrid(shape1, N) vp.show(at=0, actors=[shape1, actorpts1]) agrid2, actorpts2 = makegrid(shape2, N) vp.show(at=1, actors=[shape2, actorpts2]) vp.camera.Zoom(1.2) vp.interactive = False clm1 = pyshtools.SHGrid.from_array(agrid1).expand() clm2 = pyshtools.SHGrid.from_array(agrid2).expand() # clm1.plot_spectrum2d() # plot the value of the sph harm. coefficients # clm2.plot_spectrum2d()
# ############################################################ g, r = 9.81, Lshaft / 2 I3 = 1 / 2 * M * R**2 # moment of inertia, I, of gyroscope about its own axis I1 = M * r**2 + 1 / 2 * I3 # I about a line through the support, perpendicular to axis phi = psi = thetadot = 0 x = vector(theta, phi, psi) # Lagrangian coordinates v = vector(thetadot, phidot, psidot) # ############################################################ the scene vp = Plotter(verbose=0, axes=0, interactive=0) shaft = cylinder([[0, 0, 0], [Lshaft, 0, 0]], r=.03, c='dg') rotor = cylinder([[Lshaft / 2.2, 0, 0], [Lshaft / 1.8, 0, 0]], r=R, texture='marble') base = sphere([0, 0, 0], c='dg', r=.03) tip = sphere([Lshaft, 0, 0], c='dg', r=.03) gyro = vp.Assembly([shaft, rotor, base, tip]) # group relevant actors pedestal = box([0, -0.63, 0], height=.1, length=.1, width=1, texture='wood5') pedbase = box([0, -1.13, 0], height=.5, length=.5, width=.05, texture='wood5') pedpin = pyramid([0, -.08, 0], axis=[0, 1, 0], s=.05, height=.12, texture='wood5') vp.add([pedestal, pedbase, pedpin]) formulas = vp.load('data/images/gyro_formulas.png', alpha=.9) formulas.scale(.0035).pos(-1.4, -1.1, -1.1) # ############################################################ the physics