File: Wm5Transform.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 90,124 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 40
file content (394 lines) | stat: -rw-r--r-- 12,857 bytes parent folder | download | duplicates (3)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.1 (2010/04/14)

#include "Wm5GraphicsPCH.h"
#include "Wm5Transform.h"
using namespace Wm5;

const Transform Transform::IDENTITY;

//----------------------------------------------------------------------------
Transform::Transform ()
    :
    mHMatrix(false),  // initialize to the identity matrix
    mInvHMatrix(false),  // initialize to the identity matrix
    mMatrix(false),  // initialize to the identity matrix
    mTranslate(0.0f, 0.0f, 0.0f),
    mScale(1.0f, 1.0f, 1.0f),
    mIsIdentity(true),
    mIsRSMatrix(true),
    mIsUniformScale(true),
    mInverseNeedsUpdate(false)
{
}
//----------------------------------------------------------------------------
Transform::~Transform ()
{
}
//----------------------------------------------------------------------------
void Transform::MakeIdentity ()
{
    mMatrix = HMatrix::IDENTITY;
    mTranslate = APoint(0.0f, 0.0f, 0.0f);
    mScale = APoint(1.0f, 1.0f, 1.0f);
    mIsIdentity = true;
    mIsRSMatrix = true;
    mIsUniformScale = true;
    UpdateHMatrix();
}
//----------------------------------------------------------------------------
void Transform::MakeUnitScale ()
{
    assertion(mIsRSMatrix, "Matrix is not a rotation\n");

    mScale = APoint(1.0f, 1.0f, 1.0f);
    mIsUniformScale = true;
    UpdateHMatrix();
}
//----------------------------------------------------------------------------
void Transform::SetRotate (const HMatrix& rotate)
{
    mMatrix = rotate;
    mIsIdentity = false;
    mIsRSMatrix = true;
    UpdateHMatrix();
}
//----------------------------------------------------------------------------
void Transform::SetMatrix (const HMatrix& matrix)
{
    mMatrix = matrix;
    mIsIdentity = false;
    mIsRSMatrix = false;
    mIsUniformScale = false;
    UpdateHMatrix();
}
//----------------------------------------------------------------------------
void Transform::SetTranslate (const APoint& translate)
{
    mTranslate = translate;
    mIsIdentity = false;
    UpdateHMatrix();
}
//----------------------------------------------------------------------------
void Transform::SetScale (const APoint& scale)
{
    assertion(mIsRSMatrix, "Matrix is not a rotation\n");
    assertion(scale[0] != 0.0f && scale[1] != 0.0f && scale[2] != 0.0f,
        "Scales must be nonzero\n");

    mScale = scale;
    mIsIdentity = false;
    mIsUniformScale = false;
    UpdateHMatrix();
}
//----------------------------------------------------------------------------
void Transform::SetUniformScale (float scale)
{
    assertion(mIsRSMatrix, "Matrix is not a rotation\n");
    assertion(scale != 0.0f, "Scale must be nonzero\n");

    mScale = APoint(scale, scale, scale);
    mIsIdentity = false;
    mIsUniformScale = true;
    UpdateHMatrix();
}
//----------------------------------------------------------------------------
float Transform::GetNorm () const
{
    if (mIsRSMatrix)
    {
        float maxValue = Mathf::FAbs(mScale[0]);
        if (Mathf::FAbs(mScale[1]) > maxValue)
        {
            maxValue = Mathf::FAbs(mScale[1]);
        }
        if (Mathf::FAbs(mScale[2]) > maxValue)
        {
            maxValue = Mathf::FAbs(mScale[2]);
        }
        return maxValue;
    }

    // A general matrix.  Use the max-row-sum matrix norm.  The spectral
    // norm (the maximum absolute value of the eigenvalues) is smaller or
    // equal to this norm.  Therefore, this function returns an approximation
    // to the maximum scale.
    float maxRowSum =
        Mathf::FAbs(mMatrix[0][0]) +
        Mathf::FAbs(mMatrix[0][1]) +
        Mathf::FAbs(mMatrix[0][2]);

    float rowSum =
        Mathf::FAbs(mMatrix[1][0]) +
        Mathf::FAbs(mMatrix[1][1]) +
        Mathf::FAbs(mMatrix[1][2]);

    if (rowSum > maxRowSum)
    {
        maxRowSum = rowSum;
    }

    rowSum =
        Mathf::FAbs(mMatrix[2][0]) +
        Mathf::FAbs(mMatrix[2][1]) +
        Mathf::FAbs(mMatrix[2][2]);

    if (rowSum > maxRowSum)
    {
        maxRowSum = rowSum;
    }

    return maxRowSum;
}
//----------------------------------------------------------------------------
Transform Transform::operator* (const Transform& transform) const
{
    if (IsIdentity())
    {
        return transform;
    }

    if (transform.IsIdentity())
    {
        return *this;
    }

    Transform product;

    if (mIsRSMatrix && transform.mIsRSMatrix)
    {
        if (mIsUniformScale)
        {
            product.SetRotate(mMatrix*transform.mMatrix);

            product.SetTranslate(GetUniformScale()*(
                mMatrix*transform.mTranslate) + mTranslate);

            if (transform.IsUniformScale())
            {
                product.SetUniformScale(
                    GetUniformScale()*transform.GetUniformScale());
            }
            else
            {
                product.SetScale(GetUniformScale()*transform.GetScale());
            }

            return product;
        }
    }

    // In all remaining cases, the matrix cannot be written as R*S*X+T.
    HMatrix matMA = (mIsRSMatrix ? mMatrix.TimesDiagonal(mScale) : mMatrix);
    HMatrix matMB = (transform.mIsRSMatrix ?
        transform.mMatrix.TimesDiagonal(transform.mScale) :
        transform.mMatrix);

    product.SetMatrix(matMA*matMB);
    product.SetTranslate(matMA*transform.mTranslate + mTranslate);
    return product;
}
//----------------------------------------------------------------------------
const HMatrix& Transform::Inverse () const
{
    if (mInverseNeedsUpdate)
    {
        if (mIsIdentity)
        {
            mInvHMatrix = HMatrix::IDENTITY;
        }
        else
        {
            if (mIsRSMatrix)
            {
                if (mIsUniformScale)
                {
                    float invScale = 1.0f/mScale[0];
                    mInvHMatrix[0][0] = invScale*mMatrix[0][0];
                    mInvHMatrix[0][1] = invScale*mMatrix[1][0];
                    mInvHMatrix[0][2] = invScale*mMatrix[2][0];
                    mInvHMatrix[1][0] = invScale*mMatrix[0][1];
                    mInvHMatrix[1][1] = invScale*mMatrix[1][1];
                    mInvHMatrix[1][2] = invScale*mMatrix[2][1];
                    mInvHMatrix[2][0] = invScale*mMatrix[0][2];
                    mInvHMatrix[2][1] = invScale*mMatrix[1][2];
                    mInvHMatrix[2][2] = invScale*mMatrix[2][2];
                }
                else
                {
                    // Replace 3 reciprocals by 6 multiplies and 1 reciprocal.
                    float s01 = mScale[0]*mScale[1];
                    float s02 = mScale[0]*mScale[2];
                    float s12 = mScale[1]*mScale[2];
                    float invs012 = 1.0f/(s01*mScale[2]);
                    float invS0 = s12*invs012;
                    float invS1 = s02*invs012;
                    float invS2 = s01*invs012;
                    mInvHMatrix[0][0] = invS0*mMatrix[0][0];
                    mInvHMatrix[0][1] = invS0*mMatrix[1][0];
                    mInvHMatrix[0][2] = invS0*mMatrix[2][0];
                    mInvHMatrix[1][0] = invS1*mMatrix[0][1];
                    mInvHMatrix[1][1] = invS1*mMatrix[1][1];
                    mInvHMatrix[1][2] = invS1*mMatrix[2][1];
                    mInvHMatrix[2][0] = invS2*mMatrix[0][2];
                    mInvHMatrix[2][1] = invS2*mMatrix[1][2];
                    mInvHMatrix[2][2] = invS2*mMatrix[2][2];
                }
            }
            else
            {
                Invert3x3(mHMatrix, mInvHMatrix);
            }

            mInvHMatrix[0][3] = -(
                mInvHMatrix[0][0]*mTranslate[0] +
                mInvHMatrix[0][1]*mTranslate[1] +
                mInvHMatrix[0][2]*mTranslate[2]
            );

            mInvHMatrix[1][3] = -(
                mInvHMatrix[1][0]*mTranslate[0] +
                mInvHMatrix[1][1]*mTranslate[1] +
                mInvHMatrix[1][2]*mTranslate[2]
            );

            mInvHMatrix[2][3] = -(
                mInvHMatrix[2][0]*mTranslate[0] +
                mInvHMatrix[2][1]*mTranslate[1] +
                mInvHMatrix[2][2]*mTranslate[2]
            );

            // The last row of mHMatrix is always (0,0,0,1) for an affine
            // transformation, so it is set once in the constructor.  It is
            // not necessary to reset it here.
        }

        mInverseNeedsUpdate = false;
    }

    return mInvHMatrix;
}
//----------------------------------------------------------------------------
Transform Transform::InverseTransform () const
{
    if (mIsIdentity)
    {
        return IDENTITY;
    }

    Transform inverse;
    APoint invTrn;
    if (mIsRSMatrix)
    {
        HMatrix invRot = mMatrix.Transpose();
        inverse.SetRotate(invRot);
        if (mIsUniformScale)
        {
            float invScale = 1.0f/mScale[0];
            inverse.SetUniformScale(invScale);
            invTrn = -invScale*(invRot*mTranslate);
        }
        else
        {
            APoint invScale(1.0f/mScale[0], 1.0f/mScale[1], 1.0f/mScale[2]);
            inverse.SetScale(invScale);
            invTrn = invRot*mTranslate;
            invTrn[0] *= -invScale[0];
            invTrn[1] *= -invScale[1];
            invTrn[2] *= -invScale[2];
        }
    }
    else
    {
        HMatrix invMat;
        Invert3x3(mMatrix, invMat);
        inverse.SetMatrix(invMat);
        invTrn = -(invMat*mTranslate);
    }
    inverse.SetTranslate(invTrn);

    return inverse;
}
//----------------------------------------------------------------------------
void Transform::UpdateHMatrix ()
{
    if (mIsIdentity)
    {
        mHMatrix = HMatrix::IDENTITY;
    }
    else
    {
        if (mIsRSMatrix)
        {
            mHMatrix[0][0] = mMatrix[0][0]*mScale[0];
            mHMatrix[0][1] = mMatrix[0][1]*mScale[1];
            mHMatrix[0][2] = mMatrix[0][2]*mScale[2];
            mHMatrix[1][0] = mMatrix[1][0]*mScale[0];
            mHMatrix[1][1] = mMatrix[1][1]*mScale[1];
            mHMatrix[1][2] = mMatrix[1][2]*mScale[2];
            mHMatrix[2][0] = mMatrix[2][0]*mScale[0];
            mHMatrix[2][1] = mMatrix[2][1]*mScale[1];
            mHMatrix[2][2] = mMatrix[2][2]*mScale[2];
        }
        else
        {
            mHMatrix[0][0] = mMatrix[0][0];
            mHMatrix[0][1] = mMatrix[0][1];
            mHMatrix[0][2] = mMatrix[0][2];
            mHMatrix[1][0] = mMatrix[1][0];
            mHMatrix[1][1] = mMatrix[1][1];
            mHMatrix[1][2] = mMatrix[1][2];
            mHMatrix[2][0] = mMatrix[2][0];
            mHMatrix[2][1] = mMatrix[2][1];
            mHMatrix[2][2] = mMatrix[2][2];
        }

        mHMatrix[0][3] = mTranslate[0];
        mHMatrix[1][3] = mTranslate[1];
        mHMatrix[2][3] = mTranslate[2];

        // The last row of mHMatrix is always (0,0,0,1) for an affine
        // transformation, so it is set once in the constructor.  It is not
        // necessary to reset it here.
    }

    mInverseNeedsUpdate = true;
}
//----------------------------------------------------------------------------
void Transform::Invert3x3 (const HMatrix& mat, HMatrix& invMat)
{
    // Compute the adjoint of M (3x3).
    invMat[0][0] = mat[1][1]*mat[2][2] - mat[1][2]*mat[2][1];
    invMat[0][1] = mat[0][2]*mat[2][1] - mat[0][1]*mat[2][2];
    invMat[0][2] = mat[0][1]*mat[1][2] - mat[0][2]*mat[1][1];
    invMat[1][0] = mat[1][2]*mat[2][0] - mat[1][0]*mat[2][2];
    invMat[1][1] = mat[0][0]*mat[2][2] - mat[0][2]*mat[2][0];
    invMat[1][2] = mat[0][2]*mat[1][0] - mat[0][0]*mat[1][2];
    invMat[2][0] = mat[1][0]*mat[2][1] - mat[1][1]*mat[2][0];
    invMat[2][1] = mat[0][1]*mat[2][0] - mat[0][0]*mat[2][1];
    invMat[2][2] = mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0];

    // Compute the reciprocal of the determinant of M.
    float invDet = 1.0f/(
        mat[0][0]*invMat[0][0] +
        mat[0][1]*invMat[1][0] +
        mat[0][2]*invMat[2][0]
    );

    // inverse(M) = adjoint(M)/determinant(M).
    invMat[0][0] *= invDet;
    invMat[0][1] *= invDet;
    invMat[0][2] *= invDet;
    invMat[1][0] *= invDet;
    invMat[1][1] *= invDet;
    invMat[1][2] *= invDet;
    invMat[2][0] *= invDet;
    invMat[2][1] *= invDet;
    invMat[2][2] *= invDet;
}
//----------------------------------------------------------------------------