2018-03-31 15:11:35 -07:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-02-27 18:50:33 -08:00
|
|
|
import numpy as np
|
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
from scene.scene import Scene
|
|
|
|
from animation.creation import FadeIn
|
2018-03-31 15:20:30 -07:00
|
|
|
from camera.moving_camera import MovingCamera
|
2018-05-10 15:55:31 -07:00
|
|
|
from camera.multi_camera import MultiCamera
|
2018-03-31 18:05:02 -07:00
|
|
|
from mobject.geometry import Rectangle
|
2018-05-10 15:55:31 -07:00
|
|
|
from mobject.types.image_mobject import ImageMobjectFromCamera
|
2016-02-27 18:50:33 -08:00
|
|
|
|
2018-03-30 18:19:23 -07:00
|
|
|
from constants import *
|
2016-02-27 18:50:33 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-02-27 18:50:33 -08:00
|
|
|
class ZoomedScene(Scene):
|
2018-05-10 15:55:31 -07:00
|
|
|
CONFIG = {
|
|
|
|
"camera_class": MultiCamera,
|
|
|
|
"zoomed_display_height": 3,
|
|
|
|
"zoomed_display_width": 3,
|
|
|
|
"zoomed_display_center": None,
|
|
|
|
"zoomed_display_corner": UP + RIGHT,
|
|
|
|
"zoomed_display_corner_buff": DEFAULT_MOBJECT_TO_EDGE_BUFFER,
|
|
|
|
"zoomed_camera_config": {
|
|
|
|
"default_frame_stroke_width": 2,
|
|
|
|
},
|
|
|
|
"zoomed_camera_image_mobject_config": {},
|
|
|
|
"zoomed_camera_frame_starting_position": ORIGIN,
|
|
|
|
"zoom_factor": 0.15,
|
|
|
|
"image_frame_stroke_width": 3,
|
|
|
|
"zoom_activated": False,
|
|
|
|
}
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
# Initialize camera and display
|
|
|
|
zoomed_camera = MovingCamera(**self.zoomed_camera_config)
|
|
|
|
zoomed_display = ImageMobjectFromCamera(
|
|
|
|
zoomed_camera, **self.zoomed_camera_image_mobject_config
|
|
|
|
)
|
|
|
|
zoomed_display.add_display_frame()
|
|
|
|
for mob in zoomed_camera.frame, zoomed_display:
|
|
|
|
mob.stretch_to_fit_height(self.zoomed_display_height)
|
|
|
|
mob.stretch_to_fit_width(self.zoomed_display_width)
|
|
|
|
zoomed_camera.frame.scale(self.zoom_factor)
|
|
|
|
|
|
|
|
# Position camera and display
|
|
|
|
zoomed_camera.frame.move_to(self.zoomed_camera_frame_starting_position)
|
|
|
|
if self.zoomed_display_center is not None:
|
|
|
|
zoomed_display.move_to(self.zoomed_display_center)
|
|
|
|
else:
|
|
|
|
zoomed_display.to_corner(
|
|
|
|
self.zoomed_display_corner,
|
|
|
|
buff=self.zoomed_display_corner_buff
|
|
|
|
)
|
|
|
|
|
|
|
|
self.zoomed_camera = zoomed_camera
|
|
|
|
self.zoomed_display = zoomed_display
|
|
|
|
|
|
|
|
def activate_zooming(self, animate=False, run_times=[3, 2]):
|
|
|
|
self.zoom_activated = True
|
|
|
|
zoomed_camera = self.zoomed_camera
|
|
|
|
zoomed_display = self.zoomed_display
|
|
|
|
self.camera.add_image_mobject_from_camera(zoomed_display)
|
|
|
|
|
|
|
|
to_add = [zoomed_camera.frame, zoomed_display]
|
|
|
|
if animate:
|
|
|
|
zoomed_display.save_state()
|
|
|
|
zoomed_display.replace(zoomed_camera.frame)
|
|
|
|
|
|
|
|
full_frame_height, full_frame_width = self.camera.frame_shape
|
|
|
|
zoomed_camera.frame.save_state()
|
|
|
|
zoomed_camera.frame.stretch_to_fit_width(full_frame_width)
|
|
|
|
zoomed_camera.frame.stretch_to_fit_height(full_frame_height)
|
|
|
|
zoomed_camera.frame.center()
|
|
|
|
zoomed_camera.frame.set_stroke(width=0)
|
|
|
|
|
|
|
|
for mover, run_time in zip(to_add, run_times):
|
|
|
|
self.add_foreground_mobject(mover)
|
|
|
|
self.play(mover.restore, run_time=run_time)
|
|
|
|
else:
|
|
|
|
self.add_foreground_mobjects(*to_add)
|
|
|
|
|
|
|
|
def get_moving_mobjects(self, *animations):
|
|
|
|
moving_mobjects = Scene.get_moving_mobjects(self, *animations)
|
|
|
|
zoomed_mobjects = [self.zoomed_camera.frame, self.zoomed_display]
|
|
|
|
moving_zoomed_mobjects = set(moving_mobjects).intersection(zoomed_mobjects)
|
|
|
|
if self.zoom_activated and moving_zoomed_mobjects:
|
|
|
|
return self.mobjects
|
|
|
|
else:
|
|
|
|
return moving_mobjects
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OldZoomedScene(Scene):
|
2017-01-09 11:22:28 -08:00
|
|
|
"""
|
|
|
|
Move around self.little_rectangle to determine
|
|
|
|
which part of the screen is zoomed in on.
|
|
|
|
"""
|
2016-02-27 18:50:33 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"zoomed_canvas_frame_shape": (3, 3),
|
|
|
|
"zoomed_canvas_center": None,
|
|
|
|
"zoomed_canvas_corner": UP + RIGHT,
|
|
|
|
"zoomed_canvas_corner_buff": DEFAULT_MOBJECT_TO_EDGE_BUFFER,
|
|
|
|
"zoomed_camera_background": None,
|
|
|
|
"little_rectangle_start_position": ORIGIN,
|
|
|
|
"zoom_factor": 6,
|
|
|
|
"square_color": WHITE,
|
|
|
|
"zoom_activated": False,
|
2016-02-27 18:50:33 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-02-29 20:52:25 -08:00
|
|
|
def activate_zooming(self):
|
|
|
|
self.generate_big_rectangle()
|
2016-02-27 18:50:33 -08:00
|
|
|
self.setup_zoomed_canvas()
|
2016-02-29 20:52:25 -08:00
|
|
|
self.setup_zoomed_camera()
|
|
|
|
self.zoom_activated = True
|
2016-02-27 18:50:33 -08:00
|
|
|
|
2017-02-16 13:03:26 -08:00
|
|
|
def animate_activate_zooming(self):
|
|
|
|
self.activate_zooming()
|
|
|
|
self.play(*map(FadeIn, [
|
|
|
|
self.little_rectangle, self.big_rectangle
|
|
|
|
]))
|
|
|
|
|
2016-02-29 20:52:25 -08:00
|
|
|
def disactivate_zooming(self):
|
|
|
|
self.remove(self.big_rectangle, self.little_rectangle)
|
|
|
|
self.zoom_activated = False
|
|
|
|
|
|
|
|
def get_zoomed_camera_mobject(self):
|
|
|
|
return self.little_rectangle
|
|
|
|
|
2016-03-07 19:07:00 -08:00
|
|
|
def get_zoomed_screen(self):
|
|
|
|
return self.big_rectangle
|
|
|
|
|
2016-02-29 20:52:25 -08:00
|
|
|
def generate_big_rectangle(self):
|
2018-03-30 11:34:22 -07:00
|
|
|
height, width = self.zoomed_canvas_frame_shape
|
2016-02-29 20:52:25 -08:00
|
|
|
self.big_rectangle = Rectangle(
|
2018-04-06 13:58:59 -07:00
|
|
|
height=height,
|
|
|
|
width=width,
|
|
|
|
color=self.square_color
|
2016-02-29 20:52:25 -08:00
|
|
|
)
|
2016-02-27 18:50:33 -08:00
|
|
|
if self.zoomed_canvas_center is not None:
|
2016-02-29 20:52:25 -08:00
|
|
|
self.big_rectangle.shift(self.zoomed_canvas_center)
|
2016-02-27 18:50:33 -08:00
|
|
|
elif self.zoomed_canvas_corner is not None:
|
2017-01-30 14:34:59 -08:00
|
|
|
self.big_rectangle.to_corner(
|
|
|
|
self.zoomed_canvas_corner,
|
2018-04-06 13:58:59 -07:00
|
|
|
buff=self.zoomed_canvas_corner_buff
|
2017-01-30 14:34:59 -08:00
|
|
|
)
|
2016-02-29 20:52:25 -08:00
|
|
|
self.add(self.big_rectangle)
|
2016-02-27 18:50:33 -08:00
|
|
|
|
2016-02-29 20:52:25 -08:00
|
|
|
def setup_zoomed_canvas(self):
|
2018-04-06 13:58:59 -07:00
|
|
|
upper_left = self.big_rectangle.get_corner(UP + LEFT)
|
|
|
|
lower_right = self.big_rectangle.get_corner(DOWN + RIGHT)
|
2016-02-29 20:52:25 -08:00
|
|
|
pixel_coords = self.camera.points_to_pixel_coords(
|
|
|
|
np.array([upper_left, lower_right])
|
|
|
|
)
|
2016-02-27 18:50:33 -08:00
|
|
|
self.zoomed_canvas_pixel_indices = pixel_coords
|
2016-02-29 20:52:25 -08:00
|
|
|
(up, left), (down, right) = pixel_coords
|
2016-02-27 18:50:33 -08:00
|
|
|
self.zoomed_canvas_pixel_shape = (
|
2018-04-06 13:58:59 -07:00
|
|
|
right - left,
|
|
|
|
down - up,
|
2016-02-29 20:52:25 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
def setup_zoomed_camera(self):
|
|
|
|
self.little_rectangle = self.big_rectangle.copy()
|
2018-04-06 13:58:59 -07:00
|
|
|
self.little_rectangle.scale(1. / self.zoom_factor)
|
2017-02-16 13:03:26 -08:00
|
|
|
self.little_rectangle.move_to(
|
|
|
|
self.little_rectangle_start_position
|
|
|
|
)
|
2016-02-29 20:52:25 -08:00
|
|
|
self.zoomed_camera = MovingCamera(
|
|
|
|
self.little_rectangle,
|
2018-04-06 13:58:59 -07:00
|
|
|
pixel_shape=self.zoomed_canvas_pixel_shape,
|
|
|
|
background=self.zoomed_camera_background
|
2016-02-27 18:50:33 -08:00
|
|
|
)
|
2016-02-29 20:52:25 -08:00
|
|
|
self.add(self.little_rectangle)
|
2018-04-06 13:58:59 -07:00
|
|
|
# TODO, is there a better way to hanld this?
|
|
|
|
self.zoomed_camera.adjusted_thickness = lambda x: x
|
2016-02-27 18:50:33 -08:00
|
|
|
|
|
|
|
def get_frame(self):
|
|
|
|
frame = Scene.get_frame(self)
|
2016-02-29 20:52:25 -08:00
|
|
|
if self.zoom_activated:
|
|
|
|
(up, left), (down, right) = self.zoomed_canvas_pixel_indices
|
|
|
|
frame[left:right, up:down, :] = self.zoomed_camera.get_image()
|
2016-02-27 18:50:33 -08:00
|
|
|
return frame
|
|
|
|
|
2018-01-09 14:04:22 -08:00
|
|
|
def set_camera_pixel_array(self, pixel_array):
|
2017-09-26 17:41:45 -07:00
|
|
|
self.camera.set_pixel_array(pixel_array)
|
2016-11-11 11:18:41 -08:00
|
|
|
if self.zoom_activated:
|
|
|
|
(up, left), (down, right) = self.zoomed_canvas_pixel_indices
|
2018-04-06 13:58:59 -07:00
|
|
|
self.zoomed_camera.set_pixel_array(
|
|
|
|
pixel_array[left:right, up:down])
|
2016-12-06 22:25:26 -08:00
|
|
|
|
|
|
|
def set_camera_background(self, background):
|
2018-01-09 14:04:22 -08:00
|
|
|
self.set_camera_pixel_array(self, background)
|
2018-04-06 13:58:59 -07:00
|
|
|
# TODO, check this...
|
2016-11-11 11:18:41 -08:00
|
|
|
|
|
|
|
def reset_camera(self):
|
|
|
|
self.camera.reset()
|
2016-02-29 20:52:25 -08:00
|
|
|
if self.zoom_activated:
|
|
|
|
self.zoomed_camera.reset()
|
2016-02-27 18:50:33 -08:00
|
|
|
|
2016-11-11 11:18:41 -08:00
|
|
|
def capture_mobjects_in_camera(self, mobjects, **kwargs):
|
|
|
|
self.camera.capture_mobjects(mobjects, **kwargs)
|
|
|
|
if self.zoom_activated:
|
2017-01-25 16:40:59 -08:00
|
|
|
if self.big_rectangle in mobjects:
|
|
|
|
mobjects = list(mobjects)
|
|
|
|
mobjects.remove(self.big_rectangle)
|
2016-11-11 11:18:41 -08:00
|
|
|
self.zoomed_camera.capture_mobjects(
|
|
|
|
mobjects, **kwargs
|
|
|
|
)
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-01-17 22:13:38 -08:00
|
|
|
def get_moving_mobjects(self, *animations):
|
|
|
|
moving_mobjects = Scene.get_moving_mobjects(self, *animations)
|
2016-11-11 11:18:41 -08:00
|
|
|
if self.zoom_activated and self.little_rectangle in moving_mobjects:
|
2017-01-25 16:40:59 -08:00
|
|
|
# When the camera is moving, so is everything,
|
2018-01-17 22:13:38 -08:00
|
|
|
return self.mobjects
|
2016-11-11 11:18:41 -08:00
|
|
|
else:
|
2018-01-17 22:13:38 -08:00
|
|
|
return moving_mobjects
|