PARP Research Group University of Murcia, Spain


src/qvmath/qvvector.h

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008, 2009. 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 QVVECTOR_H
00026 #define QVVECTOR_H
00027 
00028 #include <math.h>
00029 #include <iostream>
00030 #include <QVector>
00031 #include <QPointF>
00032 #include <qvdefines.h>
00033 #include <gsl/gsl_linalg.h>
00034 
00035 class QVMatrix;
00036 
00043 class QVVector: public QVector<double>
00044         {
00045         public:
00049                 QVVector(): QVector<double>()                                                                   {}
00050 
00055                 QVVector(const int size, const double defaultValue = 0): QVector<double>(size, defaultValue)    {}
00056 
00060                 QVVector(const QVVector &vector): QVector<double>(vector)                                       {}
00061 
00065                 QVVector(const QVector<double> &vector): QVector<double>(vector)                                {}
00066 
00073                 QVVector(const QVMatrix &matrix);
00074 
00078                 QVVector(const gsl_vector *vector): QVector<double>(vector->size)
00079                                 {
00080                                 for(int i = 0; i < size(); i++)
00081                                         replace(i, gsl_vector_get(vector, i));
00082                                 }
00083 
00087                 QVVector(const QPoint &point): QVector<double>(2)       { operator[](0) = point.x(); operator[](1) = point.y(); }
00088 
00092                 QVVector(const QPointF &point): QVector<double>(2)      { operator[](0) = point.x(); operator[](1) = point.y(); }
00093 
00095 
00100                 double operator*(const QVVector &vector) const          { return dotProduct(vector); };
00101 
00106                 QVVector operator^(const QVVector &vector) const        { return crossProduct(vector); };
00107 
00112                 QVVector operator+(const QVVector &vector) const        { return add(vector); };
00113 
00118                 QVVector operator-(const QVVector &vector) const        { return substract(vector); };
00119 
00124                 QVVector operator*(const double value) const            { return scalarMultiplication(value); };
00125 
00130                 QVVector operator/(const double value) const            { return scalarDivision(value); };
00131 
00135                 QVVector operator*(const QVMatrix &matrix) const;
00136 
00141                 bool operator==(const QVVector &vector) const           { return equals(vector); };
00142 
00146                 QVVector & operator+=(const QVVector &vector)           { return (*this = add(vector)); }
00147 
00148 
00153                 operator QPointF() const
00154                         {
00155                         Q_ASSERT(size() > 1);
00156                         Q_ASSERT(size() < 4);
00157                         if (size() == 2)
00158                                 return QPointF(operator[](0), operator[](1));
00159                         else
00160                                 return QPointF(operator[](0)/operator[](2), operator[](1)/operator[](2));
00161                         }
00162 
00166                 operator gsl_vector * () const
00167                         {
00168                         gsl_vector *result = gsl_vector_alloc(size());
00169                         const double *data = this->data();
00170                         for(uint i = 0; i < result->size; i++)
00171                                 gsl_vector_set (result, i, data[i]);
00172                         return result;
00173                         }
00174 
00176 
00180                 void set(const double value)
00181                         {
00182                         for (int i = 0; i < size(); i++)
00183                                 operator[](i) =  value;
00184                         };
00185 
00189                 QVVector subVector(const int firstIndex, const int lastIndex)
00190                         {
00191                         Q_ASSERT(0 <= firstIndex);
00192                         Q_ASSERT(firstIndex <= lastIndex);
00193                         Q_ASSERT(lastIndex < this->size());
00194 
00195                         QVVector result(lastIndex - firstIndex +1);
00196                         for (int i = firstIndex; i <= lastIndex; i++)
00197                                 result[i] = operator[](i);
00198 
00199                         return result;
00200                         };
00201 
00206                 QVVector scalarDivision(const double value) const
00207                         {
00208                         QVVector result = *this;
00209                         for (int i = 0; i < size(); i++)
00210                                 result[i] /= value;
00211                         return result;
00212                         };
00213 
00218                 QVVector scalarMultiplication(const double value) const
00219                         {
00220                         QVVector result = *this;
00221                         for (int i = 0; i < size(); i++)
00222                                 result[i] *= value;
00223                         return result;
00224                         };
00225 
00229                 double norm2() const
00230                         { return sqrt(*this * *this); }
00231 
00236                 QVVector normalize()    const
00237                         { return operator/(norm2()); }
00238 
00241                 double max() const;
00242 
00245                 double min() const;
00246 
00253                 double mean() const;
00254 
00261                 double variance() const;
00262 
00267                 double dotProduct(const QVVector &vector) const;
00268 
00274                 QVVector crossProduct(const QVVector &vector) const;
00275 
00280                 QVVector add(const QVVector &vector) const;
00281 
00286                 QVVector substract(const QVVector &vector) const;
00287 
00292                 bool equals(const QVVector &vector) const;
00293 
00295 
00303                 static const QVVector gaussianVector(const int radius, const double sigma);
00304 
00308                 static const QVVector homogeneousCoordinates(const QPointF &point);
00309 
00330                 QVMatrix crossProductMatrix() const;
00331 
00333                 QVMatrix toRowMatrix() const;
00334 
00336                 QVMatrix toColumnMatrix() const;
00337         };
00338 
00341 std::ostream& operator << ( std::ostream &os, const QVVector &vector );
00342 
00345 uint qHash(const QVVector &vector);
00346 
00347 #endif
00348 



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