152 lines
9.3 KiB
C
152 lines
9.3 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(BGC_FP32_Matrix3x3* matrix, const float d1, const float d2, const float d3);
|
|
extern inline void bgc_fp64_matrix3x3_make_diagonal(BGC_FP64_Matrix3x3* matrix, const double d1, const double d2, const double d3);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_copy(BGC_FP32_Matrix3x3* destination, const BGC_FP32_Matrix3x3* source);
|
|
extern inline void bgc_fp64_matrix3x3_copy(BGC_FP64_Matrix3x3* destination, const BGC_FP64_Matrix3x3* source);
|
|
|
|
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(BGC_FP32_Matrix3x3* destination, const BGC_FP64_Matrix3x3* source);
|
|
extern inline void bgc_fp32_matrix3x3_convert_to_fp64(BGC_FP64_Matrix3x3* destination, const BGC_FP32_Matrix3x3* source);
|
|
|
|
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(BGC_FP32_Matrix3x3* transposed, const BGC_FP32_Matrix3x3* matrix);
|
|
extern inline void bgc_fp64_matrix3x3_get_transposed(BGC_FP64_Matrix3x3* transposed, const BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_get_row(BGC_FP32_Vector3* row, const BGC_FP32_Matrix3x3* matrix, const int row_number);
|
|
extern inline void bgc_fp64_matrix3x3_get_row(BGC_FP64_Vector3* row, const BGC_FP64_Matrix3x3* matrix, const int row_number);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_set_row(BGC_FP32_Matrix3x3* matrix, const int row_number, const BGC_FP32_Vector3* row);
|
|
extern inline void bgc_fp64_matrix3x3_set_row(BGC_FP64_Matrix3x3* matrix, const int row_number, const BGC_FP64_Vector3* row);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_get_column(BGC_FP32_Vector3* column, const BGC_FP32_Matrix3x3* matrix, const int column_number);
|
|
extern inline void bgc_fp64_matrix3x3_get_column(BGC_FP64_Vector3* column, const BGC_FP64_Matrix3x3* matrix, const int column_number);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_set_column(BGC_FP32_Matrix3x3* matrix, const int column_number, const BGC_FP32_Vector3* column);
|
|
extern inline void bgc_fp64_matrix3x3_set_column(BGC_FP64_Matrix3x3* matrix, const int column_number, const BGC_FP64_Vector3* column);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_add(BGC_FP32_Matrix3x3* sum, const BGC_FP32_Matrix3x3* matrix1, const BGC_FP32_Matrix3x3* matrix2);
|
|
extern inline void bgc_fp64_matrix3x3_add(BGC_FP64_Matrix3x3* sum, const BGC_FP64_Matrix3x3* matrix1, const BGC_FP64_Matrix3x3* matrix2);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_add_scaled(BGC_FP32_Matrix3x3* sum, const BGC_FP32_Matrix3x3* basic_matrix, const BGC_FP32_Matrix3x3* scalable_matrix, const float scale);
|
|
extern inline void bgc_fp64_matrix3x3_add_scaled(BGC_FP64_Matrix3x3* sum, const BGC_FP64_Matrix3x3* basic_matrix, const BGC_FP64_Matrix3x3* scalable_matrix, const double scale);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_subtract(BGC_FP32_Matrix3x3* difference, const BGC_FP32_Matrix3x3* minuend, const BGC_FP32_Matrix3x3* subtrahend);
|
|
extern inline void bgc_fp64_matrix3x3_subtract(BGC_FP64_Matrix3x3* difference, const BGC_FP64_Matrix3x3* minuend, const BGC_FP64_Matrix3x3* subtrahend);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_multiply(BGC_FP32_Matrix3x3* product, const BGC_FP32_Matrix3x3* multiplicand, const float multiplier);
|
|
extern inline void bgc_fp64_matrix3x3_multiply(BGC_FP64_Matrix3x3* product, const BGC_FP64_Matrix3x3* multiplicand, const double multiplier);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_divide(BGC_FP32_Matrix3x3* quotient, const BGC_FP32_Matrix3x3* dividend, const float divisor);
|
|
extern inline void bgc_fp64_matrix3x3_divide(BGC_FP64_Matrix3x3* quotient, const BGC_FP64_Matrix3x3* dividend, const double divisor);
|
|
|
|
extern inline void bgc_fp32_matrix3x3_interpolate(BGC_FP32_Matrix3x3* interpolation, const BGC_FP32_Matrix3x3* first, const BGC_FP32_Matrix3x3* second, const float phase);
|
|
extern inline void bgc_fp64_matrix3x3_interpolate(BGC_FP64_Matrix3x3* interpolation, const BGC_FP64_Matrix3x3* first, const BGC_FP64_Matrix3x3* second, const double phase);
|
|
|
|
extern inline void bgc_fp32_multiply_vector3_by_matrix3x3(BGC_FP32_Vector3* product, const BGC_FP32_Vector3* vector, const BGC_FP32_Matrix3x3* matrix);
|
|
extern inline void bgc_fp64_multiply_vector3_by_matrix3x3(BGC_FP64_Vector3* product, const BGC_FP64_Vector3* vector, const BGC_FP64_Matrix3x3* matrix);
|
|
|
|
extern inline void bgc_fp32_multiply_matrix3x3_by_vector3(BGC_FP32_Vector3* product, const BGC_FP32_Matrix3x3* matrix, const BGC_FP32_Vector3* vector);
|
|
extern inline void bgc_fp64_multiply_matrix3x3_by_vector3(BGC_FP64_Vector3* product, const BGC_FP64_Matrix3x3* matrix, const BGC_FP64_Vector3* vector);
|
|
|
|
// ================ Get Inverse ================= //
|
|
|
|
int bgc_fp32_matrix3x3_get_inverse(BGC_FP32_Matrix3x3* inverse, const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
const float determinant = bgc_fp32_matrix3x3_get_determinant(matrix);
|
|
|
|
if (bgc_fp32_is_zero(determinant)) {
|
|
return 0;
|
|
}
|
|
|
|
const float row1_col1 = matrix->row2_col2 * matrix->row3_col3 - matrix->row2_col3 * matrix->row3_col2;
|
|
const float row1_col2 = matrix->row1_col3 * matrix->row3_col2 - matrix->row1_col2 * matrix->row3_col3;
|
|
const float row1_col3 = matrix->row1_col2 * matrix->row2_col3 - matrix->row1_col3 * matrix->row2_col2;
|
|
|
|
const float row2_col1 = matrix->row2_col3 * matrix->row3_col1 - matrix->row2_col1 * matrix->row3_col3;
|
|
const float row2_col2 = matrix->row1_col1 * matrix->row3_col3 - matrix->row1_col3 * matrix->row3_col1;
|
|
const float row2_col3 = matrix->row1_col3 * matrix->row2_col1 - matrix->row1_col1 * matrix->row2_col3;
|
|
|
|
const float row3_col1 = matrix->row2_col1 * matrix->row3_col2 - matrix->row2_col2 * matrix->row3_col1;
|
|
const float row3_col2 = matrix->row1_col2 * matrix->row3_col1 - matrix->row1_col1 * matrix->row3_col2;
|
|
const float row3_col3 = matrix->row1_col1 * matrix->row2_col2 - matrix->row1_col2 * matrix->row2_col1;
|
|
|
|
const float multiplier = 1.0f / determinant;
|
|
|
|
inverse->row1_col1 = row1_col1 * multiplier;
|
|
inverse->row1_col2 = row1_col2 * multiplier;
|
|
inverse->row1_col3 = row1_col3 * multiplier;
|
|
|
|
inverse->row2_col1 = row2_col1 * multiplier;
|
|
inverse->row2_col2 = row2_col2 * multiplier;
|
|
inverse->row2_col3 = row2_col3 * multiplier;
|
|
|
|
inverse->row3_col1 = row3_col1 * multiplier;
|
|
inverse->row3_col2 = row3_col2 * multiplier;
|
|
inverse->row3_col3 = row3_col3 * multiplier;
|
|
|
|
return 1;
|
|
}
|
|
|
|
int bgc_fp64_matrix3x3_get_inverse(BGC_FP64_Matrix3x3* inverse, const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
const double determinant = bgc_fp64_matrix3x3_get_determinant(matrix);
|
|
|
|
if (bgc_fp64_is_zero(determinant)) {
|
|
return 0;
|
|
}
|
|
|
|
const double row1_col1 = matrix->row2_col2 * matrix->row3_col3 - matrix->row2_col3 * matrix->row3_col2;
|
|
const double row1_col2 = matrix->row1_col3 * matrix->row3_col2 - matrix->row1_col2 * matrix->row3_col3;
|
|
const double row1_col3 = matrix->row1_col2 * matrix->row2_col3 - matrix->row1_col3 * matrix->row2_col2;
|
|
|
|
const double row2_col1 = matrix->row2_col3 * matrix->row3_col1 - matrix->row2_col1 * matrix->row3_col3;
|
|
const double row2_col2 = matrix->row1_col1 * matrix->row3_col3 - matrix->row1_col3 * matrix->row3_col1;
|
|
const double row2_col3 = matrix->row1_col3 * matrix->row2_col1 - matrix->row1_col1 * matrix->row2_col3;
|
|
|
|
const double row3_col1 = matrix->row2_col1 * matrix->row3_col2 - matrix->row2_col2 * matrix->row3_col1;
|
|
const double row3_col2 = matrix->row1_col2 * matrix->row3_col1 - matrix->row1_col1 * matrix->row3_col2;
|
|
const double row3_col3 = matrix->row1_col1 * matrix->row2_col2 - matrix->row1_col2 * matrix->row2_col1;
|
|
|
|
const double multiplier = 1.0 / determinant;
|
|
|
|
inverse->row1_col1 = row1_col1 * multiplier;
|
|
inverse->row1_col2 = row1_col2 * multiplier;
|
|
inverse->row1_col3 = row1_col3 * multiplier;
|
|
|
|
inverse->row2_col1 = row2_col1 * multiplier;
|
|
inverse->row2_col2 = row2_col2 * multiplier;
|
|
inverse->row2_col3 = row2_col3 * multiplier;
|
|
|
|
inverse->row3_col1 = row3_col1 * multiplier;
|
|
inverse->row3_col2 = row3_col2 * multiplier;
|
|
inverse->row3_col3 = row3_col3 * multiplier;
|
|
|
|
return 1;
|
|
}
|