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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2014, Nicolas P. Rougier. All rights reserved.
# Distributed under the terms of the new BSD License.
# -----------------------------------------------------------------------------
import re
import math
import numpy as np
# ------------------------------------------------------------------ Matrix ---
class Matrix(object):
def __init__(self, a=1, b=0, c=0, d=1, e=0, f=0):
self._matrix = np.array([[a, c, e],
[b, d, f],
[0, 0, 1]], dtype=float)
@property
def matrix(self):
return self._matrix
def __array__(self, *args):
return self._matrix
def __repr__(self):
a, c, e = self._matrix[0]
b, d, f = self._matrix[1]
return "Matrix(%g,%g,%g,%g,%g,%g)" % (a, b, c, d, e, f)
# ---------------------------------------------------------------- Identity ---
class Identity(Matrix):
def __init__(self):
Matrix.__init__(self)
self._matrix[...] = ([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
def __repr__(self):
return "Identity()"
# --------------------------------------------------------------- Translate ---
class Translate(Matrix):
"""
Translation is equivalent to the matrix [1 0 0 1 tx ty], where tx and ty
are the distances to translate coordinates in X and Y, respectively.
"""
def __init__(self, x, y=0):
Matrix.__init__(self)
self._x, self._y = x, y
self._matrix[...] = ([[1, 0, x],
[0, 1, y],
[0, 0, 1]])
def __repr__(self):
return "Translate(%g,%g)" % (self._x, self._y)
# ------------------------------------------------------------------- Scale ---
class Scale(Matrix):
"""
Scaling is equivalent to the matrix [sx 0 0 sy 0 0]. One unit in the X and
Y directions in the new coordinate system equals sx and sy units in the
previous coordinate system, respectively.
"""
def __init__(self, x, y=0):
Matrix.__init__(self)
self._x = x
self._y = y or x
self._matrix[...] = ([[x, 0, 0],
[0, y, 0],
[0, 0, 1]])
def __repr__(self):
return "Scale(%g,%g)" % (self._x, self._y)
# ------------------------------------------------------------------- Scale ---
class Rotate(Matrix):
"""
Rotation about the origin is equivalent to the matrix [cos(a) sin(a)
-sin(a) cos(a) 0 0], which has the effect of rotating the coordinate system
axes by angle a.
"""
def __init__(self, angle, x=0, y=0):
Matrix.__init__(self)
self._angle = angle
self._x = x
self._y = y
angle = math.pi * angle / 180.0
rotate = np.array([[math.cos(angle), -math.sin(angle), 0],
[math.sin(angle), math.cos(angle), 0],
[0, 0, 1]], dtype=float)
forward = np.array([[1, 0, x],
[0, 1, y],
[0, 0, 1]], dtype=float)
inverse = np.array([[1, 0, -x],
[0, 1, -y],
[0, 0, 1]], dtype=float)
self._matrix = np.dot(inverse, np.dot(rotate, forward))
def __repr__(self):
return "Rotate(%g,%g,%g)" % (self._angle, self._x, self._y)
# ------------------------------------------------------------------- SkewX ---
class SkewX(Matrix):
"""
A skew transformation along the x-axis is equivalent to the matrix [1 0
tan(a) 1 0 0], which has the effect of skewing X coordinates by angle a.
"""
def __init__(self, angle):
Matrix.__init__(self)
self._angle = angle
angle = math.pi * angle / 180.0
self._matrix[...] = ([[1, math.tan(angle), 0],
[0, 1, 0],
[0, 0, 1]])
def __repr__(self):
return "SkewX(%g)" % (self._angle)
# ------------------------------------------------------------------- SkewY ---
class SkewY(Matrix):
"""
A skew transformation along the y-axis is equivalent to the matrix [1
tan(a) 0 1 0 0], which has the effect of skewing Y coordinates by angle a.
"""
def __init__(self, angle):
Matrix.__init__(self)
self._angle = angle
angle = math.pi * angle / 180.0
self._matrix[...] = ([[1, 0, 0],
[math.tan(angle), 1, 0],
[0, 0, 1]])
def __repr__(self):
return "SkewY(%g)" % (self._angle)
# --------------------------------------------------------------- Transform ---
class Transform(object):
"""
A Transform is defined as a list of transform definitions, which are
applied in the order provided. The individual transform definitions are
separated by whitespace and/or a comma.
"""
def __init__(self, content=""):
self._transforms = []
if not content:
return
converters = {"matrix": Matrix,
"scale": Scale,
"rotate": Rotate,
"translate": Translate,
"skewx": SkewX,
"skewy": SkewY}
keys = "|".join(converters.keys())
pattern = r"(?P<name>%s)\s*\((?P<args>[^)]*)\)" % keys
for match in re.finditer(pattern, content):
name = match.group("name").strip()
args = match.group("args").strip().replace(',', ' ')
args = [float(value) for value in args.split()]
transform = converters[name](*args)
self._transforms.append(transform)
def __add__(self, other):
T = Transform()
T._transforms.extend(self._transforms)
T._transforms.extend(other._transforms)
return T
def __radd__(self, other):
self._transforms.extend(other._transforms)
return self
@property
def matrix(self):
M = np.eye(3)
for transform in self._transforms:
M = np.dot(M, transform)
return M
def __array__(self, *args):
return self._matrix
def __repr__(self):
s = ""
for i in range(len(self._transforms)):
s += repr(self._transforms[i])
if i < len(self._transforms) - 1:
s += ", "
return s
@property
def xml(self):
return self._xml()
def _xml(self, prefix=""):
identity = True
for transform in self._transforms:
if not isinstance(transform, Identity):
identity = False
break
if identity:
return ""
return 'transform="%s" ' % repr(self)
|