def random_box(): xx = randrange(-55,54) yy = randrange(-55,54) zz = randrange(-55,54) x = randrange(0,21) y = randrange(0,21) z = randrange(0,21) red = random() green = random() blue = random() box (pos = (xx,yy,zz), length=x, height=y, width=z, color=(red,green,blue))
def random_box(): xx = randrange(-55, 54) yy = randrange(-55, 54) zz = randrange(-55, 54) x = randrange(0, 21) y = randrange(0, 21) z = randrange(0, 21) red = random() green = random() blue = random() box(pos=(xx, yy, zz), length=x, height=y, width=z, color=(red, green, blue))
def init_scene(self): # init wind field object, mayavi self.func_wind_field_obj_init() # init odor source pos object, visual visual.set_viewer( self.scene) # tell visual to use this scene as the viewer op = Config.get_odor_source_pos() # get init odor source pos self.odor_source = visual.box(pos=(op[0], op[1], op[2]/2.0), length = 0.1, \ height = 0.1, width = op[2], color = (0x05/255.0, 0x5f/255.0,0x58/255.0), ) # init robot drawing obj, visual rp = Config.get_robot_init_pos() self.func_init_robot_shape(rp) # init wind vane obj, visual, indicating mean wind vector self.func_init_wind_vane() # axes and outlines self.func_init_axes_outline() # camera view self.scene.mlab.view(*Config.get_camera_view()) # check if TCP/IP address/port is successfully bind if self.objs_comm_process['sim_state'][1] == 1: if self.enum_comm_mode == 'local': self.textbox_sim_state_display = 'Listenting to localhost:60000' elif self.enum_comm_mode == 'distributed': self.textbox_sim_state_display = 'Listenting to ' \ + self.text_comm_address + ':' + self.text_comm_port else: self.textbox_sim_state_display = "Listenting to what? I don't know" elif self.objs_comm_process['sim_state'][1] == -1: if self.enum_comm_mode == 'local': self.textbox_sim_state_display = 'Error: Cannot bind localhost:60000' elif self.enum_comm_mode == 'distributed': self.textbox_sim_state_display = 'Error: Cannot bind ' \ + self.text_comm_address + ':' + self.text_comm_port else: self.textbox_sim_state_display = "Error: Can't bind what? I forgot"
def create_unicycle(self): visual.set_viewer(self.scene) self.wheel_top = visual.box(color = (0.2, 0.5, 0.5), pos = (0, 1.0, 0), length = 1.6, width = 1.6, height = 0.04) self.spring = visual.helix(coils = 8, axis = (0.0, 1.0, 0.0), color = (0.8, 0.2, 0.8), pos = (0.14, 1.0, 0), radius = 0.1, length = 0.5) self.car = visual.box(color = (0.2, 0.2, 0.8), pos = (0.0, 1.7, 0.0), length = 0.6, height = 0.4, width = 0.6) self.dash_top = visual.cylinder(axis = (0.0, -1.0, 0.0), color = (0.8, 0.8, 0.2), pos = (-0.14, 1.7, 0.0), radius = 0.1, length = 0.3) self.dash_bottom = visual.cylinder(axis = (0.0, 1.0, 0.0), color = (0.8, 0.8, 0.2), pos = (-0.14, 1.0, 0.0), radius = 0.05, length = 0.6) self.scene.camera.azimuth(45) self.scene.camera.elevation(20) self.recompute_char_eq() self.update_chq_data() self.tdata = self.state_buf[:,0] self.ydata = self.state_buf[:,1] self._run_fired()
def main(): # Creating parameters for box size side = 4.0 thk = 0.3 s2 = 2 * side - thk s3 = 2 * side + thk # Creating the 6 walls wallR = box(pos=(side, 0, 0), size=(thk, s3, s2), color=(1, 0, 0)) wallL = box(pos=(-side, 0, 0), size=(thk, s3, s2), color=(1, 0, 0)) wallB = box(pos=(0, -side, 0), size=(s3, thk, s3), color=(0, 0, 1)) wallT = box(pos=(0, side, 0), size=(s3, thk, s3), color=(0, 0, 1)) wallBK = box(pos=(0, 0, -side), size=(s2, s2, thk), color=(0.7, 0.7, 0.7)) # Creating the ball ball = sphere(radius=0.4, color=(0, 1, 0)) ball.vector = vector(-0.15, -0.23, 0.27) side = side - thk * 0.5 - ball.radius ball.t = 0.0 ball.dt = 0.5 def anim(): #Creating the animation function which will be called at #uniform timeperiod through the iterate function ball.t = ball.t + ball.dt ball.pos = ball.pos + ball.vector * ball.dt if not (side > ball.x > -side): ball.vector.x = -ball.vector.x if not (side > ball.y > -side): ball.vector.y = -ball.vector.y if not (side > ball.z > -side): ball.vector.z = -ball.vector.z a = iterate(20, anim) show() return a
def main(): # Creating parameters for box size side = 4.0 thk = 0.3 s2 = 2*side - thk s3 = 2*side + thk # Creating the 6 walls wallR = box( pos = (side, 0, 0), size = (thk, s3, s2), color = (1, 0, 0)) wallL = box( pos = (-side, 0, 0), size = (thk, s3, s2), color = (1, 0, 0)) wallB = box( pos = (0, -side, 0), size = (s3, thk, s3), color = (0, 0, 1)) wallT = box( pos = (0, side, 0), size = (s3, thk, s3), color = (0, 0, 1)) wallBK = box( pos = (0, 0, -side), size = (s2, s2, thk), color = (0.7,0.7,0.7)) # Creating the ball ball = sphere(radius = 0.4, color = (0, 1, 0)) ball.vector = vector(-0.15, -0.23, 0.27) side = side -thk*0.5 - ball.radius ball.t = 0.0 ball.dt = 0.5 def anim(): #Creating the animation function which will be called at #uniform timeperiod through the iterate function ball.t = ball.t + ball.dt ball.pos = ball.pos + ball.vector*ball.dt if not (side > ball.x > -side): ball.vector.x = -ball.vector.x if not (side > ball.y > -side): ball.vector.y = -ball.vector.y if not (side > ball.z > -side): ball.vector.z = -ball.vector.z a = iterate(20, anim) show() return a
def _plot_mayavi(self, viewer=None): from tvtk.tools import visual visual.set_viewer(viewer) trans, rot, zoom, shear = decompose44(self.pos4d) # turn into valid color tuple self.display['color'] = get_color(self.display) # setting color here is more global than in the next line # because this automatically changes the diffuse, ambient, etc. color, too. b = visual.box(pos=trans, size=tuple(np.abs(zoom) * 2), axis=np.dot([1.,0.,0.], rot), color=self.display['color'], viewer=viewer) # No safety net here like for color converting to a tuple. # If the advnaced properties are set you are on your own. for n in b.property.trait_names(): if n in self.display: setattr(b.property, n, self.display[n])
def test_box(self): # When pos = np.array((1.0, 2.0, 3.0)) sz = pos.copy() b = visual.box(size=tuple(sz), pos=pos) # Then bounds = get_bounds(pos, sz) assert_allclose(b.polydata.bounds, bounds) # Given b = visual.box() orig_bounds = (-0.5, 0.5, -0.5, 0.5, -0.5, 0.5) assert_allclose(b.polydata.bounds, orig_bounds) # When b.axis = 1,1,0 # Then x = np.sqrt(2) bounds = get_bounds((0.,0,0), (x, x, 1.0)) assert_allclose(b.polydata.bounds, bounds) # When b.axis = 1,0,0 assert_allclose(b.polydata.bounds, orig_bounds)
def _plot_mayavi(self, viewer=None): from tvtk.tools import visual visual.set_viewer(viewer) trans, rot, zoom, shear = decompose44(self.pos4d) # turn into valid color tuple self.display['color'] = get_color(self.display) # setting color here is more global than in the next line # because this automatically changes the diffuse, ambient, etc. color, too. b = visual.box(pos=trans, size=tuple(np.abs(zoom) * 2), axis=np.dot([1., 0., 0.], rot), color=self.display['color'], viewer=viewer) # No safety net here like for color converting to a tuple. # If the advnaced properties are set you are on your own. for n in b.property.trait_names(): if n in self.display: setattr(b.property, n, self.display[n])
blue = random() box (pos = (xx,yy,zz), length=x, height=y, width=z, color=(red,green,blue)) def wirecube(s): c = curve(color = (1,1,1), radius=1) pts = [(-s, -s, -s),(-s, -s, s), (-s, s, s), (-s, s, -s), (-s, -s, -s), (s, -s, -s), (s, s, -s), (-s, s, -s), (s, s, -s), (s, s, s), (-s, s, s), (s, s, s), (s, -s, s), (-s, -s, s), (s, -s, s),(s, -s, -s)] for pt in pts: c.append(pt) side = 150.0 cube = box(size = (side,side,side), representation = 'w' ) i = 0 while i < 100: random_box() i = i + 1 arrow(axis=(0,12,0), radius_shaft=3.5, color = (1,0,0)) ball = sphere(pos=(-side/2.,-side/2.,-side/2.),color=(1,1,0),radius=3) disk = cylinder(pos=(side/2.,side/2.,-side/2.),color=(.3,.3,1),axis=(1,1,0),radius=5) xx = arange(0,4*pi,pi/10.) spring=curve(color=(1,.7,.1), radius=0.4) for y in xx: spring.append([20+cos(2*y), y/2.-30, -20+sin(2*y)+30]) show()
from tvtk.tools import visual from tvtk.api import colors print("Start") # Create a figure f = mlab.figure(size=(500,500)) # Tell visual to use this as the viewer. visual.set_viewer(f) print("Before test_plot3D") # A silly visualization. mlab.test_plot3d() print("Before box") # Even sillier animation. b1 = visual.box() b2 = visual.box(x=4., color=colors.red) b3 = visual.box(x=-4, color=colors.red) b1.v = 5.0 @mlab.show @mlab.animate(delay=250) def anim(): """Animate the b1 box.""" print("Start animate") while 1: b1.x = b1.x + b1.v*0.1 if b1.x > 2.5 or b1.x < -2.5: b1.v = -b1.v yield
#!/usr/bin/env python # Author: Raashid Baig <*****@*****.**> # License: BSD Style. from tvtk.tools.visual import curve, box, vector, show from numpy import arange, array lorenz = curve( color = (1,1,1), radius=0.3 ) # Draw grid for x in xrange(0,51,10): curve(points = [[x,0,-25],[x,0,25]], color = (0,0.5,0), radius = 0.3 ) box(pos=(x,0,0), axis=(0,0,50), height=0.4, width=0.4, length = 50) for z in xrange(-25,26,10): curve(points = [[0,0,z], [50,0,z]] , color = (0,0.5,0), radius = 0.3 ) box(pos=(25,0,z), axis=(50,0,0), height=0.4, width=0.4, length = 50) dt = 0.01 y = vector(35.0, -10.0, -7.0) pts = [] for i in xrange(2000): # Integrate a funny differential equation dydt = vector( -8.0/3*y[0] + y[1]*y[2], - 10*y[1] + 10*y[2], - y[1]*y[0] + 28*y[1] - y[2]) y = y + dydt * dt pts.append(y)
wbase = 3.*wpedestal # width of base g = 9.8 Fgrav = vector(0,-M*g,0) top = vector(0,0,0) # top of pedestal theta = pi/3. # initial polar angle of shaft (from vertical) thetadot = 0 # initial rate of change of polar angle alpha = 0 # initial spin angle alphadot = 15 # initial rate of change of spin angle (spin ang. velocity) phi = -pi/2. # initial azimuthal angle phidot = 0 # initial rate of change of azimuthal angle # Comment in following line to get pure precession ##phidot = (-alphadot+sqrt(alphadot**2+2*M*g*r*cos(theta)/I))/cos(theta) pedestal = box(pos=top-vector(0,hpedestal/2.,0), height=hpedestal, length=wpedestal, width=wpedestal, color=(0.4,0.4,0.5)) base = box(pos=top-vector(0,hpedestal+tbase/2.,0), height=tbase, length=wbase, width=wbase, color=pedestal.color) shaft = cylinder(axis=(Lshaft,0,0), length = Lshaft, radius=Rshaft, color=(0,1,0)) rotor = cylinder(pos=(Lshaft/2 - Drotor/2, 0, 0), axis=(Drotor, 0, 0), length = Drotor, radius=Rrotor, color=(1,0,0)) gyro = frame(shaft, rotor) gyro.axis = (sin(theta)*sin(phi),cos(theta),sin(theta)*cos(phi)) trail = curve(radius=Rshaft/8., color=(1,1,0))
def __init__(self,random_minerals=True,random_location=True, mineral_location= Location.CENTER, reward = Reward.RELATIVE, grayscale = False, flat = False, mineral_scale = 1.5, start_shift = 0, camera_height = 4, actions = [Action.LEFT,Action.RIGHT,Action.FORWARDS,Action.BACKWARDS,Action.CW,Action.CCW], decorations = False, camera_tilt = 0,start_pos=-23.5, width = 900, height = (500-46),resize_scale=15, x_collision_scale = 1,y_collision_scale = 1,k=5,silver=(.5,.5,.7), random_colors = False,random_lighting=False): self.random_minerals = random_minerals self.random_location = random_location self.mineral_location = mineral_location self.reward = reward self.grayscale = grayscale self.flat = flat self.actions = actions.copy() self.actions_index_dict = self.get_action_index_dictionary() self.camera_height = camera_height self.decorations = decorations self.camera_tilt = camera_tilt self.start_pos = start_pos self.resize_scale = resize_scale mlab.close(all=True) self.width = width self.height = height + 46 self.f = mlab.figure(size=(self.width,self.height),bgcolor = (1,1,1)) self.f.scene._lift() self.square_width = 23.5 self.start_shift = start_shift self.x_collision_scale = x_collision_scale self.y_collision_scale = y_collision_scale self.k = k self.k_max_iterations = 10 self.silver = silver self.random_colors = random_colors self.random_lighting = random_lighting visual.set_viewer(self.f) a_side = 34.5 tape_height = .2 #distance between minerals self.d = 14.5 / np.sqrt(2) #color for silver #silver = (.8,.8,.8) floor_color = (.4,.4,.4) #reate field self.floor_3_3 = visual.box(x=0,y=0,z=-1, length = 23.5*3,height = 23.5*3,width = 2,color = floor_color) #get mineral location locations = self.get_mineral_locations() #self.gold_mineral = visual.box(x=locations[0][0],y=locations[0][1],z=1, length=4,height=4,width=4, color = (1,1,0)) mineral_radius = 2.75 * mineral_scale self.gold_mineral = visual.sphere(x=locations[0][0],y=locations[0][1],z=mineral_radius,radius =mineral_radius,color = (1,1,0) ) self.silver_mineral_1 = visual.sphere(x=locations[1][0],y=locations[1][1],z=mineral_radius,radius =mineral_radius,color = self.silver) self.silver_mineral_2 = visual.sphere(x=locations[2][0],y=locations[2][1],z=mineral_radius,radius =mineral_radius,color = self.silver) #randomly pick the red or blue side r = np.round(np.random.random(1)[0]) b = 1 - r tape_color = (r,0,b) #23.5 is the diameter of a square #place the crater tape self.vertical_lander_tape = visual.box(x=-self.square_width*3/2 + 1,y=a_side/2 - self.square_width*3/2,z=tape_height,length = 2, height = a_side, width = tape_height,color=tape_color) self.h_lander_tape = visual.box(x=-self.square_width*3/2 + a_side/2,y=-a_side/2,z=tape_height,length = 2, height = a_side * np.sqrt(2), width = tape_height,color=tape_color) self.h_lander_tape.rotate(45,axis = [0,0,1],origin = [self.h_lander_tape.x,self.h_lander_tape.y,self.h_lander_tape.z]) self.marker_left = visual.box(x=self.square_width/2 + 1, y =self.square_width,z=tape_height,length=2,height = self.square_width,width=tape_height,color=tape_color) self.marker_right = visual.box(x=3*self.square_width/2 -1, y =self.square_width,z=tape_height,length=2,height = self.square_width,width=tape_height,color=tape_color) self.marker_bottom = visual.box(x=self.square_width,y=self.square_width/2 + 1, z = tape_height,length=self.square_width,height=2,width=tape_height,color=tape_color) self.marker_top = visual.box(x=self.square_width,y=3*self.square_width/2 - 1, z = tape_height,length=self.square_width,height=2,width=tape_height,color=tape_color) #add bars if self.decorations: bar_width = 1.5 bar_height = 1 middle_height = 12 - bar_height*2 middle_color = floor_color bar_length = self.square_width * 3 bar_color = (0,0,0) self.bar1 = visual.box(x=self.square_width*1.5-bar_width/2,y=0,z=tape_height, width= bar_width, height=bar_height,length=bar_length, color = bar_color) self.bar1.rotate(90,axis=[0,0,1],origin=self.bar1.pos) self.bar1m = visual.box(x=self.square_width*1.5-bar_width/2,y=0,z=bar_height+middle_height/2, width= middle_height, height=bar_width,length=bar_length, color = middle_color) self.bar1m.rotate(90,axis=[0,0,1],origin=self.bar1m.pos) self.bar1t = visual.box(x=self.square_width*1.5-bar_width/2,y=0,z=bar_height+middle_height, width= bar_height, height=bar_width,length=bar_length, color = bar_color) self.bar1t.rotate(90,axis=[0,0,1],origin=self.bar1t.pos) self.bar2 = visual.box(x=-self.square_width*1.5+bar_width/2,y=0,z=tape_height, width= bar_width, height=bar_height,length=bar_length, color = bar_color) self.bar2.rotate(90,axis=[0,0,1],origin=self.bar2.pos) self.bar2m = visual.box(x=-self.square_width*1.5+bar_width/2,y=0,z=bar_height+middle_height/2, width= middle_height, height=bar_width,length=bar_length, color = middle_color) self.bar2m.rotate(90,axis=[0,0,1],origin=self.bar2m.pos) self.bar2t = visual.box(x=-self.square_width*1.5+bar_width/2,y=0,z=bar_height+middle_height, width= bar_height, height=bar_width,length=bar_length, color = bar_color) self.bar2t.rotate(90,axis=[0,0,1],origin=self.bar2t.pos) self.bar3 = visual.box(x=0,y=self.square_width*1.5-bar_width/2,z=tape_height, width= bar_width, height=bar_height,length=bar_length, color = bar_color) self.bar3m = visual.box(x=0,y=self.square_width*1.5-bar_width/2,z=bar_height+middle_height/2, width= middle_height, height=bar_width,length=bar_length, color = middle_color) self.bar3t = visual.box(x=0,y=self.square_width*1.5-bar_width/2,z=bar_height+middle_height, width= bar_height, height=bar_width,length=bar_length, color = bar_color) self.bar4 = visual.box(x=0,y=-self.square_width*1.5+bar_width/2,z=tape_height, width= bar_width, height=bar_height,length=bar_length, color = bar_color) self.bar4m = visual.box(x=0,y=-self.square_width*1.5+bar_width/2,z=bar_height+middle_height/2, width= middle_height, height=bar_width,length=bar_length, color = middle_color) self.bar4t = visual.box(x=0,y=-self.square_width*1.5+bar_width/2,z=bar_height+middle_height, width= bar_height, height=bar_width,length=bar_length, color = bar_color) if self.random_colors: height_scale = 40 new_height = bar_height * height_scale self.bar1t.width= new_height self.bar1t.rotate(90,axis=[0,0,1],origin=self.bar1t.pos) self.bar2t.width= new_height self.bar2t.rotate(90,axis=[0,0,1],origin=self.bar2t.pos) self.bar3t.width = new_height self.bar4t.width = new_height self.randomize_colors() self.x, self.y, self.pos_angle = self.get_start_position() self.init_position() if self.random_lighting: self.randomize_lighting() self.move_distance = 2 self.turn_angle = 5
def __init__(self, model, building_layout, building_size, building_spacing, fig=None): self.model = model self.building_layout = np.array(building_layout) self.building_size = building_size self.building_spacing = np.float64(building_spacing) if fig is None: self.fig = mlab.figure(size=(500, 500), bgcolor=(0.1, 0.1, 0.1)) else: self.fig = fig # Set figure for visual objects visual.set_viewer(self.fig) # Convenient local aliases nx, ny = self.building_layout.shape n = nx * ny # Beautiful colors self.building_colors = map( tuple, np.array((np.linspace(0.0, 0.0, n), np.linspace(0.8, 0.3, n), np.linspace(0.3, 0.8, n))).T) # For storing buildings and their locations self.buildings = [] self.building_centers = np.zeros((n, 2)) # Generate buildings for i, x in enumerate( np.linspace(0, (nx - 1) * (self.building_size[0] + self.building_spacing), nx)): for j, y in enumerate( np.linspace( 0, (ny - 1) * (self.building_size[1] + self.building_spacing), ny)): if not self.building_layout[i, j]: continue idx = int(ny * i + j) self.building_centers[idx] = (x, y) self.buildings.append( visual.box(x=x, y=y, z=self.building_size[2] / 2, size=self.building_size, color=self.building_colors[idx])) # Generate ground plane if self.buildings: ground_xx, ground_yy = map( np.transpose, np.meshgrid( np.linspace(np.min(self.building_centers[:, 0] - 10), np.max(self.building_centers[:, 0] + 10), 2), np.linspace(np.min(self.building_centers[:, 1] - 10), np.max(self.building_centers[:, 1] + 10), 2))) self.ground = mlab.surf(ground_xx, ground_yy, np.zeros_like(ground_xx), color=(0, 0.2, 0.2), warp_scale=1) else: self.ground = None # Generate multicopter self.copter_radius = 1.1 * npl.norm( self.model.thruster_positions[0, :2]) self.copter_sticks = np.vstack(([ (0, 0, 0), (self.copter_radius, 0, 0) ], self.model.thruster_positions, (self.copter_radius, 0, 0))).T self.copter_sticks_plot = mlab.plot3d(self.copter_sticks[0, :], self.copter_sticks[1, :], self.copter_sticks[2, :], line_width=5, color=(1, 0, 1)) self.copter_nodes = np.vstack(([0, 0, 0], self.model.thruster_positions)).T self.copter_nodes_plot = mlab.points3d(self.copter_nodes[0, :], self.copter_nodes[1, :], self.copter_nodes[2, :], scale_factor=0.15) self.copter_nodes_plot.glyph.scale_mode = "scale_by_vector" self.copter_nodes_plot.mlab_source.dataset.point_data.scalars = [ 0, 1, 0.7, 0.7, 0.7, 0.7, 1, 0 ] # hack to individually color points self.copter_vecs = [ np.vstack((thr.position, thr.position + 1e-5 * thr.direction)).T for thr in self.model.thruster_list ] self.copter_vecs_plots = [ mlab.plot3d(vec[0, :], vec[1, :], vec[2, :], color=(1, 1, 1)) for vec in self.copter_vecs ] # Aliases for Mayavi animate decorator and show function self.animate = mlab.animate self.show = mlab.show
#!/usr/bin/env python # Author: Raashid Baig <*****@*****.**> # License: BSD Style. from tvtk.tools.visual import curve, box, vector, show from numpy import arange, array lorenz = curve(color=(1, 1, 1), radius=0.3) # Draw grid for x in xrange(0, 51, 10): curve(points=[[x, 0, -25], [x, 0, 25]], color=(0, 0.5, 0), radius=0.3) box(pos=(x, 0, 0), axis=(0, 0, 50), height=0.4, width=0.4, length=50) for z in xrange(-25, 26, 10): curve(points=[[0, 0, z], [50, 0, z]], color=(0, 0.5, 0), radius=0.3) box(pos=(25, 0, z), axis=(50, 0, 0), height=0.4, width=0.4, length=50) dt = 0.01 y = vector(35.0, -10.0, -7.0) pts = [] for i in xrange(2000): # Integrate a funny differential equation dydt = vector(-8.0 / 3 * y[0] + y[1] * y[2], -10 * y[1] + 10 * y[2], -y[1] * y[0] + 28 * y[1] - y[2]) y = y + dydt * dt pts.append(y) if len(pts) > 20:
def plot_trajectory(td, trial_index, state_indices): probjs = td['trials'][trial_index]['problem_instance'] prob = integrator_chains.Problem.loadJSONdict(probjs) state_dim = prob.output_dim * prob.number_integrators try: traj = np.array(td['trials'][trial_index]['trajectory']) except KeyError: print('Trial ' + str(trial_index) + ' does not have an associated trajectory.') sys.exit(-1) if state_indices.strip() == '': state_indices = [] else: state_index_bounds = (2 + prob.output_dim, 2 + prob.output_dim + state_dim - 1) state_indices = _get_index_list(state_indices, state_index_bounds) if state_indices is None: print('One of the requested state indices is out of bounds, ' '[0, ' + str(state_index_bounds[1] - state_index_bounds[0]) + ']') sys.exit(-1) if len(state_indices) == 0: print('Try `--state` option to select indices for plotting.') sys.exit(0) probjsCorrected = json.dumps(probjs) probPlot = integrator_chains.Problem.loadJSON(probjsCorrected) x = traj[:, state_indices] # 1D if len(state_indices) == 1: plt.plot(x.T[0], 'r.-') plt.plot(x.T[0][0], 'r*') plt.show() # 2D elif len(state_indices) == 2: ax = plt.axes() probPlot.Y.plot(ax, color=(1, 1, 1)) ax.hold(True) for lpoly in probPlot.goals + probPlot.obstacles: lpoly.plot(ax, alpha=0.6) xc = np.mean(lpoly.getVrep(), axis=0) ax.text(xc[0], xc[1], lpoly.label) ax.plot(probPlot.Xinit[0], probPlot.Xinit[1], 'o') plt.plot(x.T[0], x.T[1], 'r.-') plt.plot(x.T[0][0], x.T[1][0], 'r*') plt.show() # 3D elif len(state_indices) == 3: import os os.environ["ETS_TOOLKIT"] = "qt4" from mayavi import mlab from tvtk.tools import visual from tvtk.api import tvtk from tvtk.common import configure_input_data f = mlab.figure(size=(500, 500)) # Tell visual to use this as the viewer. visual.set_viewer(f) # mlab.plot3d() of Mayavi fails if there are repetitions of rows, # so we first manually delete any. y = np.zeros((x.shape[0], 3)) y[0] = x[0] i = 1 for j in range(1, x.shape[0]): if (x[j] != x[j - 1]).any(): y[i] = x[j] i += 1 y = y[:(i + 1), :] y = y[0:(len(y) - 1)] mlab.plot3d(y.T[0], y.T[1], y.T[2], color=(1.0, 0.0, 0.0)) mlab.points3d(y.T[0][0], y.T[1][0], y.T[2][0], color=(1.0, 0.0, 0.0), mode='sphere', opacity=1.0, scale_factor=0.5) for lpoly in probPlot.obstacles: xc = np.mean(lpoly.getVrep(), axis=0) bbox = lpoly.get_bbox() aObs = visual.box(x=xc[0], y=xc[1], z=xc[2], length=(bbox[1] - bbox[0]), height=(bbox[3] - bbox[2]), width=(bbox[5] - bbox[4]), color=(1.0, 0.5, 0.5)) aObs.actor.property.opacity = 0.6 for lpoly in probPlot.goals: xc = np.mean(lpoly.getVrep(), axis=0) bbox = lpoly.get_bbox() aGoal = visual.box(x=xc[0], y=xc[1], z=xc[2], length=(bbox[1] - bbox[0]), height=(bbox[3] - bbox[2]), width=(bbox[5] - bbox[4]), color=(0.0, 1.0, 0.0)) aGoal.actor.property.opacity = 0.2 mlab.show() else: print('Plotting of trajectories requires selection of 2 or 3 indices.' '\nTry `--state` option to select indices for plotting.' '\nNow changed to work for 1D too!') sys.exit(0)
g = 9.8 Fgrav = vector(0, -M * g, 0) top = vector(0, 0, 0) # top of pedestal theta = pi / 3. # initial polar angle of shaft (from vertical) thetadot = 0 # initial rate of change of polar angle alpha = 0 # initial spin angle alphadot = 15 # initial rate of change of spin angle (spin ang. velocity) phi = -pi / 2. # initial azimuthal angle phidot = 0 # initial rate of change of azimuthal angle # Comment in following line to get pure precession ##phidot = (-alphadot+sqrt(alphadot**2+2*M*g*r*cos(theta)/I))/cos(theta) pedestal = box(pos=top - vector(0, hpedestal / 2., 0), height=hpedestal, length=wpedestal, width=wpedestal, color=(0.4, 0.4, 0.5)) base = box(pos=top - vector(0, hpedestal + tbase / 2., 0), height=tbase, length=wbase, width=wbase, color=pedestal.color) shaft = cylinder(axis=(Lshaft, 0, 0), length=Lshaft, radius=Rshaft, color=(0, 1, 0)) rotor = cylinder(pos=(Lshaft / 2 - Drotor / 2, 0, 0), axis=(Drotor, 0, 0), length=Drotor,
# Author: Prabhu Ramachandran <prabhu [at] aero.iitb.ac.in> # Copyright (c) 2009, Enthought, Inc. # License: BSD Style. from mayavi import mlab from tvtk.tools import visual # Create a figure f = mlab.figure(size=(500, 500)) # Tell visual to use this as the viewer. visual.set_viewer(f) # A silly visualization. mlab.test_plot3d() # Even sillier animation. b1 = visual.box() b2 = visual.box(x=4., color=visual.color.red) b3 = visual.box(x=-4, color=visual.color.red) b1.v = 5.0 @mlab.show @mlab.animate(delay=250) def anim(): """Animate the b1 box.""" while 1: b1.x = b1.x + b1.v * 0.1 if b1.x > 2.5 or b1.x < -2.5: b1.v = -b1.v yield
width=z, color=(red, green, blue)) def wirecube(s): c = curve(color=(1, 1, 1), radius=1) pts = [(-s, -s, -s), (-s, -s, s), (-s, s, s), (-s, s, -s), (-s, -s, -s), (s, -s, -s), (s, s, -s), (-s, s, -s), (s, s, -s), (s, s, s), (-s, s, s), (s, s, s), (s, -s, s), (-s, -s, s), (s, -s, s), (s, -s, -s)] for pt in pts: c.append(pt) side = 150.0 cube = box(size=(side, side, side), representation='w') i = 0 while i < 100: random_box() i = i + 1 arrow(axis=(0, 12, 0), radius_shaft=3.5, color=(1, 0, 0)) ball = sphere(pos=(-side / 2., -side / 2., -side / 2.), color=(1, 1, 0), radius=3) disk = cylinder(pos=(side / 2., side / 2., -side / 2.), color=(.3, .3, 1), axis=(1, 1, 0), radius=5) xx = arange(0, 4 * pi, pi / 10.)
F = g * L2 * M2 / 2. hpedestal = 1.3 * (L1 + L2) # height of pedestal wpedestal = 0.1 # width of pedestal tbase = 0.05 # thickness of base wbase = 8. * gap # width of base offset = 2. * gap # from center of pedestal to center of U-shaped upper assembly top = vector(0, 0, 0) # top of inner bar of U-shaped upper assembly theta1 = 1.3 * pi / 2. # initial upper angle (from vertical) theta1dot = 0 # initial rate of change of theta1 theta2 = 0 # initial lower angle (from vertical) theta2dot = 0 # initial rate of change of theta2 pedestal = box(pos=(top - vector(0, hpedestal / 2.0, offset)), size=(wpedestal, 1.1 * hpedestal, wpedestal), color=(0.4, 0.4, 0.5)) base = box(pos=(top - vector(0, hpedestal + tbase / 2.0, offset)), size=(wbase, tbase, wbase), color=(0.4, 0.4, 0.5)) bar1 = box(pos=(L1display / 2.0 - d / 2.0, 0, -(gap + d) / 2.0), size=(L1display, d, d), color=(1, 0, 0)) bar1b = box(pos=(L1display / 2.0 - d / 2.0, 0, (gap + d) / 2.0), size=(L1display, d, d), color=(1, 0, 0)) frame1 = frame(bar1, bar1b) frame1.pos = (0.0, 0.0, 0.0)
def __init__(self): mlab.close(all=True) self.width = 900 self.height = 500 self.f = mlab.figure(size=(self.width, self.height), bgcolor=(1, 1, 1)) self.f.scene._lift() visual.set_viewer(self.f) self.square_width = 23.5 a_side = 34.5 tape_height = .2 #distance between minerals d = 14.5 / np.sqrt(2) #color for silver silver = (.8, .8, .8) #reate field self.floor_3_3 = visual.box(x=0, y=0, z=-1, length=23.5 * 3, height=23.5 * 3, width=2, color=(.4, .4, .4)) #randomize starting locations locations = [[-d, d], [0, 0], [d, -d]] #np.random.shuffle(locations) #place minerals #self.gold_mineral = visual.box(x=locations[0][0],y=locations[0][1],z=1, length=4,height=4,width=4, color = (1,1,0)) mineral_radius = 2.75 * 2 self.gold_mineral = visual.sphere(x=locations[0][0], y=locations[0][1], z=mineral_radius, radius=mineral_radius, color=(1, 1, 0)) self.silver_mineral_1 = visual.sphere(x=locations[1][0], y=locations[1][1], z=mineral_radius, radius=mineral_radius, color=silver) self.silver_mineral_2 = visual.sphere(x=locations[2][0], y=locations[2][1], z=mineral_radius, radius=mineral_radius, color=silver) #randomly pick the red or blue side r = np.round(np.random.random(1)[0]) b = 1 - r tape_color = (r, 0, b) #23.5 is the diameter of a square #place the crater tape self.vertical_lander_tape = visual.box( x=-self.square_width * 3 / 2 + 1, y=a_side / 2 - self.square_width * 3 / 2, z=tape_height, length=2, height=a_side, width=tape_height, color=tape_color) self.h_lander_tape = visual.box(x=-self.square_width * 3 / 2 + a_side / 2, y=-a_side / 2, z=tape_height, length=2, height=a_side * np.sqrt(2), width=tape_height, color=tape_color) self.h_lander_tape.rotate(45, axis=[0, 0, 1], origin=[ self.h_lander_tape.x, self.h_lander_tape.y, self.h_lander_tape.z ]) self.marker_left = visual.box(x=self.square_width / 2 + 1, y=self.square_width, z=tape_height, length=2, height=self.square_width, width=tape_height, color=tape_color) self.marker_right = visual.box(x=3 * self.square_width / 2 - 1, y=self.square_width, z=tape_height, length=2, height=self.square_width, width=tape_height, color=tape_color) self.marker_bottom = visual.box(x=self.square_width, y=self.square_width / 2 + 1, z=tape_height, length=self.square_width, height=2, width=tape_height, color=tape_color) self.marker_top = visual.box(x=self.square_width, y=3 * self.square_width / 2 - 1, z=tape_height, length=self.square_width, height=2, width=tape_height, color=tape_color) #mlab.view(focalpoint=[d,d,0],distance=64, elevation=-80) self.x = -(self.square_width - 5) * np.random.random() - 5 self.y = -(self.square_width - 5) * np.random.random() - 5 self.update_position() self.move_distance = 2.5
E = (1./12.)*M2*L2**2+(1./4.)*M2*L2**2 F = g*L2*M2/2. hpedestal = 1.3*(L1+L2) # height of pedestal wpedestal = 0.1 # width of pedestal tbase = 0.05 # thickness of base wbase = 8.*gap # width of base offset = 2.*gap # from center of pedestal to center of U-shaped upper assembly top = vector(0,0,0) # top of inner bar of U-shaped upper assembly theta1 = 1.3*pi/2. # initial upper angle (from vertical) theta1dot = 0 # initial rate of change of theta1 theta2 = 0 # initial lower angle (from vertical) theta2dot = 0 # initial rate of change of theta2 pedestal = box(pos = (top - vector(0, hpedestal/2.0, offset)),size = (wpedestal, 1.1*hpedestal, wpedestal), color = (0.4,0.4,0.5)) base = box(pos = (top - vector(0,hpedestal + tbase/2.0, offset)),size=(wbase, tbase, wbase),color = (0.4,0.4,0.5)) bar1 = box(pos=(L1display/2.0 - d/2.0, 0, -(gap+d)/2.0), size=(L1display, d, d), color=(1,0,0)) bar1b = box(pos=(L1display/2.0 - d/2.0, 0, (gap+d)/2.0), size=(L1display, d, d), color=(1,0,0)) frame1 = frame(bar1, bar1b) frame1.pos = (0.0, 0.0, 0.0) frame1.axis = (0.0, -1.0, 0.0) frame1.rotate(axis=(0,0,1), angle = 180.0*theta1/pi) bar2 = box(pos = (L2display/2.0 - d/2.0, 0, 0), size = (L2display, d, d), color = (0,1,0)) frame2 = frame(bar2) frame2.pos = (0.0, -1.0*L1, 0.0)
# Author: Prabhu Ramachandran <prabhu [at] aero.iitb.ac.in> # Copyright (c) 2009, Enthought, Inc. # License: BSD Style. from mayavi import mlab from tvtk.tools import visual # Create a figure f = mlab.figure(size=(500, 500)) # Tell visual to use this as the viewer. visual.set_viewer(f) # A silly visualization. mlab.test_plot3d() # Even sillier animation. b1 = visual.box() b2 = visual.box(x=4.) b3 = visual.box(x=-4) b1.v = 5.0 @mlab.show @mlab.animate(delay=250) def anim(): """Animate the b1 box.""" while 1: b1.x = b1.x + b1.v * 0.1 if b1.x > 2.5 or b1.x < -2.5: b1.v = -b1.v yield
# Author: Prabhu Ramachandran <prabhu [at] aero.iitb.ac.in> # Copyright (c) 2009, Enthought, Inc. # License: BSD Style. from mayavi import mlab from tvtk.tools import visual # Create a figure f = mlab.figure(size=(500,500)) # Tell visual to use this as the viewer. visual.set_viewer(f) # A silly visualization. mlab.test_plot3d() # Even sillier animation. b1 = visual.box() b2 = visual.box(x=4., color=visual.color.red) b3 = visual.box(x=-4, color=visual.color.red) b1.v = 5.0 @mlab.show @mlab.animate(delay=250) def anim(): """Animate the b1 box.""" while 1: b1.x = b1.x + b1.v*0.1 if b1.x > 2.5 or b1.x < -2.5: b1.v = -b1.v yield # Run the animation.
def __init__(self, surfaces): self.building_layout = np.ones((5, 5)) self.building_size = (30, 30, 40) # m self.building_spacing = np.float64(100) # m self.fig = mlab.figure(size=(500, 500), bgcolor=(0.1, 0.1, 0.1)) # Set figure for visual objects visual.set_viewer(self.fig) # Convenient local aliases nx, ny = self.building_layout.shape n = nx * ny # Beautiful colors self.building_colors = map( tuple, np.array((np.linspace(0.0, 0.0, n), np.linspace(0.8, 0.3, n), np.linspace(0.3, 0.8, n))).T) # For storing buildings and their locations self.buildings = [] self.building_centers = np.zeros((n, 2)) # Generate buildings for i, x in enumerate( np.linspace(0, (nx - 1) * (self.building_size[0] + self.building_spacing), nx)): for j, y in enumerate( np.linspace( 0, (ny - 1) * (self.building_size[1] + self.building_spacing), ny)): if not self.building_layout[i, j]: continue idx = int(ny * i + j) self.building_centers[idx] = (x, y) self.buildings.append( visual.box(x=x, y=y, z=self.building_size[2] / 2, size=self.building_size, color=self.building_colors[idx])) # Generate ground plane ground_xx, ground_yy = map( np.transpose, np.meshgrid( np.linspace(np.min(self.building_centers[:, 0] - 50), np.max(self.building_centers[:, 0] + 2500), 40), np.linspace(np.min(self.building_centers[:, 1] - 50), np.max(self.building_centers[:, 1] + 2500), 40))) self.ground = mlab.surf(ground_xx, ground_yy, np.random.sample(np.shape(ground_xx)) - 0.8, colormap="ocean", warp_scale=1) # Generate aircraft self.headspan = 0.4 + 2 self.tailspan = 0.6 + 2 self.wingspan = 0.8 * (self.headspan + self.tailspan) self.sweep = -0.2 * 0 self.rudheight = 0.2 * self.wingspan self.aircraft_nodes = np.vstack(([(self.headspan, 0, 0), (-self.tailspan, 0, 0)])).T # [(0, 0, 0), (self.sweep, self.wingspan/2, 0)], # [(0, 0, 0), (self.sweep, -self.wingspan/2, 0)], # [(-self.tailspan, 0, 0), (-self.tailspan+self.sweep/5, 0, self.rudheight)], # [(-self.tailspan+self.sweep/5, 0, self.rudheight), (-self.tailspan+self.sweep/5, self.wingspan/4, self.rudheight)], # [(-self.tailspan+self.sweep/5, 0, self.rudheight), (-self.tailspan+self.sweep/5, -self.wingspan/4, self.rudheight)])).T self.aircraft_fusel = np.vstack(([(self.headspan, 0, 0), (0, 0, 0)], [ (-self.tailspan, 0, 0), (-self.tailspan + self.sweep / 5, 0, self.rudheight) ])).T self.aircraft_wings = np.vstack( ([(self.sweep, self.wingspan / 2, 0), (self.sweep, -self.wingspan / 2, 0)])).T self.aircraft_tail = np.vstack( ([(-self.tailspan + self.sweep / 4, 0.25 * self.wingspan, self.rudheight), (-self.tailspan + self.sweep / 4, -0.25 * self.wingspan, self.rudheight)])).T self.aircraft_nodes_plot = mlab.points3d(self.aircraft_nodes[0, :], self.aircraft_nodes[1, :], self.aircraft_nodes[2, :], scale_factor=0.2, color=(0.5, 0.5, 0.5)) self.aircraft_fusel_plot = mlab.plot3d(self.aircraft_fusel[0, :], self.aircraft_fusel[1, :], self.aircraft_fusel[2, :], tube_sides=10, tube_radius=0.08, color=(1, 0, 0)) self.aircraft_wings_plot = mlab.plot3d(self.aircraft_wings[0, :], self.aircraft_wings[1, :], self.aircraft_wings[2, :], tube_sides=10, tube_radius=0.08, color=(1, 0, 1)) self.aircraft_tail_plot = mlab.plot3d(self.aircraft_tail[0, :], self.aircraft_tail[1, :], self.aircraft_tail[2, :], tube_sides=10, tube_radius=0.05, color=(1, 1, 0)) self.aircraft_surface_plots = [] self.aircraft_surface_corners = np.array([[0.2, 1, 0], [-0.2, 1, 0], [-0.2, -1, 0], [0.2, -1, 0]]) self.rudder_corners = np.array([[ 0.2, 0, 0.7, ], [ -0.2, 0, 0.7, ], [ -0.2, 0, 0.05, ], [ 0.2, 0, 0.05, ]]) # for surf in surfaces: # surf_corners_body = [] # for corner in self.aircraft_surface_corners: # surf_corners_body.append(surf.upoint + rotate_vector(surf.q, surf.pc+corner)) # surf_corners_body = np.array(surf_corners_body) # xx, yy = np.meshgrid(surf_corners_body[:2, 0], surf_corners_body[1:3, 1]) # self.aircraft_surface_plots.append(mlab.mesh(xx, yy, surf_corners_body[:, 2].reshape(2, 2), colormap="autumn")) # Aliases for Mayavi animate decorator and show function self.animate = mlab.animate self.show = mlab.show
bola1.dt = 1 #Creando bola 2 bola2 = visual.sphere(radius=r2, color=(1.0, 1.0, 1.0)) bola2.pos = [0., 0., 0.] bola2.t = 0 bola2.dt = 1 #Creando bola 1 bola3 = visual.sphere(radius=r3, color=(1.0, 1.0, 1.0)) bola3.pos = [0., 0., 0.] bola3.t = 0 bola3.dt = 1 #Creando mesa mesa = visual.box( pos=(ancho/2., largo/2., -grosor/2.), \ size=(ancho, largo, grosor), color=(0.0, 0.3, 0.0) ) muro_l = visual.box( pos=(-grosor/2., largo/2., 0.0), \ size=(grosor, largo + 2*grosor, grosor), \ color=(0.6, 0.3, 0.0) ) muro_r = visual.box( pos=(ancho+grosor/2., largo/2., 0.0), \ size=(grosor, largo + 2*grosor, grosor), \ color=(0.6, 0.3, 0.0) ) muro_d = visual.box( pos=(ancho/2., -grosor/2., 0.0), \ size=(ancho + 2*grosor, grosor, grosor), \ color=(0.6, 0.3, 0.0) ) muro_u = visual.box( pos=(ancho/2., largo+grosor/2., 0.0), \ size=(ancho + 2*grosor, grosor, grosor), \ color=(0.6, 0.3, 0.0) )
bola1.dt = 1 #Creando bola 2 bola2 = visual.sphere( radius=r2, color=(1.0, 1.0, 1.0) ) bola2.pos = [ 0., 0., 0. ] bola2.t = 0 bola2.dt = 1 #Creando bola 1 bola3 = visual.sphere( radius=r3, color=(1.0, 1.0, 1.0) ) bola3.pos = [ 0., 0., 0. ] bola3.t = 0 bola3.dt = 1 #Creando mesa mesa = visual.box( pos=(ancho/2., largo/2., -grosor/2.), \ size=(ancho, largo, grosor), color=(0.0, 0.3, 0.0) ) muro_l = visual.box( pos=(-grosor/2., largo/2., 0.0), \ size=(grosor, largo + 2*grosor, grosor), \ color=(0.6, 0.3, 0.0) ) muro_r = visual.box( pos=(ancho+grosor/2., largo/2., 0.0), \ size=(grosor, largo + 2*grosor, grosor), \ color=(0.6, 0.3, 0.0) ) muro_d = visual.box( pos=(ancho/2., -grosor/2., 0.0), \ size=(ancho + 2*grosor, grosor, grosor), \ color=(0.6, 0.3, 0.0) ) muro_u = visual.box( pos=(ancho/2., largo+grosor/2., 0.0), \ size=(ancho + 2*grosor, grosor, grosor), \ color=(0.6, 0.3, 0.0) )