glc_vector3d.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  Version 2.0.0, packaged on July 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 Lesser General Public License as published by
00011  the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
00018 
00019  You should have received a copy of the GNU Lesser 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 
00026 
00027 #ifndef GLC_VECTOR3D_H_
00028 #define GLC_VECTOR3D_H_
00029 
00030 #include <QDataStream>
00031 
00032 #include "glc_utils_maths.h"
00033 #include "glc_vector3df.h"
00034 #include "glc_vector2d.h"
00035 #include "../glc_config.h"
00036 
00039 
00043 
00044 
00045 class GLC_LIB_EXPORT GLC_Vector3d
00046 {
00047         friend class GLC_Vector4d;
00048         friend class GLC_Matrix4x4;
00049 
00051         inline friend GLC_Vector3d operator - (const GLC_Vector3d &Vect)
00052         {return GLC_Vector3d(-Vect.m_Vector[0], -Vect.m_Vector[1], -Vect.m_Vector[2]);}
00053 
00055         inline friend GLC_Vector3d operator*(double s, const GLC_Vector3d &v)
00056         {return GLC_Vector3d(s * v.m_Vector[0], s * v.m_Vector[1], s * v.m_Vector[2]);}
00057 
00059 
00061 
00062 public:
00064 
00069         inline GLC_Vector3d();
00070 
00072         inline GLC_Vector3d(double x, double y, double z);
00073 
00075         inline GLC_Vector3d(const GLC_Vector3d &vector)
00076         {memcpy(m_Vector, vector.m_Vector, sizeof(double) * 3);}
00077 
00079         inline GLC_Vector3d(const GLC_Vector3df &vector);
00080 
00082 
00084 
00086 
00087 public:
00089         inline double x() const
00090         {return m_Vector[0];}
00091 
00093         inline double y() const
00094         {return m_Vector[1];}
00095 
00097         inline double z() const
00098         {return m_Vector[2];}
00099 
00101         inline const double *data() const
00102         {return m_Vector;}
00103 
00105         inline bool isNull() const
00106         {return (m_Vector[0] == 0.0f) && (m_Vector[1] == 0.0f) && (m_Vector[2] == 0.0f);}
00107 
00109         inline double length() const
00110         {return sqrt(m_Vector[0] * m_Vector[0] + m_Vector[1] * m_Vector[1] + m_Vector[2] * m_Vector[2]);}
00111 
00113 
00114         inline GLC_Vector2d toVector2d(const GLC_Vector3d& mask) const;
00115 
00117         inline double angleWithVect(GLC_Vector3d Vect) const;
00118 
00120         inline GLC_Vector3df toVector3df() const
00121         {return GLC_Vector3df(static_cast<float>(m_Vector[0]), static_cast<float>(m_Vector[1]), static_cast<float>(m_Vector[2]));}
00122 
00124         inline QString toString() const;
00125 
00127         inline GLC_Vector3d inverted() const
00128         {return GLC_Vector3d(*this).invert();}
00129 
00131 
00133 
00135 
00136 public:
00138         inline GLC_Vector3d operator + (const GLC_Vector3d &vector) const
00139         {return GLC_Vector3d(m_Vector[0] + vector.m_Vector[0], m_Vector[1] + vector.m_Vector[1], m_Vector[2] + vector.m_Vector[2]);}
00140 
00142         inline GLC_Vector3d& operator = (const GLC_Vector3d &vector)
00143         {
00144                 if (this != &vector) memcpy(m_Vector, vector.m_Vector, sizeof(double) * 3);
00145                 return *this;
00146         }
00147 
00149         inline GLC_Vector3d& operator = (const GLC_Vector3df &);
00150 
00152         inline GLC_Vector3d& operator += (const GLC_Vector3d &vector)
00153         {
00154                 *this= *this + vector;
00155                 return *this;
00156         }
00157 
00159         inline GLC_Vector3d operator - (const GLC_Vector3d &Vect) const
00160         {return GLC_Vector3d(m_Vector[0] - Vect.m_Vector[0], m_Vector[1] - Vect.m_Vector[1], m_Vector[2] - Vect.m_Vector[2]);}
00161 
00163         GLC_Vector3d& operator -= (const GLC_Vector3d &Vect)
00164         {
00165                 *this= *this - Vect;
00166                 return *this;
00167         }
00168 
00170         inline GLC_Vector3d operator ^ (const GLC_Vector3d &vector) const;
00171 
00173         inline double operator * (const GLC_Vector3d &Vect) const
00174         {return m_Vector[0] * Vect.m_Vector[0] + m_Vector[1] * Vect.m_Vector[1] + m_Vector[2] * Vect.m_Vector[2];}
00175 
00177         inline GLC_Vector3d operator * (double Scalaire) const
00178         {return GLC_Vector3d(m_Vector[0] * Scalaire, m_Vector[1] * Scalaire, m_Vector[2] * Scalaire);}
00179 
00180 
00182         inline bool operator == (const GLC_Vector3d &vector) const;
00183 
00185         inline bool operator > (const GLC_Vector3d &vector) const;
00186 
00188         inline bool operator < (const GLC_Vector3d &vector) const;
00189 
00191         inline bool operator != (const GLC_Vector3d &Vect) const
00192         {return !(*this == Vect);}
00193 
00195 
00197 
00199 
00200 public:
00202         inline GLC_Vector3d& setX(const double &dX)
00203         {
00204                 m_Vector[0]= dX;
00205                 return *this;
00206         }
00207 
00209         inline GLC_Vector3d& setY(const double &dY)
00210         {
00211                 m_Vector[1]= dY;
00212                 return *this;
00213         }
00214 
00216         inline GLC_Vector3d& setZ(const double &dZ)
00217         {
00218                 m_Vector[2]= dZ;
00219                 return *this;
00220         }
00221 
00223         inline GLC_Vector3d& setVect(double, double, double);
00224 
00226         GLC_Vector3d& setVect(const GLC_Vector3d &vector)
00227         {
00228                 memcpy(m_Vector, vector.m_Vector, sizeof(double) * 3);
00229                 return *this;
00230         }
00231 
00233         inline GLC_Vector3d& setLength(double);
00234 
00236         inline GLC_Vector3d& normalize()
00237         {return setLength(1.0);}
00238 
00240         inline GLC_Vector3d& invert();
00241 
00243 
00244 
00246 //Private attributes
00248 private:
00254         double m_Vector[3];
00255 
00256 }; //class GLC_Vector3d
00257 
00258 // Vector constant in glc namespace
00259 namespace glc
00260 {
00261         // Axis definition
00264         const GLC_Vector3d X_AXIS(1.0, 0.0, 0.0);
00265 
00268         const GLC_Vector3d Y_AXIS(0.0, 1.0, 0.0);
00269 
00272         const GLC_Vector3d Z_AXIS(0.0, 0.0, 1.0);
00273 };
00274 
00276 typedef GLC_Vector3d GLC_Point3d;
00277 
00279 inline QDataStream &operator<<(QDataStream & stream, const GLC_Vector3d & vector)
00280 {
00281         stream << vector.x() << vector.y() << vector.z();
00282         return stream;
00283 }
00284 
00286 inline QDataStream &operator>>(QDataStream &stream, GLC_Vector3d &vector)
00287 {
00288         double x, y, z;
00289         stream >> x >> y >> z;
00290         vector.setVect(x, y, z);
00291         return stream;
00292 }
00293 
00295 // inline method implementation
00297 
00298 GLC_Vector3d::GLC_Vector3d()
00299 {
00300         m_Vector[0]= 0.0;
00301         m_Vector[1]= 0.0;
00302         m_Vector[2]= 0.0;
00303 }
00304 
00305 GLC_Vector3d::GLC_Vector3d(double x, double y, double z)
00306 {
00307         m_Vector[0]= x;
00308         m_Vector[1]= y;
00309         m_Vector[2]= z;
00310 }
00311 
00312 GLC_Vector3d::GLC_Vector3d(const GLC_Vector3df &vector)
00313 {
00314         m_Vector[0]= static_cast<double>(vector.m_Vector[0]);
00315         m_Vector[1]= static_cast<double>(vector.m_Vector[1]);
00316         m_Vector[2]= static_cast<double>(vector.m_Vector[2]);
00317 }
00318 
00319 GLC_Vector3d& GLC_Vector3d::operator = (const GLC_Vector3df &Vect)
00320 {
00321         m_Vector[0]= static_cast<double>(Vect.m_Vector[0]);
00322         m_Vector[1]= static_cast<double>(Vect.m_Vector[1]);
00323         m_Vector[2]= static_cast<double>(Vect.m_Vector[2]);
00324 
00325         return *this;
00326 }
00327 
00328 GLC_Vector3d GLC_Vector3d::operator ^ (const GLC_Vector3d &vector) const
00329 {
00330         GLC_Vector3d vectResult;
00331         vectResult.m_Vector[0]= m_Vector[1] * vector.m_Vector[2] - m_Vector[2] * vector.m_Vector[1];
00332         vectResult.m_Vector[1]= m_Vector[2] * vector.m_Vector[0] - m_Vector[0] * vector.m_Vector[2];
00333         vectResult.m_Vector[2]= m_Vector[0] * vector.m_Vector[1] - m_Vector[1] * vector.m_Vector[0];
00334 
00335         return vectResult;
00336 }
00337 
00338 bool GLC_Vector3d::operator == (const GLC_Vector3d &vector) const
00339 {
00340         bool bResult= qFuzzyCompare(m_Vector[0], vector.m_Vector[0]);
00341         bResult= bResult && qFuzzyCompare(m_Vector[1], vector.m_Vector[1]);
00342         bResult= bResult && qFuzzyCompare(m_Vector[2], vector.m_Vector[2]);
00343 
00344         return bResult;
00345 }
00346 
00347 bool GLC_Vector3d::operator > (const GLC_Vector3d &vector) const
00348 {
00349         bool result= m_Vector[0] > vector.m_Vector[0];
00350         result= result && (m_Vector[1] > vector.m_Vector[1]);
00351         result= result && (m_Vector[2] > vector.m_Vector[2]);
00352         return result;
00353 }
00354 
00355 bool GLC_Vector3d::operator < (const GLC_Vector3d &vector) const
00356 {
00357         bool result= m_Vector[0] < vector.m_Vector[0];
00358         result= result && (m_Vector[1] < vector.m_Vector[1]);
00359         result= result && (m_Vector[2] < vector.m_Vector[2]);
00360         return result;
00361 }
00362 
00363 GLC_Vector3d& GLC_Vector3d::setVect(double x, double y, double z)
00364 {
00365         m_Vector[0]= x;
00366         m_Vector[1]= y;
00367         m_Vector[2]= z;
00368 
00369         return *this;
00370 }
00371 
00372 inline GLC_Vector3d& GLC_Vector3d::setLength(double norme)
00373 {
00374         const double normCur= sqrt( m_Vector[0] * m_Vector[0] + m_Vector[1] * m_Vector[1] + m_Vector[2] * m_Vector[2]);
00375 
00376         if (normCur != 0.0f)
00377         {
00378                 const double Coef = norme / normCur;
00379 
00380                 m_Vector[0] = m_Vector[0] * Coef;
00381                 m_Vector[1] = m_Vector[1] * Coef;
00382                 m_Vector[2] = m_Vector[2] * Coef;
00383         }
00384         return *this;
00385 }
00386 
00387 GLC_Vector3d& GLC_Vector3d::invert()
00388 {
00389         m_Vector[0]= - m_Vector[0];
00390         m_Vector[1]= - m_Vector[1];
00391         m_Vector[2]= - m_Vector[2];
00392         return *this;
00393 }
00394 
00395 GLC_Vector2d GLC_Vector3d::toVector2d(const GLC_Vector3d& mask) const
00396 {
00397         GLC_Vector2d resultVect;
00398         if (mask.m_Vector[0] == 0.0)
00399         {
00400                 resultVect.setX(m_Vector[0]);
00401                 if (mask.m_Vector[1] == 0.0) resultVect.setY(m_Vector[1]);
00402                 else resultVect.setY(m_Vector[2]);
00403         }
00404         else resultVect.setVect(m_Vector[1], m_Vector[2]);
00405 
00406         return resultVect;
00407 }
00408 
00409 double GLC_Vector3d::angleWithVect(GLC_Vector3d Vect) const
00410 {
00411         GLC_Vector3d ThisVect(*this);
00412         ThisVect.normalize();
00413         Vect.normalize();
00414         // Rotation axis
00415         const GLC_Vector3d VectAxeRot(ThisVect ^ Vect);
00416         // Check if the rotation axis vector is null
00417         if (!VectAxeRot.isNull())
00418         {
00419                 return acos(ThisVect * Vect);
00420         }
00421         else return 0.0;
00422 }
00423 
00424 QString GLC_Vector3d::toString() const
00425 {
00426         QString result("[");
00427 
00428         result+= QString::number(m_Vector[0]) + QString(" , ");
00429         result+= QString::number(m_Vector[1]) + QString(" , ");
00430         result+= QString::number(m_Vector[2]) + QString("]");
00431 
00432         return result;
00433 }
00434 
00435 #endif /*GLC_VECTOR3D_H_*/

SourceForge.net Logo

©2005-2010 Laurent Ribon