def mouse_move_callback2(istyle, obj, self): if self._actors_dict[obj]['y'] == -np.inf: iren = istyle.GetInteractor() event_pos = iren.GetEventPosition() self._actors_dict[obj]['y'] = event_pos[1] else: iren = istyle.GetInteractor() event_pos = iren.GetEventPosition() rx, ry, rz = self.rotation_axis if event_pos[1] >= self._actors_dict[obj]['y']: clockwise_rotation = np.array( [-self.rotation_speed, rx, ry, rz]) rotate(obj, clockwise_rotation) else: anti_clockwise_rotation = np.array( [self.rotation_speed, rx, ry, rz]) rotate(obj, anti_clockwise_rotation) self._actors_dict[obj]['y'] = event_pos[1] istyle.force_render() istyle.event.abort()
def left_click_callback2(istyle, obj, self): rx, ry, rz = self.rotation_axis clockwise_rotation = np.array([self.rotation_speed, rx, ry, rz]) rotate(obj, clockwise_rotation) istyle.force_render() istyle.event.abort()
def test_rotate(interactive=False): A = np.zeros((50, 50, 50)) A[20:30, 20:30, 10:40] = 100 act = actor.contour_from_roi(A) scene = window.Scene() scene.add(act) if interactive: window.show(scene) else: arr = window.snapshot(scene, offscreen=True) red = arr[..., 0].sum() red_sum = np.sum(red) act2 = utils.shallow_copy(act) rot = (90, 1, 0, 0) rotate(act2, rot) act3 = utils.shallow_copy(act) scene.add(act2) rot = (90, 0, 1, 0) rotate(act3, rot) scene.add(act3) scene.add(actor.axes()) if interactive: window.show(scene) else: arr = window.snapshot(scene, offscreen=True) red_sum_new = arr[..., 0].sum() npt.assert_equal(red_sum_new > red_sum, True)
def test_direct_sphere_mapping(): arr = 255 * np.ones((810, 1620, 3), dtype='uint8') rows, cols, _ = arr.shape rs = rows // 2 cs = cols // 2 w = 150 // 2 arr[rs - w: rs + w, cs - 10 * w: cs + 10 * w] = np.array([255, 127, 0]) # enable to see pacman on sphere # arr[0: 2 * w, cs - 10 * w: cs + 10 * w] = np.array([255, 127, 0]) scene = window.Scene() tsa = actor.texture_on_sphere(arr) scene.add(tsa) rotate(tsa, rotation=(90, 0, 1, 0)) display = window.snapshot(scene) res = window.analyze_snapshot(display, bg_color=(0, 0, 0), colors=[(255, 127, 0)], find_objects=False) npt.assert_equal(res.colors_found, [True])
def init_planet(planet_data): """Initialize a planet actor. Parameters ---------- planet_data : dict The planet_data is a dictionary, and the keys are filename(texture), position and scale. Returns ------- planet_actor: actor The corresponding sphere actor with texture applied. """ planet_file = read_viz_textures(planet_data['filename']) planet_image = io.load_image(planet_file) planet_actor = actor.texture_on_sphere(planet_image) planet_actor.SetPosition(planet_data['position'], 0, 0) if planet_data['filename'] != '8k_saturn_ring_alpha.png': utils.rotate(planet_actor, (90, 1, 0, 0)) planet_actor.SetScale(planet_data['scale']) scene.add(planet_actor) return planet_actor
def key_press_callback(self, istyle, obj, _what): has_changed = False if istyle.event.key == "Left": has_changed = True for a in self._actors: rotate(a, self.ANTICLOCKWISE_ROTATION_Y) elif istyle.event.key == "Right": has_changed = True for a in self._actors: rotate(a, self.CLOCKWISE_ROTATION_Y) elif istyle.event.key == "Up": has_changed = True for a in self._actors: rotate(a, self.ANTICLOCKWISE_ROTATION_X) elif istyle.event.key == "Down": has_changed = True for a in self._actors: rotate(a, self.CLOCKWISE_ROTATION_X) if has_changed: istyle.force_render()
def timer_callback(_obj, _event): cnt = next(counter) showm.render() if cnt < 450: utils.rotate(earth_actor, (1, 0, 1, 0)) if cnt % 5 == 0 and cnt < 450: showm.scene.azimuth(-1) if cnt == 300: scene.set_camera(position=(-3.679, 0.00, 2.314), focal_point=(0.0, 0.35, 0.00), view_up=(0.00, 1.00, 0.00)) if cnt > 300 and cnt < 450: scene.zoom(1.01) if cnt >= 450 and cnt < 1500: scene.add(sphere_actor) scene.add(text_actor) if cnt >= 450 and cnt < 550: scene.zoom(1.01) if cnt == 575: moon_actor.SetPosition(-1, 0.1, 0.5) scene.set_camera(position=(-0.5, 0.1, 0.00), focal_point=(-1, 0.1, 0.5), view_up=(0.00, 1.00, 0.00)) scene.zoom(0.03) scene.add(satellite_actor) utils.rotate(satellite_actor, (180, 0, 1, 0)) scene.rm(earth_actor) if cnt > 575 and cnt < 750: showm.scene.azimuth(-2) utils.rotate(moon_actor, (-2, 0, 1, 0)) satellite_actor.SetPosition(-0.8, 0.1 - cnt / 10000, 0.4) if cnt >= 750 and cnt < 1100: showm.scene.azimuth(-2) utils.rotate(moon_actor, (-2, 0, 1, 0)) satellite_actor.SetPosition(-0.8, -0.07 + cnt / 10000, 0.4) if cnt == 1100: showm.exit()
############################################################################## # Add both actors to the already existing scene. scene.add(earth_actor) scene.add(moon_actor) ############################################################################## # Next, alter the position and scale of the moon to correctly size it in # comparison to the Earth using ``actor.SetPosition()`` and # ``actor.SetScale()``, and rotate the Earth using ``utils.rotate`` to # correctly align the texture. moon_actor.SetPosition(1, 0.1, 0.5) moon_actor.SetScale(0.25, 0.25, 0.25) utils.rotate(earth_actor, (-90, 1, 0, 0)) ############################################################################## # The ShowManager class is the interface between the scene, the window and the # interactor. showm = window.ShowManager(scene, size=(900, 768), reset_camera=False, order_transparent=True) ############################################################################## # Next, let's focus on creating the animation. # We can determine the duration of animation with using the ``counter``. # Use itertools to avoid global variables.
def rotate_axial(actor, time, radius): axis = (0, radius, 0) angle = 50 / time utils.rotate(actor, (angle, axis[0], axis[1], axis[2])) return angle
planet_actor_list = list(map(init_planet, planet_filenames)) mercury_actor = planet_actor_list[0] venus_actor = planet_actor_list[1] earth_actor = planet_actor_list[2] mars_actor = planet_actor_list[3] jupiter_actor = planet_actor_list[4] saturn_actor = planet_actor_list[5] saturn_rings_actor = planet_actor_list[6] uranus_actor = planet_actor_list[7] neptune_actor = planet_actor_list[8] sun_actor = planet_actor_list[9] # Rotate this actor to correctly orient the texture utils.rotate(jupiter_actor, (90, 1, 0, 0)) ############################################################################## # Next, change the positions and scales of the planets according to their # position and size within the solar system (relatively). For the purpose # of this tutorial, planet sizes and positions will not be completely # accurate. sun_actor.SetScale(5, 5, 5) mercury_actor.SetScale(0.4, 0.4, 0.4) venus_actor.SetScale(0.6, 0.6, 0.6) earth_actor.SetScale(0.4, 0.4, 0.4) mars_actor.SetScale(0.8, 0.8, 0.8) jupiter_actor.SetScale(2, 2, 2) saturn_actor.SetScale(2, 2, 2) saturn_rings_actor.SetScale(3, 0.5, 3)
# ``fetch_viz_textures`` and ``read_viz_textures``. We will use a 16k # resolution texture for maximum detail. scene = window.Scene() fetch_viz_textures() earth_file = read_viz_textures("1_earth_16k.jpg") earth_image = io.load_image(earth_file) earth_actor = actor.texture_on_sphere(earth_image) scene.add(earth_actor) ############################################################################### # Rotate the Earth to make sure the texture is correctly oriented. Change it's # scale using ``actor.SetScale()``. utils.rotate(earth_actor, (-90, 1, 0, 0)) utils.rotate(earth_actor, (180, 0, 1, 0)) earth_actor.SetScale(2, 2, 2) ############################################################################### # Define the function to convert geographical coordinates of a location in # latitude and longitude degrees to coordinates on the ``earth_actor`` surface. # In this function, convert to radians, then to spherical coordinates, and # lastly, to cartesian coordinates. def latlong_coordinates(lat, lon): # Convert latitude and longitude to spherical coordinates degrees_to_radians = math.pi / 180.0 # phi = 90 - latitude phi = (90 - lat) * degrees_to_radians