Monado OpenXR Runtime
Files | Macros | Functions

C interface to some transform-related math functions. More...

Collaboration diagram for Math:

Files

file  m_api.h
 C interface to math library.
 
file  m_base.cpp
 Base implementations for math library.
 
file  m_documentation.hpp
 Header with just documentation.
 
file  m_eigen_interop.hpp
 Interoperability helpers connecting internal math types and Eigen.
 
file  m_filter_fifo.c
 A fifo that also allows you to dynamically filter.
 
file  m_filter_fifo.h
 A fifo that also allows you to dynamically filter.
 
file  m_hash.cpp
 Hashing function.
 
file  m_imu_3dof.c
 A IMU fusion specially made for 3dof devices.
 
file  m_imu_3dof.h
 A IMU fusion specially made for 3dof devices.
 
file  m_imu_pre.c
 IMU pre filter struct.
 
file  m_imu_pre.h
 IMU pre filter struct.
 
file  m_mathinclude.h
 Wrapper header for <math.h> to ensure pi-related math constants are defined.
 
file  m_optics.c
 Functions related to field-of-view.
 
file  m_permutation.c
 Code to generate permutation of indices.
 
file  m_permutation.h
 Code to generate permutation of indices.
 
file  m_predict.c
 Simple function to predict a new pose from a given pose.
 
file  m_predict.h
 Simple function to predict a new pose from a given pose.
 
file  m_quatexpmap.cpp
 Base implementations for math library.
 
file  m_space.cpp
 Functions for manipulating a xrt_space_graph struct.
 
file  m_space.h
 Functions for manipulating xrt_pose, xrt_space_relation and xrt_space_graph structs.
 
file  m_vec2.h
 C vec2 math library.
 
file  m_vec3.h
 C vec3 math library.
 

Macros

#define MATH_GRAVITY_M_S2   (9.8066)
 Standard gravity acceleration constant. More...
 

Functions

size_t math_hash_string (const char *str_c, size_t length)
 Generate a hash value from the given string, trailing zero not included. More...
 
bool math_compute_fovs (double w_total, double w_1, double horizfov_total, double h_total, double h_1, double vertfov_total, struct xrt_fov *fov)
 Perform the computations from "Computing Half-Fields-Of-View from Simpler Display Models", to get half-FOVs from things we can retrieve from other APIs. More...
 
void m_predict_relation (const struct xrt_space_relation *rel, double delta_s, struct xrt_space_relation *out_rel)
 Using the given xrt_space_relation predicts a new xrt_space_relation delta_s into the future. More...
 
static bool m_pose_is_identity (struct xrt_pose *pose)
 
static void m_space_relation_from_pose (const struct xrt_pose *pose, struct xrt_space_relation *out_relation)
 
static void m_space_relation_ident (struct xrt_space_relation *out_relation)
 
void m_space_relation_invert (struct xrt_space_relation *relation, struct xrt_space_relation *out_relation)
 
static struct xrt_space_relationm_space_graph_reserve (struct xrt_space_graph *xsg)
 Reserve a step in the graph and return a pointer to the relation. More...
 
static void m_space_graph_add_relation (struct xrt_space_graph *xsg, const struct xrt_space_relation *relation)
 Flattens a space graph into a single relation. More...
 
static void m_space_graph_add_inverted_relation (struct xrt_space_graph *xsg, const struct xrt_space_relation *relation)
 
static void m_space_graph_add_pose (struct xrt_space_graph *xsg, const struct xrt_pose *pose)
 
static void m_space_graph_add_pose_if_not_identity (struct xrt_space_graph *xsg, const struct xrt_pose *pose)
 
static void m_space_graph_add_inverted_pose_if_not_identity (struct xrt_space_graph *xsg, const struct xrt_pose *pose)
 
void m_space_graph_resolve (const struct xrt_space_graph *xsg, struct xrt_space_relation *out_relation)
 Flattens a space graph into a single relation. More...
 
bool math_vec3_validate (const struct xrt_vec3 *vec3)
 Check if this vec3 is valid for math operations. More...
 
void math_vec3_accum (const struct xrt_vec3 *additional, struct xrt_vec3 *inAndOut)
 Accumulate a vector by adding in-place. More...
 
void math_vec3_subtract (const struct xrt_vec3 *subtrahend, struct xrt_vec3 *inAndOut)
 Subtract from a vector in-place. More...
 
void math_vec3_scalar_mul (float scalar, struct xrt_vec3 *inAndOut)
 Multiply a vector in-place. More...
 
