glc_vector4d.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib 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 License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00022 
00023 
00025 #include "glc_vector4d.h"
00026 #include <QtDebug>
00027 
00028 using namespace glc;
00030 // Operators overload
00032 
00033 
00034 // Overload dot product "^" operator
00035 GLC_Vector4d GLC_Vector4d::operator^ (const GLC_Vector4d &Vect) const
00036 {
00037         GLC_Vector4d VectResult;
00038         VectResult.vector[0]= vector[1] * Vect.vector[2] - vector[2] * Vect.vector[1];
00039         VectResult.vector[1]= vector[2] * Vect.vector[0] - vector[0] * Vect.vector[2];
00040         VectResult.vector[2]= vector[0] * Vect.vector[1] - vector[1] * Vect.vector[0];
00041 
00042         return VectResult;
00043 }
00044 
00045 // Overload equality "==" operator
00046 bool GLC_Vector4d::operator == (const GLC_Vector4d &Vect) const
00047 {
00048         bool bResult= qFuzzyCompare(vector[0], Vect.vector[0]);
00049         bResult= bResult && qFuzzyCompare(vector[1], Vect.vector[1]);
00050         bResult= bResult && qFuzzyCompare(vector[2], Vect.vector[2]);
00051         bResult= bResult && qFuzzyCompare(vector[3], Vect.vector[3]);
00052 
00053         return bResult;
00054 }
00056 // Set Function
00058 
00059 GLC_Vector4d& GLC_Vector4d::setW(const double &dW)
00060 {
00061         if (dW != 0)
00062         {
00063                 const double invDW= 1.0 / dW;
00064                 vector[0]*= invDW;
00065                 vector[1]*= invDW;
00066                 vector[2]*= invDW;
00067                 vector[3]= 1.0;         // For calculation, W = 1.
00068         }
00069         return *this;
00070 }
00071 
00072 GLC_Vector4d& GLC_Vector4d::setVect(const double &dX, const double &dY,
00073         const double &dZ, const double &dW)
00074 {
00075         if ((dW == 1.0) || (dW <= 0.0))
00076         {
00077                 vector[0]= dX;
00078                 vector[1]= dY;
00079                 vector[2]= dZ;
00080         }
00081         else
00082         {
00083                 const double invDW= 1.0 / dW;
00084                 vector[0]= dX * invDW;
00085                 vector[1]= dY * invDW;
00086                 vector[2]= dZ * invDW;
00087         }
00088 
00089         vector[3]= 1.0;         // For calculation, W = 1.
00090 
00091         return *this;
00092 }
00093 
00094 GLC_Vector4d& GLC_Vector4d::setNormal(const double &Norme)
00095 {
00096         const double dNormeCur= sqrt( vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
00097 
00098         if (dNormeCur != 0)
00099         {
00100                 const double Coef = Norme / dNormeCur;
00101 
00102                 vector[0] = vector[0] * Coef;
00103                 vector[1] = vector[1] * Coef;
00104                 vector[2] = vector[2] * Coef;
00105         }
00106         return *this;
00107 }
00108 
00110 // Get Function
00112 
00113 // Return the Angle with another vector
00114 double GLC_Vector4d::getAngleWithVect(GLC_Vector4d Vect) const
00115 {
00116         GLC_Vector4d ThisVect(*this);
00117         ThisVect.setNormal(1);
00118         Vect.setNormal(1);
00119         // Rotation axis
00120         const GLC_Vector4d VectAxeRot(ThisVect ^ Vect);
00121         // Check if the rotation axis vector is null
00122         if (!VectAxeRot.isNull())
00123         {
00124                 return acos(ThisVect * Vect);
00125         }
00126         else return 0.0;
00127 }
00128 
00129 // return the vector string
00130 QString GLC_Vector4d::toString() const
00131 {
00132         QString result("[");
00133 
00134         result+= QString::number(vector[0]) + QString(" , ");
00135         result+= QString::number(vector[1]) + QString(" , ");
00136         result+= QString::number(vector[2]) + QString(" , ");
00137         result+= QString::number(vector[3]) + QString("]");
00138 
00139         return result;
00140 }
00141 
00142 // return the 2D vector
00143 GLC_Vector2d GLC_Vector4d::toVector2d(const GLC_Vector4d& mask) const
00144 {
00145         double x;
00146         double y;
00147         if (mask.vector[0] == 0.0)
00148         {
00149                 x= vector[0];
00150                 if (mask.vector[1] == 0.0)
00151                         y= vector[1];
00152                 else
00153                         y= vector[2];
00154 
00155         }
00156         else
00157         {
00158                 x= vector[1];
00159                 y= vector[2];
00160 
00161         }
00162         return GLC_Vector2d(x, y);
00163 }
00164 
00166 // Services private function
00168 
00169 // Normalize Vector w <- 1
00170 void GLC_Vector4d::normalizeW(void)
00171 {
00172         if (fabs(vector[3]) > 0.00001)
00173         {
00174                 const double invW= 1.0 / vector[3];
00175                 vector[0]*= invW;
00176                 vector[1]*= invW;
00177                 vector[2]*= invW;
00178         }
00179         vector[3]= 1.0;
00180 }
00181 
00182 // Non-member stream operator
00183 QDataStream &operator<<(QDataStream &stream, const GLC_Vector4d &vector)
00184 {
00185         stream << vector.X() << vector.Y() << vector.Z() << vector.W();
00186         return stream;
00187 }
00188 QDataStream &operator>>(QDataStream &stream, GLC_Vector4d &vector)
00189 {
00190         double x, y, z, w;
00191         stream >> x >> y >> z >> w;
00192         vector.setVect(x, y, z, w);
00193         return stream;
00194 }
00195 
00196 

SourceForge.net Logo

©2005-2011 Laurent Ribon