LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
lg_quaternions.h
1 /*
2  * LibGame - Copyright (C) Emmanuel Thomas-Maurin 2011-2024
3  * All rights reserved
4  */
5 
6 #ifndef LG_QUATERNIONS_H
7 #define LG_QUATERNIONS_H
8 
9 #define POW2(x) (x * x)
10 #define POW3(x) (x * x * x)
11 
12 #define RAD_TO_DEG 57.295779513 /* or (180.0 / (float)M_PI) */
13 #define DEG_TO_RAD 0.017453293 /* or ((float)M_PI / 180.0) */
14 
15 /* Euler angles */
16 typedef struct {
17  double x_rot; /* Rotation around X axis in radians - Use DEG_TO_RAD macro when necessary */
18  double y_rot;
19  double z_rot;
20 } LG_EulerAng;
21 
22 /* Axis and angle */
23 typedef struct {
24  vec3_t axis; /* Unit vector - should/must always be normalized ? */
25  double angle; /* In radians */
26 } LG_AxAng;
27 
28 /* Quaternion */
29 typedef struct {
30  double x; /* Vector part */
31  double y;
32  double z;
33  double w; /* Scalar part */
34 } LG_Quat;
35 
36 /* 3 x 3, column-major order matrix -> move that to math_3d.h ? */
37 typedef union {
38  float m[3][3];
39  struct {
40  float m00, m01, m02;
41  float m10, m11, m12;
42  float m20, m21, m22;
43  };
44 } mat3_t;
45 
46 #define ROT_SEQ "XYZ"
47 #define QUAT_PI_K (1.0 / 200.0)
48 /*
49  * Convenient precomputed quat values
50  */
51 typedef struct {
52  LG_Quat id;
53  LG_Quat x_half_pi;
54  LG_Quat x_half_pi_inv;
55  LG_Quat x_half_pi_up;
56  LG_Quat x_half_pi_down;
57  LG_Quat y_half_pi;
58  LG_Quat y_half_pi_inv;
59  LG_Quat y_half_pi_up;
60  LG_Quat y_half_pi_down;
61  LG_Quat z_half_pi;
62  LG_Quat z_half_pi_inv;
63  LG_Quat z_half_pi_up;
64  LG_Quat z_half_pi_down;
65  LG_Quat x_small;
66  LG_Quat x_small_inv;
67  LG_Quat y_small;
68  LG_Quat y_small_inv;
69  LG_Quat z_small;
70  LG_Quat z_small_inv;
71 } LG_QuatV;
72 
73 LG_Quat lg_quat(double, double, double, double);
74 
76 
78 
80 
82 
84 
86 
88 
90 
92 
93 int lg_quat_copy(LG_Quat *, const LG_Quat *);
94 
95 zboolean lg_quats_are_equal(LG_Quat *, LG_Quat *);
96 
98 
100 
101 void lg_quat_invert(LG_Quat *);
102 
104 
105 double lg_quat_magnitude(LG_Quat);
106 
107 double lg_quats_dot(LG_Quat *, LG_Quat *);
108 
110 
112 
113 LG_Quat lg_quat_slerp(LG_Quat *, LG_Quat *, float);
114 
115 LG_EulerAng lg_euler_ang(double, double, double);
116 
117 LG_AxAng lg_axis_ang(vec3_t, double);
118 
120 
121 mat3_t mat3(float, float, float, float, float, float, float, float, float);
122 
124 
125 LG_QuatV *lg_quatv();
126 
127 #endif /* LG_QUATERNIONS_H */
lg_axis_ang
LG_AxAng lg_axis_ang(vec3_t axis, double angle)
Definition: lg_quaternions.c:529
mat3_t
Definition: lg_quaternions.h:37
lg_euler_ang
LG_EulerAng lg_euler_ang(double x_rot, double y_rot, double z_rot)
Definition: lg_quaternions.c:509
lg_quat_normalize
void lg_quat_normalize(LG_Quat *q)
Definition: lg_quaternions.c:35
LG_Quat
Definition: lg_quaternions.h:29
mat3
mat3_t mat3(float m00, float m10, float m20, float m01, float m11, float m21, float m02, float m12, float m22)
Definition: lg_quaternions.c:623
lg_quat_to_axis_angle
void lg_quat_to_axis_angle(LG_Quat q, LG_AxAng *axis_ang)
Definition: lg_quaternions.c:188
lg_quat_from_euler_ang
LG_Quat lg_quat_from_euler_ang(LG_EulerAng an, const char *rot_order)
Definition: lg_quaternions.c:111
lg_quat_conjugate
void lg_quat_conjugate(LG_Quat *q)
Definition: lg_quaternions.c:338
LG_EulerAng
Definition: lg_quaternions.h:16
lg_quat
LG_Quat lg_quat(double x, double y, double z, double w)
Definition: lg_quaternions.c:23
LG_QuatV
Definition: lg_quaternions.h:51
lg_quat_get_conjugate
LG_Quat lg_quat_get_conjugate(LG_Quat q)
Definition: lg_quaternions.c:351
lg_quat_rotate_vec3
vec3_t lg_quat_rotate_vec3(LG_Quat q, vec3_t v)
Definition: lg_quaternions.c:281
lg_quat_magnitude
double lg_quat_magnitude(LG_Quat q)
Definition: lg_quaternions.c:392
lg_quat_slerp
LG_Quat lg_quat_slerp(LG_Quat *q_1, LG_Quat *q_2, float slerp_k)
Definition: lg_quaternions.c:448
lg_quat_to_rot_matrix
mat4_t lg_quat_to_rot_matrix(LG_Quat q2)
Definition: lg_quaternions.c:257
vec3_t
Definition: math_3d.h:111
lg_quat_set_identity
void lg_quat_set_identity(LG_Quat *q)
Definition: lg_quaternions.c:416
lg_quat_get_identity
LG_Quat lg_quat_get_identity()
Definition: lg_quaternions.c:429
lg_quatv
LG_QuatV * lg_quatv()
Definition: lg_quaternions.c:679
lg_axis_angle_to_euler_ang
LG_EulerAng lg_axis_angle_to_euler_ang(LG_AxAng axis_ang)
Definition: lg_quaternions.c:562
m3_transpose
mat3_t m3_transpose(mat3_t matrix)
Definition: lg_quaternions.c:643
lg_quats_multiply
LG_Quat lg_quats_multiply(LG_Quat q1, LG_Quat q2)
Definition: lg_quaternions.c:79
lg_quat_get_normalized
LG_Quat lg_quat_get_normalized(LG_Quat q)
Definition: lg_quaternions.c:59
lg_quat_copy
int lg_quat_copy(LG_Quat *q_dest, const LG_Quat *q_src)
Definition: lg_quaternions.c:299
lg_quat_from_3x3_rot_matrix
LG_Quat lg_quat_from_3x3_rot_matrix(mat3_t m)
Definition: lg_quaternions.c:220
lg_quat_get_invert
LG_Quat lg_quat_get_invert(LG_Quat q)
Definition: lg_quaternions.c:378
LG_AxAng
Definition: lg_quaternions.h:23
lg_quats_dot
double lg_quats_dot(LG_Quat *q1, LG_Quat *q2)
Definition: lg_quaternions.c:404
lg_quats_are_equal
zboolean lg_quats_are_equal(LG_Quat *q1, LG_Quat *q2)
Definition: lg_quaternions.c:322
lg_quat_from_axis_angle
LG_Quat lg_quat_from_axis_angle(LG_AxAng axis_ang)
Definition: lg_quaternions.c:166
lg_quat_invert
void lg_quat_invert(LG_Quat *q)
Definition: lg_quaternions.c:366
mat4_t
Definition: math_3d.h:167