152 lines
8.6 KiB
C
152 lines
8.6 KiB
C
#include "matrix3x3.h"
|
|
|
|
extern inline void bgc_fp32_matrix3x3_reset(BGC_FP32_Matrix3x3* matrix);
|
|
extern inline void bgc_fp64_matrix3x3_reset(BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_make_identity(BGC_FP32_Matrix3x3* matrix);
|
|
extern inline void bgc_fp64_matrix3x3_make_identity(BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_make_diagonal(const float d1, const float d2, const float d3, BGC_FP32_Matrix3x3* matrix);
|
|
extern inline void bgc_fp64_matrix3x3_make_diagonal(const double d1, const double d2, const double d3, BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_copy(const BGC_FP32_Matrix3x3* source, BGC_FP32_Matrix3x3* destination);
|
|
extern inline void bgc_fp64_matrix3x3_copy(const BGC_FP64_Matrix3x3* source, BGC_FP64_Matrix3x3* destination);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_swap(BGC_FP32_Matrix3x3* matrix1, BGC_FP32_Matrix3x3* matrix2);
|
|
extern inline void bgc_fp64_matrix3x3_swap(BGC_FP64_Matrix3x3* matrix1, BGC_FP64_Matrix3x3* matrix2);
|
|
|
|
extern inline void bgc_fp64_matrix3x3_convert_to_fp32(const BGC_FP64_Matrix3x3* source, BGC_FP32_Matrix3x3* destination);
|
|
extern inline void bgc_fp32_matrix3x3_convert_to_fp64(const BGC_FP32_Matrix3x3* source, BGC_FP64_Matrix3x3* destination);
|
|
|
|
extern inline float bgc_fp32_matrix3x3_get_determinant(const BGC_FP32_Matrix3x3* matrix);
|
|
extern inline double bgc_fp64_matrix3x3_get_determinant(const BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline int bgc_fp32_matrix3x3_is_identity(const BGC_FP32_Matrix3x3* matrix);
|
|
extern inline int bgc_fp64_matrix3x3_is_identity(const BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline int bgc_fp32_matrix3x3_is_singular(const BGC_FP32_Matrix3x3* matrix);
|
|
extern inline int bgc_fp64_matrix3x3_is_singular(const BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline int bgc_fp32_matrix3x3_is_rotation(const BGC_FP32_Matrix3x3* matrix);
|
|
extern inline int bgc_fp64_matrix3x3_is_rotation(const BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline int bgc_fp32_matrix3x3_invert(BGC_FP32_Matrix3x3* matrix);
|
|
extern inline int bgc_fp64_matrix3x3_invert(BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_transpose(BGC_FP32_Matrix3x3* matrix);
|
|
extern inline void bgc_fp64_matrix3x3_transpose(BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_get_transposed(const BGC_FP32_Matrix3x3* matrix, BGC_FP32_Matrix3x3* transposed);
|
|
extern inline void bgc_fp64_matrix3x3_get_transposed(const BGC_FP64_Matrix3x3* matrix, BGC_FP64_Matrix3x3* transposed);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_get_row(const int number, const BGC_FP32_Matrix3x3* matrix, BGC_FP32_Vector3* row);
|
|
extern inline void bgc_fp64_matrix3x3_get_row(const int number, const BGC_FP64_Matrix3x3* matrix, BGC_FP64_Vector3* row);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_set_row(const int number, const BGC_FP32_Vector3* row, BGC_FP32_Matrix3x3* matrix);
|
|
extern inline void bgc_fp64_matrix3x3_set_row(const int number, const BGC_FP64_Vector3* row, BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_get_column(const int number, const BGC_FP32_Matrix3x3* matrix, BGC_FP32_Vector3* column);
|
|
extern inline void bgc_fp64_matrix3x3_get_column(const int number, const BGC_FP64_Matrix3x3* matrix, BGC_FP64_Vector3* column);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_set_column(const int number, const BGC_FP32_Vector3* column, BGC_FP32_Matrix3x3* matrix);
|
|
extern inline void bgc_fp64_matrix3x3_set_column(const int number, const BGC_FP64_Vector3* column, BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_add(const BGC_FP32_Matrix3x3* matrix1, const BGC_FP32_Matrix3x3* matrix2, BGC_FP32_Matrix3x3* sum);
|
|
extern inline void bgc_fp64_matrix3x3_add(const BGC_FP64_Matrix3x3* matrix1, const BGC_FP64_Matrix3x3* matrix2, BGC_FP64_Matrix3x3* sum);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_add_scaled(const BGC_FP32_Matrix3x3* basic_matrix, const BGC_FP32_Matrix3x3* scalable_matrix, const float scale, BGC_FP32_Matrix3x3* sum);
|
|
extern inline void bgc_fp64_matrix3x3_add_scaled(const BGC_FP64_Matrix3x3* basic_matrix, const BGC_FP64_Matrix3x3* scalable_matrix, const double scale, BGC_FP64_Matrix3x3* sum);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_subtract(const BGC_FP32_Matrix3x3* minuend, const BGC_FP32_Matrix3x3* subtrahend, BGC_FP32_Matrix3x3* difference);
|
|
extern inline void bgc_fp64_matrix3x3_subtract(const BGC_FP64_Matrix3x3* minuend, const BGC_FP64_Matrix3x3* subtrahend, BGC_FP64_Matrix3x3* difference);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_multiply(const BGC_FP32_Matrix3x3* multiplicand, const float multiplier, BGC_FP32_Matrix3x3* product);
|
|
extern inline void bgc_fp64_matrix3x3_multiply(const BGC_FP64_Matrix3x3* multiplicand, const double multiplier, BGC_FP64_Matrix3x3* product);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_divide(const BGC_FP32_Matrix3x3* dividend, const float divisor, BGC_FP32_Matrix3x3* quotient);
|
|
extern inline void bgc_fp64_matrix3x3_divide(const BGC_FP64_Matrix3x3* dividend, const double divisor, BGC_FP64_Matrix3x3* quotient);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_interpolate(const BGC_FP32_Matrix3x3* first, const BGC_FP32_Matrix3x3* second, const float phase, BGC_FP32_Matrix3x3* interpolation);
|
|
extern inline void bgc_fp64_matrix3x3_interpolate(const BGC_FP64_Matrix3x3* first, const BGC_FP64_Matrix3x3* second, const double phase, BGC_FP64_Matrix3x3* interpolation);
|
|
|
|
extern inline void bgc_fp32_multiply_vector3_by_matrix3x3(const BGC_FP32_Vector3* vector, const BGC_FP32_Matrix3x3* matrix, BGC_FP32_Vector3* result);
|
|
extern inline void bgc_fp64_multiply_vector3_by_matrix3x3(const BGC_FP64_Vector3* vector, const BGC_FP64_Matrix3x3* matrix, BGC_FP64_Vector3* result);
|
|
|
|
extern inline void bgc_fp32_multiply_matrix3x3_by_vector3(const BGC_FP32_Matrix3x3* matrix, const BGC_FP32_Vector3* vector, BGC_FP32_Vector3* result);
|
|
extern inline void bgc_fp64_multiply_matrix3x3_by_vector3(const BGC_FP64_Matrix3x3* matrix, const BGC_FP64_Vector3* vector, BGC_FP64_Vector3* result);
|
|
|
|
// ================ Get Inverse ================= //
|
|
|
|
int bgc_fp32_matrix3x3_get_inverse(const BGC_FP32_Matrix3x3* matrix, BGC_FP32_Matrix3x3* inverse)
|
|
{
|
|
const float determinant = bgc_fp32_matrix3x3_get_determinant(matrix);
|
|
|
|
if (bgc_fp32_is_zero(determinant)) {
|
|
return 0;
|
|
}
|
|
|
|
const float r1c1 = matrix->r2c2 * matrix->r3c3 - matrix->r2c3 * matrix->r3c2;
|
|
const float r1c2 = matrix->r1c3 * matrix->r3c2 - matrix->r1c2 * matrix->r3c3;
|
|
const float r1c3 = matrix->r1c2 * matrix->r2c3 - matrix->r1c3 * matrix->r2c2;
|
|
|
|
const float r2c1 = matrix->r2c3 * matrix->r3c1 - matrix->r2c1 * matrix->r3c3;
|
|
const float r2c2 = matrix->r1c1 * matrix->r3c3 - matrix->r1c3 * matrix->r3c1;
|
|
const float r2c3 = matrix->r1c3 * matrix->r2c1 - matrix->r1c1 * matrix->r2c3;
|
|
|
|
const float r3c1 = matrix->r2c1 * matrix->r3c2 - matrix->r2c2 * matrix->r3c1;
|
|
const float r3c2 = matrix->r1c2 * matrix->r3c1 - matrix->r1c1 * matrix->r3c2;
|
|
const float r3c3 = matrix->r1c1 * matrix->r2c2 - matrix->r1c2 * matrix->r2c1;
|
|
|
|
const float multiplier = 1.0f / determinant;
|
|
|
|
inverse->r1c1 = r1c1 * multiplier;
|
|
inverse->r1c2 = r1c2 * multiplier;
|
|
inverse->r1c3 = r1c3 * multiplier;
|
|
|
|
inverse->r2c1 = r2c1 * multiplier;
|
|
inverse->r2c2 = r2c2 * multiplier;
|
|
inverse->r2c3 = r2c3 * multiplier;
|
|
|
|
inverse->r3c1 = r3c1 * multiplier;
|
|
inverse->r3c2 = r3c2 * multiplier;
|
|
inverse->r3c3 = r3c3 * multiplier;
|
|
|
|
return 1;
|
|
}
|
|
|
|
int bgc_fp64_matrix3x3_get_inverse(const BGC_FP64_Matrix3x3* matrix, BGC_FP64_Matrix3x3* inverse)
|
|
{
|
|
const double determinant = bgc_fp64_matrix3x3_get_determinant(matrix);
|
|
|
|
if (bgc_fp64_is_zero(determinant)) {
|
|
return 0;
|
|
}
|
|
|
|
const double r1c1 = matrix->r2c2 * matrix->r3c3 - matrix->r2c3 * matrix->r3c2;
|
|
const double r1c2 = matrix->r1c3 * matrix->r3c2 - matrix->r1c2 * matrix->r3c3;
|
|
const double r1c3 = matrix->r1c2 * matrix->r2c3 - matrix->r1c3 * matrix->r2c2;
|
|
|
|
const double r2c1 = matrix->r2c3 * matrix->r3c1 - matrix->r2c1 * matrix->r3c3;
|
|
const double r2c2 = matrix->r1c1 * matrix->r3c3 - matrix->r1c3 * matrix->r3c1;
|
|
const double r2c3 = matrix->r1c3 * matrix->r2c1 - matrix->r1c1 * matrix->r2c3;
|
|
|
|
const double r3c1 = matrix->r2c1 * matrix->r3c2 - matrix->r2c2 * matrix->r3c1;
|
|
const double r3c2 = matrix->r1c2 * matrix->r3c1 - matrix->r1c1 * matrix->r3c2;
|
|
const double r3c3 = matrix->r1c1 * matrix->r2c2 - matrix->r1c2 * matrix->r2c1;
|
|
|
|
const double multiplier = 1.0 / determinant;
|
|
|
|
inverse->r1c1 = r1c1 * multiplier;
|
|
inverse->r1c2 = r1c2 * multiplier;
|
|
inverse->r1c3 = r1c3 * multiplier;
|
|
|
|
inverse->r2c1 = r2c1 * multiplier;
|
|
inverse->r2c2 = r2c2 * multiplier;
|
|
inverse->r2c3 = r2c3 * multiplier;
|
|
|
|
inverse->r3c1 = r3c1 * multiplier;
|
|
inverse->r3c2 = r3c2 * multiplier;
|
|
inverse->r3c3 = r3c3 * multiplier;
|
|
|
|
return 1;
|
|
}
|