void math_vec3_cross (const struct xrt_vec3 *l, const struct xrt_vec3 *r, struct xrt_vec3 *result)
 Cross product of a vector. More...
 
void math_vec3_normalize (struct xrt_vec3 *in)
 Normalize a vec3 in place. More...
 
void math_quat_from_angle_vector (float angle_rads, const struct xrt_vec3 *vector, struct xrt_quat *result)
 Create a rotation from a angle in radians and a vector. More...
 
void math_quat_from_matrix_3x3 (const struct xrt_matrix_3x3 *mat, struct xrt_quat *result)
 Create a rotation from a 3x3 rotation matrix. More...
 
void math_quat_from_plus_x_z (const struct xrt_vec3 *plus_x, const struct xrt_vec3 *plus_z, struct xrt_quat *result)
 Create a rotation from two vectors plus x and z, by creating a rotation matrix by crossing z and x to get the y axis. More...
 
bool math_quat_validate (const struct xrt_quat *quat)
 Check if this quat can be used in transformation operations. More...
 
bool math_quat_validate_within_1_percent (const struct xrt_quat *quat)
 Check if this quat is within 1% of unit length. More...
 
void math_quat_invert (const struct xrt_quat *quat, struct xrt_quat *out_quat)
 Invert a quaternion. More...
 
void math_quat_normalize (struct xrt_quat *inout)
 Normalize a quaternion. More...
 
bool math_quat_ensure_normalized (struct xrt_quat *inout)
 Normalizes a quaternion if it has accumulated float precision errors. More...
 
void math_quat_rotate_vec3 (const struct xrt_quat *left, const struct xrt_vec3 *right, struct xrt_vec3 *result)
 Rotate a vector. More...
 
void math_quat_rotate (const struct xrt_quat *left, const struct xrt_quat *right, struct xrt_quat *result)
 Rotate a quaternion (compose rotations). More...
 
void math_quat_integrate_velocity (const struct xrt_quat *quat, const struct xrt_vec3 *ang_vel, float dt, struct xrt_quat *result)
 Integrate an angular velocity vector (exponential map) and apply to a quaternion. More...
 
void math_quat_finite_difference (const struct xrt_quat *quat0, const struct xrt_quat *quat1, float dt, struct xrt_vec3 *out_ang_vel)
 Compute an angular velocity vector (exponential map format) by taking the finite difference of two quaternions. More...
 
void math_quat_rotate_derivative (const struct xrt_quat *rot, const struct xrt_vec3 *deriv, struct xrt_vec3 *result)
 Used to rotate a derivative like a angular velocity. More...
 
void math_matrix_2x2_multiply (const struct xrt_matrix_2x2 *left, const struct xrt_matrix_2x2 *right, struct xrt_matrix_2x2 *result)
 Multiply Matrix2x2. More...
 
void math_matrix_3x3_multiply (const struct xrt_matrix_3x3 *left, const struct xrt_matrix_3x3 *right, struct xrt_matrix_3x3 *result)
 Multiply Matrix3x3. More...
 
void math_matrix_4x4_identity (struct xrt_matrix_4x4 *result)
 Initialize Matrix4x4 with identity. More...
 
void math_matrix_4x4_multiply (const struct xrt_matrix_4x4 *left, const struct xrt_matrix_4x4 *right, struct xrt_matrix_4x4 *result)
 Multiply Matrix4x4. More...
 
void math_matrix_4x4_view_from_pose (const struct xrt_pose *pose, struct xrt_matrix_4x4 *result)
 Compute view matrix from xrt_pose. More...
 
void math_matrix_4x4_model (const struct xrt_pose *pose, const struct xrt_vec3 *size, struct xrt_matrix_4x4 *result)
 Compute quad layer model matrix from xrt_pose and xrt_vec2 size. More...
 
void math_matrix_4x4_inverse_view_projection (const struct xrt_matrix_4x4 *view, const struct xrt_matrix_4x4 *projection, struct xrt_matrix_4x4 *result)
 Compute inverse view projection matrix, using only the starting 3x3 block of the view. More...
 
void math_pose_identity (struct xrt_pose *pose)
 Somewhat laboriously make an xrt_pose identity. More...
 
bool math_pose_validate (const struct xrt_pose *pose)
 Check if this pose can be used in transformation operations. More...
 
void math_pose_invert (const struct xrt_pose *pose, struct xrt_pose *outPose)
 Invert pose. More...
 
