Slightly cleaner implementation of set_cairo_context_path

This commit is contained in:
Grant Sanderson 2019-02-11 16:05:35 -08:00
parent 1f320a3974
commit c23f456fd6
2 changed files with 16 additions and 41 deletions

View file

@ -326,53 +326,23 @@ class Camera(object):
self.apply_stroke(ctx, vmobject)
return self
# def old_set_cairo_context_path(self, ctx, vmobject):
# ctx.new_path()
# for vmob in it.chain([vmobject], vmobject.get_subpath_mobjects()):
# points = self.transform_points_pre_display(
# vmob, vmob.points
# )
# if np.any(np.isnan(points)) or np.any(points == np.inf):
# # TODO, print some kind of warning about
# # mobject having invalid points?
# points = np.zeros((1, 3))
# ctx.new_sub_path()
# ctx.move_to(*points[0][:2])
# for p0, p1, p2 in zip(points[1::3], points[2::3], points[3::3]):
# ctx.curve_to(*p0[:2], *p1[:2], *p2[:2])
# if vmob.is_closed():
# ctx.close_path()
# return self
def set_cairo_context_path(self, ctx, vmobject):
points = self.transform_points_pre_display(
vmobject, vmobject.points
)
if len(points) == 0:
return
elif np.any(np.isnan(points)) or np.any(points == np.inf):
# TODO, print some kind of warning about
# mobject having invalid points?
points = np.zeros((1, 3))
def should_start_new_path(last_p3, p0):
if last_p3 is None:
return True
else:
return not vmobject.consider_points_equals(
last_p3, p0
)
last_p3 = None
quads = vmobject.get_cubic_bezier_tuples_from_points(points)
ctx.new_path()
for p0, p1, p2, p3 in quads:
if should_start_new_path(last_p3, p0):
subpaths = vmobject.get_subpaths_from_points(points)
for subpath in subpaths:
quads = vmobject.get_cubic_bezier_tuples_from_points(subpath)
ctx.new_sub_path()
ctx.move_to(*p0[:2])
start = subpath[0]
ctx.move_to(*start[:2])
for p0, p1, p2, p3 in quads:
ctx.curve_to(*p1[:2], *p2[:2], *p3[:2])
last_p3 = p3
if vmobject.is_closed():
if vmobject.consider_points_equals(subpath[0], subpath[-1]):
ctx.close_path()
return self
@ -574,7 +544,11 @@ class Camera(object):
def transform_points_pre_display(self, mobject, points):
# Subclasses (like ThreeDCamera) may want to
# adjust points before they're shown
# adjust points futher before they're shown
if np.any(np.isnan(points)) or np.any(points == np.inf):
# TODO, print some kind of warning about
# mobject having invalid points?
points = np.zeros((1, 3))
return points
def points_to_pixel_coords(self, mobject, points):

View file

@ -183,6 +183,7 @@ class ThreeDCamera(Camera):
return self.project_points(point.reshape((1, 3)))[0, :]
def transform_points_pre_display(self, mobject, points):
points = super().transform_points_pre_display(mobject, points)
fixed_orientation = mobject in self.fixed_orientation_mobjects
fixed_in_frame = mobject in self.fixed_in_frame_mobjects