mirror of
https://github.com/3b1b/manim.git
synced 2025-09-19 04:41:56 +00:00
Merge pull request #20 from mirefek/master
3 little fixes + SVG matrix transform support
This commit is contained in:
commit
e27e7f539f
3 changed files with 36 additions and 13 deletions
|
@ -5,6 +5,15 @@ from vectorized_mobject import VMobject
|
||||||
from topics.geometry import Rectangle, Circle
|
from topics.geometry import Rectangle, Circle
|
||||||
from helpers import *
|
from helpers import *
|
||||||
|
|
||||||
|
def string_to_numbers(num_string):
|
||||||
|
num_string = num_string.replace("-",",-")
|
||||||
|
num_string = num_string.replace("e,-","e-")
|
||||||
|
return [
|
||||||
|
float(s)
|
||||||
|
for s in re.split("[ ,]", num_string)
|
||||||
|
if s != ""
|
||||||
|
]
|
||||||
|
|
||||||
class SVGMobject(VMobject):
|
class SVGMobject(VMobject):
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
"initial_scale_factor" : 1,
|
"initial_scale_factor" : 1,
|
||||||
|
@ -129,6 +138,27 @@ class SVGMobject(VMobject):
|
||||||
y = -float(element.getAttribute('y'))
|
y = -float(element.getAttribute('y'))
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
transform = element.getAttribute('transform')
|
||||||
|
prefix = "matrix("
|
||||||
|
suffix = ")"
|
||||||
|
if not transform.startswith(prefix) or not transform.endswith(suffix): raise Exception()
|
||||||
|
transform = transform[len(prefix):-len(suffix)]
|
||||||
|
transform = string_to_numbers(transform)
|
||||||
|
transform = np.array(transform).reshape([3,2])
|
||||||
|
x += transform[2][0]
|
||||||
|
y += transform[2][1]
|
||||||
|
matrix = np.identity(self.dim)
|
||||||
|
matrix[:2,:2] = transform[:2,:]
|
||||||
|
t_matrix = np.transpose(matrix)
|
||||||
|
|
||||||
|
for mob in mobject.family_members_with_points():
|
||||||
|
mob.points = np.dot(mob.points, t_matrix)
|
||||||
|
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
mobject.shift(x*RIGHT+y*UP)
|
mobject.shift(x*RIGHT+y*UP)
|
||||||
#TODO, transforms
|
#TODO, transforms
|
||||||
|
|
||||||
|
@ -146,8 +176,6 @@ class SVGMobject(VMobject):
|
||||||
self.scale_in_place(self.initial_scale_factor)
|
self.scale_in_place(self.initial_scale_factor)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class VMobjectFromSVGPathstring(VMobject):
|
class VMobjectFromSVGPathstring(VMobject):
|
||||||
def __init__(self, path_string, **kwargs):
|
def __init__(self, path_string, **kwargs):
|
||||||
digest_locals(self)
|
digest_locals(self)
|
||||||
|
@ -195,7 +223,8 @@ class VMobjectFromSVGPathstring(VMobject):
|
||||||
if len(points) > 0:
|
if len(points) > 0:
|
||||||
self.growing_path = self.add_subpath(new_points)
|
self.growing_path = self.add_subpath(new_points)
|
||||||
else:
|
else:
|
||||||
self.growing_path.start_at(new_points[0])
|
if isLower: self.growing_path.start_at(np.sum(new_points, axis=0))
|
||||||
|
else: self.growing_path.start_at(new_points[-1])
|
||||||
return
|
return
|
||||||
elif command in ["L", "H", "V"]: #lineto
|
elif command in ["L", "H", "V"]: #lineto
|
||||||
if command == "H":
|
if command == "H":
|
||||||
|
@ -224,7 +253,7 @@ class VMobjectFromSVGPathstring(VMobject):
|
||||||
# self.mark_paths_closed = True
|
# self.mark_paths_closed = True
|
||||||
|
|
||||||
#Handle situations where there's multiple relative control points
|
#Handle situations where there's multiple relative control points
|
||||||
if isLower and len(points) > 3:
|
if isLower and len(new_points) > 3:
|
||||||
for i in range(3, len(new_points), 3):
|
for i in range(3, len(new_points), 3):
|
||||||
new_points[i:i+3] -= points[-1]
|
new_points[i:i+3] -= points[-1]
|
||||||
new_points[i:i+3] += new_points[i-1]
|
new_points[i:i+3] += new_points[i-1]
|
||||||
|
@ -232,12 +261,7 @@ class VMobjectFromSVGPathstring(VMobject):
|
||||||
self.growing_path.add_control_points(new_points)
|
self.growing_path.add_control_points(new_points)
|
||||||
|
|
||||||
def string_to_points(self, coord_string):
|
def string_to_points(self, coord_string):
|
||||||
coord_string = coord_string.replace("-",",-")
|
numbers = string_to_numbers(coord_string)
|
||||||
numbers = [
|
|
||||||
float(s)
|
|
||||||
for s in re.split("[ ,]", coord_string)
|
|
||||||
if s != ""
|
|
||||||
]
|
|
||||||
if len(numbers)%2 == 1:
|
if len(numbers)%2 == 1:
|
||||||
numbers.append(0)
|
numbers.append(0)
|
||||||
num_points = len(numbers)/2
|
num_points = len(numbers)/2
|
||||||
|
@ -247,4 +271,3 @@ class VMobjectFromSVGPathstring(VMobject):
|
||||||
|
|
||||||
def get_original_path_string(self):
|
def get_original_path_string(self):
|
||||||
return self.path_string
|
return self.path_string
|
||||||
|
|
||||||
|
|
|
@ -220,7 +220,7 @@ class VMobject(Mobject):
|
||||||
|
|
||||||
def get_subpath_mobjects(self):
|
def get_subpath_mobjects(self):
|
||||||
return filter(
|
return filter(
|
||||||
lambda m : m.is_subpath,
|
lambda m : hasattr(m, 'is_subpath') and m.is_subpath,
|
||||||
self.submobjects
|
self.submobjects
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -85,7 +85,7 @@ class Line(VMobject):
|
||||||
start_to_end = self.end - self.start
|
start_to_end = self.end - self.start
|
||||||
length = np.linalg.norm(start_to_end)
|
length = np.linalg.norm(start_to_end)
|
||||||
if length > 2*self.buff:
|
if length > 2*self.buff:
|
||||||
start_to_end /= np.linalg.norm(start_to_end)
|
start_to_end = start_to_end / np.linalg.norm(start_to_end)
|
||||||
self.start = self.start + self.buff*start_to_end
|
self.start = self.start + self.buff*start_to_end
|
||||||
self.end = self.end - self.buff*start_to_end
|
self.end = self.end - self.buff*start_to_end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue