#include #include "angle.h" #include "versor.h" const BgFP32Versor BG_FP32_IDLE_VERSOR = { 1.0f, 0.0f, 0.0f, 0.0f }; const BgFP64Versor BG_FP64_IDLE_VERSOR = { 1.0, 0.0, 0.0, 0.0 }; // ==================== Set ===================== // void bg_fp32_versor_set_values(const float s0, const float x1, const float x2, const float x3, BgFP32Versor* versor) { __BgFP32DarkTwinVersor* twin = (__BgFP32DarkTwinVersor*)versor; twin->s0 = s0; twin->x1 = x1; twin->x2 = x2; twin->x3 = x3; const float square_modulus = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); if (1.0f - BG_FP32_TWO_EPSYLON <= square_modulus && square_modulus <= 1.0f + BG_FP32_TWO_EPSYLON) { return; } if (square_modulus <= BG_FP32_SQUARE_EPSYLON) { twin->s0 = 1.0f; twin->x1 = 0.0f; twin->x2 = 0.0f; twin->x3 = 0.0f; return; } const float multiplier = sqrtf(1.0f / square_modulus); twin->s0 *= multiplier; twin->x1 *= multiplier; twin->x2 *= multiplier; twin->x3 *= multiplier; } void bg_fp64_versor_set_values(const double s0, const double x1, const double x2, const double x3, BgFP64Versor* versor) { __BgFP64DarkTwinVersor* twin = (__BgFP64DarkTwinVersor*)versor; twin->s0 = s0; twin->x1 = x1; twin->x2 = x2; twin->x3 = x3; const double square_modulus = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); if (1.0 - BG_FP64_TWO_EPSYLON <= square_modulus && square_modulus <= 1.0 + BG_FP64_TWO_EPSYLON) { return; } if (square_modulus <= BG_FP64_SQUARE_EPSYLON) { twin->s0 = 1.0; twin->x1 = 0.0; twin->x2 = 0.0; twin->x3 = 0.0; return; } const double multiplier = sqrt(1.0 / square_modulus); twin->s0 *= multiplier; twin->x1 *= multiplier; twin->x2 *= multiplier; twin->x3 *= multiplier; } // =============== Set Crude Turn =============== // void bg_fp32_versor_set_crude_turn(const float x1, const float x2, const float x3, const float angle, const angle_unit_t unit, BgFP32Versor* result) { const float square_vector = x1 * x1 + x2 * x2 + x3 * x3; if (square_vector <= BG_FP32_SQUARE_EPSYLON) { bg_fp32_versor_reset(result); return; } const float half_angle = bg_fp32_angle_to_radians(0.5f * angle, unit); const float sine = sinf(half_angle); if (-BG_FP32_EPSYLON <= sine && sine <= BG_FP32_EPSYLON) { bg_fp32_versor_reset(result); return; } const float multiplier = sine / sqrtf(square_vector); bg_fp32_versor_set_values(cosf(half_angle), x1 * multiplier, x2 * multiplier, x3 * multiplier, result); } void bg_fp64_versor_set_crude_turn(const double x1, const double x2, const double x3, const double angle, const angle_unit_t unit, BgFP64Versor* result) { const double square_vector = x1 * x1 + x2 * x2 + x3 * x3; if (square_vector <= BG_FP64_SQUARE_EPSYLON) { bg_fp64_versor_reset(result); return; } const double half_angle = bg_fp64_angle_to_radians(0.5 * angle, unit); const double sine = sin(half_angle); if (-BG_FP64_EPSYLON <= sine && sine <= BG_FP64_EPSYLON) { bg_fp64_versor_reset(result); return; } const double multiplier = sine / sqrt(square_vector); bg_fp64_versor_set_values(cos(half_angle), x1 * multiplier, x2 * multiplier, x3 * multiplier, result); } // ================= Rotation3 ================== // void bg_fp32_versor_get_rotation(const BgFP32Versor* versor, BgFP32Rotation3* result) { if (versor == 0 || result == 0) { return; } if (versor->s0 <= -(1.0f - BG_FP32_EPSYLON) || 1.0f - BG_FP32_EPSYLON <= versor->s0) { bg_fp32_rotation_reset(result); return; } const float square_vector = versor->x1 * versor->x1 + versor->x2 * versor->x2 + versor->x3 * versor->x3; result->radians = 2.0f * acosf(versor->s0 / sqrtf(versor->s0 * versor->s0 + square_vector)); const float multiplier = sqrtf(1.0f / square_vector); result->axis.x1 = versor->x1 * multiplier; result->axis.x2 = versor->x2 * multiplier; result->axis.x3 = versor->x3 * multiplier; } void bg_fp64_versor_get_rotation(const BgFP64Versor* versor, BgFP64Rotation3* result) { if (versor == 0 || result == 0) { return; } if (versor->s0 <= -(1.0 - BG_FP64_EPSYLON) || 1.0 - BG_FP64_EPSYLON <= versor->s0) { bg_fp64_rotation_reset(result); return; } const double square_vector = versor->x1 * versor->x1 + versor->x2 * versor->x2 + versor->x3 * versor->x3; result->radians = 2.0 * acos(versor->s0 / sqrt(versor->s0 * versor->s0 + square_vector)); const double multiplier = sqrt(1.0 / square_vector); result->axis.x1 = versor->x1 * multiplier; result->axis.x2 = versor->x2 * multiplier; result->axis.x3 = versor->x3 * multiplier; } // ================ Combination ================= // void bg_fp32_versor_combine(const BgFP32Versor* second, const BgFP32Versor* first, BgFP32Versor* result) { const float s0 = (second->s0 * first->s0 - second->x1 * first->x1) - (second->x2 * first->x2 + second->x3 * first->x3); const float x1 = (second->x1 * first->s0 + second->s0 * first->x1) - (second->x3 * first->x2 - second->x2 * first->x3); const float x2 = (second->x2 * first->s0 + second->s0 * first->x2) - (second->x1 * first->x3 - second->x3 * first->x1); const float x3 = (second->x3 * first->s0 + second->s0 * first->x3) - (second->x2 * first->x1 - second->x1 * first->x2); const float square_modulus = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); __BgFP32DarkTwinVersor* twin = (__BgFP32DarkTwinVersor*)result; twin->s0 = s0; twin->x1 = x1; twin->x2 = x2; twin->x3 = x3; if (1.0f - BG_FP32_TWO_EPSYLON <= square_modulus && square_modulus <= 1.0f + BG_FP32_TWO_EPSYLON) { return; } if (square_modulus <= BG_FP32_SQUARE_EPSYLON) { twin->s0 = 1.0f; twin->x1 = 0.0f; twin->x2 = 0.0f; twin->x3 = 0.0f; return; } const float multiplier = sqrtf(1.0f / square_modulus); twin->s0 *= multiplier; twin->x1 *= multiplier; twin->x2 *= multiplier; twin->x3 *= multiplier; } void bg_fp64_versor_combine(const BgFP64Versor* second, const BgFP64Versor* first, BgFP64Versor* result) { const double s0 = (second->s0 * first->s0 - second->x1 * first->x1) - (second->x2 * first->x2 + second->x3 * first->x3); const double x1 = (second->x1 * first->s0 + second->s0 * first->x1) - (second->x3 * first->x2 - second->x2 * first->x3); const double x2 = (second->x2 * first->s0 + second->s0 * first->x2) - (second->x1 * first->x3 - second->x3 * first->x1); const double x3 = (second->x3 * first->s0 + second->s0 * first->x3) - (second->x2 * first->x1 - second->x1 * first->x2); const double square_modulus = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); __BgFP64DarkTwinVersor* twin = (__BgFP64DarkTwinVersor*)result; twin->s0 = s0; twin->x1 = x1; twin->x2 = x2; twin->x3 = x3; if (1.0 - BG_FP64_TWO_EPSYLON <= square_modulus && square_modulus <= 1.0 + BG_FP64_TWO_EPSYLON) { return; } if (square_modulus <= BG_FP64_SQUARE_EPSYLON) { twin->s0 = 1.0; twin->x1 = 0.0; twin->x2 = 0.0; twin->x3 = 0.0; return; } const double multiplier = sqrt(1.0 / square_modulus); twin->s0 *= multiplier; twin->x1 *= multiplier; twin->x2 *= multiplier; twin->x3 *= multiplier; } // =========== Make Rotation Matrix3x3 ========== // void bg_fp32_versor_get_rotation_matrix(const BgFP32Versor* versor, BgFP32Matrix3x3* matrix) { const float s0s0 = versor->s0 * versor->s0; const float x1x1 = versor->x1 * versor->x1; const float x2x2 = versor->x2 * versor->x2; const float x3x3 = versor->x3 * versor->x3; const float s0x1 = 2.0f * versor->s0 * versor->x1; const float s0x2 = 2.0f * versor->s0 * versor->x2; const float s0x3 = 2.0f * versor->s0 * versor->x3; const float x1x2 = 2.0f * versor->x1 * versor->x2; const float x1x3 = 2.0f * versor->x1 * versor->x3; const float x2x3 = 2.0f * versor->x2 * versor->x3; matrix->r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); matrix->r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); matrix->r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); matrix->r1c2 = x1x2 - s0x3; matrix->r2c3 = x2x3 - s0x1; matrix->r3c1 = x1x3 - s0x2; matrix->r2c1 = x1x2 + s0x3; matrix->r3c2 = x2x3 + s0x1; matrix->r1c3 = x1x3 + s0x2; } void bg_fp64_versor_get_rotation_matrix(const BgFP64Versor* versor, BgFP64Matrix3x3* matrix) { const double s0s0 = versor->s0 * versor->s0; const double x1x1 = versor->x1 * versor->x1; const double x2x2 = versor->x2 * versor->x2; const double x3x3 = versor->x3 * versor->x3; const double s0x1 = 2.0 * versor->s0 * versor->x1; const double s0x2 = 2.0 * versor->s0 * versor->x2; const double s0x3 = 2.0 * versor->s0 * versor->x3; const double x1x2 = 2.0 * versor->x1 * versor->x2; const double x1x3 = 2.0 * versor->x1 * versor->x3; const double x2x3 = 2.0 * versor->x2 * versor->x3; matrix->r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); matrix->r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); matrix->r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); matrix->r1c2 = x1x2 - s0x3; matrix->r2c3 = x2x3 - s0x1; matrix->r3c1 = x1x3 - s0x2; matrix->r2c1 = x1x2 + s0x3; matrix->r3c2 = x2x3 + s0x1; matrix->r1c3 = x1x3 + s0x2; } // =========== Make Reverse Matrix3x3 =========== // void bg_fp32_versor_get_reverse_matrix(const BgFP32Versor* versor, BgFP32Matrix3x3* matrix) { const float s0s0 = versor->s0 * versor->s0; const float x1x1 = versor->x1 * versor->x1; const float x2x2 = versor->x2 * versor->x2; const float x3x3 = versor->x3 * versor->x3; const float s0x1 = 2.0f * versor->s0 * versor->x1; const float s0x2 = 2.0f * versor->s0 * versor->x2; const float s0x3 = 2.0f * versor->s0 * versor->x3; const float x1x2 = 2.0f * versor->x1 * versor->x2; const float x1x3 = 2.0f * versor->x1 * versor->x3; const float x2x3 = 2.0f * versor->x2 * versor->x3; matrix->r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); matrix->r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); matrix->r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); matrix->r1c2 = x1x2 + s0x3; matrix->r2c3 = x2x3 + s0x1; matrix->r3c1 = x1x3 + s0x2; matrix->r2c1 = x1x2 - s0x3; matrix->r3c2 = x2x3 - s0x1; matrix->r1c3 = x1x3 - s0x2; } void bg_fp64_versor_get_reverse_matrix(const BgFP64Versor* versor, BgFP64Matrix3x3* matrix) { const double s0s0 = versor->s0 * versor->s0; const double x1x1 = versor->x1 * versor->x1; const double x2x2 = versor->x2 * versor->x2; const double x3x3 = versor->x3 * versor->x3; const double s0x1 = 2.0 * versor->s0 * versor->x1; const double s0x2 = 2.0 * versor->s0 * versor->x2; const double s0x3 = 2.0 * versor->s0 * versor->x3; const double x1x2 = 2.0 * versor->x1 * versor->x2; const double x1x3 = 2.0 * versor->x1 * versor->x3; const double x2x3 = 2.0 * versor->x2 * versor->x3; matrix->r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); matrix->r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); matrix->r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); matrix->r1c2 = x1x2 + s0x3; matrix->r2c3 = x2x3 + s0x1; matrix->r3c1 = x1x3 + s0x2; matrix->r2c1 = x1x2 - s0x3; matrix->r3c2 = x2x3 - s0x1; matrix->r1c3 = x1x3 - s0x2; } // ================ Turn Vector ================= // void bg_fp32_versor_turn(const BgFP32Versor* versor, const BgFP32Vector3* vector, BgFP32Vector3* result) { const float tx1 = 2.0f * (versor->x2 * vector->x3 - versor->x3 * vector->x2); const float tx2 = 2.0f * (versor->x3 * vector->x1 - versor->x1 * vector->x3); const float tx3 = 2.0f * (versor->x1 * vector->x2 - versor->x2 * vector->x1); const float x1 = (vector->x1 + tx1 * versor->s0) + (versor->x2 * tx3 - versor->x3 * tx2); const float x2 = (vector->x2 + tx2 * versor->s0) + (versor->x3 * tx1 - versor->x1 * tx3); const float x3 = (vector->x3 + tx3 * versor->s0) + (versor->x1 * tx2 - versor->x2 * tx1); result->x1 = x1; result->x2 = x2; result->x3 = x3; } void bg_fp64_versor_turn(const BgFP64Versor* versor, const BgFP64Vector3* vector, BgFP64Vector3* result) { const double tx1 = 2.0 * (versor->x2 * vector->x3 - versor->x3 * vector->x2); const double tx2 = 2.0 * (versor->x3 * vector->x1 - versor->x1 * vector->x3); const double tx3 = 2.0 * (versor->x1 * vector->x2 - versor->x2 * vector->x1); const double x1 = (vector->x1 + tx1 * versor->s0) + (versor->x2 * tx3 - versor->x3 * tx2); const double x2 = (vector->x2 + tx2 * versor->s0) + (versor->x3 * tx1 - versor->x1 * tx3); const double x3 = (vector->x3 + tx3 * versor->s0) + (versor->x1 * tx2 - versor->x2 * tx1); result->x1 = x1; result->x2 = x2; result->x3 = x3; } // ============== Turn Vector Back ============== // void bg_fp32_versor_turn_back(const BgFP32Versor* versor, const BgFP32Vector3* vector, BgFP32Vector3* result) { const float tx1 = 2.0f * (versor->x2 * vector->x3 - versor->x3 * vector->x2); const float tx2 = 2.0f * (versor->x3 * vector->x1 - versor->x1 * vector->x3); const float tx3 = 2.0f * (versor->x1 * vector->x2 - versor->x2 * vector->x1); const float x1 = (vector->x1 - tx1 * versor->s0) + (versor->x2 * tx3 - versor->x3 * tx2); const float x2 = (vector->x2 - tx2 * versor->s0) + (versor->x3 * tx1 - versor->x1 * tx3); const float x3 = (vector->x3 - tx3 * versor->s0) + (versor->x1 * tx2 - versor->x2 * tx1); result->x1 = x1; result->x2 = x2; result->x3 = x3; } void bg_fp64_versor_turn_back(const BgFP64Versor* versor, const BgFP64Vector3* vector, BgFP64Vector3* result) { const double tx1 = 2.0 * (versor->x2 * vector->x3 - versor->x3 * vector->x2); const double tx2 = 2.0 * (versor->x3 * vector->x1 - versor->x1 * vector->x3); const double tx3 = 2.0 * (versor->x1 * vector->x2 - versor->x2 * vector->x1); const double x1 = (vector->x1 - tx1 * versor->s0) + (versor->x2 * tx3 - versor->x3 * tx2); const double x2 = (vector->x2 - tx2 * versor->s0) + (versor->x3 * tx1 - versor->x1 * tx3); const double x3 = (vector->x3 - tx3 * versor->s0) + (versor->x1 * tx2 - versor->x2 * tx1); result->x1 = x1; result->x2 = x2; result->x3 = x3; }