Skip to content

Commit d49de94

Browse files
authored
Replace some setters and getters from Camera (#330)
* Replace some setters and getters from Camera. Also turn ThreeDCamera.frame_center into a property. * thanks black
1 parent bcb7551 commit d49de94

File tree

7 files changed

+56
-166
lines changed

7 files changed

+56
-166
lines changed

manim/camera/camera.py

Lines changed: 34 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -108,92 +108,6 @@ def reset_pixel_shape(self, new_height, new_width):
108108
self.resize_frame_shape()
109109
self.reset()
110110

111-
def get_pixel_height(self):
112-
"""Returns the height of the scene in
113-
pixel at that moment.
114-
115-
Returns
116-
-------
117-
int
118-
The height of the scene in pixels.
119-
"""
120-
return self.pixel_height
121-
122-
def get_pixel_width(self):
123-
"""Returns the width of the scene in
124-
pixels at that moment.
125-
126-
Returns
127-
-------
128-
int
129-
The width of the scene in pixels.
130-
"""
131-
return self.pixel_width
132-
133-
def get_frame_height(self):
134-
"""Returns the height of the frame
135-
in MUnits. Default is 8.0
136-
137-
Returns
138-
-------
139-
float
140-
The frame height
141-
"""
142-
return self.frame_height
143-
144-
def get_frame_width(self):
145-
"""Returns the width of the frame
146-
in MUnits.
147-
148-
Returns
149-
-------
150-
float
151-
The frame width
152-
"""
153-
return self.frame_width
154-
155-
def get_frame_center(self):
156-
"""Returns the absolute center of the frame as Cartesian
157-
Coordinates with the unit MUnits.
158-
159-
Returns
160-
-------
161-
np.array
162-
The array of x,y,z coordinates.
163-
"""
164-
return self.frame_center
165-
166-
def set_frame_height(self, frame_height):
167-
"""Sets the frame height to the passed value.
168-
169-
Parameters
170-
----------
171-
frame_height : int, float
172-
The frame_height in MUnits.
173-
"""
174-
self.frame_height = frame_height
175-
176-
def set_frame_width(self, frame_width):
177-
"""Sets the frame width to the passed value.
178-
179-
Parameters
180-
----------
181-
frame_width : int, float
182-
The frame_width in MUnits.
183-
"""
184-
self.frame_width = frame_width
185-
186-
def set_frame_center(self, frame_center):
187-
"""Sets the center of the frame to the passed
188-
cartesian coordinates.
189-
190-
Parameters
191-
----------
192-
frame_center : np.array
193-
The center of the frame.
194-
"""
195-
self.frame_center = frame_center
196-
197111
def resize_frame_shape(self, fixed_dimension=0):
198112
"""
199113
Changes frame_shape to match the aspect ratio
@@ -207,26 +121,26 @@ def resize_frame_shape(self, fixed_dimension=0):
207121
If 0, height is scaled with respect to width
208122
else, width is scaled with respect to height.
209123
"""
210-
pixel_height = self.get_pixel_height()
211-
pixel_width = self.get_pixel_width()
212-
frame_height = self.get_frame_height()
213-
frame_width = self.get_frame_width()
124+
pixel_height = self.pixel_height
125+
pixel_width = self.pixel_width
126+
frame_height = self.frame_height
127+
frame_width = self.frame_width
214128
aspect_ratio = fdiv(pixel_width, pixel_height)
215129
if fixed_dimension == 0:
216130
frame_height = frame_width / aspect_ratio
217131
else:
218132
frame_width = aspect_ratio * frame_height
219-
self.set_frame_height(frame_height)
220-
self.set_frame_width(frame_width)
133+
self.frame_height = frame_height
134+
self.frame_width = frame_width
221135

222136
def init_background(self):
223137
"""Initialize the background.
224138
If self.background_image is the path of an image
225139
the image is set as background; else, the default
226140
background color fills the background.
227141
"""
228-
height = self.get_pixel_height()
229-
width = self.get_pixel_width()
142+
height = self.pixel_height
143+
width = self.pixel_width
230144
if self.background_image is not None:
231145
path = get_full_raster_image_path(self.background_image)
232146
image = Image.open(path).convert(self.image_mode)
@@ -262,17 +176,6 @@ def get_image(self, pixel_array=None):
262176
pixel_array = self.pixel_array
263177
return Image.fromarray(pixel_array, mode=self.image_mode)
264178

265-
def get_pixel_array(self):
266-
"""Returns the pixel array
267-
of the current frame.
268-
269-
Returns
270-
-------
271-
np.array
272-
The array of RGB values of each pixel.
273-
"""
274-
return self.pixel_array
275-
276179
def convert_pixel_array(self, pixel_array, convert_from_floats=False):
277180
"""Converts a pixel array from values that have floats in then
278181
to proper RGB values.
@@ -460,9 +363,9 @@ def is_in_frame(self, mobject):
460363
bool
461364
True if in frame, False otherwise.
462365
"""
463-
fc = self.get_frame_center()
464-
fh = self.get_frame_height()
465-
fw = self.get_frame_width()
366+
fc = self.frame_center
367+
fh = self.frame_height
368+
fw = self.frame_width
466369
return not reduce(
467370
op.or_,
468371
[
@@ -559,11 +462,11 @@ def get_cairo_context(self, pixel_array):
559462
cached_ctx = self.get_cached_cairo_context(pixel_array)
560463
if cached_ctx:
561464
return cached_ctx
562-
pw = self.get_pixel_width()
563-
ph = self.get_pixel_height()
564-
fw = self.get_frame_width()
565-
fh = self.get_frame_height()
566-
fc = self.get_frame_center()
465+
pw = self.pixel_width
466+
ph = self.pixel_height
467+
fw = self.frame_width
468+
fh = self.frame_height
469+
fc = self.frame_center
567470
surface = cairo.ImageSurface.create_for_data(
568471
pixel_array, cairo.FORMAT_ARGB32, pw, ph
569472
)
@@ -756,7 +659,7 @@ def apply_stroke(self, ctx, vmobject, background=False):
756659
*
757660
# This ensures lines have constant width
758661
# as you zoom in on them.
759-
(self.get_frame_width() / self.frame_width)
662+
(self.frame_width / self.frame_width)
760663
)
761664
ctx.stroke_preserve()
762665
return self
@@ -886,8 +789,8 @@ def display_point_cloud(self, pmobject, points, rgbas, thickness, pixel_array):
886789
pixel_coords = pixel_coords[on_screen_indices]
887790
rgbas = rgbas[on_screen_indices]
888791

889-
ph = self.get_pixel_height()
890-
pw = self.get_pixel_width()
792+
ph = self.pixel_height
793+
pw = self.pixel_width
891794

892795
flattener = np.array([1, pw], dtype="int")
893796
flattener = flattener.reshape((2, 1))
@@ -948,7 +851,7 @@ def display_image_mobject(self, image_mobject, pixel_array):
948851

949852
# Paste into an image as large as the camear's pixel array
950853
full_image = Image.fromarray(
951-
np.zeros((self.get_pixel_height(), self.get_pixel_width())), mode="RGBA"
854+
np.zeros((self.pixel_height, self.pixel_width)), mode="RGBA"
952855
)
953856
new_ul_coords = center_coords - np.array(sub_image.size) / 2
954857
new_ul_coords = new_ul_coords.astype(int)
@@ -1034,13 +937,13 @@ def points_to_pixel_coords(
1034937
self, mobject, points
1035938
): # TODO: Write more detailed docstrings for this method.
1036939
points = self.transform_points_pre_display(mobject, points)
1037-
shifted_points = points - self.get_frame_center()
940+
shifted_points = points - self.frame_center
1038941

1039942
result = np.zeros((len(points), 2))
1040-
pixel_height = self.get_pixel_height()
1041-
pixel_width = self.get_pixel_width()
1042-
frame_height = self.get_frame_height()
1043-
frame_width = self.get_frame_width()
943+
pixel_height = self.pixel_height
944+
pixel_width = self.pixel_width
945+
frame_height = self.frame_height
946+
frame_width = self.frame_width
1044947
width_mult = pixel_width / frame_width
1045948
width_add = pixel_width / 2
1046949
height_mult = pixel_height / frame_height
@@ -1070,9 +973,9 @@ def on_screen_pixels(self, pixel_coords):
1070973
op.and_,
1071974
[
1072975
pixel_coords[:, 0] >= 0,
1073-
pixel_coords[:, 0] < self.get_pixel_width(),
976+
pixel_coords[:, 0] < self.pixel_width,
1074977
pixel_coords[:, 1] >= 0,
1075-
pixel_coords[:, 1] < self.get_pixel_height(),
978+
pixel_coords[:, 1] < self.pixel_height,
1076979
],
1077980
)
1078981

@@ -1092,7 +995,7 @@ def adjusted_thickness(self, thickness):
1092995
big_sum = op.add(
1093996
camera_config["default_pixel_height"], camera_config["default_pixel_width"]
1094997
)
1095-
this_sum = op.add(self.get_pixel_height(), self.get_pixel_width())
998+
this_sum = op.add(self.pixel_height, self.pixel_width)
1096999
factor = fdiv(big_sum, this_sum)
10971000
return 1 + (thickness - 1) / factor
10981001

@@ -1143,14 +1046,14 @@ def get_coords_of_all_pixels(self):
11431046
The array of cartesian coordinates.
11441047
"""
11451048
# These are in x, y order, to help me keep things straight
1146-
full_space_dims = np.array([self.get_frame_width(), self.get_frame_height()])
1147-
full_pixel_dims = np.array([self.get_pixel_width(), self.get_pixel_height()])
1049+
full_space_dims = np.array([self.frame_width, self.frame_height])
1050+
full_pixel_dims = np.array([self.pixel_width, self.pixel_height])
11481051

11491052
# These are addressed in the same y, x order as in pixel_array, but the values in them
11501053
# are listed in x, y order
1151-
uncentered_pixel_coords = np.indices(
1152-
[self.get_pixel_height(), self.get_pixel_width()]
1153-
)[::-1].transpose(1, 2, 0)
1054+
uncentered_pixel_coords = np.indices([self.pixel_height, self.pixel_width])[
1055+
::-1
1056+
].transpose(1, 2, 0)
11541057
uncentered_space_coords = fdiv(
11551058
uncentered_pixel_coords * full_space_dims, full_pixel_dims
11561059
)
@@ -1180,7 +1083,7 @@ def __init__(self, camera):
11801083
"""
11811084
self.camera = camera
11821085
self.file_name_to_pixel_array_map = {}
1183-
self.pixel_array = np.array(camera.get_pixel_array())
1086+
self.pixel_array = np.array(camera.pixel_array)
11841087
self.reset_pixel_array()
11851088

11861089
def reset_pixel_array(self):

manim/camera/mapping_camera.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def __init__(self, *cameras_with_start_positions, **kwargs):
5858
"start_x": camera_with_start_positions[1][1],
5959
"start_y": camera_with_start_positions[1][0],
6060
"end_x": camera_with_start_positions[1][1]
61-
+ camera_with_start_positions[0].get_pixel_width(),
61+
+ camera_with_start_positions[0].pixel_width,
6262
"end_y": camera_with_start_positions[1][0]
63-
+ camera_with_start_positions[0].get_pixel_height(),
63+
+ camera_with_start_positions[0].pixel_height,
6464
}
6565
)
6666
for camera_with_start_positions in cameras_with_start_positions
@@ -113,10 +113,10 @@ def __init__(self, left_camera, right_camera, **kwargs):
113113
self.left_camera = left_camera
114114
self.right_camera = right_camera
115115

