00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
00025 #ifndef GLC_VECTOR3D_H_
00026 #define GLC_VECTOR3D_H_
00027
00028 #include <QDataStream>
00029
00030 #include "glc_utils_maths.h"
00031 #include "glc_vector3df.h"
00032 #include "glc_vector2d.h"
00033 #include "../glc_config.h"
00034
00037
00041
00042
00043 class GLC_LIB_EXPORT GLC_Vector3d
00044 {
00045 friend class GLC_Vector4d;
00046 friend class GLC_Matrix4x4;
00047
00049 inline friend GLC_Vector3d operator - (const GLC_Vector3d &Vect)
00050 {return GLC_Vector3d(-Vect.m_Vector[0], -Vect.m_Vector[1], -Vect.m_Vector[2]);}
00051
00053 inline friend GLC_Vector3d operator*(double s, const GLC_Vector3d &v)
00054 {return GLC_Vector3d(s * v.m_Vector[0], s * v.m_Vector[1], s * v.m_Vector[2]);}
00055
00057
00059
00060 public:
00062
00067 inline GLC_Vector3d();
00068
00070 inline GLC_Vector3d(double x, double y, double z);
00071
00073 inline GLC_Vector3d(const GLC_Vector3d &vector)
00074 {memcpy(m_Vector, vector.m_Vector, sizeof(double) * 3);}
00075
00077 inline GLC_Vector3d(const GLC_Vector3df &vector);
00078
00080
00082
00084
00085 public:
00087 inline double x() const
00088 {return m_Vector[0];}
00089
00091 inline double y() const
00092 {return m_Vector[1];}
00093
00095 inline double z() const
00096 {return m_Vector[2];}
00097
00099 inline const double *data() const
00100 {return m_Vector;}
00101
00103 inline bool isNull() const
00104 {return (m_Vector[0] == 0.0f) && (m_Vector[1] == 0.0f) && (m_Vector[2] == 0.0f);}
00105
00107 inline double length() const
00108 {return sqrt(m_Vector[0] * m_Vector[0] + m_Vector[1] * m_Vector[1] + m_Vector[2] * m_Vector[2]);}
00109
00111
00112 inline GLC_Vector2d toVector2d(const GLC_Vector3d& mask) const;
00113
00115 inline double angleWithVect(GLC_Vector3d Vect) const;
00116
00118 inline GLC_Vector3df toVector3df() const
00119 {return GLC_Vector3df(static_cast<float>(m_Vector[0]), static_cast<float>(m_Vector[1]), static_cast<float>(m_Vector[2]));}
00120
00122 inline QString toString() const;
00123
00125 inline GLC_Vector3d inverted() const
00126 {return GLC_Vector3d(*this).invert();}
00127
00129
00131
00133
00134 public:
00136 inline GLC_Vector3d operator + (const GLC_Vector3d &vector) const
00137 {return GLC_Vector3d(m_Vector[0] + vector.m_Vector[0], m_Vector[1] + vector.m_Vector[1], m_Vector[2] + vector.m_Vector[2]);}
00138
00140 inline GLC_Vector3d& operator = (const GLC_Vector3d &vector)
00141 {
00142 if (this != &vector) memcpy(m_Vector, vector.m_Vector, sizeof(double) * 3);
00143 return *this;
00144 }
00145
00147 inline GLC_Vector3d& operator = (const GLC_Vector3df &);
00148
00150 inline GLC_Vector3d& operator += (const GLC_Vector3d &vector)
00151 {
00152 *this= *this + vector;
00153 return *this;
00154 }
00155
00157 inline GLC_Vector3d operator - (const GLC_Vector3d &Vect) const
00158 {return GLC_Vector3d(m_Vector[0] - Vect.m_Vector[0], m_Vector[1] - Vect.m_Vector[1], m_Vector[2] - Vect.m_Vector[2]);}
00159
00161 GLC_Vector3d& operator -= (const GLC_Vector3d &Vect)
00162 {
00163 *this= *this - Vect;
00164 return *this;
00165 }
00166
00168 inline GLC_Vector3d operator ^ (const GLC_Vector3d &vector) const;
00169
00171 inline double operator * (const GLC_Vector3d &Vect) const
00172 {return m_Vector[0] * Vect.m_Vector[0] + m_Vector[1] * Vect.m_Vector[1] + m_Vector[2] * Vect.m_Vector[2];}
00173
00175 inline GLC_Vector3d operator * (double Scalaire) const
00176 {return GLC_Vector3d(m_Vector[0] * Scalaire, m_Vector[1] * Scalaire, m_Vector[2] * Scalaire);}
00177
00178
00180 inline bool operator == (const GLC_Vector3d &vector) const;
00181
00183 inline bool operator > (const GLC_Vector3d &vector) const;
00184
00186 inline bool operator < (const GLC_Vector3d &vector) const;
00187
00189 inline bool operator != (const GLC_Vector3d &Vect) const
00190 {return !(*this == Vect);}
00191
00193
00195
00197
00198 public:
00200 inline GLC_Vector3d& setX(const double &dX)
00201 {
00202 m_Vector[0]= dX;
00203 return *this;
00204 }
00205
00207 inline GLC_Vector3d& setY(const double &dY)
00208 {
00209 m_Vector[1]= dY;
00210 return *this;
00211 }
00212
00214 inline GLC_Vector3d& setZ(const double &dZ)
00215 {
00216 m_Vector[2]= dZ;
00217 return *this;
00218 }
00219
00221 inline GLC_Vector3d& setVect(double, double, double);
00222
00224 GLC_Vector3d& setVect(const GLC_Vector3d &vector)
00225 {
00226 memcpy(m_Vector, vector.m_Vector, sizeof(double) * 3);
00227 return *this;
00228 }
00229
00231 inline GLC_Vector3d& setLength(double);
00232
00234 inline GLC_Vector3d& normalize()
00235 {return setLength(1.0);}
00236
00238 inline GLC_Vector3d& invert();
00239
00241
00242
00244
00246 private:
00252 double m_Vector[3];
00253
00254 };
00255
00256
00257 namespace glc
00258 {
00259
00262 const GLC_Vector3d X_AXIS(1.0, 0.0, 0.0);
00263
00266 const GLC_Vector3d Y_AXIS(0.0, 1.0, 0.0);
00267
00270 const GLC_Vector3d Z_AXIS(0.0, 0.0, 1.0);
00271 };
00272
00274 typedef GLC_Vector3d GLC_Point3d;
00275
00277 inline QDataStream &operator<<(QDataStream & stream, const GLC_Vector3d & vector)
00278 {
00279 stream << vector.x() << vector.y() << vector.z();
00280 return stream;
00281 }
00282
00284 inline QDataStream &operator>>(QDataStream &stream, GLC_Vector3d &vector)
00285 {
00286 double x, y, z;
00287 stream >> x >> y >> z;
00288 vector.setVect(x, y, z);
00289 return stream;
00290 }
00291
00293
00295
00296 GLC_Vector3d::GLC_Vector3d()
00297 {
00298 m_Vector[0]= 0.0;
00299 m_Vector[1]= 0.0;
00300 m_Vector[2]= 0.0;
00301 }
00302
00303 GLC_Vector3d::GLC_Vector3d(double x, double y, double z)
00304 {
00305 m_Vector[0]= x;
00306 m_Vector[1]= y;
00307 m_Vector[2]= z;
00308 }
00309
00310 GLC_Vector3d::GLC_Vector3d(const GLC_Vector3df &vector)
00311 {
00312 m_Vector[0]= static_cast<double>(vector.m_Vector[0]);
00313 m_Vector[1]= static_cast<double>(vector.m_Vector[1]);
00314 m_Vector[2]= static_cast<double>(vector.m_Vector[2]);
00315 }
00316
00317 GLC_Vector3d& GLC_Vector3d::operator = (const GLC_Vector3df &Vect)
00318 {
00319 m_Vector[0]= static_cast<double>(Vect.m_Vector[0]);
00320 m_Vector[1]= static_cast<double>(Vect.m_Vector[1]);
00321 m_Vector[2]= static_cast<double>(Vect.m_Vector[2]);
00322
00323 return *this;
00324 }
00325
00326 GLC_Vector3d GLC_Vector3d::operator ^ (const GLC_Vector3d &vector) const
00327 {
00328 GLC_Vector3d vectResult;
00329 vectResult.m_Vector[0]= m_Vector[1] * vector.m_Vector[2] - m_Vector[2] * vector.m_Vector[1];
00330 vectResult.m_Vector[1]= m_Vector[2] * vector.m_Vector[0] - m_Vector[0] * vector.m_Vector[2];
00331 vectResult.m_Vector[2]= m_Vector[0] * vector.m_Vector[1] - m_Vector[1] * vector.m_Vector[0];
00332
00333 return vectResult;
00334 }
00335
00336 bool GLC_Vector3d::operator == (const GLC_Vector3d &vector) const
00337 {
00338 bool bResult= qFuzzyCompare(m_Vector[0], vector.m_Vector[0]);
00339 bResult= bResult && qFuzzyCompare(m_Vector[1], vector.m_Vector[1]);
00340 bResult= bResult && qFuzzyCompare(m_Vector[2], vector.m_Vector[2]);
00341
00342 return bResult;
00343 }
00344
00345 bool GLC_Vector3d::operator > (const GLC_Vector3d &vector) const
00346 {
00347 bool result= m_Vector[0] > vector.m_Vector[0];
00348 result= result && (m_Vector[1] > vector.m_Vector[1]);
00349 result= result && (m_Vector[2] > vector.m_Vector[2]);
00350 return result;
00351 }
00352
00353 bool GLC_Vector3d::operator < (const GLC_Vector3d &vector) const
00354 {
00355 bool result= m_Vector[0] < vector.m_Vector[0];
00356 result= result && (m_Vector[1] < vector.m_Vector[1]);
00357 result= result && (m_Vector[2] < vector.m_Vector[2]);
00358 return result;
00359 }
00360
00361 GLC_Vector3d& GLC_Vector3d::setVect(double x, double y, double z)
00362 {
00363 m_Vector[0]= x;
00364 m_Vector[1]= y;
00365 m_Vector[2]= z;
00366
00367 return *this;
00368 }
00369
00370 inline GLC_Vector3d& GLC_Vector3d::setLength(double norme)
00371 {
00372 const double normCur= sqrt( m_Vector[0] * m_Vector[0] + m_Vector[1] * m_Vector[1] + m_Vector[2] * m_Vector[2]);
00373
00374 if (normCur != 0.0f)
00375 {
00376 const double Coef = norme / normCur;
00377
00378 m_Vector[0] = m_Vector[0] * Coef;
00379 m_Vector[1] = m_Vector[1] * Coef;
00380 m_Vector[2] = m_Vector[2] * Coef;
00381 }
00382 return *this;
00383 }
00384
00385 GLC_Vector3d& GLC_Vector3d::invert()
00386 {
00387 m_Vector[0]= - m_Vector[0];
00388 m_Vector[1]= - m_Vector[1];
00389 m_Vector[2]= - m_Vector[2];
00390 return *this;
00391 }
00392
00393 GLC_Vector2d GLC_Vector3d::toVector2d(const GLC_Vector3d& mask) const
00394 {
00395 GLC_Vector2d resultVect;
00396 if (mask.m_Vector[0] == 0.0)
00397 {
00398 resultVect.setX(m_Vector[0]);
00399 if (mask.m_Vector[1] == 0.0) resultVect.setY(m_Vector[1]);
00400 else resultVect.setY(m_Vector[2]);
00401 }
00402 else resultVect.setVect(m_Vector[1], m_Vector[2]);
00403
00404 return resultVect;
00405 }
00406
00407 double GLC_Vector3d::angleWithVect(GLC_Vector3d Vect) const
00408 {
00409 GLC_Vector3d ThisVect(*this);
00410 ThisVect.normalize();
00411 Vect.normalize();
00412
00413 const GLC_Vector3d VectAxeRot(ThisVect ^ Vect);
00414
00415 if (!VectAxeRot.isNull())
00416 {
00417 return acos(ThisVect * Vect);
00418 }
00419 else return 0.0;
00420 }
00421
00422 QString GLC_Vector3d::toString() const
00423 {
00424 QString result("[");
00425
00426 result+= QString::number(m_Vector[0]) + QString(" , ");
00427 result+= QString::number(m_Vector[1]) + QString(" , ");
00428 result+= QString::number(m_Vector[2]) + QString("]");
00429
00430 return result;
00431 }
00432
00433 #endif