glc_vector4d.h

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 
00024 
00025 #ifndef GLC_VECTOR4D_H_
00026 #define GLC_VECTOR4D_H_
00027 
00028 #include <QVector>
00029 #include <QDataStream>
00030 
00031 #include "glc_utils_maths.h"
00032 #include "glc_vector2d.h"
00033 #include "glc_vector3d.h"
00034 #include "glc_vector3df.h"
00035 
00036 #include "../glc_config.h"
00037 
00040 
00045 
00046 
00047 class GLC_LIB_EXPORT GLC_Vector4d
00048 {
00050         friend class GLC_Matrix4x4;
00051 
00053         inline friend GLC_Vector4d operator - (const GLC_Vector4d &Vect)
00054         {
00055                 return GLC_Vector4d(-Vect.vector[0], -Vect.vector[1], -Vect.vector[2]);
00056         }
00057 
00058 
00060 
00062 
00063 public:
00070         inline GLC_Vector4d()
00071         {
00072                 vector[0]= 0.0;
00073                 vector[1]= 0.0;
00074                 vector[2]= 0.0;
00075 
00076                 vector[3]= 1.0;
00077         }
00079         inline GLC_Vector4d(const double &dX, const double &dY, const double &dZ, const double &dW= 1.0)
00080         {
00081                 setVect(dX, dY, dZ, dW);
00082         }
00084         inline GLC_Vector4d(const GLC_Vector4d &Vect)
00085         {
00086                 vector[0]= Vect.vector[0];
00087                 vector[1]= Vect.vector[1];
00088                 vector[2]= Vect.vector[2];
00089                 vector[3]= Vect.vector[3];
00090         }
00091 
00093         inline GLC_Vector4d(const GLC_Vector3d &Vect)
00094         {
00095                 vector[0]= Vect.m_Vector[0];
00096                 vector[1]= Vect.m_Vector[1];
00097                 vector[2]= Vect.m_Vector[2];
00098                 vector[3]= 1.0;
00099         }
00100 
00102         inline GLC_Vector4d(const GLC_Vector3df &Vect)
00103         {
00104                 vector[0]= static_cast<double>(Vect.m_Vector[0]);
00105                 vector[1]= static_cast<double>(Vect.m_Vector[1]);
00106                 vector[2]= static_cast<double>(Vect.m_Vector[2]);
00107                 vector[3]= 1.0;
00108         }
00109 
00111         inline GLC_Vector4d(const GLC_Vector2d &Vect)
00112         {
00113                 vector[0]= Vect.m_Vector[0];
00114                 vector[1]= Vect.m_Vector[1];
00115                 vector[2]= 0.0;
00116                 vector[3]= 1.0;
00117         }
00118 
00120 
00122 
00124 
00125 public:
00127         inline GLC_Vector4d operator + (const GLC_Vector4d &Vect) const
00128         {
00129                 GLC_Vector4d VectResult(vector[0] + Vect.vector[0], vector[1] + Vect.vector[1],
00130                         vector[2] + Vect.vector[2]);
00131 
00132                 return VectResult;
00133         }
00134 
00136         inline GLC_Vector4d& operator = (const GLC_Vector4d &Vect)
00137         {
00138                 vector[0]= Vect.vector[0];
00139                 vector[1]= Vect.vector[1];
00140                 vector[2]= Vect.vector[2];
00141                 vector[3]= Vect.vector[3];
00142 
00143                 return *this;
00144         }
00145 
00147         inline GLC_Vector4d& operator = (const GLC_Vector3d &Vect)
00148         {
00149                 vector[0]= Vect.m_Vector[0];
00150                 vector[1]= Vect.m_Vector[1];
00151                 vector[2]= Vect.m_Vector[2];
00152                 vector[3]= 1.0;
00153 
00154                 return *this;
00155         }
00156 
00158         inline GLC_Vector4d& operator = (const GLC_Vector3df &Vect)
00159         {
00160                 vector[0]= static_cast<double>(Vect.m_Vector[0]);
00161                 vector[1]= static_cast<double>(Vect.m_Vector[1]);
00162                 vector[2]= static_cast<double>(Vect.m_Vector[2]);
00163                 vector[3]= 1.0;
00164 
00165                 return *this;
00166         }
00167 
00169         inline GLC_Vector4d& operator = (const GLC_Vector2d &Vect)
00170         {
00171                 vector[0]= Vect.m_Vector[0];
00172                 vector[1]= Vect.m_Vector[1];
00173                 vector[2]= 0.0;
00174                 vector[3]= 1.0;
00175 
00176                 return *this;
00177         }
00178 
00180         inline GLC_Vector4d* operator += (const GLC_Vector4d &Vect)
00181         {
00182                 *this= *this + Vect;
00183                 return this;
00184         }
00185 
00186 
00188         inline GLC_Vector4d operator - (const GLC_Vector4d &Vect) const
00189         {
00190                 GLC_Vector4d VectResult(vector[0] - Vect.vector[0], vector[1] - Vect.vector[1],
00191                         vector[2] - Vect.vector[2]);
00192 
00193                 return VectResult;
00194         }
00195 
00197         GLC_Vector4d* operator -= (const GLC_Vector4d &Vect)
00198         {
00199                 *this= *this - Vect;
00200                 return this;
00201         }
00202 
00204         GLC_Vector4d operator ^ (const GLC_Vector4d &Vect) const;
00205 
00207         inline double operator * (const GLC_Vector4d &Vect) const
00208         {
00209                 // W Component is ignored
00210                 return vector[0] * Vect.vector[0] + vector[1] * Vect.vector[1] +
00211                         vector[2] * Vect.vector[2];
00212         }
00213 
00215         inline GLC_Vector4d operator * (double Scalaire) const
00216         {
00217                 return GLC_Vector4d(vector[0] * Scalaire, vector[1] * Scalaire, vector[2] * Scalaire);
00218         }
00219 
00220 
00222         bool operator == (const GLC_Vector4d &Vect) const;
00223 
00225         inline bool operator != (const GLC_Vector4d &Vect) const
00226         {
00227                 return !(*this == Vect);
00228         }
00229 
00231 
00233 
00235 
00236 public:
00238         inline GLC_Vector4d& setX(const double &dX)
00239         {
00240                 vector[0]= dX;
00241                 return *this;
00242         }
00243 
00245         inline GLC_Vector4d& setY(const double &dY)
00246         {
00247                 vector[1]= dY;
00248                 return *this;
00249         }
00250 
00252         inline GLC_Vector4d& setZ(const double &dZ)
00253         {
00254                 vector[2]= dZ;
00255                 return *this;
00256         }
00257 
00259         GLC_Vector4d& setW(const double &dW);
00260 
00262         GLC_Vector4d& setVect(const double &dX, const double &dY, const double &dZ, const double &dW= 1);
00263 
00265         inline GLC_Vector4d& setVect(const GLC_Vector4d &Vect)
00266         {
00267                 vector[0]= Vect.vector[0];
00268                 vector[1]= Vect.vector[1];
00269                 vector[2]= Vect.vector[2];
00270                 vector[3]= Vect.vector[3];
00271                 return *this;
00272         }
00273 
00275         GLC_Vector4d& setNormal(const double &Norme);
00276 
00278         inline GLC_Vector4d& invert(void)
00279         {
00280                 vector[0]= - vector[0];
00281                 vector[1]= - vector[1];
00282                 vector[2]= - vector[2];
00283                 return *this;
00284         }
00285 
00287 
00289 
00291 
00292 public:
00294         inline double X(void) const
00295         {
00296                 return vector[0];
00297         }
00299         inline double Y(void) const
00300         {
00301                 return vector[1];
00302         }
00304         inline double Z(void) const
00305         {
00306                 return vector[2];
00307         }
00309         inline double W(void) const
00310         {
00311                 return vector[3];
00312         }
00313         inline GLC_Vector3d toVector3d() const
00314         {
00315                 return GLC_Vector3d(vector[0], vector[1], vector[2]);
00316         }
00317         inline GLC_Vector3df toVector3df() const
00318         {
00319                 return GLC_Vector3df(static_cast<float>(vector[0]), static_cast<float>(vector[1]), static_cast<float>(vector[2]));
00320         }
00322         inline const double *data(void) const
00323         {
00324                 return vector;
00325         }
00327         inline double norm(void) const
00328         {
00329                 return sqrt(vector[0] * vector[0] + vector[1] * vector[1]
00330                         + vector[2] * vector[2]);
00331         }
00333         inline bool isNull(void) const
00334         {
00335                 bool result;
00336 
00337                 result= qFuzzyCompare(vector[0], 0.0) && qFuzzyCompare(vector[1], 0.0)
00338                         && qFuzzyCompare(vector[2], 0.0);
00339 
00340                 return result;
00341         }
00342 
00344         double getAngleWithVect(GLC_Vector4d Vect) const;
00345 
00347         QString toString() const;
00348 
00350 
00352         GLC_Vector2d toVector2d(const GLC_Vector4d&) const;
00353 
00355         inline QVector<float> toFloat3dQVector() const
00356         {
00357                 QVector<float> result;
00358                 result << static_cast<float>(vector[0]) << static_cast<float>(vector[1]) << static_cast<float>(vector[2]);
00359                 return result;
00360         }
00362 
00364 // Private services functions
00366 private:
00367 
00369         void normalizeW(void);
00370 
00372 //name Private attributes
00374 private:
00381         enum {VECT4DIMENSION = 4};
00382         double vector[VECT4DIMENSION];
00383 
00384 }; //class GLC_Vector4d
00385 
00387 //typedef GLC_Vector4d GLC_Point4d;
00388 
00390 QDataStream &operator<<(QDataStream &, const GLC_Vector4d &);
00391 QDataStream &operator>>(QDataStream &, GLC_Vector4d &);
00392 
00393 #endif /*GLC_VECTOR4D_H_*/

SourceForge.net Logo

©2005-2011 Laurent Ribon