Changed camera implementation by renaming space_shape to frame_shape, where its height and width actually correspond to the camera's height and width

This commit is contained in:
Grant Sanderson
2018-03-30 11:34:22 -07:00
parent e168bfe5ba
commit 213c615870
8 changed files with 32 additions and 34 deletions

View File

@ -17,8 +17,8 @@ class Camera(object):
CONFIG = {
"background_image" : None,
"pixel_shape" : (DEFAULT_HEIGHT, DEFAULT_WIDTH),
# Note: space_shape will be resized to match pixel_shape
"space_shape" : (FRAME_Y_RADIUS, FRAME_X_RADIUS),
# Note: frame_shape will be resized to match pixel_shape
"frame_shape" : (FRAME_HEIGHT, FRAME_WIDTH),
"space_center" : ORIGIN,
"background_color" : BLACK,
#Points in vectorized mobjects with norm greater
@ -38,7 +38,7 @@ class Camera(object):
digest_config(self, kwargs, locals())
self.rgb_max_val = np.iinfo(self.pixel_array_dtype).max
self.init_background()
self.resize_space_shape()
self.resize_frame_shape()
self.reset()
def __deepcopy__(self, memo):
@ -48,20 +48,20 @@ class Camera(object):
self.canvas = None
return copy.copy(self)
def resize_space_shape(self, fixed_dimension = 0):
def resize_frame_shape(self, fixed_dimension = 0):
"""
Changes space_shape to match the aspect ratio
Changes frame_shape to match the aspect ratio
of pixel_shape, where fixed_dimension determines
whether space_shape[0] (height) or space_shape[1] (width)
whether frame_shape[0] (height) or frame_shape[1] (width)
remains fixed while the other changes accordingly.
"""
aspect_ratio = float(self.pixel_shape[1])/self.pixel_shape[0]
space_x_radius, space_y_radius = self.space_shape
frame_width, frame_height = self.frame_shape
if fixed_dimension == 0:
space_y_radius = aspect_ratio*space_x_radius
frame_height = aspect_ratio*frame_width
else:
space_x_radius = space_y_radius/aspect_ratio
self.space_shape = (space_x_radius, space_y_radius)
frame_width = frame_height/aspect_ratio
self.frame_shape = (frame_width, frame_height)
def init_background(self):
if self.background_image is not None:
@ -470,10 +470,10 @@ class Camera(object):
def points_to_pixel_coords(self, points):
result = np.zeros((len(points), 2))
ph, pw = self.pixel_shape
sh, sw = self.space_shape
width_mult = pw/sw/2
sh, sw = self.frame_shape
width_mult = pw/sw
width_add = pw/2
height_mult = ph/sh/2
height_mult = ph/sh
height_add = ph/2
#Flip on y-axis as you go
height_mult *= -1
@ -510,7 +510,7 @@ class Camera(object):
def get_coords_of_all_pixels(self):
# These are in x, y order, to help me keep things straight
full_space_dims = np.array(self.space_shape)[::-1] * 2
full_space_dims = np.array(self.frame_shape)[::-1]
full_pixel_dims = np.array(self.pixel_shape)[::-1]
# These are addressed in the same y, x order as in pixel_array, but the values in them
@ -520,7 +520,7 @@ class Camera(object):
uncentered_pixel_coords * full_space_dims,
full_pixel_dims)
# Could structure above line's computation slightly differently, but figured (without much
# thought) multiplying by space_shape first, THEN dividing by pixel_shape, is probably
# thought) multiplying by frame_shape first, THEN dividing by pixel_shape, is probably
# better than the other order, for avoiding underflow quantization in the division (whereas
# overflow is unlikely to be a problem)
@ -614,18 +614,16 @@ class MovingCamera(Camera):
def capture_mobjects(self, *args, **kwargs):
self.space_center = self.mobject.get_center()
self.realign_space_shape()
self.realign_frame_shape()
Camera.capture_mobjects(self, *args, **kwargs)
def realign_space_shape(self):
height, width = self.space_shape
def realign_frame_shape(self):
height, width = self.frame_shape
if self.aligned_dimension == "height":
self.space_shape = (self.mobject.get_height()/2, width)
self.frame_shape = (self.mobject.get_height(), width)
else:
self.space_shape = (height, self.mobject.get_width()/2)
self.resize_space_shape(
0 if self.aligned_dimension == "height" else 1
)
self.frame_shape = (height, self.mobject.get_width())
self.resize_frame_shape(0 if self.aligned_dimension == "height" else 1)
# TODO: Add an attribute to mobjects under which they can specify that they should just
# map their centers but remain otherwise undistorted (useful for labels, etc.)
@ -720,7 +718,7 @@ class SplitScreenCamera(MultiCamera):
for camera in [self.left_camera, self.right_camera]:
camera.pixel_shape = (self.pixel_shape[0], half_width) # TODO: Round up on one if width is odd
camera.init_background()
camera.resize_space_shape()
camera.resize_frame_shape()
camera.reset()
MultiCamera.__init__(self, (left_camera, (0, 0)), (right_camera, (0, half_width)))