• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 template <typename T, int Rows, int Cols>
37 Matrix<T, Cols, Rows> transpose(const Matrix<T, Rows, Cols> &mat);
38 
39 // 2D affine transformations.
40 Matrix<float, 2, 2> rotationMatrix(float radians);
41 Matrix<float, 2, 2> shearMatrix(const Vector<float, 2> &shear);
42 
43 // 3D axis rotations.
44 Matrix<float, 3, 3> rotationMatrixX(float radiansX);
45 Matrix<float, 3, 3> rotationMatrixY(float radiansY);
46 Matrix<float, 3, 3> rotationMatrixZ(float radiansZ);
47 
48 template <class T, int Rows, int Cols>
49 Matrix<T, Rows, Cols> ortho2DMatrix(T l, T r, T b, T t, T n, T f);
50 
51 // Implementations.
52 
53 // Builds a translation matrix for a homogenous coordinate system
54 template <typename T, int Len>
translationMatrix(const Vector<T,Len> & translation)55 inline Matrix<T, Len + 1, Len + 1> translationMatrix(const Vector<T, Len> &translation)
56 {
57     Matrix<T, Len + 1, Len + 1> res = Matrix<T, Len + 1, Len + 1>();
58     for (int row = 0; row < Len; row++)
59         res(row, Len) = translation.m_data[row];
60     return res;
61 }
62 
63 template <typename T, int Rows, int Cols>
transpose(const Matrix<T,Rows,Cols> & mat)64 inline Matrix<T, Cols, Rows> transpose(const Matrix<T, Rows, Cols> &mat)
65 {
66     Matrix<T, Cols, Rows> res;
67     for (int row = 0; row < Rows; row++)
68         for (int col = 0; col < Cols; col++)
69             res(col, row) = mat(row, col);
70     return res;
71 }
72 
rotationMatrix(float radians)73 inline Matrix<float, 2, 2> rotationMatrix(float radians)
74 {
75     Matrix<float, 2, 2> mat;
76     float c = deFloatCos(radians);
77     float s = deFloatSin(radians);
78 
79     mat(0, 0) = c;
80     mat(0, 1) = -s;
81     mat(1, 0) = s;
82     mat(1, 1) = c;
83 
84     return mat;
85 }
86 
shearMatrix(const Vector<float,2> & shear)87 inline Matrix<float, 2, 2> shearMatrix(const Vector<float, 2> &shear)
88 {
89     Matrix<float, 2, 2> mat;
90     mat(0, 0) = 1.0f;
91     mat(0, 1) = shear.x();
92     mat(1, 0) = shear.y();
93     mat(1, 1) = 1.0f + shear.x() * shear.y();
94     return mat;
95 }
96 
rotationMatrixX(float radiansX)97 inline Matrix<float, 3, 3> rotationMatrixX(float radiansX)
98 {
99     Matrix<float, 3, 3> mat(1.0f);
100     float c = deFloatCos(radiansX);
101     float s = deFloatSin(radiansX);
102 
103     mat(1, 1) = c;
104     mat(1, 2) = -s;
105     mat(2, 1) = s;
106     mat(2, 2) = c;
107 
108     return mat;
109 }
110 
rotationMatrixY(float radiansY)111 inline Matrix<float, 3, 3> rotationMatrixY(float radiansY)
112 {
113     Matrix<float, 3, 3> mat(1.0f);
114     float c = deFloatCos(radiansY);
115     float s = deFloatSin(radiansY);
116 
117     mat(0, 0) = c;
118     mat(0, 2) = s;
119     mat(2, 0) = -s;
120     mat(2, 2) = c;
121 
122     return mat;
123 }
124 
rotationMatrixZ(float radiansZ)125 inline Matrix<float, 3, 3> rotationMatrixZ(float radiansZ)
126 {
127     Matrix<float, 3, 3> mat(1.0f);
128     float c = deFloatCos(radiansZ);
129     float s = deFloatSin(radiansZ);
130 
131     mat(0, 0) = c;
132     mat(0, 1) = -s;
133     mat(1, 0) = s;
134     mat(1, 1) = c;
135 
136     return mat;
137 }
138 
139 template <class T, int Rows, int Cols>
ortho2DMatrix(T l,T r,T b,T t,T n,T f)140 Matrix<T, Rows, Cols> ortho2DMatrix(T l, T r, T b, T t, T n, T f)
141 {
142     Matrix<T, Rows, Cols> res;
143 
144     if ((r - l) == 0.f || (t - b) == 0.f || (f - n) == 0.f)
145         return res;
146 
147     T inv_width  = 1.0f / (r - l);
148     T inv_height = 1.0f / (t - b);
149     T inv_depth  = 1.0f / (f - n);
150 
151     res(0, 0) = 2.0f * inv_width;
152     res(1, 1) = 2.0f * inv_height;
153     res(2, 2) = 2.0f * inv_depth;
154 
155     res(3, 0) = -(r + l) * inv_width;
156     res(3, 1) = -(t + b) * inv_height;
157     res(3, 2) = -(f + n) * inv_depth;
158     res(3, 3) = 1.0f;
159 
160     return res;
161 }
162 
163 } // namespace tcu
164 
165 #endif // _TCUMATRIXUTIL_HPP
166