00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #ifndef QVMATRIX_H
00026 #define QVMATRIX_H
00027
00028 #include <math.h>
00029 #include <gsl/gsl_blas.h>
00030 #include <iostream>
00031 #include <QVQuaternion>
00032 #include <qvmath/qvblasdatabuffer.h>
00033
00034 #ifdef OPENCV
00035 #include <cv.h>
00036 #endif
00037
00044 class QVMatrix
00045 {
00046 public:
00047
00048
00053 QVMatrix();
00054
00059 QVMatrix(const QVMatrix &matrix);
00060
00068 QVMatrix(const int rows, const int cols, const double *data = NULL);
00069
00077 QVMatrix(const int rows, const int cols, const QVVector &data);
00078
00086 QVMatrix(const int rows, const int cols, const double value);
00087
00094 QVMatrix(const QVQuaternion &quaternion);
00095
00096 #ifdef OPENCV
00103 QVMatrix(const CvMat *cvMatrix);
00104
00115 CvMat *toCvMat(const int cvMatType = CV_64F) const;
00116
00122 operator CvMat *() const { return toCvMat(); }
00123 #endif
00124
00131 QVMatrix(const QVVector &vector, const bool rowVector = true);
00132
00138 QVMatrix(const QList<QVVector> &vectorList);
00139
00145 QVMatrix(const QList< QVector<double> > &vectorList);
00146
00150 QVMatrix(const gsl_matrix *matrix);
00151
00157 QVMatrix(const QList<QPointF> &pointList);
00158
00161
00162 operator QList<QVVector> () const
00163 {
00164 QList<QVVector> list;
00165 for (int n = 0; n < getRows(); n++)
00166 list.append(getRow(n));
00167 return list;
00168 }
00169
00172
00173 operator QList< QVector<double> > () const
00174 {
00175 QList< QVector<double> > list;
00176 for (int n = 0; n < getRows(); n++)
00177 list.append(getRow(n));
00178 return list;
00179 }
00180
00183
00184 operator gsl_matrix * () const
00185 {
00186 gsl_matrix *result = gsl_matrix_alloc(rows, cols);
00187 for(int i = 0; i < rows; i++)
00188 for(int j = 0; j < cols; j++)
00189 gsl_matrix_set(result, i, j, operator()(i, j));
00190 return result;
00191 }
00192
00196 QVMatrix & operator=(const QVMatrix &matrix);
00197
00198
00199
00205 bool operator==(const QVMatrix &matrix) const { return equals(matrix); };
00206
00212 bool operator!=(const QVMatrix &matrix) const { return !equals(matrix); };
00213
00218 QVMatrix operator*(const QVMatrix &matrix) const { return dotProduct(matrix); };
00219
00245 QVMatrix operator/(const QVMatrix &matrix) const { return matrixDivide(matrix); };
00246
00251 QVMatrix operator+(const QVMatrix &matrix) const { return addition(matrix); };
00252
00257 QVMatrix operator-(const QVMatrix &matrix) const { return substract(matrix); };
00258
00264 QVMatrix operator-() const { return operator*(-1); };
00265
00266
00267
00272 QVMatrix operator*(const double value) const { return scalarProduct(value); };
00273
00278 QVMatrix operator/(const double value) const { return scalarDivide(value); };
00279
00280
00281
00285 QVVector operator*(const QVVector &vector) const;
00286
00287
00288
00297 QVMatrix verticalAppend(const QVMatrix &matrix) const;
00298
00299
00308 QVMatrix horizontalAppend(const QVMatrix &matrix) const;
00309
00318 QVMatrix operator&(const QVMatrix &matrix) const { return verticalAppend(matrix); };
00319
00328 QVMatrix operator|(const QVMatrix &matrix) const { return horizontalAppend(matrix); };
00329
00338 QVMatrix operator&(const QVVector &vector) const { return verticalAppend(QVMatrix(vector)); };
00339
00348 QVMatrix operator|(const QVVector &vector) const { return horizontalAppend(QVMatrix(vector).transpose()); };
00349
00350
00351
00356 inline double &operator()(const int row, const int col)
00357 { return data->getWriteData()[row*cols + col]; }
00358
00363 inline const double operator()(const int row, const int col) const
00364 { return data->getReadData()[row*cols + col]; }
00365
00370 const int getDataSize() const { return cols*rows; }
00371
00375 const double *getReadData() const { return data->getReadData(); }
00376
00380 double *getWriteData() { return data->getWriteData(); }
00381
00385 QVMatrix transpose() const;
00386
00390 void set(const double value);
00391
00392
00393
00400 bool equals(const QVMatrix &matrix) const;
00401
00406 QVMatrix dotProduct(const QVMatrix &matrix) const;
00407
00414 QVMatrix elementProduct(const QVMatrix &matrix) const;
00415
00427 QVMatrix matrixDivide(const QVMatrix &matrix) const;
00428
00430 QVMatrix inverse() const;
00431
00433 double det() const;
00434
00436 QVVector meanCol() const;
00437
00442 QVMatrix addition(const QVMatrix &matrix) const;
00443
00448 QVMatrix substract(const QVMatrix &matrix) const;
00449
00450
00451
00456 QVMatrix scalarDivide(const double value) const;
00457
00462 QVMatrix scalarProduct(const double value) const;
00463
00472 double norm2() const;
00473
00482 double trace() const;
00483
00489 QVMatrix rowHomogeneousNormalize() const;
00490
00494 const int getCols() const { return cols; }
00495
00499 const int getRows() const { return rows; }
00500
00506 const QVMatrix getCols(const int firstCol, const int lastCol) const
00507 {
00508 Q_ASSERT(-1 < firstCol);
00509 Q_ASSERT(lastCol < getCols());
00510 Q_ASSERT(firstCol <= lastCol);
00511
00512 QVMatrix result(getRows(), lastCol - firstCol + 1);
00513 for (int i = 0; i < getRows(); i++)
00514 for (int j = firstCol; j <= lastCol; j++)
00515 result(i,j-firstCol) = operator()(i,j);
00516 return result;
00517 }
00518
00524 const QVMatrix getRows(const int firstRow, const int lastRow) const
00525 {
00526 Q_ASSERT(-1 < firstRow);
00527 Q_ASSERT(lastRow < getRows());
00528 Q_ASSERT(firstRow <= lastRow);
00529
00530 QVMatrix result(lastRow - firstRow + 1, getCols());
00531 for (int i = firstRow; i <= lastRow; i++)
00532 for (int j = 0; j < getCols(); j++)
00533 result(i-firstRow,j) = operator()(i,j);
00534 return result;
00535 }
00536
00537
00538
00543 const QVVector getRow(const int row) const;
00544
00549 void setRow(const int row, QVVector vector);
00550
00555 void setRow(const int row, QVector<double> vector);
00556
00561 const QVVector getCol(const int col) const;
00562
00567 void setCol(const int col, QVVector vector);
00568
00576 const QVMatrix getSubmatrix(const int firstRow, const int lastRow, const int firstCol, const int lastCol) const;
00577
00581 const QVVector diagonal() const { return QVVector(); };
00582
00583
00584
00588 static QVMatrix identity(const int size);
00589
00594 static QVMatrix zeros(const int rows, const int cols);
00595
00602 static QVMatrix random(const int rows, const int cols);
00603
00609 static QVMatrix diagonal(const QVVector &diagonalVector);
00610
00614 static QVMatrix rotationMatrix(const double angle);
00615
00620 static QVMatrix rotationMatrix(const QPointF center, const double angle);
00621
00626 static QVMatrix translationMatrix(const double x, const double y);
00627
00631 static QVMatrix scaleMatrix(const double zoom);
00632
00636 static QVMatrix rotationMatrix3dXAxis(const double angle);
00637
00641 static QVMatrix rotationMatrix3dYAxis(const double angle);
00642
00646 static QVMatrix rotationMatrix3dZAxis(const double angle);
00647
00653 static QVMatrix translationMatrix3d(const double x, const double y, const double z);
00654
00662 QVMatrix reshape(const int newCols, const int newRows) const
00663 {
00664 QVMatrix result(newCols, newRows);
00665 result.data = this->data;
00666 return result;
00667 }
00668
00672 static QVMatrix multiply(const QList<QVMatrix> &matrices)
00673 {
00674 QVMatrix product = QVMatrix::identity(3);
00675 foreach(QVMatrix matrix, matrices)
00676 product = product * matrix;
00677 return product;
00678 }
00679
00680 private:
00681 int cols, rows;
00682 QSharedDataPointer< QBlasDataBuffer > data;
00683 };
00684
00687 std::ostream& operator << ( std::ostream &os, const QVMatrix &matrix );
00688
00691 std::istream& operator >> ( std::istream &is, QVMatrix &matrix );
00692
00695 uint qHash(const QVMatrix &matrix);
00696
00697 #include <QString>
00707 bool writeQVMatrixToFile(const QString fileName, const QVMatrix &matrix);
00708
00717 bool readQVMatrixFromFile(const QString fileName, QVMatrix &matrix);
00718
00719 #include <QMetaType>
00720 Q_DECLARE_METATYPE(QVMatrix);
00721
00722 #endif
00723