/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtGui 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$ ** ****************************************************************************/ #ifndef QGENERICMATRIX_H #define QGENERICMATRIX_H #include #include #include #include QT_BEGIN_NAMESPACE template class QGenericMatrix { public: QGenericMatrix(); explicit QGenericMatrix(Qt::Initialization) {} explicit QGenericMatrix(const T *values); const T& operator()(int row, int column) const; T& operator()(int row, int column); bool isIdentity() const; void setToIdentity(); void fill(T value); QGenericMatrix transposed() const Q_REQUIRED_RESULT; QGenericMatrix& operator+=(const QGenericMatrix& other); QGenericMatrix& operator-=(const QGenericMatrix& other); QGenericMatrix& operator*=(T factor); QGenericMatrix& operator/=(T divisor); bool operator==(const QGenericMatrix& other) const; bool operator!=(const QGenericMatrix& other) const; void copyDataTo(T *values) const; T *data() { return *m; } const T *data() const { return *m; } const T *constData() const { return *m; } #if !defined(Q_NO_TEMPLATE_FRIENDS) template friend QGenericMatrix operator+(const QGenericMatrix& m1, const QGenericMatrix& m2); template friend QGenericMatrix operator-(const QGenericMatrix& m1, const QGenericMatrix& m2); template friend QGenericMatrix operator*(const QGenericMatrix& m1, const QGenericMatrix& m2); template friend QGenericMatrix operator-(const QGenericMatrix& matrix); template friend QGenericMatrix operator*(TT factor, const QGenericMatrix& matrix); template friend QGenericMatrix operator*(const QGenericMatrix& matrix, TT factor); template friend QGenericMatrix operator/(const QGenericMatrix& matrix, TT divisor); private: #endif T m[N][M]; // Column-major order to match OpenGL. #if !defined(Q_NO_TEMPLATE_FRIENDS) template friend class QGenericMatrix; #endif }; template class QTypeInfo > : public QTypeInfoMerger, T> { #if QT_VERSION < QT_VERSION_CHECK(6,0,0) public: enum { isStatic = true, }; // at least Q_RELOCATABLE_TYPE, for BC during Qt 5 #endif }; template Q_INLINE_TEMPLATE QGenericMatrix::QGenericMatrix() { setToIdentity(); } template Q_OUTOFLINE_TEMPLATE QGenericMatrix::QGenericMatrix(const T *values) { for (int col = 0; col < N; ++col) for (int row = 0; row < M; ++row) m[col][row] = values[row * N + col]; } template Q_INLINE_TEMPLATE const T& QGenericMatrix::operator()(int row, int column) const { Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N); return m[column][row]; } template Q_INLINE_TEMPLATE T& QGenericMatrix::operator()(int row, int column) { Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N); return m[column][row]; } template Q_OUTOFLINE_TEMPLATE bool QGenericMatrix::isIdentity() const { for (int col = 0; col < N; ++col) { for (int row = 0; row < M; ++row) { if (row == col) { if (m[col][row] != 1.0f) return false; } else { if (m[col][row] != 0.0f) return false; } } } return true; } template Q_OUTOFLINE_TEMPLATE void QGenericMatrix::setToIdentity() { for (int col = 0; col < N; ++col) { for (int row = 0; row < M; ++row) { if (row == col) m[col][row] = 1.0f; else m[col][row] = 0.0f; } } } template Q_OUTOFLINE_TEMPLATE void QGenericMatrix::fill(T value) { for (int col = 0; col < N; ++col) for (int row = 0; row < M; ++row) m[col][row] = value; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix QGenericMatrix::transposed() const { QGenericMatrix result(Qt::Uninitialized); for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) result.m[row][col] = m[col][row]; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix& QGenericMatrix::operator+=(const QGenericMatrix& other) { for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) m[col][row] += other.m[col][row]; return *this; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix& QGenericMatrix::operator-=(const QGenericMatrix& other) { for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) m[col][row] -= other.m[col][row]; return *this; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix& QGenericMatrix::operator*=(T factor) { for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) m[col][row] *= factor; return *this; } template Q_OUTOFLINE_TEMPLATE bool QGenericMatrix::operator==(const QGenericMatrix& other) const { for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) { if (m[col][row] != other.m[col][row]) return false; } return true; } template Q_OUTOFLINE_TEMPLATE bool QGenericMatrix::operator!=(const QGenericMatrix& other) const { for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) { if (m[col][row] != other.m[col][row]) return true; } return false; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix& QGenericMatrix::operator/=(T divisor) { for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) m[col][row] /= divisor; return *this; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator+(const QGenericMatrix& m1, const QGenericMatrix& m2) { QGenericMatrix result(Qt::Uninitialized); for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) result.m[col][row] = m1.m[col][row] + m2.m[col][row]; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator-(const QGenericMatrix& m1, const QGenericMatrix& m2) { QGenericMatrix result(Qt::Uninitialized); for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) result.m[col][row] = m1.m[col][row] - m2.m[col][row]; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator*(const QGenericMatrix& m1, const QGenericMatrix& m2) { QGenericMatrix result(Qt::Uninitialized); for (int row = 0; row < M2; ++row) { for (int col = 0; col < M1; ++col) { T sum(0.0f); for (int j = 0; j < N; ++j) sum += m1.m[j][row] * m2.m[col][j]; result.m[col][row] = sum; } } return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator-(const QGenericMatrix& matrix) { QGenericMatrix result(Qt::Uninitialized); for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) result.m[col][row] = -matrix.m[col][row]; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator*(T factor, const QGenericMatrix& matrix) { QGenericMatrix result(Qt::Uninitialized); for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) result.m[col][row] = matrix.m[col][row] * factor; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator*(const QGenericMatrix& matrix, T factor) { QGenericMatrix result(Qt::Uninitialized); for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) result.m[col][row] = matrix.m[col][row] * factor; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator/(const QGenericMatrix& matrix, T divisor) { QGenericMatrix result(Qt::Uninitialized); for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) result.m[col][row] = matrix.m[col][row] / divisor; return result; } template Q_OUTOFLINE_TEMPLATE void QGenericMatrix::copyDataTo(T *values) const { for (int col = 0; col < N; ++col) for (int row = 0; row < M; ++row) values[row * N + col] = T(m[col][row]); } // Define aliases for the useful variants of QGenericMatrix. typedef QGenericMatrix<2, 2, float> QMatrix2x2; typedef QGenericMatrix<2, 3, float> QMatrix2x3; typedef QGenericMatrix<2, 4, float> QMatrix2x4; typedef QGenericMatrix<3, 2, float> QMatrix3x2; typedef QGenericMatrix<3, 3, float> QMatrix3x3; typedef QGenericMatrix<3, 4, float> QMatrix3x4; typedef QGenericMatrix<4, 2, float> QMatrix4x2; typedef QGenericMatrix<4, 3, float> QMatrix4x3; #ifndef QT_NO_DEBUG_STREAM template QDebug operator<<(QDebug dbg, const QGenericMatrix &m) { QDebugStateSaver saver(dbg); dbg.nospace() << "QGenericMatrix<" << N << ", " << M << ", " << QTypeInfo::name() << ">(" << endl << qSetFieldWidth(10); for (int row = 0; row < M; ++row) { for (int col = 0; col < N; ++col) dbg << m(row, col); dbg << endl; } dbg << qSetFieldWidth(0) << ')'; return dbg; } #endif #ifndef QT_NO_DATASTREAM template QDataStream &operator<<(QDataStream &stream, const QGenericMatrix &matrix) { for (int row = 0; row < M; ++row) for (int col = 0; col < N; ++col) stream << double(matrix(row, col)); return stream; } template QDataStream &operator>>(QDataStream &stream, QGenericMatrix &matrix) { double x; for (int row = 0; row < M; ++row) { for (int col = 0; col < N; ++col) { stream >> x; matrix(row, col) = T(x); } } return stream; } #endif QT_END_NAMESPACE Q_DECLARE_METATYPE(QMatrix2x2) Q_DECLARE_METATYPE(QMatrix2x3) Q_DECLARE_METATYPE(QMatrix2x4) Q_DECLARE_METATYPE(QMatrix3x2) Q_DECLARE_METATYPE(QMatrix3x3) Q_DECLARE_METATYPE(QMatrix3x4) Q_DECLARE_METATYPE(QMatrix4x2) Q_DECLARE_METATYPE(QMatrix4x3) #endif