Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Using Camera class instead of displayer module

Grant Sanderson74517d798d76
diff --git a/brachistochrone/curves.py b/brachistochrone/curves.py
index 539173cf0..85dfec8d6 100644
--- a/brachistochrone/curves.py
+++ b/brachistochrone/curves.py
@@ -24,7 +24,7 @@ from topics.geometry import \
from topics.characters import Randolph, Mathematician
from topics.functions import ParametricFunction, FunctionGraph
from topics.number_line import NumberPlane
-from region import Region, region_from_polygon_vertices
+from mobject.region import Region, region_from_polygon_vertices
from scene import Scene
RANDY_SCALE_VAL = 0.3
diff --git a/brachistochrone/drawing_images.py b/brachistochrone/drawing_images.py
index 0afd3cf80..f37796d74 100644
--- a/brachistochrone/drawing_images.py
+++ b/brachistochrone/drawing_images.py
@@ -28,7 +28,7 @@ from topics.geometry import \
from topics.characters import Randolph, Mathematician, ThoughtBubble
from topics.functions import ParametricFunction
from topics.number_line import NumberPlane
-from region import Region, region_from_polygon_vertices
+from mobject.region import Region, region_from_polygon_vertices
from scene import Scene
diff --git a/brachistochrone/light.py b/brachistochrone/light.py
index 77b265e98..7ba181ed9 100644
--- a/brachistochrone/light.py
+++ b/brachistochrone/light.py
@@ -24,7 +24,7 @@ from topics.geometry import \
from topics.characters import Randolph, Mathematician
from topics.functions import ParametricFunction, FunctionGraph
from topics.number_line import NumberPlane
-from region import Region, region_from_polygon_vertices
+from mobject.region import Region, region_from_polygon_vertices
from scene import Scene
from brachistochrone.curves import Cycloid
diff --git a/brachistochrone/metaphors.py b/brachistochrone/metaphors.py
index bfd4d6149..51ce4ca1e 100644
--- a/brachistochrone/metaphors.py
+++ b/brachistochrone/metaphors.py
@@ -24,7 +24,7 @@ from topics.geometry import \
from topics.characters import Randolph, Mathematician
from topics.functions import ParametricFunction, FunctionGraph
from topics.number_line import NumberPlane
-from region import Region, region_from_polygon_vertices
+from mobject.region import Region, region_from_polygon_vertices
from scene import Scene
diff --git a/brachistochrone/wordplay.py b/brachistochrone/wordplay.py
index fb912bd63..aedc466eb 100644
--- a/brachistochrone/wordplay.py
+++ b/brachistochrone/wordplay.py
@@ -25,7 +25,7 @@ from topics.geometry import \
from topics.characters import Randolph, Mathematician
from topics.functions import ParametricFunction, FunctionGraph
from topics.number_line import NumberPlane
-from region import Region, region_from_polygon_vertices
+from mobject.region import Region, region_from_polygon_vertices
from scene import Scene
from generate_logo import LogoGeneration
from brachistochrone.drawing_images import sort_by_color
diff --git a/camera.py b/camera.py
index 1c03c582b..e33c7c600 100644
--- a/camera.py
+++ b/camera.py
@@ -54,17 +54,6 @@ class Camera(object):
def reset(self):
self.set_image(np.array(self.background))
- # def paint_region(region, image_array = None, color = None):
- # pixels = get_pixels(image_array)
- # assert region.shape == pixels.shape[:2]
- # if color is None:
- # #Random dark color
- # rgb = 0.5 * np.random.random(3)
- # else:
- # rgb = np.array(Color(color).get_rgb())
- # pixels[region.bool_grid] = (255*rgb).astype('uint8')
- # return pixels
-
def capture_mobject(self, mobject):
return self.capture_mobjects([mobject])
@@ -77,10 +66,27 @@ class Camera(object):
mobjects = reduce(op.add, all_families, [])
for mobject in mobjects:
- self.display_points(
- mobject.points, mobject.rgbs,
- self.adjusted_thickness(mobject.point_thickness)
- )
+ if mobject.display_mode == "region":
+ self.display_region(mobject)
+ elif mobject.display_mode == "points":
+ self.display_points(
+ mobject.points, mobject.rgbs,
+ self.adjusted_thickness(mobject.point_thickness)
+ )
+ else:
+ raise Exception("Invalid display mode")
+
+ def display_region(self, region):
+ (h, w) = self.pixel_height, self.pixel_width
+ scalar = 2*self.space_height / h
+ xs = scalar*np.arange(-w/2, w/2)
+ ys = -scalar*np.arange(-h/2, h/2)
+ x_array = np.dot(np.ones((h, 1)), xs.reshape((1, w)))
+ y_array = np.dot(ys.reshape(h, 1), np.ones((1, w)))
+ covered = region.condition(x_array, y_array)
+ rgb = np.array(Color(region.color).get_rgb())
+ rgb = (255*rgb).astype('uint8')
+ self.pixel_array[covered] = rgb
def display_points(self, points, rgbs, thickness):
if len(points) == 0:
@@ -138,31 +144,6 @@ class Camera(object):
pixel_coords[:,1] < self.pixel_height,
])
- # def add_thickness(pixel_indices_and_rgbs, thickness, width, height):
- # """
- # Imagine dragging each pixel around like a paintbrush in
- # a plus-sign-shaped pixel arrangement surrounding it.
-
- # Pass rgb = None to do nothing to them
- # """
- # thickness = adjusted_thickness(thickness, width, height)
- # original = np.array(pixel_indices_and_rgbs)
- # n_extra_columns = pixel_indices_and_rgbs.shape[1] - 2
- # for nudge in range(-thickness/2+1, thickness/2+1):
- # if nudge == 0:
- # continue
- # for x, y in [[nudge, 0], [0, nudge]]:
- # pixel_indices_and_rgbs = np.append(
- # pixel_indices_and_rgbs,
- # original+([x, y] + [0]*n_extra_columns),
- # axis = 0
- # )
- # admissibles = (pixel_indices_and_rgbs[:,0] >= 0) & \
- # (pixel_indices_and_rgbs[:,0] < width) & \
- # (pixel_indices_and_rgbs[:,1] >= 0) & \
- # (pixel_indices_and_rgbs[:,1] < height)
- # return pixel_indices_and_rgbs[admissibles]
-
def adjusted_thickness(self, thickness):
big_width = PRODUCTION_QUALITY_DISPLAY_CONFIG["width"]
big_height = PRODUCTION_QUALITY_DISPLAY_CONFIG["height"]
@@ -185,7 +166,18 @@ class Camera(object):
+class MovingCamera(Camera):
+ """
+ Stays in line with the height, width and position
+ of a given mobject
+ """
+ def __init__(self, mobject, **kwargs):
+ digest_locals(self)
+ Camera.__init__(self, **kwargs)
+ def capture_mobjects(self, *args, **kwargs):
+ self.space_height = self.mobject.get_height()
+ self.space_center = self.mobject.get_center()
diff --git a/fluid_flow.py b/fluid_flow.py
index c2e524990..a948fc6ef 100644
--- a/fluid_flow.py
+++ b/fluid_flow.py
@@ -1,7 +1,7 @@
from mobject import Mobject
from mobject.image_mobject import MobjectFromRegion
from mobject.tex_mobject import TextMobject
-from region import region_from_polygon_vertices
+from mobject.region import region_from_polygon_vertices
from topics.geometry import Arrow, Dot, Circle, Line, FilledRectangle
from topics.number_line import NumberPlane, XYZAxes
from topics.three_dimensions import Sphere
diff --git a/mobject/mobject.py b/mobject/mobject.py
index 932a6d5a1..bb519af55 100644
--- a/mobject/mobject.py
+++ b/mobject/mobject.py
@@ -7,7 +7,6 @@ from random import random
from copy import deepcopy
from colour import Color
-import displayer as disp
from helpers import *
@@ -20,6 +19,7 @@ class Mobject(object):
"color" : WHITE,
"point_thickness" : DEFAULT_POINT_THICKNESS,
"name" : None,
+ "display_mode" : "points"
}
DIM = 3
def __init__(self, *sub_mobjects, **kwargs):
@@ -100,7 +100,10 @@ class Mobject(object):
return self
def show(self):
- Image.fromarray(disp.paint_mobject(self)).show()
+ from camera import Camera
+ camera = Camera()
+ camera.capture_mobject(self)
+ Image.fromarray(camera.get_image()).show()
def save_image(self, name = None):
Image.fromarray(disp.paint_mobject(self)).save(
diff --git a/region.py b/mobject/region.py
similarity index 72%
rename from region.py
rename to mobject/region.py
index def9b1300..89769fca5 100644
--- a/region.py
+++ b/mobject/region.py
@@ -4,15 +4,16 @@ from PIL import Image
import cv2
from copy import deepcopy
+from mobject import Mobject
-import displayer as disp
from helpers import *
-class Region(object):
- def __init__(self,
- condition = None,
- shape = None,
- ):
+#TODO, this whole class should be something vectorized.
+class Region(Mobject):
+ DEFAULT_CONFIG = {
+ "display_mode" : "region"
+ }
+ def __init__(self, condition = (lambda x, y : True), **kwargs):
"""
Condition must be a function which takes in two real
arrays (representing x and y values of space respectively)
@@ -20,35 +21,14 @@ class Region(object):
a function from R^2 to {True, False}, but & and | must be
used in place of "and" and "or"
"""
- if shape == None:
- self.shape = (DEFAULT_HEIGHT, DEFAULT_WIDTH)
- else:
- self.shape = shape
- # self.condition = condition
- (h, w) = self.shape
- scalar = 2*SPACE_HEIGHT / h
- xs = scalar*np.arange(-w/2, w/2)
- ys = -scalar*np.arange(-h/2, h/2)
- #TODO, do xs and ys really need to be saved in self?
- self.xs = np.dot(
- np.ones((h, 1)),
- xs.reshape((1, w))
- )
- self.ys = np.dot(
- ys.reshape(h, 1),
- np.ones((1, w))
- )
- if condition:
- self.bool_grid = condition(self.xs, self.ys)
- else:
- self.bool_grid = np.ones(self.shape, dtype = 'bool')
-
- def show(self, color = None):
- Image.fromarray(disp.paint_region(self, color = color)).show()
+ Mobject.__init__(self, **kwargs)
+ self.condition = condition
def _combine(self, region, op):
- assert region.shape == self.shape
- self.bool_grid = op(self.bool_grid, region.bool_grid)
+ self.condition = lambda x, y : op(
+ self.condition(x, y),
+ region.condition(x, y)
+ )
def union(self, region):
self._combine(region, lambda bg1, bg2 : bg1 | bg2)
diff --git a/old_projects/complex_multiplication_article.py b/old_projects/complex_multiplication_article.py
index 4a893fcc0..9af406fa5 100644
--- a/old_projects/complex_multiplication_article.py
+++ b/old_projects/complex_multiplication_article.py
@@ -9,7 +9,7 @@ import sys
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
from scene import Scene
from topics.complex_numbers import *
diff --git a/old_projects/counting_in_binary.py b/old_projects/counting_in_binary.py
index 3d77ccf53..459d30731 100644
--- a/old_projects/counting_in_binary.py
+++ b/old_projects/counting_in_binary.py
@@ -9,7 +9,7 @@ import sys
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
from scene import Scene, SceneFromVideo
from script_wrapper import command_line_create_scene
diff --git a/old_projects/eulers_characteristic_formula.py b/old_projects/eulers_characteristic_formula.py
index b74748b35..20aca735c 100644
--- a/old_projects/eulers_characteristic_formula.py
+++ b/old_projects/eulers_characteristic_formula.py
@@ -9,7 +9,7 @@ import sys
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
import displayer as disp
from scene import Scene, GraphScene
from scene.graphs import *
diff --git a/old_projects/inventing_math.py b/old_projects/inventing_math.py
index 8f1a92d8a..430db5eba 100644
--- a/old_projects/inventing_math.py
+++ b/old_projects/inventing_math.py
@@ -11,7 +11,7 @@ from random import sample
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
from scene import Scene, RearrangeEquation
from script_wrapper import command_line_create_scene
diff --git a/old_projects/inventing_math_images.py b/old_projects/inventing_math_images.py
index 8d48b2903..81273f873 100644
--- a/old_projects/inventing_math_images.py
+++ b/old_projects/inventing_math_images.py
@@ -9,7 +9,7 @@ import sys
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
from scene import Scene
from script_wrapper import command_line_create_scene
from inventing_math import divergent_sum, draw_you
diff --git a/old_projects/moser_intro.py b/old_projects/moser_intro.py
index 58febd6f5..32bcf4f5c 100644
--- a/old_projects/moser_intro.py
+++ b/old_projects/moser_intro.py
@@ -9,7 +9,7 @@ from copy import deepcopy
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
from scene import Scene
RADIUS = SPACE_HEIGHT - 0.1
diff --git a/old_projects/moser_main.py b/old_projects/moser_main.py
index 2cb10fdba..26800ea05 100644
--- a/old_projects/moser_main.py
+++ b/old_projects/moser_main.py
@@ -12,7 +12,7 @@ import inspect
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
from scene import Scene, GraphScene, PascalsTriangleScene
from script_wrapper import command_line_create_scene
diff --git a/old_projects/music_and_measure.py b/old_projects/music_and_measure.py
index 69a86386f..02ee8e60a 100644
--- a/old_projects/music_and_measure.py
+++ b/old_projects/music_and_measure.py
@@ -9,7 +9,7 @@ from fractions import Fraction, gcd
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
from scene import Scene
from inventing_math import Underbrace
diff --git a/old_projects/playground_counting_in_binary.py b/old_projects/playground_counting_in_binary.py
index 5e0367308..6ebe7e572 100644
--- a/old_projects/playground_counting_in_binary.py
+++ b/old_projects/playground_counting_in_binary.py
@@ -9,7 +9,7 @@ import sys
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
from scene import Scene, SceneFromVideo
from script_wrapper import command_line_create_scene
diff --git a/old_projects/pythagorean_proof.py b/old_projects/pythagorean_proof.py
index 6d0b5d6af..ba909fc9c 100644
--- a/old_projects/pythagorean_proof.py
+++ b/old_projects/pythagorean_proof.py
@@ -7,7 +7,7 @@ from helpers import *
from scene import Scene
from geometry import Polygon
-from region import region_from_polygon_vertices, region_from_line_boundary
+from mobject.region import region_from_polygon_vertices, region_from_line_boundary
A_COLOR = BLUE
B_COLOR = MAROON_D
diff --git a/old_projects/tau_poem.py b/old_projects/tau_poem.py
index bb530b730..8fa136971 100644
--- a/old_projects/tau_poem.py
+++ b/old_projects/tau_poem.py
@@ -9,7 +9,7 @@ import sys
from animation import *
from mobject import *
from constants import *
-from region import *
+from mobject.region import *
from scene import Scene
from script_wrapper import command_line_create_scene
from generate_logo import LogoGeneration
diff --git a/scene/scene.py b/scene/scene.py
index 6081e33fa..ccd74c106 100644
--- a/scene/scene.py
+++ b/scene/scene.py
@@ -103,32 +103,30 @@ class Scene(object):
self.camera.capture_mobjects(self.mobjects)
return self
- def play(self, *animations, **kwargs):
- self.num_animations += 1
+ def align_run_times(self, *animations, **kwargs):
if "run_time" in kwargs:
run_time = kwargs["run_time"]
else:
run_time = animations[0].run_time
for animation in animations:
animation.set_run_time(run_time)
+ return animations
+
+ def separate_static_and_moving_mobjects(self, *animations):
moving_mobjects = [
mobject
for anim in animations
for mobject in anim.mobject.submobject_family()
]
-
bundle = Mobject(*self.mobjects)
static_mobjects = filter(
lambda m : m not in moving_mobjects,
bundle.submobject_family()
)
- self.camera.reset()
- self.camera.capture_mobjects(
- static_mobjects,
- include_sub_mobjects = False
- )
- static_image = self.camera.get_image()
+ return moving_mobjects, static_mobjects
+ def get_time_progression(self, animations):
+ run_time = animations[0].run_time
times = np.arange(0, run_time, self.frame_duration)
time_progression = ProgressDisplay(times)
time_progression.set_description(
@@ -136,7 +134,24 @@ class Scene(object):
str(animations[0]) + \
(", etc." if len(animations) > 1 else "")
)
- for t in time_progression:
+ return time_progression
+
+ def play(self, *animations, **kwargs):
+ if len(animations) == 0:
+ raise Warning("Called Scene.play with no animations")
+ return
+ self.num_animations += 1
+ animations = self.align_run_times(*animations, **kwargs)
+ moving_mobjects, static_mobjects = \
+ self.separate_static_and_moving_mobjects(*animations)
+ self.camera.reset()
+ self.camera.capture_mobjects(
+ static_mobjects,
+ include_sub_mobjects = False
+ )
+ static_image = self.camera.get_image()
+
+ for t in self.get_time_progression(animations):
for animation in animations:
animation.update(t / animation.run_time)
self.camera.capture_mobjects([