theta = 1.3 # initial polar angle of shaft (from vertical) psidot = -40 # spinning angular velocity (rad/s) phidot = 0 # (try -1 and +1 to get first and second pattern) # ############################################################ 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=3, interactive=0) shaft = vp.cylinder([[0,0,0], [Lshaft,0,0]], r=.03, c='dg') rotor = vp.cylinder([[Lshaft/2.2,0,0],[Lshaft/1.8,0,0]], r=R, texture='marble') base = vp.sphere([ 0, 0, 0], c='dg', r=.03) tip = vp.sphere([Lshaft, 0, 0], c='dg', r=.03) gyro = vp.makeAssembly([shaft, rotor, base, tip]) # group relevant actors 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])
gpos = vector(0, 0, 0) # initial position of spring free end # ############################################################ inits top = vector(0, 2, 0) # where top of spring is held precess = vector(0, 0, 0) # initial momentum of center of mass Fgrav = vector(0, -M*9.81, 0) gaxis = vector(0, 0, 1) # initial orientation of gyroscope gaxis = norm(gaxis) I = 1/2*M*R**2 # moment of inertia of gyroscope Lrot = I*omega*gaxis # angular momentum cm = gpos + 0.5*Ls*gaxis # center of mass of shaft # ############################################################ the scene vp = Plotter(verbose=0, axes=0, interactive=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
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) vp.show('data/ring.gmsh', at=3, c=3, wire=1) vp.show('data/images/dog.jpg', at=4) # 2d images can be loaded the same way vp.show('data/shuttle.obj', at=5, c=5) vp.show('data/shapes/man.vtk', at=6, c=6, axes=2) # show negative axes from (0, 0, 0)
nc,n = conc.shape # nc= nr. of time points, n= nr. of vertices # Create the vtkPlotter instance and position the camera. # (values can be copied in the code by pressing C in the rendering window) vp = Plotter(verbose=0, axes=0, interactive=0) vp.camera.SetPosition(962, -239, 1034) vp.camera.SetFocalPoint(0.0, 0.0, 10.0) vp.camera.SetViewUp(-0.693, -0.479, 0.539) pb = ProgressBar(0,nc, c='g') # a green progress bar for t1 in pb.range(): # for each time point t2=t1+1 if t1==nc-1: t2=t1 # avoid index overflow with last time point vp.actors=[] # clean up the list of actors at each iteration vp.cylinder([0,0,-15], r=260, height=10, texture='marble', res=60) vp.cylinder([0,0, 10], r=260, height=50, wire=1, c='gray', res=60) pts, cols = [],[] for i,p in enumerate(mesh): # for each vertex in the mesh c1, c2 = conc[t1,i], conc[t2,i] cgrad = abs(c2-c1)*cgradfac # intensity of variation gx, gy, gz = np.random.randn(3) # make points wiggle a bit pts.append(p + vector(gx/4, gy/4, gz + c1*20)) cols.append([0, c1, cgrad]) # RGB color vp.points(pts, c=cols, alpha=1.0, r=6) # points actor vp.points(pts, c=cols, alpha=0.1, r=30) # halos actor vp.camera.Azimuth(60/nc) # rotate camera by a fraction vp.show() # show the four new actors at each iteration pb.print()
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.box(pos=(bob_x[0], bob_y[0], 0), length=12, width=12, height=.7, c='k', wire=1) bob = [vp.sphere(pos=(bob_x[0], bob_y[0], 0), r=R / 2, c='gray')] for k in range(1, N + 1): bob.append(vp.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.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 dij_m = [0] * (N + 1) for k in range(1, N + 1): dij[k] = mag([bob_x[k] - bob_x[k - 1], bob_y[k] - bob_y[k - 1]])
nc, n = conc.shape # nc= nr. of time points, n= nr. of vertices # Create the Plotter instance and position the camera. # (values can be copied in the code by pressing C in the rendering window) vp = Plotter(verbose=0, axes=0, interactive=0, size=(700, 700)) vp.camera.SetPosition(962, -239, 1034) vp.camera.SetFocalPoint(0.0, 0.0, 10.0) vp.camera.SetViewUp(-0.693, -0.479, 0.539) pb = ProgressBar(0, nc, c='g') # a green progress bar for t1 in pb.range(): # for each time point t2 = t1 + 1 if t1 == nc - 1: t2 = t1 # avoid index overflow with last time point vp.actors = [] # clean up the list of actors at each iteration vp.cylinder([0, 0, -15], r=260, height=10, texture='marble', res=60) vp.cylinder([0, 0, 10], r=260, height=50, wire=1, c='gray', res=60) pts, cols = [], [] for i, p in enumerate(mesh): # for each vertex in the mesh c1, c2 = conc[t1, i], conc[t2, i] cgrad = abs(c2 - c1) * cgradfac # intensity of variation gx, gy, gz = np.random.randn(3) # make points wiggle a bit pts.append(p + vector(gx / 4, gy / 4, gz + c1 * 20)) cols.append([0., c1, cgrad]) # RGB color vp.points(pts, c=cols, alpha=1.0, r=6) # points actor vp.points(pts, c=cols, alpha=0.1, r=30) # halos actor vp.camera.Azimuth(60 / nc) # rotate camera by a fraction vp.show() # show the four new actors at each iteration pb.print()