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
00069 QVMatrix(const int rows, const int cols, const double *data = NULL);
00070
00078 QVMatrix(const int rows, const int cols, const double value);
00079
00085 QVMatrix(const QVQuaternion &quaternion);
00086
00087 #ifdef OPENCV
00094 QVMatrix(const CvMat *cvMatrix);
00095
00106 CvMat *toCvMat(const int cvMatType = CV_64F) const;
00107
00113 operator CvMat *() const { return toCvMat(); }
00114 #endif
00115
00122 QVMatrix(const QVVector &vector, const bool rowVector = true);
00123
00129 QVMatrix(const QList<QVVector> &vectorList);
00130
00136 QVMatrix(const QList< QVector<double> > &vectorList);
00137
00141 QVMatrix(const gsl_matrix *matrix);
00142
00148 QVMatrix(const QList<QPointF> &pointList);
00149
00152
00153 operator QList<QVVector> () const
00154 {
00155 QList<QVVector> list;
00156 for (int n = 0; n < getRows(); n++)
00157 list.append(getRow(n));
00158 return list;
00159 }
00160
00163
00164 operator QList< QVector<double> > () const
00165 {
00166 QList< QVector<double> > list;
00167 for (int n = 0; n < getRows(); n++)
00168 list.append(getRow(n));
00169 return list;
00170 }
00171
00174
00175 operator gsl_matrix * () const
00176 {
00177 gsl_matrix *result = gsl_matrix_alloc(rows, cols);
00178 for(int i = 0; i < rows; i++)
00179 for(int j = 0; j < cols; j++)
00180 gsl_matrix_set(result, i, j, operator()(i, j));
00181 return result;
00182 }
00183
00187 QVMatrix & operator=(const QVMatrix &matrix);
00188
00189
00190
00196 bool operator==(const QVMatrix &matrix) const { return equals(matrix); };
00197
00203 bool operator!=(const QVMatrix &matrix) const { return !equals(matrix); };
00204
00209 QVMatrix operator*(const QVMatrix &matrix) const { return dotProduct(matrix); };
00210
00236 QVMatrix operator/(const QVMatrix &matrix) const { return matrixDivide(matrix); };
00237
00242 QVMatrix operator+(const QVMatrix &matrix) const { return addition(matrix); };
00243
00248 QVMatrix operator-(const QVMatrix &matrix) const { return substract(matrix); };
00249
00255 QVMatrix operator-() const { return operator*(-1); };
00256
00257
00258
00263 QVMatrix operator*(const double value) const { return scalarProduct(value); };
00264
00269 QVMatrix operator/(const double value) const { return scalarDivide(value); };
00270
00271
00272
00276 QVVector operator*(const QVVector &vector) const;
00277
00278
00279
00288 QVMatrix verticalAppend(const QVMatrix &matrix) const;
00289
00290
00299 QVMatrix horizontalAppend(const QVMatrix &matrix) const;
00300
00309 QVMatrix operator&(const QVMatrix &matrix) const { return verticalAppend(matrix); };
00310
00319 QVMatrix operator|(const QVMatrix &matrix) const { return horizontalAppend(matrix); };
00320
00329 QVMatrix operator&(const QVVector &vector) const { return verticalAppend(QVMatrix(vector)); };
00330
00339 QVMatrix operator|(const QVVector &vector) const { return horizontalAppend(QVMatrix(vector).transpose()); };
00340
00341
00342
00347 inline double &operator()(const int row, const int col)
00348 { return data->getWriteData()[row*cols + col]; }
00349
00354 inline const double operator()(const int row, const int col) const
00355 { return data->getReadData()[row*cols + col]; }
00356
00361 const int getDataSize() const { return cols*rows; }
00362
00366 const double *getReadData() const { return data->getReadData(); }
00367
00371 double *getWriteData() { return data->getWriteData(); }
00372
00376 QVMatrix transpose() const;
00377
00381 void set(const double value);
00382
00383
00384
00391 bool equals(const QVMatrix &matrix) const;
00392
00397 QVMatrix dotProduct(const QVMatrix &matrix) const;
00398
00405 QVMatrix elementProduct(const QVMatrix &matrix) const;
00406
00418 QVMatrix matrixDivide(const QVMatrix &matrix) const;
00419
00421 QVMatrix inverse() const;
00422
00424 double det() const;
00425
00427 QVVector meanCol() const;
00428
00433 QVMatrix addition(const QVMatrix &matrix) const;
00434
00439 QVMatrix substract(const QVMatrix &matrix) const;
00440
00441
00442
00447 QVMatrix scalarDivide(const double value) const;
00448
00453 QVMatrix scalarProduct(const double value) const;
00454
00463 double norm2() const;
00464
00470 QVMatrix rowHomogeneousNormalize() const;
00471
00475 const int getCols() const { return cols; }
00476
00480 const int getRows() const { return rows; }
00481
00487 const QVMatrix getCols(const int firstCol, const int lastCol) const
00488 {
00489 Q_ASSERT(-1 < firstCol);
00490 Q_ASSERT(lastCol < getCols());
00491 Q_ASSERT(firstCol <= lastCol);
00492
00493 QVMatrix result(getRows(), lastCol - firstCol + 1);
00494 for (int i = 0; i < getRows(); i++)
00495 for (int j = firstCol; j <= lastCol; j++)
00496 result(i,j-firstCol) = operator()(i,j);
00497 return result;
00498 }
00499
00505 const QVMatrix getRows(const int firstRow, const int lastRow) const
00506 {
00507 Q_ASSERT(-1 < firstRow);
00508 Q_ASSERT(lastRow < getRows());
00509 Q_ASSERT(firstRow <= lastRow);
00510
00511 QVMatrix result(lastRow - firstRow + 1, getCols());
00512 for (int i = firstRow; i <= lastRow; i++)
00513 for (int j = 0; j < getCols(); j++)
00514 result(i-firstRow,j) = operator()(i,j);
00515 return result;
00516 }
00517
00518
00519
00524 const QVVector getRow(const int row) const;
00525
00530 void setRow(const int row, QVVector vector);
00531
00536 void setRow(const int row, QVector<double> vector);
00537
00542 const QVVector getCol(const int col) const;
00543
00548 void setCol(const int col, QVVector vector);
00549
00557 const QVMatrix getSubmatrix(const int firstRow, const int lastRow, const int firstCol, const int lastCol) const;
00558
00559
00560
00564 static QVMatrix identity(const int size);
00565
00570 static QVMatrix zeros(const int rows, const int cols);
00571
00578 static QVMatrix random(const int rows, const int cols);
00579
00585 static QVMatrix diagonal(const QVVector &diagonalVector);
00586
00590 static QVMatrix rotationMatrix(const double angle);
00591
00596 static QVMatrix traslationMatrix(const double x, const double y);
00597
00601 static QVMatrix scaleMatrix(const double zoom);
00602
00606 static QVMatrix rotationMatrix3dXAxis(const double angle);
00607
00611 static QVMatrix rotationMatrix3dYAxis(const double angle);
00612
00616 static QVMatrix rotationMatrix3dZAxis(const double angle);
00617
00623 static QVMatrix traslationMatrix3d(const double x, const double y, const double z);
00624
00632 QVMatrix reshape(const int newCols, const int newRows) const
00633 {
00634 QVMatrix result(newCols, newRows);
00635 result.data = this->data;
00636 return result;
00637 }
00638
00642 static QVMatrix multiply(const QList<QVMatrix> &matrices)
00643 {
00644 QVMatrix product = QVMatrix::identity(3);
00645 foreach(QVMatrix matrix, matrices)
00646 product = product * matrix;
00647 return product;
00648 }
00649
00650 private:
00651 int cols, rows;
00652 QSharedDataPointer< QBlasDataBuffer > data;
00653 };
00654
00657 std::ostream& operator << ( std::ostream &os, const QVMatrix &matrix );
00658
00661 std::istream& operator >> ( std::istream &is, QVMatrix &matrix );
00662
00665 uint qHash(const QVMatrix &matrix);
00666
00667 #include <QMetaType>
00668 Q_DECLARE_METATYPE(QVMatrix);
00669
00670 #endif
00671