#ifndef _BGC_MATRIX2X2_H_ #define _BGC_MATRIX2X2_H_ #include "angle.h" #include "vector2.h" #include "matrixes.h" // =================== Reset ==================== // inline void bgc_fp32_matrix2x2_reset(BGC_FP32_Matrix2x2* matrix) { matrix->r1c1 = 0.0f; matrix->r1c2 = 0.0f; matrix->r2c1 = 0.0f; matrix->r2c2 = 0.0f; } inline void bgc_fp64_matrix2x2_reset(BGC_FP64_Matrix2x2* matrix) { matrix->r1c1 = 0.0; matrix->r1c2 = 0.0; matrix->r2c1 = 0.0; matrix->r2c2 = 0.0; } // ================== Identity ================== // inline void bgc_fp32_matrix2x2_make_identity(BGC_FP32_Matrix2x2* matrix) { matrix->r1c1 = 1.0f; matrix->r1c2 = 0.0f; matrix->r2c1 = 0.0f; matrix->r2c2 = 1.0f; } inline void bgc_fp64_matrix2x2_make_identity(BGC_FP64_Matrix2x2* matrix) { matrix->r1c1 = 1.0; matrix->r1c2 = 0.0; matrix->r2c1 = 0.0; matrix->r2c2 = 1.0; } // ================ Set Diagonal ================ // inline void bgc_fp32_matrix2x2_make_diagonal(const float d1, const float d2, BGC_FP32_Matrix2x2* matrix) { matrix->r1c1 = d1; matrix->r1c2 = 0.0f; matrix->r2c1 = 0.0f; matrix->r2c2 = d2; } inline void bgc_fp64_matrix2x2_make_diagonal(const double d1, const double d2, BGC_FP64_Matrix2x2* matrix) { matrix->r1c1 = d1; matrix->r1c2 = 0.0; matrix->r2c1 = 0.0; matrix->r2c2 = d2; } // ============== Rotation Matrix =============== // inline void bgc_fp32_matrix2x2_make_for_turn(const float angle, const int angle_unit, BGC_FP32_Matrix2x2* matrix) { const float radians = bgc_fp32_angle_to_radians(angle, angle_unit); const float cosine = cosf(radians); const float sine = sinf(radians); matrix->r1c1 = cosine; matrix->r1c2 = -sine; matrix->r2c1 = sine; matrix->r2c2 = cosine; } inline void bgc_fp64_matrix2x2_make_for_turn(const double angle, const int angle_unit, BGC_FP64_Matrix2x2* matrix) { const double radians = bgc_fp64_angle_to_radians(angle, angle_unit); const double cosine = cos(radians); const double sine = sin(radians); matrix->r1c1 = cosine; matrix->r1c2 = -sine; matrix->r2c1 = sine; matrix->r2c2 = cosine; } // ================ Determinant ================= // inline float bgc_fp32_matrix2x2_get_determinant(const BGC_FP32_Matrix2x2* matrix) { return matrix->r1c1 * matrix->r2c2 - matrix->r1c2 * matrix->r2c1; } inline double bgc_fp64_matrix2x2_get_determinant(const BGC_FP64_Matrix2x2* matrix) { return matrix->r1c1 * matrix->r2c2 - matrix->r1c2 * matrix->r2c1; } // ================ Is Identity ================= // inline int bgc_fp32_matrix2x2_is_identity(const BGC_FP32_Matrix2x2* matrix) { return bgc_fp32_is_unit(matrix->r1c1) && bgc_fp32_is_zero(matrix->r1c2) && bgc_fp32_is_zero(matrix->r2c1) && bgc_fp32_is_unit(matrix->r2c2); } inline int bgc_fp64_matrix2x2_is_identity(const BGC_FP64_Matrix2x2* matrix) { return bgc_fp64_is_unit(matrix->r1c1) && bgc_fp64_is_zero(matrix->r1c2) && bgc_fp64_is_zero(matrix->r2c1) && bgc_fp64_is_unit(matrix->r2c2); } // ================ Is Singular ================= // inline int bgc_fp32_matrix2x2_is_singular(const BGC_FP32_Matrix2x2* matrix) { return bgc_fp32_is_zero(bgc_fp32_matrix2x2_get_determinant(matrix)); } inline int bgc_fp64_matrix2x2_is_singular(const BGC_FP64_Matrix2x2* matrix) { return bgc_fp64_is_zero(bgc_fp64_matrix2x2_get_determinant(matrix)); } // ================ Is Rotation ================= // inline int bgc_fp32_matrix2x2_is_rotation(const BGC_FP32_Matrix2x2* matrix) { BGC_FP32_Matrix2x2 product; product.r1c1 = matrix->r1c1 * matrix->r1c1 + matrix->r1c2 * matrix->r2c1; product.r1c2 = matrix->r1c1 * matrix->r1c2 + matrix->r1c2 * matrix->r2c2; product.r2c1 = matrix->r2c1 * matrix->r1c1 + matrix->r2c2 * matrix->r2c1; product.r2c2 = matrix->r2c1 * matrix->r1c2 + matrix->r2c2 * matrix->r2c2; return bgc_fp32_matrix2x2_is_identity(&product); } inline int bgc_fp64_matrix2x2_is_rotation(const BGC_FP64_Matrix2x2* matrix) { BGC_FP64_Matrix2x2 product; product.r1c1 = matrix->r1c1 * matrix->r1c1 + matrix->r1c2 * matrix->r2c1; product.r1c2 = matrix->r1c1 * matrix->r1c2 + matrix->r1c2 * matrix->r2c2; product.r2c1 = matrix->r2c1 * matrix->r1c1 + matrix->r2c2 * matrix->r2c1; product.r2c2 = matrix->r2c1 * matrix->r1c2 + matrix->r2c2 * matrix->r2c2; return bgc_fp64_matrix2x2_is_identity(&product); } // ==================== Copy ==================== // inline void bgc_fp32_matrix2x2_copy(const BGC_FP32_Matrix2x2* source, BGC_FP32_Matrix2x2* destination) { destination->r1c1 = source->r1c1; destination->r1c2 = source->r1c2; destination->r2c1 = source->r2c1; destination->r2c2 = source->r2c2; } inline void bgc_fp64_matrix2x2_copy(const BGC_FP64_Matrix2x2* source, BGC_FP64_Matrix2x2* destination) { destination->r1c1 = source->r1c1; destination->r1c2 = source->r1c2; destination->r2c1 = source->r2c1; destination->r2c2 = source->r2c2; } // ==================== Swap ==================== // inline void bgc_fp32_matrix2x2_swap(BGC_FP32_Matrix2x2* matrix1, BGC_FP32_Matrix2x2* matrix2) { const float r1c1 = matrix2->r1c1; const float r1c2 = matrix2->r1c2; const float r2c1 = matrix2->r2c1; const float r2c2 = matrix2->r2c2; matrix2->r1c1 = matrix1->r1c1; matrix2->r1c2 = matrix1->r1c2; matrix2->r2c1 = matrix1->r2c1; matrix2->r2c2 = matrix1->r2c2; matrix1->r1c1 = r1c1; matrix1->r1c2 = r1c2; matrix1->r2c1 = r2c1; matrix1->r2c2 = r2c2; } inline void bgc_fp64_matrix2x2_swap(BGC_FP64_Matrix2x2* matrix1, BGC_FP64_Matrix2x2* matrix2) { const double r1c1 = matrix2->r1c1; const double r1c2 = matrix2->r1c2; const double r2c1 = matrix2->r2c1; const double r2c2 = matrix2->r2c2; matrix2->r1c1 = matrix1->r1c1; matrix2->r1c2 = matrix1->r1c2; matrix2->r2c1 = matrix1->r2c1; matrix2->r2c2 = matrix1->r2c2; matrix1->r1c1 = r1c1; matrix1->r1c2 = r1c2; matrix1->r2c1 = r2c1; matrix1->r2c2 = r2c2; } // ================== Convert =================== // inline void bgc_fp64_matrix2x2_convert_to_fp32(const BGC_FP64_Matrix2x2* source, BGC_FP32_Matrix2x2* destination) { destination->r1c1 = (float)source->r1c1; destination->r1c2 = (float)source->r1c2; destination->r2c1 = (float)source->r2c1; destination->r2c2 = (float)source->r2c2; } inline void bgc_fp32_matrix2x2_convert_to_fp64(const BGC_FP32_Matrix2x2* source, BGC_FP64_Matrix2x2* destination) { destination->r1c1 = source->r1c1; destination->r1c2 = source->r1c2; destination->r2c1 = source->r2c1; destination->r2c2 = source->r2c2; } // ================ Get Inverse ================= // inline int bgc_fp32_matrix2x2_get_inverse(const BGC_FP32_Matrix2x2* matrix, BGC_FP32_Matrix2x2* inverse) { const float determinant = bgc_fp32_matrix2x2_get_determinant(matrix); if (bgc_fp32_is_zero(determinant)) { return 0; } const float r1c1 = matrix->r2c2; const float r1c2 = -matrix->r1c2; const float r2c1 = -matrix->r2c1; const float r2c2 = matrix->r1c1; const float multiplier = 1.0f / determinant; inverse->r1c1 = r1c1 * multiplier; inverse->r1c2 = r1c2 * multiplier; inverse->r2c1 = r2c1 * multiplier; inverse->r2c2 = r2c2 * multiplier; return 1; } inline int bgc_fp64_matrix2x2_get_inverse(const BGC_FP64_Matrix2x2* matrix, BGC_FP64_Matrix2x2* inverse) { const double determinant = bgc_fp64_matrix2x2_get_determinant(matrix); if (bgc_fp64_is_zero(determinant)) { return 0; } const double r1c1 = matrix->r2c2; const double r1c2 = -matrix->r1c2; const double r2c1 = -matrix->r2c1; const double r2c2 = matrix->r1c1; const double multiplier = 1.0 / determinant; inverse->r1c1 = r1c1 * multiplier; inverse->r1c2 = r1c2 * multiplier; inverse->r2c1 = r2c1 * multiplier; inverse->r2c2 = r2c2 * multiplier; return 1; } // =================== Invert =================== // inline int bgc_fp32_matrix2x2_invert(BGC_FP32_Matrix2x2* matrix) { return bgc_fp32_matrix2x2_get_inverse(matrix, matrix); } inline int bgc_fp64_matrix2x2_invert(BGC_FP64_Matrix2x2* matrix) { return bgc_fp64_matrix2x2_get_inverse(matrix, matrix); } // ================= Transpose ================== // inline void bgc_fp32_matrix2x2_transpose(BGC_FP32_Matrix2x2* matrix) { const float r1c2 = matrix->r1c2; matrix->r1c2 = matrix->r2c1; matrix->r2c1 = r1c2; } inline void bgc_fp64_matrix2x2_transpose(BGC_FP64_Matrix2x2* matrix) { const double r1c2 = matrix->r1c2; matrix->r1c2 = matrix->r2c1; matrix->r2c1 = r1c2; } // =============== Get Transpose ================ // inline void bgc_fp32_matrix2x2_get_transposed(const BGC_FP32_Matrix2x2* matrix, BGC_FP32_Matrix2x2* transposed) { const float r1c2 = matrix->r1c2; transposed->r1c1 = matrix->r1c1; transposed->r1c2 = matrix->r2c1; transposed->r2c1 = r1c2; transposed->r2c2 = matrix->r2c2; } inline void bgc_fp64_matrix2x2_get_transposed(const BGC_FP64_Matrix2x2* matrix, BGC_FP64_Matrix2x2* transposed) { const double r1c2 = matrix->r1c2; transposed->r1c1 = matrix->r1c1; transposed->r1c2 = matrix->r2c1; transposed->r2c1 = r1c2; transposed->r2c2 = matrix->r2c2; } // ================== Get Row =================== // inline void bgc_fp32_matrix2x2_get_row(const int number, const BGC_FP32_Matrix2x2* matrix, BGC_FP32_Vector2* row) { if (number == 1) { row->x1 = matrix->r1c1; row->x2 = matrix->r1c2; return; } if (number == 2) { row->x1 = matrix->r2c1; row->x2 = matrix->r2c2; return; } row->x1 = 0.0f; row->x2 = 0.0f; } inline void bgc_fp64_matrix2x2_get_row(const int number, const BGC_FP64_Matrix2x2* matrix, BGC_FP64_Vector2* row) { if (number == 1) { row->x1 = matrix->r1c1; row->x2 = matrix->r1c2; return; } if (number == 2) { row->x1 = matrix->r2c1; row->x2 = matrix->r2c2; return; } row->x1 = 0.0; row->x2 = 0.0; } // ================== Set Row =================== // inline void bgc_fp32_matrix2x2_set_row(const int number, const BGC_FP32_Vector2* row, BGC_FP32_Matrix2x2* matrix) { if (number == 1) { matrix->r1c1 = row->x1; matrix->r1c2 = row->x2; return; } if (number == 2) { matrix->r2c1 = row->x1; matrix->r2c2 = row->x2; } } inline void bgc_fp64_matrix2x2_set_row(const int number, const BGC_FP64_Vector2* row, BGC_FP64_Matrix2x2* matrix) { if (number == 1) { matrix->r1c1 = row->x1; matrix->r1c2 = row->x2; return; } if (number == 2) { matrix->r2c1 = row->x1; matrix->r2c2 = row->x2; } } // ================= Get Column ================= // inline void bgc_fp32_matrix2x2_get_column(const int number, const BGC_FP32_Matrix2x2* matrix, BGC_FP32_Vector2* column) { if (number == 1) { column->x1 = matrix->r1c1; column->x2 = matrix->r2c1; return; } if (number == 2) { column->x1 = matrix->r1c2; column->x2 = matrix->r2c2; return; } column->x1 = 0.0f; column->x2 = 0.0f; } inline void bgc_fp64_matrix2x2_get_column(const int number, const BGC_FP64_Matrix2x2* matrix, BGC_FP64_Vector2* column) { if (number == 1) { column->x1 = matrix->r1c1; column->x2 = matrix->r2c1; return; } if (number == 2) { column->x1 = matrix->r1c2; column->x2 = matrix->r2c2; return; } column->x1 = 0.0; column->x2 = 0.0; } // ================= Set Column ================= // inline void bgc_fp32_matrix2x2_set_column(const int number, const BGC_FP32_Vector2* column, BGC_FP32_Matrix2x2* matrix) { if (number == 1) { matrix->r1c1 = column->x1; matrix->r2c1 = column->x2; return; } if (number == 2) { matrix->r1c2 = column->x1; matrix->r2c2 = column->x2; } } inline void bgc_fp64_matrix2x2_set_column(const int number, const BGC_FP64_Vector2* column, BGC_FP64_Matrix2x2* matrix) { if (number == 1) { matrix->r1c1 = column->x1; matrix->r2c1 = column->x2; return; } if (number == 2) { matrix->r1c2 = column->x1; matrix->r2c2 = column->x2; } } // ==================== Add ===================== // inline void bgc_fp32_matrix2x2_add(const BGC_FP32_Matrix2x2* matrix1, const BGC_FP32_Matrix2x2* matrix2, BGC_FP32_Matrix2x2* sum) { sum->r1c1 = matrix1->r1c1 + matrix2->r1c1; sum->r1c2 = matrix1->r1c2 + matrix2->r1c2; sum->r2c1 = matrix1->r2c1 + matrix2->r2c1; sum->r2c2 = matrix1->r2c2 + matrix2->r2c2; } inline void bgc_fp64_matrix2x2_add(const BGC_FP64_Matrix2x2* matrix1, const BGC_FP64_Matrix2x2* matrix2, BGC_FP64_Matrix2x2* sum) { sum->r1c1 = matrix1->r1c1 + matrix2->r1c1; sum->r1c2 = matrix1->r1c2 + matrix2->r1c2; sum->r2c1 = matrix1->r2c1 + matrix2->r2c1; sum->r2c2 = matrix1->r2c2 + matrix2->r2c2; } // ================= Add scaled ================= // inline void bgc_fp32_matrix2x2_add_scaled(const BGC_FP32_Matrix2x2* basic_matrix, const BGC_FP32_Matrix2x2* scalable_matrix, const float scale, BGC_FP32_Matrix2x2* sum) { sum->r1c1 = basic_matrix->r1c1 + scalable_matrix->r1c1 * scale; sum->r1c2 = basic_matrix->r1c2 + scalable_matrix->r1c2 * scale; sum->r2c1 = basic_matrix->r2c1 + scalable_matrix->r2c1 * scale; sum->r2c2 = basic_matrix->r2c2 + scalable_matrix->r2c2 * scale; } inline void bgc_fp64_matrix2x2_add_scaled(const BGC_FP64_Matrix2x2* basic_matrix, const BGC_FP64_Matrix2x2* scalable_matrix, const double scale, BGC_FP64_Matrix2x2* sum) { sum->r1c1 = basic_matrix->r1c1 + scalable_matrix->r1c1 * scale; sum->r1c2 = basic_matrix->r1c2 + scalable_matrix->r1c2 * scale; sum->r2c1 = basic_matrix->r2c1 + scalable_matrix->r2c1 * scale; sum->r2c2 = basic_matrix->r2c2 + scalable_matrix->r2c2 * scale; } // ================== Subtract ================== // inline void bgc_fp32_matrix2x2_subtract(const BGC_FP32_Matrix2x2* minuend, const BGC_FP32_Matrix2x2* subtrahend, BGC_FP32_Matrix2x2* difference) { difference->r1c1 = minuend->r1c1 - subtrahend->r1c1; difference->r1c2 = minuend->r1c2 - subtrahend->r1c2; difference->r2c1 = minuend->r2c1 - subtrahend->r2c1; difference->r2c2 = minuend->r2c2 - subtrahend->r2c2; } inline void bgc_fp64_matrix2x2_subtract(const BGC_FP64_Matrix2x2* minuend, const BGC_FP64_Matrix2x2* subtrahend, BGC_FP64_Matrix2x2* difference) { difference->r1c1 = minuend->r1c1 - subtrahend->r1c1; difference->r1c2 = minuend->r1c2 - subtrahend->r1c2; difference->r2c1 = minuend->r2c1 - subtrahend->r2c1; difference->r2c2 = minuend->r2c2 - subtrahend->r2c2; } // ================== Multiply ================== // inline void bgc_fp32_matrix2x2_multiply(const BGC_FP32_Matrix2x2* multiplicand, const float multiplier, BGC_FP32_Matrix2x2* product) { product->r1c1 = multiplicand->r1c1 * multiplier; product->r1c2 = multiplicand->r1c2 * multiplier; product->r2c1 = multiplicand->r2c1 * multiplier; product->r2c2 = multiplicand->r2c2 * multiplier; } inline void bgc_fp64_matrix2x2_multiply(const BGC_FP64_Matrix2x2* multiplicand, const double multiplier, BGC_FP64_Matrix2x2* product) { product->r1c1 = multiplicand->r1c1 * multiplier; product->r1c2 = multiplicand->r1c2 * multiplier; product->r2c1 = multiplicand->r2c1 * multiplier; product->r2c2 = multiplicand->r2c2 * multiplier; } // =================== Divide =================== // inline void bgc_fp32_matrix2x2_divide(const BGC_FP32_Matrix2x2* dividend, const float divisor, BGC_FP32_Matrix2x2* quotient) { bgc_fp32_matrix2x2_multiply(dividend, 1.0f / divisor, quotient); } inline void bgc_fp64_matrix2x2_divide(const BGC_FP64_Matrix2x2* dividend, const double divisor, BGC_FP64_Matrix2x2* quotient) { bgc_fp64_matrix2x2_multiply(dividend, 1.0 / divisor, quotient); } // ================ Interpolate ================= // inline void bgc_fp32_matrix2x2_interpolate(const BGC_FP32_Matrix2x2* first, const BGC_FP32_Matrix2x2* second, const float phase, BGC_FP32_Matrix2x2* interpolation) { 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->r2c1 = first->r2c1 * counter_phase + second->r2c1 * phase; interpolation->r2c2 = first->r2c2 * counter_phase + second->r2c2 * phase; } inline void bgc_fp64_matrix2x2_interpolate(const BGC_FP64_Matrix2x2* first, const BGC_FP64_Matrix2x2* second, const double phase, BGC_FP64_Matrix2x2* interpolation) { 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->r2c1 = first->r2c1 * counter_phase + second->r2c1 * phase; interpolation->r2c2 = first->r2c2 * counter_phase + second->r2c2 * phase; } // ============ Right Vector Product ============ // inline void bgc_fp32_multiply_matrix2x2_by_vector2(const BGC_FP32_Matrix2x2* matrix, const BGC_FP32_Vector2* vector, BGC_FP32_Vector2* product) { const float x1 = matrix->r1c1 * vector->x1 + matrix->r1c2 * vector->x2; const float x2 = matrix->r2c1 * vector->x1 + matrix->r2c2 * vector->x2; product->x1 = x1; product->x2 = x2; } inline void bgc_fp64_multiply_matrix2x2_by_vector2(const BGC_FP64_Matrix2x2* matrix, const BGC_FP64_Vector2* vector, BGC_FP64_Vector2* product) { const double x1 = matrix->r1c1 * vector->x1 + matrix->r1c2 * vector->x2; const double x2 = matrix->r2c1 * vector->x1 + matrix->r2c2 * vector->x2; product->x1 = x1; product->x2 = x2; } // ============ Left Vector Product ============= // inline void bgc_fp32_multiply_vector2_by_matrix2x2(const BGC_FP32_Vector2* vector, const BGC_FP32_Matrix2x2* matrix, BGC_FP32_Vector2* product) { const float x1 = vector->x1 * matrix->r1c1 + vector->x2 * matrix->r2c1; const float x2 = vector->x1 * matrix->r1c2 + vector->x2 * matrix->r2c2; product->x1 = x1; product->x2 = x2; } inline void bgc_fp64_multiply_vector2_by_matrix2x2(const BGC_FP64_Vector2* vector, const BGC_FP64_Matrix2x2* matrix, BGC_FP64_Vector2* product) { const double x1 = vector->x1 * matrix->r1c1 + vector->x2 * matrix->r2c1; const double x2 = vector->x1 * matrix->r1c2 + vector->x2 * matrix->r2c2; product->x1 = x1; product->x2 = x2; } #endif