File size: 9,971 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "Matrix.h"

#include <cfloat>

using namespace Math;

namespace
{
    static bool CheckForZeroScaleInRow(float scale, const Vector& row)
    {
        float const absScale = Math::Abs(scale);

        for (int i = 0; i < 3; i++)
        {
            if (absScale < 1 && Math::Abs(row[i]) >= FLT_MAX * absScale)
            {
                return false;
            }
        }

        return true;
    }

    static bool ExtractAndRemoveScalingAndShear(Matrix& matrix, Vector& scale, Vector& shear)
    {
        scale = Vector::Zero;
        shear = Vector::Zero;

        Float3 scaleValues = Float3::Zero;
        Float3 shearValues = Float3::Zero;

        // This implementation follows the technique described in the paper by
        // Spencer W. Thomas in the Graphics Gems II article: "Decomposing a
        // Matrix into Simple Transformations", p. 320.

        Vector row[3];
        row[0] = Vector(matrix[0][0], matrix[0][1], matrix[0][2]);
        row[1] = Vector(matrix[1][0], matrix[1][1], matrix[1][2]);
        row[2] = Vector(matrix[2][0], matrix[2][1], matrix[2][2]);

        float maxVal = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (Math::Abs(row[i][j]) > maxVal)
                {
                    maxVal = Math::Abs(row[i][j]);
                }
            }
        }

        // We normalize the 3x3 matrix here.
        // It was noticed that this can improve numerical stability significantly,
        // especially when many of the upper 3x3 matrix's coefficients are very
        // close to zero; we correct for this step at the end by multiplying the
        // scaling factors by maxVal at the end (shear and rotation are not
        // affected by the normalization).

        if (maxVal != 0)
        {
            for (int i = 0; i < 3; i++)
            {
                if (!CheckForZeroScaleInRow(maxVal, row[i]))
                {
                    return false;
                }
                else
                {
                    row[i] /= maxVal;
                }
            }
        }

        // Compute X scale factor.
        scaleValues.m_x = row[0].Length3().ToFloat();
        if (!CheckForZeroScaleInRow(scaleValues.m_x, row[0]))
        {
            return false;
        }

        // Normalize first row.
        row[0] /= scaleValues.m_x;

        // An XY shear factor will shear the X coord. as the Y coord. changes.
        // There are 6 combinations (XY, XZ, YZ, YX, ZX, ZY), although we only
        // extract the first 3 because we can effect the last 3 by shearing in
        // XY, XZ, YZ combined rotations and scales.
        //
        // shear matrix <   1,  YX,  ZX,  0,
        //                 XY,   1,  ZY,  0,
        //                 XZ,  YZ,   1,  0,
        //                  0,   0,   0,  1 >

        // Compute XY shear factor and make 2nd row orthogonal to 1st.
        shearValues[0] = Vector::Dot3(row[0], row[1]).ToFloat();
        row[1] -= row[0] * shearValues[0];

        // Now, compute Y scale.
        scaleValues.m_y = row[1].Length3().ToFloat();
        if (!CheckForZeroScaleInRow(scaleValues.m_y, row[1]))
        {
            return false;
        }

        // Normalize 2nd row and correct the XY shear factor for Y scaling.
        row[1] /= scaleValues.m_y;
        shearValues[0] /= scaleValues.m_y;

        // Compute XZ and YZ shears, orthogonalize 3rd row.
        shearValues[1] = Vector::Dot3(row[0], row[2]).ToFloat();
        row[2] -= row[0] * shearValues[1];
        shearValues[2] = Vector::Dot3(row[1], row[2]).ToFloat();
        row[2] -= row[1] * shearValues[2];

        // Next, get Z scale.
        scaleValues.m_z = row[2].Length3().ToFloat();
        if (!CheckForZeroScaleInRow(scaleValues.m_z, row[2]))
        {
            return false;
        }

        // Normalize 3rd row and correct the XZ and YZ shear factors for Z scaling.
        row[2] /= scaleValues.m_z;
        shearValues[1] /= scaleValues.m_z;
        shearValues[2] /= scaleValues.m_z;

        // At this point, the upper 3x3 matrix in mat is orthonormal.
        // Check for a coordinate system flip. If the determinant
        // is less than zero, then negate the matrix and the scaling factors.
        if (Vector::Dot3(row[0], Vector::Cross3(row[1], row[2])).ToFloat() < 0)
        {
            for (int i = 0; i < 3; i++)
            {
                scaleValues[i] *= -1;
                row[i] *= -1;
            }
        }

        // Copy over the orthonormal rows into the returned matrix.
        // The upper 3x3 matrix in mat is now a rotation matrix.
        for (int i = 0; i < 3; i++)
        {
            matrix[i].SetX(row[i][0]);
            matrix[i].SetY(row[i][1]);
            matrix[i].SetZ(row[i][2]);
        }

        // Correct the scaling factors for the normalization step that we
        // performed above; shear and rotation are not affected by the
        // normalization.
        scaleValues *= maxVal;

        scale = Vector(scaleValues);
        shear = Vector(shearValues);

        return true;
    }
}

