PARP Research Group University of Murcia, Spain


src/qvmath/qvmatrix.h

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
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 
00040 class QVMatrix
00041         {
00042         public:
00043                 // Constructors
00044 
00049                 QVMatrix();
00050 
00055                 QVMatrix(const QVMatrix &matrix);
00056 
00063                 QVMatrix(const int rows, const int cols);
00064 
00070                 QVMatrix(const QVQuaternion &quaternion);
00071 
00078                 QVMatrix(const QVVector &vector, const bool rowVector = true);
00079 
00085                 QVMatrix(const QList<QVVector> &vectorList);
00086 
00092                 QVMatrix(const QList< QVector<double> > &vectorList);
00093 
00097                 QVMatrix(const gsl_matrix *matrix);
00098 
00104                 QVMatrix(const QList<QPointF> &pointList);
00105 
00108                 // Creates a new list of row QVVectors from a matrix
00109                 operator QList<QVVector> () const
00110                         {
00111                         QList<QVVector> list;
00112                         for (int n = 0; n < getRows(); n++)
00113                                 list.append(getRow(n));
00114                         return list;
00115                         }
00116 
00119                 // Creates a new list of row QVectors from a matrix
00120                 operator QList< QVector<double> > () const
00121                         {
00122                         QList< QVector<double> > list;
00123                         for (int n = 0; n < getRows(); n++)
00124                                 list.append(getRow(n));
00125                         return list;
00126                         }
00127 
00130                 // Creates a new gsl_matrix * from a matrix
00131                 operator gsl_matrix * () const
00132                         {
00133                         gsl_matrix *result = gsl_matrix_alloc(rows, cols);
00134                         for(int i = 0; i < rows; i++)
00135                                 for(int j = 0; j < cols; j++)
00136                                         gsl_matrix_set(result, i, j, operator()(i, j));
00137                         return result;
00138                         }
00139 
00143                 QVMatrix & operator=(const QVMatrix &matrix);
00144 
00145                 // Matrix-matrix operators
00146 
00152                 bool operator==(const QVMatrix &matrix) const                   { return equals(matrix);        };
00153 
00159                 bool operator!=(const QVMatrix &matrix) const                   { return !equals(matrix);       };
00160 
00165                 QVMatrix operator*(const QVMatrix &matrix) const                { return dotProduct(matrix);    };
00166 
00192                 QVMatrix operator/(const QVMatrix &matrix) const                { return matrixDivide(matrix);  };
00193 
00198                 QVMatrix operator+(const QVMatrix &matrix) const                { return addition(matrix);      };
00199 
00204                 QVMatrix operator-(const QVMatrix &matrix) const                { return substract(matrix);     };
00205 
00211                 QVMatrix operator-() const                                      { return operator*(-1);         };
00212 
00213                 // Matrix-scalar operators
00214 
00219                 QVMatrix operator*(const double value) const                    { return scalarProduct(value);  };
00220 
00225                 QVMatrix operator/(const double value) const                    { return scalarDivide(value);   };
00226 
00227                 // Matrix-vector operators
00228 
00232                 QVVector operator*(QVVector &vector) const;
00233 
00234                 // Operation methods
00235 
00240                 inline double &operator()(const int row, const int col)
00241                         { return data->getWriteData()[row*cols + col]; }
00242 
00247                 inline const double operator()(const int row, const int col) const
00248                         { return data->getReadData()[row*cols + col]; }
00249 
00254                 const int getDataSize()                 const   { return cols*rows; }
00255 
00259                 const double *getReadData()             const   { return data->getReadData(); }
00260 
00264                 double *getWriteData()                          { return data->getWriteData(); }
00265 
00269                 QVMatrix transpose() const;
00270 
00274                 void set(const double value);
00275 
00276                 // Matrix-matrix operations
00277 
00284                 bool equals(const QVMatrix &matrix) const;
00285 
00290                 QVMatrix dotProduct(const QVMatrix &matrix) const;
00291 
00303                 QVMatrix matrixDivide(const QVMatrix &matrix) const;
00304 
00306                 QVMatrix inverse() const;
00307 
00309                 double det() const;
00310 
00312                 QVVector meanCol() const;
00313 
00318                 QVMatrix addition(const QVMatrix &matrix) const;
00319 
00324                 QVMatrix substract(const QVMatrix &matrix) const;
00325 
00326                 // Matrix-scalar operations
00327 
00332                 QVMatrix scalarDivide(const double value) const;
00333 
00338                 QVMatrix scalarProduct(const double value) const;
00339 
00348                 double norm2() const;
00349 
00355                 QVMatrix rowHomogeneousNormalize() const;
00356 
00360                 const int getCols() const       { return cols; }
00361 
00365                 const int getRows() const       { return rows; }
00366 
00367                 // Access operations
00368 
00373                 const QVVector getRow(const int row) const;
00374 
00379                 void setRow(const int row, QVVector vector);
00380 
00385                 void setRow(const int row, QVector<double> vector);
00386 
00391                 const QVVector getCol(const int col) const;
00392 
00397                 void setCol(const int col, QVVector vector);
00398 
00406                 const QVMatrix getSubmatrix(const int firstRow, const int lastRow, const int firstCol, const int lastCol) const;
00407 
00408                 // Misc matrices
00409 
00413                 static QVMatrix identity(const int size);
00414 
00421                 static QVMatrix random(const int rows, const int cols);
00422 
00428                 static QVMatrix diagonal(const QVVector &diagonalVector);
00429 
00433                 static QVMatrix rotationMatrix(const double angle);
00434 
00439                 static QVMatrix traslationMatrix(const double x, const double y);
00440 
00444                 static QVMatrix scaleMatrix(const double zoom);
00445 
00449                 static QVMatrix rotationMatrix3dXAxis(const double angle);
00450 
00454                 static QVMatrix rotationMatrix3dYAxis(const double angle);
00455 
00459                 static QVMatrix rotationMatrix3dZAxis(const double angle);
00460 
00466                 static QVMatrix traslationMatrix3d(const double x, const double y, const double z);
00467 
00468         private:
00469                 int cols, rows;
00470                 QSharedDataPointer< QBlasDataBuffer > data;
00471         };
00472 
00475 std::ostream& operator << ( std::ostream &os, const QVMatrix &matrix );
00476 
00479 std::istream& operator >> ( std::istream &is, QVMatrix &matrix );
00480 
00483 uint qHash(const QVMatrix &matrix);
00484 
00485 #include <QMetaType>
00486 Q_DECLARE_METATYPE(QVMatrix);
00487 
00488 #endif
00489 



QVision framework. PARP research group, copyright 2007, 2008.