def _timer(_obj, _event): nonlocal counter, pos counter += 1 if mode == 0: iterate(1) else: pos[:] += (np.random.random(pos.shape) - 0.5) * 1.5 spheres_positions = vertices_from_actor(sphere_actor) spheres_positions[:] = sphere_geometry + \ np.repeat(pos, geometry_length, axis=0) edges_positions = vertices_from_actor(lines_actor) edges_positions[::2] = pos[edges_list[:, 0]] edges_positions[1::2] = pos[edges_list[:, 1]] update_actor(lines_actor) compute_bounds(lines_actor) update_actor(sphere_actor) compute_bounds(lines_actor) showm.scene.reset_clipping_range() showm.render() if counter >= max_iterations: showm.exit()
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 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) npt.assert_array_almost_equal(expected, res_vertices)
def create_surface(x, y, equation, colormap_name): xyz, colors = update_surface(x, y, equation=equation, cmap_name=colormap_name) surf = actor.surface(xyz, colors=colors) surf.equation = equation surf.cmap_name = colormap_name surf.vertices = utils.vertices_from_actor(surf) surf.no_vertices_per_point = len(surf.vertices)/npoints**2 surf.initial_vertices = surf.vertices.copy() - \ np.repeat(xyz, surf.no_vertices_per_point, axis=0) return surf
def test_symmetric_param(): dirs000 = np.array([[1, 0, 0], [0, -1, 0]]) dirs001 = np.array([[1, 1, 0], [-1, 1, 0]]) peak_dirs = np.zeros((1, 1, 2, 2, 3)) peak_dirs[0, 0, 0, :, :] = dirs000 peak_dirs[0, 0, 1, :, :] = dirs001 valid_mask = np.abs(peak_dirs).max(axis=(-2, -1)) > 0 indices = np.nonzero(valid_mask) peak_actor_asym = PeakActor(peak_dirs, indices, symmetric=False) actor_points = utils.vertices_from_actor(peak_actor_asym) desired_points = np.array([[0, 0, 0], [1, 0, 0], [0, 0, 0], [0, -1, 0], [0, 0, 1], [1, 1, 1], [0, 0, 1], [-1, 1, 1]]) npt.assert_array_equal(actor_points, desired_points) peak_actor_sym = PeakActor(peak_dirs, indices) actor_points = utils.vertices_from_actor(peak_actor_sym) desired_points = np.array([[-1, 0, 0], [1, 0, 0], [0, 1, 0], [0, -1, 0], [-1, -1, 1], [1, 1, 1], [1, -1, 1], [-1, 1, 1]]) npt.assert_array_equal(actor_points, desired_points)
def particle(colors, origin=[0, 0, 0], num_total_steps=300, total_time=5, delta=1.8, path_thickness=3): origin = np.asarray(origin, dtype=float) position = np.tile(origin, (num_total_steps, 1)) path_actor = actor.line([position], colors, linewidth=path_thickness) path_actor.position = position path_actor.delta = delta path_actor.num_total_steps = num_total_steps path_actor.time_step = total_time / num_total_steps path_actor.vertices = utils.vertices_from_actor(path_actor) path_actor.no_vertices_per_point = \ len(path_actor.vertices) / num_total_steps path_actor.initial_vertices = path_actor.vertices.copy() - \ np.repeat(position, path_actor.no_vertices_per_point, axis=0) return path_actor
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)
def test_vertices_from_actor(): my_vertices = np.array([[2.5, -0.5, 0.], [1.5, -0.5, 0.], [1.5, 0.5, 0.], [2.5, 0.5, 0.], [1., 1., 0.], [-1., 1., 0.], [-1., 3., 0.], [1., 3., 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]]) scale = [1, 2, 1] verts, faces = fp.prim_square() res = fp.repeat_primitive(verts, faces, centers=centers, colors=colors, scale=scale) 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() res_vertices = vertices_from_actor(actr) npt.assert_array_almost_equal(my_vertices, res_vertices)
showm.initialize() # Counter iterator for tracking simulation steps. counter = itertools.count() # Variable for tracking applied force. apply_force = True ############################################################################### # Now, we define methods to sync objects between fury and Pybullet. # Get the position of base and set it. base_pos, _ = p.getBasePositionAndOrientation(base) base_actor.SetPosition(*base_pos) # Calculate the vertices of the dominos. vertices = utils.vertices_from_actor(domino_actor) num_vertices = vertices.shape[0] num_objects = domino_centers.shape[0] sec = int(num_vertices / num_objects) ############################################################################### # ================ # Syncing Dominoes # ================ # # Here, we perform three major steps to sync Dominoes accurately. # * Get the position and orientation of the Dominoes from pybullet. # * Calculate the Rotation Matrix. # * Get the difference in orientations (Quaternion). # * Generate the corresponding rotation matrix according to that difference. # * Reshape it in a 3x3 matrix.
# Counter interator for tracking simulation steps. counter = itertools.count() ############################################################################### # We define a couple of syncing methods for the base and chain. # Function for syncing actors with multi-bodies. def sync_actor(actor, multibody): pos, orn = p.getBasePositionAndOrientation(multibody) actor.SetPosition(*pos) orn_deg = np.degrees(p.getEulerFromQuaternion(orn)) actor.SetOrientation(*orn_deg) vertices = utils.vertices_from_actor(rope_actor) num_vertices = vertices.shape[0] num_objects = linkPositions.shape[0] sec = np.int(num_vertices / num_objects) def sync_joints(actor_list, multibody): for joint in range(p.getNumJoints(multibody)): # `p.getLinkState` offers various information about the joints # as a list and the values in 4th and 5th index refer to the joint's # position and orientation respectively. pos, orn = p.getLinkState(multibody, joint)[4:6] rot_mat = np.reshape( p.getMatrixFromQuaternion( p.getDifferenceQuaternion(orn, linkOrientations[joint])),
scene.add(arrow_actor) ############################################################################### # 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
############################################################################### # 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)
# Variable for tracking applied force. apply_force = True ############################################################################### # Now, we define methods to sync objects between fury and Pybullet. # Get the position of base and set it. base_pos, _ = p.getBasePositionAndOrientation(base) base_actor.SetPosition(*base_pos) # Do the same for ball. ball_pos, _ = p.getBasePositionAndOrientation(ball) ball_actor.SetPosition(*ball_pos) # Calculate the vertices of the bricks. vertices = utils.vertices_from_actor(brick_actor) num_vertices = vertices.shape[0] num_objects = brick_centers.shape[0] sec = int(num_vertices / num_objects) ############################################################################### # ============== # Syncing Bricks # ============== # # Here, we perform three major steps to sync bricks accurately. # * Get the position and orientation of the bricks from pybullet. # * Calculate the Rotation Matrix. # - Get the difference in orientations (Quaternion). # - Generate the corresponding rotation matrix according to that difference. # - Reshape it in a 3x3 matrix.
opacity=1, dot_size=1) return orbit_actor ############################################################################## # All of the planets will have the same initial positions, so assign each of # those to the positions variables for each planet. These variables will be # updated within the ``timer_callback`` function. Initialize and add the # orbit actors into the scene. Also initialize the track variables for each # planet. orbit_points = np.zeros((1000, 3), dtype='f8') mercury_orbit_actor = get_orbit_actor(orbit_points) scene.add(mercury_orbit_actor) positions_mercury = utils.vertices_from_actor(mercury_orbit_actor) venus_orbit_actor = get_orbit_actor(orbit_points) scene.add(venus_orbit_actor) positions_venus = utils.vertices_from_actor(venus_orbit_actor) earth_orbit_actor = get_orbit_actor(orbit_points) scene.add(earth_orbit_actor) positions_earth = utils.vertices_from_actor(earth_orbit_actor) mars_orbit_actor = get_orbit_actor(orbit_points) scene.add(mars_orbit_actor) positions_mars = utils.vertices_from_actor(mars_orbit_actor) jupiter_orbit_actor = get_orbit_actor(orbit_points) scene.add(jupiter_orbit_actor)
sphere_actor = actor.sphere(centers=xyz, colors=colors, radii=radii) 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)
# Build scene and add an actor with many objects. scene = window.Scene() ############################################################################### # Build the actor containing all the cubes 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))
size=(900, 768), reset_camera=False, order_transparent=True) showm.initialize() ############################################################################### # Position the base correctly. base_pos, base_orn = p.getBasePositionAndOrientation(base) base_actor.SetPosition(*base_pos) ############################################################################### # Calculate the vertices of the bricks. brick_vertices = utils.vertices_from_actor(brick_actor) num_vertices = brick_vertices.shape[0] num_objects = brick_centers.shape[0] brick_sec = int(num_vertices / num_objects) ############################################################################### # Calculate the vertices of the wrecking ball. chain_vertices = utils.vertices_from_actor(rope_actor) num_vertices = chain_vertices.shape[0] num_objects = brick_centers.shape[0] chain_sec = int(num_vertices / num_objects) ############################################################################### # We define methods to sync bricks and wrecking ball.
def new_layout_timer(showm, edges_list, vertices_count, max_iterations=1000, vertex_initial_positions=None): view_size = 500 viscosity = 0.10 alpha = 0.5 a = 0.0005 b = 1.0 deltaT = 1.0 sphere_geometry = np.array(vertices_from_actor(sphere_actor)) geometry_length = sphere_geometry.shape[0] / vertices_count if vertex_initial_positions is not None: pos = np.array(vertex_initial_positions) else: pos = view_size * \ np.random.random((vertices_count, 3)) - view_size / 2.0 velocities = np.zeros((vertices_count, 3)) def iterate(iterationCount): nonlocal pos, velocities for _ in range(iterationCount): forces = np.zeros((vertices_count, 3)) # repulstive forces for vertex1 in range(vertices_count): for vertex2 in range(vertex1): x1, y1, z1 = pos[vertex1] x2, y2, z2 = pos[vertex2] distance = math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)) + alpha rx = (x2 - x1) / distance ry = (y2 - y1) / distance rz = (z2 - z1) / distance Fx = -b * rx / distance / distance Fy = -b * ry / distance / distance Fz = -b * rz / distance / distance forces[vertex1] += np.array([Fx, Fy, Fz]) forces[vertex2] -= np.array([Fx, Fy, Fz]) # attractive forces for vFrom, vTo in edges_list: if vFrom == vTo: continue x1, y1, z1 = pos[vFrom] x2, y2, z2 = pos[vTo] distance = math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)) Rx = (x2 - x1) Ry = (y2 - y1) Rz = (z2 - z1) Fx = a * Rx * distance Fy = a * Ry * distance Fz = a * Rz * distance forces[vFrom] += np.array([Fx, Fy, Fz]) forces[vTo] -= np.array([Fx, Fy, Fz]) velocities += forces * deltaT velocities *= (1.0 - viscosity) pos += velocities * deltaT pos[:, 0] -= np.mean(pos[:, 0]) pos[:, 1] -= np.mean(pos[:, 1]) pos[:, 2] -= np.mean(pos[:, 2]) counter = 0 def _timer(_obj, _event): nonlocal counter, pos counter += 1 if mode == 0: iterate(1) else: pos[:] += (np.random.random(pos.shape) - 0.5) * 1.5 spheres_positions = vertices_from_actor(sphere_actor) spheres_positions[:] = sphere_geometry + \ np.repeat(pos, geometry_length, axis=0) edges_positions = vertices_from_actor(lines_actor) edges_positions[::2] = pos[edges_list[:, 0]] edges_positions[1::2] = pos[edges_list[:, 1]] update_actor(lines_actor) compute_bounds(lines_actor) update_actor(sphere_actor) compute_bounds(lines_actor) showm.scene.reset_clipping_range() showm.render() if counter >= max_iterations: showm.exit() return _timer
scene = window.Scene() label_actor = actor.label(text='Test') ############################################################################### # This actor is made with 3 cubes of different orientation 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)
# Creating a scene object and configuring the camera's position scene = window.Scene() scene.set_camera(position=(0, 10, -1), focal_point=(0.0, 0.0, 0.0), view_up=(0.0, 0.0, 0.0)) showm = window.ShowManager(scene, size=(1920, 1080), order_transparent=True) showm.initialize() ############################################################################### # Creating vertices and points actors verts3D = rotate4D(verts4D) if not wireframe: points = actor.point(verts3D, colors=p_color) point_verts = utils.vertices_from_actor(points) no_vertices = len(point_verts) / 16 initial_verts = point_verts.copy() - \ np.repeat(verts3D, no_vertices, axis=0) scene.add(points) ############################################################################### # Connecting points with lines actor lines = connect_points(verts3D) edges = actor.line(lines=lines, colors=e_color, lod=False, fake_tube=True, linewidth=4)