Merge pull request #115 from 3b1b/camera-performance

Camera performance
This commit is contained in:
Grant Sanderson
2018-02-10 22:35:47 -08:00
committed by GitHub
8 changed files with 62 additions and 47 deletions

View File

@@ -48,7 +48,7 @@ class Transform(Animation):
self.path_arc,
self.path_arc_axis,
)
def get_all_mobjects(self):
return self.mobject, self.starting_mobject, self.target_mobject

View File

@@ -10,10 +10,6 @@ from helpers import *
from mobject import Mobject, PMobject, VMobject, \
ImageMobject, Group, BackgroundColoredVMobject
# Set a @profile decorator over any method whose
# performance you'd like to analyze
from profilehooks import profile
class Camera(object):
CONFIG = {
"background_image" : None,
@@ -240,14 +236,22 @@ class Camera(object):
canvas.symbol((0, 0), symbol, pen, fill)
def get_pen_and_fill(self, vmobject):
pen = aggdraw.Pen(
self.color_to_hex_l(self.get_stroke_color(vmobject)),
max(vmobject.stroke_width, 0)
)
fill = aggdraw.Brush(
self.color_to_hex_l(self.get_fill_color(vmobject)),
opacity = int(self.color_max_val*vmobject.get_fill_opacity())
)
stroke_width = max(vmobject.get_stroke_width(), 0)
if stroke_width == 0:
pen = None
else:
stroke_rgb = self.get_stroke_rgb(vmobject)
stroke_hex = rgb_to_hex(stroke_rgb)
pen = aggdraw.Pen(stroke_hex, stroke_width)
fill_opacity = int(self.color_max_val*vmobject.get_fill_opacity())
if fill_opacity == 0:
fill = None
else:
fill_rgb = self.get_fill_rgb(vmobject)
fill_hex = rgb_to_hex(fill_rgb)
fill = aggdraw.Brush(fill_hex, fill_opacity)
return (pen, fill)
def color_to_hex_l(self, color):
@@ -256,33 +260,31 @@ class Camera(object):
except:
return Color(BLACK).get_hex_l()
def get_stroke_color(self, vmobject):
return vmobject.get_stroke_color()
def get_stroke_rgb(self, vmobject):
return vmobject.get_stroke_rgb()
def get_fill_color(self, vmobject):
return vmobject.get_fill_color()
def get_fill_rgb(self, vmobject):
return vmobject.get_fill_rgb()
def get_pathstring(self, vmobject):
result = ""
result = ""
for mob in [vmobject]+vmobject.get_subpath_mobjects():
points = mob.points
# points = self.adjust_out_of_range_points(points)
if len(points) == 0:
continue
points = self.align_points_to_camera(points)
coords = self.points_to_pixel_coords(points)
start = "M%d %d"%tuple(coords[0])
#(handle1, handle2, anchor) tripletes
triplets = zip(*[
coords[i+1::3]
for i in range(3)
])
cubics = [
"C" + " ".join(map(str, it.chain(*triplet)))
for triplet in triplets
]
end = "Z" if vmobject.mark_paths_closed else ""
result += " ".join([start] + cubics + [end])
aligned_points = self.align_points_to_camera(points)
coords = self.points_to_pixel_coords(aligned_points)
coord_strings = coords.flatten().astype(str)
#Start new path string with M
coord_strings[0] = "M" + coord_strings[0]
#The C at the start of every 6th number communicates
#that the following 6 define a cubic Bezier
coord_strings[2::6] = map(lambda s : "C" + str(s), coord_strings[2::6])
#Possibly finish with "Z"
if vmobject.mark_paths_closed:
coord_strings[-1] = coord_strings[-1] + " Z"
result += " ".join(coord_strings)
return result
def display_background_colored_vmobject(self, cvmobject):
@@ -307,6 +309,8 @@ class Camera(object):
self.pixel_array, array
)
## Methods for other rendering
def display_point_cloud(self, points, rgbas, thickness):
if len(points) == 0:
return

View File

