src/qvmath/qvvector.cpp

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 #include <QString>
00026 #include <QVVector>
00027 #include <QVMatrix>
00028 
00029 QVVector::QVVector(const QVMatrix &matrix): QVector<double>(matrix.getCols() * matrix.getRows())
00030         {
00031         const int n = size();
00032         const double *matrixData = matrix.getReadData();
00033 
00034         for(int i = 0; i < n; i++)
00035                 operator[](i) = matrixData[i];
00036         }
00037 
00038 double QVVector::dotProduct(const QVVector &vector) const
00039         {
00040         Q_ASSERT(size() == vector.size());
00041 
00042         double accum = 0;
00043         for (int i = 0; i < size(); i++)
00044                 accum += at(i) * vector[i];
00045 
00046         return accum;
00047         }
00048 
00049 QVVector QVVector::crossProduct(const QVVector &vector) const
00050         {
00051         Q_ASSERT(size() == vector.size());
00052         Q_ASSERT(size() == 3);
00053 
00054         const double    x1 = at(0), y1 = at(1), z1 = at(2),
00055                         x2 = vector[0], y2 = vector[1], z2 = vector[2];
00056 
00057         QVVector v(3);
00058 
00059         v[0] = -y2*z1 + y1*z2;
00060         v[1] = x2*z1 - x1*z2;
00061         v[2] = -x2*y1 + x1*y2;
00062 
00063         return v;
00064         }
00065 
00066 QVVector QVVector::operator*(const QVMatrix &matrix) const
00067         {
00068         Q_ASSERT(size() == matrix.getRows());
00069         return QVMatrix(*this).dotProduct(matrix).getRow(0);
00070         }
00071 
00072 QVVector QVVector::add(const QVVector &vector) const
00073         {
00074         Q_ASSERT(size() == vector.size());
00075 
00076         QVVector result(size());
00077         for (int i = 0; i < size(); i++)
00078                 result[i] = at(i) + vector[i];
00079 
00080         return result;
00081         }
00082 
00083 QVVector QVVector::substract(const QVVector &vector) const
00084         {
00085         Q_ASSERT(size() == vector.size());
00086 
00087         QVVector result(size());
00088         for (int i = 0; i < size(); i++)
00089                 result[i] = at(i) - vector[i];
00090 
00091         return result;
00092         }
00093 
00094 bool QVVector::equals(const QVVector &vector) const
00095         {
00096         if (size() != vector.size())
00097                 return false;
00098 
00099         for (int i = 0; i < size(); i++)
00100                 if (at(i) != vector[i])
00101                         return false;
00102 
00103         return true;
00104         }
00105 
00107 
00108 std::ostream& operator << ( std::ostream &os, const QVVector &vector )
00109         {
00110         const int size = vector.size();
00111 
00112         os << "QVVector (" << size << ") [ ";
00113 
00114         for (int i = 0; i < size; i++)
00115                 os << qPrintable(QString("%1").arg(vector[i], -8, 'f', 6)) << " ";
00116 
00117         os << "]";
00118         return os;
00119         }
00120 
00121 uint qHash(const QVVector &vector)
00122         {
00123         const int size = vector.size();
00124         double accum = 0;
00125         for (int i = 0; i < size; i++)
00126                 accum += vector[i] / vector[size-i-1];
00127 
00128         return (uint) ((100000 * accum) / ((double) size));
00129         }
00130