Public Attributes | List of all members
qglviewer::Vec Class Reference

The Vec class represents 3D positions and 3D vectors. More...

#include <QGLViewer/vec.h>

Public Attributes

union {
   struct {
      qreal   x
 
      qreal   y
 
      qreal   z
 
   } 
 
   qreal   v_ [3]
 
}; 
 

Setting the value

 Vec ()
 
 Vec (qreal X, qreal Y, qreal Z)
 
template<class C >
 Vec (const C &c)
 
Vecoperator= (const Vec &v)
 
void setValue (qreal X, qreal Y, qreal Z)
 

Accessing the value

qreal operator[] (int i) const
 
qreal & operator[] (int i)
 
const double * address () const
 
 operator const double * () const
 
 operator double * ()
 
 operator const float * () const
 

Algebraic computations

Vecoperator+= (const Vec &a)
 
Vecoperator-= (const Vec &a)
 
Vecoperator*= (qreal k)
 
Vecoperator/= (qreal 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, qreal k)
 
Vec operator* (qreal k, const Vec &a)
 
Vec operator/ (const Vec &a, qreal k)
 
bool operator!= (const Vec &a, const Vec &b)
 
bool operator== (const Vec &a, const Vec &b)
 
qreal 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

qreal sqNorm () const
 
qreal squaredNorm () const
 
qreal norm () const
 
qreal 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)
 

Detailed Description

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;

Interface with other vector classes

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.

Definition at line 85 of file vec.h.

Constructor & Destructor Documentation

◆ Vec() [1/4]

qglviewer::Vec::Vec ( )
inline

Default constructor. Value is set to (0,0,0).

Definition at line 109 of file vec.h.

◆ Vec() [2/4]

qglviewer::Vec::Vec ( qreal  X,
qreal  Y,
qreal  Z 
)
inline

Standard constructor with the x, y and z values.

Definition at line 112 of file vec.h.

◆ Vec() [3/4]

template<class C >
qglviewer::Vec::Vec ( const C &  c)
inlineexplicit

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
{
// ...
qreal 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, qreal[3], ...) implement this operator and can hence be used in place of Vec. See also operator const qreal*() .

Definition at line 131 of file vec.h.

◆ Vec() [4/4]

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().

Definition at line 78 of file vec.cpp.

Member Function Documentation

◆ address()

const double* qglviewer::Vec::address ( ) const
inline

This method is deprecated since version 2.0. Use operator const double* instead.

Definition at line 180 of file vec.h.

◆ domElement()

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()...

Definition at line 124 of file vec.cpp.

◆ initFromDOMElement()

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.

Definition at line 154 of file vec.cpp.

◆ norm()

qreal qglviewer::Vec::norm ( ) const
inline

Returns the norm of the vector.

Definition at line 355 of file vec.h.

◆ normalize()

qreal qglviewer::Vec::normalize ( )
inline

Normalizes the Vec and returns its original norm.

Normalizing a null vector will result in NaN values.

Definition at line 360 of file vec.h.

◆ operator const double *()

qglviewer::Vec::operator const double * ( ) const
inline

Conversion operator returning the memory address of the vector.

Very convenient to pass a Vec pointer as a parameter to GLdouble OpenGL functions:

Vec pos, normal;
glNormal3dv(normal);
glVertex3dv(pos);

Definition at line 191 of file vec.h.

◆ operator const float *()

qglviewer::Vec::operator const float * ( ) const
inline

Conversion operator returning the memory address of the vector.

Very convenient to pass a Vec pointer as a float parameter to OpenGL functions:

Vec pos, normal;
glNormal3fv(normal);
glVertex3fv(pos);
Note
The returned float array is a static shared by all Vec instances.

Definition at line 219 of file vec.h.

◆ operator double *()

qglviewer::Vec::operator double * ( )
inline

Non const conversion operator returning the memory address of the vector.

Useful to pass a Vec to a method that requires and fills a double*, as provided by certain libraries.

Definition at line 202 of file vec.h.

◆ operator*=()

Vec& qglviewer::Vec::operator*= ( qreal  k)
inline

Multiply the vector by a scalar k.

Definition at line 301 of file vec.h.

◆ operator+=()

Vec& qglviewer::Vec::operator+= ( const Vec a)
inline

Adds a to the vector.

Definition at line 287 of file vec.h.

◆ operator-=()

Vec& qglviewer::Vec::operator-= ( const Vec a)
inline

Subtracts a to the vector.

Definition at line 294 of file vec.h.

◆ operator/=()

Vec& qglviewer::Vec::operator/= ( qreal  k)
inline

Divides the vector by a scalar k.

An absolute k value lower than 1E-10 will print a warning if the library was compiled with the "debug" Qt CONFIG flag. Otherwise, no test is performed for efficiency reasons.

Definition at line 311 of file vec.h.

◆ operator=()

Vec& qglviewer::Vec::operator= ( const Vec v)
inline

Equal operator.

Definition at line 138 of file vec.h.

◆ operator[]() [1/2]

qreal& qglviewer::Vec::operator[] ( int  i)
inline

