Performance improvements in affine helpers

This commit is contained in:
Steven Robertson 2011-10-12 13:56:34 -04:00
parent d409f02e4a
commit 0f615bd98b

View File

@ -5,6 +5,9 @@ in row-major order, like C, instead of Fortran-style column-major storage.
import numpy as np
_ident = np.matrix([[1,0,0], [0,1,0], [0,0,1]], dtype=np.float64)
_point = np.matrix([0,0,1], dtype=np.float64).T
def from_flam3(a):
"""Convert from flam3-format [3][2] arrays to an affine matrix."""
return np.matrix([ [a[0][0], a[1][0], a[2][0]]
@ -12,15 +15,24 @@ def from_flam3(a):
, [0, 0, 1]])
def scale(x, y):
return np.matrix([[x,0,0], [0,y,0], [0,0,1]])
r = _ident.copy()
r[0,0] = x
r[1,1] = y
return r
def translate(x, y):
return np.matrix([[1,0,x], [0,1,y], [0,0,1]])
r = _ident.copy()
r[0,2] = x
r[1,2] = y
return r
def rotOrigin(rad):
c = np.cos(rad)
r = _ident.copy()
r[0,0] = r[1,1] = np.cos(rad)
s = np.sin(rad)
return np.matrix([[c, -s, 0], [s, c, 0], [0, 0, 1]])
r[0,1] = -s
r[1,0] = s
return r
def rotate(rad, x, y):
"""Rotates around the given point (x, y)."""
@ -29,6 +41,9 @@ def rotate(rad, x, y):
def apply(m, x, y):
"""Apply matrix to point, returning new point as a tuple. Extends point
to homogeneous coordinates before applying. Mostly here as an example."""
r = m * np.matrix([x, y, 1]).T
p = _point.copy()
p[0,0] = x
p[1,0] = y
r = m * p
return r[0], r[1]