CGPdocs

quaternion

Quaternion, used to represent rotations.

Derives from: numarray_stack<float,4> (vec4)

module 09_geometric_transformationsource: quaternion.hpp
A quaternion is a vec4 (x, y, z, w) with the scalar part w last: a rotation of angle theta around the unit axis u is (sin(theta/2) u, cos(theta/2)).
A quaternion is a vec4 (x, y, z, w) with the scalar part w last: a rotation of angle theta around the unit axis u is (sin(theta/2) u, cos(theta/2)). Tap to enlarge.

Fields and methods

Members of the struct quaternion. Most of what you do with a CGP type goes through the free functions listed below.

Inherited from vec4 (18 members)

Fields

T xFirst coordinate of the vector.
T ySecond coordinate of the vector.
T zThird coordinate of the vector.
T wFourth coordinate of the vector.

Methods

Number of components N, fixed at compile time. src
Sets the N components to the given value and returns the vector. src
Returns the element at index, bound-checked unless CGP_NO_DEBUG is defined. src
Returns a const reference to the element at the given index, with bound checking unless CGP_NO_DEBUG is defined. src
Returns a const reference to the element at the given index; bound-checked for sizes 2, 3 and 4 (vec2, vec3, vec4) unless CGP_NO_DEBUG is defined, but not checked for the other sizes N. src
Returns a const iterator to the first element (a plain pointer for sizes 2, 3 and 4). src
Returns a const iterator one past the last element (a plain pointer for sizes 2, 3 and 4). src
Returns a const iterator to the first element (a plain pointer for sizes 2, 3 and 4). src
Returns a const iterator one past the last element (a plain pointer for sizes 2, 3 and 4). src
Returns a const reference to the element at the given index, without bound checking. src
Returns a copy of the first three coordinates (x, y, z) as a vec3. src
Returns a copy of the coordinates (x, y) as a 2D vector (for vec3 and vec4). src
Returns a copy of the coordinates (y, z) as a 2D vector (for vec3 and vec4). src
Returns a copy of the coordinates (x, z) as a 2D vector (for vec3 and vec4). src

Functions taking a quaternion

Construct 1

Documented on another page

Transform 3

quaternion conjugate(quaternion const& q)

Quaternion conjugate (= inverse for a unit quaternion).

quaternion inverse(quaternion const& q)

Quaternion inverse.

quaternion normalize(quaternion const& q)

Unit quaternion in the same direction.

Convert 3

std::string type_str(quaternion const&)

Type name "quaternion".

Documented on another page

Access 2

Documented on another page

  • check_index_boundsRaises an error if the index is outside [0, 3]; no-op when CGP_NO_DEBUG is defined.on vec4
  • getComponent idx with the index checked at compile time (static_assert).on vec4

Other 1

Documented on another page

  • opengl_uniformSend a vec4 uniform to the shader; warns if the uniform is not found and expected is true.on opengl_shader_structure

Functions returning a quaternion

None.

Operators

Show the 17 operators (+, -, *, ==, ...)

operator*

3 overloads
quaternion operator*(quaternion const& a, quaternion const& b)
source

Quaternion (Hamilton) product, with the scalar part stored in w.

quaternion operator*(quaternion const& a, float b)
source

Quaternion with each component multiplied by a scalar.

quaternion operator*(float a, quaternion const& b)
source

Quaternion with each component multiplied by a scalar.

quaternion& operator*=(quaternion& a, quaternion const& b)

In-place quaternion (Hamilton) product a = a * b.

quaternion operator+(quaternion const& a, quaternion const& b)

Component-wise sum of two quaternions.

quaternion& operator+=(quaternion& a, quaternion const& b)

In-place component-wise sum of two quaternions.

quaternion operator-(quaternion const& a, quaternion const& b)

Component-wise difference of two quaternions.

quaternion& operator-=(quaternion& a, quaternion const& b)

In-place component-wise difference of two quaternions.

operator/

3 overloads
quaternion operator/(quaternion const& a, quaternion const& b)
source

Quaternion division a * inverse(b).

quaternion operator/(quaternion const& a, float b)
source

Quaternion with each component divided by a scalar.

quaternion operator/(float a, quaternion const& b)
source

Scalar times the inverse quaternion, a * inverse(b).

quaternion& operator/=(quaternion& a, quaternion const& b)

In-place quaternion division a = a * inverse(b).

std::istream& operator>>(std::istream& stream, quaternion& data)

Read the four components x y z w of a quaternion from a stream.

Documented on another page

  • operator*Apply the affine transform to a homogeneous vector, the translation being weighted by w: a direction (w=0) is not translated.on affine
  • operator*Apply the rigid transform to a homogeneous vector, the translation being weighted by w.on affine_rt
  • operator*Apply the transform to a homogeneous vector, the translation being weighted by w.on affine_rts
  • operator*Homogeneous vector whose xyz part is rotated, w being kept.on rotation_transform
CGP - Computer Graphics Programming.