LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
lg_quaternions.c File Reference

Functions

LG_Quat lg_quat (double x, double y, double z, double w)
 
void lg_quat_normalize (LG_Quat *q)
 
LG_Quat lg_quat_get_normalized (LG_Quat q)
 
LG_Quat lg_quats_multiply (LG_Quat q1, LG_Quat q2)
 
LG_Quat lg_quat_from_euler_ang (LG_EulerAng an, const char *rot_order)
 
LG_Quat lg_quat_from_axis_angle (LG_AxAng axis_ang)
 
void lg_quat_to_axis_angle (LG_Quat q, LG_AxAng *axis_ang)
 
LG_Quat lg_quat_from_3x3_rot_matrix (mat3_t m)
 
mat4_t lg_quat_to_rot_matrix (LG_Quat q2)
 
vec3_t lg_quat_rotate_vec3 (LG_Quat q, vec3_t v)
 
int lg_quat_copy (LG_Quat *q_dest, const LG_Quat *q_src)
 
zboolean lg_quats_are_equal (LG_Quat *q1, LG_Quat *q2)
 
void lg_quat_conjugate (LG_Quat *q)
 
LG_Quat lg_quat_get_conjugate (LG_Quat q)
 
void lg_quat_invert (LG_Quat *q)
 
LG_Quat lg_quat_get_invert (LG_Quat q)
 
double lg_quat_magnitude (LG_Quat q)
 
double lg_quats_dot (LG_Quat *q1, LG_Quat *q2)
 
void lg_quat_set_identity (LG_Quat *q)
 
LG_Quat lg_quat_get_identity ()
 
LG_Quat lg_quat_slerp (LG_Quat *q_1, LG_Quat *q_2, float slerp_k)
 
LG_EulerAng lg_euler_ang (double x_rot, double y_rot, double z_rot)
 
LG_AxAng lg_axis_ang (vec3_t axis, double angle)
 
LG_EulerAng lg_axis_angle_to_euler_ang (LG_AxAng axis_ang)
 
mat3_t mat3 (float m00, float m10, float m20, float m01, float m11, float m21, float m02, float m12, float m22)
 
mat3_t m3_transpose (mat3_t matrix)
 
LG_QuatVlg_quatv ()
 

Detailed Description

 === All about quaternions ===

Function Documentation

◆ lg_quat()

LG_Quat lg_quat ( double  x,
double  y,
double  z,
double  w 
)

Create a new quat

Parameters
x
y
z
w
Returns
A new LG_Quat

◆ lg_quat_normalize()

void lg_quat_normalize ( LG_Quat q)

Normalize quat

Parameters
qA pointer to a LG_Quat

◆ lg_quat_get_normalized()

LG_Quat lg_quat_get_normalized ( LG_Quat  q)

Get normalized quat

Parameters
qA LG_Quat
Returns
A normalized q

◆ lg_quats_multiply()

◆ lg_quat_from_euler_ang()

LG_Quat lg_quat_from_euler_ang ( LG_EulerAng  an,
const char *  rot_order 
)

Get new quat from Euler angles