namespace Math
{
    Matrix const Matrix::Identity(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);

    Matrix::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)
    {
        m_rows[0] = Vector(v00, v01, v02, v03);
        m_rows[1] = Vector(v10, v11, v12, v13);
        m_rows[2] = Vector(v20, v21, v22, v23);
        m_rows[3] = Vector(v30, v31, v32, v33);
    }

    Matrix::Matrix(float values[16])
    {
        m_rows[0] = Vector(values[0], values[1], values[2], values[3]);
        m_rows[1] = Vector(values[4], values[5], values[6], values[7]);
        m_rows[2] = Vector(values[8], values[9], values[10], values[11]);
        m_rows[3] = Vector(values[12], values[13], values[14], values[15]);
    }

    Matrix::Matrix(const Vector& xAxis, const Vector& yAxis, const Vector& zAxis)
    {
        m_rows[0] = xAxis;
        m_rows[1] = yAxis;
        m_rows[2] = zAxis;
        m_rows[3] = Vector::UnitW;
    }

    Matrix::Matrix(const Vector& xAxis, const Vector& yAxis, const Vector& zAxis, const Vector& translation)
    {
        m_rows[0] = xAxis;
        m_rows[1] = yAxis;
        m_rows[2] = zAxis;
        m_rows[3] = translation.GetWithW1();
    }

    Matrix::Matrix(const EulerAngles& eulerAngles, const Vector translation)
    {
        float cx, cy, cz, sx, sy, sz, czsx, cxcz, sysz;

        sx = sinf((float)eulerAngles.m_x); cx = cosf((float)eulerAngles.m_x);
        sy = sinf((float)eulerAngles.m_y); cy = cosf((float)eulerAngles.m_y);
        sz = sinf((float)eulerAngles.m_z); cz = cosf((float)eulerAngles.m_z);

        czsx = cz * sx;
        cxcz = cx * cz;
        sysz = sy * sz;

        // Order is XYZ
        m_values[0][0] = cy * cz;
        m_values[0][1] = cy * sz;
        m_values[0][2] = -sy;
        m_values[1][0] = czsx * sy - cx * sz;
        m_values[1][1] = cxcz + sx * sysz;
        m_values[1][2] = cy * sx;
        m_values[2][0] = cxcz * sy + sx * sz;
        m_values[2][1] = -czsx + cx * sysz;
        m_values[2][2] = cx * cy;
        m_values[0][3] = 0.0f;
        m_values[1][3] = 0.0f;
        m_values[2][3] = 0.0f;

        // Translation
        m_rows[3] = translation.GetWithW1();
    }

    EulerAngles Matrix::ToEulerAngles() const
    {
        EulerAngles result;

        result.m_x = Radians(Math::ATan2(m_values[1][2], m_values[2][2]));

        float const c2 = Math::Sqrt((m_values[0][0] * m_values[0][0]) + (m_values[0][1] * m_values[0][1]));
        result.m_y = Radians(Math::ATan2(-m_values[0][2], c2));

        float const s1 = Math::Sin((float)result.m_x);
        float const c1 = Math::Cos((float)result.m_x);
        result.m_z = Radians(Math::ATan2((s1 * m_values[2][0]) - (c1 * m_values[1][0]), (c1 * m_values[1][1]) - (s1 * m_values[2][1])));

        return result;
    }

    bool Matrix::Decompose(Quaternion& outRotation, Vector& outTranslation, Vector& outScale) const
    {
        Matrix copy = *this;
        Vector shr = Vector::Zero;
        outScale = Vector::Zero;

        // Extract and remove scale and shear from matrix
        if (ExtractAndRemoveScalingAndShear(copy, outScale, shr))
        {
            // Extract rotation and translation from unscaled matrix
            outRotation = copy.GetRotation();
            outTranslation = copy.GetTranslation().GetWithW0();
            return true;
        }

        return false;
    }

    Vector Matrix::GetScale() const
    {
        Matrix copy = *this;
        Vector scale = Vector::Zero, shear;
        if (!ExtractAndRemoveScalingAndShear(copy, scale, shear))
        {
            float const lengthX = m_rows[0].Length3().ToFloat();
            float const lengthY = m_rows[1].Length3().ToFloat();
            float const lengthZ = m_rows[2].Length3().ToFloat();
            scale = Vector(lengthX, lengthY, lengthZ, 0.0f);
        }

        return scale;
    }

    Matrix& Matrix::SetScale(const Vector& newScale)
    {
        Vector scale, shear;
        bool result = ExtractAndRemoveScalingAndShear(*this, scale, shear);

        // Cannot set scale on matrix that contains zero-scale
        ASSERT(result);

        m_rows[0] = m_rows[0] * newScale.GetSplatX();
        m_rows[1] = m_rows[1] * newScale.GetSplatY();
        m_rows[2] = m_rows[2] * newScale.GetSplatZ();
        return *this;
    }

    Matrix& Matrix::RemoveScale()
    {
        Vector scale, shear;
        bool result = ExtractAndRemoveScalingAndShear(*this, scale, shear);

        // Cannot remove zero scale from matrix
        ASSERT(result);

        return *this;
    }
}