Return a reference to an element instead of a copied dictionary of attributes

pull/58/head
Michael X. Grey 2018-05-25 16:42:36 +08:00
parent b547969846
commit a6ceec4f0d
2 changed files with 23 additions and 15 deletions

View File

@ -123,7 +123,7 @@ def flatten_all_paths(
stack = [new_stack_element(group, np.identity(3))]
FlattenedPath = collections.namedtuple('FlattenedPath', ['path', 'attributes', 'transform'])
FlattenedPath = collections.namedtuple('FlattenedPath', ['path', 'element', 'transform'])
paths = []
while stack:
@ -135,7 +135,7 @@ def flatten_all_paths(
for path_elem in filter(path_filter, top.group.iterfind('svg:'+key, SVG_NAMESPACE)):
path_tf = top.transform.dot(parse_transform(path_elem.get('transform')))
path = transform(parse_path(converter(path_elem)), path_tf)
paths.append(FlattenedPath(path, path_elem.attrib, path_tf))
paths.append(FlattenedPath(path, path_elem, path_tf))
stack.extend(get_relevant_children(top.group, top.transform))

View File

@ -6,7 +6,7 @@ import numpy as np
def get_desired_path(name, paths):
return next(p for p in paths if p.attributes['{some://testuri}name'] == name)
return next(p for p in paths if p.element.get('{some://testuri}name') == name)
def column_vector(values):
@ -19,10 +19,17 @@ def column_vector(values):
class TestGroups(unittest.TestCase):
def check_values(self, v, z):
# Check that the components of 2D vector v match the components of complex number z
self.assertAlmostEqual(v[0], z.real)
self.assertAlmostEqual(v[1], z.imag)
def check_path(self, tf, v_s_vals, v_e_relative_vals, name, paths):
def check_line(self, tf, v_s_vals, v_e_relative_vals, name, paths):
# Check that the endpoints of the line have been correctly transformed.
# * tf is the transform that should have been applied.
# * v_s_vals is a 2D list of the values of the line's start point
# * v_e_relative_vals is a 2D list of the values of the line's end point relative to the start point
# * name is the path name (value of the test:name attribute in the SVG document)
# * paths is the output of doc.flatten_all_paths()
v_s_vals.append(1.0)
v_e_relative_vals.append(0.0)
v_s = column_vector(v_s_vals)
@ -34,6 +41,7 @@ class TestGroups(unittest.TestCase):
self.check_values(tf.dot(v_e), actual.path.end)
def test_group_flatten(self):
# Test the Document.flatten_all_paths() function against the groups.svg test file.
doc = Document(join(dirname(__file__), 'groups.svg'))
result = doc.flatten_all_paths()
@ -41,39 +49,39 @@ class TestGroups(unittest.TestCase):
tf_matrix_group = np.matrix([[1.5, 0.0, -40.0], [0.0, 0.5, 20.0], [0.0, 0.0, 1.0]])
self.check_path(tf_matrix_group,
self.check_line(tf_matrix_group,
[183, 183], [0.0, -50],
'path00', result)
tf_scale_group = np.matrix([[1.25, 0.0, 0.0], [0.0, 1.25, 0.0], [0.0, 0.0, 1.0]])
self.check_path(tf_matrix_group.dot(tf_scale_group),
self.check_line(tf_matrix_group.dot(tf_scale_group),
[122, 320], [-50.0, 0.0],
'path01', result)
self.check_path(tf_matrix_group.dot(tf_scale_group),
self.check_line(tf_matrix_group.dot(tf_scale_group),
[150, 200], [-50, 25],
'path02', result)
self.check_path(tf_matrix_group.dot(tf_scale_group),
self.check_line(tf_matrix_group.dot(tf_scale_group),
[150, 200], [-50, 25],
'path03', result)
tf_nested_translate_group = np.matrix([[1, 0, 20], [0, 1, 0], [0, 0, 1]])
self.check_path(tf_matrix_group.dot(tf_scale_group).dot(tf_nested_translate_group),
self.check_line(tf_matrix_group.dot(tf_scale_group).dot(tf_nested_translate_group),
[150, 200], [-50, 25],
'path04', result)
tf_nested_translate_xy_group = np.matrix([[1, 0, 20], [0, 1, 30], [0, 0, 1]])
self.check_path(tf_matrix_group.dot(tf_scale_group).dot(tf_nested_translate_xy_group),
self.check_line(tf_matrix_group.dot(tf_scale_group).dot(tf_nested_translate_xy_group),
[150, 200], [-50, 25],
'path05', result)
tf_scale_xy_group = np.matrix([[0.5, 0, 0], [0, 1.5, 0.0], [0, 0, 1]])
self.check_path(tf_matrix_group.dot(tf_scale_xy_group),
self.check_line(tf_matrix_group.dot(tf_scale_xy_group),
[122, 320], [-50, 0],
'path06', result)
@ -82,7 +90,7 @@ class TestGroups(unittest.TestCase):
[np.sin(a_07), np.cos(a_07), 0],
[0, 0, 1]])
self.check_path(tf_matrix_group.dot(tf_rotate_group),
self.check_line(tf_matrix_group.dot(tf_rotate_group),
[183, 183], [0, 30],
'path07', result)
@ -93,21 +101,21 @@ class TestGroups(unittest.TestCase):
tf_rotate_xy_group_T = np.matrix([[1, 0, 183], [0, 1, 183], [0, 0, 1]])
tf_rotate_xy_group = tf_rotate_xy_group_T.dot(tf_rotate_xy_group_R).dot(np.linalg.inv(tf_rotate_xy_group_T))
self.check_path(tf_matrix_group.dot(tf_rotate_xy_group),
self.check_line(tf_matrix_group.dot(tf_rotate_xy_group),
[183, 183], [0, 30],
'path08', result)
a_09 = 5.0*np.pi/180.0
tf_skew_x_group = np.matrix([[1, np.tan(a_09), 0], [0, 1, 0], [0, 0, 1]])
self.check_path(tf_matrix_group.dot(tf_skew_x_group),
self.check_line(tf_matrix_group.dot(tf_skew_x_group),
[183, 183], [40, 40],
'path09', result)
a_10 = 5.0*np.pi/180.0
tf_skew_y_group = np.matrix([[1, 0, 0], [np.tan(a_10), 1, 0], [0, 0, 1]])
self.check_path(tf_matrix_group.dot(tf_skew_y_group),
self.check_line(tf_matrix_group.dot(tf_skew_y_group),
[183, 183], [40, 40],
'path10', result)