File size: 5,306 Bytes
7b853a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

#pragma once

#include "Vector.h"
#include "Quaternion.h"

enum class CoordinateSpace : uint8_t
{
    World,
    Local,
};

//
// Matrices are Row-Major
// Multiplication order is right to left
// ObjectWorldTransform = LocalObjectTransform * WorldTransform
//

namespace Math
{
    class alignas(16) Matrix
    {
    public:

        static Matrix const Identity;

    public:

        static Matrix FromRotation(const Quaternion& rotation);
        static Matrix FromTranslation(const Vector& translation);
        static Matrix FromScale(const Vector& scale);
        static Matrix FromUniformScale(float uniformScale);
        static Matrix FromTranslationAndScale(const Vector& translation, const Vector& scale);
        static Matrix FromRotationBetweenVectors(const Vector sourceVector, const Vector targetVector);

    public:

        explicit Matrix();
        explicit Matrix(NoInit_t);
        explicit Matrix(ZeroInit_t);
        explicit Matrix(float v00, float v01, float v02, float v03,
                        float v10, float v11, float v12, float v13,
                        float v20, float v21, float v22, float v23,
                        float v30, float v31, float v32, float v33);
        explicit Matrix(float values[16]);
        explicit Matrix(Vector const& xAxis, Vector const& yAxis, Vector const& zAxis);
        explicit Matrix(Vector const& xAxis, Vector const& yAxis, Vector const& zAxis, Vector const& translation);

        Matrix(const Vector axis, Radians angleRadians);
        Matrix(const AxisAngle axisAngle);

        explicit Matrix(const Quaternion& rotation);
        explicit Matrix(const Quaternion& rotation, const Vector& translation, const Vector& scale = Vector::One);
        explicit Matrix(const Quaternion& rotation, const Vector& translation, float scale = 1.0f);
        explicit Matrix(const EulerAngles& eulerAngles, const Vector translation = Vector::UnitW);

        EulerAngles ToEulerAngles() const;

        float* AsFloatArray();
        const float* AsFloatArray() const;
        const Vector& GetRow(uint32_t row) const;

        const Vector& GetAxisX() const;
        const Vector& GetAxisY() const;
        const Vector& GetAxisZ() const;

        void SetAxisX(const Vector& xAxis);
        void SetAxisY(const Vector& yAxis);
        void SetAxisZ(const Vector& zAxis);

        Float3 GetForwardVector() const;
        Float3 GetRightVector() const;
        Float3 GetUpVector() const;

        Vector GetUnitAxisX() const;
        Vector GetUnitAxisY() const;
        Vector GetUnitAxisZ() const;

        bool IsIdentity() const;
        bool IsOrthogonal() const;
        bool IsOrthonormal() const;

        bool Decompose(Quaternion& outRotation, Vector& outTranslation, Vector& outScale) const;

        Matrix& Transpose();
        Matrix GetTransposed() const;

        Matrix& Invert();
        Matrix GetInverse() const;

        Vector GetDeterminant() const;
        float GetDeterminantAsFloat() const;

        Vector GetTranslation() const;
        const Vector& GetTranslationWithW() const;
        Matrix& SetTranslation(Vector const& v);
        Matrix& SetTranslation(Float3 const& v);
        Matrix& SetTranslation(Float4 const& v);

        Quaternion GetRotation() const;

        Matrix& SetRotation(const Matrix& rotation);
        Matrix& SetRotation(const Quaternion& rotation);

        Matrix& SetRotationMaintainingScale(const Matrix& rotation);
        Matrix& SetRotationMaintainingScale(const Quaternion& rotation);

        Vector GetScale() const;

        Matrix& RemoveScale();
        Matrix& SetScale(const Vector& scale);
        Matrix& SetScale(float uniformScale);

        Matrix& RemoveScaleFast();
        Matrix& SetScaleFast(const Vector& scale);
        Matrix& SetScaleFast(float uniformScale);

        //
        // Operators
        //

        // Applies rotation and scale to a vector and returns a result with the W = 0
        Vector RotateVector(const Vector& vector) const;

        // Applies rotation and scale to a vector and returns a result with the W = 0
        Vector TransformNormal(const Vector& vector) const;

        // Applies the transformation to a given point and ensures the resulting W = 1
        Vector TransformPoint(const Vector& point) const;

        // Applies the transformation to a vector ignoring the W value.
        // Same as TransformPoint with the result W left unchanged
        Vector TransformVector3(const Vector& vector) const;

        // Applies the transformation to a given vector with the result W left unchanged
        Vector TransformVector4(const Vector& vector) const;

        Vector& operator[](uint32_t i);
        const Vector operator[](uint32_t i) const;

        Matrix operator*(const Matrix& rhs) const;
        Matrix& operator*=(const Matrix& rhs);

        Matrix operator*(const Quaternion& rhs) const;
        Matrix operator*=(const Quaternion& rhs);

        bool operator==(const Matrix& rhs) const;

    public:

        union
        {
            Vector      m_rows[4];
            float       m_values[4][4];
        };
    };
}

#include "Matrix.inl"