Lightmetrica  0.0.1.50dbee3 (yosakoi)
 All Classes Functions Variables Typedefs Enumerations Enumerator
math.matrix.h
1 /*
2  Lightmetrica : A research-oriented renderer
3 
4  Copyright (c) 2014 Hisanari Otsu
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 #ifndef LIB_LIGHTMETRICA_MATH_MATRIX_H
22 #define LIB_LIGHTMETRICA_MATH_MATRIX_H
23 
24 #include "math.vector.h"
25 
26 LM_NAMESPACE_BEGIN
27 LM_MATH_NAMESPACE_BEGIN
28 
29 template <typename T> struct TMat3;
30 template <typename T> struct TMat4;
31 
42 template <typename T>
43 struct TMat3
44 {
45 
46  TVec3<T> v[3];
47 
48  LM_FORCE_INLINE TMat3();
49  LM_FORCE_INLINE TMat3(const TMat3<T>& m);
50  LM_FORCE_INLINE TMat3(const TMat4<T>& m);
51  LM_FORCE_INLINE TMat3(const T& v);
52  LM_FORCE_INLINE TMat3(const TVec3<T>& v0, const TVec3<T>& v1, const TVec3<T>& v2);
53  LM_FORCE_INLINE TMat3(const T* v);
54  LM_FORCE_INLINE TMat3(
55  T v00, T v10, T v20,
56  T v01, T v11, T v21,
57  T v02, T v12, T v22);
58 
59  static LM_FORCE_INLINE TMat3<T> Zero();
60  static LM_FORCE_INLINE TMat3<T> Diag(T v);
61  static LM_FORCE_INLINE TMat3<T> Identity();
62 
63  LM_FORCE_INLINE TVec3<T>& operator[](int i);
64  LM_FORCE_INLINE const TVec3<T>& operator[](int i) const;
65 
66  LM_FORCE_INLINE TMat3<T>& operator*=(const TMat3<T>& m);
67  LM_FORCE_INLINE TMat3<T>& operator*=(const T& s);
68  LM_FORCE_INLINE TMat3<T>& operator/=(const T& s);
69 
70 };
71 
72 template <typename T> LM_FORCE_INLINE TMat3<T> operator*(const TMat3<T>& m, const T& s);
73 template <typename T> LM_FORCE_INLINE TMat3<T> operator*(const T& v, const TMat3<T>& m);
74 template <typename T> LM_FORCE_INLINE TVec3<T> operator*(const TMat3<T>& m, const TVec3<T>& v);
75 template <typename T> LM_FORCE_INLINE TMat3<T> operator*(const TMat3<T>& m1, const TMat3<T>& m2);
76 template <typename T> LM_FORCE_INLINE TMat3<T> operator/(const TMat3<T>& m, const T& s);
77 
78 template <typename T> LM_FORCE_INLINE TMat3<T> Transpose(const TMat3<T>& m);
79 template <typename T> LM_FORCE_INLINE TMat3<T> Inverse(const TMat3<T>& m);
80 
81 typedef TMat3<float> Mat3f;
82 typedef TMat3<double> Mat3d;
83 typedef TMat3<int> Mat3i;
84 
85 // --------------------------------------------------------------------------------
86 
98 template <typename T>
99 struct TMat4
100 {
101 
102  TVec4<T> v[4];
103 
104  LM_FORCE_INLINE TMat4();
105  LM_FORCE_INLINE TMat4(const TMat3<T>& m);
106  LM_FORCE_INLINE TMat4(const TMat4<T>& m);
107  LM_FORCE_INLINE TMat4(const T& v);
108  LM_FORCE_INLINE TMat4(const TVec4<T>& v0, const TVec4<T>& v1, const TVec4<T>& v2, const TVec4<T>& v3);
109  LM_FORCE_INLINE TMat4(const T* v);
110  LM_FORCE_INLINE TMat4(
111  T v00, T v10, T v20, T v30,
112  T v01, T v11, T v21, T v31,
113  T v02, T v12, T v22, T v32,
114  T v03, T v13, T v23, T v33);
115 
116  static LM_FORCE_INLINE TMat4<T> Zero();
117  static LM_FORCE_INLINE TMat4<T> Diag(T v);
118  static LM_FORCE_INLINE TMat4<T> Identity();
119 
120  LM_FORCE_INLINE TVec4<T>& operator[](int i);
121  LM_FORCE_INLINE const TVec4<T>& operator[](int i) const;
122 
123  LM_FORCE_INLINE TMat4<T>& operator*=(const TMat4<T>& m);
124  LM_FORCE_INLINE TMat4<T>& operator*=(const T& s);
125  LM_FORCE_INLINE TMat4<T>& operator/=(const T& s);
126 
127 };
128 
129 template <typename T> LM_FORCE_INLINE TMat4<T> operator*(const TMat4<T>& m, const T& s);
130 template <typename T> LM_FORCE_INLINE TMat4<T> operator*(const T& v, const TMat4<T>& m);
131 template <typename T> LM_FORCE_INLINE TVec4<T> operator*(const TMat4<T>& m, const TVec4<T>& v);
132 template <typename T> LM_FORCE_INLINE TMat4<T> operator*(const TMat4<T>& m1, const TMat4<T>& m2);
133 template <typename T> LM_FORCE_INLINE TMat4<T> operator/(const TMat4<T>& m, const T& s);
134 
135 template <typename T> LM_FORCE_INLINE TMat4<T> Transpose(const TMat4<T>& m);
136 template <typename T> LM_FORCE_INLINE TMat4<T> Inverse(const TMat4<T>& m);
137 
138 typedef TMat4<float> Mat4f;
139 typedef TMat4<double> Mat4d;
140 typedef TMat4<int> Mat4i;
141 
142 // --------------------------------------------------------------------------------
143 
144 #if LM_SSE2
145 
150 template <>
151 struct LM_ALIGN_16 TMat3<float>
152 {
153 
154  Vec3f v[3];
155 
156  LM_FORCE_INLINE TMat3();
157  LM_FORCE_INLINE TMat3(const Mat3f& m);
158  LM_FORCE_INLINE TMat3(const Mat4f& m);
159  LM_FORCE_INLINE TMat3(float v);
160  LM_FORCE_INLINE TMat3(const Vec3f& v0, const Vec3f& v1, const Vec3f& v2);
161  LM_FORCE_INLINE TMat3(const float* v);
162  LM_FORCE_INLINE TMat3(
163  float v00, float v10, float v20,
164  float v01, float v11, float v21,
165  float v02, float v12, float v22);
166 
167  static LM_FORCE_INLINE Mat3f Zero();
168  static LM_FORCE_INLINE Mat3f Diag(float v);
169  static LM_FORCE_INLINE Mat3f Identity();
170 
171  LM_FORCE_INLINE Vec3f& operator[](int i);
172  LM_FORCE_INLINE const Vec3f& operator[](int i) const;
173 
174  LM_FORCE_INLINE Mat3f& operator*=(const Mat3f& m);
175  LM_FORCE_INLINE Mat3f& operator*=(const float& s);
176  LM_FORCE_INLINE Mat3f& operator/=(const float& s);
177 
178 };
179 
180 template <> LM_FORCE_INLINE Mat3f operator*(const Mat3f& m, const float& s);
181 template <> LM_FORCE_INLINE Mat3f operator*(const float& s, const Mat3f& m);
182 template <> LM_FORCE_INLINE Vec3f operator*(const Mat3f& m, const Vec3f& v);
183 template <> LM_FORCE_INLINE Mat3f operator*(const Mat3f& m1, const Mat3f& m2);
184 template <> LM_FORCE_INLINE Mat3f operator/(const Mat3f& m, const float& s);
185 
186 template <> LM_FORCE_INLINE Mat3f Transpose(const Mat3f& m);
187 //template <> LM_FORCE_INLINE Mat4f Inverse(const Mat4f& m);
188 
189 // --------------------------------------------------------------------------------
190 
195 template <>
196 struct LM_ALIGN_16 TMat4<float>
197 {
198 
199  Vec4f v[4];
200 
201  LM_FORCE_INLINE TMat4();
202  LM_FORCE_INLINE TMat4(const Mat3f& m);
203  LM_FORCE_INLINE TMat4(const Mat4f& m);
204  LM_FORCE_INLINE TMat4(float v);
205  LM_FORCE_INLINE TMat4(const Vec4f& v0, const Vec4f& v1, const Vec4f& v2, const Vec4f& v3);
206  LM_FORCE_INLINE TMat4(const float* v);
207  LM_FORCE_INLINE TMat4(
208  float v00, float v10, float v20, float v30,
209  float v01, float v11, float v21, float v31,
210  float v02, float v12, float v22, float v32,
211  float v03, float v13, float v23, float v33);
212 
213  static LM_FORCE_INLINE Mat4f Zero();
214  static LM_FORCE_INLINE Mat4f Diag(float v);
215  static LM_FORCE_INLINE Mat4f Identity();
216 
217  LM_FORCE_INLINE Vec4f& operator[](int i);
218  LM_FORCE_INLINE const Vec4f& operator[](int i) const;
219 
220  LM_FORCE_INLINE Mat4f& operator*=(const Mat4f& m);
221  LM_FORCE_INLINE Mat4f& operator*=(const float& s);
222  LM_FORCE_INLINE Mat4f& operator/=(const float& s);
223 
224 };
225 
226 template <> LM_FORCE_INLINE Mat4f operator*(const Mat4f& m, const float& s);
227 template <> LM_FORCE_INLINE Mat4f operator*(const float& s, const Mat4f& m);
228 template <> LM_FORCE_INLINE Vec4f operator*(const Mat4f& m, const Vec4f& v);
229 template <> LM_FORCE_INLINE Mat4f operator*(const Mat4f& m1, const Mat4f& m2);
230 template <> LM_FORCE_INLINE Mat4f operator/(const Mat4f& m, const float& s);
231 
232 template <> LM_FORCE_INLINE Mat4f Transpose(const Mat4f& m);
233 template <> LM_FORCE_INLINE Mat4f Inverse(const Mat4f& m);
234 
235 #endif
236 
237 // --------------------------------------------------------------------------------
238 
239 #if LM_AVX
240 
245 template <>
246 struct LM_ALIGN_32 TMat3<double>
247 {
248 
249  Vec3d v[3];
250 
251  LM_FORCE_INLINE TMat3();
252  LM_FORCE_INLINE TMat3(const Mat3d& m);
253  LM_FORCE_INLINE TMat3(const Mat4d& m);
254  LM_FORCE_INLINE TMat3(double v);
255  LM_FORCE_INLINE TMat3(const Vec3d& v0, const Vec3d& v1, const Vec3d& v2);
256  LM_FORCE_INLINE TMat3(const double* v);
257  LM_FORCE_INLINE TMat3(
258  double v00, double v10, double v20,
259  double v01, double v11, double v21,
260  double v02, double v12, double v22);
261 
262  static LM_FORCE_INLINE Mat3d Zero();
263  static LM_FORCE_INLINE Mat3d Diag(double v);
264  static LM_FORCE_INLINE Mat3d Identity();
265 
266  LM_FORCE_INLINE Vec3d& operator[](int i);
267  LM_FORCE_INLINE const Vec3d& operator[](int i) const;
268 
269  LM_FORCE_INLINE Mat3d& operator*=(const Mat3d& m);
270  LM_FORCE_INLINE Mat3d& operator*=(const double& s);
271  LM_FORCE_INLINE Mat3d& operator/=(const double& s);
272 
273 };
274 
275 template <> LM_FORCE_INLINE Mat3d operator*(const Mat3d& m, const double& s);
276 template <> LM_FORCE_INLINE Mat3d operator*(const double& s, const Mat3d& m);
277 template <> LM_FORCE_INLINE Vec3d operator*(const Mat3d& m, const Vec3d& v);
278 template <> LM_FORCE_INLINE Mat3d operator*(const Mat3d& m1, const Mat3d& m2);
279 template <> LM_FORCE_INLINE Mat3d operator/(const Mat3d& m, const double& s);
280 
281 // --------------------------------------------------------------------------------
282 
287 template <>
288 struct LM_ALIGN_32 TMat4<double>
289 {
290 
291  Vec4d v[4];
292 
293  LM_FORCE_INLINE TMat4();
294  LM_FORCE_INLINE TMat4(const Mat3d& m);
295  LM_FORCE_INLINE TMat4(const Mat4d& m);
296  LM_FORCE_INLINE TMat4(double v);
297  LM_FORCE_INLINE TMat4(const Vec4d& v0, const Vec4d& v1, const Vec4d& v2, const Vec4d& v3);
298  LM_FORCE_INLINE TMat4(const double* v);
299  LM_FORCE_INLINE TMat4(
300  double v00, double v10, double v20, double v30,
301  double v01, double v11, double v21, double v31,
302  double v02, double v12, double v22, double v32,
303  double v03, double v13, double v23, double v33);
304 
305  static LM_FORCE_INLINE Mat4d Zero();
306  static LM_FORCE_INLINE Mat4d Diag(double v);
307  static LM_FORCE_INLINE Mat4d Identity();
308 
309  LM_FORCE_INLINE Vec4d& operator[](int i);
310  LM_FORCE_INLINE const Vec4d& operator[](int i) const;
311 
312  LM_FORCE_INLINE Mat4d& operator*=(const Mat4d& m);
313  LM_FORCE_INLINE Mat4d& operator*=(const double& s);
314  LM_FORCE_INLINE Mat4d& operator/=(const double& s);
315 
316 };
317 
318 template <> LM_FORCE_INLINE Mat4d operator*(const Mat4d& m, const double& s);
319 template <> LM_FORCE_INLINE Mat4d operator*(const double& s, const Mat4d& m);
320 template <> LM_FORCE_INLINE Vec4d operator*(const Mat4d& m, const Vec4d& v);
321 template <> LM_FORCE_INLINE Mat4d operator*(const Mat4d& m1, const Mat4d& m2);
322 template <> LM_FORCE_INLINE Mat4d operator/(const Mat4d& m, const double& s);
323 
324 #endif
325 
326 LM_MATH_NAMESPACE_END
327 LM_NAMESPACE_END
328 
329 #include "math.matrix.inl"
330 
331 #endif // LIB_LIGHTMETRICA_MATH_MATRIX_H
Definition: math.matrix.h:30
Definition: math.vector.h:32
Definition: math.matrix.h:29
Definition: math.vector.h:31