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 <iostream>
00029 #include <QVQuaternion>
00030 #include <QV3DPointF>
00031 #include <qvmath.h>
00032 #include <qvmath/qvblasdatabuffer.h>
00033
00034 #ifdef QVOPENCV
00035 #include <opencv/cv.h>
00036 #endif
00037
00038 #ifdef QVOCTAVE
00039 #include <octave/oct.h>
00040 #endif
00041
00042 #ifdef _MSC_VER
00043 #ifdef max
00044 #undef max
00045 #endif // max
00046
00047 #ifdef min
00048 #undef min
00049 #endif // min
00050 #endif // _MSC_VER
00051
00052
00053
00054
00055
00056
00057
00058
00059
00068 class QVMatrix
00069 {
00070 public:
00071
00073 enum MatrixType
00074 {
00076 General = 0x00,
00078 Symmetric = 0x01,
00080 UpperTriangular = 0x2,
00082 LowerTriangular = 0x03,
00084 Diagonal = 0x04,
00086 PermutationMatrix = 0x05
00087 };
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00102 QVMatrix();
00103
00108 QVMatrix(const QVMatrix &matrix);
00109
00117 QVMatrix(const int rows, const int cols, const double *data = NULL);
00118
00126 QVMatrix(const int rows, const int cols, const QVVector &data);
00127
00135 QVMatrix(const int rows, const int cols, const double value);
00136
00143 QVMatrix(const QVQuaternion &quaternion);
00144
00149 #ifdef GSL_AVAILABLE
00150 QVMatrix(const gsl_matrix *matrix);
00151 #endif
00152
00159 #ifdef QVOPENCV
00160 QVMatrix(const CvMat *cvMatrix);
00161 #endif
00162
00173 #ifdef QVOPENCV
00174 CvMat *toCvMat(const int cvMatType = CV_64F) const;
00175 #endif
00176
00182 #ifdef QVOPENCV
00183 operator CvMat *() const { return toCvMat(); }
00184 #endif
00185
00192 #ifdef QVOCTAVE
00193 QVMatrix(const Matrix octMatrix);
00194 #endif
00195
00202 #ifdef QVOCTAVE
00203 Matrix toOctaveMat() const;
00204 #endif
00205
00211 #ifdef QVOCTAVE
00212 operator Matrix () const { return toOctaveMat(); }
00213 #endif
00214
00221 QVMatrix(const QVVector &vector, const bool rowVector = true);
00222
00228 QVMatrix(const QList<QVVector> &vectorList);
00229
00235 QVMatrix(const QList< QVector<double> > &vectorList);
00236
00242 QVMatrix(const QList<QPointF> &pointList);
00243
00246
00247 operator QList<QVVector> () const
00248 {
00249 QList<QVVector> list;
00250 for (int n = 0; n < getRows(); n++)
00251 list.append(getRow(n));
00252 return list;
00253 }
00254
00257
00258 operator QList< QVector<double> > () const
00259 {
00260 QList< QVector<double> > list;
00261 for (int n = 0; n < getRows(); n++)
00262 list.append(getRow(n));
00263 return list;
00264 }
00265
00270 #ifdef GSL_AVAILABLE
00271 operator gsl_matrix * () const;
00272
00273
00274
00275
00276
00277
00278
00279 #endif
00280
00284 QVMatrix & operator=(const QVMatrix &matrix);
00285
00286
00287
00293 bool operator==(const QVMatrix &matrix) const { return equals(matrix); };
00294
00300 QVMatrix & operator+=(const QVMatrix &matrix)
00301 {
00302 inlineAddition(matrix);
00303 return *this;
00304 };
00305
00311 bool operator!=(const QVMatrix &matrix) const { return !equals(matrix); };
00312
00317 QVMatrix operator*(const QVMatrix &matrix) const { return dotProduct(matrix, false, false); };
00318
00345 #ifdef QVMATRIXALGEBRA_AVAILABLE
00346 QVMatrix operator/(const QVMatrix &matrix) const { return matrixDivide(matrix); };
00347 #endif
00348
00353 QVMatrix operator+(const QVMatrix &matrix) const { return addition(matrix); };
00354
00359 QVMatrix operator-(const QVMatrix &matrix) const { return substract(matrix); };
00360
00366 QVMatrix operator-() const { return operator*(-1.0); };
00367
00368
00369
00374 QVMatrix operator*(const double value) const { return scalarProduct(value); };
00375
00380 QVMatrix operator/(const double value) const { return scalarDivide(value); };
00381
00386 QVMatrix operator+(const double value) const { return scalarAdd(value); };
00387
00392 QVMatrix operator-(const double value) const { return scalarSubstract(value); };
00393
00394
00395
00399 QVVector operator*(const QVVector &vector) const { return dotProduct(vector, false); };
00400
00401
00402
00411 QVMatrix verticalAppend(const QVMatrix &matrix) const;
00412
00413
00422 QVMatrix horizontalAppend(const QVMatrix &matrix) const;
00423
00432 QVMatrix operator&(const QVMatrix &matrix) const { return verticalAppend(matrix); };
00433
00442 QVMatrix operator|(const QVMatrix &matrix) const { return horizontalAppend(matrix); };
00443
00452 QVMatrix operator&(const QVVector &vector) const { return verticalAppend(QVMatrix(vector)); };
00453
00462 QVMatrix operator|(const QVVector &vector) const { return horizontalAppend(QVMatrix(vector).transpose()); };
00463
00464
00465
00470 inline double &operator()(const int row, const int col)
00471 { return data->getWriteData()[row*cols + col]; }
00472
00477 inline double operator()(const int row, const int col) const
00478 { return data->getReadData()[row*cols + col]; }
00479
00484 int getDataSize() const { return cols*rows; }
00485
00489 const double *getReadData() const { return data->getReadData(); }
00490
00494 double *getWriteData() { return data->getWriteData(); }
00495
00499 QVMatrix transpose() const;
00500
00504 void set(const double value);
00505
00506
00507
00514 bool equals(const QVMatrix &matrix) const;
00515
00520 QVMatrix dotProduct(const QVMatrix &matrix, const bool transposeFirstOperand = false, const bool transposeSecondOperand = false) const;
00521
00526 QVVector dotProduct(const QVVector &vector, const bool transposeMatrix = false) const;
00527
00534 QVMatrix elementProduct(const QVMatrix &matrix) const;
00535
00545
00548 #ifdef QVMATRIXALGEBRA_AVAILABLE
00549 QVMatrix matrixDivide(const QVMatrix &matrix) const;
00550 #endif
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561 void triangularSolve(const QVVector &b, QVVector &x, bool transposed = false,
00562 bool unit_diagonal = false) const;
00563
00574 #ifdef BLAS_AVAILABLE
00575 void triangularSolve(const QVMatrix &B, QVMatrix &X, bool transposed = false,
00576 bool unit_diagonal = false, bool left = true) const;
00577 #endif
00578
00581 #ifdef QVMATRIXALGEBRA_AVAILABLE
00582 QVMatrix inverse() const;
00583 #endif
00584
00587 #ifdef QVMATRIXALGEBRA_AVAILABLE
00588 double det() const;
00589 #endif
00590
00592 QVVector meanCol() const;
00593
00598 QVMatrix addition(const QVMatrix &matrix) const;
00599
00604 void inlineAddition(const QVMatrix &matrix);
00605
00610 QVMatrix substract(const QVMatrix &matrix) const;
00611
00612
00613
00618 QVMatrix scalarDivide(const double value) const;
00619
00624 QVMatrix scalarProduct(const double value) const;
00625
00630 QVMatrix scalarAdd(const double value) const;
00631
00636 QVMatrix scalarSubstract(const double value) const;
00637
00646 double norm2() const;
00647
00656 double trace() const;
00657
00661 bool containsNaN() const;
00662
00666 bool containsInf() const;
00667
00671 bool isDiagonal() const;
00672
00678 QVMatrix rowHomogeneousNormalize() const;
00679
00683 int getCols() const { return cols; }
00684
00688 int getRows() const { return rows; }
00689
00696 MatrixType getType() const { return type; }
00697
00704 void setType(MatrixType type) { this->type = type; }
00705
00711 const QVMatrix getCols(const int firstCol, const int lastCol) const
00712 {
00713 Q_ASSERT(-1 < firstCol);
00714 Q_ASSERT(lastCol < getCols());
00715 Q_ASSERT(firstCol <= lastCol);
00716
00717 QVMatrix result(getRows(), lastCol - firstCol + 1);
00718 for (int i = 0; i < getRows(); i++)
00719 for (int j = firstCol; j <= lastCol; j++)
00720 result(i,j-firstCol) = operator()(i,j);
00721 return result;
00722 }
00723
00729 const QVMatrix getRows(const int firstRow, const int lastRow) const
00730 {
00731 Q_ASSERT(-1 < firstRow);
00732 Q_ASSERT(lastRow < getRows());
00733 Q_ASSERT(firstRow <= lastRow);
00734
00735 QVMatrix result(lastRow - firstRow + 1, getCols());
00736 for (int i = firstRow; i <= lastRow; i++)
00737 for (int j = 0; j < getCols(); j++)
00738 result(i-firstRow,j) = operator()(i,j);
00739 return result;
00740 }
00741
00742
00743
00748 const QVVector getRow(const int row) const;
00749
00754 void setRow(const int row, QVVector vector);
00755
00760 void setRow(const int row, QVector<double> vector);
00761
00766 const QVVector getCol(const int col) const;
00767
00772 void setCol(const int col, QVVector vector);
00773
00781 const QVMatrix getSubmatrix(const int firstRow, const int lastRow, const int firstCol, const int lastCol) const;
00782
00788 void setSubmatrix(const int row, const int col, const QVMatrix &M);
00789
00793 QVMatrix reversedRows() const;
00794
00798 QVMatrix reversedCols() const;
00799
00803 const QVVector diagonal() const;
00804
00805
00806
00810 static QVMatrix identity(const int size);
00811
00825 static QVMatrix cameraCalibrationMatrix(const double f, const double r = 1.0, const double cx = 0.0, const double cy = 0.0, const double s = 0.0);
00826
00831 static QVMatrix zeros(const int rows, const int cols);
00832
00839 static QVMatrix random(const int rows, const int cols);
00840
00844 static QVMatrix diagonal(const QVVector &diagonalVector);
00845
00849 static QVMatrix rotationMatrix(const double angle);
00850
00855 static QVMatrix rotationMatrix(const QPointF center, const double angle);
00856
00861 static QVMatrix translationMatrix(const double x, const double y);
00862
00866 static QVMatrix scaleMatrix(const double zoom);
00867
00871 static QVMatrix rotationMatrix3dXAxis(const double angle);
00872
00876 static QVMatrix rotationMatrix3dYAxis(const double angle);
00877
00881 static QVMatrix rotationMatrix3dZAxis(const double angle);
00882
00888 static QVMatrix translationMatrix3d(const double x, const double y, const double z);
00889
00897 QVMatrix reshape(const int newRows, const int newCols) const
00898 {
00899 QVMatrix result(newRows, newCols);
00900 result.data = this->data;
00901 return result;
00902 }
00903
00908 static QVMatrix multiply(const QList<QVMatrix> &matrices)
00909 {
00910 QVMatrix product = QVMatrix::identity(3);
00911 foreach(QVMatrix matrix, matrices)
00912 product = product * matrix;
00913 return product;
00914 }
00915
00916 private:
00917 int cols, rows;
00918 MatrixType type;
00919 bool transposed;
00920 QSharedDataPointer< QBlasDataBuffer > data;
00921 };
00922
00928 QVMatrix operator*(const double value, const QVMatrix &matrix);
00929
00935 QVMatrix operator+(const double value, const QVMatrix &matrix);
00936
00942 QVMatrix operator-(const double value, const QVMatrix &matrix);
00943
00946 std::ostream& operator << ( std::ostream &os, const QVMatrix &matrix );
00947
00950 std::istream& operator >> ( std::istream &is, QVMatrix &matrix );
00951
00954 uint qHash(const QVMatrix &matrix);
00955
00956 #include <QString>
00967 bool writeQVMatrixToFile(const QString fileName, const QVMatrix &matrix, const int precission = 7);
00976 bool readQVMatrixFromFile(const QString fileName, QVMatrix &matrix);
00977
00997 QV3DPointF lnSO3(const QVMatrix &R);
00998
01018 QVMatrix expSO3(const QV3DPointF& w);
01019
01020 #include <QMetaType>
01021 Q_DECLARE_METATYPE(QVMatrix);
01022
01023 #endif
01024