00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef QGLVIEWER_VEC_H
00026 #define QGLVIEWER_VEC_H
00027
00028 #include <math.h>
00029 #include <iostream>
00030 #include <qdom.h>
00031 #include "qapplication.h"
00032
00033
00034 #include "config.h"
00035
00036 namespace qglviewer {
00037
00071 class QGLVIEWER_EXPORT Vec
00072 {
00073 public:
00075 float x, y, z;
00076
00079
00080 explicit Vec() : x(0.0), y(0.0), z(0.0) {}
00081
00083 explicit Vec(const float X, const float Y, const float Z) : x(X), y(Y), z(Z) {}
00084
00087
00096 template <class C>
00097 explicit Vec(const C& c) : x(c[0]), y(c[1]), z(c[2]) {}
00098
00100
00101
00103 Vec& operator=(const Vec& v)
00104 {
00105 x = v.x; y = v.y; z = v.z;
00106 return *this;
00107 }
00108
00110 void setValue(const float X, const float Y, const float Z)
00111 { x=X; y=Y; z=Z; }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00122
00125
00126 float operator[](int i) const { return (&x)[i]; }
00127
00129 float& operator[](int i) { return (&x)[i]; }
00130
00132 const float* address() const { return &x; };
00133
00135 operator const float*() const { return &x; };
00137
00140
00141 friend Vec operator+(const Vec &a, const Vec &b)
00142 {
00143 return Vec(a.x+b.x, a.y+b.y, a.z+b.z);
00144 }
00145
00147 friend Vec operator-(const Vec &a, const Vec &b)
00148 {
00149 return Vec(a.x-b.x, a.y-b.y, a.z-b.z);
00150 }
00151
00153 friend Vec operator-(const Vec &a)
00154 {
00155 return Vec(-a.x, -a.y, -a.z);
00156 }
00157
00159 friend Vec operator*(const Vec &a, const float k)
00160 {
00161 return Vec(a.x*k, a.y*k, a.z*k);
00162 }
00163
00165 friend Vec operator*(float k, const Vec &a)
00166 {
00167 return Vec(a.x*k, a.y*k, a.z*k);
00168 }
00169
00171 friend Vec operator/(const Vec &a, const float k)
00172 {
00173 #ifndef QT_NO_DEBUG
00174 if (fabs(k) < 1.0E-10)
00175 qWarning("Vec::operator / : dividing by a null value");
00176 #endif
00177 return Vec(a.x/k, a.y/k, a.z/k);
00178 }
00179
00181 friend bool operator!=(const Vec &a, const Vec &b)
00182 {
00183 return !(a==b);
00184 }
00185
00187 friend bool operator==(const Vec &a, const Vec &b)
00188 {
00189 const float epsilon = 1.0E-10f;
00190 return (a-b).sqNorm() < epsilon;
00191 }
00192
00194 Vec& operator+=(const Vec &a)
00195 {
00196 x += a.x; y += a.y; z += a.z;
00197 return *this;
00198 }
00199
00201 Vec& operator-=(const Vec &a)
00202 {
00203 x -= a.x; y -= a.y; z -= a.z;
00204 return *this;
00205 }
00206
00208 Vec& operator*=(float k)
00209 {
00210 x *= k; y *= k; z *= k;
00211 return *this;
00212 }
00213
00215 Vec& operator/=(float k)
00216 {
00217 #ifndef QT_NO_DEBUG
00218 if (fabs(k)<1.0E-10)
00219 qWarning("Vec::operator /= : dividing by a null value");
00220 #endif
00221 x /= k; y /= k; z /= k;
00222 return *this;
00223 }
00224
00226 friend float operator*(const Vec &a, const Vec &b)
00227 {
00228 return a.x*b.x + a.y*b.y + a.z*b.z;
00229 }
00230
00232 friend Vec cross(const Vec &a, const Vec &b)
00233 {
00234 return Vec(a.y*b.z - a.z*b.y,
00235 a.z*b.x - a.x*b.z,
00236 a.x*b.y - a.y*b.x);
00237 }
00238
00240 friend Vec operator^(const Vec &a, const Vec &b)
00241 {
00242 return Vec(a.y*b.z - a.z*b.y,
00243 a.z*b.x - a.x*b.z,
00244 a.x*b.y - a.y*b.x);
00245 }
00247
00250
00251 float sqNorm() const { return x*x + y*y + z*z; }
00252
00254 float norm() const { return sqrt(x*x + y*y + z*z); }
00255
00257 Vec& normalize()
00258 {
00259 const float n = norm();
00260 #ifndef QT_NO_DEBUG
00261 if (n < 1.0E-10)
00262 qWarning("Vec::normalize : normalizing a null vector");
00263 #endif
00264 *this /= n;
00265 return *this;
00266 }
00268
00271 void projectOnAxis(const Vec& dir);
00272 void projectOnPlane(const Vec& n);
00274
00279 QDomElement domElement(const QString& name, QDomDocument& doc) const;
00280 void initFromDOMElement(const QDomElement& de);
00282 };
00283
00284 }
00285
00286 std::ostream& operator<<(std::ostream& o, const qglviewer::Vec&);
00287
00288 #endif // QGLVIEWER_VEC_H