Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Replace some setters and getters from Camera (#330)

Leo Torresd49de94e0af8
diff --git a/manim/camera/camera.py b/manim/camera/camera.py
index 7520e31cd..924fcfd55 100644
--- a/manim/camera/camera.py
+++ b/manim/camera/camera.py
@@ -108,92 +108,6 @@ class Camera(object):
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 @@ class Camera(object):
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 @@ class Camera(object):
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 @@ class Camera(object):
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 @@ class Camera(object):
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 @@ class Camera(object):
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 @@ class Camera(object):
*
# 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 @@ class Camera(object):
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 @@ class Camera(object):
# 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 @@ class Camera(object):
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 @@ class Camera(object):
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 @@ class Camera(object):
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,14 +1046,14 @@ class Camera(object):
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()]
- )[::-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
)
@@ -1180,7 +1083,7 @@ class BackgroundColoredVMobjectDisplayer(object):
"""
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 f86d612cc..184f0d8d4 100644
--- a/manim/camera/mapping_camera.py
+++ b/manim/camera/mapping_camera.py
@@ -58,9 +58,9 @@ class OldMultiCamera(Camera):
"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 @@ class SplitScreenCamera(OldMultiCamera):
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 6065786d3..b78685812 100644
--- a/manim/camera/multi_camera.py
+++ b/manim/camera/multi_camera.py
@@ -43,14 +43,14 @@ class MultiCamera(MovingCamera):
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 1bf1668ab..5b4706304 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 @@ class ThreeDCamera(Camera):
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 @@ class ThreeDCamera(Camera):
"""
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 @@ class ThreeDCamera(Camera):
"""
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 @@ class ThreeDCamera(Camera):
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 b7780d2e3..b9584a8a8 100644
--- a/manim/scene/scene.py
+++ b/manim/scene/scene.py
@@ -160,7 +160,7 @@ class Scene(Container):
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 update_frame( # TODO Description in Docstring
self,
diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py
index 7ef58e21b..bd5342b66 100644
--- a/manim/scene/scene_file_writer.py
+++ b/manim/scene/scene_file_writer.py
@@ -394,8 +394,8 @@ class SceneFileWriter(object):
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 967b607ae..f2060978d 100644
--- a/manim/scene/zoomed_scene.py
+++ b/manim/scene/zoomed_scene.py
@@ -101,8 +101,8 @@ class ZoomedScene(MovingCameraScene):
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)