void math_pose_transform (const struct xrt_pose *transform, const struct xrt_pose *pose, struct xrt_pose *outPose)
 Apply a rigid-body transformation to a pose. More...
 
void math_pose_transform_point (const struct xrt_pose *transform, const struct xrt_vec3 *point, struct xrt_vec3 *out_point)
 Apply a rigid-body transformation to a point. More...
 

Detailed Description

C interface to some transform-related math functions.

Macro Definition Documentation

◆ MATH_GRAVITY_M_S2

#define MATH_GRAVITY_M_S2   (9.8066)

Function Documentation

◆ m_pose_is_identity()

static bool m_pose_is_identity ( struct xrt_pose pose)
inlinestatic

◆ m_predict_relation()

void m_predict_relation ( const struct xrt_space_relation rel,
double  delta_s,
struct xrt_space_relation out_rel 
)

#include <auxiliary/math/m_predict.h>

Using the given xrt_space_relation predicts a new xrt_space_relation delta_s into the future.

Assumes that angular velocity is relative to the space the relation is in, not relative to relation::pose.

References do_orientation(), do_position(), and xrt_space_relation::relation_flags.

Referenced by _predict_pose(), predict_pose(), rs_6dof_get_tracked_pose(), and wmr_hmd_get_tracked_pose().

◆ m_space_graph_add_inverted_pose_if_not_identity()

static void m_space_graph_add_inverted_pose_if_not_identity ( struct xrt_space_graph xsg,
const struct xrt_pose pose 
)
inlinestatic

◆ m_space_graph_add_inverted_relation()

static void m_space_graph_add_inverted_relation ( struct xrt_space_graph xsg,
const struct xrt_space_relation relation 
)
inlinestatic

◆ m_space_graph_add_pose()

static void m_space_graph_add_pose ( struct xrt_space_graph xsg,
const struct xrt_pose pose 
)
inlinestatic

◆ m_space_graph_add_pose_if_not_identity()

static void m_space_graph_add_pose_if_not_identity ( struct xrt_space_graph xsg,
const struct xrt_pose pose 
)
inlinestatic

◆ m_space_graph_add_relation()

static void m_space_graph_add_relation ( struct xrt_space_graph xsg,
const struct xrt_space_relation relation 
)
inlinestatic

◆ m_space_graph_reserve()

static struct xrt_space_relation* m_space_graph_reserve ( struct xrt_space_graph xsg)
static

#include <auxiliary/math/m_space.h>

Reserve a step in the graph and return a pointer to the relation.

References xrt_space_graph::num_steps, xrt_space_graph::steps, and XRT_SPACE_GRAPHS_MAX.

Referenced by oxr_xdev_get_space_graph(), and psmv_device_get_space_graph().

◆ m_space_graph_resolve()

void m_space_graph_resolve ( const struct xrt_space_graph xsg,
struct xrt_space_relation out_relation 
)

◆ m_space_relation_from_pose()

static void m_space_relation_from_pose ( const struct xrt_pose pose,
struct xrt_space_relation out_relation 
)
inlinestatic

◆ m_space_relation_ident()

static void m_space_relation_ident ( struct xrt_space_relation out_relation)
inlinestatic

◆ m_space_relation_invert()

void m_space_relation_invert ( struct xrt_space_relation relation,
struct xrt_space_relation out_relation 
)

◆ math_compute_fovs()

bool math_compute_fovs ( double  w_total,
double  w_1,
double  horizfov_total,
double  h_total,
double  h_1,
double  vertfov_total,
struct xrt_fov fov 
)

#include <auxiliary/math/m_api.h>

Perform the computations from "Computing Half-Fields-Of-View from Simpler Display Models", to get half-FOVs from things we can retrieve from other APIs.

The origin is in the lower-left corner of the display, so w_1 is the width to the left of CoP, and h_1 is the height below CoP.

If vertfov_total is set to 0, it will be computed from h_total.

Distances are in arbitrary but consistent units. Angles are in radians.

In the diagram below, treating it like a FOV for horizontal, the top angle is horizfov_total, the length of the bottom is w_total, and the distance between the vertical line and the left corner is w_1. Vertical is similar - h_1 is above the center line. The triangle need not be symmetrical, despite how the diagram looks.

horizfov_total
*
angle_left (neg) -> / | \ <- angle_right
/ | \
/ | \
/ | \
-------------
[ w_1 ]
[ --- w --- ]
------- --- |\
| \
h_1 | \ angle_up
h_total ___ |-------* vertfov_total
| / angle_down (neg)
| /
| /
------- |/
Returns
true if successful.

