/usr/lib/python2.7/dist-packages/kiva/affine.py is in python-enable 4.1.0-1ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | #------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Enthought, Inc.
# Description: <Enthought kiva package component>
#------------------------------------------------------------------------------
""" Functions for affine matrices.
:Copyright: Space Telescope Science Institute
:License: BSD Style
:Author: Eric Jones, Enthought, Inc., eric@enthought.com
These affine operations are based coordinate system transformations,
not translations of pts, etc. To translate a point, you multiply
it by the affine transform matrix::
a b 0
[x, y, 1] * c d 0 = [x',y',1]
tx ty 1
This is the opposite order of multiplication from what many are
accustomed to.
Here is a useful link:
http://mathworld.wolfram.com/AffineTransformation.html
Notes:
I'm not using a class because of possible speed implications.
Currently the affine transform is a 3x3 array. Other tools use
a 6-tuple of (a,b,c,d,tx,ty) to represent the transform because
the other 3 array entries are constant. Other code should call
methods from this module instead of manipulating the array,
in case the implementation is changed at some future date.
"""
from numpy import array, alltrue, arctan2, cos, dot, eye, float64, ones, \
ravel, sin, zeros
#-----------------------------------------------------------------------------
# Affine transform construction
#-----------------------------------------------------------------------------
def affine_identity():
""" Returns a new identity affine_transform object.
"""
return eye(3,3)
def affine_from_values(a,b,c,d,tx,ty):
"""
"""
transform = array(((a, b, 0), (c, d, 0), (tx, ty, 1)), float64)
return transform
def affine_from_scale(sx,sy):
""" Returns an affine transform providing the given scaling.
"""
r = affine_identity()
return scale(r,sx,sy)
def affine_from_rotation(angle):
""" Returns an affine transform rotated by angle in radians.
"""
r = affine_identity()
return rotate(r,angle)
def affine_from_translation(x,y):
""" Returns an affine transform with the given translation.
"""
r = affine_identity()
return translate(r,x,y)
#-----------------------------------------------------------------------------
# Affine transform manipulation
#-----------------------------------------------------------------------------
def scale(transform,sx,sy):
""" Returns a scaled version of the transform by the given values.
Scaling is done using the following formula::
sx 0 0 a b 0 sx*a sx*b 0
0 sy 0 * c d 0 = sy*c sy*d 0
0 0 1 tx ty 1 0 0 1
"""
# this isn't the operation described above, but produces the
# same results.
scaled = transform.copy()
scaled[0] *= sx
scaled[1] *= sy
return scaled
def rotate(transform,angle):
""" Rotates transform by angle in radians.
Rotation is done using the following formula::
cos(x) sin(x) 0 a b 0
-sin(x) cos(x) 0 * c d 0 =
0 0 1 tx ty 1
::
cos(x)*a+sin(x)*b cos(x)*b+sin(x)*d 0
-sin(x)*a+cos(x)*c -sin(x)*b+cos(x)*d 0
tx ty 1
where x = angle.
"""
a = cos(angle)
b = sin(angle)
c = -b
d = a
tx = 0.
ty = 0.
rot = affine_from_values(a,b,c,d,tx,ty)
return dot(rot,transform)
def translate(transform,x,y):
""" Returns transform translated by (x,y).
Translation::
1 0 0 a b 0 a b 0
0 1 0 * c d 0 = c d 0
x y 1 tx ty 1 x*a+y*c+y x*b+y*d+ty 1
"""
r = affine_identity()
r[2,0] = x
r[2,1] = y
return dot(r,transform)
def concat(transform,other):
""" Returns the concatenation of transform with other. This
is simply transform pre-multiplied by other.
"""
return dot(other,transform)
def invert(m):
""" Returns the inverse of the transform, m.
"""
inv = zeros(m.shape, float64)
det = m[0,0] * m[1,1] - m[0,1] * m[1,0]
inv[0,0] = m[1,1]
inv[0,1] = -m[0,1]
inv[0,2] = 0
inv[1,0] = -m[1,0]
inv[1,1] = m[0,0]
inv[1,2] = 0
inv[2,0] = m[1,0]*m[2,1] - m[1,1]*m[2,0]
inv[2,1] = -m[0,0]*m[2,1] + m[0,1]*m[2,0]
inv[2,2] = m[0,0]*m[1,1] - m[0,1]*m[1,0]
inv /= det
return inv
#-----------------------------------------------------------------------------
# Affine transform information
#-----------------------------------------------------------------------------
IDENTITY = affine_identity()
def is_identity(m):
""" Tests whether an affine transform is the identity transform.
"""
m1 = ravel(IDENTITY)
m2 = ravel(m)
return alltrue(m1 == m2)
# numarray return None for .flat...
#return alltrue(IDENTITY.flat == m.flat)
def affine_params(m):
""" Returns the a, b, c, d, tx, ty values of an
affine transform.
"""
a = m[0,0]
b = m[0,1]
c = m[1,0]
d = m[1,1]
tx = m[2,0]
ty = m[2,1]
return a,b,c,d,tx,ty
def trs_factor(m):
""" Factors a matrix as if it is the product of translate/rotate/scale
matrices applied (i.e., concatenated) in that order. It returns:
tx,ty,sx,sy,angle
where tx and ty are the translations, sx and sy are the scaling
values and angle is the rotational angle in radians.
If the input matrix was created in a way other than concatenating
t/r/s matrices, the results could be wrong. For example, if there
is any skew in the matrix, the returned results are wrong.
Needs Test!
"""
#-------------------------------------------------------------------------
# Extract Values from Matrix
#
# Translation values are correct as extracted. Rotation and
# scaling need a little massaging.
#-------------------------------------------------------------------------
a,b,c,d,tx,ty = affine_params(m)
#-------------------------------------------------------------------------
# Rotation -- tan(angle) = b/d
#-------------------------------------------------------------------------
angle = arctan2(b,d)
#-------------------------------------------------------------------------
# Scaling
#
# sx = a/cos(angle) or sx = -c/sin(angle)
# sy = d/cos(angle) or sy = b/sin(angle)
#-------------------------------------------------------------------------
cos_ang = cos(angle)
sin_ang = sin(angle)
if cos_ang != 0.0:
sx,sy = a/cos_ang, d/cos_ang
else:
sx,sy = -c/sin_ang, b/sin_ang
return tx,ty,sx,sy,angle
#-----------------------------------------------------------------------------
# Transforming points and arrays of points
#-----------------------------------------------------------------------------
def transform_point(ctm,pt):
""" Returns pt transformed by the affine transform, ctm.
"""
p1 = ones(3,float64)
p1[:2] = pt
res = dot(p1,ctm)[:2]
return res
def transform_points(ctm,pts):
""" Transforms an array of points using the affine transform, ctm.
"""
if is_identity(ctm):
res = pts
else:
x = pts[...,0]
y = pts[...,1]
a,b,c,d,tx,ty = affine_params(ctm)
# x_p = a*x+c*y+tx
# y_p = b*x+d*y+ty
res = zeros(pts.shape, float64)
res[...,0] = a*x+c*y+tx
res[...,1] = b*x+d*y+ty
return res
|