23 #ifndef QGLVIEWER_VEC_H
24 #define QGLVIEWER_VEC_H
29 # include <QDomElement>
70 #if defined (Q_OS_IRIX) || defined (Q_OS_AIX) || defined (Q_OS_HPUX)
71 # define QGLVIEWER_UNION_NOT_SUPPORTED
76 #if defined (DOXYGEN) || defined (QGLVIEWER_UNION_NOT_SUPPORTED)
81 struct { qreal x, y, z; };
89 Vec() : x(0.0), y(0.0), z(0.0) {}
92 Vec(qreal X, qreal Y, qreal Z) : x(X), y(Y), z(Z) {}
111 explicit Vec(
const C& c) : x(c[0]), y(c[1]), z(c[2]) {}
118 Vec& operator=(
const Vec& v)
120 x = v.
x; y = v.
y; z = v.
z;
125 void setValue(qreal X, qreal Y, qreal Z)
141 qreal operator[](
int i)
const {
142 #ifdef QGLVIEWER_UNION_NOT_SUPPORTED
150 qreal& operator[](
int i) {
151 #ifdef QGLVIEWER_UNION_NOT_SUPPORTED
160 const double* address()
const { qWarning(
"Vec::address() is deprecated, use operator const double* instead.");
return operator const double*(); }
171 operator const double*()
const {
172 #ifdef QGLVIEWER_UNION_NOT_SUPPORTED
183 #ifdef QGLVIEWER_UNION_NOT_SUPPORTED
199 operator const float*()
const {
200 static float*
const result =
new float[3];
201 result[0] = (float)x;
202 result[1] = (float)y;
203 result[2] = (float)z;
211 friend Vec operator+(
const Vec &a,
const Vec &b)
217 friend Vec operator-(
const Vec &a,
const Vec &b)
219 return Vec(a.x-b.x, a.y-b.y, a.z-b.z);
223 friend Vec operator-(
const Vec &a)
225 return Vec(-a.
x, -a.
y, -a.
z);
229 friend Vec
operator*(
const Vec &a, qreal k)
231 return Vec(a.x*k, a.y*k, a.z*k);
244 friend Vec operator/(
const Vec &a, qreal k)
247 if (fabs(k) < 1.0E-10)
248 qWarning(
"Vec::operator / : dividing by a null value (%f)", k);
250 return Vec(a.
x/k, a.
y/k, a.
z/k);
254 friend bool operator!=(
const Vec &a,
const Vec &b)
260 friend bool operator==(
const Vec &a,
const Vec &b)
262 const qreal epsilon = 1.0E-10;
263 return (a-b).squaredNorm() < epsilon;
267 Vec& operator+=(
const Vec &a)
269 x += a.
x; y += a.
y; z += a.
z;
276 x -= a.
x; y -= a.
y; z -= a.
z;
281 Vec& operator*=(qreal k)
283 x *= k; y *= k; z *= k;
291 Vec& operator/=(qreal k)
295 qWarning(
"Vec::operator /= : dividing by a null value (%f)", k);
297 x /= k; y /= k; z /= k;
304 return a.
x*b.
x + a.
y*b.
y + a.
z*b.
z;
308 friend Vec operator^(
const Vec &a,
const Vec &b)
314 friend Vec cross(
const Vec &a,
const Vec &b)
321 Vec orthogonalVec()
const;
328 qreal sqNorm()
const {
return x*x + y*y + z*z; }
332 qreal squaredNorm()
const {
return x*x + y*y + z*z; }
335 qreal norm()
const {
return sqrt(x*x + y*y + z*z); }
342 const qreal n = norm();
345 qWarning(
"Vec::normalize: normalizing a null vector (norm=%f)", n);
362 void projectOnAxis(
const Vec& direction);
363 void projectOnPlane(
const Vec& normal);
368 explicit Vec(
const QDomElement& element);
369 QDomElement domElement(
const QString& name, QDomDocument& document)
const;
370 void initFromDOMElement(
const QDomElement& element);
390 #endif // QGLVIEWER_VEC_H