aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/util/qsgtransform_p.h
blob: f19d7a0efd0e3dea412a12bf6943d5a89aa614fc (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QSGTRANSFORM_P_H
#define QSGTRANSFORM_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of a number of Qt sources files.  This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//

#include <QSharedPointer>
#include <QMatrix4x4>
#include <QtQuick/qtquickexports.h>

QT_BEGIN_NAMESPACE

class Q_QUICK_EXPORT QSGTransform
{
public:
    void setMatrix(const QMatrix4x4 &matrix)
    {
        if (matrix.isIdentity())
            m_matrixPtr.clear();
        else
            m_matrixPtr = QSharedPointer<QMatrix4x4>::create(matrix);
        m_invertedPtr.clear();
    }

    QMatrix4x4 matrix() const
    {
        return m_matrixPtr ? *m_matrixPtr : m_identity;
    }

    bool isIdentity() const
    {
        return !m_matrixPtr;
    }

    bool operator==(const QMatrix4x4 &other) const
    {
        return m_matrixPtr ? (other == *m_matrixPtr) : other.isIdentity();
    }

    bool operator!=(const QMatrix4x4 &other) const
    {
        return !(*this == other);
    }

    bool operator==(const QSGTransform &other) const
    {
        return (m_matrixPtr == other.m_matrixPtr)
                || (m_matrixPtr && other.m_matrixPtr && *m_matrixPtr == *other.m_matrixPtr);
    }

    bool operator!=(const QSGTransform &other) const
    {
        return !(*this == other);
    }

    int compareTo(const QSGTransform &other) const
    {
        int diff = 0;
        if (m_matrixPtr != other.m_matrixPtr) {
            if (m_matrixPtr.isNull()) {
                diff = -1;
            } else if (other.m_matrixPtr.isNull()) {
                diff = 1;
            } else {
                const float *ptr1 = m_matrixPtr->constData();
                const float *ptr2 = other.m_matrixPtr->constData();
                for (int i = 0; i < 16 && !diff; i++) {
                    float d = ptr1[i] - ptr2[i];
                    if (d != 0)
                        diff = (d > 0) ? 1 : -1;
                }
            }
        }
        return diff;
    }

    const float *invertedData() const
    {
        if (!m_matrixPtr)
            return m_identity.constData();
        if (!m_invertedPtr)
            m_invertedPtr = QSharedPointer<QMatrix4x4>::create(m_matrixPtr->inverted());
        return m_invertedPtr->constData();
    }

private:
    static QMatrix4x4 m_identity;
    QSharedPointer<QMatrix4x4> m_matrixPtr;
    mutable QSharedPointer<QMatrix4x4> m_invertedPtr;
};

QT_END_NAMESPACE

#endif // QSGTRANSFORM_P_H