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;
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);
231 return Vec(a.x*k, a.y*k, a.z*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);
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;
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
friend Vec operator*(double k, const Vec &a)
Returns the product of a scalar with the vector.
friend Vec operator^(const Vec &a, const Vec &b)
Cross product of the two vectors.
double squaredNorm() const
Returns the squared norm of the Vec.
double sqNorm() const
This method is deprecated since version 2.0.
friend Vec operator-(const Vec &a, const Vec &b)
Returns the difference of the two vectors.
Vec(double X, double Y, double Z)
Standard constructor with the x, y and z values.
double norm() const
Returns the norm of the vector.
Vec & operator*=(double k)
Multiply the vector by a scalar k.
Vec()
Default constructor.
double & operator[](int i)
Bracket operator returning an l-value.
Vec & operator-=(const Vec &a)
Subtracts a to the vector.
Vec & operator+=(const Vec &a)
Adds a to the vector.
The Vec class represents 3D positions and 3D vectors.
friend bool operator!=(const Vec &a, const Vec &b)
Returns true only when the two vector are not equal (see operator==()).
double operator[](int i) const
Bracket operator, with a constant return value.
friend Vec operator/(const Vec &a, double k)
Returns the division of the vector with a scalar.
Vec & operator/=(double k)
Divides the vector by a scalar k.
const double * address() const
This method is deprecated since version 2.0.
Vec unit() const
Returns a unitary (normalized) representation of the vector.
friend double operator*(const Vec &a, const Vec &b)
Dot product of the two Vec.
void setValue(double X, double Y, double Z)
Set the current value.
Vec(const C &c)
Universal explicit converter from any class to Vec.
friend Vec operator-(const Vec &a)
Unary minus operator.
friend Vec cross(const Vec &a, const Vec &b)
Cross product of the two Vec.
double normalize()
Normalizes the Vec and returns its original norm.
friend bool operator==(const Vec &a, const Vec &b)
Returns true when the squaredNorm() of the difference vector is lower than 1E-10. ...
Vec & operator=(const Vec &v)
Equal operator.
friend Vec operator*(const Vec &a, double k)
Returns the product of the vector with a scalar.
friend Vec operator+(const Vec &a, const Vec &b)
Returns the sum of the two vectors.