881 lines
28 KiB
C
881 lines
28 KiB
C
#ifndef _BGC_MATRIX3X3_H_INCLUDED_
|
|
#define _BGC_MATRIX3X3_H_INCLUDED_
|
|
|
|
#include "vector3.h"
|
|
#include "matrices.h"
|
|
|
|
// =================== Reset ==================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_reset(BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
matrix->r1c1 = 0.0f;
|
|
matrix->r1c2 = 0.0f;
|
|
matrix->r1c3 = 0.0f;
|
|
|
|
matrix->r2c1 = 0.0f;
|
|
matrix->r2c2 = 0.0f;
|
|
matrix->r2c3 = 0.0f;
|
|
|
|
matrix->r3c1 = 0.0f;
|
|
matrix->r3c2 = 0.0f;
|
|
matrix->r3c3 = 0.0f;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_reset(BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
matrix->r1c1 = 0.0;
|
|
matrix->r1c2 = 0.0;
|
|
matrix->r1c3 = 0.0;
|
|
|
|
matrix->r2c1 = 0.0;
|
|
matrix->r2c2 = 0.0;
|
|
matrix->r2c3 = 0.0;
|
|
|
|
matrix->r3c1 = 0.0;
|
|
matrix->r3c2 = 0.0;
|
|
matrix->r3c3 = 0.0;
|
|
}
|
|
|
|
// ================== Identity ================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_make_identity(BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
matrix->r1c1 = 1.0f;
|
|
matrix->r1c2 = 0.0f;
|
|
matrix->r1c3 = 0.0f;
|
|
|
|
matrix->r2c1 = 0.0f;
|
|
matrix->r2c2 = 1.0f;
|
|
matrix->r2c3 = 0.0f;
|
|
|
|
matrix->r3c1 = 0.0f;
|
|
matrix->r3c2 = 0.0f;
|
|
matrix->r3c3 = 1.0f;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_make_identity(BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
matrix->r1c1 = 1.0;
|
|
matrix->r1c2 = 0.0;
|
|
matrix->r1c3 = 0.0;
|
|
|
|
matrix->r2c1 = 0.0;
|
|
matrix->r2c2 = 1.0;
|
|
matrix->r2c3 = 0.0;
|
|
|
|
matrix->r3c1 = 0.0;
|
|
matrix->r3c2 = 0.0;
|
|
matrix->r3c3 = 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->r1c1 = d1;
|
|
matrix->r1c2 = 0.0f;
|
|
matrix->r1c3 = 0.0f;
|
|
|
|
matrix->r2c1 = 0.0f;
|
|
matrix->r2c2 = d2;
|
|
matrix->r2c3 = 0.0f;
|
|
|
|
matrix->r3c1 = 0.0f;
|
|
matrix->r3c2 = 0.0f;
|
|
matrix->r3c3 = d2;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_make_diagonal(BGC_FP64_Matrix3x3* matrix, const double d1, const double d2, const double d3)
|
|
{
|
|
matrix->r1c1 = d1;
|
|
matrix->r1c2 = 0.0;
|
|
matrix->r1c3 = 0.0;
|
|
|
|
matrix->r2c1 = 0.0;
|
|
matrix->r2c2 = d2;
|
|
matrix->r2c3 = 0.0;
|
|
|
|
matrix->r3c1 = 0.0;
|
|
matrix->r3c2 = 0.0;
|
|
matrix->r3c3 = d2;
|
|
}
|
|
|
|
// ==================== Copy ==================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_copy(BGC_FP32_Matrix3x3* destination, const BGC_FP32_Matrix3x3* source)
|
|
{
|
|
destination->r1c1 = source->r1c1;
|
|
destination->r1c2 = source->r1c2;
|
|
destination->r1c3 = source->r1c3;
|
|
|
|
destination->r2c1 = source->r2c1;
|
|
destination->r2c2 = source->r2c2;
|
|
destination->r2c3 = source->r2c3;
|
|
|
|
destination->r3c1 = source->r3c1;
|
|
destination->r3c2 = source->r3c2;
|
|
destination->r3c3 = source->r3c3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_copy(BGC_FP64_Matrix3x3* destination, const BGC_FP64_Matrix3x3* source)
|
|
{
|
|
destination->r1c1 = source->r1c1;
|
|
destination->r1c2 = source->r1c2;
|
|
destination->r1c3 = source->r1c3;
|
|
|
|
destination->r2c1 = source->r2c1;
|
|
destination->r2c2 = source->r2c2;
|
|
destination->r2c3 = source->r2c3;
|
|
|
|
destination->r3c1 = source->r3c1;
|
|
destination->r3c2 = source->r3c2;
|
|
destination->r3c3 = source->r3c3;
|
|
}
|
|
|
|
// ==================== Swap ==================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_swap(BGC_FP32_Matrix3x3* matrix1, BGC_FP32_Matrix3x3* matrix2)
|
|
{
|
|
const float r1c1 = matrix2->r1c1;
|
|
const float r1c2 = matrix2->r1c2;
|
|
const float r1c3 = matrix2->r1c3;
|
|
|
|
const float r2c1 = matrix2->r2c1;
|
|
const float r2c2 = matrix2->r2c2;
|
|
const float r2c3 = matrix2->r2c3;
|
|
|
|
const float r3c1 = matrix2->r3c1;
|
|
const float r3c2 = matrix2->r3c2;
|
|
const float r3c3 = matrix2->r3c3;
|
|
|
|
matrix2->r1c1 = matrix1->r1c1;
|
|
matrix2->r1c2 = matrix1->r1c2;
|
|
matrix2->r1c3 = matrix1->r1c3;
|
|
|
|
matrix2->r2c1 = matrix1->r2c1;
|
|
matrix2->r2c2 = matrix1->r2c2;
|
|
matrix2->r2c3 = matrix1->r2c3;
|
|
|
|
matrix2->r3c1 = matrix1->r3c1;
|
|
matrix2->r3c2 = matrix1->r3c2;
|
|
matrix2->r3c3 = matrix1->r3c3;
|
|
|
|
matrix1->r1c1 = r1c1;
|
|
matrix1->r1c2 = r1c2;
|
|
matrix1->r1c3 = r1c3;
|
|
|
|
matrix1->r2c1 = r2c1;
|
|
matrix1->r2c2 = r2c2;
|
|
matrix1->r2c3 = r2c3;
|
|
|
|
matrix1->r3c1 = r3c1;
|
|
matrix1->r3c2 = r3c2;
|
|
matrix1->r3c3 = r3c3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_swap(BGC_FP64_Matrix3x3* matrix1, BGC_FP64_Matrix3x3* matrix2)
|
|
{
|
|
const double r1c1 = matrix2->r1c1;
|
|
const double r1c2 = matrix2->r1c2;
|
|
const double r1c3 = matrix2->r1c3;
|
|
|
|
const double r2c1 = matrix2->r2c1;
|
|
const double r2c2 = matrix2->r2c2;
|
|
const double r2c3 = matrix2->r2c3;
|
|
|
|
const double r3c1 = matrix2->r3c1;
|
|
const double r3c2 = matrix2->r3c2;
|
|
const double r3c3 = matrix2->r3c3;
|
|
|
|
matrix2->r1c1 = matrix1->r1c1;
|
|
matrix2->r1c2 = matrix1->r1c2;
|
|
matrix2->r1c3 = matrix1->r1c3;
|
|
|
|
matrix2->r2c1 = matrix1->r2c1;
|
|
matrix2->r2c2 = matrix1->r2c2;
|
|
matrix2->r2c3 = matrix1->r2c3;
|
|
|
|
matrix2->r3c1 = matrix1->r3c1;
|
|
matrix2->r3c2 = matrix1->r3c2;
|
|
matrix2->r3c3 = matrix1->r3c3;
|
|
|
|
matrix1->r1c1 = r1c1;
|
|
matrix1->r1c2 = r1c2;
|
|
matrix1->r1c3 = r1c3;
|
|
|
|
matrix1->r2c1 = r2c1;
|
|
matrix1->r2c2 = r2c2;
|
|
matrix1->r2c3 = r2c3;
|
|
|
|
matrix1->r3c1 = r3c1;
|
|
matrix1->r3c2 = r3c2;
|
|
matrix1->r3c3 = r3c3;
|
|
}
|
|
|
|
// ================== Convert =================== //
|
|
|
|
inline void bgc_fp64_matrix3x3_convert_to_fp32(BGC_FP32_Matrix3x3* destination, const BGC_FP64_Matrix3x3* source)
|
|
{
|
|
destination->r1c1 = (float)source->r1c1;
|
|
destination->r1c2 = (float)source->r1c2;
|
|
destination->r1c3 = (float)source->r1c3;
|
|
|
|
destination->r2c1 = (float)source->r2c1;
|
|
destination->r2c2 = (float)source->r2c2;
|
|
destination->r2c3 = (float)source->r2c3;
|
|
|
|
destination->r3c1 = (float)source->r3c1;
|
|
destination->r3c2 = (float)source->r3c2;
|
|
destination->r3c3 = (float)source->r3c3;
|
|
}
|
|
|
|
inline void bgc_fp32_matrix3x3_convert_to_fp64(BGC_FP64_Matrix3x3* destination, const BGC_FP32_Matrix3x3* source)
|
|
{
|
|
destination->r1c1 = source->r1c1;
|
|
destination->r1c2 = source->r1c2;
|
|
destination->r1c3 = source->r1c3;
|
|
|
|
destination->r2c1 = source->r2c1;
|
|
destination->r2c2 = source->r2c2;
|
|
destination->r2c3 = source->r2c3;
|
|
|
|
destination->r3c1 = source->r3c1;
|
|
destination->r3c2 = source->r3c2;
|
|
destination->r3c3 = source->r3c3;
|
|
}
|
|
|
|
// ================ Determinant ================= //
|
|
|
|
inline float bgc_fp32_matrix3x3_get_determinant(const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
return matrix->r1c1 * (matrix->r2c2 * matrix->r3c3 - matrix->r2c3 * matrix->r3c2)
|
|
+ matrix->r1c2 * (matrix->r2c3 * matrix->r3c1 - matrix->r2c1 * matrix->r3c3)
|
|
+ matrix->r1c3 * (matrix->r2c1 * matrix->r3c2 - matrix->r2c2 * matrix->r3c1);
|
|
}
|
|
|
|
inline double bgc_fp64_matrix3x3_get_determinant(const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
return matrix->r1c1 * (matrix->r2c2 * matrix->r3c3 - matrix->r2c3 * matrix->r3c2)
|
|
+ matrix->r1c2 * (matrix->r2c3 * matrix->r3c1 - matrix->r2c1 * matrix->r3c3)
|
|
+ matrix->r1c3 * (matrix->r2c1 * matrix->r3c2 - matrix->r2c2 * matrix->r3c1);
|
|
}
|
|
|
|
// ================ Is Identity ================= //
|
|
|
|
inline int bgc_fp32_matrix3x3_is_identity(const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
return bgc_fp32_is_unit(matrix->r1c1) && bgc_fp32_is_zero(matrix->r1c2) && bgc_fp32_is_zero(matrix->r1c3)
|
|
&& bgc_fp32_is_zero(matrix->r2c1) && bgc_fp32_is_unit(matrix->r2c2) && bgc_fp32_is_zero(matrix->r2c3)
|
|
&& bgc_fp32_is_zero(matrix->r3c1) && bgc_fp32_is_zero(matrix->r3c2) && bgc_fp32_is_unit(matrix->r3c3);
|
|
}
|
|
|
|
inline int bgc_fp64_matrix3x3_is_identity(const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
return bgc_fp64_is_unit(matrix->r1c1) && bgc_fp64_is_zero(matrix->r1c2) && bgc_fp64_is_zero(matrix->r1c3)
|
|
&& bgc_fp64_is_zero(matrix->r2c1) && bgc_fp64_is_unit(matrix->r2c2) && bgc_fp64_is_zero(matrix->r2c3)
|
|
&& bgc_fp64_is_zero(matrix->r3c1) && bgc_fp64_is_zero(matrix->r3c2) && bgc_fp64_is_unit(matrix->r3c3);
|
|
}
|
|
|
|
// ================ 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.r1c1 = matrix->r1c1 * matrix->r1c1 + matrix->r1c2 * matrix->r2c1 + matrix->r1c3 * matrix->r3c1;
|
|
product.r1c2 = matrix->r1c1 * matrix->r1c2 + matrix->r1c2 * matrix->r2c2 + matrix->r1c3 * matrix->r3c2;
|
|
product.r1c3 = matrix->r1c1 * matrix->r1c3 + matrix->r1c2 * matrix->r2c3 + matrix->r1c3 * matrix->r3c3;
|
|
|
|
product.r2c1 = matrix->r2c1 * matrix->r1c1 + matrix->r2c2 * matrix->r2c1 + matrix->r2c3 * matrix->r3c1;
|
|
product.r2c2 = matrix->r2c1 * matrix->r1c2 + matrix->r2c2 * matrix->r2c2 + matrix->r2c3 * matrix->r3c2;
|
|
product.r2c3 = matrix->r2c1 * matrix->r1c3 + matrix->r2c2 * matrix->r2c3 + matrix->r2c3 * matrix->r3c3;
|
|
|
|
product.r3c1 = matrix->r3c1 * matrix->r1c1 + matrix->r3c2 * matrix->r2c1 + matrix->r3c3 * matrix->r3c1;
|
|
product.r3c2 = matrix->r3c1 * matrix->r1c2 + matrix->r3c2 * matrix->r2c2 + matrix->r3c3 * matrix->r3c2;
|
|
product.r3c3 = matrix->r3c1 * matrix->r1c3 + matrix->r3c2 * matrix->r2c3 + matrix->r3c3 * matrix->r3c3;
|
|
|
|
return bgc_fp32_matrix3x3_is_identity(&product);
|
|
}
|
|
|
|
inline int bgc_fp64_matrix3x3_is_rotation(const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
BGC_FP64_Matrix3x3 product;
|
|
|
|
product.r1c1 = matrix->r1c1 * matrix->r1c1 + matrix->r1c2 * matrix->r2c1 + matrix->r1c3 * matrix->r3c1;
|
|
product.r1c2 = matrix->r1c1 * matrix->r1c2 + matrix->r1c2 * matrix->r2c2 + matrix->r1c3 * matrix->r3c2;
|
|
product.r1c3 = matrix->r1c1 * matrix->r1c3 + matrix->r1c2 * matrix->r2c3 + matrix->r1c3 * matrix->r3c3;
|
|
|
|
product.r2c1 = matrix->r2c1 * matrix->r1c1 + matrix->r2c2 * matrix->r2c1 + matrix->r2c3 * matrix->r3c1;
|
|
product.r2c2 = matrix->r2c1 * matrix->r1c2 + matrix->r2c2 * matrix->r2c2 + matrix->r2c3 * matrix->r3c2;
|
|
product.r2c3 = matrix->r2c1 * matrix->r1c3 + matrix->r2c2 * matrix->r2c3 + matrix->r2c3 * matrix->r3c3;
|
|
|
|
product.r3c1 = matrix->r3c1 * matrix->r1c1 + matrix->r3c2 * matrix->r2c1 + matrix->r3c3 * matrix->r3c1;
|
|
product.r3c2 = matrix->r3c1 * matrix->r1c2 + matrix->r3c2 * matrix->r2c2 + matrix->r3c3 * matrix->r3c2;
|
|
product.r3c3 = matrix->r3c1 * matrix->r1c3 + matrix->r3c2 * matrix->r2c3 + matrix->r3c3 * matrix->r3c3;
|
|
|
|
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 r1c2 = matrix->r1c2;
|
|
const float r1c3 = matrix->r1c3;
|
|
const float r2c3 = matrix->r2c3;
|
|
|
|
matrix->r1c2 = matrix->r2c1;
|
|
matrix->r1c3 = matrix->r3c1;
|
|
matrix->r2c3 = matrix->r3c2;
|
|
|
|
matrix->r2c1 = r1c2;
|
|
matrix->r3c1 = r1c3;
|
|
matrix->r3c2 = r2c3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_transpose(BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
const double r1c2 = matrix->r1c2;
|
|
const double r1c3 = matrix->r1c3;
|
|
const double r2c3 = matrix->r2c3;
|
|
|
|
matrix->r1c2 = matrix->r2c1;
|
|
matrix->r1c3 = matrix->r3c1;
|
|
matrix->r2c3 = matrix->r3c2;
|
|
|
|
matrix->r2c1 = r1c2;
|
|
matrix->r3c1 = r1c3;
|
|
matrix->r3c2 = r2c3;
|
|
}
|
|
|
|
// =============== Get Transpose ================ //
|
|
|
|
inline void bgc_fp32_matrix3x3_get_transposed(BGC_FP32_Matrix3x3* transposed, const BGC_FP32_Matrix3x3* matrix)
|
|
{
|
|
transposed->r1c1 = matrix->r1c1;
|
|
transposed->r2c2 = matrix->r2c2;
|
|
transposed->r3c3 = matrix->r3c3;
|
|
|
|
const float r1c2 = matrix->r1c2;
|
|
const float r1c3 = matrix->r1c3;
|
|
const float r2c3 = matrix->r2c3;
|
|
|
|
transposed->r1c2 = matrix->r2c1;
|
|
transposed->r1c3 = matrix->r3c1;
|
|
transposed->r2c3 = matrix->r3c2;
|
|
|
|
transposed->r2c1 = r1c2;
|
|
transposed->r3c1 = r1c3;
|
|
transposed->r3c2 = r2c3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_get_transposed(BGC_FP64_Matrix3x3* transposed, const BGC_FP64_Matrix3x3* matrix)
|
|
{
|
|
transposed->r1c1 = matrix->r1c1;
|
|
transposed->r2c2 = matrix->r2c2;
|
|
transposed->r3c3 = matrix->r3c3;
|
|
|
|
const double r1c2 = matrix->r1c2;
|
|
const double r1c3 = matrix->r1c3;
|
|
const double r2c3 = matrix->r2c3;
|
|
|
|
transposed->r1c2 = matrix->r2c1;
|
|
transposed->r1c3 = matrix->r3c1;
|
|
transposed->r2c3 = matrix->r3c2;
|
|
|
|
transposed->r2c1 = r1c2;
|
|
transposed->r3c1 = r1c3;
|
|
transposed->r3c2 = r2c3;
|
|
}
|
|
|
|
// ================== 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->r1c1;
|
|
row->x2 = matrix->r1c2;
|
|
row->x3 = matrix->r1c3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 2)
|
|
{
|
|
row->x1 = matrix->r2c1;
|
|
row->x2 = matrix->r2c2;
|
|
row->x3 = matrix->r2c3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 3)
|
|
{
|
|
row->x1 = matrix->r3c1;
|
|
row->x2 = matrix->r3c2;
|
|
row->x3 = matrix->r3c3;
|
|
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->r1c1;
|
|
row->x2 = matrix->r1c2;
|
|
row->x3 = matrix->r1c3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 2)
|
|
{
|
|
row->x1 = matrix->r2c1;
|
|
row->x2 = matrix->r2c2;
|
|
row->x3 = matrix->r2c3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 3)
|
|
{
|
|
row->x1 = matrix->r3c1;
|
|
row->x2 = matrix->r3c2;
|
|
row->x3 = matrix->r3c3;
|
|
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->r1c1 = row->x1;
|
|
matrix->r1c2 = row->x2;
|
|
matrix->r1c3 = row->x3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 2)
|
|
{
|
|
matrix->r2c1 = row->x1;
|
|
matrix->r2c2 = row->x2;
|
|
matrix->r2c3 = row->x3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 3)
|
|
{
|
|
matrix->r3c1 = row->x1;
|
|
matrix->r3c2 = row->x2;
|
|
matrix->r3c3 = 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->r1c1 = row->x1;
|
|
matrix->r1c2 = row->x2;
|
|
matrix->r1c3 = row->x3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 2)
|
|
{
|
|
matrix->r2c1 = row->x1;
|
|
matrix->r2c2 = row->x2;
|
|
matrix->r2c3 = row->x3;
|
|
return;
|
|
}
|
|
|
|
if (row_number == 3)
|
|
{
|
|
matrix->r3c1 = row->x1;
|
|
matrix->r3c2 = row->x2;
|
|
matrix->r3c3 = 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->r1c1;
|
|
column->x2 = matrix->r2c1;
|
|
column->x3 = matrix->r3c1;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 2)
|
|
{
|
|
column->x1 = matrix->r1c2;
|
|
column->x2 = matrix->r2c2;
|
|
column->x3 = matrix->r3c2;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 3)
|
|
{
|
|
column->x1 = matrix->r1c3;
|
|
column->x2 = matrix->r2c3;
|
|
column->x3 = matrix->r3c3;
|
|
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->r1c1;
|
|
column->x2 = matrix->r2c1;
|
|
column->x3 = matrix->r3c1;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 2)
|
|
{
|
|
column->x1 = matrix->r1c2;
|
|
column->x2 = matrix->r2c2;
|
|
column->x3 = matrix->r3c2;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 3)
|
|
{
|
|
column->x1 = matrix->r1c3;
|
|
column->x2 = matrix->r2c3;
|
|
column->x3 = matrix->r3c3;
|
|
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->r1c1 = column->x1;
|
|
matrix->r2c1 = column->x2;
|
|
matrix->r3c1 = column->x3;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 2)
|
|
{
|
|
matrix->r1c2 = column->x1;
|
|
matrix->r2c2 = column->x2;
|
|
matrix->r3c2 = column->x3;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 3)
|
|
{
|
|
matrix->r1c3 = column->x1;
|
|
matrix->r2c3 = column->x2;
|
|
matrix->r3c3 = 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->r1c1 = column->x1;
|
|
matrix->r2c1 = column->x2;
|
|
matrix->r3c1 = column->x3;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 2)
|
|
{
|
|
matrix->r1c2 = column->x1;
|
|
matrix->r2c2 = column->x2;
|
|
matrix->r3c2 = column->x3;
|
|
return;
|
|
}
|
|
|
|
if (column_number == 3)
|
|
{
|
|
matrix->r1c3 = column->x1;
|
|
matrix->r2c3 = column->x2;
|
|
matrix->r3c3 = column->x3;
|
|
}
|
|
}
|
|
|
|
// ==================== Add ===================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_add(BGC_FP32_Matrix3x3* sum, const BGC_FP32_Matrix3x3* matrix1, const BGC_FP32_Matrix3x3* matrix2)
|
|
{
|
|
sum->r1c1 = matrix1->r1c1 + matrix2->r1c1;
|
|
sum->r1c2 = matrix1->r1c2 + matrix2->r1c2;
|
|
sum->r1c3 = matrix1->r1c3 + matrix2->r1c3;
|
|
|
|
sum->r2c1 = matrix1->r2c1 + matrix2->r2c1;
|
|
sum->r2c2 = matrix1->r2c2 + matrix2->r2c2;
|
|
sum->r2c3 = matrix1->r2c3 + matrix2->r2c3;
|
|
|
|
sum->r3c1 = matrix1->r3c1 + matrix2->r3c1;
|
|
sum->r3c2 = matrix1->r3c2 + matrix2->r3c2;
|
|
sum->r3c3 = matrix1->r3c3 + matrix2->r3c3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_add(BGC_FP64_Matrix3x3* sum, const BGC_FP64_Matrix3x3* matrix1, const BGC_FP64_Matrix3x3* matrix2)
|
|
{
|
|
sum->r1c1 = matrix1->r1c1 + matrix2->r1c1;
|
|
sum->r1c2 = matrix1->r1c2 + matrix2->r1c2;
|
|
sum->r1c3 = matrix1->r1c3 + matrix2->r1c3;
|
|
|
|
sum->r2c1 = matrix1->r2c1 + matrix2->r2c1;
|
|
sum->r2c2 = matrix1->r2c2 + matrix2->r2c2;
|
|
sum->r2c3 = matrix1->r2c3 + matrix2->r2c3;
|
|
|
|
sum->r3c1 = matrix1->r3c1 + matrix2->r3c1;
|
|
sum->r3c2 = matrix1->r3c2 + matrix2->r3c2;
|
|
sum->r3c3 = matrix1->r3c3 + matrix2->r3c3;
|
|
}
|
|
|
|
// ================= 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->r1c1 = basic_matrix->r1c1 + scalable_matrix->r1c1 * scale;
|
|
sum->r1c2 = basic_matrix->r1c2 + scalable_matrix->r1c2 * scale;
|
|
sum->r1c3 = basic_matrix->r1c3 + scalable_matrix->r1c3 * scale;
|
|
|
|
sum->r2c1 = basic_matrix->r2c1 + scalable_matrix->r2c1 * scale;
|
|
sum->r2c2 = basic_matrix->r2c2 + scalable_matrix->r2c2 * scale;
|
|
sum->r2c3 = basic_matrix->r2c3 + scalable_matrix->r2c3 * scale;
|
|
|
|
sum->r3c1 = basic_matrix->r3c1 + scalable_matrix->r3c1 * scale;
|
|
sum->r3c2 = basic_matrix->r3c2 + scalable_matrix->r3c2 * scale;
|
|
sum->r3c3 = basic_matrix->r3c3 + scalable_matrix->r3c3 * 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->r1c1 = basic_matrix->r1c1 + scalable_matrix->r1c1 * scale;
|
|
sum->r1c2 = basic_matrix->r1c2 + scalable_matrix->r1c2 * scale;
|
|
sum->r1c3 = basic_matrix->r1c3 + scalable_matrix->r1c3 * scale;
|
|
|
|
sum->r2c1 = basic_matrix->r2c1 + scalable_matrix->r2c1 * scale;
|
|
sum->r2c2 = basic_matrix->r2c2 + scalable_matrix->r2c2 * scale;
|
|
sum->r2c3 = basic_matrix->r2c3 + scalable_matrix->r2c3 * scale;
|
|
|
|
sum->r3c1 = basic_matrix->r3c1 + scalable_matrix->r3c1 * scale;
|
|
sum->r3c2 = basic_matrix->r3c2 + scalable_matrix->r3c2 * scale;
|
|
sum->r3c3 = basic_matrix->r3c3 + scalable_matrix->r3c3 * scale;
|
|
}
|
|
|
|
// ================== Subtract ================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_subtract(BGC_FP32_Matrix3x3* difference, const BGC_FP32_Matrix3x3* minuend, const BGC_FP32_Matrix3x3* subtrahend)
|
|
{
|
|
difference->r1c1 = minuend->r1c1 - subtrahend->r1c1;
|
|
difference->r1c2 = minuend->r1c2 - subtrahend->r1c2;
|
|
difference->r1c3 = minuend->r1c3 - subtrahend->r1c3;
|
|
|
|
difference->r2c1 = minuend->r2c1 - subtrahend->r2c1;
|
|
difference->r2c2 = minuend->r2c2 - subtrahend->r2c2;
|
|
difference->r2c3 = minuend->r2c3 - subtrahend->r2c3;
|
|
|
|
difference->r3c1 = minuend->r3c1 - subtrahend->r3c1;
|
|
difference->r3c2 = minuend->r3c2 - subtrahend->r3c2;
|
|
difference->r3c3 = minuend->r3c3 - subtrahend->r3c3;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_subtract(BGC_FP64_Matrix3x3* difference, const BGC_FP64_Matrix3x3* minuend, const BGC_FP64_Matrix3x3* subtrahend)
|
|
{
|
|
difference->r1c1 = minuend->r1c1 - subtrahend->r1c1;
|
|
difference->r1c2 = minuend->r1c2 - subtrahend->r1c2;
|
|
difference->r1c3 = minuend->r1c3 - subtrahend->r1c3;
|
|
|
|
difference->r2c1 = minuend->r2c1 - subtrahend->r2c1;
|
|
difference->r2c2 = minuend->r2c2 - subtrahend->r2c2;
|
|
difference->r2c3 = minuend->r2c3 - subtrahend->r2c3;
|
|
|
|
difference->r3c1 = minuend->r3c1 - subtrahend->r3c1;
|
|
difference->r3c2 = minuend->r3c2 - subtrahend->r3c2;
|
|
difference->r3c3 = minuend->r3c3 - subtrahend->r3c3;
|
|
}
|
|
|
|
// ================== Multiply ================== //
|
|
|
|
inline void bgc_fp32_matrix3x3_multiply(BGC_FP32_Matrix3x3* product, const BGC_FP32_Matrix3x3* multiplicand, const float multiplier)
|
|
{
|
|
product->r1c1 = multiplicand->r1c1 * multiplier;
|
|
product->r1c2 = multiplicand->r1c2 * multiplier;
|
|
product->r1c3 = multiplicand->r1c3 * multiplier;
|
|
|
|
product->r2c1 = multiplicand->r2c1 * multiplier;
|
|
product->r2c2 = multiplicand->r2c2 * multiplier;
|
|
product->r2c3 = multiplicand->r2c3 * multiplier;
|
|
|
|
product->r3c1 = multiplicand->r3c1 * multiplier;
|
|
product->r3c2 = multiplicand->r3c2 * multiplier;
|
|
product->r3c3 = multiplicand->r3c3 * multiplier;
|
|
}
|
|
|
|
inline void bgc_fp64_matrix3x3_multiply(BGC_FP64_Matrix3x3* product, const BGC_FP64_Matrix3x3* multiplicand, const double multiplier)
|
|
{
|
|
product->r1c1 = multiplicand->r1c1 * multiplier;
|
|
product->r1c2 = multiplicand->r1c2 * multiplier;
|
|
product->r1c3 = multiplicand->r1c3 * multiplier;
|
|
|
|
product->r2c1 = multiplicand->r2c1 * multiplier;
|
|
product->r2c2 = multiplicand->r2c2 * multiplier;
|
|
product->r2c3 = multiplicand->r2c3 * multiplier;
|
|
|
|
product->r3c1 = multiplicand->r3c1 * multiplier;
|
|
product->r3c2 = multiplicand->r3c2 * multiplier;
|
|
product->r3c3 = multiplicand->r3c3 * 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->r1c1 = first->r1c1 * counter_phase + second->r1c1 * phase;
|
|
interpolation->r1c2 = first->r1c2 * counter_phase + second->r1c2 * phase;
|
|
interpolation->r1c3 = first->r1c3 * counter_phase + second->r1c3 * phase;
|
|
|
|
interpolation->r2c1 = first->r2c1 * counter_phase + second->r2c1 * phase;
|
|
interpolation->r2c2 = first->r2c2 * counter_phase + second->r2c2 * phase;
|
|
interpolation->r2c3 = first->r2c3 * counter_phase + second->r2c3 * phase;
|
|
|
|
interpolation->r3c1 = first->r3c1 * counter_phase + second->r3c1 * phase;
|
|
interpolation->r3c2 = first->r3c2 * counter_phase + second->r3c2 * phase;
|
|
interpolation->r3c3 = first->r3c3 * counter_phase + second->r3c3 * 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->r1c1 = first->r1c1 * counter_phase + second->r1c1 * phase;
|
|
interpolation->r1c2 = first->r1c2 * counter_phase + second->r1c2 * phase;
|
|
interpolation->r1c3 = first->r1c3 * counter_phase + second->r1c3 * phase;
|
|
|
|
interpolation->r2c1 = first->r2c1 * counter_phase + second->r2c1 * phase;
|
|
interpolation->r2c2 = first->r2c2 * counter_phase + second->r2c2 * phase;
|
|
interpolation->r2c3 = first->r2c3 * counter_phase + second->r2c3 * phase;
|
|
|
|
interpolation->r3c1 = first->r3c1 * counter_phase + second->r3c1 * phase;
|
|
interpolation->r3c2 = first->r3c2 * counter_phase + second->r3c2 * phase;
|
|
interpolation->r3c3 = first->r3c3 * counter_phase + second->r3c3 * 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->r1c1 + vector->x2 * matrix->r2c1 + vector->x3 * matrix->r3c1;
|
|
const float x2 = vector->x1 * matrix->r1c2 + vector->x2 * matrix->r2c2 + vector->x3 * matrix->r3c2;
|
|
const float x3 = vector->x1 * matrix->r1c3 + vector->x2 * matrix->r2c3 + vector->x3 * matrix->r3c3;
|
|
|
|
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->r1c1 + vector->x2 * matrix->r2c1 + vector->x3 * matrix->r3c1;
|
|
const double x2 = vector->x1 * matrix->r1c2 + vector->x2 * matrix->r2c2 + vector->x3 * matrix->r3c2;
|
|
const double x3 = vector->x1 * matrix->r1c3 + vector->x2 * matrix->r2c3 + vector->x3 * matrix->r3c3;
|
|
|
|
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->r1c1 * vector->x1 + matrix->r1c2 * vector->x2 + matrix->r1c3 * vector->x3;
|
|
const float x2 = matrix->r2c1 * vector->x1 + matrix->r2c2 * vector->x2 + matrix->r2c3 * vector->x3;
|
|
const float x3 = matrix->r3c1 * vector->x1 + matrix->r3c2 * vector->x2 + matrix->r3c3 * 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->r1c1 * vector->x1 + matrix->r1c2 * vector->x2 + matrix->r1c3 * vector->x3;
|
|
const double x2 = matrix->r2c1 * vector->x1 + matrix->r2c2 * vector->x2 + matrix->r2c3 * vector->x3;
|
|
const double x3 = matrix->r3c1 * vector->x1 + matrix->r3c2 * vector->x2 + matrix->r3c3 * vector->x3;
|
|
|
|
product->x1 = x1;
|
|
product->x2 = x2;
|
|
product->x3 = x3;
|
|
}
|
|
|
|
#endif
|