/**************************************************************************** ** ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of the QtGui module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** No Commercial Usage ** This file contains pre-release code and may not be distributed. ** You may use this file in accordance with the terms and conditions ** contained in the either Technology Preview License Agreement or the ** Beta Release License Agreement. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain ** additional rights. These rights are described in the Nokia Qt LGPL ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this ** package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** If you are unsure which license is appropriate for your use, please ** contact the sales department at http://www.qtsoftware.com/contact. ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QGENERICMATRIX_H #define QGENERICMATRIX_H #include #include QT_BEGIN_HEADER QT_BEGIN_NAMESPACE QT_MODULE(Gui) template class QGenericMatrix { public: QGenericMatrix(); QGenericMatrix(const QGenericMatrix& other); explicit QGenericMatrix(const T *values); T operator()(int row, int column) const; InnerT& operator()(int row, int column); bool isIdentity() const; void setIdentity(); void fill(T value); QGenericMatrix transposed() const; 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 toValueArray(T *values); InnerT *data() { return m[0]; } const InnerT *data() const { return m[0]; } const InnerT *constData() const { return m[0]; } #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 InnerT m[N][M]; // Column-major order to match OpenGL. QGenericMatrix(int) {} // Construct without initializing identity matrix. #if !defined(Q_NO_TEMPLATE_FRIENDS) template friend class QGenericMatrix; #endif }; template Q_INLINE_TEMPLATE QGenericMatrix::QGenericMatrix() { setIdentity(); } template Q_INLINE_TEMPLATE QGenericMatrix::QGenericMatrix(const QGenericMatrix& other) { for (int col = 0; col < N; ++col) for (int row = 0; row < M; ++row) m[col][row] = other.m[col][row]; } 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 T QGenericMatrix::operator()(int row, int column) const { Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N); return T(m[column][row]); } template Q_INLINE_TEMPLATE InnerT& 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::setIdentity() { 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(1); 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 index = 0; index < N * M; ++index) m[0][index] += other.m[0][index]; return *this; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix& QGenericMatrix::operator-=(const QGenericMatrix& other) { for (int index = 0; index < N * M; ++index) m[0][index] -= other.m[0][index]; return *this; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix& QGenericMatrix::operator*=(T factor) { InnerT f(factor); for (int index = 0; index < N * M; ++index) m[0][index] *= f; return *this; } template Q_OUTOFLINE_TEMPLATE bool QGenericMatrix::operator==(const QGenericMatrix& other) const { for (int index = 0; index < N * M; ++index) { if (m[0][index] != other.m[0][index]) return false; } return true; } template Q_OUTOFLINE_TEMPLATE bool QGenericMatrix::operator!=(const QGenericMatrix& other) const { for (int index = 0; index < N * M; ++index) { if (m[0][index] != other.m[0][index]) return true; } return false; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix& QGenericMatrix::operator/=(T divisor) { InnerT d(divisor); for (int index = 0; index < N * M; ++index) m[0][index] /= d; return *this; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator+(const QGenericMatrix& m1, const QGenericMatrix& m2) { QGenericMatrix result(1); for (int index = 0; index < N * M; ++index) result.m[0][index] = m1.m[0][index] + m2.m[0][index]; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator-(const QGenericMatrix& m1, const QGenericMatrix& m2) { QGenericMatrix result(1); for (int index = 0; index < N * M; ++index) result.m[0][index] = m1.m[0][index] - m2.m[0][index]; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator*(const QGenericMatrix& m1, const QGenericMatrix& m2) { QGenericMatrix result(1); for (int row = 0; row < M2; ++row) { for (int col = 0; col < M1; ++col) { InnerT 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(1); for (int index = 0; index < N * M; ++index) result.m[0][index] = -matrix.m[0][index]; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator*(T factor, const QGenericMatrix& matrix) { InnerT f(factor); QGenericMatrix result(1); for (int index = 0; index < N * M; ++index) result.m[0][index] = matrix.m[0][index] * f; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator*(const QGenericMatrix& matrix, T factor) { InnerT f(factor); QGenericMatrix result(1); for (int index = 0; index < N * M; ++index) result.m[0][index] = matrix.m[0][index] * f; return result; } template Q_OUTOFLINE_TEMPLATE QGenericMatrix operator/(const QGenericMatrix& matrix, T divisor) { InnerT d(divisor); QGenericMatrix result(1); for (int index = 0; index < N * M; ++index) result.m[0][index] = matrix.m[0][index] / d; return result; } template Q_OUTOFLINE_TEMPLATE void QGenericMatrix::toValueArray(T *values) { 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, qreal, float> QMatrix2x2; typedef QGenericMatrix<2, 3, qreal, float> QMatrix2x3; typedef QGenericMatrix<2, 4, qreal, float> QMatrix2x4; typedef QGenericMatrix<3, 2, qreal, float> QMatrix3x2; typedef QGenericMatrix<3, 3, qreal, float> QMatrix3x3; typedef QGenericMatrix<3, 4, qreal, float> QMatrix3x4; typedef QGenericMatrix<4, 2, qreal, float> QMatrix4x2; typedef QGenericMatrix<4, 3, qreal, float> QMatrix4x3; #ifndef QT_NO_DEBUG_STREAM template QDebug operator<<(QDebug dbg, const QGenericMatrix &m) { dbg.nospace() << "QGenericMatrix<" << N << ", " << M << ", " << QTypeInfo::name() << ", " << 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.space(); } #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) QT_END_HEADER #endif