Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
MathMatrices.h
Go to the documentation of this file.
1/***************************************************************************
2 * Mathlib
3 *
4 * Copyright (C) 2003-2004, Alexander Zaprjagaev <frustum@frustum.org>
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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 ***************************************************************************
21 * Update 2004/08/19
22 *
23 * added ivec2, ivec3 & ivec4 methods
24 * vec2, vec3 & vec4 data : added texture coords (s,t,p,q) and colour enums
25 *(r,g,b,a)
26 * mat3 & mat4 : added multiple float constructor ad modified methods returning
27 *mat3 or mat4
28 * optimisations like "x / 2.0f" replaced by faster "x * 0.5f"
29 * defines of multiples usefull maths values and radian/degree conversions
30 * vec2 : added methods : set, reset, compare, dot, closestPointOnLine,
31 *closestPointOnSegment,
32 * projectionOnLine, lerp, angle
33 * vec3 : added methods : set, reset, compare, dot, cross, closestPointOnLine,
34 *closestPointOnSegment,
35 * projectionOnLine, lerp, angle
36 * vec4 : added methods : set, reset, compare
37 ***************************************************************************
38 */
39 /*
40 *
41 * MathLibrary
42 * Copyright (c) 2011 NoLimitsDesigns
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the copyright holders nor the names of its
54 * contributors may be used to endorse or promote products derived from
55 * this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
58 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
61 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
67 * THE POSSIBILITY OF SUCH DAMAGE.
68
69 * If there are any concerns or questions about the code, please e-mail
70smasherprog@gmail.com or visit www.nolimitsdesigns.com
71 */
72
73/*
74 * Author: Scott Lee
75 */
76
77/*
78 Copyright (c) 2018 DIVIDE-Studio
79 Copyright (c) 2009 Ionut Cava
80
81 This file is part of DIVIDE Framework.
82
83 Permission is hereby granted, free of charge, to any person obtaining a copy
84 of this software
85 and associated documentation files (the "Software"), to deal in the Software
86 without restriction,
87 including without limitation the rights to use, copy, modify, merge, publish,
88 distribute, sublicense,
89 and/or sell copies of the Software, and to permit persons to whom the
90 Software is furnished to do so,
91 subject to the following conditions:
92
93 The above copyright notice and this permission notice shall be included in
94 all copies or substantial portions of the Software.
95
96 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
97 IMPLIED,
98 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
99 PARTICULAR PURPOSE AND NONINFRINGEMENT.
100 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
101 DAMAGES OR OTHER LIABILITY,
102 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
103 IN CONNECTION WITH THE SOFTWARE
104 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
105
106 */
107
108#pragma once
109#ifndef DVD_MATH_MATRICES_H_
110#define DVD_MATH_MATRICES_H_
111
112#include "Plane.h"
113
114namespace Divide {
115
116/*********************************
117* mat2
118*********************************/
119template <typename T>
120class mat2 {
121 static_assert(ValidMathType<T>, "Invalid base type!");
122
123 // m0 m1
124 // m2 m3
125public:
126 mat2() noexcept;
127 template<typename U>
128 explicit mat2(U m) noexcept;
129 template<typename U>
130 explicit mat2(U m0, U m1,
131 U m2, U m3) noexcept;
132 template<typename U>
133 explicit mat2(const U *values) noexcept;
134 mat2(const mat2 &B) noexcept;
135 template<typename U>
136 explicit mat2(const mat2<U> &B) noexcept;
137 template<typename U>
138 explicit mat2(const mat3<U> &B) noexcept;
139 template<typename U>
140 explicit mat2(const mat4<U> &B) noexcept;
141
142 template<typename U>
143 [[nodiscard]] vec2<T> operator*(const vec2<U> v) const noexcept;
144 template<typename U>
145 [[nodiscard]] vec3<T> operator*(const vec3<U> &v) const noexcept;
146 template<typename U>
147 [[nodiscard]] vec4<T> operator*(const vec4<U> &v) const noexcept;
148
149 template<typename U>
150 [[nodiscard]] mat2 operator*(const mat2<U> &B) const noexcept;
151 template<typename U>
152 [[nodiscard]] mat2 operator/(const mat2<U> &B) const noexcept;
153 template<typename U>
154 [[nodiscard]] mat2 operator+(const mat2<U> &B) const noexcept;
155 template<typename U>
156 [[nodiscard]] mat2 operator-(const mat2<U> &B) const noexcept;
157
158 template<typename U>
159 mat2 &operator*=(const mat2<U> &B) noexcept;
160 template<typename U>
161 mat2 &operator/=(const mat2<U> &B) noexcept;
162 template<typename U>
163 mat2 &operator+=(const mat2<U> &B) noexcept;
164 template<typename U>
165 mat2 &operator-=(const mat2<U> &B) noexcept;
166
167 template<typename U>
168 [[nodiscard]] mat2 operator*(U f) const noexcept;
169 template<typename U>
170 [[nodiscard]] mat2 operator/(U f) const noexcept;
171 template<typename U>
172 [[nodiscard]] mat2 operator+(U f) const noexcept;
173 template<typename U>
174 [[nodiscard]] mat2 operator-(U f) const noexcept;
175
176 template<typename U>
177 mat2 &operator*=(U f) noexcept;
178 template<typename U>
179 mat2 &operator/=(U f) noexcept;
180 template<typename U>
181 mat2 &operator+=(U f) noexcept;
182 template<typename U>
183 mat2 &operator-=(U f) noexcept;
184
185 [[nodiscard]] bool operator==(const mat2 &B) const noexcept;
186 [[nodiscard]] bool operator!=(const mat2 &B) const noexcept;
187 template<typename U>
188 [[nodiscard]] bool operator==(const mat2<U> &B) const noexcept;
189 template<typename U>
190 [[nodiscard]] bool operator!=(const mat2<U> &B) const noexcept;
191
192 [[nodiscard]] bool compare(const mat2 &B, F32 epsilon) const noexcept;
193 template<typename U>
194 [[nodiscard]] bool compare(const mat2<U> &B, F32 epsilon) const noexcept;
195
196 [[nodiscard]] operator T *();
197 [[nodiscard]] operator const T *() const;
198
199 [[nodiscard]] T& operator[](I32 i);
200 [[nodiscard]] T operator[](I32 i) const;
201
202 [[nodiscard]] T &element(U8 row, U8 column) noexcept;
203 [[nodiscard]] const T &element(U8 row, U8 column) const noexcept;
204
205 template<typename U>
206 void set(U m0, U m1, U m2, U m3) noexcept;
207 template<typename U>
208 void set(const U *matrix) noexcept;
209 template<typename U>
210 void set(const mat2<U> &matrix) noexcept;
211 template<typename U>
212 void set(const mat3<U> &matrix) noexcept;
213 template<typename U>
214 void set(const mat4<U> &matrix) noexcept;
215
216 template<typename U>
217 void setRow(I32 index, U value) noexcept;
218 template<typename U>
219 void setRow(I32 index, const vec2<U> value) noexcept;
220 template<typename U>
221 void setRow(I32 index, U x, U y) noexcept;
222 [[nodiscard]] vec2<T> getRow(I32 index) const noexcept;
223
224 template<typename U>
225 void setCol(I32 index, vec2<U> value) noexcept;
226 template<typename U>
227 void setCol(I32 index, U value) noexcept;
228 template<typename U>
229 void setCol(I32 index, U x, U y) noexcept;
230
231 [[nodiscard]] vec2<T> getCol(I32 index) const noexcept;
232
233 void zero() noexcept;
234 void identity() noexcept;
235 [[nodiscard]] bool isIdentity() const noexcept;
236 void swap(mat2 &B) noexcept;
237
238 [[nodiscard]] T det() const noexcept;
239 [[nodiscard]] T elementSum() const noexcept;
240 void inverse() noexcept;
241 void transpose() noexcept;
242 void inverseTranspose() noexcept;
243
244 [[nodiscard]] mat2 getInverse() const noexcept;
245 void getInverse(mat2 &ret) const noexcept;
246
247 [[nodiscard]] mat2 getTranspose() const noexcept;
248 void getTranspose(mat2 &ret) const noexcept;
249
250 [[nodiscard]] mat2 getInverseTranspose() const noexcept;
251 void getInverseTranspose(mat2 &ret) const noexcept;
252
253 union {
254 struct {
257 };
258 T mat[4];
259 T m[2][2];
261 };
262};
263
264/*********************************
265 * mat3
266 *********************************/
267template <typename T>
268class mat3 {
269 static_assert(ValidMathType<T>, "Invalid base type!");
270
271 // m0 m1 m2
272 // m3 m4 m5
273 // m7 m8 m9
274 public:
275 mat3() noexcept;
276 template<typename U>
277 explicit mat3(U m) noexcept;
278 template<typename U>
279 explicit mat3(U m0, U m1, U m2,
280 U m3, U m4, U m5,
281 U m6, U m7, U m8) noexcept;
282 template<typename U>
283 explicit mat3(const U *values) noexcept;
284 template<typename U>
285 explicit mat3(const mat2<U> &B, bool zeroFill) noexcept;
286 mat3(const mat3 &B) noexcept;
287 template<typename U>
288 explicit mat3(const mat3<U> &B) noexcept;
289 template<typename U>
290 explicit mat3(const mat4<U> &B) noexcept;
291 template<typename U>
292 explicit mat3(const vec3<U>& scale) noexcept;
293 template<typename U>
294 explicit mat3(const vec3<U>& rotStart, const vec3<U>& rotEnd) noexcept;
295
296 template<typename U>
297 [[nodiscard]] vec2<U> operator*(const vec2<U> v) const noexcept;
298 template<typename U>
299 [[nodiscard]] vec3<U> operator*(const vec3<U> &v) const noexcept;
300 template<typename U>
301 [[nodiscard]] vec4<U> operator*(const vec4<U> &v) const noexcept;
302
303 template<typename U>
304 [[nodiscard]] mat3 operator*(const mat3<U> &B) const noexcept;
305 template<typename U>
306 [[nodiscard]] mat3 operator/(const mat3<U> &B) const noexcept;
307 template<typename U>
308 [[nodiscard]] mat3 operator+(const mat3<U> &B) const noexcept;
309 template<typename U>
310 [[nodiscard]] mat3 operator-(const mat3<U> &B) const noexcept;
311
312 template<typename U>
313 mat3 &operator*=(const mat3<U> &B) noexcept;
314 template<typename U>
315 mat3 &operator/=(const mat3<U> &B) noexcept;
316 template<typename U>
317 mat3 &operator+=(const mat3<U> &B) noexcept;
318 template<typename U>
319 mat3 &operator-=(const mat3<U> &B) noexcept;
320
321 template<typename U>
322 [[nodiscard]] mat3 operator*(U f) const noexcept;
323 template<typename U>
324 [[nodiscard]] mat3 operator/(U f) const noexcept;
325 template<typename U>
326 [[nodiscard]] mat3 operator+(U f) const noexcept;
327 template<typename U>
328 [[nodiscard]] mat3 operator-(U f) const noexcept;
329
330 template<typename U>
331 mat3 &operator*=(U f) noexcept;
332 template<typename U>
333 mat3 &operator/=(U f) noexcept;
334 template<typename U>
335 mat3 &operator+=(U f) noexcept;
336 template<typename U>
337 mat3 &operator-=(U f) noexcept;
338
339 [[nodiscard]] bool operator==(const mat3 &B) const noexcept;
340 [[nodiscard]] bool operator!=(const mat3 &B) const noexcept;
341
342 template<typename U>
343 [[nodiscard]] bool operator==(const mat3<U> &B) const noexcept;
344 template<typename U>
345 [[nodiscard]] bool operator!=(const mat3<U> &B) const noexcept;
346
347 [[nodiscard]] bool compare(const mat3 &B, F32 epsilon) const noexcept;
348 template<typename U>
349 [[nodiscard]] bool compare(const mat3<U> &B, F32 epsilon) const noexcept;
350
351 [[nodiscard]] operator T *() noexcept;
352 [[nodiscard]] operator const T *() const noexcept;
353
354 [[nodiscard]] T &operator[](I32 i) noexcept;
355 [[nodiscard]] T operator[](I32 i) const noexcept;
356
357 [[nodiscard]] T &element(U8 row, U8 column) noexcept;
358 [[nodiscard]] const T &element(U8 row, U8 column) const noexcept;
359
360 template<typename U>
361 void set(U m0, U m1, U m2, U m3, U m4, U m5, U m6, U m7, U m8) noexcept;
362 template<typename U>
363 void set(const U *matrix) noexcept;
364 template<typename U>
365 void set(const mat2<U> &matrix) noexcept;
366 template<typename U>
367 void set(const mat3<U> &matrix) noexcept;
368 template<typename U>
369 void set(const mat4<U> &matrix) noexcept;
370
371 template<typename U>
372 void setRow(I32 index, U value) noexcept;
373 template<typename U>
374 void setRow(I32 index, const vec3<U> &value) noexcept;
375 template<typename U>
376 void setRow(I32 index, U x, U y, U z) noexcept;
377
378 [[nodiscard]] const vec3<T>& getRow(I32 index) const noexcept;
379
380 template<typename U>
381 void setCol(I32 index, const vec3<U> &value) noexcept;
382 template<typename U>
383 void setCol(I32 index, U value) noexcept;
384 template<typename U>
385 void setCol(I32 index, U x, U y, U z) noexcept;
386
387 [[nodiscard]] vec3<T> getCol(I32 index) const noexcept;
388
389 void zero() noexcept;
390 void identity() noexcept;
391 [[nodiscard]] bool isIdentity() const noexcept;
392 [[nodiscard]] bool isUniformScale(F32 tolerance = 0.0001f) const noexcept;
393 [[nodiscard]] bool isColOrthogonal() const noexcept;
394 void swap(mat3 &B) noexcept;
395
396 [[nodiscard]] T det() const noexcept;
397 [[nodiscard]] T elementSum() const noexcept;
398 void inverse() noexcept;
399 void transpose() noexcept;
400 void inverseTranspose() noexcept;
401
402 [[nodiscard]] mat3 getInverse() const noexcept;
403 void getInverse(mat3 &ret) const noexcept;
404
405 [[nodiscard]] mat3 getTranspose() const noexcept;
406 void getTranspose(mat3 &ret) const noexcept;
407
408 [[nodiscard]] mat3 getInverseTranspose() const noexcept;
409 void getInverseTranspose(mat3 &ret) const noexcept;
410
411 template<typename U>
412 void fromRotation(const vec3<U> &v, Angle::RADIANS<U> angle);
413 template<typename U>
414 void fromRotation(U x, U y, U z, Angle::RADIANS<U> angle);
415 template<typename U>
416 void fromXRotation(Angle::RADIANS<U> angle);
417 template<typename U>
418 void fromYRotation(Angle::RADIANS<U> angle);
419 template<typename U>
420 void fromZRotation(Angle::RADIANS<U> angle);
421
422 // setScale replaces the main diagonal!
423 template<typename U>
424 void setScale(U x, U y, U z) noexcept;
425 template<typename U>
426 void setScale(const vec3<U> &v) noexcept;
427
428 [[nodiscard]] vec3<T> getScale() const noexcept;
429 [[nodiscard]] vec3<T> getScaleSq() const noexcept;
430
432 [[nodiscard]] vec3<T> getRightVec( ) const noexcept;
434 [[nodiscard]] vec3<T> getUpVec() const noexcept;
436 [[nodiscard]] vec3<T> getForwardVec() const noexcept;
437
439 [[nodiscard]] vec3<T> getRightDirection( ) const noexcept;
441 [[nodiscard]] vec3<T> getUpDirection() const noexcept;
443 [[nodiscard]] vec3<T> getForwardDirection() const noexcept;
444
445 void orthoNormalize();
446
447 union {
448 struct {
449 T _11, _12, _13; // standard names for components
450 T _21, _22, _23; // standard names for components
451 T _31, _32, _33; // standard names for components
452 };
453 T mat[9];
454 T m[3][3];
456 };
457};
458
459/***************
460 * mat4
461 ***************/
462#pragma pack(push)
463#pragma pack(1)
464template <typename T>
465class mat4 {
466 static_assert(ValidMathType<T>, "Invalid base type!");
467
468 // m0 m1 m2 m3
469 // m4 m5 m6 m7
470 // m8 m9 m10 m11
471 // m12 m13 m14 m15
472 public:
473 mat4() noexcept;
474
475 template<typename U>
476 mat4( U m0, U m1, U m2, U m3,
477 U m4, U m5, U m6, U m7,
478 U m8, U m9, U m10, U m11,
479 U m12, U m13, U m14, U m15 ) noexcept;
480 template<typename U>
481 explicit mat4(U value) noexcept;
482 template<typename U>
483 explicit mat4(const U *values) noexcept;
484 template<typename U>
485 explicit mat4(const mat2<U> &B, bool zeroFill) noexcept;
486 template<typename U>
487 explicit mat4(const mat3<U> &B, bool zeroFill) noexcept;
488 mat4(const mat4 &B) noexcept;
489 template<typename U>
490 explicit mat4(const mat4<U> &B) noexcept;
491 template<typename U>
492 explicit mat4(const vec3<U> &translation, const vec3<U> &scale) noexcept;
493 template<typename U>
494 explicit mat4(const vec3<U> &translation, const vec3<U> &scale, const mat3<U>& rotation) noexcept;
495 template<typename U>
496 explicit mat4(const vec3<U> &translation) noexcept;
497 template<typename U>
498 explicit mat4(U translationX, U translationY, U translationZ) noexcept;
499 template<typename U>
500 explicit mat4(const vec3<U> &axis, Angle::RADIANS<U> angle) noexcept;
501 template<typename U>
502 explicit mat4(U x, U y, U z, Angle::RADIANS<U> angle) noexcept;
503 template<typename U>
504 explicit mat4(const Plane<U>& reflectionPlane) noexcept;
505
506 template<typename U>
507 [[nodiscard]] vec2<U> operator*(const vec2<U> v) const noexcept;
508 template<typename U>
509 [[nodiscard]] vec3<U> operator*(const vec3<U> &v) const noexcept;
510 template<typename U>
511 [[nodiscard]] vec4<U> operator*(const vec4<U> &v) const noexcept;
512
513 template<typename U>
514 [[nodiscard]] mat4 operator*(const mat4<U>& matrix) const noexcept;
515 template<typename U>
516 [[nodiscard]] mat4 operator/(const mat4<U>& matrix) const noexcept;
517 template<typename U>
518 [[nodiscard]] mat4 operator+(const mat4<U> &matrix) const noexcept;
519 template<typename U>
520 [[nodiscard]] mat4 operator-(const mat4<U> &matrix) const noexcept;
521
522 template<typename U>
523 mat4 &operator*=(const mat4<U> &matrix) noexcept;
524 template<typename U>
525 mat4 &operator/=(const mat4<U> &matrix) noexcept;
526 template<typename U>
527 mat4 &operator+=(const mat4<U> &matrix) noexcept;
528 template<typename U>
529 mat4 &operator-=(const mat4<U> &matrix) noexcept;
530
531 template<typename U>
532 [[nodiscard]] mat4 operator*(U f) const noexcept;
533 template<typename U>
534 [[nodiscard]] mat4 operator/(U f) const noexcept;
535 template<typename U>
536 [[nodiscard]] mat4 operator+(U f) const noexcept;
537 template<typename U>
538 [[nodiscard]] mat4 operator-(U f) const noexcept;
539
540 template<typename U>
541 mat4 &operator*=(U f) noexcept;
542 template<typename U>
543 mat4 &operator/=(U f) noexcept;
544 template<typename U>
545 mat4 &operator+=(U f) noexcept;
546 template<typename U>
547 mat4 &operator-=(U f) noexcept;
548
549 [[nodiscard]] bool operator==(const mat4& B) const noexcept;
550 [[nodiscard]] bool operator!=(const mat4 &B) const noexcept;
551 template<typename U>
552 [[nodiscard]] bool operator==(const mat4<U>& B) const noexcept;
553 template<typename U>
554 [[nodiscard]] bool operator!=(const mat4<U> &B) const noexcept;
555
556 [[nodiscard]] bool compare(const mat4 &B, F32 epsilon) const noexcept;
557 template<typename U>
558 [[nodiscard]] bool compare(const mat4<U> &B, F32 epsilon) const noexcept;
559
560 [[nodiscard]] operator T *() noexcept;
561 [[nodiscard]] operator const T *() const noexcept;
562
563 [[nodiscard]] T &operator[](I32 i) noexcept;
564 [[nodiscard]] const T &operator[](I32 i) const noexcept;
565
566 [[nodiscard]] T &element(U8 row, U8 column) noexcept;
567 [[nodiscard]] const T &element(U8 row, U8 column) const noexcept;
568
569 void set(std::initializer_list<T> matrix) noexcept;
570
571 template<typename U>
572 void set(U const *matrix) noexcept;
573 template<typename U>
574 void set(const mat2<U> &matrix) noexcept;
575 template<typename U>
576 void set(const mat3<U> &matrix) noexcept;
577 template<typename U>
578 void set(const mat4<U> &matrix) noexcept;
579 template<typename U>
580 void set(const vec3<U> &translation, const vec3<U> &scale, const mat4<U>& rotation) noexcept;
581
582 template<typename U>
583 void setRow(I32 index, U value) noexcept;
584 template<typename U>
585 void setRow(I32 index, const vec4<U> &value) noexcept;
586 template<typename U>
587 void setRow(I32 index, U x, U y, U z, U w) noexcept;
588
589 [[nodiscard]] const vec4<T>& getRow(I32 index) const noexcept;
590
591 template<typename U>
592 void setCol(I32 index, const vec4<U> &value) noexcept;
593 template<typename U>
594 void setCol(I32 index, U value) noexcept;
595 template<typename U>
596 void setCol(I32 index, U x, U y, U z, U w) noexcept;
597
598 [[nodiscard]] vec4<T> getCol(I32 index) const noexcept;
599
600 void zero() noexcept;
601 void identity() noexcept;
602 [[nodiscard]] bool isIdentity() const noexcept;
603 [[nodiscard]] bool isUniformScale(F32 tolerance = 0.0001f) const noexcept;
604 [[nodiscard]] bool isColOrthogonal() const noexcept;
605 void swap(mat4 &B) noexcept;
606
607 [[nodiscard]] T det() const noexcept;
608 [[nodiscard]] T elementSum() const noexcept;
609 void orthoNormalize() noexcept;
610 void inverse() noexcept;
611 void transpose() noexcept;
612 void inverseTranspose() noexcept;
613 [[nodiscard]] mat4 transposeRotation() const noexcept;
614
615 [[nodiscard]] mat4 getInverse() const noexcept;
616 void getInverse(mat4 &ret) const noexcept;
617
618 [[nodiscard]] mat4 getTranspose() const noexcept;
619 void getTranspose(mat4 &out) const noexcept;
620
621 [[nodiscard]] mat4 getInverseTranspose() const noexcept;
622 void getInverseTranspose(mat4 &ret) const noexcept;
623
624 [[nodiscard]] mat4 getTransposeRotation() const noexcept;
625 void getTransposeRotation(mat4 &ret) const noexcept;
626
627 template<typename U>
628 void fromRotation(U x, U y, U z, Angle::RADIANS<U> angle) noexcept;
629 template<typename U>
630 void fromXRotation(Angle::RADIANS<U> angle) noexcept;
631 template<typename U>
632 void fromYRotation(Angle::RADIANS<U> angle) noexcept;
633 template<typename U>
634 void fromZRotation(Angle::RADIANS<U> angle) noexcept;
635
636 template<typename U>
637 void setTranslation(const vec3<U> &v) noexcept;
638 template<typename U>
639 void setTranslation(U x, U y, U z) noexcept;
640
641 template<typename U>
642 void setScale(U x, U y, U z) noexcept;
643 template<typename U>
644 void setScale(const vec3<U> &v) noexcept;
645
646 [[nodiscard]] vec3<T> getScale() const noexcept;
647 [[nodiscard]] vec3<T> getScaleSq() const noexcept;
648
650 [[nodiscard]] vec3<T> getRightVec( ) const noexcept;
652 [[nodiscard]] vec3<T> getUpVec( ) const noexcept;
654 [[nodiscard]] vec3<T> getForwardVec( ) const noexcept;
655
657 [[nodiscard]] vec3<T> getRightDirection( ) const noexcept;
659 [[nodiscard]] vec3<T> getUpDirection( ) const noexcept;
661 [[nodiscard]] vec3<T> getForwardDirection( ) const noexcept;
662
663 template<typename U>
664 [[nodiscard]] vec3<U> transform(const vec3<U> &v, bool homogeneous) const;
665 template<typename U>
666 [[nodiscard]] vec3<U> transformHomogeneous(const vec3<U> &v) const;
667 template<typename U>
668 [[nodiscard]] vec3<U> transformNonHomogeneous(const vec3<U> &v) const noexcept;
669
670 template<typename U>
671 void translate(const vec3<U> &v) noexcept;
672 template<typename U>
673 void translate(U x, U y, U z) noexcept;
674
675 template<typename U>
676 void scale(const vec3<U> &v) noexcept;
677 template<typename U>
678 void scale(U x, U y, U z) noexcept;
679
680 [[nodiscard]] vec3<T> getTranslation() const noexcept;
681 [[nodiscard]] mat4 getRotation() const;
682
683 template<typename U>
684 const mat4& reflect(U x, U y, U z, U w) noexcept;
685 template<typename U>
686 const mat4& reflect(const Plane<U> &plane) noexcept;
687
688 template<typename U>
689 void extractMat3(mat3<U> &matrix3) const noexcept;
690
692 static mat4<T> Multiply(const mat4<T>& matrixA, const mat4<T>& matrixB) noexcept;
693
695 static void Multiply(const mat4<T>& matrixA, const mat4<T>& matrixB, mat4<T>& ret) noexcept;
696
697 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
698 static void Inverse(const T* in, T* out) noexcept;
699
700 union {
701 struct {
706 };
707 T mat[16];
708 T m[4][4];
711 };
712};
713#pragma pack(pop)
714
715static const mat4<F32> MAT4_BIAS_NEGATIVE_ONE_Z{ 0.5, 0.0, 0.0, 0.0,
716 0.0, 0.5, 0.0, 0.0,
717 0.0, 0.0, 0.5, 0.0,
718 0.5, 0.5, 0.5, 1.0 };
719
720static const mat4<F32> MAT4_BIAS_ZERO_ONE_Z{ 0.5, 0.0, 0.0, 0.0,
721 0.0, 0.5, 0.0, 0.0,
722 0.0, 0.0, 1.0, 0.0,
723 0.5, 0.5, 0.0, 1.0 };
724static const mat2<F32> MAT2_ZERO{ 0.f, 0.f,
725 0.f, 0.f };
726static const mat3<F32> MAT3_ZERO{ 0.f, 0.f, 0.f,
727 0.f, 0.f, 0.f,
728 0.f, 0.f, 0.f };
729static const mat4<F32> MAT4_ZERO{ 0.f, 0.f, 0.f, 0.f,
730 0.f, 0.f, 0.f, 0.f,
731 0.f, 0.f, 0.f, 0.f,
732 0.f, 0.f, 0.f, 0.f };
733
734static const mat4<F32> MAT4_NEGATIVE_ONE{ -1.f, -1.f, -1.f, -1.f,
735 -1.f, -1.f, -1.f, -1.f,
736 -1.f, -1.f, -1.f, -1.f,
737 -1.f, -1.f, -1.f, -1.f };
741
742//MAT4_INITIAL_TRANSFORM is a special transform matrix that has the Y position and all of the scale axis set to a really low values
743//This avoids object popping up at (0,0,0) with whatever scale they were exported at while loading for MAX_FRAMES_IN_FLIGHT - 1u frames.
745 vec3<F32>(0.f, -65535.f, 0.f),
746 vec3<F32>(1.f / U8_MAX)
747};
748
749} // namespace Divide
750
751#endif //DVD_MATH_MATRICES_H_
752
753#include "MathMatrices.inl"
vec2< T > _vec[2]
Definition: MathMatrices.h:260
T & operator[](I32 i)
void set(U m0, U m1, U m2, U m3) noexcept
void setCol(I32 index, vec2< U > value) noexcept
bool isIdentity() const noexcept
vec2< T > getRow(I32 index) const noexcept
mat2() noexcept
bool operator!=(const mat2 &B) const noexcept
mat2 & operator+=(U f) noexcept
void identity() noexcept
vec2< T > getCol(I32 index) const noexcept
mat2 operator/(U f) const noexcept
mat2 & operator+=(const mat2< U > &B) noexcept
mat2 operator+(const mat2< U > &B) const noexcept
void inverse() noexcept
mat2 & operator/=(const mat2< U > &B) noexcept
mat2 getTranspose() const noexcept
mat2 operator*(U f) const noexcept
T det() const noexcept
mat2 operator-(U f) const noexcept
void swap(mat2 &B) noexcept
mat2 getInverseTranspose() const noexcept
void transpose() noexcept
mat2 getInverse() const noexcept
mat2 & operator-=(const mat2< U > &B) noexcept
mat2 & operator/=(U f) noexcept
mat2 & operator-=(U f) noexcept
mat2 operator*(const mat2< U > &B) const noexcept
void inverseTranspose() noexcept
T elementSum() const noexcept
bool compare(const mat2 &B, F32 epsilon) const noexcept
bool operator==(const mat2 &B) const noexcept
void zero() noexcept
mat2 operator+(U f) const noexcept
T & element(U8 row, U8 column) noexcept
void setRow(I32 index, U value) noexcept
mat2 & operator*=(U f) noexcept
vec2< T > operator*(const vec2< U > v) const noexcept
mat2 & operator*=(const mat2< U > &B) noexcept
mat2 operator/(const mat2< U > &B) const noexcept
mat2 operator-(const mat2< U > &B) const noexcept
void fromZRotation(Angle::RADIANS< U > angle)
mat3 & operator*=(const mat3< U > &B) noexcept
T elementSum() const noexcept
T det() const noexcept
vec3< T > getForwardDirection() const noexcept
Returns normalized(getForwardVec())
bool operator==(const mat3 &B) const noexcept
mat3 getTranspose() const noexcept
const vec3< T > & getRow(I32 index) const noexcept
void setCol(I32 index, const vec3< U > &value) noexcept
mat3 & operator+=(U f) noexcept
mat3 & operator/=(const mat3< U > &B) noexcept
T & operator[](I32 i) noexcept
vec3< T > getScaleSq() const noexcept
vec3< T > getScale() const noexcept
mat3 operator/(const mat3< U > &B) const noexcept
void setRow(I32 index, U value) noexcept
vec3< T > getRightVec() const noexcept
Alias for getCol(0)
mat3 operator+(U f) const noexcept
mat3 getInverseTranspose() const noexcept
mat3 & operator-=(const mat3< U > &B) noexcept
bool isColOrthogonal() const noexcept
mat3 operator*(U f) const noexcept
mat3 operator-(U f) const noexcept
vec3< T > getCol(I32 index) const noexcept
void fromXRotation(Angle::RADIANS< U > angle)
void inverse() noexcept
mat3 & operator/=(U f) noexcept
T & element(U8 row, U8 column) noexcept
bool operator!=(const mat3 &B) const noexcept
mat3 operator/(U f) const noexcept
bool isUniformScale(F32 tolerance=0.0001f) const noexcept
void identity() noexcept
vec3< T > _vec[3]
Definition: MathMatrices.h:455
void setScale(U x, U y, U z) noexcept
mat3 & operator*=(U f) noexcept
void set(U m0, U m1, U m2, U m3, U m4, U m5, U m6, U m7, U m8) noexcept
bool compare(const mat3 &B, F32 epsilon) const noexcept
vec2< U > operator*(const vec2< U > v) const noexcept
void inverseTranspose() noexcept
mat3 operator+(const mat3< U > &B) const noexcept
mat3 & operator+=(const mat3< U > &B) noexcept
vec3< T > getRightDirection() const noexcept
Returns normalized(getRightVec())
bool isIdentity() const noexcept
void orthoNormalize()
void zero() noexcept
void transpose() noexcept
mat3 getInverse() const noexcept
vec3< T > getForwardVec() const noexcept
Alias for -getCol(2). Assumes -Z fwd.
mat3 operator-(const mat3< U > &B) const noexcept
mat3 & operator-=(U f) noexcept
vec3< T > getUpDirection() const noexcept
Returns normalized(getUpVec())
void fromRotation(const vec3< U > &v, Angle::RADIANS< U > angle)
vec3< T > getUpVec() const noexcept
Alias for getCol(1)
mat3 operator*(const mat3< U > &B) const noexcept
void swap(mat3 &B) noexcept
mat3() noexcept
void fromYRotation(Angle::RADIANS< U > angle)
vec3< T > getForwardDirection() const noexcept
Returns normalized(getForwardVec())
vec3< T > getScaleSq() const noexcept
void setRow(I32 index, U x, U y, U z, U w) noexcept
vec3< T > getForwardVec() const noexcept
Alias for -getCol(2). Assumes -Z fwd.
T & element(U8 row, U8 column) noexcept
void swap(mat4 &B) noexcept
void setCol(I32 index, const vec4< U > &value) noexcept
mat4 getTransposeRotation() const noexcept
void inverseTranspose() noexcept
bool isUniformScale(F32 tolerance=0.0001f) const noexcept
void setCol(I32 index, U value) noexcept
void set(const mat3< U > &matrix) noexcept
void zero() noexcept
vec3< T > getTranslation() const noexcept
vec4< T > _vec[4]
Definition: MathMatrices.h:709
void inverse() noexcept
const mat4 & reflect(U x, U y, U z, U w) noexcept
vec3< T > getUpDirection() const noexcept
Returns normalized(getUpVec())
mat4 transposeRotation() const noexcept
void setRow(I32 index, U value) noexcept
T elementSum() const noexcept
mat4 operator*(U f) const noexcept
vec2< U > operator*(const vec2< U > v) const noexcept
mat4 & operator*=(U f) noexcept
mat4 & operator*=(const mat4< U > &matrix) noexcept
void setCol(I32 index, U x, U y, U z, U w) noexcept
mat4 getInverse() const noexcept
mat4 operator+(U f) const noexcept
SimdVector< T > _reg[4]
Definition: MathMatrices.h:710
void fromXRotation(Angle::RADIANS< U > angle) noexcept
T & operator[](I32 i) noexcept
void identity() noexcept
mat4 operator*(const mat4< U > &matrix) const noexcept
void transpose() noexcept
bool compare(const mat4< U > &B, F32 epsilon) const noexcept
mat4 operator-(U f) const noexcept
bool isIdentity() const noexcept
void set(const mat4< U > &matrix) noexcept
vec3< T > getRightVec() const noexcept
Alias for getCol(0)
static mat4< T > Multiply(const mat4< T > &matrixA, const mat4< T > &matrixB) noexcept
ret = A * B
void extractMat3(mat3< U > &matrix3) const noexcept
bool operator==(const mat4 &B) const noexcept
mat4 getRotation() const
void fromZRotation(Angle::RADIANS< U > angle) noexcept
vec3< U > operator*(const vec3< U > &v) const noexcept
vec3< U > transformHomogeneous(const vec3< U > &v) const
vec3< T > getRightDirection() const noexcept
Returns normalized(getRightVec())
T det() const noexcept
void setScale(U x, U y, U z) noexcept
void fromRotation(U x, U y, U z, Angle::RADIANS< U > angle) noexcept
void setRow(I32 index, const vec4< U > &value) noexcept
void fromYRotation(Angle::RADIANS< U > angle) noexcept
static void Inverse(const T *in, T *out) noexcept
const vec4< T > & getRow(I32 index) const noexcept
void setTranslation(const vec3< U > &v) noexcept
vec3< T > getUpVec() const noexcept
Alias for getCol(1)
mat4 & operator-=(U f) noexcept
vec3< U > transformNonHomogeneous(const vec3< U > &v) const noexcept
mat4 & operator/=(U f) noexcept
mat4 & operator/=(const mat4< U > &matrix) noexcept
mat4 operator/(const mat4< U > &matrix) const noexcept
void orthoNormalize() noexcept
void set(std::initializer_list< T > matrix) noexcept
mat4 operator-(const mat4< U > &matrix) const noexcept
vec4< U > operator*(const vec4< U > &v) const noexcept
mat4 & operator+=(const mat4< U > &matrix) noexcept
mat4 & operator-=(const mat4< U > &matrix) noexcept
mat4 & operator+=(U f) noexcept
void translate(const vec3< U > &v) noexcept
mat4 getInverseTranspose() const noexcept
vec3< T > getScale() const noexcept
vec3< U > transform(const vec3< U > &v, bool homogeneous) const
vec4< T > getCol(I32 index) const noexcept
bool compare(const mat4 &B, F32 epsilon) const noexcept
mat4 getTranspose() const noexcept
void set(U const *matrix) noexcept
mat4 operator+(const mat4< U > &matrix) const noexcept
void scale(const vec3< U > &v) noexcept
mat4 operator/(U f) const noexcept
void set(const mat2< U > &matrix) noexcept
bool operator!=(const mat4 &B) const noexcept
bool isColOrthogonal() const noexcept
mat4() noexcept
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
static const mat3< F32 > MAT3_IDENTITY
Definition: MathMatrices.h:739
static const mat4< F32 > MAT4_BIAS_ZERO_ONE_Z
Definition: MathMatrices.h:720
static const mat2< F32 > MAT2_ZERO
Definition: MathMatrices.h:724
int32_t I32
uint8_t U8
static const mat4< F32 > MAT4_BIAS_NEGATIVE_ONE_Z
Definition: MathMatrices.h:715
static const mat4< F32 > MAT4_NEGATIVE_ONE
Definition: MathMatrices.h:734
static const mat2< F32 > MAT2_IDENTITY
Definition: MathMatrices.h:738
constexpr U8 U8_MAX
static const mat4< F32 > MAT4_INITIAL_TRANSFORM
Definition: MathMatrices.h:744
static const mat4< F32 > MAT4_ZERO
Definition: MathMatrices.h:729
static const mat4< F32 > MAT4_IDENTITY
Definition: MathMatrices.h:740
static const mat3< F32 > MAT3_ZERO
Definition: MathMatrices.h:726