Package glitter :: Package convenience :: Module matrices
[hide private]
[frames] | no frames]

Source Code for Module glitter.convenience.matrices

 1  """Convenience factory methods for geometric transform matrices. 
 2   
 3  @author: Stephan Wenger 
 4  @date: 2012-03-05 
 5  """ 
 6   
 7  import numpy as _np 
 8   
9 -def identity_matrix():
10 return _np.matrix(_np.eye(4))
11
12 -def rotation_matrix(angle, axis, degrees=False):
13 if degrees: 14 angle = _np.deg2rad(angle) 15 c, s = _np.cos(angle), _np.sin(angle) 16 x, y, z = axis / _np.sqrt(sum(a ** 2 for a in axis)) 17 return _np.matrix(( 18 (x * x * (1 - c) + c, x * y * (1 - c) - z * s, x * z * (1 - c) + y * s, 0), 19 (y * x * (1 - c) + z * s, y * y * (1 - c) + c, y * z * (1 - c) - x * s, 0), 20 (z * x * (1 - c) - y * s, z * y * (1 - c) + x * s, z * z * (1 - c) + c, 0), 21 (0, 0, 0, 1), 22 ))
23
24 -def scale_matrix(*s):
25 if len(s) == 3: 26 x, y, z = s 27 elif len(s) == 1: 28 x = y = z = s[0] 29 else: 30 raise ValueError("invalid scaling parameter") 31 return _np.matrix(((x, 0, 0, 0), (0, y, 0, 0), (0, 0, z, 0), (0, 0, 0, 1)))
32 33 __all__ = ["identity_matrix", "rotation_matrix", "scale_matrix"] 34