vkf_math Module

Math for vkframes: the working precision, quaternions and 4x4 transforms.

Quaternions are q(4) = [x, y, z, w] (vector part first, as OpenFrames and OpenSceneGraph store them), unit length for rotations. The rotation of q takes vectors in a frame to its parent frame: v_parent = rotate(q, v_local). Matrices are 4x4, column-major, acting on column vectors.


Uses

  • module~~vkf_math~~UsesGraph module~vkf_math vkf_math iso_fortran_env iso_fortran_env module~vkf_math->iso_fortran_env

Used by

  • module~~vkf_math~~UsedByGraph module~vkf_math vkf_math module~vkf_artists vkf_artists module~vkf_artists->module~vkf_math module~vkf_frames vkf_frames module~vkf_artists->module~vkf_frames module~vkf_line_chain vkf_line_chain module~vkf_artists->module~vkf_line_chain module~vkf_pick vkf_pick module~vkf_artists->module~vkf_pick module~vkf_trajectory vkf_trajectory module~vkf_artists->module~vkf_trajectory module~vkf_clock vkf_clock module~vkf_clock->module~vkf_math module~vkf_diagnostics vkf_diagnostics module~vkf_diagnostics->module~vkf_math module~vkf_follower vkf_follower module~vkf_follower->module~vkf_math module~vkf_follower->module~vkf_trajectory module~vkf_frames->module~vkf_math module~vkf_frames->module~vkf_follower module~vkf_geometry vkf_geometry module~vkf_frames->module~vkf_geometry module~vkf_frames->module~vkf_pick module~vkf_text vkf_text module~vkf_frames->module~vkf_text module~vkf_frames->module~vkf_trajectory module~vkf_geometry->module~vkf_math module~vkf_gui vkf_gui module~vkf_gui->module~vkf_math module~vkf_handles vkf_handles module~vkf_handles->module~vkf_math module~vkf_handles->module~vkf_artists module~vkf_handles->module~vkf_frames module~vkf_handles->module~vkf_geometry module~vkf_handles->module~vkf_pick module~vkf_handles->module~vkf_trajectory module~vkf_hud vkf_hud module~vkf_hud->module~vkf_math module~vkf_hud->module~vkf_frames module~vkf_line_chain->module~vkf_math module~vkf_line_segments vkf_line_segments module~vkf_line_segments->module~vkf_math module~vkf_line_segments->module~vkf_frames module~vkf_line_segments->module~vkf_line_chain module~vkf_model vkf_model module~vkf_model->module~vkf_math module~vkf_model->module~vkf_frames module~vkf_model->module~vkf_pick module~vkf_pick->module~vkf_math module~vkf_shadow vkf_shadow module~vkf_shadow->module~vkf_math module~vkf_shadow->module~vkf_frames module~vkf_sphere vkf_sphere module~vkf_shadow->module~vkf_sphere module~vkf_terrain vkf_terrain module~vkf_shadow->module~vkf_terrain module~vkf_shapes vkf_shapes module~vkf_shapes->module~vkf_math module~vkf_shapes->module~vkf_artists module~vkf_shapes->module~vkf_frames module~vkf_sky vkf_sky module~vkf_sky->module~vkf_math module~vkf_sky->module~vkf_text module~vkf_sphere->module~vkf_math module~vkf_sphere->module~vkf_frames module~vkf_sphere->module~vkf_geometry module~vkf_sphere->module~vkf_pick module~vkf_terrain->module~vkf_math module~vkf_terrain->module~vkf_frames module~vkf_terrain->module~vkf_pick module~vkf_text->module~vkf_math module~vkf_trajectory->module~vkf_math module~vkf_view vkf_view module~vkf_view->module~vkf_math module~vkf_view->module~vkf_frames module~vkf_window vkf_window module~vkf_window->module~vkf_math module~vkf_window->module~vkf_clock module~vkf_window->module~vkf_diagnostics module~vkf_window->module~vkf_frames module~vkf_window->module~vkf_gui module~vkf_window->module~vkf_handles module~vkf_window->module~vkf_hud module~vkf_window->module~vkf_pick module~vkf_window->module~vkf_sky module~vkf_window->module~vkf_text module~vkf_window->module~vkf_view module~vkframes vkframes module~vkframes->module~vkf_math module~vkframes->module~vkf_artists module~vkframes->module~vkf_clock module~vkframes->module~vkf_diagnostics module~vkframes->module~vkf_follower module~vkframes->module~vkf_frames module~vkframes->module~vkf_gui module~vkframes->module~vkf_handles module~vkframes->module~vkf_hud module~vkframes->module~vkf_line_segments module~vkframes->module~vkf_model module~vkframes->module~vkf_pick module~vkframes->module~vkf_shadow module~vkframes->module~vkf_shapes module~vkframes->module~vkf_sky module~vkframes->module~vkf_sphere module~vkframes->module~vkf_terrain module~vkframes->module~vkf_trajectory module~vkframes->module~vkf_view module~vkframes->module~vkf_window

