Vec Class Reference

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

## Public Attributes

qreal | x
qreal | y
qreal | z

## Setting the value

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

## Accessing the value

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

## Algebraic computations

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

## Output stream

std::ostream & | operator<< (std::ostream &o, const qglviewer::Vec &)

## 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.

## Constructor & Destructor Documentation

## Vec() [1/4]

Vec | ( |  | ) |

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

## Vec() [2/4]

Vec | ( | qreal | X,
|  | qreal | Y,
|  | qreal | Z
| ) |  |

Standard constructor with the x, y and z values.

## Vec() [3/4]

Vec | ( | const C & | c | ) |

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
{
// ...
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*() .

## Vec() [4/4]

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

## Member Function Documentation

## domElement()

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

## initFromDOMElement()

void 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.

## norm()

qreal norm | ( |  | ) | const

Returns the norm of the vector.

## normalize()

qreal normalize | ( |  | ) |

Normalizes the Vec and returns its original norm.

Normalizing a null vector will result in `NaN` values.

## operator const float *()

operator const float * | ( |  | ) | const

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.

## operator const qreal *()

operator const qreal * | ( |  | ) | const

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);

## operator qreal *()

operator qreal * | ( |  | ) |

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

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

## operator*=()

Vec& operator*= | ( | qreal | k | ) |

Multiply the vector by a scalar `k`.

## operator+=()

Vec& operator+= | ( | const Vec & | a | ) |

Adds `a` to the vector.

## operator-=()

Vec& operator-= | ( | const Vec & | a | ) |

Subtracts `a` to the vector.

## operator/=()

Vec& operator/= | ( | qreal | k | ) |

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.

## operator<<()

std::ostream& operator<< | ( | std::ostream & | o,
|  | const qglviewer::Vec & |
| ) |  |

Output stream operator. Enables debugging code like:

Vec pos(...);
cout << "Position=" << pos << endl;

## operator=()

Vec& operator= | ( | const Vec & | v | ) |

Equal operator.

## operator[]() [1/2]

qreal operator[] | ( | int | i | ) | const

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

## operator[]() [2/2]

qreal& operator[] | ( | int | i | ) |

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

## orthogonalVec()

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.

## projectOnAxis()

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

## projectOnPlane()

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

## setValue()

void setValue | ( | qreal | X,
|  | qreal | Y,
|  | qreal | Z
| ) |  |

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

## squaredNorm()

qreal squaredNorm | ( |  | ) | const

Returns the squared norm of the Vec.

## unit()

Vec unit | ( |  | ) | const

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

## Friends And Related Function Documentation

## cross

Vec cross | ( | const Vec & | a,
|  | const Vec & | b
| ) |  |

friend

Cross product of the two Vec. Mind the order !

## operator!=

bool operator!= | ( | const Vec & | a,
|  | const Vec & | b
| ) |  |

friend

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

## operator* [1/3]

Vec operator* | ( | const Vec & | a,
|  | qreal | k
| ) |  |

friend

Returns the product of the vector with a scalar.

## operator* [2/3]

Vec operator* | ( | qreal | k,
|  | const Vec & | a
| ) |  |

friend

Returns the product of a scalar with the vector.

## operator* [3/3]

qreal operator* | ( | const Vec & | a,
|  | const Vec & | b
| ) |  |

friend

Dot product of the two Vec.

## operator+

Vec operator+ | ( | const Vec & | a,
|  | const Vec & | b
| ) |  |

friend

Returns the sum of the two vectors.

## operator- [1/2]

Vec operator- | ( | const Vec & | a,
|  | const Vec & | b
| ) |  |

friend

Returns the difference of the two vectors.

## operator- [2/2]

Vec operator- | ( | const Vec & | a | ) |

friend

Unary minus operator.

## 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.

## operator==

bool operator== | ( | const Vec & | a,
|  | const Vec & | b
| ) |  |

friend

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

## operator^

Vec operator^ | ( | const Vec & | a,
|  | const Vec & | b
| ) |  |

friend

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

## Member Data Documentation

## x

qreal x

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

## y

qreal y

## z

qreal z