From f7bc7475442620fccdb9686019201d5be57f16de Mon Sep 17 00:00:00 2001 From: leotrs Date: Fri, 21 Aug 2020 15:36:46 -0400 Subject: [PATCH 1/2] Replace some setters and getters from Camera. Also turn ThreeDCamera.frame_center into a property. --- manim/camera/camera.py | 161 ++++++------------------------- manim/camera/mapping_camera.py | 8 +- manim/camera/multi_camera.py | 6 +- manim/camera/three_d_camera.py | 33 ++----- manim/scene/scene.py | 2 +- manim/scene/scene_file_writer.py | 4 +- manim/scene/zoomed_scene.py | 4 +- 7 files changed, 54 insertions(+), 164 deletions(-) diff --git a/manim/camera/camera.py b/manim/camera/camera.py index 7520e31cd7..57a30531de 100644 --- a/manim/camera/camera.py +++ b/manim/camera/camera.py @@ -108,92 +108,6 @@ def reset_pixel_shape(self, new_height, new_width): self.resize_frame_shape() self.reset() - def get_pixel_height(self): - """Returns the height of the scene in - pixel at that moment. - - Returns - ------- - int - The height of the scene in pixels. - """ - return self.pixel_height - - def get_pixel_width(self): - """Returns the width of the scene in - pixels at that moment. - - Returns - ------- - int - The width of the scene in pixels. - """ - return self.pixel_width - - def get_frame_height(self): - """Returns the height of the frame - in MUnits. Default is 8.0 - - Returns - ------- - float - The frame height - """ - return self.frame_height - - def get_frame_width(self): - """Returns the width of the frame - in MUnits. - - Returns - ------- - float - The frame width - """ - return self.frame_width - - def get_frame_center(self): - """Returns the absolute center of the frame as Cartesian - Coordinates with the unit MUnits. - - Returns - ------- - np.array - The array of x,y,z coordinates. - """ - return self.frame_center - - def set_frame_height(self, frame_height): - """Sets the frame height to the passed value. - - Parameters - ---------- - frame_height : int, float - The frame_height in MUnits. - """ - self.frame_height = frame_height - - def set_frame_width(self, frame_width): - """Sets the frame width to the passed value. - - Parameters - ---------- - frame_width : int, float - The frame_width in MUnits. - """ - self.frame_width = frame_width - - def set_frame_center(self, frame_center): - """Sets the center of the frame to the passed - cartesian coordinates. - - Parameters - ---------- - frame_center : np.array - The center of the frame. - """ - self.frame_center = frame_center - def resize_frame_shape(self, fixed_dimension=0): """ Changes frame_shape to match the aspect ratio @@ -207,17 +121,17 @@ def resize_frame_shape(self, fixed_dimension=0): If 0, height is scaled with respect to width else, width is scaled with respect to height. """ - pixel_height = self.get_pixel_height() - pixel_width = self.get_pixel_width() - frame_height = self.get_frame_height() - frame_width = self.get_frame_width() + pixel_height = self.pixel_height + pixel_width = self.pixel_width + frame_height = self.frame_height + frame_width = self.frame_width aspect_ratio = fdiv(pixel_width, pixel_height) if fixed_dimension == 0: frame_height = frame_width / aspect_ratio else: frame_width = aspect_ratio * frame_height - self.set_frame_height(frame_height) - self.set_frame_width(frame_width) + self.frame_height = frame_height + self.frame_width = frame_width def init_background(self): """Initialize the background. @@ -225,8 +139,8 @@ def init_background(self): the image is set as background; else, the default background color fills the background. """ - height = self.get_pixel_height() - width = self.get_pixel_width() + height = self.pixel_height + width = self.pixel_width if self.background_image is not None: path = get_full_raster_image_path(self.background_image) image = Image.open(path).convert(self.image_mode) @@ -262,17 +176,6 @@ def get_image(self, pixel_array=None): pixel_array = self.pixel_array return Image.fromarray(pixel_array, mode=self.image_mode) - def get_pixel_array(self): - """Returns the pixel array - of the current frame. - - Returns - ------- - np.array - The array of RGB values of each pixel. - """ - return self.pixel_array - def convert_pixel_array(self, pixel_array, convert_from_floats=False): """Converts a pixel array from values that have floats in then to proper RGB values. @@ -460,9 +363,9 @@ def is_in_frame(self, mobject): bool True if in frame, False otherwise. """ - fc = self.get_frame_center() - fh = self.get_frame_height() - fw = self.get_frame_width() + fc = self.frame_center + fh = self.frame_height + fw = self.frame_width return not reduce( op.or_, [ @@ -559,11 +462,11 @@ def get_cairo_context(self, pixel_array): cached_ctx = self.get_cached_cairo_context(pixel_array) if cached_ctx: return cached_ctx - pw = self.get_pixel_width() - ph = self.get_pixel_height() - fw = self.get_frame_width() - fh = self.get_frame_height() - fc = self.get_frame_center() + pw = self.pixel_width + ph = self.pixel_height + fw = self.frame_width + fh = self.frame_height + fc = self.frame_center surface = cairo.ImageSurface.create_for_data( pixel_array, cairo.FORMAT_ARGB32, pw, ph ) @@ -756,7 +659,7 @@ def apply_stroke(self, ctx, vmobject, background=False): * # This ensures lines have constant width # as you zoom in on them. - (self.get_frame_width() / self.frame_width) + (self.frame_width / self.frame_width) ) ctx.stroke_preserve() return self @@ -886,8 +789,8 @@ def display_point_cloud(self, pmobject, points, rgbas, thickness, pixel_array): pixel_coords = pixel_coords[on_screen_indices] rgbas = rgbas[on_screen_indices] - ph = self.get_pixel_height() - pw = self.get_pixel_width() + ph = self.pixel_height + pw = self.pixel_width flattener = np.array([1, pw], dtype="int") flattener = flattener.reshape((2, 1)) @@ -948,7 +851,7 @@ def display_image_mobject(self, image_mobject, pixel_array): # Paste into an image as large as the camear's pixel array full_image = Image.fromarray( - np.zeros((self.get_pixel_height(), self.get_pixel_width())), mode="RGBA" + np.zeros((self.pixel_height, self.pixel_width)), mode="RGBA" ) new_ul_coords = center_coords - np.array(sub_image.size) / 2 new_ul_coords = new_ul_coords.astype(int) @@ -1034,13 +937,13 @@ def points_to_pixel_coords( self, mobject, points ): # TODO: Write more detailed docstrings for this method. points = self.transform_points_pre_display(mobject, points) - shifted_points = points - self.get_frame_center() + shifted_points = points - self.frame_center result = np.zeros((len(points), 2)) - pixel_height = self.get_pixel_height() - pixel_width = self.get_pixel_width() - frame_height = self.get_frame_height() - frame_width = self.get_frame_width() + pixel_height = self.pixel_height + pixel_width = self.pixel_width + frame_height = self.frame_height + frame_width = self.frame_width width_mult = pixel_width / frame_width width_add = pixel_width / 2 height_mult = pixel_height / frame_height @@ -1070,9 +973,9 @@ def on_screen_pixels(self, pixel_coords): op.and_, [ pixel_coords[:, 0] >= 0, - pixel_coords[:, 0] < self.get_pixel_width(), + pixel_coords[:, 0] < self.pixel_width, pixel_coords[:, 1] >= 0, - pixel_coords[:, 1] < self.get_pixel_height(), + pixel_coords[:, 1] < self.pixel_height, ], ) @@ -1092,7 +995,7 @@ def adjusted_thickness(self, thickness): big_sum = op.add( camera_config["default_pixel_height"], camera_config["default_pixel_width"] ) - this_sum = op.add(self.get_pixel_height(), self.get_pixel_width()) + this_sum = op.add(self.pixel_height, self.pixel_width) factor = fdiv(big_sum, this_sum) return 1 + (thickness - 1) / factor @@ -1143,13 +1046,13 @@ def get_coords_of_all_pixels(self): The array of cartesian coordinates. """ # These are in x, y order, to help me keep things straight - full_space_dims = np.array([self.get_frame_width(), self.get_frame_height()]) - full_pixel_dims = np.array([self.get_pixel_width(), self.get_pixel_height()]) + full_space_dims = np.array([self.frame_width, self.frame_height]) + full_pixel_dims = np.array([self.pixel_width, self.pixel_height]) # These are addressed in the same y, x order as in pixel_array, but the values in them # are listed in x, y order uncentered_pixel_coords = np.indices( - [self.get_pixel_height(), self.get_pixel_width()] + [self.pixel_height, self.pixel_width] )[::-1].transpose(1, 2, 0) uncentered_space_coords = fdiv( uncentered_pixel_coords * full_space_dims, full_pixel_dims @@ -1180,7 +1083,7 @@ def __init__(self, camera): """ self.camera = camera self.file_name_to_pixel_array_map = {} - self.pixel_array = np.array(camera.get_pixel_array()) + self.pixel_array = np.array(camera.pixel_array) self.reset_pixel_array() def reset_pixel_array(self): diff --git a/manim/camera/mapping_camera.py b/manim/camera/mapping_camera.py index f86d612cc5..184f0d8d46 100644 --- a/manim/camera/mapping_camera.py +++ b/manim/camera/mapping_camera.py @@ -58,9 +58,9 @@ def __init__(self, *cameras_with_start_positions, **kwargs): "start_x": camera_with_start_positions[1][1], "start_y": camera_with_start_positions[1][0], "end_x": camera_with_start_positions[1][1] - + camera_with_start_positions[0].get_pixel_width(), + + camera_with_start_positions[0].pixel_width, "end_y": camera_with_start_positions[1][0] - + camera_with_start_positions[0].get_pixel_height(), + + camera_with_start_positions[0].pixel_height, } ) for camera_with_start_positions in cameras_with_start_positions @@ -113,10 +113,10 @@ def __init__(self, left_camera, right_camera, **kwargs): self.left_camera = left_camera self.right_camera = right_camera - half_width = self.get_pixel_width() / 2 + half_width = self.pixel_width / 2 for camera in [self.left_camera, self.right_camera]: # TODO: Round up on one if width is odd - camera.reset_pixel_shape(camera.get_pixel_height(), half_width) + camera.reset_pixel_shape(camera.pixel_height, half_width) OldMultiCamera.__init__( self, (left_camera, (0, 0)), (right_camera, (0, half_width)), diff --git a/manim/camera/multi_camera.py b/manim/camera/multi_camera.py index 6065786d38..b786858129 100644 --- a/manim/camera/multi_camera.py +++ b/manim/camera/multi_camera.py @@ -43,14 +43,14 @@ def add_image_mobject_from_camera(self, image_mobject_from_camera): def update_sub_cameras(self): """ Reshape sub_camera pixel_arrays """ for imfc in self.image_mobjects_from_cameras: - pixel_height, pixel_width = self.get_pixel_array().shape[:2] + pixel_height, pixel_width = self.pixel_array.shape[:2] imfc.camera.frame_shape = ( imfc.camera.frame.get_height(), imfc.camera.frame.get_width(), ) imfc.camera.reset_pixel_shape( - int(pixel_height * imfc.get_height() / self.get_frame_height()), - int(pixel_width * imfc.get_width() / self.get_frame_width()), + int(pixel_height * imfc.get_height() / self.frame_height), + int(pixel_width * imfc.get_width() / self.frame_width), ) def reset(self): diff --git a/manim/camera/three_d_camera.py b/manim/camera/three_d_camera.py index 79b3f908a4..6618b38620 100644 --- a/manim/camera/three_d_camera.py +++ b/manim/camera/three_d_camera.py @@ -24,7 +24,6 @@ class ThreeDCamera(Camera): "theta": -90 * DEGREES, # Rotation about z axis "gamma": 0, # Rotation about normal vector to camera "light_source_start_point": 9 * DOWN + 7 * LEFT + 10 * OUT, - "frame_center": ORIGIN, "should_apply_shading": True, "exponential_projection": False, "max_allowable_norm": 3 * config["frame_width"], @@ -46,11 +45,19 @@ def __init__(self, *args, **kwargs): self.distance_tracker = ValueTracker(self.distance) self.gamma_tracker = ValueTracker(self.gamma) self.light_source = Point(self.light_source_start_point) - self.frame_center = Point(self.frame_center) + self._frame_center = Point(kwargs.get("frame_center", ORIGIN)) self.fixed_orientation_mobjects = dict() self.fixed_in_frame_mobjects = set() self.reset_rotation_matrix() + @property + def frame_center(self): + return self._frame_center.points[0] + + @frame_center.setter + def frame_center(self, point): + self._frame_center.move_to(point) + def capture_mobjects(self, mobjects, **kwargs): self.reset_rotation_matrix() Camera.capture_mobjects(self, mobjects, **kwargs) @@ -158,16 +165,6 @@ def get_gamma(self): """ return self.gamma_tracker.get_value() - def get_frame_center(self): - """Returns the center of the camera frame in cartesian coordinates. - - Returns - ------- - np.array - The cartesian coordinates of the center of the camera frame. - """ - return self.frame_center.points[0] - def set_phi(self, value): """Sets the polar angle i.e the angle between Z_AXIS and Camera through ORIGIN in radians. @@ -208,16 +205,6 @@ def set_gamma(self, value): """ self.gamma_tracker.set_value(value) - def set_frame_center(self, point): - """Sets the camera frame center to the passed cartesian coordinate. - - Parameters - ---------- - point : list, tuple, np.array - The cartesian coordinates of the new frame center. - """ - self.frame_center.move_to(point) - def reset_rotation_matrix(self): """Sets the value of self.rotation_matrix to the matrix corresponding to the current position of the camera @@ -269,7 +256,7 @@ def project_points(self, points): np.array The points after projecting. """ - frame_center = self.get_frame_center() + frame_center = self.frame_center distance = self.get_distance() rot_matrix = self.get_rotation_matrix() diff --git a/manim/scene/scene.py b/manim/scene/scene.py index edb96f5125..171afcb30b 100644 --- a/manim/scene/scene.py +++ b/manim/scene/scene.py @@ -168,7 +168,7 @@ def get_frame(self): NumPy array of pixel values of each pixel in screen. The shape of the array is height x width x 3 """ - return np.array(self.camera.get_pixel_array()) + return np.array(self.camera.pixel_array) def get_image(self): """ diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py index 66f370632f..9f40771b6f 100644 --- a/manim/scene/scene_file_writer.py +++ b/manim/scene/scene_file_writer.py @@ -394,8 +394,8 @@ def open_movie_pipe(self): self.temp_partial_movie_file_path = temp_file_path fps = self.scene.camera.frame_rate - height = self.scene.camera.get_pixel_height() - width = self.scene.camera.get_pixel_width() + height = self.scene.camera.pixel_height + width = self.scene.camera.pixel_width command = [ FFMPEG_BIN, diff --git a/manim/scene/zoomed_scene.py b/manim/scene/zoomed_scene.py index 967b607ae1..f2060978d5 100644 --- a/manim/scene/zoomed_scene.py +++ b/manim/scene/zoomed_scene.py @@ -101,8 +101,8 @@ def get_zoom_in_animation(self, run_time=2, **kwargs): The animation of the camera zooming in. """ frame = self.zoomed_camera.frame - full_frame_height = self.camera.get_frame_height() - full_frame_width = self.camera.get_frame_width() + full_frame_height = self.camera.frame_height + full_frame_width = self.camera.frame_width frame.save_state() frame.stretch_to_fit_width(full_frame_width) frame.stretch_to_fit_height(full_frame_height) From 2401ba2a0b117431f135aa08ea7b52236e670dda Mon Sep 17 00:00:00 2001 From: leotrs Date: Fri, 21 Aug 2020 15:46:29 -0400 Subject: [PATCH 2/2] thanks black --- manim/camera/camera.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/manim/camera/camera.py b/manim/camera/camera.py index 57a30531de..924fcfd551 100644 --- a/manim/camera/camera.py +++ b/manim/camera/camera.py @@ -1051,9 +1051,9 @@ def get_coords_of_all_pixels(self): # These are addressed in the same y, x order as in pixel_array, but the values in them # are listed in x, y order - uncentered_pixel_coords = np.indices( - [self.pixel_height, self.pixel_width] - )[::-1].transpose(1, 2, 0) + uncentered_pixel_coords = np.indices([self.pixel_height, self.pixel_width])[ + ::-1 + ].transpose(1, 2, 0) uncentered_space_coords = fdiv( uncentered_pixel_coords * full_space_dims, full_pixel_dims )