• 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 		// Row major order
40 		float m[4][4];
41 
42 		static Matrix diag(float m11, float m22, float m33, float m44);
43 
44 		operator float*();
45 
46 		Matrix operator+() const;
47 		Matrix operator-() const;
48 
49 		Matrix operator!() const;   // Inverse
50 		Matrix operator~() const;   // Transpose
51 
52 		Matrix &operator+=(const Matrix &N);
53 		Matrix &operator-=(const Matrix &N);
54 		Matrix &operator*=(float s);
55 		Matrix &operator*=(const Matrix &N);
56 		Matrix &operator/=(float s);
57 
58 		float *operator[](int i);   // Access element [row][col], starting with [0][0]
59 		const float *operator[](int i) const;
60 
61 		float &operator()(int i, int j);   // Access element (row, col), starting with (1, 1)
62 		const float &operator()(int i, int j) const;
63 
64 		friend bool operator==(const Matrix &M, const Matrix &N);
65 		friend bool operator!=(const Matrix &M, const Matrix &N);
66 
67 		friend Matrix operator+(const Matrix &M, const Matrix &N);
68 		friend Matrix operator-(const Matrix &M, const Matrix &N);
69 		friend Matrix operator*(float s, const Matrix &M);
70 		friend Matrix operator*(const Matrix &M, const Matrix &N);
71 		friend Matrix operator/(const Matrix &M, float s);
72 
73 		float4 operator*(const float4 &v) const;
74 
75 		static float det(const Matrix &M);
76 		static float det(float m11);
77 		static float det(float m11, float m12,
78 		                 float m21, float m22);
79 		static float det(float m11, float m12, float m13,
80 		                 float m21, float m22, float m23,
81 		                 float m31, float m32, float m33);
82 		static float det(float m11, float m12, float m13, float m14,
83 		                 float m21, float m22, float m23, float m24,
84 		                 float m31, float m32, float m33, float m34,
85 		                 float m41, float m42, float m43, float m44);
86 		static float det(const Vector &v1, const Vector &v2, const Vector &v3);
87 		static float det3(const Matrix &M);
88 
89 		static float tr(const Matrix &M);
90 
91 		Matrix &orthogonalise();   // Gram-Schmidt orthogonalisation of 3x3 submatrix
92 
93 		static Matrix eulerRotate(const Vector &v);
94 		static Matrix eulerRotate(float x, float y, float z);
95 
96 		static Matrix translate(const Vector &v);
97 		static Matrix translate(float x, float y, float z);
98 
99 		static Matrix scale(const Vector &v);
100 		static Matrix scale(float x, float y, float z);
101 
102 		static Matrix lookAt(const Vector &v);
103 		static Matrix lookAt(float x, float y, float z);
104 	};
105 }
106 
107 #include "Vector.hpp"
108 
109 namespace sw
110 {
Matrix()111 	inline Matrix::Matrix()
112 	{
113 	}
114 
Matrix(const int i)115 	inline Matrix::Matrix(const int i)
116 	{
117 		const float s = (float)i;
118 
119 		Matrix &M = *this;
120 
121 		M(1, 1) = s; M(1, 2) = 0; M(1, 3) = 0; M(1, 4) = 0;
122 		M(2, 1) = 0; M(2, 2) = s; M(2, 3) = 0; M(2, 4) = 0;
123 		M(3, 1) = 0; M(3, 2) = 0; M(3, 3) = s; M(3, 4) = 0;
124 		M(4, 1) = 0; M(4, 2) = 0; M(4, 3) = 0; M(4, 4) = s;
125 	}
126 
Matrix(const float m[16])127 	inline Matrix::Matrix(const float m[16])
128 	{
129 		Matrix &M = *this;
130 
131 		M(1, 1) = m[0];  M(1, 2) = m[1];  M(1, 3) = m[2];  M(1, 4) = m[3];
132 		M(2, 1) = m[4];  M(2, 2) = m[5];  M(2, 3) = m[6];  M(2, 4) = m[7];
133 		M(3, 1) = m[8];  M(3, 2) = m[8];  M(3, 3) = m[10]; M(3, 4) = m[11];
134 		M(4, 1) = m[12]; M(4, 2) = m[13]; M(4, 3) = m[14]; M(4, 4) = m[15];
135 	}
136 
Matrix(const float m[4][4])137 	inline Matrix::Matrix(const float m[4][4])
138 	{
139 		Matrix &M = *this;
140 
141 		M[0][0] = m[0][0];  M[0][1] = m[0][1];  M[0][2] = m[0][2];  M[0][3] = m[0][3];
142 		M[1][0] = m[1][0];  M[1][1] = m[1][1];  M[1][2] = m[1][2];  M[1][3] = m[1][3];
143 		M[2][0] = m[2][0];  M[2][1] = m[2][1];  M[2][2] = m[2][2];  M[2][3] = m[2][3];
144 		M[3][0] = m[3][0];  M[3][1] = m[3][1];  M[3][2] = m[3][2];  M[3][3] = m[3][3];
145 	}
146 
Matrix(float m11,float m12,float m13,float m21,float m22,float m23,float m31,float m32,float m33)147 	inline Matrix::Matrix(float m11, float m12, float m13,
148 	                      float m21, float m22, float m23,
149 	                      float m31, float m32, float m33)
150 	{
151 		Matrix &M = *this;
152 
153 		M(1, 1) = m11; M(1, 2) = m12; M(1, 3) = m13; M(1, 4) = 0;
154 		M(2, 1) = m21; M(2, 2) = m22; M(2, 3) = m23; M(2, 4) = 0;
155 		M(3, 1) = m31; M(3, 2) = m32; M(3, 3) = m33; M(3, 4) = 0;
156 		M(4, 1) = 0;   M(4, 2) = 0;   M(4, 3) = 0;   M(4, 4) = 1;
157 	}
158 
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)159 	inline Matrix::Matrix(float m11, float m12, float m13, float m14,
160 	                      float m21, float m22, float m23, float m24,
161 	                      float m31, float m32, float m33, float m34,
162 	                      float m41, float m42, float m43, float m44)
163 	{
164 		Matrix &M = *this;
165 
166 		M(1, 1) = m11; M(1, 2) = m12; M(1, 3) = m13; M(1, 4) = m14;
167 		M(2, 1) = m21; M(2, 2) = m22; M(2, 3) = m23; M(2, 4) = m24;
168 		M(3, 1) = m31; M(3, 2) = m32; M(3, 3) = m33; M(3, 4) = m34;
169 		M(4, 1) = m41; M(4, 2) = m42; M(4, 3) = m43; M(4, 4) = m44;
170 	}
171 
Matrix(const Vector & v1,const Vector & v2,const Vector & v3)172 	inline Matrix::Matrix(const Vector &v1, const Vector &v2, const Vector &v3)
173 	{
174 		Matrix &M = *this;
175 
176 		M(1, 1) = v1.x; M(1, 2) = v2.x; M(1, 3) = v3.x; M(1, 4) = 0;
177 		M(2, 1) = v1.y; M(2, 2) = v2.y; M(2, 3) = v3.y; M(2, 4) = 0;
178 		M(3, 1) = v1.z; M(3, 2) = v2.z; M(3, 3) = v3.z; M(3, 4) = 0;
179 		M(4, 1) = 0;    M(4, 2) = 0;    M(4, 3) = 0;    M(4, 4) = 1;
180 	}
181 
operator [](int i)182 	inline float *Matrix::operator[](int i)
183 	{
184 		return m[i];
185 	}
186 
operator [](int i) const187 	inline const float *Matrix::operator[](int i) const
188 	{
189 		return m[i];
190 	}
191 
operator ()(int i,int j)192 	inline float &Matrix::operator()(int i, int j)
193 	{
194 		return m[i - 1][j - 1];
195 	}
196 
operator ()(int i,int j) const197 	inline const float &Matrix::operator()(int i, int j) const
198 	{
199 		return m[i - 1][j - 1];
200 	}
201 }
202 
203 #endif   // Matrix_hpp
204