157 lines
5.8 KiB
C
157 lines
5.8 KiB
C
#include "quaternion.h"
|
|
|
|
// ============ Make Rotation Matrix ============ //
|
|
|
|
void sp_quaternion_make_matrix(const SPQuaternion* quaternion, SPMatrix3x3* matrix)
|
|
{
|
|
const float s0s0 = quaternion->s0 * quaternion->s0;
|
|
const float x1x1 = quaternion->x1 * quaternion->x1;
|
|
const float x2x2 = quaternion->x2 * quaternion->x2;
|
|
const float x3x3 = quaternion->x3 * quaternion->x3;
|
|
|
|
const float square_module = (s0s0 + x1x1) + (x2x2 + x3x3);
|
|
|
|
if (-SP_EPSYLON <= square_module && square_module <= SP_EPSYLON)
|
|
{
|
|
sp_matrix3x3_make_identity(matrix);
|
|
return;
|
|
}
|
|
|
|
const float corrector1 = 1.0f / square_module;
|
|
const float corrector2 = 2.0f * corrector1;
|
|
|
|
const float s0x1 = quaternion->s0 * quaternion->x1;
|
|
const float s0x2 = quaternion->s0 * quaternion->x2;
|
|
const float s0x3 = quaternion->s0 * quaternion->x3;
|
|
const float x1x2 = quaternion->x1 * quaternion->x2;
|
|
const float x1x3 = quaternion->x1 * quaternion->x3;
|
|
const float x2x3 = quaternion->x2 * quaternion->x3;
|
|
|
|
matrix->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3));
|
|
matrix->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3));
|
|
matrix->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2));
|
|
|
|
matrix->r1c2 = corrector2 * (x1x2 - s0x3);
|
|
matrix->r2c3 = corrector2 * (x2x3 - s0x1);
|
|
matrix->r3c1 = corrector2 * (x1x3 - s0x2);
|
|
|
|
matrix->r2c1 = corrector2 * (x1x2 + s0x3);
|
|
matrix->r3c2 = corrector2 * (x2x3 + s0x1);
|
|
matrix->r1c3 = corrector2 * (x1x3 + s0x2);
|
|
}
|
|
|
|
void dp_quaternion_make_matrix(const DPQuaternion* quaternion, DPMatrix3x3* matrix)
|
|
{
|
|
const double s0s0 = quaternion->s0 * quaternion->s0;
|
|
const double x1x1 = quaternion->x1 * quaternion->x1;
|
|
const double x2x2 = quaternion->x2 * quaternion->x2;
|
|
const double x3x3 = quaternion->x3 * quaternion->x3;
|
|
|
|
const double square_module = (s0s0 + x1x1) + (x2x2 + x3x3);
|
|
|
|
if (-DP_EPSYLON <= square_module && square_module <= DP_EPSYLON)
|
|
{
|
|
dp_matrix3x3_make_identity(matrix);
|
|
return;
|
|
}
|
|
|
|
const double corrector1 = 1.0f / square_module;
|
|
const double corrector2 = 2.0f * corrector1;
|
|
|
|
const double s0x1 = quaternion->s0 * quaternion->x1;
|
|
const double s0x2 = quaternion->s0 * quaternion->x2;
|
|
const double s0x3 = quaternion->s0 * quaternion->x3;
|
|
const double x1x2 = quaternion->x1 * quaternion->x2;
|
|
const double x1x3 = quaternion->x1 * quaternion->x3;
|
|
const double x2x3 = quaternion->x2 * quaternion->x3;
|
|
|
|
matrix->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3));
|
|
matrix->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3));
|
|
matrix->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2));
|
|
|
|
matrix->r1c2 = corrector2 * (x1x2 - s0x3);
|
|
matrix->r2c3 = corrector2 * (x2x3 - s0x1);
|
|
matrix->r3c1 = corrector2 * (x1x3 - s0x2);
|
|
|
|
matrix->r2c1 = corrector2 * (x1x2 + s0x3);
|
|
matrix->r3c2 = corrector2 * (x2x3 + s0x1);
|
|
matrix->r1c3 = corrector2 * (x1x3 + s0x2);
|
|
}
|
|
|
|
// ============ Make Reverse Matrix ============= //
|
|
|
|
void sp_quaternion_make_reverse_matrix(const SPQuaternion* quaternion, SPMatrix3x3* matrix)
|
|
{
|
|
const float s0s0 = quaternion->s0 * quaternion->s0;
|
|
const float x1x1 = quaternion->x1 * quaternion->x1;
|
|
const float x2x2 = quaternion->x2 * quaternion->x2;
|
|
const float x3x3 = quaternion->x3 * quaternion->x3;
|
|
|
|
const float square_module = (s0s0 + x1x1) + (x2x2 + x3x3);
|
|
|
|
if (-SP_EPSYLON <= square_module && square_module <= SP_EPSYLON)
|
|
{
|
|
sp_matrix3x3_make_identity(matrix);
|
|
return;
|
|
}
|
|
|
|
const float corrector1 = 1.0f / square_module;
|
|
const float corrector2 = 2.0f * corrector1;
|
|
|
|
const float s0x1 = quaternion->s0 * quaternion->x1;
|
|
const float s0x2 = quaternion->s0 * quaternion->x2;
|
|
const float s0x3 = quaternion->s0 * quaternion->x3;
|
|
const float x1x2 = quaternion->x1 * quaternion->x2;
|
|
const float x1x3 = quaternion->x1 * quaternion->x3;
|
|
const float x2x3 = quaternion->x2 * quaternion->x3;
|
|
|
|
matrix->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3));
|
|
matrix->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3));
|
|
matrix->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2));
|
|
|
|
matrix->r1c2 = corrector2 * (x1x2 + s0x3);
|
|
matrix->r2c3 = corrector2 * (x2x3 + s0x1);
|
|
matrix->r3c1 = corrector2 * (x1x3 + s0x2);
|
|
|
|
matrix->r2c1 = corrector2 * (x1x2 - s0x3);
|
|
matrix->r3c2 = corrector2 * (x2x3 - s0x1);
|
|
matrix->r1c3 = corrector2 * (x1x3 - s0x2);
|
|
}
|
|
|
|
void dp_quaternion_make_reverse_matrix(const DPQuaternion* quaternion, DPMatrix3x3* matrix)
|
|
{
|
|
const double s0s0 = quaternion->s0 * quaternion->s0;
|
|
const double x1x1 = quaternion->x1 * quaternion->x1;
|
|
const double x2x2 = quaternion->x2 * quaternion->x2;
|
|
const double x3x3 = quaternion->x3 * quaternion->x3;
|
|
|
|
const double square_module = (s0s0 + x1x1) + (x2x2 + x3x3);
|
|
|
|
if (-DP_EPSYLON <= square_module && square_module <= DP_EPSYLON)
|
|
{
|
|
dp_matrix3x3_make_identity(matrix);
|
|
return;
|
|
}
|
|
|
|
const double corrector1 = 1.0f / square_module;
|
|
const double corrector2 = 2.0f * corrector1;
|
|
|
|
const double s0x1 = quaternion->s0 * quaternion->x1;
|
|
const double s0x2 = quaternion->s0 * quaternion->x2;
|
|
const double s0x3 = quaternion->s0 * quaternion->x3;
|
|
const double x1x2 = quaternion->x1 * quaternion->x2;
|
|
const double x1x3 = quaternion->x1 * quaternion->x3;
|
|
const double x2x3 = quaternion->x2 * quaternion->x3;
|
|
|
|
matrix->r1c1 = corrector1 * ((s0s0 + x1x1) - (x2x2 + x3x3));
|
|
matrix->r2c2 = corrector1 * ((s0s0 + x2x2) - (x1x1 + x3x3));
|
|
matrix->r3c3 = corrector1 * ((s0s0 + x3x3) - (x1x1 + x2x2));
|
|
|
|
matrix->r1c2 = corrector2 * (x1x2 + s0x3);
|
|
matrix->r2c3 = corrector2 * (x2x3 + s0x1);
|
|
matrix->r3c1 = corrector2 * (x1x3 + s0x2);
|
|
|
|
matrix->r2c1 = corrector2 * (x1x2 - s0x3);
|
|
matrix->r3c2 = corrector2 * (x2x3 - s0x1);
|
|
matrix->r1c3 = corrector2 * (x1x3 - s0x2);
|
|
}
|