• 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 // Implementations.
49 
50 // Builds a translation matrix for a homogenous coordinate system
51 template <typename T, int Len>
translationMatrix(const Vector<T,Len> & translation)52 inline Matrix<T, Len+1, Len+1> translationMatrix (const Vector<T, Len>& translation)
53 {
54 	Matrix<T, Len+1, Len+1> res =  Matrix<T, Len+1, Len+1>();
55 	for (int row = 0; row < Len; row++)
56 		res(row, Len) = translation.m_data[row];
57 	return res;
58 }
59 
60 template <typename T, int Rows, int Cols>
transpose(const Matrix<T,Rows,Cols> & mat)61 inline Matrix<T, Cols, Rows> transpose (const Matrix<T, Rows, Cols>& mat)
62 {
63 	Matrix<T, Cols, Rows> res;
64 	for (int row = 0; row < Rows; row++)
65 		for (int col = 0; col < Cols; col++)
66 			res(col, row) = mat(row, col);
67 	return res;
68 }
69 
rotationMatrix(float radians)70 inline Matrix<float, 2, 2> rotationMatrix (float radians)
71 {
72 	Matrix<float, 2, 2> mat;
73 	float				c	= deFloatCos(radians);
74 	float				s	= deFloatSin(radians);
75 
76 	mat(0, 0) = c;
77 	mat(0, 1) = -s;
78 	mat(1, 0) = s;
79 	mat(1, 1) = c;
80 
81 	return mat;
82 }
83 
shearMatrix(const Vector<float,2> & shear)84 inline Matrix<float, 2, 2> shearMatrix (const Vector<float, 2>& shear)
85 {
86 	Matrix<float, 2, 2> mat;
87 	mat(0, 0) = 1.0f;
88 	mat(0, 1) = shear.x();
89 	mat(1, 0) = shear.y();
90 	mat(1, 1) = 1.0f + shear.x()*shear.y();
91 	return mat;
92 }
93 
rotationMatrixX(float radiansX)94 inline Matrix<float, 3, 3> rotationMatrixX (float radiansX)
95 {
96 	Matrix<float, 3, 3> mat(1.0f);
97 	float				c	= deFloatCos(radiansX);
98 	float				s	= deFloatSin(radiansX);
99 
100 	mat(1, 1) = c;
101 	mat(1, 2) = -s;
102 	mat(2, 1) = s;
103 	mat(2, 2) = c;
104 
105 	return mat;
106 }
107 
rotationMatrixY(float radiansY)108 inline Matrix<float, 3, 3> rotationMatrixY (float radiansY)
109 {
110 	Matrix<float, 3, 3> mat(1.0f);
111 	float				c	= deFloatCos(radiansY);
112 	float				s	= deFloatSin(radiansY);
113 
114 	mat(0, 0) = c;
115 	mat(0, 2) = s;
116 	mat(2, 0) = -s;
117 	mat(2, 2) = c;
118 
119 	return mat;
120 }
121 
rotationMatrixZ(float radiansZ)122 inline Matrix<float, 3, 3> rotationMatrixZ (float radiansZ)
123 {
124 	Matrix<float, 3, 3> mat(1.0f);
125 	float				c	= deFloatCos(radiansZ);
126 	float				s	= deFloatSin(radiansZ);
127 
128 	mat(0, 0) = c;
129 	mat(0, 1) = -s;
130 	mat(1, 0) = s;
131 	mat(1, 1) = c;
132 
133 	return mat;
134 }
135 
136 } // tcu
137 
138 #endif // _TCUMATRIXUTIL_HPP
139