1 /*
2  * Copyright © 2018 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef __IGT_MATRIX_H__
25 #define __IGT_MATRIX_H__
26 
27 /**
28  * igt_vec4:
29  * @d: vector elements
30  *
31  * A 4 element column vector (4x1 matrix).
32  */
33 struct igt_vec4 {
34 	float d[4];
35 };
36 
37 /**
38  * igt_mat4:
39  * @d: matrix elements
40  *
41  * A 4x4 column major matrix.
42  */
43 struct igt_mat4 {
44 	float d[16];
45 };
46 
47 #define m(row, col) ((col) * 4 + (row))
48 
49 void igt_matrix_print(const struct igt_mat4 *m);
50 struct igt_mat4 igt_matrix_identity(void);
51 struct igt_mat4 igt_matrix_scale(float x, float y, float z);
52 struct igt_mat4 igt_matrix_translate(float x, float y, float z);
53 struct igt_mat4 igt_matrix_multiply(const struct igt_mat4 *a,
54 				    const struct igt_mat4 *b);
55 
56 /**
57  * igt_matrix_transform:
58  * @m: The matrix
59  * @v: The vector
60  *
61  * Transform the vector @v by the matrix @m. @m is on the left,
62  * @v on the right.
63  *
64  * Returns:
65  * The transformed vector.
66  */
67 static inline struct igt_vec4
igt_matrix_transform(const struct igt_mat4 * m,const struct igt_vec4 * v)68 igt_matrix_transform(const struct igt_mat4 *m,
69 		     const struct igt_vec4 *v)
70 {
71 	struct igt_vec4 ret = {
72 		.d = { m->d[m(0, 0)] * v->d[0] +
73 		       m->d[m(0, 1)] * v->d[1] +
74 		       m->d[m(0, 2)] * v->d[2] +
75 		       m->d[m(0, 3)] * v->d[3],
76 
77 		       m->d[m(1, 0)] * v->d[0] +
78 		       m->d[m(1, 1)] * v->d[1] +
79 		       m->d[m(1, 2)] * v->d[2] +
80 		       m->d[m(1, 3)] * v->d[3],
81 
82 		       m->d[m(2, 0)] * v->d[0] +
83 		       m->d[m(2, 1)] * v->d[1] +
84 		       m->d[m(2, 2)] * v->d[2] +
85 		       m->d[m(2, 3)] * v->d[3],
86 
87 		       m->d[m(3, 0)] * v->d[0] +
88 		       m->d[m(3, 1)] * v->d[1] +
89 		       m->d[m(3, 2)] * v->d[2] +
90 		       m->d[m(3, 3)] * v->d[3],
91 		},
92 	};
93 
94 	return ret;
95 }
96 
97 #endif /* __IGT_MATRIX_H__ */
98