Добавление проверки при делении, стандартизация возвращаемого значения (BGC_SUCCESS, BGC_FAILURE)
This commit is contained in:
parent
a4b9f8b2b9
commit
e9558ff977
27 changed files with 589 additions and 370 deletions
|
|
@ -246,7 +246,25 @@ inline void bgc_fp64_quaternion_subtract(BGC_FP64_Quaternion* difference, const
|
|||
difference->x3 = minuend->x3 - subtrahend->x3;
|
||||
}
|
||||
|
||||
// ================== Multiply ================== //
|
||||
// ============= Multiply By Number ============= //
|
||||
|
||||
inline void bgc_fp32_quaternion_multiply_by_real(BGC_FP32_Quaternion* product, const BGC_FP32_Quaternion* multiplicand, const float multipier)
|
||||
{
|
||||
product->s0 = multiplicand->s0 * multipier;
|
||||
product->x1 = multiplicand->x1 * multipier;
|
||||
product->x2 = multiplicand->x2 * multipier;
|
||||
product->x3 = multiplicand->x3 * multipier;
|
||||
}
|
||||
|
||||
inline void bgc_fp64_quaternion_multiply_by_real(BGC_FP64_Quaternion* product, const BGC_FP64_Quaternion* multiplicand, const double multipier)
|
||||
{
|
||||
product->s0 = multiplicand->s0 * multipier;
|
||||
product->x1 = multiplicand->x1 * multipier;
|
||||
product->x2 = multiplicand->x2 * multipier;
|
||||
product->x3 = multiplicand->x3 * multipier;
|
||||
}
|
||||
|
||||
// =========== Multiply By Quaternion =========== //
|
||||
|
||||
inline void bgc_fp32_quaternion_multiply_by_quaternion(BGC_FP32_Quaternion* product, const BGC_FP32_Quaternion* left, const BGC_FP32_Quaternion* right)
|
||||
{
|
||||
|
|
@ -274,6 +292,8 @@ inline void bgc_fp64_quaternion_multiply_by_quaternion(BGC_FP64_Quaternion* prod
|
|||
product->x3 = x3;
|
||||
}
|
||||
|
||||
// ====== Multiply By Conjugate Quaternion ====== //
|
||||
|
||||
inline void bgc_fp32_quaternion_multiply_by_conjugate(BGC_FP32_Quaternion* product, const BGC_FP32_Quaternion* left, const BGC_FP32_Quaternion* right)
|
||||
{
|
||||
const float s0 = (left->s0 * right->s0 + left->x1 * right->x1) + (left->x2 * right->x2 + left->x3 * right->x3);
|
||||
|
|
@ -300,43 +320,42 @@ inline void bgc_fp64_quaternion_multiply_by_conjugate(BGC_FP64_Quaternion* produ
|
|||
product->x3 = x3;
|
||||
}
|
||||
|
||||
inline void bgc_fp32_quaternion_multiply_by_number(BGC_FP32_Quaternion* product, const BGC_FP32_Quaternion* multiplicand, const float multipier)
|
||||
// ============== Divide By Number ============== //
|
||||
|
||||
inline int bgc_fp32_quaternion_divide_by_real(BGC_FP32_Quaternion* quotient, const BGC_FP32_Quaternion* dividend, const float divisor)
|
||||
{
|
||||
product->s0 = multiplicand->s0 * multipier;
|
||||
product->x1 = multiplicand->x1 * multipier;
|
||||
product->x2 = multiplicand->x2 * multipier;
|
||||
product->x3 = multiplicand->x3 * multipier;
|
||||
if (bgc_fp32_is_zero(divisor) || isnan(divisor)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
bgc_fp32_quaternion_multiply_by_real(quotient, dividend, 1.0f / divisor);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline void bgc_fp64_quaternion_multiply_by_number(BGC_FP64_Quaternion* product, const BGC_FP64_Quaternion* multiplicand, const double multipier)
|
||||
inline int bgc_fp64_quaternion_divide_by_real(BGC_FP64_Quaternion* quotient, const BGC_FP64_Quaternion* dividend, const double divisor)
|
||||
{
|
||||
product->s0 = multiplicand->s0 * multipier;
|
||||
product->x1 = multiplicand->x1 * multipier;
|
||||
product->x2 = multiplicand->x2 * multipier;
|
||||
product->x3 = multiplicand->x3 * multipier;
|
||||
if (bgc_fp64_is_zero(divisor) || isnan(divisor)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
bgc_fp64_quaternion_multiply_by_real(quotient, dividend, 1.0 / divisor);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
// =================== Divide =================== //
|
||||
// ============ Divide By Quaternion ============ //
|
||||
|
||||
inline int bgc_fp32_quaternion_divide_by_quaternion(BGC_FP32_Quaternion* quotient, const BGC_FP32_Quaternion* divident, const BGC_FP32_Quaternion* divisor)
|
||||
{
|
||||
const float square_modulus = bgc_fp32_quaternion_get_square_modulus(divisor);
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const float s0 = (divident->s0 * divisor->s0 + divident->x1 * divisor->x1) + (divident->x2 * divisor->x2 + divident->x3 * divisor->x3);
|
||||
const float x1 = (divident->x1 * divisor->s0 + divident->x3 * divisor->x2) - (divident->s0 * divisor->x1 + divident->x2 * divisor->x3);
|
||||
const float x2 = (divident->x2 * divisor->s0 + divident->x1 * divisor->x3) - (divident->s0 * divisor->x2 + divident->x3 * divisor->x1);
|
||||
const float x3 = (divident->x3 * divisor->s0 + divident->x2 * divisor->x1) - (divident->s0 * divisor->x3 + divident->x1 * divisor->x2);
|
||||
|
||||
const float multiplicand = 1.0f / square_modulus;
|
||||
|
||||
quotient->s0 = s0 * multiplicand;
|
||||
quotient->x1 = x1 * multiplicand;
|
||||
quotient->x2 = x2 * multiplicand;
|
||||
quotient->x3 = x3 * multiplicand;
|
||||
bgc_fp32_quaternion_multiply_by_conjugate(quotient, divident, divisor);
|
||||
bgc_fp32_quaternion_multiply_by_real(quotient, quotient, 1.0f / square_modulus);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
|
@ -346,32 +365,43 @@ inline int bgc_fp64_quaternion_divide_by_quaternion(BGC_FP64_Quaternion* quotien
|
|||
const double square_modulus = bgc_fp64_quaternion_get_square_modulus(divisor);
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const double s0 = (divident->s0 * divisor->s0 + divident->x1 * divisor->x1) + (divident->x2 * divisor->x2 + divident->x3 * divisor->x3);
|
||||
const double x1 = (divident->x1 * divisor->s0 + divident->x3 * divisor->x2) - (divident->s0 * divisor->x1 + divident->x2 * divisor->x3);
|
||||
const double x2 = (divident->x2 * divisor->s0 + divident->x1 * divisor->x3) - (divident->s0 * divisor->x2 + divident->x3 * divisor->x1);
|
||||
const double x3 = (divident->x3 * divisor->s0 + divident->x2 * divisor->x1) - (divident->s0 * divisor->x3 + divident->x1 * divisor->x2);
|
||||
|
||||
const double multiplicand = 1.0 / square_modulus;
|
||||
|
||||
quotient->s0 = s0 * multiplicand;
|
||||
quotient->x1 = x1 * multiplicand;
|
||||
quotient->x2 = x2 * multiplicand;
|
||||
quotient->x3 = x3 * multiplicand;
|
||||
bgc_fp64_quaternion_multiply_by_conjugate(quotient, divident, divisor);
|
||||
bgc_fp64_quaternion_multiply_by_real(quotient, quotient, 1.0 / square_modulus);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline void bgc_fp32_quaternion_divide_by_number(BGC_FP32_Quaternion* quotient, const BGC_FP32_Quaternion* dividend, const float divisor)
|
||||
// ======= Divide By Conjugate Quaternion ======= //
|
||||
|
||||
inline int bgc_fp32_quaternion_divide_by_conjugate(BGC_FP32_Quaternion* quotient, const BGC_FP32_Quaternion* divident, const BGC_FP32_Quaternion* divisor_to_conjugate)
|
||||
{
|
||||
bgc_fp32_quaternion_multiply_by_number(quotient, dividend, 1.0f / divisor);
|
||||
const float square_modulus = bgc_fp32_quaternion_get_square_modulus(divisor_to_conjugate);
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
bgc_fp32_quaternion_multiply_by_quaternion(quotient, divisor_to_conjugate, divisor);
|
||||
bgc_fp32_quaternion_multiply_by_real(quotient, quotient, 1.0f / square_modulus);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline void bgc_fp64_quaternion_divide_by_number(BGC_FP64_Quaternion* quotient, const BGC_FP64_Quaternion* dividend, const double divisor)
|
||||
inline int bgc_fp64_quaternion_divide_by_conjugate(BGC_FP64_Quaternion* quotient, const BGC_FP64_Quaternion* divident, const BGC_FP64_Quaternion* divisor_to_conjugate)
|
||||
{
|
||||
bgc_fp64_quaternion_multiply_by_number(quotient, dividend, 1.0 / divisor);
|
||||
const double square_modulus = bgc_fp64_quaternion_get_square_modulus(divisor_to_conjugate);
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
bgc_fp64_quaternion_multiply_by_quaternion(quotient, divisor_to_conjugate, divisor);
|
||||
bgc_fp64_quaternion_multiply_by_real(quotient, quotient, 1.0 / square_modulus);
|
||||
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
// ================ Mean of Two ================= //
|
||||
|
|
@ -505,7 +535,7 @@ inline int bgc_fp32_quaternion_get_inverse(BGC_FP32_Quaternion* inverse, const B
|
|||
const float square_modulus = bgc_fp32_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const float multiplicand = 1.0f / square_modulus;
|
||||
|
|
@ -523,7 +553,7 @@ inline int bgc_fp64_quaternion_get_inverse(BGC_FP64_Quaternion* inverse, const B
|
|||
const double square_modulus = bgc_fp64_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const double multiplicand = 1.0 / square_modulus;
|
||||
|
|
@ -552,12 +582,12 @@ inline int bgc_fp32_quaternion_normalize(BGC_FP32_Quaternion* quaternion)
|
|||
{
|
||||
const float square_modulus = bgc_fp32_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (bgc_fp32_is_square_unit(square_modulus)) {
|
||||
return BGC_SUCCESS;
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
if (bgc_fp32_is_square_unit(square_modulus)) {
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
const float multiplier = sqrtf(1.0f / square_modulus);
|
||||
|
|
@ -574,12 +604,12 @@ inline int bgc_fp64_quaternion_normalize(BGC_FP64_Quaternion* quaternion)
|
|||
{
|
||||
const double square_modulus = bgc_fp64_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (bgc_fp64_is_square_unit(square_modulus)) {
|
||||
return BGC_SUCCESS;
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
if (bgc_fp64_is_square_unit(square_modulus)) {
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
const double multiplier = sqrt(1.0 / square_modulus);
|
||||
|
|
@ -596,17 +626,17 @@ inline int bgc_fp32_quaternion_get_normalized(BGC_FP32_Quaternion* normalized, c
|
|||
{
|
||||
const float square_modulus = bgc_fp32_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
bgc_fp32_quaternion_reset(normalized);
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
if (bgc_fp32_is_square_unit(square_modulus)) {
|
||||
bgc_fp32_quaternion_copy(normalized, quaternion);
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
bgc_fp32_quaternion_reset(normalized);
|
||||
return BGC_FAILED;
|
||||
}
|
||||
|
||||
bgc_fp32_quaternion_multiply_by_number(normalized, quaternion, sqrtf(1.0f / square_modulus));
|
||||
bgc_fp32_quaternion_multiply_by_real(normalized, quaternion, sqrtf(1.0f / square_modulus));
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
|
|
@ -614,17 +644,17 @@ inline int bgc_fp64_quaternion_get_normalized(BGC_FP64_Quaternion* normalized, c
|
|||
{
|
||||
const double square_modulus = bgc_fp64_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
bgc_fp64_quaternion_reset(normalized);
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
if (bgc_fp64_is_square_unit(square_modulus)) {
|
||||
bgc_fp64_quaternion_copy(normalized, quaternion);
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
bgc_fp64_quaternion_reset(normalized);
|
||||
return BGC_FAILED;
|
||||
}
|
||||
|
||||
bgc_fp64_quaternion_multiply_by_number(normalized, quaternion, sqrt(1.0 / square_modulus));
|
||||
bgc_fp64_quaternion_multiply_by_real(normalized, quaternion, sqrt(1.0 / square_modulus));
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
|
|
@ -709,7 +739,7 @@ inline int bgc_fp32_quaternion_turn_vector(BGC_FP32_Vector3* turned_vector, cons
|
|||
const float square_modulus = bgc_fp32_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (square_modulus < BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const float multiplier = 2.0f / square_modulus;
|
||||
|
|
@ -735,7 +765,7 @@ inline int bgc_fp64_quaternion_turn_vector(BGC_FP64_Vector3* turned_vector, cons
|
|||
const double square_modulus = bgc_fp64_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (square_modulus < BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const double multiplier = 2.0 / square_modulus;
|
||||
|
|
@ -763,7 +793,7 @@ inline int bgc_fp32_quaternion_turn_vector_back(BGC_FP32_Vector3* turned_vector,
|
|||
const float square_modulus = bgc_fp32_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (square_modulus < BGC_FP32_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const float multiplier = 2.0f / square_modulus;
|
||||
|
|
@ -789,7 +819,7 @@ inline int bgc_fp64_quaternion_turn_vector_back(BGC_FP64_Vector3* turned_vector,
|
|||
const double square_modulus = bgc_fp64_quaternion_get_square_modulus(quaternion);
|
||||
|
||||
if (square_modulus < BGC_FP64_SQUARE_EPSILON || isnan(square_modulus)) {
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const double multiplier = 2.0 / square_modulus;
|
||||
|
|
@ -824,7 +854,7 @@ inline int bgc_fp32_quaternion_get_rotation_matrix(BGC_FP32_Matrix3x3* rotation,
|
|||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus))
|
||||
{
|
||||
bgc_fp32_matrix3x3_make_identity(rotation);
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const float corrector1 = 1.0f / square_modulus;
|
||||
|
|
@ -865,7 +895,7 @@ inline int bgc_fp64_quaternion_get_rotation_matrix(BGC_FP64_Matrix3x3* rotation,
|
|||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus))
|
||||
{
|
||||
bgc_fp64_matrix3x3_make_identity(rotation);
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const double corrector1 = 1.0f / square_modulus;
|
||||
|
|
@ -908,7 +938,7 @@ inline int bgc_fp32_quaternion_get_reverse_matrix(BGC_FP32_Matrix3x3* reverse, c
|
|||
if (square_modulus <= BGC_FP32_SQUARE_EPSILON || isnan(square_modulus))
|
||||
{
|
||||
bgc_fp32_matrix3x3_make_identity(reverse);
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const float corrector1 = 1.0f / square_modulus;
|
||||
|
|
@ -949,7 +979,7 @@ inline int bgc_fp64_quaternion_get_reverse_matrix(BGC_FP64_Matrix3x3* reverse, c
|
|||
if (square_modulus <= BGC_FP64_SQUARE_EPSILON || isnan(square_modulus))
|
||||
{
|
||||
bgc_fp64_matrix3x3_make_identity(reverse);
|
||||
return BGC_FAILED;
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
const double corrector1 = 1.0f / square_modulus;
|
||||
|
|
@ -975,29 +1005,29 @@ inline int bgc_fp64_quaternion_get_reverse_matrix(BGC_FP64_Matrix3x3* reverse, c
|
|||
reverse->r3c2 = corrector2 * (x2x3 - s0x1);
|
||||
reverse->r1c3 = corrector2 * (x1x3 - s0x2);
|
||||
|
||||
return 1;
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
// ============= Get Both Matrixes ============== //
|
||||
|
||||
inline int bgc_fp32_quaternion_get_both_matrices(BGC_FP32_Matrix3x3* rotation, BGC_FP32_Matrix3x3* reverse, const BGC_FP32_Quaternion* quaternion)
|
||||
{
|
||||
if (bgc_fp32_quaternion_get_reverse_matrix(reverse, quaternion) == BGC_SUCCESS) {
|
||||
bgc_fp32_matrix3x3_get_transposed(rotation, reverse);
|
||||
return BGC_SUCCESS;
|
||||
if (bgc_fp32_quaternion_get_reverse_matrix(reverse, quaternion) != BGC_SUCCESS) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
return BGC_FAILED;
|
||||
bgc_fp32_matrix3x3_get_transposed(rotation, reverse);
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
inline int bgc_fp64_quaternion_get_both_matrices(BGC_FP64_Matrix3x3* rotation, BGC_FP64_Matrix3x3* reverse, const BGC_FP64_Quaternion* quaternion)
|
||||
{
|
||||
if (bgc_fp64_quaternion_get_reverse_matrix(reverse, quaternion) == BGC_SUCCESS) {
|
||||
bgc_fp64_matrix3x3_get_transposed(rotation, reverse);
|
||||
return BGC_SUCCESS;
|
||||
if (bgc_fp64_quaternion_get_reverse_matrix(reverse, quaternion) != BGC_SUCCESS) {
|
||||
return BGC_FAILURE;
|
||||
}
|
||||
|
||||
return BGC_FAILED;
|
||||
bgc_fp64_matrix3x3_get_transposed(rotation, reverse);
|
||||
return BGC_SUCCESS;
|
||||
}
|
||||
|
||||
// ================== Are Close ================= //
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue