• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef Matrix_hpp
16 #define Matrix_hpp
17 
18 namespace sw
19 {
20 	struct Vector;
21 	struct Point;
22 	struct float4;
23 
24 	struct Matrix
25 	{
26 		Matrix();
27 		Matrix(const int i);
28 		Matrix(const float m[16]);
29 		Matrix(const float m[4][4]);
30 		Matrix(float m11, float m12, float m13,
31 		       float m21, float m22, float m23,
32 		       float m31, float m32, float m33);
33 		Matrix(float m11, float m12, float m13, float m14,
34 		       float m21, float m22, float m23, float m24,
35 		       float m31, float m32, float m33, float m34,
36 		       float m41, float m42, float m43, float m44);
37 		Matrix(const Vector &v1, const Vector &v2, const Vector &v3);   // Column vectors
38 
39 		Matrix &operator=(const Matrix &N);
40 
41 		// Row major order
42 		float m[4][4];
43 
44 		static Matrix diag(float m11, float m22, float m33, float m44);
45 
46 		operator float*();
47 
48 		Matrix operator+() const;
49 		Matrix operator-() const;
50 
51 		Matrix operator!() const;   // Inverse
52 		Matrix operator~() const;   // Transpose
53 
54 		Matrix &operator+=(const Matrix &N);
55 		Matrix &operator-=(const Matrix &N);
56 		Matrix &operator*=(float s);
57 		Matrix &operator*=(const Matrix &N);
58 		Matrix &operator/=(float s);
59 
60 		float *operator[](int i);   // Access element [row][col], starting with [0][0]
61 		const float *operator[](int i) const;
62 
63 		float &operator()(int i, int j);   // Access element (row, col), starting with (1, 1)
64 		const float &operator()(int i, int j) const;
65 
66 		friend bool operator==(const Matrix &M, const Matrix &N);
67 		friend bool operator!=(const Matrix &M, const Matrix &N);
68 
69 		friend Matrix operator+(const Matrix &M, const Matrix &N);
70 		friend Matrix operator-(const Matrix &M, const Matrix &N);
71 		friend Matrix operator*(float s, const Matrix &M);
72 		friend Matrix operator*(const Matrix &M, const Matrix &N);
73 		friend Matrix operator/(const Matrix &M, float s);
74 
75 		float4 operator*(const float4 &v) const;
76 
77 		static float det(const Matrix &M);
78 		static float det(float m11);
79 		static float det(float m11, float m12,
80 		                 float m21, float m22);
81 		static float det(float m11, float m12, float m13,
82 		                 float m21, float m22, float m23,
83 		                 float m31, float m32, float m33);
84 		static float det(float m11, float m12, float m13, float m14,
85 		                 float m21, float m22, float m23, float m24,
86 		                 float m31, float m32, float m33, float m34,
87 		                 float m41, float m42, float m43, float m44);
88 		static float det(const Vector &v1, const Vector &v2, const Vector &v3);
89 		static float det3(const Matrix &M);
90 
91 		static float tr(const Matrix &M);
92 
93 		Matrix &orthogonalise();   // Gram-Schmidt orthogonalisation of 3x3 submatrix
94 
95 		static Matrix eulerRotate(const Vector &v);
96 		static Matrix eulerRotate(float x, float y, float z);
97 
98 		static Matrix translate(const Vector &v);
99 		static Matrix translate(float x, float y, float z);
100 
101 		static Matrix scale(const Vector &v);
102 		static Matrix scale(float x, float y, float z);
103 
104 		static Matrix lookAt(const Vector &v);
105 		static Matrix lookAt(float x, float y, float z);
106 	};
107 }
108 
109 #include "Vector.hpp"
110 
111 namespace sw
112 {
Matrix()113 	inline Matrix::Matrix()
114 	{
115 	}
116 
Matrix(const int i)117 	inline Matrix::Matrix(const int i)
118 	{
119 		const float s = (float)i;
120 
121 		Matrix &M = *this;
122 
123 		M(1, 1) = s; M(1, 2) = 0; M(1, 3) = 0; M(1, 4) = 0;
124 		M(2, 1) = 0; M(2, 2) = s; M(2, 3) = 0; M(2, 4) = 0;
125 		M(3, 1) = 0; M(3, 2) = 0; M(3, 3) = s; M(3, 4) = 0;
126 		M(4, 1) = 0; M(4, 2) = 0; M(4, 3) = 0; M(4, 4) = s;
127 	}
128 
Matrix(const float m[16])129 	inline Matrix::Matrix(const float m[16])
130 	{
131 		Matrix &M = *this;
132 
133 		M(1, 1) = m[0];  M(1, 2) = m[1];  M(1, 3) = m[2];  M(1, 4) = m[3];
134 		M(2, 1) = m[4];  M(2, 2) = m[5];  M(2, 3) = m[6];  M(2, 4) = m[7];
135 		M(3, 1) = m[8];  M(3, 2) = m[8];  M(3, 3) = m[10]; M(3, 4) = m[11];
136 		M(4, 1) = m[12]; M(4, 2) = m[13]; M(4, 3) = m[14]; M(4, 4) = m[15];
137 	}
138 
Matrix(const float m[4][4])139 	inline Matrix::Matrix(const float m[4][4])
140 	{
141 		Matrix &M = *this;
142 
143 		M[0][0] = m[0][0];  M[0][1] = m[0][1];  M[0][2] = m[0][2];  M[0][3] = m[0][3];
144 		M[1][0] = m[1][0];  M[1][1] = m[1][1];  M[1][2] = m[1][2];  M[1][3] = m[1][3];
145 		M[2][0] = m[2][0];  M[2][1] = m[2][1];  M[2][2] = m[2][2];  M[2][3] = m[2][3];
146 		M[3][0] = m[3][0];  M[3][1] = m[3][1];  M[3][2] = m[3][2];  M[3][3] = m[3][3];
147 	}
148 
Matrix(float m11,float m12,float m13,float m21,float m22,float m23,float m31,float m32,float m33)149 	inline Matrix::Matrix(float m11, float m12, float m13,
150 	                      float m21, float m22, float m23,
151 	                      float m31, float m32, float m33)
152 	{
153 		Matrix &M = *this;
154 
155 		M(1, 1) = m11; M(1, 2) = m12; M(1, 3) = m13; M(1, 4) = 0;
156 		M(2, 1) = m21; M(2, 2) = m22; M(2, 3) = m23; M(2, 4) = 0;
157 		M(3, 1) = m31; M(3, 2) = m32; M(3, 3) = m33; M(3, 4) = 0;
158 		M(4, 1) = 0;   M(4, 2) = 0;   M(4, 3) = 0;   M(4, 4) = 1;
159 	}
160 
Matrix(float m11,float m12,float m13,float m14,float m21,float m22,float m23,float m24,float m31,float m32,float m33,float m34,float m41,float m42,float m43,float m44)161 	inline Matrix::Matrix(float m11, float m12, float m13, float m14,
162 	                      float m21, float m22, float m23, float m24,
163 	                      float m31, float m32, float m33, float m34,
164 	                      float m41, float m42, float m43, float m44)
165 	{
166 		Matrix &M = *this;
167 
168 		M(1, 1) = m11; M(1, 2) = m12; M(1, 3) = m13; M(1, 4) = m14;
169 		M(2, 1) = m21; M(2, 2) = m22; M(2, 3) = m23; M(2, 4) = m24;
170 		M(3, 1) = m31; M(3, 2) = m32; M(3, 3) = m33; M(3, 4) = m34;
171 		M(4, 1) = m41; M(4, 2) = m42; M(4, 3) = m43; M(4, 4) = m44;
172 	}
173 
Matrix(const Vector & v1,const Vector & v2,const Vector & v3)174 	inline Matrix::Matrix(const Vector &v1, const Vector &v2, const Vector &v3)
175 	{
176 		Matrix &M = *this;
177 
178 		M(1, 1) = v1.x; M(1, 2) = v2.x; M(1, 3) = v3.x; M(1, 4) = 0;
179 		M(2, 1) = v1.y; M(2, 2) = v2.y; M(2, 3) = v3.y; M(2, 4) = 0;
180 		M(3, 1) = v1.z; M(3, 2) = v2.z; M(3, 3) = v3.z; M(3, 4) = 0;
181 		M(4, 1) = 0;    M(4, 2) = 0;    M(4, 3) = 0;    M(4, 4) = 1;
182 	}
183 
operator =(const Matrix & N)184 	inline Matrix &Matrix::operator=(const Matrix &N)
185 	{
186 		Matrix &M = *this;
187 
188 		M(1, 1) = N(1, 1); M(1, 2) = N(1, 2); M(1, 3) = N(1, 3); M(1, 4) = N(1, 4);
189 		M(2, 1) = N(2, 1); M(2, 2) = N(2, 2); M(2, 3) = N(2, 3); M(2, 4) = N(2, 4);
190 		M(3, 1) = N(3, 1); M(3, 2) = N(3, 2); M(3, 3) = N(3, 3); M(3, 4) = N(3, 4);
191 		M(4, 1) = N(4, 1); M(4, 2) = N(4, 2); M(4, 3) = N(4, 3); M(4, 4) = N(4, 4);
192 
193 		return M;
194 	}
195 
operator [](int i)196 	inline float *Matrix::operator[](int i)
197 	{
198 		return m[i];
199 	}
200 
operator [](int i) const201 	inline const float *Matrix::operator[](int i) const
202 	{
203 		return m[i];
204 	}
205 
operator ()(int i,int j)206 	inline float &Matrix::operator()(int i, int j)
207 	{
208 		return m[i - 1][j - 1];
209 	}
210 
operator ()(int i,int j) const211 	inline const float &Matrix::operator()(int i, int j) const
212 	{
213 		return m[i - 1][j - 1];
214 	}
215 }
216 
217 #endif   // Matrix_hpp
218