(From: https://github.com/mrdoob/three.js/blob/master/src/math/Quaternion.js)

Parameters
anA LG_EulerAng
rot_orderOne of "XYZ", "YXZ", "ZXY", "ZYX", "YZX", "XZY"
Returns
A new quat

◆ lg_quat_from_axis_angle()

LG_Quat lg_quat_from_axis_angle ( LG_AxAng  axis_ang)

Get quat from axis and angle

Parameters
axis_angA LG_AxAng
Returns
A new LG_Quat

◆ lg_quat_to_axis_angle()

void lg_quat_to_axis_angle ( LG_Quat  q,
LG_AxAng axis_ang 
)

Get axis and angle from quat

Parameters
qA LG_Quat
axis_angA pointer to a LG_AxAng

◆ lg_quat_from_3x3_rot_matrix()

LG_Quat lg_quat_from_3x3_rot_matrix ( mat3_t  m)

Get quat from 3 x 3 rotation matrix

Using 'Converting a Rotation Matrix to a Quaternion' by Mike Day, Insomniac Games

https://d3cw3dd2w32x2b.cloudfront.net/wp-content/uploads/2015/01/matrix-to-quat.pdf

https://www.opengl-tutorial.org/assets/faq_quaternions/index.html#Q55

https://danceswithcode.net/engineeringnotes/quaternions/quaternions.html

Parameters
mA 3 x 3 rotation matrix
Returns
A LG_Quat

◆ lg_quat_to_rot_matrix()

mat4_t lg_quat_to_rot_matrix ( LG_Quat  q2)

Get new rotation matrix from quat

Parameters
q2A LG_Quat
Returns
A rotation matrix

◆ lg_quat_rotate_vec3()

vec3_t lg_quat_rotate_vec3 ( LG_Quat  q,
vec3_t  v 
)

Rotate vector by quat

Parameters
qA LG_Quat
vA vec3_t
Returns
A new vec3_t

◆ lg_quat_copy()

int lg_quat_copy ( LG_Quat q_dest,
const LG_Quat q_src 
)

Copy quat (copy q_src into q_dest)

Parameters
q_destA pointer to a LG_Quat
q_srcA pointer to a LG_Quat
Returns
LG_OK if OK

◆ lg_quats_are_equal()

zboolean lg_quats_are_equal ( LG_Quat q1,
LG_Quat q2 
)

Compare 2 quats

FIXME: should use adaptative epsilon or another better way of comparison

Parameters
q1A pointer to a LG_Quat
q2A pointer to a LG_Quat
Returns
TRUE if quats are equal, FALSE otherwise

◆ lg_quat_conjugate()

void lg_quat_conjugate ( LG_Quat q)

Conjugate quat

Conj = same rotation in opposite dir

Parameters
qA pointer to a LG_Quat

◆ lg_quat_get_conjugate()

LG_Quat lg_quat_get_conjugate ( LG_Quat  q)

Get quat conjugate

Parameters
qA LG_Quat
Returns
Conjugate of q

◆ lg_quat_invert()

void lg_quat_invert ( LG_Quat q)

Invert quat

Invert = conjugate / magnitude

Parameters
qA pointer to a LG_Quat

◆ lg_quat_get_invert()

LG_Quat lg_quat_get_invert ( LG_Quat  q)

Get quat invert

Parameters
qA LG_Quat
Returns
Invert of q

◆ lg_quat_magnitude()

double lg_quat_magnitude ( LG_Quat  q)

Get quat magnitude (also called length)

Parameters
qA LG_Quat
Returns
Magnitude as a double

◆ lg_quats_dot()

double lg_quats_dot ( LG_Quat q1,
LG_Quat q2 
)

Get dot product of 2 quats

Parameters
q1A pointer to a LG_Quat
q2A pointer to a LG_Quat
Returns
Dot product of q1 and q2 as a double

◆ lg_quat_set_identity()

void lg_quat_set_identity ( LG_Quat q)

Set quat to identity

The identity quat represents NO rotation

Parameters
qA pointer to a LG_Quat

◆ lg_quat_get_identity()

LG_Quat lg_quat_get_identity ( )

Get identity quat

Returns
An identity quat

◆ lg_quat_slerp()

LG_Quat lg_quat_slerp ( LG_Quat q_1,
LG_Quat q_2,
float  slerp_k 
)

Get SLERP interpolated quat

SLERP = spherical linear interpolation

Parameters
q1A pointer to a LG_Quat
q2A pointer to a LG_Quat
slerp_kSLERP k, in range [0.0, 1.0]
Returns
Interpolated quat

◆ lg_euler_ang()

LG_EulerAng lg_euler_ang ( double  x_rot,
double  y_rot,
double  z_rot 
)

Create a new euler angle

Use DEG_TO_RAD macro when necessary

Parameters
x_rotRotation around X axis in radians
y_rotRotation around Y axis in radians
z_rotRotation around Z axis in radians
Returns
A new LG_EulerAng

◆ lg_axis_ang()

LG_AxAng lg_axis_ang ( vec3_t  axis,
double  angle 
)

Create a new axis and angle

Use DEG_TO_RAD macro when necessary

Parameters
axisAxis unit vector
angleAngle in radian
Returns
A new LG_AxAng

◆ lg_axis_angle_to_euler_ang()

LG_EulerAng lg_axis_angle_to_euler_ang ( LG_AxAng  axis_ang)

Get Euler angles from axis and angle

(From: https://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToEuler/index.htm

Also check out 'terminological ambiguities' in: https://academicflight.com/articles/kinematics/rotation-formalisms/euler-angles/)

Note:
Euler angles extra terminology (in addition to aircraft/flight simulator YAW/PITCH/ROLL):
Rot around X axis is also called 'bank'
Rot around Y axis is also called 'heading'
Rot around Z axis is also called 'attitude'
With rot order = heading/attitude/bank

=== TODO: check this func is correct ===

Parameters
axis_angA LG_AxAng
Returns
A new LG_EulerAng

◆ mat3()

mat3_t mat3 ( float  m00,
float  m10,
float  m20,
float  m01,
float  m11,
float  m21,
float  m02,
float  m12,
float  m22 
)

Create a 3 x 3, column-major order matrix, suitable to store pure rotations

-> MOVE THAT TO MATH_3D.H ?

// 3 x 3, column-major order matrix
typedef union {
float m[3][3];
struct {
float m00, m01, m02;
float m10, m11, m12;
float m20, m21, m22;
};
Parameters
m00Float value to store at m[0][0]
m10Float value to store at m[1][0]
m20Float value to store at m[2][0]
m01Float value to store at m[0][1]
m11Float value to store at m[1][1]
m21Float value to store at m[2][1]
m02Float value to store at m[0][2]
m12Float value to store at m[1][2]
m22Float value to store at m[2][2]
Returns
A mat3_t, ie a 3 x 3 matrix

◆ m3_transpose()

mat3_t m3_transpose ( mat3_t  matrix)

Transpose a 3 x 3 matrix

-> MOVE THAT TO MATH_3D.H ?

Parameters
matrixA 3 x 3 matrix
Returns
The transposed matrix

◆ lg_quatv()

LG_QuatV* lg_quatv ( )

Return a static LG_QuatV struct, ie convenient precomputed quat values

So far:

id, x_half_pi, x_half_pi_inv, x_half_pi_up, x_half_pi_down,
y_half_pi, y_half_pi_inv, y_half_pi_up, y_half_pi_down,
z_half_pi, z_half_pi_inv, z_half_pi_up, z_half_pi_down,
x_small, x_small_inv, y_small, y_small_inv, z_smallv z_small_inv

Angles for x/y/z_small = M_PI * QUAT_PI_K (= M_PI / 200.0)

Rot order defined by ROT_SEQ in lg_quaternions.h

=== TODO: test this func is correct ===

Typical usage example:

LG_QuatV *quats = lg_init_quatv();
LG_Quat q_id = lg_quatv->id;
Returns
The (unique) LG_QuatV instance
mat3_t
Definition: lg_quaternions.h:37
LG_Quat
Definition: lg_quaternions.h:29
LG_QuatV
Definition: lg_quaternions.h:51
lg_quatv
LG_QuatV * lg_quatv()
Definition: lg_quaternions.c:679