-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformation.py
150 lines (136 loc) · 4.65 KB
/
transformation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from typing import Tuple
import numpy as np
SVG_NAMESPACE = "http://www.w3.org/2000/svg"
NSMAP = {None: SVG_NAMESPACE}
IDENTITY_MATRIX = np.identity(3, dtype=float)
SVG_FUNC_MATRIX = 'matrix'
SVG_FUNC_TRANSLATE = 'translate'
SVG_FUNC_SCALE = 'scale'
SVG_FUNC_ROTATE = 'rotate'
SVG_FUNC_SKEWX = 'skewx'
SVG_FUNC_SKEWY = 'skewy'
SVG_X_ATTR = 'x'
SVG_Y_ATTR = 'y'
def ctw_to_svg_matrix_transform(ctw: np.ndarray) -> str:
a, c, e = ctw[0]
b, d, f = ctw[1]
return f'matrix({a},{b},{c},{d},{e},{f})'
def calculate_node_coodination(node, ctw: np.ndarray) -> Tuple[float, float]:
x = float(node.get(SVG_X_ATTR)) if SVG_X_ATTR in node.attrib else 0
y = float(node.get(SVG_Y_ATTR)) if SVG_Y_ATTR in node.attrib else 0
coord_vector = np.dot(ctw, np.array([x, y, 1], dtype=float))
return (coord_vector[0], coord_vector[1])
def svg_matrix_transform(transform_attr):
# matrix(1,0,0,-0.99998571,28.0033,17.5688)
# slice [7:-1] => 1,0,0,-0.99998571,28.0033,17.5688
#
# reference:
# https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform#matrix
a, b, c, d, e, f = list(map(float, transform_attr[7:-1].split(',')))
return np.array([
[a, c, e],
[b, d, f],
[0, 0, 1]
], dtype=float)
def svg_translate_transform(transform_attr):
# translate(1,2)
# slice [10:-1] => 1,2
coord_list = list(map(float, transform_attr[10:-1].split(',')))
if len(coord_list) > 1:
tx = coord_list[0]
ty = coord_list[1]
else:
tx = coord_list.pop()
ty = 0
return np.array([
[1, 0, tx],
[0, 1, ty],
[0, 0, 1]
], dtype=float)
def svg_scale_transform(transform_attr):
# scale(1,2)
# slice [6:-1] => 1,2
coord_list = list(map(float, transform_attr[6:-1].split(',')))
if len(coord_list) > 1:
sx = coord_list[0]
sy = coord_list[1]
else:
sx = coord_list.pop()
sy = 1
return np.array([
[sx, 0, 0],
[ 0, sy, 0],
[ 0, 0, 1]
], dtype=float)
def svg_rotate_transform(transform_attr):
# rotate(1, 2, 3)
# slice [7:-1] => 1,2,3
rotate_arr = list(map(float, transform_attr[7:-1].split(',')))
rotate_deg = rotate_arr[0]
rotate_rad = np.deg2rad(rotate_deg)
rotate_cos = np.cos(rotate_rad)
rotate_sin = np.sin(rotate_rad)
rotation_matrix = np.array([
[rotate_cos, -rotate_sin, 0],
[rotate_sin, rotate_cos, 0],
[ 0, 0, 1]
], dtype=float)
if rotate_arr > 2:
_, cx, cy = rotate_arr
elif rotate_arr > 1:
_, cx = rotate_arr
cy = 0
else:
cx, cy = 0
if cx and cy:
forward_translation_matrix = np.array([
[1, 0, cx],
[0, 1, cy],
[0, 0, 1]
], dtype=float)
backward_translation_matrix = np.array([
[1, 0, -cx],
[0, 1, -cy],
[0, 0, 1]
], dtype=float)
rotation_matrix = np.matmul(backward_translation_matrix,np.matmul(rotation_matrix, forward_translation_matrix))
return rotation_matrix
def svg_skewx_transform(transform_attr):
# skewX(1)
# slice [6:-1] => 1
skew_deg, = list(map(float, transform_attr[6:-1].split(',')))
skew_rad = np.rad2deg(skew_deg)
return np.array([
[1, np.tan(skew_rad), 0],
[0, 1, 0],
[0, 0, 1]
], dtype=float)
def svg_skewy_transform(transform_attr):
# skewY(1)
# slice [6:-1] => 1
skew_deg, = list(map(float, transform_attr[6:-1].split(',')))
skew_rad = np.rad2deg(skew_deg)
return np.array([
[ 1, 1, 0],
[np.tan(skew_rad), 1, 0],
[ 0, 0, 1]
], dtype=float)
def get_transformation_matrix(svg_transform_attrs: str) -> np.ndarray:
transforms = svg_transform_attrs.split(' ')
result_matrix = IDENTITY_MATRIX
for transform_func in transforms:
func_name, _ = transform_func.split('(')
if SVG_FUNC_MATRIX == func_name:
trans_matrix = svg_matrix_transform(transform_func)
elif SVG_FUNC_TRANSLATE == func_name:
trans_matrix = svg_translate_transform(transform_func)
elif SVG_FUNC_SCALE == func_name:
trans_matrix = svg_scale_transform(transform_func)
elif SVG_FUNC_ROTATE == func_name:
trans_matrix = svg_rotate_transform(transform_func)
elif SVG_FUNC_SKEWX == func_name:
trans_matrix = svg_skewx_transform(transform_func)
elif SVG_FUNC_SKEWY == func_name:
trans_matrix = svg_skewy_transform(transform_func)
result_matrix = np.matmul(trans_matrix, result_matrix)
return result_matrix