23 #ifndef QGLVIEWER_VEC_H
24 #define QGLVIEWER_VEC_H
29 # include <QDomElement>
65 class QGLVIEWER_EXPORT
Vec
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 {
double x, y, z; };
89 Vec() : x(0.0), y(0.0), z(0.0) {}
92 Vec(
double X,
double Y,
double Z) : x(X), y(Y), z(Z) {}
111 explicit Vec(
const C& c) : x(c[0]), y(c[1]), z(c[2]) {}
120 x = v.x; y = v.y; z = v.z;
125 void setValue(
double X,
double Y,
double Z)
142 #ifdef QGLVIEWER_UNION_NOT_SUPPORTED
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;
213 return Vec(a.x+b.x, a.y+b.y, a.z+b.z);
219 return Vec(a.x-b.x, a.y-b.y, a.z-b.z);
225 return Vec(-a.x, -a.y, -a.z);
229 friend Vec operator*(
const Vec &a,
double k)
231 return Vec(a.x*k, a.y*k, a.z*k);
235 friend Vec operator*(
double k,
const Vec &a)
244 friend Vec operator/(
const Vec &a,
double 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 double epsilon = 1.0E-10f;
263 return (a-b).squaredNorm() < epsilon;
269 x += a.x; y += a.y; z += a.z;
276 x -= a.x; y -= a.y; z -= a.z;
283 x *= k; y *= k; z *= k;
295 qWarning(
"Vec::operator /= : dividing by a null value (%f)", k);
297 x /= k; y /= k; z /= k;
302 friend double operator*(
const Vec &a,
const Vec &b)
304 return a.x*b.x + a.y*b.y + a.z*b.z;
316 return Vec(a.y*b.z - a.z*b.y,
321 Vec orthogonalVec()
const;
328 double sqNorm()
const {
return x*x + y*y + z*z; }
335 double norm()
const {
return sqrt(x*x + y*y + z*z); }
342 const double 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