881 lines
32 KiB
C
881 lines
32 KiB
C
#ifndef _BGC_MATRIX3X3_H_
|
|
#define _BGC_MATRIX3X3_H_
|
|
|
|
#include "vector3.h"
|
|
#include "matrices.h"
|
|
|
|
// =================== Reset ==================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_reset(BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
matrix->row1_col1 = 0.0f;
|
|
matrix->row1_col2 = 0.0f;
|
|
matrix->row1_col3 = 0.0f;
|
|
|
|
matrix->row2_col1 = 0.0f;
|
|
matrix->row2_col2 = 0.0f;
|
|
matrix->row2_col3 = 0.0f;
|
|
|
|
matrix->row3_col1 = 0.0f;
|
|
matrix->row3_col2 = 0.0f;
|
|
matrix->row3_col3 = 0.0f;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_reset(BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
matrix->row1_col1 = 0.0;
|
|
matrix->row1_col2 = 0.0;
|
|
matrix->row1_col3 = 0.0;
|
|
|
|
matrix->row2_col1 = 0.0;
|
|
matrix->row2_col2 = 0.0;
|
|
matrix->row2_col3 = 0.0;
|
|
|
|
matrix->row3_col1 = 0.0;
|
|
matrix->row3_col2 = 0.0;
|
|
matrix->row3_col3 = 0.0;
|
|
}
|
|
|
|
// ================== Identity ================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_make_identity(BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
matrix->row1_col1 = 1.0f;
|
|
matrix->row1_col2 = 0.0f;
|
|
matrix->row1_col3 = 0.0f;
|
|
|
|
matrix->row2_col1 = 0.0f;
|
|
matrix->row2_col2 = 1.0f;
|
|
matrix->row2_col3 = 0.0f;
|
|
|
|
matrix->row3_col1 = 0.0f;
|
|
matrix->row3_col2 = 0.0f;
|
|
matrix->row3_col3 = 1.0f;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_make_identity(BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
matrix->row1_col1 = 1.0;
|
|
matrix->row1_col2 = 0.0;
|
|
matrix->row1_col3 = 0.0;
|
|
|
|
matrix->row2_col1 = 0.0;
|
|
matrix->row2_col2 = 1.0;
|
|
matrix->row2_col3 = 0.0;
|
|
|
|
matrix->row3_col1 = 0.0;
|
|
matrix->row3_col2 = 0.0;
|
|
matrix->row3_col3 = 1.0;
|
|
}
|
|
|
|
// ================ Set Diagonal ================ //
|
|
|
|
inline void bgc_fp32_matrix3x3_make_diagonal(BGC_FP32_Matrix3x3* matrix, const float d1, const float d2, const float d3)
|
|
{
|
|
matrix->row1_col1 = d1;
|
|
matrix->row1_col2 = 0.0f;
|
|
matrix->row1_col3 = 0.0f;
|
|
|
|
matrix->row2_col1 = 0.0f;
|
|
matrix->row2_col2 = d2;
|
|
matrix->row2_col3 = 0.0f;
|
|
|
|
matrix->row3_col1 = 0.0f;
|
|
matrix->row3_col2 = 0.0f;
|
|
matrix->row3_col3 = d2;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_make_diagonal(BGC_FP64_Matrix3x3* matrix, const double d1, const double d2, const double d3)
|
|
{
|
|
matrix->row1_col1 = d1;
|
|
matrix->row1_col2 = 0.0;
|
|
matrix->row1_col3 = 0.0;
|
|
|
|
matrix->row2_col1 = 0.0;
|
|
matrix->row2_col2 = d2;
|
|
matrix->row2_col3 = 0.0;
|
|
|
|
matrix->row3_col1 = 0.0;
|
|
matrix->row3_col2 = 0.0;
|
|
matrix->row3_col3 = d2;
|
|
}
|
|
|
|
// ==================== Copy ==================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_copy(BGC_FP32_Matrix3x3* destination, const BGC_FP32_Matrix3x3* source)
|
|
{
|
|
destination->row1_col1 = source->row1_col1;
|
|
destination->row1_col2 = source->row1_col2;
|
|
destination->row1_col3 = source->row1_col3;
|
|
|
|
destination->row2_col1 = source->row2_col1;
|
|
destination->row2_col2 = source->row2_col2;
|
|
destination->row2_col3 = source->row2_col3;
|
|
|
|
destination->row3_col1 = source->row3_col1;
|
|
destination->row3_col2 = source->row3_col2;
|
|
destination->row3_col3 = source->row3_col3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_copy(BGC_FP64_Matrix3x3* destination, const BGC_FP64_Matrix3x3* source)
|
|
{
|
|
destination->row1_col1 = source->row1_col1;
|
|
destination->row1_col2 = source->row1_col2;
|
|
destination->row1_col3 = source->row1_col3;
|
|
|
|
destination->row2_col1 = source->row2_col1;
|
|
destination->row2_col2 = source->row2_col2;
|
|
destination->row2_col3 = source->row2_col3;
|
|
|
|
destination->row3_col1 = source->row3_col1;
|
|
destination->row3_col2 = source->row3_col2;
|
|
destination->row3_col3 = source->row3_col3;
|
|
}
|
|
|
|
// ==================== Swap ==================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_swap(BGC_FP32_Matrix3x3* matrix1, BGC_FP32_Matrix3x3* matrix2)
|
|
{
|
|
const float row1_col1 = matrix2->row1_col1;
|
|
const float row1_col2 = matrix2->row1_col2;
|
|
const float row1_col3 = matrix2->row1_col3;
|
|
|
|
const float row2_col1 = matrix2->row2_col1;
|
|
const float row2_col2 = matrix2->row2_col2;
|
|
const float row2_col3 = matrix2->row2_col3;
|
|
|
|
const float row3_col1 = matrix2->row3_col1;
|
|
const float row3_col2 = matrix2->row3_col2;
|
|
const float row3_col3 = matrix2->row3_col3;
|
|
|
|
matrix2->row1_col1 = matrix1->row1_col1;
|
|
matrix2->row1_col2 = matrix1->row1_col2;
|
|
matrix2->row1_col3 = matrix1->row1_col3;
|
|
|
|
matrix2->row2_col1 = matrix1->row2_col1;
|
|
matrix2->row2_col2 = matrix1->row2_col2;
|
|
matrix2->row2_col3 = matrix1->row2_col3;
|
|
|
|
matrix2->row3_col1 = matrix1->row3_col1;
|
|
matrix2->row3_col2 = matrix1->row3_col2;
|
|
matrix2->row3_col3 = matrix1->row3_col3;
|
|
|
|
matrix1->row1_col1 = row1_col1;
|
|
matrix1->row1_col2 = row1_col2;
|
|
matrix1->row1_col3 = row1_col3;
|
|
|
|
matrix1->row2_col1 = row2_col1;
|
|
matrix1->row2_col2 = row2_col2;
|
|
matrix1->row2_col3 = row2_col3;
|
|
|
|
matrix1->row3_col1 = row3_col1;
|
|
matrix1->row3_col2 = row3_col2;
|
|
matrix1->row3_col3 = row3_col3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_swap(BGC_FP64_Matrix3x3* matrix1, BGC_FP64_Matrix3x3* matrix2)
|
|
{
|
|
const double row1_col1 = matrix2->row1_col1;
|
|
const double row1_col2 = matrix2->row1_col2;
|
|
const double row1_col3 = matrix2->row1_col3;
|
|
|
|
const double row2_col1 = matrix2->row2_col1;
|
|
const double row2_col2 = matrix2->row2_col2;
|
|
const double row2_col3 = matrix2->row2_col3;
|
|
|
|
const double row3_col1 = matrix2->row3_col1;
|
|
const double row3_col2 = matrix2->row3_col2;
|
|
const double row3_col3 = matrix2->row3_col3;
|
|
|
|
matrix2->row1_col1 = matrix1->row1_col1;
|
|
matrix2->row1_col2 = matrix1->row1_col2;
|
|
matrix2->row1_col3 = matrix1->row1_col3;
|
|
|
|
matrix2->row2_col1 = matrix1->row2_col1;
|
|
matrix2->row2_col2 = matrix1->row2_col2;
|
|
matrix2->row2_col3 = matrix1->row2_col3;
|
|
|
|
matrix2->row3_col1 = matrix1->row3_col1;
|
|
matrix2->row3_col2 = matrix1->row3_col2;
|
|
matrix2->row3_col3 = matrix1->row3_col3;
|
|
|
|
matrix1->row1_col1 = row1_col1;
|
|
matrix1->row1_col2 = row1_col2;
|
|
matrix1->row1_col3 = row1_col3;
|
|
|
|
matrix1->row2_col1 = row2_col1;
|
|
matrix1->row2_col2 = row2_col2;
|
|
matrix1->row2_col3 = row2_col3;
|
|
|
|
matrix1->row3_col1 = row3_col1;
|
|
matrix1->row3_col2 = row3_col2;
|
|
matrix1->row3_col3 = row3_col3;
|
|
}
|
|
|
|
// ================== Convert =================== //
|
|
|
|
inline void bgc_fp64_matrix3x3_convert_to_fp32(BGC_FP32_Matrix3x3* destination, const BGC_FP64_Matrix3x3* source)
|
|
{
|
|
destination->row1_col1 = (float)source->row1_col1;
|
|
destination->row1_col2 = (float)source->row1_col2;
|
|
destination->row1_col3 = (float)source->row1_col3;
|
|
|
|
destination->row2_col1 = (float)source->row2_col1;
|
|
destination->row2_col2 = (float)source->row2_col2;
|
|
destination->row2_col3 = (float)source->row2_col3;
|
|
|
|
destination->row3_col1 = (float)source->row3_col1;
|
|
destination->row3_col2 = (float)source->row3_col2;
|
|
destination->row3_col3 = (float)source->row3_col3;
|
|
}
|
|
|
|
inline void bgc_fp32_matrix3x3_convert_to_fp64(BGC_FP64_Matrix3x3* destination, const BGC_FP32_Matrix3x3* source)
|
|
{
|
|
destination->row1_col1 = source->row1_col1;
|
|
destination->row1_col2 = source->row1_col2;
|
|
destination->row1_col3 = source->row1_col3;
|
|
|
|
destination->row2_col1 = source->row2_col1;
|
|
destination->row2_col2 = source->row2_col2;
|
|
destination->row2_col3 = source->row2_col3;
|
|
|
|
destination->row3_col1 = source->row3_col1;
|
|
destination->row3_col2 = source->row3_col2;
|
|
destination->row3_col3 = source->row3_col3;
|
|
}
|
|
|
|
// ================ Determinant ================= //
|
|
|
|
inline float bgc_fp32_matrix3x3_get_determinant(const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
return matrix->row1_col1 * (matrix->row2_col2 * matrix->row3_col3 - matrix->row2_col3 * matrix->row3_col2)
|
|
+ matrix->row1_col2 * (matrix->row2_col3 * matrix->row3_col1 - matrix->row2_col1 * matrix->row3_col3)
|
|
+ matrix->row1_col3 * (matrix->row2_col1 * matrix->row3_col2 - matrix->row2_col2 * matrix->row3_col1);
|
|
}
|
|
|
|
inline double bgc_fp64_matrix3x3_get_determinant(const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
return matrix->row1_col1 * (matrix->row2_col2 * matrix->row3_col3 - matrix->row2_col3 * matrix->row3_col2)
|
|
+ matrix->row1_col2 * (matrix->row2_col3 * matrix->row3_col1 - matrix->row2_col1 * matrix->row3_col3)
|
|
+ matrix->row1_col3 * (matrix->row2_col1 * matrix->row3_col2 - matrix->row2_col2 * matrix->row3_col1);
|
|
}
|
|
|
|
// ================ Is Identity ================= //
|
|
|
|
inline int bgc_fp32_matrix3x3_is_identity(const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
return bgc_fp32_is_unit(matrix->row1_col1) && bgc_fp32_is_zero(matrix->row1_col2) && bgc_fp32_is_zero(matrix->row1_col3)
|
|
&& bgc_fp32_is_zero(matrix->row2_col1) && bgc_fp32_is_unit(matrix->row2_col2) && bgc_fp32_is_zero(matrix->row2_col3)
|
|
&& bgc_fp32_is_zero(matrix->row3_col1) && bgc_fp32_is_zero(matrix->row3_col2) && bgc_fp32_is_unit(matrix->row3_col3);
|
|
}
|
|
|
|
inline int bgc_fp64_matrix3x3_is_identity(const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
return bgc_fp64_is_unit(matrix->row1_col1) && bgc_fp64_is_zero(matrix->row1_col2) && bgc_fp64_is_zero(matrix->row1_col3)
|
|
&& bgc_fp64_is_zero(matrix->row2_col1) && bgc_fp64_is_unit(matrix->row2_col2) && bgc_fp64_is_zero(matrix->row2_col3)
|
|
&& bgc_fp64_is_zero(matrix->row3_col1) && bgc_fp64_is_zero(matrix->row3_col2) && bgc_fp64_is_unit(matrix->row3_col3);
|
|
}
|
|
|
|
// ================ Is Singular ================= //
|
|
|
|
inline int bgc_fp32_matrix3x3_is_singular(const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
return bgc_fp32_is_zero(bgc_fp32_matrix3x3_get_determinant(matrix));
|
|
}
|
|
|
|
inline int bgc_fp64_matrix3x3_is_singular(const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
return bgc_fp64_is_zero(bgc_fp64_matrix3x3_get_determinant(matrix));
|
|
}
|
|
|
|
// ================ Is Rotation ================= //
|
|
|
|
inline int bgc_fp32_matrix3x3_is_rotation(const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
BGC_FP32_Matrix3x3 product;
|
|
|
|
product.row1_col1 = matrix->row1_col1 * matrix->row1_col1 + matrix->row1_col2 * matrix->row2_col1 + matrix->row1_col3 * matrix->row3_col1;
|
|
product.row1_col2 = matrix->row1_col1 * matrix->row1_col2 + matrix->row1_col2 * matrix->row2_col2 + matrix->row1_col3 * matrix->row3_col2;
|
|
product.row1_col3 = matrix->row1_col1 * matrix->row1_col3 + matrix->row1_col2 * matrix->row2_col3 + matrix->row1_col3 * matrix->row3_col3;
|
|
|
|
product.row2_col1 = matrix->row2_col1 * matrix->row1_col1 + matrix->row2_col2 * matrix->row2_col1 + matrix->row2_col3 * matrix->row3_col1;
|
|
product.row2_col2 = matrix->row2_col1 * matrix->row1_col2 + matrix->row2_col2 * matrix->row2_col2 + matrix->row2_col3 * matrix->row3_col2;
|
|
product.row2_col3 = matrix->row2_col1 * matrix->row1_col3 + matrix->row2_col2 * matrix->row2_col3 + matrix->row2_col3 * matrix->row3_col3;
|
|
|
|
product.row3_col1 = matrix->row3_col1 * matrix->row1_col1 + matrix->row3_col2 * matrix->row2_col1 + matrix->row3_col3 * matrix->row3_col1;
|
|
product.row3_col2 = matrix->row3_col1 * matrix->row1_col2 + matrix->row3_col2 * matrix->row2_col2 + matrix->row3_col3 * matrix->row3_col2;
|
|
product.row3_col3 = matrix->row3_col1 * matrix->row1_col3 + matrix->row3_col2 * matrix->row2_col3 + matrix->row3_col3 * matrix->row3_col3;
|
|
|
|
return bgc_fp32_matrix3x3_is_identity(&product);
|
|
}
|
|
|
|
inline int bgc_fp64_matrix3x3_is_rotation(const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
BGC_FP64_Matrix3x3 product;
|
|
|
|
product.row1_col1 = matrix->row1_col1 * matrix->row1_col1 + matrix->row1_col2 * matrix->row2_col1 + matrix->row1_col3 * matrix->row3_col1;
|
|
product.row1_col2 = matrix->row1_col1 * matrix->row1_col2 + matrix->row1_col2 * matrix->row2_col2 + matrix->row1_col3 * matrix->row3_col2;
|
|
product.row1_col3 = matrix->row1_col1 * matrix->row1_col3 + matrix->row1_col2 * matrix->row2_col3 + matrix->row1_col3 * matrix->row3_col3;
|
|
|
|
product.row2_col1 = matrix->row2_col1 * matrix->row1_col1 + matrix->row2_col2 * matrix->row2_col1 + matrix->row2_col3 * matrix->row3_col1;
|
|
product.row2_col2 = matrix->row2_col1 * matrix->row1_col2 + matrix->row2_col2 * matrix->row2_col2 + matrix->row2_col3 * matrix->row3_col2;
|
|
product.row2_col3 = matrix->row2_col1 * matrix->row1_col3 + matrix->row2_col2 * matrix->row2_col3 + matrix->row2_col3 * matrix->row3_col3;
|
|
|
|
product.row3_col1 = matrix->row3_col1 * matrix->row1_col1 + matrix->row3_col2 * matrix->row2_col1 + matrix->row3_col3 * matrix->row3_col1;
|
|
product.row3_col2 = matrix->row3_col1 * matrix->row1_col2 + matrix->row3_col2 * matrix->row2_col2 + matrix->row3_col3 * matrix->row3_col2;
|
|
product.row3_col3 = matrix->row3_col1 * matrix->row1_col3 + matrix->row3_col2 * matrix->row2_col3 + matrix->row3_col3 * matrix->row3_col3;
|
|
|
|
return bgc_fp64_matrix3x3_is_identity(&product);
|
|
}
|
|
|
|
// ================ Get Inverse ================= //
|
|
|
|
int bgc_fp32_matrix3x3_get_inverse(BGC_FP32_Matrix3x3* inverse, const BGC_FP32_Matrix3x3* matrix);
|
|
|
|
int bgc_fp64_matrix3x3_get_inverse(BGC_FP64_Matrix3x3* inverse, const BGC_FP64_Matrix3x3* matrix);
|
|
|
|
// =================== Invert =================== //
|
|
|
|
inline int bgc_fp32_matrix3x3_invert(BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
return bgc_fp32_matrix3x3_get_inverse(matrix, matrix);
|
|
}
|
|
|
|
inline int bgc_fp64_matrix3x3_invert(BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
return bgc_fp64_matrix3x3_get_inverse(matrix, matrix);
|
|
}
|
|
|
|
// ================= Transpose ================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_transpose(BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
const float row1_col2 = matrix->row1_col2;
|
|
const float row1_col3 = matrix->row1_col3;
|
|
const float row2_col3 = matrix->row2_col3;
|
|
|
|
matrix->row1_col2 = matrix->row2_col1;
|
|
matrix->row1_col3 = matrix->row3_col1;
|
|
matrix->row2_col3 = matrix->row3_col2;
|
|
|
|
matrix->row2_col1 = row1_col2;
|
|
matrix->row3_col1 = row1_col3;
|
|
matrix->row3_col2 = row2_col3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_transpose(BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
const double row1_col2 = matrix->row1_col2;
|
|
const double row1_col3 = matrix->row1_col3;
|
|
const double row2_col3 = matrix->row2_col3;
|
|
|
|
matrix->row1_col2 = matrix->row2_col1;
|
|
matrix->row1_col3 = matrix->row3_col1;
|
|
matrix->row2_col3 = matrix->row3_col2;
|
|
|
|
matrix->row2_col1 = row1_col2;
|
|
matrix->row3_col1 = row1_col3;
|
|
matrix->row3_col2 = row2_col3;
|
|
}
|
|
|
|
// =============== Get Transpose ================ //
|
|
|
|
inline void bgc_fp32_matrix3x3_get_transposed(BGC_FP32_Matrix3x3* transposed, const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
transposed->row1_col1 = matrix->row1_col1;
|
|
transposed->row2_col2 = matrix->row2_col2;
|
|
transposed->row3_col3 = matrix->row3_col3;
|
|
|
|
const float row1_col2 = matrix->row1_col2;
|
|
const float row1_col3 = matrix->row1_col3;
|
|
const float row2_col3 = matrix->row2_col3;
|
|
|
|
transposed->row1_col2 = matrix->row2_col1;
|
|
transposed->row1_col3 = matrix->row3_col1;
|
|
transposed->row2_col3 = matrix->row3_col2;
|
|
|
|
transposed->row2_col1 = row1_col2;
|
|
transposed->row3_col1 = row1_col3;
|
|
transposed->row3_col2 = row2_col3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_get_transposed(BGC_FP64_Matrix3x3* transposed, const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
transposed->row1_col1 = matrix->row1_col1;
|
|
transposed->row2_col2 = matrix->row2_col2;
|
|
transposed->row3_col3 = matrix->row3_col3;
|
|
|
|
const double row1_col2 = matrix->row1_col2;
|
|
const double row1_col3 = matrix->row1_col3;
|
|
const double row2_col3 = matrix->row2_col3;
|
|
|
|
transposed->row1_col2 = matrix->row2_col1;
|
|
transposed->row1_col3 = matrix->row3_col1;
|
|
transposed->row2_col3 = matrix->row3_col2;
|
|
|
|
transposed->row2_col1 = row1_col2;
|
|
transposed->row3_col1 = row1_col3;
|
|
transposed->row3_col2 = row2_col3;
|
|
}
|
|
|
|
// ================== Get Row -================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_get_row(BGC_FP32_Vector3* row, const BGC_FP32_Matrix3x3* matrix, const int row_number)
|
|
{
|
|
if (row_number == 1)
|
|
{
|
|
row->x1 = matrix->row1_col1;
|
|
row->x2 = matrix->row1_col2;
|
|
row->x3 = matrix->row1_col3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 2)
|
|
{
|
|
row->x1 = matrix->row2_col1;
|
|
row->x2 = matrix->row2_col2;
|
|
row->x3 = matrix->row2_col3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 3)
|
|
{
|
|
row->x1 = matrix->row3_col1;
|
|
row->x2 = matrix->row3_col2;
|
|
row->x3 = matrix->row3_col3;
|
|
return;
|
|
}
|
|
|
|
row->x1 = 0.0f;
|
|
row->x2 = 0.0f;
|
|
row->x3 = 0.0f;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_get_row(BGC_FP64_Vector3* row, const BGC_FP64_Matrix3x3* matrix, const int row_number)
|
|
{
|
|
if (row_number == 1)
|
|
{
|
|
row->x1 = matrix->row1_col1;
|
|
row->x2 = matrix->row1_col2;
|
|
row->x3 = matrix->row1_col3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 2)
|
|
{
|
|
row->x1 = matrix->row2_col1;
|
|
row->x2 = matrix->row2_col2;
|
|
row->x3 = matrix->row2_col3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 3)
|
|
{
|
|
row->x1 = matrix->row3_col1;
|
|
row->x2 = matrix->row3_col2;
|
|
row->x3 = matrix->row3_col3;
|
|
return;
|
|
}
|
|
|
|
row->x1 = 0.0;
|
|
row->x2 = 0.0;
|
|
row->x3 = 0.0;
|
|
}
|
|
|
|
// ================== Set Row =================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_set_row(BGC_FP32_Matrix3x3* matrix, const int row_number, const BGC_FP32_Vector3* row)
|
|
{
|
|
if (row_number == 1)
|
|
{
|
|
matrix->row1_col1 = row->x1;
|
|
matrix->row1_col2 = row->x2;
|
|
matrix->row1_col3 = row->x3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 2)
|
|
{
|
|
matrix->row2_col1 = row->x1;
|
|
matrix->row2_col2 = row->x2;
|
|
matrix->row2_col3 = row->x3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 3)
|
|
{
|
|
matrix->row3_col1 = row->x1;
|
|
matrix->row3_col2 = row->x2;
|
|
matrix->row3_col3 = row->x3;
|
|
}
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_set_row(BGC_FP64_Matrix3x3* matrix, const int row_number, const BGC_FP64_Vector3* row)
|
|
{
|
|
if (row_number == 1)
|
|
{
|
|
matrix->row1_col1 = row->x1;
|
|
matrix->row1_col2 = row->x2;
|
|
matrix->row1_col3 = row->x3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 2)
|
|
{
|
|
matrix->row2_col1 = row->x1;
|
|
matrix->row2_col2 = row->x2;
|
|
matrix->row2_col3 = row->x3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 3)
|
|
{
|
|
matrix->row3_col1 = row->x1;
|
|
matrix->row3_col2 = row->x2;
|
|
matrix->row3_col3 = row->x3;
|
|
}
|
|
}
|
|
|
|
// ================= Get Column ================= //
|
|
|
|
inline void bgc_fp32_matrix3x3_get_column(BGC_FP32_Vector3* column, const BGC_FP32_Matrix3x3* matrix, const int column_number)
|
|
{
|
|
if (column_number == 1)
|
|
{
|
|
column->x1 = matrix->row1_col1;
|
|
column->x2 = matrix->row2_col1;
|
|
column->x3 = matrix->row3_col1;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 2)
|
|
{
|
|
column->x1 = matrix->row1_col2;
|
|
column->x2 = matrix->row2_col2;
|
|
column->x3 = matrix->row3_col2;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 3)
|
|
{
|
|
column->x1 = matrix->row1_col3;
|
|
column->x2 = matrix->row2_col3;
|
|
column->x3 = matrix->row3_col3;
|
|
return;
|
|
}
|
|
|
|
column->x1 = 0.0f;
|
|
column->x2 = 0.0f;
|
|
column->x3 = 0.0f;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_get_column(BGC_FP64_Vector3* column, const BGC_FP64_Matrix3x3* matrix, const int column_number)
|
|
{
|
|
if (column_number == 1)
|
|
{
|
|
column->x1 = matrix->row1_col1;
|
|
column->x2 = matrix->row2_col1;
|
|
column->x3 = matrix->row3_col1;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 2)
|
|
{
|
|
column->x1 = matrix->row1_col2;
|
|
column->x2 = matrix->row2_col2;
|
|
column->x3 = matrix->row3_col2;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 3)
|
|
{
|
|
column->x1 = matrix->row1_col3;
|
|
column->x2 = matrix->row2_col3;
|
|
column->x3 = matrix->row3_col3;
|
|
return;
|
|
}
|
|
|
|
column->x1 = 0.0;
|
|
column->x2 = 0.0;
|
|
column->x3 = 0.0;
|
|
}
|
|
|
|
// ================= Set Column ================= //
|
|
|
|
inline void bgc_fp32_matrix3x3_set_column(BGC_FP32_Matrix3x3* matrix, const int column_number, const BGC_FP32_Vector3* column)
|
|
{
|
|
if (column_number == 1)
|
|
{
|
|
matrix->row1_col1 = column->x1;
|
|
matrix->row2_col1 = column->x2;
|
|
matrix->row3_col1 = column->x3;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 2)
|
|
{
|
|
matrix->row1_col2 = column->x1;
|
|
matrix->row2_col2 = column->x2;
|
|
matrix->row3_col2 = column->x3;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 3)
|
|
{
|
|
matrix->row1_col3 = column->x1;
|
|
matrix->row2_col3 = column->x2;
|
|
matrix->row3_col3 = column->x3;
|
|
}
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_set_column(BGC_FP64_Matrix3x3* matrix, const int column_number, const BGC_FP64_Vector3* column)
|
|
{
|
|
if (column_number == 1)
|
|
{
|
|
matrix->row1_col1 = column->x1;
|
|
matrix->row2_col1 = column->x2;
|
|
matrix->row3_col1 = column->x3;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 2)
|
|
{
|
|
matrix->row1_col2 = column->x1;
|
|
matrix->row2_col2 = column->x2;
|
|
matrix->row3_col2 = column->x3;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 3)
|
|
{
|
|
matrix->row1_col3 = column->x1;
|
|
matrix->row2_col3 = column->x2;
|
|
matrix->row3_col3 = column->x3;
|
|
}
|
|
}
|
|
|
|
// ==================== Add ===================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_add(BGC_FP32_Matrix3x3* sum, const BGC_FP32_Matrix3x3* matrix1, const BGC_FP32_Matrix3x3* matrix2)
|
|
{
|
|
sum->row1_col1 = matrix1->row1_col1 + matrix2->row1_col1;
|
|
sum->row1_col2 = matrix1->row1_col2 + matrix2->row1_col2;
|
|
sum->row1_col3 = matrix1->row1_col3 + matrix2->row1_col3;
|
|
|
|
sum->row2_col1 = matrix1->row2_col1 + matrix2->row2_col1;
|
|
sum->row2_col2 = matrix1->row2_col2 + matrix2->row2_col2;
|
|
sum->row2_col3 = matrix1->row2_col3 + matrix2->row2_col3;
|
|
|
|
sum->row3_col1 = matrix1->row3_col1 + matrix2->row3_col1;
|
|
sum->row3_col2 = matrix1->row3_col2 + matrix2->row3_col2;
|
|
sum->row3_col3 = matrix1->row3_col3 + matrix2->row3_col3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_add(BGC_FP64_Matrix3x3* sum, const BGC_FP64_Matrix3x3* matrix1, const BGC_FP64_Matrix3x3* matrix2)
|
|
{
|
|
sum->row1_col1 = matrix1->row1_col1 + matrix2->row1_col1;
|
|
sum->row1_col2 = matrix1->row1_col2 + matrix2->row1_col2;
|
|
sum->row1_col3 = matrix1->row1_col3 + matrix2->row1_col3;
|
|
|
|
sum->row2_col1 = matrix1->row2_col1 + matrix2->row2_col1;
|
|
sum->row2_col2 = matrix1->row2_col2 + matrix2->row2_col2;
|
|
sum->row2_col3 = matrix1->row2_col3 + matrix2->row2_col3;
|
|
|
|
sum->row3_col1 = matrix1->row3_col1 + matrix2->row3_col1;
|
|
sum->row3_col2 = matrix1->row3_col2 + matrix2->row3_col2;
|
|
sum->row3_col3 = matrix1->row3_col3 + matrix2->row3_col3;
|
|
}
|
|
|
|
// ================= Add scaled ================= //
|
|
|
|
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)
|
|
{
|
|
sum->row1_col1 = basic_matrix->row1_col1 + scalable_matrix->row1_col1 * scale;
|
|
sum->row1_col2 = basic_matrix->row1_col2 + scalable_matrix->row1_col2 * scale;
|
|
sum->row1_col3 = basic_matrix->row1_col3 + scalable_matrix->row1_col3 * scale;
|
|
|
|
sum->row2_col1 = basic_matrix->row2_col1 + scalable_matrix->row2_col1 * scale;
|
|
sum->row2_col2 = basic_matrix->row2_col2 + scalable_matrix->row2_col2 * scale;
|
|
sum->row2_col3 = basic_matrix->row2_col3 + scalable_matrix->row2_col3 * scale;
|
|
|
|
sum->row3_col1 = basic_matrix->row3_col1 + scalable_matrix->row3_col1 * scale;
|
|
sum->row3_col2 = basic_matrix->row3_col2 + scalable_matrix->row3_col2 * scale;
|
|
sum->row3_col3 = basic_matrix->row3_col3 + scalable_matrix->row3_col3 * scale;
|
|
}
|
|
|
|
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)
|
|
{
|
|
sum->row1_col1 = basic_matrix->row1_col1 + scalable_matrix->row1_col1 * scale;
|
|
sum->row1_col2 = basic_matrix->row1_col2 + scalable_matrix->row1_col2 * scale;
|
|
sum->row1_col3 = basic_matrix->row1_col3 + scalable_matrix->row1_col3 * scale;
|
|
|
|
sum->row2_col1 = basic_matrix->row2_col1 + scalable_matrix->row2_col1 * scale;
|
|
sum->row2_col2 = basic_matrix->row2_col2 + scalable_matrix->row2_col2 * scale;
|
|
sum->row2_col3 = basic_matrix->row2_col3 + scalable_matrix->row2_col3 * scale;
|
|
|
|
sum->row3_col1 = basic_matrix->row3_col1 + scalable_matrix->row3_col1 * scale;
|
|
sum->row3_col2 = basic_matrix->row3_col2 + scalable_matrix->row3_col2 * scale;
|
|
sum->row3_col3 = basic_matrix->row3_col3 + scalable_matrix->row3_col3 * scale;
|
|
}
|
|
|
|
// ================== Subtract ================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_subtract(BGC_FP32_Matrix3x3* difference, const BGC_FP32_Matrix3x3* minuend, const BGC_FP32_Matrix3x3* subtrahend)
|
|
{
|
|
difference->row1_col1 = minuend->row1_col1 - subtrahend->row1_col1;
|
|
difference->row1_col2 = minuend->row1_col2 - subtrahend->row1_col2;
|
|
difference->row1_col3 = minuend->row1_col3 - subtrahend->row1_col3;
|
|
|
|
difference->row2_col1 = minuend->row2_col1 - subtrahend->row2_col1;
|
|
difference->row2_col2 = minuend->row2_col2 - subtrahend->row2_col2;
|
|
difference->row2_col3 = minuend->row2_col3 - subtrahend->row2_col3;
|
|
|
|
difference->row3_col1 = minuend->row3_col1 - subtrahend->row3_col1;
|
|
difference->row3_col2 = minuend->row3_col2 - subtrahend->row3_col2;
|
|
difference->row3_col3 = minuend->row3_col3 - subtrahend->row3_col3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_subtract(BGC_FP64_Matrix3x3* difference, const BGC_FP64_Matrix3x3* minuend, const BGC_FP64_Matrix3x3* subtrahend)
|
|
{
|
|
difference->row1_col1 = minuend->row1_col1 - subtrahend->row1_col1;
|
|
difference->row1_col2 = minuend->row1_col2 - subtrahend->row1_col2;
|
|
difference->row1_col3 = minuend->row1_col3 - subtrahend->row1_col3;
|
|
|
|
difference->row2_col1 = minuend->row2_col1 - subtrahend->row2_col1;
|
|
difference->row2_col2 = minuend->row2_col2 - subtrahend->row2_col2;
|
|
difference->row2_col3 = minuend->row2_col3 - subtrahend->row2_col3;
|
|
|
|
difference->row3_col1 = minuend->row3_col1 - subtrahend->row3_col1;
|
|
difference->row3_col2 = minuend->row3_col2 - subtrahend->row3_col2;
|
|
difference->row3_col3 = minuend->row3_col3 - subtrahend->row3_col3;
|
|
}
|
|
|
|
// ================== Multiply ================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_multiply(BGC_FP32_Matrix3x3* product, const BGC_FP32_Matrix3x3* multiplicand, const float multiplier)
|
|
{
|
|
product->row1_col1 = multiplicand->row1_col1 * multiplier;
|
|
product->row1_col2 = multiplicand->row1_col2 * multiplier;
|
|
product->row1_col3 = multiplicand->row1_col3 * multiplier;
|
|
|
|
product->row2_col1 = multiplicand->row2_col1 * multiplier;
|
|
product->row2_col2 = multiplicand->row2_col2 * multiplier;
|
|
product->row2_col3 = multiplicand->row2_col3 * multiplier;
|
|
|
|
product->row3_col1 = multiplicand->row3_col1 * multiplier;
|
|
product->row3_col2 = multiplicand->row3_col2 * multiplier;
|
|
product->row3_col3 = multiplicand->row3_col3 * multiplier;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_multiply(BGC_FP64_Matrix3x3* product, const BGC_FP64_Matrix3x3* multiplicand, const double multiplier)
|
|
{
|
|
product->row1_col1 = multiplicand->row1_col1 * multiplier;
|
|
product->row1_col2 = multiplicand->row1_col2 * multiplier;
|
|
product->row1_col3 = multiplicand->row1_col3 * multiplier;
|
|
|
|
product->row2_col1 = multiplicand->row2_col1 * multiplier;
|
|
product->row2_col2 = multiplicand->row2_col2 * multiplier;
|
|
product->row2_col3 = multiplicand->row2_col3 * multiplier;
|
|
|
|
product->row3_col1 = multiplicand->row3_col1 * multiplier;
|
|
product->row3_col2 = multiplicand->row3_col2 * multiplier;
|
|
product->row3_col3 = multiplicand->row3_col3 * multiplier;
|
|
}
|
|
|
|
// =================== Divide =================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_divide(BGC_FP32_Matrix3x3* quotient, const BGC_FP32_Matrix3x3* dividend, const float divisor)
|
|
{
|
|
bgc_fp32_matrix3x3_multiply(quotient, dividend, 1.0f / divisor);
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_divide(BGC_FP64_Matrix3x3* quotient, const BGC_FP64_Matrix3x3* dividend, const double divisor)
|
|
{
|
|
bgc_fp64_matrix3x3_multiply(quotient, dividend, 1.0 / divisor);
|
|
}
|
|
|
|
// ================ Interpolate ================= //
|
|
|
|
inline void bgc_fp32_matrix3x3_interpolate(BGC_FP32_Matrix3x3* interpolation, const BGC_FP32_Matrix3x3* first, const BGC_FP32_Matrix3x3* second, const float phase)
|
|
{
|
|
const float counter_phase = 1.0f - phase;
|
|
|
|
interpolation->row1_col1 = first->row1_col1 * counter_phase + second->row1_col1 * phase;
|
|
interpolation->row1_col2 = first->row1_col2 * counter_phase + second->row1_col2 * phase;
|
|
interpolation->row1_col3 = first->row1_col3 * counter_phase + second->row1_col3 * phase;
|
|
|
|
interpolation->row2_col1 = first->row2_col1 * counter_phase + second->row2_col1 * phase;
|
|
interpolation->row2_col2 = first->row2_col2 * counter_phase + second->row2_col2 * phase;
|
|
interpolation->row2_col3 = first->row2_col3 * counter_phase + second->row2_col3 * phase;
|
|
|
|
interpolation->row3_col1 = first->row3_col1 * counter_phase + second->row3_col1 * phase;
|
|
interpolation->row3_col2 = first->row3_col2 * counter_phase + second->row3_col2 * phase;
|
|
interpolation->row3_col3 = first->row3_col3 * counter_phase + second->row3_col3 * phase;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_interpolate(BGC_FP64_Matrix3x3* interpolation, const BGC_FP64_Matrix3x3* first, const BGC_FP64_Matrix3x3* second, const double phase)
|
|
{
|
|
const double counter_phase = 1.0 - phase;
|
|
|
|
interpolation->row1_col1 = first->row1_col1 * counter_phase + second->row1_col1 * phase;
|
|
interpolation->row1_col2 = first->row1_col2 * counter_phase + second->row1_col2 * phase;
|
|
interpolation->row1_col3 = first->row1_col3 * counter_phase + second->row1_col3 * phase;
|
|
|
|
interpolation->row2_col1 = first->row2_col1 * counter_phase + second->row2_col1 * phase;
|
|
interpolation->row2_col2 = first->row2_col2 * counter_phase + second->row2_col2 * phase;
|
|
interpolation->row2_col3 = first->row2_col3 * counter_phase + second->row2_col3 * phase;
|
|
|
|
interpolation->row3_col1 = first->row3_col1 * counter_phase + second->row3_col1 * phase;
|
|
interpolation->row3_col2 = first->row3_col2 * counter_phase + second->row3_col2 * phase;
|
|
interpolation->row3_col3 = first->row3_col3 * counter_phase + second->row3_col3 * phase;
|
|
}
|
|
|
|
// ============ Left Vector Product ============= //
|
|
|
|
inline void bgc_fp32_multiply_vector3_by_matrix3x3(BGC_FP32_Vector3* product, const BGC_FP32_Vector3* vector, const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
const float x1 = vector->x1 * matrix->row1_col1 + vector->x2 * matrix->row2_col1 + vector->x3 * matrix->row3_col1;
|
|
const float x2 = vector->x1 * matrix->row1_col2 + vector->x2 * matrix->row2_col2 + vector->x3 * matrix->row3_col2;
|
|
const float x3 = vector->x1 * matrix->row1_col3 + vector->x2 * matrix->row2_col3 + vector->x3 * matrix->row3_col3;
|
|
|
|
product->x1 = x1;
|
|
product->x2 = x2;
|
|
product->x3 = x3;
|
|
}
|
|
|
|
inline void bgc_fp64_multiply_vector3_by_matrix3x3(BGC_FP64_Vector3* product, const BGC_FP64_Vector3* vector, const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
const double x1 = vector->x1 * matrix->row1_col1 + vector->x2 * matrix->row2_col1 + vector->x3 * matrix->row3_col1;
|
|
const double x2 = vector->x1 * matrix->row1_col2 + vector->x2 * matrix->row2_col2 + vector->x3 * matrix->row3_col2;
|
|
const double x3 = vector->x1 * matrix->row1_col3 + vector->x2 * matrix->row2_col3 + vector->x3 * matrix->row3_col3;
|
|
|
|
product->x1 = x1;
|
|
product->x2 = x2;
|
|
product->x3 = x3;
|
|
}
|
|
|
|
// ============ Right Vector Product ============ //
|
|
|
|
inline void bgc_fp32_multiply_matrix3x3_by_vector3(BGC_FP32_Vector3* product, const BGC_FP32_Matrix3x3* matrix, const BGC_FP32_Vector3* vector)
|
|
{
|
|
const float x1 = matrix->row1_col1 * vector->x1 + matrix->row1_col2 * vector->x2 + matrix->row1_col3 * vector->x3;
|
|
const float x2 = matrix->row2_col1 * vector->x1 + matrix->row2_col2 * vector->x2 + matrix->row2_col3 * vector->x3;
|
|
const float x3 = matrix->row3_col1 * vector->x1 + matrix->row3_col2 * vector->x2 + matrix->row3_col3 * vector->x3;
|
|
|
|
product->x1 = x1;
|
|
product->x2 = x2;
|
|
product->x3 = x3;
|
|
}
|
|
|
|
inline void bgc_fp64_multiply_matrix3x3_by_vector3(BGC_FP64_Vector3* product, const BGC_FP64_Matrix3x3* matrix, const BGC_FP64_Vector3* vector)
|
|
{
|
|
const double x1 = matrix->row1_col1 * vector->x1 + matrix->row1_col2 * vector->x2 + matrix->row1_col3 * vector->x3;
|
|
const double x2 = matrix->row2_col1 * vector->x1 + matrix->row2_col2 * vector->x2 + matrix->row2_col3 * vector->x3;
|
|
const double x3 = matrix->row3_col1 * vector->x1 + matrix->row3_col2 * vector->x2 + matrix->row3_col3 * vector->x3;
|
|
|
|
product->x1 = x1;
|
|
product->x2 = x2;
|
|
product->x3 = x3;
|
|
}
|
|
|
|
#endif
|