The Vec class represents 3D positions and 3D vectors. More...
#include <QGLViewer/vec.h>
Public Attributes | |
union { | |
struct { | |
double x | |
double y | |
double z | |
} | |
double v_ [3] | |
}; | |
Setting the value | |
Vec () | |
Vec (double X, double Y, double Z) | |
template<class C > | |
Vec (const C &c) | |
Vec & | operator= (const Vec &v) |
void | setValue (double X, double Y, double Z) |
Accessing the value | |
double | operator[] (int i) const |
double & | operator[] (int i) |
const double * | address () const |
operator const double * () const | |
operator double * () | |
operator const float * () const | |
Algebraic computations | |
Vec & | operator+= (const Vec &a) |
Vec & | operator-= (const Vec &a) |
Vec & | operator*= (double k) |
Vec & | operator/= (double k) |
Vec | orthogonalVec () const |
Vec | operator+ (const Vec &a, const Vec &b) |
Vec | operator- (const Vec &a, const Vec &b) |
Vec | operator- (const Vec &a) |
Vec | operator* (const Vec &a, double k) |
Vec | operator* (double k, const Vec &a) |
Vec | operator/ (const Vec &a, double k) |
bool | operator!= (const Vec &a, const Vec &b) |
bool | operator== (const Vec &a, const Vec &b) |
double | operator* (const Vec &a, const Vec &b) |
Vec | operator^ (const Vec &a, const Vec &b) |
Vec | cross (const Vec &a, const Vec &b) |
Norm of the vector | |
double | sqNorm () const |
double | squaredNorm () const |
double | norm () const |
double | normalize () |
Vec | unit () const |
Projection | |
void | projectOnAxis (const Vec &direction) |
void | projectOnPlane (const Vec &normal) |
XML representation | |
Vec (const QDomElement &element) | |
QDomElement | domElement (const QString &name, QDomDocument &document) const |
void | initFromDOMElement (const QDomElement &element) |
The Vec class represents 3D positions and 3D vectors.
Vec is used as a parameter and return type by many methods of the library. It provides classical algebraic computational methods and is compatible with OpenGL:
// Draws a point located at 3.0 OpenGL units in front of the camera Vec pos = camera()->position() + 3.0 * camera()->viewDirection(); glBegin(GL_POINTS); glVertex3fv(pos); glEnd();
This makes of Vec a good candidate for representing positions and vectors in your programs. Since it is part of the qglviewer
namespace, specify qglviewer::Vec
or use the qglviewer namespace:
using namespace qglviewer;
Vec implements a universal explicit converter, based on the []
operator
. Everywhere a const
Vec&
argument is expected, you can use your own vector type instead, as long as it implements this operator (see the Vec(const C& c) documentation).
See also the Quaternion and the Frame documentations.
qglviewer::Vec::Vec | ( | ) | [inline] |
qglviewer::Vec::Vec | ( | double | X, |
double | Y, | ||
double | Z | ||
) | [inline] |
qglviewer::Vec::Vec | ( | const C & | c | ) | [inline, explicit] |
Universal explicit converter from any class to Vec. You can use your own vector class everywhere a const
Vec&
parameter is required, as long as it implements the operator
[ ]:
class MyVec { // ... double operator[](int i) const { returns x, y or z when i=0, 1 or 2; } } MyVec v(...); camera()->setPosition(v);
Note that standard vector types (STL, double
[3], ...) implement this operator and can hence be used in place of Vec. See also operator const double*() .
Vec::Vec | ( | const QDomElement & | element | ) | [explicit] |
Constructs a Vec from a QDomElement
representing an XML code of the form
< anyTagName x=".." y=".." z=".." />
If one of these attributes is missing or is not a number, a warning is displayed and the associated value is set to 0.0.
See also domElement() and initFromDOMElement().
const double* qglviewer::Vec::address | ( | ) | const [inline] |
QDomElement Vec::domElement | ( | const QString & | name, |
QDomDocument & | document | ||
) | const |
Returns an XML QDomElement
that represents the Vec.
name
is the name of the QDomElement tag. doc
is the QDomDocument
factory used to create QDomElement.
When output to a file, the resulting QDomElement will look like:
<name x=".." y=".." z=".." />
Use initFromDOMElement() to restore the Vec state from the resulting QDomElement
. See also the Vec(const QDomElement&) constructor.
Here is complete example that creates a QDomDocument and saves it into a file:
Vec sunPos; QDomDocument document("myDocument"); QDomElement sunElement = document.createElement("Sun"); document.appendChild(sunElement); sunElement.setAttribute("brightness", sunBrightness()); sunElement.appendChild(sunPos.domElement("sunPosition", document)); // Other additions to the document hierarchy... // Save doc document QFile f("myFile.xml"); if (f.open(IO_WriteOnly)) { QTextStream out(&f); document.save(out, 2); f.close(); }
See also Quaternion::domElement(), Frame::domElement(), Camera::domElement()...
void Vec::initFromDOMElement | ( | const QDomElement & | element | ) |
Restores the Vec state from a QDomElement
created by domElement().
The QDomElement
should contain x
, y
and z
attributes. If one of these attributes is missing or is not a number, a warning is displayed and the associated value is set to 0.0.
To restore the Vec state from an xml file, use:
// Load DOM from file QDomDocument doc; QFile f("myFile.xml"); if (f.open(IO_ReadOnly)) { doc.setContent(&f); f.close(); } // Parse the DOM tree and initialize QDomElement main=doc.documentElement(); myVec.initFromDOMElement(main);
See also the Vec(const QDomElement&) constructor.
double qglviewer::Vec::norm | ( | ) | const [inline] |
double qglviewer::Vec::normalize | ( | ) | [inline] |
qglviewer::Vec::operator const double * | ( | ) | const [inline] |
qglviewer::Vec::operator const float * | ( | ) | const [inline] |
qglviewer::Vec::operator double * | ( | ) | [inline] |
Vec& qglviewer::Vec::operator*= | ( | double | k | ) | [inline] |
Vec& qglviewer::Vec::operator/= | ( | double | k | ) | [inline] |
double qglviewer::Vec::operator[] | ( | int | i | ) | const [inline] |
double& qglviewer::Vec::operator[] | ( | int | i | ) | [inline] |
Vec Vec::orthogonalVec | ( | ) | const |
void Vec::projectOnAxis | ( | const Vec & | direction | ) |
void Vec::projectOnPlane | ( | const Vec & | normal | ) |
void qglviewer::Vec::setValue | ( | double | X, |
double | Y, | ||
double | Z | ||
) | [inline] |
Set the current value. May be faster than using operator=() with a temporary Vec(x,y,z).
double qglviewer::Vec::sqNorm | ( | ) | const [inline] |
This method is deprecated since version 2.0. Use squaredNorm() instead.
double qglviewer::Vec::squaredNorm | ( | ) | const [inline] |
Vec qglviewer::Vec::unit | ( | ) | const [inline] |
Returns true
only when the two vector are not equal (see operator==()).
Returns true
when the squaredNorm() of the difference vector is lower than 1E-10.
union { ... } |
The internal data representation is public. One can use v.x, v.y, v.z. See also operator[]().
double qglviewer::Vec::v_[3] |
double qglviewer::Vec::x |
double qglviewer::Vec::y |
double qglviewer::Vec::z |