Mercurial > hg > dmlib
annotate src/dmvecmat.c @ 2261:e286454d305f
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 17 Jun 2019 00:55:07 +0300 |
parents | 696c58784635 |
children | 69a5af2eb1ea |
rev | line source |
---|---|
0 | 1 /* |
2 * DMLib | |
3 * -- Vector and matrix functions | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
863 | 5 * (C) Copyright 2011-2015 Tecnic Software productions (TNSP) |
0 | 6 */ |
7 #include "dmvecmat.h" | |
8 | |
9 | |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
10 void dm_vector_add_n(DMVector *dst, const DMVector *src, const int nlist) |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
11 { |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
12 int i; |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
13 for (i = 0; i < nlist; i++) |
283
4d42b8910d7e
Using SSE inline asm is not worth it in single operations, it hinders
Matti Hamalainen <ccr@tnsp.org>
parents:
281
diff
changeset
|
14 { |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
15 dm_vector_add(dst + i, src + i); |
283
4d42b8910d7e
Using SSE inline asm is not worth it in single operations, it hinders
Matti Hamalainen <ccr@tnsp.org>
parents:
281
diff
changeset
|
16 } |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
17 } |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
18 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
19 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
20 void dm_vector_add_r_n(DMVector *dst, const DMVector *src1, const DMVector *src2, const int nlist) |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
21 { |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
22 int i; |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
23 for (i = 0; i < nlist; i++) |
283
4d42b8910d7e
Using SSE inline asm is not worth it in single operations, it hinders
Matti Hamalainen <ccr@tnsp.org>
parents:
281
diff
changeset
|
24 { |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
25 dm_vector_add_r(dst + i, src1 + i, src2 + i); |
283
4d42b8910d7e
Using SSE inline asm is not worth it in single operations, it hinders
Matti Hamalainen <ccr@tnsp.org>
parents:
281
diff
changeset
|
26 } |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
27 } |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
28 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
29 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
30 void dm_vector_sub_n(DMVector *dst, const DMVector *src, const int nlist) |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
31 { |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
32 int i; |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
33 for (i = 0; i < nlist; i++) |
283
4d42b8910d7e
Using SSE inline asm is not worth it in single operations, it hinders
Matti Hamalainen <ccr@tnsp.org>
parents:
281
diff
changeset
|
34 { |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
35 dm_vector_add(dst + i, src + i); |
283
4d42b8910d7e
Using SSE inline asm is not worth it in single operations, it hinders
Matti Hamalainen <ccr@tnsp.org>
parents:
281
diff
changeset
|
36 } |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
37 } |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
38 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
39 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
40 void dm_vector_sub_r_n(DMVector *dst, const DMVector *src1, const DMVector *src2, const int nlist) |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
41 { |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
42 int i; |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
43 for (i = 0; i < nlist; i++) |
283
4d42b8910d7e
Using SSE inline asm is not worth it in single operations, it hinders
Matti Hamalainen <ccr@tnsp.org>
parents:
281
diff
changeset
|
44 { |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
45 dm_vector_sub_r(dst + i, src1 + i, src2 + i); |
283
4d42b8910d7e
Using SSE inline asm is not worth it in single operations, it hinders
Matti Hamalainen <ccr@tnsp.org>
parents:
281
diff
changeset
|
46 } |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
47 } |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
48 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
49 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
50 /* Multiply given vector with a matrix |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
51 */ |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
52 void dm_vector_mul_by_mat(DMVector *vd, const DMVector *vs, const DMMatrix *mat) |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
53 { |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
54 vd->x = (vs->x * mat->m[0][0]) + (vs->y * mat->m[1][0]) + (vs->z * mat->m[2][0]); |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
55 vd->y = (vs->x * mat->m[0][1]) + (vs->y * mat->m[1][1]) + (vs->z * mat->m[2][1]); |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
56 vd->z = (vs->x * mat->m[0][2]) + (vs->y * mat->m[1][2]) + (vs->z * mat->m[2][2]); |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
57 } |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
58 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
59 |
0 | 60 /* Multiply list of given vectors with given matrix. |
61 */ | |
62 void dm_vector_mul_by_mat_n(DMVector *list, const int nlist, const DMMatrix *mat) | |
63 { | |
64 int i; | |
65 | |
66 for (i = 0; i < nlist; i++) | |
67 { | |
68 DMVector q; | |
69 memcpy(&q, &list[i], sizeof(DMVector)); | |
70 | |
71 list[i].x = (q.x * mat->m[0][0]) + (q.y * mat->m[1][0]) + (q.z * mat->m[2][0]); | |
72 list[i].y = (q.x * mat->m[0][1]) + (q.y * mat->m[1][1]) + (q.z * mat->m[2][1]); | |
73 list[i].z = (q.x * mat->m[0][2]) + (q.y * mat->m[1][2]) + (q.z * mat->m[2][2]); | |
74 } | |
75 } | |
76 | |
77 | |
78 /* Set matrix to unit-matrix | |
79 */ | |
80 void dm_matrix_unit(DMMatrix *mat) | |
81 { | |
1167 | 82 dmMemset(mat, 0, sizeof(DMMatrix)); |
0 | 83 mat->m[0][0] = 1.0f; |
84 mat->m[1][1] = 1.0f; | |
85 mat->m[2][2] = 1.0f; | |
279
5acc1232c8c0
Set the last 1 of the unit matrix as they are now 4x4 instead of 3x3.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
86 mat->m[3][3] = 1.0f; |
0 | 87 } |
88 | |
89 | |
90 /* Transpose the matrix mat2 to mat1 | |
91 */ | |
92 void dm_matrix_transpose(DMMatrix *mat1, const DMMatrix *mat2) | |
93 { | |
94 int i, j; | |
95 | |
96 for (i = 0; i < DM_MATRIX_SIZE; i++) | |
97 for (j = 0; j < DM_MATRIX_SIZE; j++) | |
98 mat1->m[i][j] = mat2->m[j][i]; | |
99 } | |
100 | |
101 | |
102 /* Multiply matrices mat1 and mat2, putting result into mat1 | |
103 */ | |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
104 void dm_matrix_mul_r(DMMatrix *dst, const DMMatrix *mat1, const DMMatrix *mat2) |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
105 { |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
106 int i, j; |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
107 for (i = 0; i < DM_MATRIX_SIZE; i++) |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
108 for (j = 0; j < DM_MATRIX_SIZE; j++) |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
109 dst->m[i][j] = |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
110 (mat1->m[i][0] * mat2->m[0][j]) + |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
111 (mat1->m[i][1] * mat2->m[1][j]) + |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
112 (mat1->m[i][2] * mat2->m[2][j]); |
270
89a05a5e7a82
Add (untested) SSE asm version of matrix product (matrix x matrix multiplication).
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
113 } |
89a05a5e7a82
Add (untested) SSE asm version of matrix product (matrix x matrix multiplication).
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
114 |
89a05a5e7a82
Add (untested) SSE asm version of matrix product (matrix x matrix multiplication).
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
115 |
89a05a5e7a82
Add (untested) SSE asm version of matrix product (matrix x matrix multiplication).
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
116 void dm_matrix_mul(DMMatrix *mat1, const DMMatrix *mat2) |
89a05a5e7a82
Add (untested) SSE asm version of matrix product (matrix x matrix multiplication).
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
117 { |
89a05a5e7a82
Add (untested) SSE asm version of matrix product (matrix x matrix multiplication).
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
118 DMMatrix tmpM; |
89a05a5e7a82
Add (untested) SSE asm version of matrix product (matrix x matrix multiplication).
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
119 dm_matrix_mul_r(&tmpM, mat1, mat2); |
89a05a5e7a82
Add (untested) SSE asm version of matrix product (matrix x matrix multiplication).
Matti Hamalainen <ccr@tnsp.org>
parents:
269
diff
changeset
|
120 memcpy(mat1, &tmpM, sizeof(DMMatrix)); |
269
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
121 } |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
122 |
159264c27929
Add some new vector and matrix operations, and introduce some SSE inline
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
123 |
0 | 124 /* Multiply given list of matrices (size of nMatrices units) with given matrix. |
125 */ | |
126 void dm_matrix_mul_n(DMMatrix * list, const int nlist, const DMMatrix *mat) | |
127 { | |
128 int i; | |
129 for (i = 0; i < nlist; i++) | |
130 dm_matrix_mul(&list[i], mat); | |
131 } | |
132 | |
133 | |
134 /* Optimized rotation matrix creation | |
135 */ | |
136 void dm_matrix_rot(DMMatrix *mat, | |
137 const DMFloat sx, const DMFloat sy, const DMFloat sz, | |
138 const DMFloat cx, const DMFloat cy, const DMFloat cz) | |
139 { | |
140 const DMFloat | |
141 q = cx * sz, | |
142 l = cx * cz, | |
143 i = sx * sz, | |
144 j = sx * cz; | |
145 | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
146 mat->m[0][3] = |
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
147 mat->m[1][3] = |
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
148 mat->m[2][3] = |
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
149 mat->m[3][0] = |
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
863
diff
changeset
|
150 mat->m[3][1] = |
839 | 151 mat->m[3][2] = 0; |
152 | |
153 mat->m[3][3] = 1.0f; | |
281
9ba4f25abbce
Fix rotation matrix creation.
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
154 |
0 | 155 mat->m[0][0] = cy * cz; |
156 mat->m[0][1] = cy * sz; | |
157 mat->m[0][2] = -sy; | |
158 | |
159 mat->m[1][0] = (sy * j) - q; | |
160 mat->m[1][1] = (sy * i) + l; | |
161 mat->m[1][2] = sx * cy; | |
162 | |
163 | |
164 mat->m[2][0] = (sy * l) + i; | |
165 mat->m[2][1] = (sy * q) - j; | |
166 mat->m[2][2] = cx * cy; | |
167 } |