summaryrefslogtreecommitdiffstats
path: root/src/core/transforms/qtransform.cpp
blob: d41b87f796ab6806874a78f49ba8b6cf8194d646 (plain)
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qtransform.h"
#include "qtransform_p.h"

#include <Qt3DCore/qpropertyupdatedchange.h>

#include <Qt3DCore/private/qmath3d_p.h>

QT_BEGIN_NAMESPACE

namespace Qt3DCore {

QTransformPrivate::QTransformPrivate()
    : QComponentPrivate()
    , m_rotation()
    , m_scale(1.0f, 1.0f, 1.0f)
    , m_translation()
    , m_eulerRotationAngles()
    , m_matrixDirty(false)
{
    m_shareable = false;
}

QTransformPrivate::~QTransformPrivate()
{
}

/*!
    \qmltype Transform
    \inqmlmodule Qt3D.Core
    \inherits Component3D
    \instantiates Qt3DCore::QTransform
    \since 5.6
    \brief Used to perform transforms on meshes.

    The Transform component is not shareable between multiple Entity's.
    The transformation is held as vector3d scale, quaternion rotation and
    vector3d translation components. The transformations are applied to the
    mesh in that order. When Transform::matrix property is set, it is decomposed
    to these transform components and corresponding transform signals are emitted.

    Several helper functions are provided to set up the Transform;
    fromAxisAndAngle and fromAxesAndAngles can be used to set the rotation around
    specific axes, fromEulerAngles can be used to set the rotation based on euler
    angles and rotateAround can be used to rotate the object around specific point
    relative to local origin.
 */

/*!
    \qmlproperty matrix4x4 Transform::matrix

    Holds the matrix4x4 of the transform.
    \note When the matrix property is set, it is decomposed to translation, rotation and scale components.
 */

/*!
    \qmlproperty real Transform::rotationX

    Holds the x rotation of the transform as Euler angle.
 */

/*!
    \qmlproperty real Transform::rotationY

    Holds the y rotation of the transform as Euler angle.
 */

/*!
    \qmlproperty real Transform::rotationZ

    Holds the z rotation of the transform as Euler angle.
 */

/*!
    \qmlproperty vector3d Transform::scale3D

    Holds the scale of the transform as vector3d.
 */

/*!
    \qmlproperty real Transform::scale

    Holds the uniform scale of the transform. If the scale has been set with scale3D, holds
    the x value only.
 */

/*!
    \qmlproperty quaternion Transform::rotation

    Holds the rotation of the transform as quaternion.
 */

/*!
    \qmlproperty vector3d Transform::translation

    Holds the translation of the transform as vector3d.
 */

/*!
    \qmlproperty matrix4x4 QTransform::worldMatrix

    Holds the world transformation matrix for the transform. This assumes the
    Transform component is being referenced by an Entity. This makes it more
    convenient to identify when an Entity part of a subtree has been
    transformed in the world even though its local transformation might not
    have changed.

    \since 5.14
 */

/*!
    \qmlmethod quaternion Transform::fromAxisAndAngle(vector3d axis, real angle)
    Creates a quaternion from \a axis and \a angle.
    Returns the resulting quaternion.
 */

/*!
    \qmlmethod quaternion Transform::fromAxisAndAngle(real x, real y, real z, real angle)
    Creates a quaternion from \a x, \a y, \a z, and \a angle.
    Returns the resulting quaternion.
 */

/*!
    \qmlmethod quaternion Transform::fromAxesAndAngles(vector3d axis1, real angle1,
                                                       vector3d axis2, real angle2)
    Creates a quaternion from \a axis1, \a angle1, \a axis2, and \a angle2.
    Returns the resulting quaternion.
 */

/*!
    \qmlmethod quaternion Transform::fromAxesAndAngles(vector3d axis1, real angle1,
                                                       vector3d axis2, real angle2,
                                                       vector3d axis3, real angle3)
    Creates a quaternion from \a axis1, \a angle1, \a axis2, \a angle2, \a axis3, and \a angle3.
    Returns the resulting quaternion.
 */

/*!
    \qmlmethod quaternion Transform::fromEulerAngles(vector3d eulerAngles)
    Creates a quaternion from \a eulerAngles.
    Returns the resulting quaternion.
 */

/*!
    \qmlmethod quaternion Transform::fromEulerAngles(real pitch, real yaw, real roll)
    Creates a quaternion from \a pitch, \a yaw, and \a roll.
    Returns the resulting quaternion.
 */

/*!
    \qmlmethod matrix4x4 Transform::rotateAround(vector3d point, real angle, vector3d axis)
    Creates a rotation matrix from \a axis and \a angle around \a point relative to local origin.
    Returns the resulting matrix4x4.
 */

/*!
    \class Qt3DCore::QTransform
    \inmodule Qt3DCore
    \inherits Qt3DCore::QComponent
    \since 5.6
    \brief Used to perform transforms on meshes.

    The QTransform component is not shareable between multiple QEntity's.
    The transformation is held as QVector3D scale, QQuaternion rotation and
    QVector3D translation components. The transformations are applied to the
    mesh in that order. When QTransform::matrix property is set, it is decomposed
    to these transform components and corresponding signals are emitted.

    Several helper functions are provided to set up the QTransform;
    fromAxisAndAngle and fromAxesAndAngles can be used to set the rotation around
    specific axes, fromEulerAngles can be used to set the rotation based on euler
    angles and rotateAround can be used to rotate the object around specific point
    relative to local origin.
 */

/*!
    Constructs a new QTransform with \a parent.
 */
QTransform::QTransform(QNode *parent)
    : QComponent(*new QTransformPrivate, parent)
{
}

/*!
    \internal
 */
QTransform::~QTransform()
{
}

/*!
    \internal
 */
QTransform::QTransform(QTransformPrivate &dd, QNode *parent)
    : QComponent(dd, parent)
{
}

/*!
    \internal
 */
// TODO Unused remove in Qt6
void QTransform::sceneChangeEvent(const QSceneChangePtr &change)
{
    Q_D(QTransform);
    switch (change->type()) {
    case PropertyUpdated: {
        Qt3DCore::QPropertyUpdatedChangePtr propertyChange = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(change);
        if (propertyChange->propertyName() == QByteArrayLiteral("worldMatrix")) {
            const bool blocked = blockNotifications(true);
            d->setWorldMatrix(propertyChange->value().value<QMatrix4x4>());
            blockNotifications(blocked);
        }
        break;
    }
    default:
        break;
    }
}

void QTransformPrivate::setWorldMatrix(const QMatrix4x4 &worldMatrix)
{
    Q_Q(QTransform);
    if (m_worldMatrix == worldMatrix)
        return;
    m_worldMatrix = worldMatrix;
    emit q->worldMatrixChanged(worldMatrix);
}

void QTransform::setMatrix(const QMatrix4x4 &m)
{
    Q_D(QTransform);
    if (m != matrix()) {
        d->m_matrix = m;
        d->m_matrixDirty = false;

        QVector3D s;
        QVector3D t;
        QQuaternion r;
        decomposeQMatrix4x4(m, t, r, s);
        d->m_scale = s;
        d->m_rotation = r;
        d->m_translation = t;
        d->m_eulerRotationAngles = d->m_rotation.toEulerAngles();
        emit scale3DChanged(s);
        emit rotationChanged(r);
        emit translationChanged(t);
        const bool wasBlocked = blockNotifications(true);
        emit matrixChanged();
        emit scaleChanged(d->m_scale.x());
        emit rotationXChanged(d->m_eulerRotationAngles.x());
        emit rotationYChanged(d->m_eulerRotationAngles.y());
        emit rotationZChanged(d->m_eulerRotationAngles.z());
        blockNotifications(wasBlocked);
    }
}

void QTransform::setRotationX(float rotationX)
{
    Q_D(QTransform);

    if (d->m_eulerRotationAngles.x() == rotationX)
        return;

    d->m_eulerRotationAngles.setX(rotationX);
    QQuaternion rotation = QQuaternion::fromEulerAngles(d->m_eulerRotationAngles);
    if (rotation != d->m_rotation) {
        d->m_rotation = rotation;
        d->m_matrixDirty = true;
        emit rotationChanged(rotation);
    }

    const bool wasBlocked = blockNotifications(true);
    emit rotationXChanged(rotationX);
    emit matrixChanged();
    blockNotifications(wasBlocked);
}

void QTransform::setRotationY(float rotationY)
{
    Q_D(QTransform);

    if (d->m_eulerRotationAngles.y() == rotationY)
        return;

    d->m_eulerRotationAngles.setY(rotationY);
    QQuaternion rotation = QQuaternion::fromEulerAngles(d->m_eulerRotationAngles);
    if (rotation != d->m_rotation) {
        d->m_rotation = rotation;
        d->m_matrixDirty = true;
        emit rotationChanged(rotation);
    }

    const bool wasBlocked = blockNotifications(true);
    emit rotationYChanged(rotationY);
    emit matrixChanged();
    blockNotifications(wasBlocked);
}

void QTransform::setRotationZ(float rotationZ)
{
    Q_D(QTransform);
    if (d->m_eulerRotationAngles.z() == rotationZ)
        return;

    d->m_eulerRotationAngles.setZ(rotationZ);
    QQuaternion rotation = QQuaternion::fromEulerAngles(d->m_eulerRotationAngles);
    if (rotation != d->m_rotation) {
        d->m_rotation = rotation;
        d->m_matrixDirty = true;
        emit rotationChanged(rotation);
    }

    const bool wasBlocked = blockNotifications(true);
    emit rotationZChanged(rotationZ);
    emit matrixChanged();
    blockNotifications(wasBlocked);
}

/*!
    \property Qt3DCore::QTransform::matrix

    Holds the QMatrix4x4 of the transform.
    \note When the matrix property is set, it is decomposed to translation, rotation and scale components.
 */
QMatrix4x4 QTransform::matrix() const
{
    Q_D(const QTransform);
    if (d->m_matrixDirty) {
        composeQMatrix4x4(d->m_translation, d->m_rotation, d->m_scale, d->m_matrix);
        d->m_matrixDirty = false;
    }
    return d->m_matrix;
}

/*!
    \property QTransform::worldMatrix

    Holds the world transformation matrix for the transform. This assumes the
    QTransform component is being referenced by a QEntity. This makes it more
    convenient to identify when a QEntity part of a subtree has been
    transformed in the world even though its local transformation might not
    have changed.

    \since 5.14
 */

/*!
    Returns the world transformation matrix associated to the QTransform when
    referenced by a QEntity which may be part of a QEntity hierarchy.

    \since 5.14
 */
QMatrix4x4 QTransform::worldMatrix() const
{
    Q_D(const QTransform);
    return d->m_worldMatrix;
}

/*!
    \property Qt3DCore::QTransform::rotationX

    Holds the x rotation of the transform as Euler angle.
 */
float QTransform::rotationX() const
{
    Q_D(const QTransform);
    return d->m_eulerRotationAngles.x();
}

/*!
    \property Qt3DCore::QTransform::rotationY

    Holds the y rotation of the transform as Euler angle.
 */
float QTransform::rotationY() const
{
    Q_D(const QTransform);
    return d->m_eulerRotationAngles.y();
}

/*!
    \property Qt3DCore::QTransform::rotationZ

    Holds the z rotation of the transform as Euler angle.
 */
float QTransform::rotationZ() const
{
    Q_D(const QTransform);
    return d->m_eulerRotationAngles.z();
}

void QTransform::setScale3D(const QVector3D &scale)
{
    Q_D(QTransform);
    if (scale != d->m_scale) {
        d->m_scale = scale;
        d->m_matrixDirty = true;
        emit scale3DChanged(scale);

        const bool wasBlocked = blockNotifications(true);
        emit matrixChanged();
        blockNotifications(wasBlocked);
    }
}

/*!
    \property Qt3DCore::QTransform::scale3D

    Holds the scale of the transform as QVector3D.
 */
QVector3D QTransform::scale3D() const
{
    Q_D(const QTransform);
    return d->m_scale;
}

void QTransform::setScale(float scale)
{
    Q_D(QTransform);
    if (scale != d->m_scale.x()) {
        setScale3D(QVector3D(scale, scale, scale));

        const bool wasBlocked = blockNotifications(true);
        emit scaleChanged(scale);
        blockNotifications(wasBlocked);
    }
}

/*!
    \property Qt3DCore::QTransform::scale

    Holds the uniform scale of the transform. If the scale has been set with setScale3D, holds
    the x value only.
 */
float QTransform::scale() const
{
    Q_D(const QTransform);
    return d->m_scale.x();
}

void QTransform::setRotation(const QQuaternion &rotation)
{
    Q_D(QTransform);
    if (rotation != d->m_rotation) {
        d->m_rotation = rotation;
        const QVector3D oldRotation = d->m_eulerRotationAngles;
        d->m_eulerRotationAngles = d->m_rotation.toEulerAngles();
        d->m_matrixDirty = true;
        emit rotationChanged(rotation);

        const bool wasBlocked = blockNotifications(true);
        emit matrixChanged();
        if (d->m_eulerRotationAngles.x() != oldRotation.x())
            emit rotationXChanged(d->m_eulerRotationAngles.x());
        if (d->m_eulerRotationAngles.y() != oldRotation.y())
            emit rotationYChanged(d->m_eulerRotationAngles.y());
        if (d->m_eulerRotationAngles.z() != oldRotation.z())
            emit rotationZChanged(d->m_eulerRotationAngles.z());
        blockNotifications(wasBlocked);
    }
}

/*!
    \property Qt3DCore::QTransform::rotation

    Holds the rotation of the transform as QQuaternion.
 */
QQuaternion QTransform::rotation() const
{
    Q_D(const QTransform);
    return d->m_rotation;
}

void QTransform::setTranslation(const QVector3D &translation)
{
    Q_D(QTransform);
    if (translation != d->m_translation) {
        d->m_translation = translation;
        d->m_matrixDirty = true;
        emit translationChanged(translation);

        const bool wasBlocked = blockNotifications(true);
        emit matrixChanged();
        blockNotifications(wasBlocked);
    }
}

/*!
    \property Qt3DCore::QTransform::translation

    Holds the translation of the transform as QVector3D.
 */
QVector3D QTransform::translation() const
{
    Q_D(const QTransform);
    return d->m_translation;
}

/*!
    Creates a QQuaternion from \a axis and \a angle.
    Returns the resulting QQuaternion.
 */
QQuaternion QTransform::fromAxisAndAngle(const QVector3D &axis, float angle)
{
    return QQuaternion::fromAxisAndAngle(axis, angle);
}

/*!
    Creates a QQuaternion from \a x, \a y, \a z, and \a angle.
    Returns the resulting QQuaternion.
 */
QQuaternion QTransform::fromAxisAndAngle(float x, float y, float z, float angle)
{
    return QQuaternion::fromAxisAndAngle(x, y, z, angle);
}

/*!
    Creates a QQuaternion from \a axis1, \a angle1, \a axis2, and \a angle2.
    Returns the resulting QQuaternion.
 */
QQuaternion QTransform::fromAxesAndAngles(const QVector3D &axis1, float angle1,
                                          const QVector3D &axis2, float angle2)
{
    const QQuaternion q1 = QQuaternion::fromAxisAndAngle(axis1, angle1);
    const QQuaternion q2 = QQuaternion::fromAxisAndAngle(axis2, angle2);
    return q2 * q1;
}

/*!
    Creates a QQuaternion from \a axis1, \a angle1, \a axis2, \a angle2, \a axis3, and \a angle3.
    Returns the resulting QQuaternion.
 */
QQuaternion QTransform::fromAxesAndAngles(const QVector3D &axis1, float angle1,
                                          const QVector3D &axis2, float angle2,
                                          const QVector3D &axis3, float angle3)
{
    const QQuaternion q1 = QQuaternion::fromAxisAndAngle(axis1, angle1);
    const QQuaternion q2 = QQuaternion::fromAxisAndAngle(axis2, angle2);
    const QQuaternion q3 = QQuaternion::fromAxisAndAngle(axis3, angle3);
    return q3 * q2 * q1;
}

/*!
    Creates a QQuaterniom definining a rotation from the axes \a xAxis, \a yAxis and \a zAxis.
    \since 5.11
 */
QQuaternion QTransform::fromAxes(const QVector3D &xAxis, const QVector3D &yAxis, const QVector3D &zAxis)
{
    return QQuaternion::fromAxes(xAxis, yAxis, zAxis);
}

/*!
    Creates a QQuaternion from \a eulerAngles.
    Returns the resulting QQuaternion.
 */
QQuaternion QTransform::fromEulerAngles(const QVector3D &eulerAngles)
{
    return QQuaternion::fromEulerAngles(eulerAngles);
}

/*!
    Creates a QQuaternion from \a pitch, \a yaw, and \a roll.
    Returns the resulting QQuaternion.
 */
QQuaternion QTransform::fromEulerAngles(float pitch, float yaw, float roll)
{
    return QQuaternion::fromEulerAngles(pitch, yaw, roll);
}

/*!
    Creates a rotation matrix from \a axis and \a angle around \a point.
    Returns the resulting QMatrix4x4.
 */
QMatrix4x4 QTransform::rotateAround(const QVector3D &point, float angle, const QVector3D &axis)
{
    QMatrix4x4 m;
    m.translate(point);
    m.rotate(angle, axis);
    m.translate(-point);
    return m;
}

/*!
    Returns a rotation matrix defined from the axes \a xAxis, \a yAxis, \a zAxis.
    \since 5.11
 */
QMatrix4x4 QTransform::rotateFromAxes(const QVector3D &xAxis, const QVector3D &yAxis, const QVector3D &zAxis)
{
    return QMatrix4x4(xAxis.x(), yAxis.x(), zAxis.x(), 0.0f,
                      xAxis.y(), yAxis.y(), zAxis.y(), 0.0f,
                      xAxis.z(), yAxis.z(), zAxis.z(), 0.0f,
                      0.0f, 0.0f, 0.0f, 1.0f);
}

QNodeCreatedChangeBasePtr QTransform::createNodeCreationChange() const
{
    auto creationChange = QNodeCreatedChangePtr<QTransformData>::create(this);
    auto &data = creationChange->data;

    Q_D(const QTransform);
    data.rotation = d->m_rotation;
    data.scale = d->m_scale;
    data.translation = d->m_translation;

    return creationChange;
}

} // namespace Qt3DCore

QT_END_NAMESPACE

#include "moc_qtransform.cpp"