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

SourceForge.net Logo

©2005 Laurent Ribon