References xrt_fov::angle_down, xrt_fov::angle_left, xrt_fov::angle_right, xrt_fov::angle_up, and math_solve_triangle().

Referenced by _create_hmd_device(), create_hmd(), hdk_device_create(), u_device_setup_split_side_by_side(), and vive_device_create().

◆ math_hash_string()

size_t math_hash_string ( const char *  str_c,
size_t  length 
)

#include <auxiliary/math/m_api.h>

Generate a hash value from the given string, trailing zero not included.

Hashing function used is not specified so no guarantee of staying the same between different versions of the software, or even when the same version is compiled on different platforms/libc++ as it might use std::hash.

Referenced by oxr_allocate_path().

◆ math_matrix_2x2_multiply()

void math_matrix_2x2_multiply ( const struct xrt_matrix_2x2 left,
const struct xrt_matrix_2x2 right,
struct xrt_matrix_2x2 result 
)
related

Multiply Matrix2x2.

◆ math_matrix_3x3_multiply()

void math_matrix_3x3_multiply ( const struct xrt_matrix_3x3 left,
const struct xrt_matrix_3x3 right,
struct xrt_matrix_3x3 result 
)
related

Multiply Matrix3x3.

◆ math_matrix_4x4_identity()

void math_matrix_4x4_identity ( struct xrt_matrix_4x4 result)
related

Initialize Matrix4x4 with identity.

Referenced by _init_ubos().

◆ math_matrix_4x4_inverse_view_projection()

void math_matrix_4x4_inverse_view_projection ( const struct xrt_matrix_4x4 view,
const struct xrt_matrix_4x4 projection,
struct xrt_matrix_4x4 result 
)
related

Compute inverse view projection matrix, using only the starting 3x3 block of the view.

◆ math_matrix_4x4_model()

void math_matrix_4x4_model ( const struct xrt_pose pose,
const struct xrt_vec3 size,
struct xrt_matrix_4x4 result 
)
related

Compute quad layer model matrix from xrt_pose and xrt_vec2 size.

◆ math_matrix_4x4_multiply()

void math_matrix_4x4_multiply ( const struct xrt_matrix_4x4 left,
const struct xrt_matrix_4x4 right,
struct xrt_matrix_4x4 result 
)
related

Multiply Matrix4x4.

Referenced by _update_mvp_matrix().

◆ math_matrix_4x4_view_from_pose()

void math_matrix_4x4_view_from_pose ( const struct xrt_pose pose,
struct xrt_matrix_4x4 result 
)
related

Compute view matrix from xrt_pose.

◆ math_pose_identity()

void math_pose_identity ( struct xrt_pose pose)
related

Somewhat laboriously make an xrt_pose identity.

◆ math_pose_invert()

void math_pose_invert ( const struct xrt_pose pose,
struct xrt_pose outPose 
)
related

Invert pose.

OK if input and output are the same addresses.

Referenced by _get_lighthouse(), m_space_graph_add_inverted_pose_if_not_identity(), m_space_relation_invert(), and oxr_space_ref_relation().

◆ math_pose_transform()

void math_pose_transform ( const struct xrt_pose transform,
const struct xrt_pose pose,
struct xrt_pose outPose 
)
related

Apply a rigid-body transformation to a pose.

OK if input and output are the same addresses.

◆ math_pose_transform_point()

void math_pose_transform_point ( const struct xrt_pose transform,
const struct xrt_vec3 point,
struct xrt_vec3 out_point 
)
related

Apply a rigid-body transformation to a point.

The input point and output may be the same pointer.

See also
xrt_vec3

Referenced by _get_lighthouse().

◆ math_pose_validate()

bool math_pose_validate ( const struct xrt_pose pose)
related

Check if this pose can be used in transformation operations.

Referenced by oxr_space_reference_create().

◆ math_quat_ensure_normalized()

bool math_quat_ensure_normalized ( struct xrt_quat inout)
related

Normalizes a quaternion if it has accumulated float precision errors.

Returns true if the quaternion was already normalized or was normalized after being found within a small float precision tolerance. Returns false if the quaternion was not at all normalized.

◆ math_quat_finite_difference()

void math_quat_finite_difference ( const struct xrt_quat quat0,
const struct xrt_quat quat1,
float  dt,
struct xrt_vec3 out_ang_vel 
)
related

Compute an angular velocity vector (exponential map format) by taking the finite difference of two quaternions.

quat1 is the orientation dt time after the orientation was quat0

out_ang_vel and dt share the same units of time, and out_ang_vel is be in radians per unit of time.