Bracket operator returning an l-value. i must range in [0..2].

Definition at line 170 of file vec.h.

◆ operator[]() [2/2]

qreal qglviewer::Vec::operator[] ( int  i) const
inline

Bracket operator, with a constant return value. i must range in [0..2].

Definition at line 161 of file vec.h.

◆ orthogonalVec()

Vec Vec::orthogonalVec ( ) const

Returns a Vec orthogonal to the Vec. Its norm() depends on the Vec, but is zero only for a null Vec. Note that the function that associates an orthogonalVec() to a Vec is not continous.

Definition at line 59 of file vec.cpp.

◆ projectOnAxis()

void Vec::projectOnAxis ( const Vec direction)

Projects the Vec on the axis of direction direction that passes through the origin.

direction does not need to be normalized (but must be non null).

Definition at line 34 of file vec.cpp.

◆ projectOnPlane()

void Vec::projectOnPlane ( const Vec normal)

Projects the Vec on the plane whose normal is normal that passes through the origin.

normal does not need to be normalized (but must be non null).

Definition at line 47 of file vec.cpp.

◆ setValue()

void qglviewer::Vec::setValue ( qreal  X,
qreal  Y,
qreal  Z 
)
inline

Set the current value. May be faster than using operator=() with a temporary Vec(x,y,z).

Definition at line 145 of file vec.h.

◆ sqNorm()

qreal qglviewer::Vec::sqNorm ( ) const
inline

This method is deprecated since version 2.0. Use squaredNorm() instead.

Definition at line 348 of file vec.h.

◆ squaredNorm()

qreal qglviewer::Vec::squaredNorm ( ) const
inline

Returns the squared norm of the Vec.

Definition at line 352 of file vec.h.

◆ unit()

Vec qglviewer::Vec::unit ( ) const
inline

Returns a unitary (normalized) representation of the vector. The original Vec is not modified.

Definition at line 372 of file vec.h.

Friends And Related Function Documentation

◆ cross

Vec cross ( const Vec a,
const Vec b 
)
friend

Cross product of the two Vec. Mind the order !

Definition at line 334 of file vec.h.

◆ operator!=

bool operator!= ( const Vec a,
const Vec b 
)
friend

Returns true only when the two vector are not equal (see operator==()).

Definition at line 274 of file vec.h.

◆ operator* [1/3]

qreal operator* ( const Vec a,
const Vec b 
)
friend

Dot product of the two Vec.

Definition at line 322 of file vec.h.

◆ operator* [2/3]

Vec operator* ( const Vec a,
qreal  k 
)
friend

Returns the product of the vector with a scalar.

Definition at line 249 of file vec.h.

◆ operator* [3/3]

Vec operator* ( qreal  k,
const Vec a 
)
friend

Returns the product of a scalar with the vector.

Definition at line 255 of file vec.h.

◆ operator+

Vec operator+ ( const Vec a,
const Vec b 
)
friend

Returns the sum of the two vectors.

Definition at line 231 of file vec.h.

◆ operator- [1/2]

Vec operator- ( const Vec a)
friend

Unary minus operator.

Definition at line 243 of file vec.h.

◆ operator- [2/2]

Vec operator- ( const Vec a,
const Vec b 
)
friend

Returns the difference of the two vectors.

Definition at line 237 of file vec.h.

◆ operator/

Vec operator/ ( const Vec a,
qreal  k 
)
friend

Returns the division of the vector with a scalar.

Too small k values are not tested (unless the library was compiled with the "debug" Qt CONFIG flag) and may result in NaN values.

Definition at line 264 of file vec.h.

◆ operator==

bool operator== ( const Vec a,
const Vec b 
)
friend

Returns true when the squaredNorm() of the difference vector is lower than 1E-10.

Definition at line 280 of file vec.h.

◆ operator^

Vec operator^ ( const Vec a,
const Vec b 
)
friend

Cross product of the two vectors. Same as cross().

Definition at line 328 of file vec.h.

Member Data Documentation

◆ @1

union { ... }

The internal data representation is public. One can use v.x, v.y, v.z. See also operator[]().

◆ v_

qreal qglviewer::Vec::v_[3]

Definition at line 102 of file vec.h.

◆ x

qreal qglviewer::Vec::x

Definition at line 101 of file vec.h.

◆ y

qreal qglviewer::Vec::y

Definition at line 101 of file vec.h.

◆ z

qreal qglviewer::Vec::z

Definition at line 101 of file vec.h.


The documentation for this class was generated from the following files:
qglviewer::Vec::Vec
Vec()
Definition: vec.h:109
qglviewer::Vec::x
qreal x
Definition: vec.h:101
qglviewer
Definition: camera.h:29
qglviewer::Vec::operator[]
qreal operator[](int i) const
Definition: vec.h:161
qglviewer::Vec::z
qreal z
Definition: vec.h:101
qglviewer::Vec::y
qreal y
Definition: vec.h:101
main
int main(int argc, char *argv[])
Definition: main.cpp:30


octovis
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Thu Apr 3 2025 02:40:44