def test_vertices_from_actor(interactive=False): expected = np.array([[1.5, -0.5, 0.], [1.5, 0.5, 0], [2.5, 0.5, 0], [2.5, -0.5, 0], [-1, 1, 0], [-1, 3, 0], [1, 3, 0], [1, 1, 0], [-0.5, -0.5, 0], [-0.5, 0.5, 0], [0.5, 0.5, 0], [0.5, -0.5, 0]]) centers = np.array([[2, 0, 0], [0, 2, 0], [0, 0, 0]]) colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]]) scales = [1, 2, 1] verts, faces = fp.prim_square() res = fp.repeat_primitive(verts, faces, centers=centers, colors=colors, scales=scales) big_verts = res[0] big_faces = res[1] big_colors = res[2] actr = get_actor_from_primitive(big_verts, big_faces, big_colors) actr.GetProperty().BackfaceCullingOff() if interactive: scene = window.Scene() scene.add(actor.axes()) scene.add(actr) window.show(scene) res_vertices = vertices_from_actor(actr) res_vertices_vtk = vertices_from_actor(actr, as_vtk=True) npt.assert_array_almost_equal(expected, res_vertices) npt.assert_equal(isinstance(res_vertices_vtk, vtk.vtkDoubleArray), True) # test colors_from_actor: l_colors = utils.colors_from_actor(actr) l_colors_vtk = utils.colors_from_actor(actr, as_vtk=True) l_colors_none = utils.colors_from_actor(actr, array_name='col') npt.assert_equal(l_colors_none, None) npt.assert_equal(isinstance(l_colors_vtk, vtk.vtkUnsignedCharArray), True) npt.assert_equal(np.unique(l_colors, axis=0).shape, colors.shape) l_array = utils.array_from_actor(actr, 'colors') l_array_vtk = utils.array_from_actor(actr, 'colors', as_vtk=True) l_array_none = utils.array_from_actor(actr, 'col') npt.assert_array_equal(l_array, l_colors) npt.assert_equal(l_array_none, None) npt.assert_equal(isinstance(l_array_vtk, vtk.vtkUnsignedCharArray), True)
def __init__(self, colors, origin=[0, 0, 0], num_total_steps=300, total_time=5, delta=1.8, path_thickness=3): origin = np.asarray(origin, dtype=float) self.position = np.tile(origin, (num_total_steps, 1)) self.colors = colors self.delta = delta self.num_total_steps = num_total_steps self.time_step = total_time / num_total_steps self.path_actor = actor.line([self.position], colors, linewidth=path_thickness) self.vertices = utils.vertices_from_actor(self.path_actor) self.vcolors = utils.colors_from_actor(self.path_actor, 'colors') self.no_vertices_per_point = len(self.vertices) / num_total_steps nvpp = self.no_vertices_per_point self.initial_vertices = self.vertices.copy() - np.repeat( self.position, nvpp, axis=0)
directions = np.array([[np.sqrt(2) / 2, 0, np.sqrt(2) / 2], [np.sqrt(2) / 2, np.sqrt(2) / 2, 0], [0, np.sqrt(2) / 2, np.sqrt(2) / 2]]) fury_actor = actor.cube(centers, directions, colors, heights=radii) ############################################################################### # Access the memory of the vertices of all the cubes vertices = utils.vertices_from_actor(fury_actor) num_vertices = vertices.shape[0] num_objects = centers.shape[0] ############################################################################### # Access the memory of the colors of all the cubes vcolors = utils.colors_from_actor(fury_actor, 'colors') ############################################################################### # Adding an actor showing the axes of the world coordinates ax = actor.axes(scale=(10, 10, 10)) scene.add(fury_actor) scene.add(label_actor) scene.add(ax) scene.reset_camera() ############################################################################### # Create the Picking manager pickm = pick.PickingManager()
# Initializing the initial coordinates of the particle x = initial_velocity*time + 0.5*acc*(time**2) y = np.sin(angular_frq*time + phase_angle) z = np.cos(angular_frq*time + phase_angle) ############################################################################### # Initializing point actor which will represent the charged particle color_particle = window.colors.red # color of particle can be manipulated pts = np.array([[x, y, z]]) charge_actor = actor.point(pts, color_particle, point_radius=radius_particle) scene.add(charge_actor) vertices = utils.vertices_from_actor(charge_actor) vcolors = utils.colors_from_actor(charge_actor, 'colors') no_vertices_per_point = len(vertices) initial_vertices = vertices.copy() - \ np.repeat(pts, no_vertices_per_point, axis=0) ############################################################################### # Initializing text box to display the name of the animation tb = ui.TextBlock2D(bold=True, position=(100, 90)) m1 = "Motion of a charged particle in a " m2 = "combined electric and magnetic field" tb.message = m1 + m2 scene.add(tb) ###############################################################################
scene.add(sphere_actor) showm = window.ShowManager(scene, size=(900, 768), reset_camera=True, order_transparent=True) showm.initialize() tb = ui.TextBlock2D(bold=True) scene.zoom(0.8) scene.azimuth(30) # use itertools to avoid global variables counter = itertools.count() vertices = utils.vertices_from_actor(sphere_actor) vcolors = utils.colors_from_actor(sphere_actor, 'colors') no_vertices_per_sphere = len(vertices) / num_particles initial_vertices = vertices.copy() - \ np.repeat(xyz, no_vertices_per_sphere, axis=0) def timer_callback(_obj, _event): global xyz cnt = next(counter) tb.message = "Let's count up to 1000 and exit :" + str(cnt) xyz = xyz + vel * dt collision() vertices[:] = initial_vertices + \ np.repeat(xyz, no_vertices_per_sphere, axis=0) utils.update_actor(sphere_actor)
def toggle_color(radio): vcolors = utils.colors_from_actor(sphere) color = options[radio.checked_labels[0]] vcolors[:] = np.array(color) utils.update_actor(sphere)
############################################################################### # Creating point actor that renders the magnetic field x = np.linspace(-3, 3, npoints) y = np.sin(wavenumber * x - angular_frq * time + phase_angle) z = np.array([0 for i in range(npoints)]) pts = np.array([(a, b, c) for (a, b, c) in zip(x, y, z)]) pts = [pts] colors = window.colors.red wave_actor1 = actor.line(pts, colors, linewidth=3) scene.add(wave_actor1) vertices = utils.vertices_from_actor(wave_actor1) vcolors = utils.colors_from_actor(wave_actor1, 'colors') no_vertices_per_point = len(vertices) / npoints initial_vertices = vertices.copy() - \ np.repeat(pts, no_vertices_per_point, axis=0) ############################################################################### # Creating point actor that renders the electric field xx = np.linspace(-3, 3, npoints) yy = np.array([0 for i in range(npoints)]) zz = np.sin(wavenumber * xx - angular_frq * time + phase_angle) pts2 = np.array([(a, b, c) for (a, b, c) in zip(xx, yy, zz)]) pts2 = [pts2] colors2 = window.colors.blue wave_actor2 = actor.line(pts2, colors2, linewidth=3)
def update_colors(color_array): for _, figure in figure_dict.items(): vcolors = utils.colors_from_actor(figure) vcolors[:] = color_array utils.update_actor(figure)
cube_actor = actor.cube(centers, directions=(1, 0, 0), colors=colors, scales=radii) ############################################################################### # Access the memory of the vertices of all the cubes vertices = utils.vertices_from_actor(cube_actor) num_vertices = vertices.shape[0] num_objects = centers.shape[0] ############################################################################### # Access the memory of the colors of all the cubes vcolors = utils.colors_from_actor(cube_actor, 'colors') ############################################################################### # Create a rectangular 2d box as a texture rgba = 255 * np.ones((100, 200, 4)) rgba[1:-1, 1:-1] = np.zeros((98, 198, 4)) + 100 texa = actor.texture_2d(rgba.astype(np.uint8)) scene.add(cube_actor) scene.add(texa) scene.reset_camera() scene.zoom(3.) ############################################################################### # Create the Selection Manager