Coverage for tatlin/lib/vector.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-20 05:56 +0000

1# -*- coding: utf-8 -*- 

2# Copyright (C) 2011 Denis Kobozev 

3# 

4# This program is free software; you can redistribute it and/or modify 

5# it under the terms of the GNU General Public License as published by 

6# the Free Software Foundation; either version 2 of the License, or 

7# (at your option) any later version. 

8# 

9# This program is distributed in the hope that it will be useful, 

10# but WITHOUT ANY WARRANTY; without even the implied warranty of 

11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

12# GNU General Public License for more details. 

13# 

14# You should have received a copy of the GNU General Public License 

15# along with this program; if not, write to the Free Software Foundation, 

16# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

17 

18 

19import numpy 

20import math 

21 

22 

23_identity_matrix = [ 

24 [1.0, 0.0, 0.0], 

25 [0.0, 1.0, 0.0], 

26 [0.0, 0.0, 1.0], 

27] 

28 

29_rotation_matrix_cache = {} 

30 

31 

32def identity_matrix(): 

33 return numpy.require(_identity_matrix[:], 'f') 

34 

35 

36def rotation_matrix(angle, x, y, z): 

37 angle_r = math.radians(angle) 

38 c = math.cos(angle_r) 

39 s = math.sin(angle_r) 

40 C = 1 - c 

41 matrix = numpy.require([ 

42 [x ** 2 * C + c, x * y * C - z * s, x * z * C + y * s], 

43 [y * x * C + z * s, y ** 2 * C + c, y * z * C - x * s], 

44 [x * z * C - y * s, y * z * C + x * s, z ** 2 * C + c], 

45 ], 'f') 

46 return matrix 

47 

48 

49def translate(vertices, x, y, z): 

50 translated = vertices + numpy.array([x, y, z], 'f') 

51 return translated 

52 

53 

54def rotate(vertices, angle, x, y, z): 

55 key = (angle, x, y, z) 

56 if key not in _rotation_matrix_cache: 

57 _rotation_matrix_cache[key] = rotation_matrix(angle, x, y, z) 

58 

59 matrix = _rotation_matrix_cache[key] 

60 rotated = numpy.dot(vertices, matrix) 

61 return rotated