116-
half_width = self.get_pixel_width() / 2
116+
half_width = self.pixel_width / 2
117117
for camera in [self.left_camera, self.right_camera]:
118118
# TODO: Round up on one if width is odd
119-
camera.reset_pixel_shape(camera.get_pixel_height(), half_width)
119+
camera.reset_pixel_shape(camera.pixel_height, half_width)
120120

121121
OldMultiCamera.__init__(
122122
self, (left_camera, (0, 0)), (right_camera, (0, half_width)),

manim/camera/multi_camera.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ def add_image_mobject_from_camera(self, image_mobject_from_camera):
4343
def update_sub_cameras(self):
4444
""" Reshape sub_camera pixel_arrays """
4545
for imfc in self.image_mobjects_from_cameras:
46-
pixel_height, pixel_width = self.get_pixel_array().shape[:2]
46+
pixel_height, pixel_width = self.pixel_array.shape[:2]
4747
imfc.camera.frame_shape = (
4848
imfc.camera.frame.get_height(),
4949
imfc.camera.frame.get_width(),
5050
)
5151
imfc.camera.reset_pixel_shape(
52-
int(pixel_height * imfc.get_height() / self.get_frame_height()),
53-
int(pixel_width * imfc.get_width() / self.get_frame_width()),
52+
int(pixel_height * imfc.get_height() / self.frame_height),
53+
int(pixel_width * imfc.get_width() / self.frame_width),
5454
)
5555

5656
def reset(self):

manim/camera/three_d_camera.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class ThreeDCamera(Camera):
2424
"theta": -90 * DEGREES, # Rotation about z axis
2525
"gamma": 0, # Rotation about normal vector to camera
2626
"light_source_start_point": 9 * DOWN + 7 * LEFT + 10 * OUT,
27-
"frame_center": ORIGIN,
2827
"should_apply_shading": True,
2928
"exponential_projection": False,
3029
"max_allowable_norm": 3 * config["frame_width"],
@@ -46,11 +45,19 @@ def __init__(self, *args, **kwargs):
4645
self.distance_tracker = ValueTracker(self.distance)
4746
self.gamma_tracker = ValueTracker(self.gamma)
4847
self.light_source = Point(self.light_source_start_point)
49-
self.frame_center = Point(self.frame_center)
48+
self._frame_center = Point(kwargs.get("frame_center", ORIGIN))
5049
self.fixed_orientation_mobjects = dict()
5150
self.fixed_in_frame_mobjects = set()
5251
self.reset_rotation_matrix()
5352

53+
@property
54+
def frame_center(self):
55+
return self._frame_center.points[0]
56+
57+
@frame_center.setter
58+
def frame_center(self, point):
59+
self._frame_center.move_to(point)
60+
5461
def capture_mobjects(self, mobjects, **kwargs):
5562
self.reset_rotation_matrix()
5663
Camera.capture_mobjects(self, mobjects, **kwargs)
@@ -158,16 +165,6 @@ def get_gamma(self):
158165
"""
159166
return self.gamma_tracker.get_value()
160167

161-
def get_frame_center(self):
162-
"""Returns the center of the camera frame in cartesian coordinates.
163-
164-
Returns
165-
-------
166-
np.array
167-
The cartesian coordinates of the center of the camera frame.
168-
"""
169-
return self.frame_center.points[0]
170-
171168
def set_phi(self, value):
172169
"""Sets the polar angle i.e the angle between Z_AXIS and Camera through ORIGIN in radians.
173170
@@ -208,16 +205,6 @@ def set_gamma(self, value):
208205
"""
209206
self.gamma_tracker.set_value(value)
210207

211-
def set_frame_center(self, point):
212-
"""Sets the camera frame center to the passed cartesian coordinate.
213-
214-
Parameters
215-
----------
216-
point : list, tuple, np.array
217-
The cartesian coordinates of the new frame center.
218-
"""
219-
self.frame_center.move_to(point)
220-
221208
def reset_rotation_matrix(self):
222209
"""Sets the value of self.rotation_matrix to
223210
the matrix corresponding to the current position of the camera
@@ -269,7 +256,7 @@ def project_points(self, points):
269256
np.array
270257
The points after projecting.
271258
"""
272-
frame_center = self.get_frame_center()
259+
frame_center = self.frame_center
273260
distance = self.get_distance()
274261
rot_matrix = self.get_rotation_matrix()
275262

manim/scene/scene.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def get_frame(self):
160160
NumPy array of pixel values of each pixel in screen.
161161
The shape of the array is height x width x 3
162162
"""
163-
return np.array(self.camera.get_pixel_array())
163+
return np.array(self.camera.pixel_array)
164164

165165
def update_frame( # TODO Description in Docstring
166166
self,

0 commit comments

Comments
 (0)