#initial conditions v = vector(0, 0, 0.2) x = vector(x0, 0, 0) xr = vector(l_rest, 0, 0) sx0 = vector(-0.8, 0, 0) offx = vector(0, 0.3, 0) vp.box(pos=(0, -0.1, 0), length=2.0, width=0.02, height=0.5) #surface vp.box(pos=(-.82, .15, 0), length=.04, width=0.50, height=0.3) #wall block = vp.cube(pos=x, length=0.2, c='t') spring = vp.helix(sx0, x, r=.06, thickness=.01, texture='metal1') pb = ProgressBar(0, 500, c='r') for i in pb.range(): F = -k * (x - xr) - b * v # Force and friction a = F / m # acceleration v = v + a * dt # velocity x = x + v * dt + 1 / 2 * a * dt**2 # position block.pos(x) # update block position spring.stretch(sx0, x) # stretch helix accordingly trace = vp.point(x + offx, c='r/0.5', r=3) # leave a red trace vp.camera.Azimuth(.1) vp.camera.Elevation(.1) vp.render(trace) # add trace to the list of actors and render pb.print('Fx=' + str(F[0])) vp.show(interactive=1)
from __future__ import division, print_function from vtkplotter import Plotter, mag import numpy as np scene = Plotter(title='Lorenz attractor', axes=2, verbose = 0) dt = 0.001 y = [25, -10, -7] # Starting point (initial condition) pts, cols = [], [] scene.point(y, r=20, c='g', alpha=0.3) for t in np.linspace(0,20, int(20/dt)): # Integrate a funny differential equation dydt = np.array([-8/3*y[0]+ y[1]*y[2], -10*(y[1]-y[2]), -y[1]*y[0]+28*y[1]-y[2]]) y = y + dydt * dt c = np.clip( [mag(dydt) * 0.005], 0, 1)[0] # color by speed pts.append(y) cols.append([c,0, 1-c]) scene.points(pts, cols, r=2) scene.show()
pedestal = vp.box([0,-0.63,0], height=.1, length=.1, width=1, texture='wood5') pedbase = vp.box([0,-1.13,0], height=.5, length=.5, width=.05, texture='wood5') pedpin = vp.pyramid([0,-.08,0], axis=[0,1,0], s=.05, height=.12, texture='wood5') formulas = vp.load('data/images/gyro_formulas.png', alpha=.9).scale(.003).pos([-1,-1,-1.1]) # ############################################################ the physics pb = ProgressBar(0, 4, dt, c='b') for i, t in enumerate(pb.range()): st, ct, sp, cp = sin(x[0]), cos(x[0]), sin(x[1]), cos(x[1]) thetadot, phidot, psidot = v # unpack atheta = st*ct*phidot**2 + (M*g*r*st-I3*(psidot+phidot*ct)*phidot*st)/I1 aphi = (I3/I1)*(psidot+phidot*ct)*thetadot/st - 2*ct*thetadot*phidot/st apsi = phidot*thetadot*st - aphi*ct a = vector(atheta, aphi, apsi) v += a*dt # update velocities x += v*dt # update Lagrangian coordinates gaxis = (Lshaft+0.03)*vector(st*sp, ct, st*cp) # set orientation along gaxis and rotate it around its axis by psidot*t degrees gyro.orientation(gaxis, rotation=psidot*t*57.3) if not i%200: # add trace and render all, every 200 iterations trace = vp.point(gaxis, r=3, c='r') vp.render(trace, resetcam=1) pb.print() vp.show(interactive=1)
# Example usage of addTrail() # from vtkplotter import Plotter, sin vp = Plotter(axes=6) s = vp.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)
shaft = vp.cylinder([[0,0,0], Ls*gaxis], r=0.03, c='dg') rotor = vp.cylinder([(Ls-0.55)*gaxis, (Ls-0.45)*gaxis], r=R, c='t') bar = vp.cylinder([Ls*gaxis/2-R*vector(0,1,0), Ls*gaxis/2+R*vector(0,1,0)], r=R/6, c='r') gyro = vp.Assembly([shaft, rotor, bar]) # group actors into a single one spring= vp.helix(top, gpos, r=0.06, thickness=0.01, c='gray') vp.box(top, length=0.2, width=0.02, height=0.2, c='gray') vp.box(pos=(0,.5,0), length=2.2, width=3, height=2.2, c='gray', wire=1, alpha=.2) # ############################################################ the physics pb = ProgressBar(0, 5, dt, c='b') for t in pb.range(): Fspring = -ks*norm(gpos-top)*(mag(gpos-top)-Lrest) torque = cross(-1/2*Ls*norm(Lrot), Fspring) # torque about center of mass Lrot += torque*dt precess += (Fgrav+Fspring)*dt # momentum of center of mass cm += (precess/M)*dt gpos = cm - 1/2*Ls*norm(Lrot) # set orientation along gaxis and rotate it around its axis by omega*t degrees gyro.orientation(Lrot, rotation=omega*t*57.3).pos(gpos) spring.stretch(top, gpos) vp.point(gpos + Ls*norm(Lrot), r=1, c='g') # add trace point to show in the end vp.render() pb.print() vp.show(interactive=1)
# In this example we fit a plane to regions of a surface defined by # N points that are closest to a given point of the surface. # For some of these point we show the fitting plane. # Blue points are the N points used for fitting. # Green histogram is the distribution of residuals from the fitting. # Both plane center and normal can be accessed from the # attribute actor.center and actor.normal (direction is arbitrary). # from __future__ import division, print_function from vtkplotter import Plotter from vtkplotter.analysis import fitPlane vp = Plotter(verbose=0, axes=0) s = vp.load('data/shapes/cow.vtk').alpha(0.3).subdivide() # remesh variances = [] for i, p in enumerate(s.coordinates()): if i % 100: continue # skip most points pts = s.closestPoint(p, N=12) # find the N closest points to p plane = fitPlane(pts, bc='r', alpha=0.3) # find the fitting plane vp.actors.append(plane) vp.points(pts) # blue points vp.point(p, c='red 0.2') # mark in red the current point vp.arrow(plane.center, plane.center + plane.normal / 15, c='g') variances.append(plane.variance) vp.histogram(variances, title='variance', c='g') vp.show()
# by moving the points along the normals to the surface # and along the radius of a sphere centered at the center of mass. # At each step we redefine the actor so that the normals are # recalculated for the underlying polydata. # from __future__ import division, print_function from vtkplotter import Plotter, norm, mag from vtkplotter.utils import centerOfMass, averageSize vp = Plotter(axes=0) s = vp.load('data/290.vtk', wire=1) vp.actors.append(s.clone(c='red 1.0', wire=0)) c = centerOfMass(s) vp.point(c) Niter = 4 for t in range(Niter): print('iteration', t) coords = s.coordinates() normals = s.normals() aves = averageSize(s) * 1.5 for i in range(s.N()): n = normals[i] p = coords[i] q = norm(p - c) * aves + c dp = mag(q - p) alongn = n * dp alongr = q - p # bias normal
from __future__ import division, print_function from random import uniform as u from vtkplotter import printc, ProgressBar, Plotter N = 10 # nr of particles along axis s = 0.01 # random step size scene = Plotter(verbose=0, axes=0) scene.plane(pos=[.44, .44, -.1], texture='wood7') for i in range(N): # generate a grid of points for j in range(N): for k in range(N): p = [i / N, j / N, k / N] scene.point(p, c=p) # color point by its own position pb = ProgressBar(0, 80, c='red') for t in pb.range(): # loop of 400 steps pb.print() for i in range(1, N * N * N): # for each particle actor = scene.actors[i] r = [u(-s, s), u(-s, s), u(-s, s)] # random step p = actor.pos() # get point position q = p + r # add the noise if q[2] < 0: q[2] *= -1 # if bounce on the floor actor.pos(q) # set its new position scene.camera.Azimuth(.5) scene.camera.Roll(-.5) scene.render()
y_eu, v_eu = euler(y_eu, v_eu, t, dt) y_rk, v_rk = rk4(y_rk, v_rk, t, dt) t += dt positions_eu.append(y_eu) # store result of integration positions_rk.append(y_rk) pb.print('Integrate: RK-4 and Euler') #################################################### # Visualize the result #################################################### vp = Plotter(verbose=0, axes=2) # choose axes type nr.2 vp.ytitle = 'u(x,t)' vp.ztitle = '' # will not draw z axis for i in x: vp.point([i, 0, 0], c='green', r=6) pts_actors_eu = vp.actors # save a copy of the actors list pts_actors_eu[0].legend = 'Euler method' vp.actors = [] # clean up the list for i in x: vp.point([i, 0, 0], c='red', r=6) pts_actors_rk = vp.actors # save a copy of the actors list pts_actors_rk[0].legend = 'Runge-Kutta4' # merge the two lists and set it as the current vtkPlotter actors vp.actors = pts_actors_eu + pts_actors_rk # let's also add a fancy background image from wikipedia vp.load('data/images/wave_wiki.png', alpha=.8).scale(0.4).pos([0, -100, -20])