Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 34 additions & 131 deletions manim/camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -207,26 +121,26 @@ 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.
If self.background_image is the path of an image
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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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_,
[
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
],
)

Expand All @@ -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

Expand Down Expand Up @@ -1143,14 +1046,14 @@ 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()]
)[::-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
)
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions manim/camera/mapping_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)),
Expand Down
6 changes: 3 additions & 3 deletions manim/camera/multi_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
33 changes: 10 additions & 23 deletions manim/camera/three_d_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion manim/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
Loading