Merge "Generate flat triangles in polygon offset tests." into nougat-cts-dev am:...
[platform/upstream/VK-GL-CTS.git] / framework / common / tcuMatrixUtil.hpp
1 #ifndef _TCUMATRIXUTIL_HPP
2 #define _TCUMATRIXUTIL_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program Tester Core
5  * ----------------------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Matrix utility functions
24  *//*--------------------------------------------------------------------*/
25
26 #include "tcuDefs.hpp"
27 #include "tcuMatrix.hpp"
28 #include "deMath.h"
29
30 namespace tcu
31 {
32
33 template <typename T, int Size>
34 Matrix<T, Size+1, Size+1>               translationMatrix               (const Vector<T, Size>& translation);
35
36 // 2D affine transformations.
37 Matrix<float, 2, 2>                             rotationMatrix                  (float radians);
38 Matrix<float, 2, 2>                             shearMatrix                             (const Vector<float, 2>& shear);
39
40 // 3D axis rotations.
41 Matrix<float, 3, 3>                             rotationMatrixX                 (float radiansX);
42 Matrix<float, 3, 3>                             rotationMatrixY                 (float radiansY);
43 Matrix<float, 3, 3>                             rotationMatrixZ                 (float radiansZ);
44
45 // Implementations.
46
47 // Builds a translation matrix for a homogenous coordinate system
48 template <typename T, int Len>
49 inline Matrix<T, Len+1, Len+1> translationMatrix (const Vector<T, Len>& translation)
50 {
51         Matrix<T, Len+1, Len+1> res =  Matrix<T, Len+1, Len+1>();
52         for (int row = 0; row < Len; row++)
53                 res(row, Len) = translation.m_data[row];
54         return res;
55 }
56
57 inline Matrix<float, 2, 2> rotationMatrix (float radians)
58 {
59         Matrix<float, 2, 2> mat;
60         float                           c       = deFloatCos(radians);
61         float                           s       = deFloatSin(radians);
62
63         mat(0, 0) = c;
64         mat(0, 1) = -s;
65         mat(1, 0) = s;
66         mat(1, 1) = c;
67
68         return mat;
69 }
70
71 inline Matrix<float, 2, 2> shearMatrix (const Vector<float, 2>& shear)
72 {
73         Matrix<float, 2, 2> mat;
74         mat(0, 0) = 1.0f;
75         mat(0, 1) = shear.x();
76         mat(1, 0) = shear.y();
77         mat(1, 1) = 1.0f + shear.x()*shear.y();
78         return mat;
79 }
80
81 inline Matrix<float, 3, 3> rotationMatrixX (float radiansX)
82 {
83         Matrix<float, 3, 3> mat(1.0f);
84         float                           c       = deFloatCos(radiansX);
85         float                           s       = deFloatSin(radiansX);
86
87         mat(1, 1) = c;
88         mat(1, 2) = -s;
89         mat(2, 1) = s;
90         mat(2, 2) = c;
91
92         return mat;
93 }
94
95 inline Matrix<float, 3, 3> rotationMatrixY (float radiansY)
96 {
97         Matrix<float, 3, 3> mat(1.0f);
98         float                           c       = deFloatCos(radiansY);
99         float                           s       = deFloatSin(radiansY);
100
101         mat(0, 0) = c;
102         mat(0, 2) = s;
103         mat(2, 0) = -s;
104         mat(2, 2) = c;
105
106         return mat;
107 }
108
109 inline Matrix<float, 3, 3> rotationMatrixZ (float radiansZ)
110 {
111         Matrix<float, 3, 3> mat(1.0f);
112         float                           c       = deFloatCos(radiansZ);
113         float                           s       = deFloatSin(radiansZ);
114
115         mat(0, 0) = c;
116         mat(0, 1) = -s;
117         mat(1, 0) = s;
118         mat(1, 1) = c;
119
120         return mat;
121 }
122
123 } // tcu
124
125 #endif // _TCUMATRIXUTIL_HPP