Mercurial > touhou
comparison pytouhou/utils/matrix.pyx @ 590:e15672733c93
Switch to Python 3.x instead of 2.7.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 30 Sep 2014 17:14:24 +0200 |
parents | db28538cd399 |
children | 560b45a7d014 |
comparison
equal
deleted
inserted
replaced
589:0768122da817 | 590:e15672733c93 |
---|---|
39 | 39 |
40 out = <Matrix*> malloc(sizeof(Matrix)) | 40 out = <Matrix*> malloc(sizeof(Matrix)) |
41 d1 = <float*>mat1 | 41 d1 = <float*>mat1 |
42 d2 = <float*>mat2 | 42 d2 = <float*>mat2 |
43 d3 = <float*>out | 43 d3 = <float*>out |
44 for i in xrange(4): | 44 for i in range(4): |
45 for j in xrange(4): | 45 for j in range(4): |
46 d3[4*i+j] = 0 | 46 d3[4*i+j] = 0 |
47 for k in xrange(4): | 47 for k in range(4): |
48 d3[4*i+j] += d1[4*i+k] * d2[4*k+j] | 48 d3[4*i+j] += d1[4*i+k] * d2[4*k+j] |
49 memcpy(mat1, out, sizeof(Matrix)) | 49 memcpy(mat1, out, sizeof(Matrix)) |
50 free(out) | 50 free(out) |
51 | 51 |
52 | 52 |
53 cdef void flip(Matrix *mat) nogil: | 53 cdef void flip(Matrix *mat) nogil: |
54 data = <float*>mat | 54 data = <float*>mat |
55 for i in xrange(4): | 55 for i in range(4): |
56 data[i] = -data[i] | 56 data[i] = -data[i] |
57 | 57 |
58 | 58 |
59 cdef void scale(Matrix *mat, float x, float y, float z) nogil: | 59 cdef void scale(Matrix *mat, float x, float y, float z) nogil: |
60 cdef float coordinate[3] | 60 cdef float coordinate[3] |
62 data = <float*>mat | 62 data = <float*>mat |
63 coordinate[0] = x | 63 coordinate[0] = x |
64 coordinate[1] = y | 64 coordinate[1] = y |
65 coordinate[2] = z | 65 coordinate[2] = z |
66 | 66 |
67 for i in xrange(3): | 67 for i in range(3): |
68 for j in xrange(4): | 68 for j in range(4): |
69 data[4*i+j] *= coordinate[i] | 69 data[4*i+j] *= coordinate[i] |
70 | 70 |
71 | 71 |
72 cdef void scale2d(Matrix *mat, float x, float y) nogil: | 72 cdef void scale2d(Matrix *mat, float x, float y) nogil: |
73 data = <float*>mat | 73 data = <float*>mat |
74 for i in xrange(4): | 74 for i in range(4): |
75 data[ i] *= x | 75 data[ i] *= x |
76 data[4+i] *= y | 76 data[4+i] *= y |
77 | 77 |
78 | 78 |
79 cdef void translate(Matrix *mat, float[3] offset) nogil: | 79 cdef void translate(Matrix *mat, float[3] offset) nogil: |
80 cdef float item[3] | 80 cdef float item[3] |
81 | 81 |
82 data = <float*>mat | 82 data = <float*>mat |
83 for i in xrange(3): | 83 for i in range(3): |
84 item[i] = data[12+i] * offset[i] | 84 item[i] = data[12+i] * offset[i] |
85 | 85 |
86 for i in xrange(3): | 86 for i in range(3): |
87 for j in xrange(4): | 87 for j in range(4): |
88 data[4*i+j] += item[i] | 88 data[4*i+j] += item[i] |
89 | 89 |
90 | 90 |
91 cdef void translate2d(Matrix *mat, float x, float y) nogil: | 91 cdef void translate2d(Matrix *mat, float x, float y) nogil: |
92 cdef float[3] offset | 92 cdef float[3] offset |
103 cdef float lines[8] | 103 cdef float lines[8] |
104 | 104 |
105 data = <float*>mat | 105 data = <float*>mat |
106 cos_a = cos(angle) | 106 cos_a = cos(angle) |
107 sin_a = sin(angle) | 107 sin_a = sin(angle) |
108 for i in xrange(8): | 108 for i in range(8): |
109 lines[i] = data[i+4] | 109 lines[i] = data[i+4] |
110 for i in xrange(4): | 110 for i in range(4): |
111 data[4+i] = cos_a * lines[i] - sin_a * lines[4+i] | 111 data[4+i] = cos_a * lines[i] - sin_a * lines[4+i] |
112 data[8+i] = sin_a * lines[i] + cos_a * lines[4+i] | 112 data[8+i] = sin_a * lines[i] + cos_a * lines[4+i] |
113 | 113 |
114 | 114 |
115 cdef void rotate_y(Matrix *mat, float angle) nogil: | 115 cdef void rotate_y(Matrix *mat, float angle) nogil: |
117 cdef float lines[8] | 117 cdef float lines[8] |
118 | 118 |
119 data = <float*>mat | 119 data = <float*>mat |
120 cos_a = cos(angle) | 120 cos_a = cos(angle) |
121 sin_a = sin(angle) | 121 sin_a = sin(angle) |
122 for i in xrange(4): | 122 for i in range(4): |
123 lines[i] = data[i] | 123 lines[i] = data[i] |
124 lines[i+4] = data[i+8] | 124 lines[i+4] = data[i+8] |
125 for i in xrange(4): | 125 for i in range(4): |
126 data[ i] = cos_a * lines[i] + sin_a * lines[4+i] | 126 data[ i] = cos_a * lines[i] + sin_a * lines[4+i] |
127 data[8+i] = -sin_a * lines[i] + cos_a * lines[4+i] | 127 data[8+i] = -sin_a * lines[i] + cos_a * lines[4+i] |
128 | 128 |
129 | 129 |
130 cdef void rotate_z(Matrix *mat, float angle) nogil: | 130 cdef void rotate_z(Matrix *mat, float angle) nogil: |
132 cdef float lines[8] | 132 cdef float lines[8] |
133 | 133 |
134 data = <float*>mat | 134 data = <float*>mat |
135 cos_a = cos(angle) | 135 cos_a = cos(angle) |
136 sin_a = sin(angle) | 136 sin_a = sin(angle) |
137 for i in xrange(8): | 137 for i in range(8): |
138 lines[i] = data[i] | 138 lines[i] = data[i] |
139 for i in xrange(4): | 139 for i in range(4): |
140 data[ i] = cos_a * lines[i] - sin_a * lines[4+i] | 140 data[ i] = cos_a * lines[i] - sin_a * lines[4+i] |
141 data[4+i] = sin_a * lines[i] + cos_a * lines[4+i] | 141 data[4+i] = sin_a * lines[i] + cos_a * lines[4+i] |