0
|
1 /*
|
|
2 * DMLib
|
|
3 * -- Vector and matrix functions
|
|
4 * Programmed and designed by Matti 'ccr' Hamalainen
|
|
5 * (C) Copyright 2011 Tecnic Software productions (TNSP)
|
|
6 */
|
|
7 #include "dmvecmat.h"
|
|
8
|
|
9
|
|
10 /* Multiply list of given vectors with given matrix.
|
|
11 */
|
|
12 void dm_vector_mul_by_mat_n(DMVector *list, const int nlist, const DMMatrix *mat)
|
|
13 {
|
|
14 int i;
|
|
15
|
|
16 for (i = 0; i < nlist; i++)
|
|
17 {
|
|
18 DMVector q;
|
|
19 memcpy(&q, &list[i], sizeof(DMVector));
|
|
20
|
|
21 list[i].x = (q.x * mat->m[0][0]) + (q.y * mat->m[1][0]) + (q.z * mat->m[2][0]);
|
|
22 list[i].y = (q.x * mat->m[0][1]) + (q.y * mat->m[1][1]) + (q.z * mat->m[2][1]);
|
|
23 list[i].z = (q.x * mat->m[0][2]) + (q.y * mat->m[1][2]) + (q.z * mat->m[2][2]);
|
|
24 }
|
|
25 }
|
|
26
|
|
27
|
|
28 /* Set matrix to unit-matrix
|
|
29 */
|
|
30 void dm_matrix_unit(DMMatrix *mat)
|
|
31 {
|
|
32 memset(mat, 0, sizeof(DMMatrix));
|
|
33 mat->m[0][0] = 1.0f;
|
|
34 mat->m[1][1] = 1.0f;
|
|
35 mat->m[2][2] = 1.0f;
|
|
36 }
|
|
37
|
|
38
|
|
39 /* Transpose the matrix mat2 to mat1
|
|
40 */
|
|
41 void dm_matrix_transpose(DMMatrix *mat1, const DMMatrix *mat2)
|
|
42 {
|
|
43 int i, j;
|
|
44
|
|
45 for (i = 0; i < DM_MATRIX_SIZE; i++)
|
|
46 for (j = 0; j < DM_MATRIX_SIZE; j++)
|
|
47 mat1->m[i][j] = mat2->m[j][i];
|
|
48 }
|
|
49
|
|
50
|
|
51 /* Multiply matrices mat1 and mat2, putting result into mat1
|
|
52 */
|
|
53 void dm_matrix_mul(DMMatrix *mat1, const DMMatrix *mat2)
|
|
54 {
|
|
55 int i, j;
|
|
56 DMMatrix tmpM;
|
|
57
|
|
58 for (i = 0; i < DM_MATRIX_SIZE; i++)
|
|
59 for (j = 0; j < DM_MATRIX_SIZE; j++)
|
|
60 tmpM.m[i][j] =
|
|
61 (mat1->m[i][0] * mat2->m[0][j]) +
|
|
62 (mat1->m[i][1] * mat2->m[1][j]) +
|
|
63 (mat1->m[i][2] * mat2->m[2][j]);
|
|
64
|
|
65 memcpy(mat1, &tmpM, sizeof(DMMatrix));
|
|
66 }
|
|
67
|
|
68
|
|
69 /* Multiply given list of matrices (size of nMatrices units) with given matrix.
|
|
70 */
|
|
71 void dm_matrix_mul_n(DMMatrix * list, const int nlist, const DMMatrix *mat)
|
|
72 {
|
|
73 int i;
|
|
74 for (i = 0; i < nlist; i++)
|
|
75 dm_matrix_mul(&list[i], mat);
|
|
76 }
|
|
77
|
|
78
|
|
79 /* Optimized rotation matrix creation
|
|
80 */
|
|
81 void dm_matrix_rot(DMMatrix *mat,
|
|
82 const DMFloat sx, const DMFloat sy, const DMFloat sz,
|
|
83 const DMFloat cx, const DMFloat cy, const DMFloat cz)
|
|
84 {
|
|
85 const DMFloat
|
|
86 q = cx * sz,
|
|
87 l = cx * cz,
|
|
88 i = sx * sz,
|
|
89 j = sx * cz;
|
|
90
|
|
91 mat->m[0][0] = cy * cz;
|
|
92 mat->m[0][1] = cy * sz;
|
|
93 mat->m[0][2] = -sy;
|
|
94
|
|
95
|
|
96 mat->m[1][0] = (sy * j) - q;
|
|
97 mat->m[1][1] = (sy * i) + l;
|
|
98 mat->m[1][2] = sx * cy;
|
|
99
|
|
100
|
|
101 mat->m[2][0] = (sy * l) + i;
|
|
102 mat->m[2][1] = (sy * q) - j;
|
|
103 mat->m[2][2] = cx * cy;
|
|
104 }
|