@@ -89,6 +89,7 @@ def get_configuration():
"write_all" : args.write_all,
"output_name" : args.output_name,
"skip_to_animation_number" : args.skip_to_animation_number,
"end_after_animation_number" : None,
}
if args.low_quality:
config["camera_config"] = LOW_QUALITY_CAMERA_CONFIG
@@ -102,7 +103,12 @@ def get_configuration():
stan = config["skip_to_animation_number"]
if stan is not None:
config["skip_to_animation_number"] = int(stan)
if "," in stan:
start, end = stan.split(",")
config["skip_to_animation_number"] = int(start)
config["end_after_animation_number"] = int(end)
else:
config["skip_to_animation_number"] = int(stan)
config["skip_animations"] = any([
config["show_last_frame"] and not config["write_to_movie"],
@@ -221,6 +227,7 @@ def main():
"output_directory",
"save_pngs",
"skip_to_animation_number",
"end_after_animation_number",
]
])

View File

@@ -126,7 +126,7 @@ def rgba_to_color(rgba):
return rgb_to_color(rgba[:3])
def rgb_to_hex(rgb):
return Color(rgb = rgb).get_hex_l()
return "#" + "".join('%02x'%int(255*x) for x in rgb)
def invert_color(color):
return rgb_to_color(1.0 - color_to_rgb(color))

View File

@@ -120,6 +120,9 @@ class VMobject(Mobject):
)
return self
def get_fill_rgb(self):
return self.fill_rgb
def get_fill_color(self):
try:
self.fill_rgb = np.clip(self.fill_rgb, 0.0, 1.0)
@@ -130,6 +133,9 @@ class VMobject(Mobject):
def get_fill_opacity(self):
return np.clip(self.fill_opacity, 0, 1)
def get_stroke_rgb(self):
return self.stroke_rgb
def get_stroke_color(self):
try:
self.stroke_rgb = np.clip(self.stroke_rgb, 0, 1)

View File

@@ -40,6 +40,7 @@ class Scene(Container):
"always_continually_update" : False,
"random_seed" : 0,
"skip_to_animation_number" : None,
"end_after_animation_number" : None,
}
def __init__(self, **kwargs):
Container.__init__(self, **kwargs) # Perhaps allow passing in a non-empty *mobjects parameter?
@@ -409,6 +410,10 @@ class Scene(Container):
if self.skip_to_animation_number:
if self.num_plays + 1 == self.skip_to_animation_number:
self.skip_animations = False
if self.end_after_animation_number:
if self.num_plays >= self.end_after_animation_number:
self.skip_animations = True
return self #Don't even both with the rest...
if self.skip_animations:
kwargs["run_time"] = 0

View File

@@ -136,8 +136,6 @@ class NumberLine(VMobject):
self.tip = tip
self.add(tip)
class UnitInterval(NumberLine):
CONFIG = {
"x_min" : 0,

View File

@@ -40,22 +40,17 @@ class ThreeDCamera(CameraWithPerspective):
self.rotation_mobject = VectorizedPoint()
self.set_position(self.phi, self.theta, self.distance)
def get_color(self, method):
color = method()
vmobject = method.im_self
def modified_rgb(self, vmobject, rgb):
if should_shade_in_3d(vmobject):
return Color(rgb = self.get_shaded_rgb(
color_to_rgb(color),
normal_vect = self.get_unit_normal_vect(vmobject)
))
return self.get_shaded_rgb(rgb, self.get_unit_normal_vect(vmobject))
else:
return color
def get_stroke_color(self, vmobject):
return self.get_color(vmobject.get_stroke_color)
def get_stroke_rgb(self, vmobject):
return self.modified_rgb(vmobject, vmobject.get_stroke_rgb())
def get_fill_color(self, vmobject):
return self.get_color(vmobject.get_fill_color)
def get_fill_rgb(self, vmobject):
return self.modified_rgb(vmobject, vmobject.get_fill_rgb())
def get_shaded_rgb(self, rgb, normal_vect):
brightness = np.dot(normal_vect, self.unit_sun_vect)**2