See also
xrt_vec3

◆ math_quat_from_angle_vector()

void math_quat_from_angle_vector ( float  angle_rads,
const struct xrt_vec3 vector,
struct xrt_quat result 
)
related

Create a rotation from a angle in radians and a vector.

See also
xrt_vec3

Referenced by gravity_correction(), m_imu_3dof_update(), and qwerty_get_tracked_pose().

◆ math_quat_from_matrix_3x3()

void math_quat_from_matrix_3x3 ( const struct xrt_matrix_3x3 mat,
struct xrt_quat result 
)
related

Create a rotation from a 3x3 rotation matrix.

See also
xrt_matrix_3x3

Referenced by _get_distortion_properties().

◆ math_quat_from_plus_x_z()

void math_quat_from_plus_x_z ( const struct xrt_vec3 plus_x,
const struct xrt_vec3 plus_z,
struct xrt_quat result 
)
related

Create a rotation from two vectors plus x and z, by creating a rotation matrix by crossing z and x to get the y axis.

See also
xrt_vec3

Referenced by _get_pose_from_pos_x_z().

◆ math_quat_integrate_velocity()

void math_quat_integrate_velocity ( const struct xrt_quat quat,
const struct xrt_vec3 ang_vel,
float  dt,
struct xrt_quat result 
)
related

Integrate an angular velocity vector (exponential map) and apply to a quaternion.

ang_vel and dt should share the same units of time, and the ang_vel vector should be in radians per unit of time.

See also
xrt_vec3

Referenced by do_orientation(), and m_imu_3dof_update().

◆ math_quat_invert()

void math_quat_invert ( const struct xrt_quat quat,
struct xrt_quat out_quat 
)
related

Invert a quaternion.

Referenced by do_orientation().

◆ math_quat_normalize()

void math_quat_normalize ( struct xrt_quat inout)
related

◆ math_quat_rotate()

void math_quat_rotate ( const struct xrt_quat left,
const struct xrt_quat right,
struct xrt_quat result 
)
related

Rotate a quaternion (compose rotations).

Referenced by gravity_correction(), hdk_device_update(), m_imu_3dof_update(), and qwerty_get_tracked_pose().

◆ math_quat_rotate_derivative()

void math_quat_rotate_derivative ( const struct xrt_quat rot,
const struct xrt_vec3 deriv,
struct xrt_vec3 result 
)
related

Used to rotate a derivative like a angular velocity.

See also
xrt_vec3

Referenced by do_orientation(), and r_device_get_tracked_pose().

◆ math_quat_rotate_vec3()

void math_quat_rotate_vec3 ( const struct xrt_quat left,
const struct xrt_vec3 right,
struct xrt_vec3 result 
)
related

Rotate a vector.

See also
xrt_vec3

Referenced by _get_lighthouse(), m_imu_3dof_update(), and qwerty_get_tracked_pose().

◆ math_quat_validate()

bool math_quat_validate ( const struct xrt_quat quat)
related

Check if this quat can be used in transformation operations.

◆ math_quat_validate_within_1_percent()

bool math_quat_validate_within_1_percent ( const struct xrt_quat quat)
related

Check if this quat is within 1% of unit length.

◆ math_vec3_accum()

void math_vec3_accum ( const struct xrt_vec3 additional,
struct xrt_vec3 inAndOut 
)
related

Accumulate a vector by adding in-place.

Logically, *inAndOut += *additional OK if the two arguments are the same addresses.

Referenced by qwerty_get_tracked_pose().

◆ math_vec3_cross()

void math_vec3_cross ( const struct xrt_vec3 l,
const struct xrt_vec3 r,
struct xrt_vec3 result 
)
related

Cross product of a vector.

◆ math_vec3_normalize()

void math_vec3_normalize ( struct xrt_vec3 in)
related

Normalize a vec3 in place.

◆ math_vec3_scalar_mul()

void math_vec3_scalar_mul ( float  scalar,
struct xrt_vec3 inAndOut 
)
related

Multiply a vector in-place.

Logically, *inAndOut *= scalar

◆ math_vec3_subtract()

void math_vec3_subtract ( const struct xrt_vec3 subtrahend,
struct xrt_vec3 inAndOut 
)
related

Subtract from a vector in-place.

Logically, *inAndOut -= *subtrahend OK if the two arguments are the same addresses.

◆ math_vec3_validate()

bool math_vec3_validate ( const struct xrt_vec3 vec3)
related

Check if this vec3 is valid for math operations.