Variables

Type Visibility Attributes Name Initial
real(kind=wp), public, parameter :: pi = acos(-1.0_wp)

pi

real(kind=wp), public, parameter :: quat_identity(4) = [0.0_wp, 0.0_wp, 0.0_wp, 1.0_wp]

the identity rotation

integer, public, parameter :: wp = real64

working precision


Functions

public pure function cross(a, b) result(c)

The cross product a x b.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: a(3)

the vectors

real(kind=wp), intent(in) :: b(3)

the vectors

Return Value real(kind=wp), (3)

public pure function frame_matrix(position, attitude, scale, pivot) result(m)

A frame's local-to-parent transform, as OpenFrames' FrameTransform builds it: translate(position) * rotate(attitude) * scale(scale) * translate(-pivot).

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: position(3)

origin in the parent frame

real(kind=wp), intent(in) :: attitude(4)

quaternion [x, y, z, w]

real(kind=wp), intent(in), optional :: scale(3)

scale along each local axis (default 1)

real(kind=wp), intent(in), optional :: pivot(3)

local point placed at position (default 0)

Return Value real(kind=wp), (4,4)

public pure function identity_matrix() result(m)

The 4x4 identity matrix.

Arguments

None

Return Value real(kind=wp), (4,4)

public pure function quat_conjugate(q) result(c)

The conjugate of q: the inverse rotation, for a unit quaternion.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: q(4)

quaternion [x, y, z, w]

Return Value real(kind=wp), (4)

public pure function quat_from_axis_angle(axis, angle) result(q)

The rotation by angle (radians) about axis (need not be unit length).

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: axis(3)

rotation axis (any length; zero: the identity)

real(kind=wp), intent(in) :: angle

(radians)

Return Value real(kind=wp), (4)

public pure function quat_from_matrix(m) result(q)

The unit quaternion of a 3x3 rotation matrix (columns: the rotated axes; the inverse of quat_to_matrix), by Shepperd's method (from the largest of w and the vector components, for accuracy).

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: m(3,3)

rotation matrix (columns: the rotated axes)

Return Value real(kind=wp), (4)

public pure function quat_from_vectors(from, to) result(q)

The shortest rotation taking the direction of from to that of to (OpenSceneGraph's osg::Quat::makeRotate(from, to)); a half turn about a perpendicular axis if they are opposite, the identity if either is zero.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: from(3)

directions (any length)

real(kind=wp), intent(in) :: to(3)

directions (any length)

Return Value real(kind=wp), (4)

public pure function quat_multiply(a, b) result(q)

The product a*b: rotating by b, then by a.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: a(4)

quaternions [x, y, z, w]

real(kind=wp), intent(in) :: b(4)

quaternions [x, y, z, w]

Return Value real(kind=wp), (4)

public pure function quat_normalize(q) result(u)

q / |q| (the identity for a zero quaternion).

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: q(4)

quaternion [x, y, z, w] (any length)

Return Value real(kind=wp), (4)

public pure function quat_rotate(q, v) result(r)

The vector v rotated by the unit quaternion q.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: q(4)

unit quaternion [x, y, z, w], and the vector

real(kind=wp), intent(in) :: v(3)

unit quaternion [x, y, z, w], and the vector

Return Value real(kind=wp), (3)

public pure function quat_slerp(t, a, b) result(q)

Spherical linear interpolation from a (t = 0) to b (t = 1), along the shorter arc; linear when the two are nearly equal. As OpenSceneGraph's osg::Quat::slerp, which OpenFrames' TrajectoryFollower uses, except that the linear case is normalized (OSG's isn't: a quaternion a little short of unit length, its rotation matrix then also scaling, by a millionth for attitudes 0.25 deg apart, a kilometre at the Moon's distance).

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: t

the fraction of the way (0 to 1)

real(kind=wp), intent(in) :: a(4)

unit quaternions [x, y, z, w] at t = 0 and t = 1

real(kind=wp), intent(in) :: b(4)

unit quaternions [x, y, z, w] at t = 0 and t = 1

Return Value real(kind=wp), (4)

public pure function quat_to_matrix(q) result(m)

The 3x3 rotation matrix of the unit quaternion q (columns: the rotated axes).

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: q(4)

unit quaternion [x, y, z, w]

Return Value real(kind=wp), (3,3)

public pure function rigid_inverse(m) result(inv)

The inverse of a rigid transform (rotation and translation): [R' -R't].

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: m(4,4)

rigid transform (rotation and translation)

Return Value real(kind=wp), (4,4)

public pure function translation_matrix(t) result(m)

The 4x4 transform translating by t.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: t(3)

translation

Return Value real(kind=wp), (4,4)


Subroutines

public pure subroutine set_rgba(rgba, color)

Set rgba from color: [r, g, b] (alpha unchanged) or [r, g, b, a].

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(inout) :: rgba(4)

[r, g, b, a]

real(kind=wp), intent(in) :: color(:)

[r, g, b] or [r, g, b, a] (0 to 1)