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
00067 QVMatrix(const int rows, const int cols);
00068
00076 QVMatrix(const int rows, const int cols, const double value);
00077
00083 QVMatrix(const QVQuaternion &quaternion);
00084
00085 #ifdef OPENCV
00092 QVMatrix(const CvMat *cvMatrix);
00093
00104 CvMat *toCvMat(const int cvMatType = CV_64F) const;
00105
00111 operator CvMat *() const { return toCvMat(); }
00112 #endif
00113
00120 QVMatrix(const QVVector &vector, const bool rowVector = true);
00121
00127 QVMatrix(const QList<QVVector> &vectorList);
00128
00134 QVMatrix(const QList< QVector<double> > &vectorList);
00135
00139 QVMatrix(const gsl_matrix *matrix);
00140
00146 QVMatrix(const QList<QPointF> &pointList);
00147
00150
00151 operator QList<QVVector> () const
00152 {
00153 QList<QVVector> list;
00154 for (int n = 0; n < getRows(); n++)
00155 list.append(getRow(n));
00156 return list;
00157 }
00158
00161
00162 operator QList< QVector<double> > () const
00163 {
00164 QList< QVector<double> > list;
00165 for (int n = 0; n < getRows(); n++)
00166 list.append(getRow(n));
00167 return list;
00168 }
00169
00172
00173 operator gsl_matrix * () const
00174 {
00175 gsl_matrix *result = gsl_matrix_alloc(rows, cols);
00176 for(int i = 0; i < rows; i++)
00177 for(int j = 0; j < cols; j++)
00178 gsl_matrix_set(result, i, j, operator()(i, j));
00179 return result;
00180 }
00181
00185 QVMatrix & operator=(const QVMatrix &matrix);
00186
00187
00188
00194 bool operator==(const QVMatrix &matrix) const { return equals(matrix); };
00195
00201 bool operator!=(const QVMatrix &matrix) const { return !equals(matrix); };
00202
00207 QVMatrix operator*(const QVMatrix &matrix) const { return dotProduct(matrix); };
00208
00234 QVMatrix operator/(const QVMatrix &matrix) const { return matrixDivide(matrix); };
00235
00240 QVMatrix operator+(const QVMatrix &matrix) const { return addition(matrix); };
00241
00246 QVMatrix operator-(const QVMatrix &matrix) const { return substract(matrix); };
00247
00253 QVMatrix operator-() const { return operator*(-1); };
00254
00255
00256
00261 QVMatrix operator*(const double value) const { return scalarProduct(value); };
00262
00267 QVMatrix operator/(const double value) const { return scalarDivide(value); };
00268
00269
00270
00274 QVVector operator*(const QVVector &vector) const;
00275
00276
00277
00282 inline double &operator()(const int row, const int col)
00283 { return data->getWriteData()[row*cols + col]; }
00284
00289 inline const double operator()(const int row, const int col) const
00290 { return data->getReadData()[row*cols + col]; }
00291
00296 const int getDataSize() const { return cols*rows; }
00297
00301 const double *getReadData() const { return data->getReadData(); }
00302
00306 double *getWriteData() { return data->getWriteData(); }
00307
00311 QVMatrix transpose() const;
00312
00316 void set(const double value);
00317
00318
00319
00326 bool equals(const QVMatrix &matrix) const;
00327
00332 QVMatrix dotProduct(const QVMatrix &matrix) const;
00333
00340 QVMatrix elementProduct(const QVMatrix &matrix) const;
00341
00353 QVMatrix matrixDivide(const QVMatrix &matrix) const;
00354
00356 QVMatrix inverse() const;
00357
00359 double det() const;
00360
00362 QVVector meanCol() const;
00363
00368 QVMatrix addition(const QVMatrix &matrix) const;
00369
00374 QVMatrix substract(const QVMatrix &matrix) const;
00375
00376
00377
00382 QVMatrix scalarDivide(const double value) const;
00383
00388 QVMatrix scalarProduct(const double value) const;
00389
00398 double norm2() const;
00399
00405 QVMatrix rowHomogeneousNormalize() const;
00406
00410 const int getCols() const { return cols; }
00411
00415 const int getRows() const { return rows; }
00416
00422 const QVMatrix getCols(const int firstCol, const int lastCol) const
00423 {
00424 Q_ASSERT(-1 < firstCol);
00425 Q_ASSERT(lastCol < getCols());
00426 Q_ASSERT(firstCol <= lastCol);
00427
00428 QVMatrix result(getRows(), lastCol - firstCol + 1);
00429 for (int i = 0; i < getRows(); i++)
00430 for (int j = firstCol; j <= lastCol; j++)
00431 result(i,j-firstCol) = operator()(i,j);
00432 return result;
00433 }
00434
00440 const QVMatrix getRows(const int firstRow, const int lastRow) const
00441 {
00442 Q_ASSERT(-1 < firstRow);
00443 Q_ASSERT(lastRow < getRows());
00444 Q_ASSERT(firstRow <= lastRow);
00445
00446 QVMatrix result(lastRow - firstRow + 1, getCols());
00447 for (int i = firstRow; i <= lastRow; i++)
00448 for (int j = 0; j < getCols(); j++)
00449 result(i-firstRow,j) = operator()(i,j);
00450 return result;
00451 }
00452
00453
00454
00459 const QVVector getRow(const int row) const;
00460
00465 void setRow(const int row, QVVector vector);
00466
00471 void setRow(const int row, QVector<double> vector);
00472
00477 const QVVector getCol(const int col) const;
00478
00483 void setCol(const int col, QVVector vector);
00484
00492 const QVMatrix getSubmatrix(const int firstRow, const int lastRow, const int firstCol, const int lastCol) const;
00493
00494
00495
00499 static QVMatrix identity(const int size);
00500
00505 static QVMatrix zeros(const int rows, const int cols);
00506
00513 static QVMatrix random(const int rows, const int cols);
00514
00520 static QVMatrix diagonal(const QVVector &diagonalVector);
00521
00525 static QVMatrix rotationMatrix(const double angle);
00526
00531 static QVMatrix traslationMatrix(const double x, const double y);
00532
00536 static QVMatrix scaleMatrix(const double zoom);
00537
00541 static QVMatrix rotationMatrix3dXAxis(const double angle);
00542
00546 static QVMatrix rotationMatrix3dYAxis(const double angle);
00547
00551 static QVMatrix rotationMatrix3dZAxis(const double angle);
00552
00558 static QVMatrix traslationMatrix3d(const double x, const double y, const double z);
00559
00560 private:
00561 int cols, rows;
00562 QSharedDataPointer< QBlasDataBuffer > data;
00563 };
00564
00567 std::ostream& operator << ( std::ostream &os, const QVMatrix &matrix );
00568
00571 std::istream& operator >> ( std::istream &is, QVMatrix &matrix );
00572
00575 uint qHash(const QVMatrix &matrix);
00576
00577 #include <QMetaType>
00578 Q_DECLARE_METATYPE(QVMatrix);
00579
00580 #endif
00581