src/gui/math3d/qgenericmatrix.h
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    42 #ifndef QGENERICMATRIX_H
    42 #ifndef QGENERICMATRIX_H
    43 #define QGENERICMATRIX_H
    43 #define QGENERICMATRIX_H
    44 
    44 
    45 #include <QtCore/qmetatype.h>
    45 #include <QtCore/qmetatype.h>
    46 #include <QtCore/qdebug.h>
    46 #include <QtCore/qdebug.h>
       
    47 #include <QtCore/qdatastream.h>
    47 
    48 
    48 QT_BEGIN_HEADER
    49 QT_BEGIN_HEADER
    49 
    50 
    50 QT_BEGIN_NAMESPACE
    51 QT_BEGIN_NAMESPACE
    51 
    52 
    61 
    62 
    62     const T& operator()(int row, int column) const;
    63     const T& operator()(int row, int column) const;
    63     T& operator()(int row, int column);
    64     T& operator()(int row, int column);
    64 
    65 
    65     bool isIdentity() const;
    66     bool isIdentity() const;
    66     void setIdentity();
    67     void setToIdentity();
    67 
    68 
    68     void fill(T value);
    69     void fill(T value);
    69 
    70 
    70     QGenericMatrix<M, N, T> transposed() const;
    71     QGenericMatrix<M, N, T> transposed() const;
    71 
    72 
    74     QGenericMatrix<N, M, T>& operator*=(T factor);
    75     QGenericMatrix<N, M, T>& operator*=(T factor);
    75     QGenericMatrix<N, M, T>& operator/=(T divisor);
    76     QGenericMatrix<N, M, T>& operator/=(T divisor);
    76     bool operator==(const QGenericMatrix<N, M, T>& other) const;
    77     bool operator==(const QGenericMatrix<N, M, T>& other) const;
    77     bool operator!=(const QGenericMatrix<N, M, T>& other) const;
    78     bool operator!=(const QGenericMatrix<N, M, T>& other) const;
    78 
    79 
    79     void toValueArray(T *values);
    80     void copyDataTo(T *values) const;
    80 
    81 
    81     T *data() { return m[0]; }
    82     T *data() { return m[0]; }
    82     const T *data() const { return m[0]; }
    83     const T *data() const { return m[0]; }
    83     const T *constData() const { return m[0]; }
    84     const T *constData() const { return m[0]; }
    84 
    85 
   111 };
   112 };
   112 
   113 
   113 template <int N, int M, typename T>
   114 template <int N, int M, typename T>
   114 Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix()
   115 Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix()
   115 {
   116 {
   116     setIdentity();
   117     setToIdentity();
   117 }
   118 }
   118 
   119 
   119 template <int N, int M, typename T>
   120 template <int N, int M, typename T>
   120 Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const QGenericMatrix<N, M, T>& other)
   121 Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const QGenericMatrix<N, M, T>& other)
   121 {
   122 {
   162     }
   163     }
   163     return true;
   164     return true;
   164 }
   165 }
   165 
   166 
   166 template <int N, int M, typename T>
   167 template <int N, int M, typename T>
   167 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setIdentity()
   168 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setToIdentity()
   168 {
   169 {
   169     for (int col = 0; col < N; ++col) {
   170     for (int col = 0; col < N; ++col) {
   170         for (int row = 0; row < M; ++row) {
   171         for (int row = 0; row < M; ++row) {
   171             if (row == col)
   172             if (row == col)
   172                 m[col][row] = 1.0f;
   173                 m[col][row] = 1.0f;
   314         result.m[0][index] = matrix.m[0][index] / divisor;
   315         result.m[0][index] = matrix.m[0][index] / divisor;
   315     return result;
   316     return result;
   316 }
   317 }
   317 
   318 
   318 template <int N, int M, typename T>
   319 template <int N, int M, typename T>
   319 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::toValueArray(T *values)
   320 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::copyDataTo(T *values) const
   320 {
   321 {
   321     for (int col = 0; col < N; ++col)
   322     for (int col = 0; col < N; ++col)
   322         for (int row = 0; row < M; ++row)
   323         for (int row = 0; row < M; ++row)
   323             values[row * N + col] = T(m[col][row]);
   324             values[row * N + col] = T(m[col][row]);
   324 }
   325 }
   350     return dbg.space();
   351     return dbg.space();
   351 }
   352 }
   352 
   353 
   353 #endif
   354 #endif
   354 
   355 
       
   356 #ifndef QT_NO_DATASTREAM
       
   357 
       
   358 template <int N, int M, typename T>
       
   359 QDataStream &operator<<(QDataStream &stream, const QGenericMatrix<N, M, T> &matrix)
       
   360 {
       
   361     for (int row = 0; row < M; ++row)
       
   362         for (int col = 0; col < N; ++col)
       
   363             stream << double(matrix(row, col));
       
   364     return stream;
       
   365 }
       
   366 
       
   367 template <int N, int M, typename T>
       
   368 QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M, T> &matrix)
       
   369 {
       
   370     double x;
       
   371     for (int row = 0; row < M; ++row) {
       
   372         for (int col = 0; col < N; ++col) {
       
   373             stream >> x;
       
   374             matrix(row, col) = T(x);
       
   375         }
       
   376     }
       
   377     return stream;
       
   378 }
       
   379 
       
   380 #endif
       
   381 
   355 QT_END_NAMESPACE
   382 QT_END_NAMESPACE
   356 
   383 
   357 Q_DECLARE_METATYPE(QMatrix2x2)
   384 Q_DECLARE_METATYPE(QMatrix2x2)
   358 Q_DECLARE_METATYPE(QMatrix2x3)
   385 Q_DECLARE_METATYPE(QMatrix2x3)
   359 Q_DECLARE_METATYPE(QMatrix2x4)
   386 Q_DECLARE_METATYPE(QMatrix2x4)