#ifndef _BGC_TURN3_H_INCLUDED_ #define _BGC_TURN3_H_INCLUDED_ #include #include "./utilities.h" #include "./types.h" #include "./angle.h" #include "./quaternion.h" #define BGC_ERROR_TURN3_FIRST_VECTOR_ZERO -3010 #define BGC_ERROR_TURN3_SECOND_VECTOR_ZERO -3011 #define BGC_ERROR_TURN3_VECTORS_OPPOSITE -3012 #define _BGC_ERROR_TURN3_FIRST_PAIR -3020 #define _BGC_ERROR_TURN3_SECOND_PAIR -3030 #define _BGC_ERROR_TURN3_EMPTY_MAIN -1 #define _BGC_ERROR_TURN3_EMPTY_BRANCH -2 #define _BGC_ERROR_TURN3_PAIR_PARALLEL -3 #define BGC_ERROR_TURN3_FIRST_PAIR_ZERO_MAIN -3021 #define BGC_ERROR_TURN3_FIRST_PAIR_ZERO_BRANCH -3022 #define BGC_ERROR_TURN3_FIRST_PAIR_PARALLEL -3023 #define BGC_ERROR_TURN3_SECOND_PAIR_ZERO_MAIN -3031 #define BGC_ERROR_TURN3_SECOND_PAIR_ZERO_BRANCH -3032 #define BGC_ERROR_TURN3_SECOND_PAIR_PARALLEL -3033 // ================= Constants ================== // extern const BGC_FP32_Turn3 BGC_FP32_IDLE_TURN3; extern const BGC_FP64_Turn3 BGC_FP64_IDLE_TURN3; // =================== Reset ==================== // inline void bgc_fp32_turn3_reset(BGC_FP32_Turn3* turn) { bgc_fp32_quaternion_make(&turn->_versor, 1.0f, 0.0f, 0.0f, 0.0f); } inline void bgc_fp64_turn3_reset(BGC_FP64_Turn3* turn) { bgc_fp64_quaternion_make(&turn->_versor, 1.0, 0.0, 0.0, 0.0); } // ============= Private: Normalize ============= // void _bgc_fp32_turn3_normalize(BGC_FP32_Turn3* turn, const float square_modulus); void _bgc_fp64_turn3_normalize(BGC_FP64_Turn3* turn, const double square_modulus); // ================= Set Values ================= // inline void bgc_fp32_turn3_set_raw_values(BGC_FP32_Turn3* turn, const float s0, const float x1, const float x2, const float x3) { bgc_fp32_quaternion_make(&turn->_versor, s0, x1, x2, x3); const float square_modulus = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); if (!bgc_fp32_is_square_unit(square_modulus)) { _bgc_fp32_turn3_normalize(turn, square_modulus); } } inline void bgc_fp64_turn3_set_raw_values(BGC_FP64_Turn3* turn, const double s0, const double x1, const double x2, const double x3) { bgc_fp64_quaternion_make(&turn->_versor, s0, x1, x2, x3); const double square_modulus = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); if (!bgc_fp64_is_square_unit(square_modulus)) { _bgc_fp64_turn3_normalize(turn, square_modulus); } } // =============== Get Quaternion =============== // inline void bgc_fp32_turn3_get_quaternion(BGC_FP32_Quaternion* quaternion, const BGC_FP32_Turn3* turn) { bgc_fp32_quaternion_copy(quaternion, &turn->_versor); } inline void bgc_fp64_turn3_get_quaternion(BGC_FP64_Quaternion* quaternion, const BGC_FP64_Turn3* turn) { bgc_fp64_quaternion_copy(quaternion, &turn->_versor); } // =============== Set Quaternion =============== // inline void bgc_fp32_turn3_set_quaternion(BGC_FP32_Turn3* turn, const BGC_FP32_Quaternion* quaternion) { bgc_fp32_quaternion_copy(&turn->_versor, quaternion); const float square_modulus = bgc_fp32_quaternion_get_square_modulus(quaternion); if (!bgc_fp32_is_square_unit(square_modulus)) { _bgc_fp32_turn3_normalize(turn, square_modulus); } } inline void bgc_fp64_turn3_set_quaternion(BGC_FP64_Turn3* turn, const BGC_FP64_Quaternion* quaternion) { bgc_fp64_quaternion_copy(&turn->_versor, quaternion); const double square_modulus = bgc_fp64_quaternion_get_square_modulus(quaternion); if (!bgc_fp64_is_square_unit(square_modulus)) { _bgc_fp64_turn3_normalize(turn, square_modulus); } } // ================ Get Rotation ================ // float bgc_fp32_turn3_get_rotation(BGC_FP32_Vector3* axis, const BGC_FP32_Turn3* turn, const int angle_unit); double bgc_fp64_turn3_get_rotation(BGC_FP64_Vector3* axis, const BGC_FP64_Turn3* turn, const int angle_unit); // ================ Set Rotation ================ // void bgc_fp32_turn3_set_rotation(BGC_FP32_Turn3* turn, const float x1, const float x2, const float x3, const float angle, const int angle_unit); void bgc_fp64_turn3_set_rotation(BGC_FP64_Turn3* turn, const double x1, const double x2, const double x3, const double angle, const int angle_unit); // ========= Find Direction Difference ========== // int bgc_fp32_turn3_find_direction_difference(BGC_FP32_Turn3* turn, const BGC_FP32_Vector3* start, const BGC_FP32_Vector3* end); int bgc_fp64_turn3_find_direction_difference(BGC_FP64_Turn3* turn, const BGC_FP64_Vector3* start, const BGC_FP64_Vector3* end); // ======= Find Direction Pair Difference ======= // int bgc_fp32_turn3_find_pair_difference( BGC_FP32_Turn3* turn, const BGC_FP32_Vector3* first_pair_main, const BGC_FP32_Vector3* first_pair_branch, const BGC_FP32_Vector3* second_pair_main, const BGC_FP32_Vector3* second_pair_branch ); int bgc_fp64_turn3_find_pair_difference( BGC_FP64_Turn3* turn, const BGC_FP64_Vector3* first_pair_main, const BGC_FP64_Vector3* first_pair_branch, const BGC_FP64_Vector3* second_pair_main, const BGC_FP64_Vector3* second_pair_branch ); // ==================== Copy ==================== // inline void bgc_fp32_turn3_copy(BGC_FP32_Turn3* destination, const BGC_FP32_Turn3* source) { bgc_fp32_quaternion_copy(&destination->_versor, &source->_versor); } inline void bgc_fp64_turn3_copy(BGC_FP64_Turn3* destination, const BGC_FP64_Turn3* source) { bgc_fp64_quaternion_copy(&destination->_versor, &source->_versor); } // ==================== Swap ==================== // inline void bgc_fp32_turn3_swap(BGC_FP32_Turn3* turn1, BGC_FP32_Turn3* turn2) { bgc_fp32_quaternion_swap(&turn1->_versor, &turn2->_versor); } inline void bgc_fp64_turn3_swap(BGC_FP64_Turn3* turn1, BGC_FP64_Turn3* turn2) { bgc_fp64_quaternion_swap(&turn1->_versor, &turn2->_versor); } // ================= Comparison ================= // inline int bgc_fp32_turn3_is_idle(const BGC_FP32_Turn3* turn) { return turn->_versor.x1 * turn->_versor.x1 + turn->_versor.x2 * turn->_versor.x2 + turn->_versor.x3 * turn->_versor.x3 <= BGC_FP32_SQUARE_EPSILON; } inline int bgc_fp64_turn3_is_idle(const BGC_FP64_Turn3* turn) { return turn->_versor.x1 * turn->_versor.x1 + turn->_versor.x2 * turn->_versor.x2 + turn->_versor.x3 * turn->_versor.x3 <= BGC_FP64_SQUARE_EPSILON; } // ================== Convert =================== // inline void bgc_fp32_turn3_convert_to_fp64(BGC_FP64_Turn3* destination, const BGC_FP32_Turn3* source) { bgc_fp32_quaternion_convert_to_fp64(&destination->_versor, &source->_versor); const double square_modulus = bgc_fp64_quaternion_get_square_modulus(&destination->_versor); if (!bgc_fp64_is_square_unit(square_modulus)) { _bgc_fp64_turn3_normalize(destination, square_modulus); } } inline void bgc_fp64_turn3_convert_to_fp32(BGC_FP32_Turn3* destination, const BGC_FP64_Turn3* source) { bgc_fp64_quaternion_convert_to_fp32(&destination->_versor, &source->_versor); const float square_modulus = bgc_fp32_quaternion_get_square_modulus(&destination->_versor); if (!bgc_fp32_is_square_unit(square_modulus)) { _bgc_fp32_turn3_normalize(destination, square_modulus); } } // ================== Shorten =================== // inline void bgc_fp32_turn3_shorten(BGC_FP32_Turn3* turn) { if (turn->_versor.s0 < 0.0f) { bgc_fp32_quaternion_revert(&turn->_versor); } } inline void bgc_fp64_turn3_shorten(BGC_FP64_Turn3* turn) { if (turn->_versor.s0 < 0.0) { bgc_fp64_quaternion_revert(&turn->_versor); } } inline void bgc_fp32_turn3_get_shortened(BGC_FP32_Turn3* shortened, const BGC_FP32_Turn3* turn) { if (turn->_versor.s0 >= 0.0f) { bgc_fp32_quaternion_copy(&shortened->_versor, &turn->_versor); } else { bgc_fp32_quaternion_get_reverse(&shortened->_versor, &turn->_versor); } } inline void bgc_fp64_turn3_get_shortened(BGC_FP64_Turn3* shortened, const BGC_FP64_Turn3* turn) { if (turn->_versor.s0 >= 0.0) { bgc_fp64_quaternion_copy(&shortened->_versor, &turn->_versor); } else { bgc_fp64_quaternion_get_reverse(&shortened->_versor, &turn->_versor); } } // ================= Alternate ================== // inline void bgc_fp32_turn3_alternate(BGC_FP32_Turn3* turn) { bgc_fp32_quaternion_revert(&turn->_versor); } inline void bgc_fp64_turn3_alternate(BGC_FP64_Turn3* turn) { bgc_fp64_quaternion_revert(&turn->_versor); } inline void bgc_fp32_turn3_get_alternative(BGC_FP32_Turn3* alternative, const BGC_FP32_Turn3* turn) { bgc_fp32_quaternion_get_reverse(&alternative->_versor, &turn->_versor); } inline void bgc_fp64_turn3_get_alternative(BGC_FP64_Turn3* alternative, const BGC_FP64_Turn3* turn) { bgc_fp64_quaternion_get_reverse(&alternative->_versor, &turn->_versor); } // =================== Revert =================== // inline void bgc_fp32_turn3_revert(BGC_FP32_Turn3* turn) { bgc_fp32_quaternion_conjugate(&turn->_versor); } inline void bgc_fp64_turn3_revert(BGC_FP64_Turn3* turn) { bgc_fp64_quaternion_conjugate(&turn->_versor); } inline void bgc_fp32_turn3_get_reverse(BGC_FP32_Turn3* inverse, const BGC_FP32_Turn3* turn) { bgc_fp32_quaternion_get_conjugate(&inverse->_versor, &turn->_versor); } inline void bgc_fp64_turn3_get_reverse(BGC_FP64_Turn3* inverse, const BGC_FP64_Turn3* turn) { bgc_fp64_quaternion_get_conjugate(&inverse->_versor, &turn->_versor); } // =============== Get Exponation =============== // void bgc_fp32_turn3_get_exponation(BGC_FP32_Turn3* power, const BGC_FP32_Turn3* base, const float exponent); void bgc_fp64_turn3_get_exponation(BGC_FP64_Turn3* power, const BGC_FP64_Turn3* base, const double exponent); // ================ Combination ================= // inline void bgc_fp32_turn3_combine(BGC_FP32_Turn3* combination, const BGC_FP32_Turn3* first, const BGC_FP32_Turn3* second) { bgc_fp32_quaternion_multiply_by_quaternion(&combination->_versor, &second->_versor, &first->_versor); const float square_modulus = bgc_fp32_quaternion_get_square_modulus(&combination->_versor); if (!bgc_fp32_is_square_unit(square_modulus)) { _bgc_fp32_turn3_normalize(combination, square_modulus); } } inline void bgc_fp64_turn3_combine(BGC_FP64_Turn3* combination, const BGC_FP64_Turn3* first, const BGC_FP64_Turn3* second) { bgc_fp64_quaternion_multiply_by_quaternion(&combination->_versor, &second->_versor, &first->_versor); const double square_modulus = bgc_fp64_quaternion_get_square_modulus(&combination->_versor); if (!bgc_fp64_is_square_unit(square_modulus)) { _bgc_fp64_turn3_normalize(combination, square_modulus); } } // ============ Combination of three ============ // inline void bgc_fp32_turn3_combine3(BGC_FP32_Turn3* combination, const BGC_FP32_Turn3* first, const BGC_FP32_Turn3* second, const BGC_FP32_Turn3* third) { BGC_FP32_Quaternion product; bgc_fp32_quaternion_multiply_by_quaternion(&product, &second->_versor, &first->_versor); bgc_fp32_quaternion_multiply_by_quaternion(&combination->_versor, &third->_versor, &product); const float square_modulus = bgc_fp32_quaternion_get_square_modulus(&combination->_versor); if (!bgc_fp32_is_square_unit(square_modulus)) { _bgc_fp32_turn3_normalize(combination, square_modulus); } } inline void bgc_fp64_turn3_combine3(BGC_FP64_Turn3* combination, const BGC_FP64_Turn3* first, const BGC_FP64_Turn3* second, const BGC_FP64_Turn3* third) { BGC_FP64_Quaternion product; bgc_fp64_quaternion_multiply_by_quaternion(&product, &second->_versor, &first->_versor); bgc_fp64_quaternion_multiply_by_quaternion(&combination->_versor, &third->_versor, &product); const double square_modulus = bgc_fp64_quaternion_get_square_modulus(&combination->_versor); if (!bgc_fp64_is_square_unit(square_modulus)) { _bgc_fp64_turn3_normalize(combination, square_modulus); } } // ================= Exclusion ================== // inline void bgc_fp32_turn3_exclude(BGC_FP32_Turn3* difference, const BGC_FP32_Turn3* base, const BGC_FP32_Turn3* excludant) { bgc_fp32_quaternion_multiply_by_conjugate(&difference->_versor, &base->_versor, &excludant->_versor); const float square_modulus = bgc_fp32_quaternion_get_square_modulus(&difference->_versor); if (!bgc_fp32_is_square_unit(square_modulus)) { _bgc_fp32_turn3_normalize(difference, square_modulus); } } inline void bgc_fp64_turn3_exclude(BGC_FP64_Turn3* difference, const BGC_FP64_Turn3* base, const BGC_FP64_Turn3* excludant) { bgc_fp64_quaternion_multiply_by_conjugate(&difference->_versor, &base->_versor, &excludant->_versor); const double square_modulus = bgc_fp64_quaternion_get_square_modulus(&difference->_versor); if (!bgc_fp64_is_square_unit(square_modulus)) { _bgc_fp64_turn3_normalize(difference, square_modulus); } } // ============ Sphere Interpolation ============ // void bgc_fp32_turn3_spherically_interpolate(BGC_FP32_Turn3* interpolation, const BGC_FP32_Turn3* start, const BGC_FP32_Turn3* end, const float phase); void bgc_fp64_turn3_spherically_interpolate(BGC_FP64_Turn3* interpolation, const BGC_FP64_Turn3* start, const BGC_FP64_Turn3* end, const double phase); // ============ Get Rotation Matrix ============= // inline void bgc_fp32_turn3_get_rotation_matrix(BGC_FP32_Matrix3x3* matrix, const BGC_FP32_Turn3* turn) { const float s0s0 = turn->_versor.s0 * turn->_versor.s0; const float x1x1 = turn->_versor.x1 * turn->_versor.x1; const float x2x2 = turn->_versor.x2 * turn->_versor.x2; const float x3x3 = turn->_versor.x3 * turn->_versor.x3; const float s0x1 = turn->_versor.s0 * turn->_versor.x1; const float s0x2 = turn->_versor.s0 * turn->_versor.x2; const float s0x3 = turn->_versor.s0 * turn->_versor.x3; const float x1x2 = turn->_versor.x1 * turn->_versor.x2; const float x1x3 = turn->_versor.x1 * turn->_versor.x3; const float x2x3 = turn->_versor.x2 * turn->_versor.x3; matrix->r1c1 = ((s0s0 + x1x1) - (x2x2 + x3x3)); matrix->r2c2 = ((s0s0 + x2x2) - (x1x1 + x3x3)); matrix->r3c3 = ((s0s0 + x3x3) - (x1x1 + x2x2)); matrix->r1c2 = 2.0f * (x1x2 - s0x3); matrix->r2c3 = 2.0f * (x2x3 - s0x1); matrix->r3c1 = 2.0f * (x1x3 - s0x2); matrix->r2c1 = 2.0f * (x1x2 + s0x3); matrix->r3c2 = 2.0f * (x2x3 + s0x1); matrix->r1c3 = 2.0f * (x1x3 + s0x2); } inline void bgc_fp64_turn3_get_rotation_matrix(BGC_FP64_Matrix3x3* matrix, const BGC_FP64_Turn3* turn) { const double s0s0 = turn->_versor.s0 * turn->_versor.s0; const double x1x1 = turn->_versor.x1 * turn->_versor.x1; const double x2x2 = turn->_versor.x2 * turn->_versor.x2; const double x3x3 = turn->_versor.x3 * turn->_versor.x3; const double s0x1 = turn->_versor.s0 * turn->_versor.x1; const double s0x2 = turn->_versor.s0 * turn->_versor.x2; const double s0x3 = turn->_versor.s0 * turn->_versor.x3; const double x1x2 = turn->_versor.x1 * turn->_versor.x2; const double x1x3 = turn->_versor.x1 * turn->_versor.x3; const double x2x3 = turn->_versor.x2 * turn->_versor.x3; matrix->r1c1 = ((s0s0 + x1x1) - (x2x2 + x3x3)); matrix->r2c2 = ((s0s0 + x2x2) - (x1x1 + x3x3)); matrix->r3c3 = ((s0s0 + x3x3) - (x1x1 + x2x2)); matrix->r1c2 = 2.0 * (x1x2 - s0x3); matrix->r2c3 = 2.0 * (x2x3 - s0x1); matrix->r3c1 = 2.0 * (x1x3 - s0x2); matrix->r2c1 = 2.0 * (x1x2 + s0x3); matrix->r3c2 = 2.0 * (x2x3 + s0x1); matrix->r1c3 = 2.0 * (x1x3 + s0x2); } // ============= Get Reverse Matrix ============= // inline void bgc_fp32_turn3_get_reverse_matrix(BGC_FP32_Matrix3x3* matrix, const BGC_FP32_Turn3* turn) { const float s0s0 = turn->_versor.s0 * turn->_versor.s0; const float x1x1 = turn->_versor.x1 * turn->_versor.x1; const float x2x2 = turn->_versor.x2 * turn->_versor.x2; const float x3x3 = turn->_versor.x3 * turn->_versor.x3; const float s0x1 = turn->_versor.s0 * turn->_versor.x1; const float s0x2 = turn->_versor.s0 * turn->_versor.x2; const float s0x3 = turn->_versor.s0 * turn->_versor.x3; const float x1x2 = turn->_versor.x1 * turn->_versor.x2; const float x1x3 = turn->_versor.x1 * turn->_versor.x3; const float x2x3 = turn->_versor.x2 * turn->_versor.x3; matrix->r1c1 = ((s0s0 + x1x1) - (x2x2 + x3x3)); matrix->r2c2 = ((s0s0 + x2x2) - (x1x1 + x3x3)); matrix->r3c3 = ((s0s0 + x3x3) - (x1x1 + x2x2)); matrix->r1c2 = 2.0f * (x1x2 + s0x3); matrix->r2c3 = 2.0f * (x2x3 + s0x1); matrix->r3c1 = 2.0f * (x1x3 + s0x2); matrix->r2c1 = 2.0f * (x1x2 - s0x3); matrix->r3c2 = 2.0f * (x2x3 - s0x1); matrix->r1c3 = 2.0f * (x1x3 - s0x2); } inline void bgc_fp64_turn3_get_reverse_matrix(BGC_FP64_Matrix3x3* matrix, const BGC_FP64_Turn3* turn) { const double s0s0 = turn->_versor.s0 * turn->_versor.s0; const double x1x1 = turn->_versor.x1 * turn->_versor.x1; const double x2x2 = turn->_versor.x2 * turn->_versor.x2; const double x3x3 = turn->_versor.x3 * turn->_versor.x3; const double s0x1 = turn->_versor.s0 * turn->_versor.x1; const double s0x2 = turn->_versor.s0 * turn->_versor.x2; const double s0x3 = turn->_versor.s0 * turn->_versor.x3; const double x1x2 = turn->_versor.x1 * turn->_versor.x2; const double x1x3 = turn->_versor.x1 * turn->_versor.x3; const double x2x3 = turn->_versor.x2 * turn->_versor.x3; matrix->r1c1 = ((s0s0 + x1x1) - (x2x2 + x3x3)); matrix->r2c2 = ((s0s0 + x2x2) - (x1x1 + x3x3)); matrix->r3c3 = ((s0s0 + x3x3) - (x1x1 + x2x2)); matrix->r1c2 = 2.0 * (x1x2 + s0x3); matrix->r2c3 = 2.0 * (x2x3 + s0x1); matrix->r3c1 = 2.0 * (x1x3 + s0x2); matrix->r2c1 = 2.0 * (x1x2 - s0x3); matrix->r3c2 = 2.0 * (x2x3 - s0x1); matrix->r1c3 = 2.0 * (x1x3 - s0x2); } // ============= Get Both Matrixes ============== // inline void bgc_fp32_turn3_get_both_matrices(BGC_FP32_Matrix3x3* rotation, BGC_FP32_Matrix3x3* reverse, const BGC_FP32_Turn3* turn) { bgc_fp32_turn3_get_reverse_matrix(reverse, turn); bgc_fp32_matrix3x3_get_transposed(rotation, reverse); } inline void bgc_fp64_turn3_get_both_matrices(BGC_FP64_Matrix3x3* rotation, BGC_FP64_Matrix3x3* reverse, const BGC_FP64_Turn3* turn) { bgc_fp64_turn3_get_reverse_matrix(reverse, turn); bgc_fp64_matrix3x3_get_transposed(rotation, reverse); } // ================ Turn Vector ================= // inline void bgc_fp32_turn3_vector(BGC_FP32_Vector3* turned_vector, const BGC_FP32_Turn3* turn, const BGC_FP32_Vector3* original_vector) { _bgc_fp32_quaternion_turn_vector_roughly(turned_vector, &turn->_versor, original_vector); } inline void bgc_fp64_turn3_vector(BGC_FP64_Vector3* turned_vector, const BGC_FP64_Turn3* turn, const BGC_FP64_Vector3* original_vector) { _bgc_fp64_quaternion_turn_vector_roughly(turned_vector, &turn->_versor, original_vector); } // ============== Turn Vector Back ============== // inline void bgc_fp32_turn3_vector_back(BGC_FP32_Vector3* turned_vector, const BGC_FP32_Turn3* turn, const BGC_FP32_Vector3* original_vector) { _bgc_fp32_quaternion_turn_vector_back_roughly(turned_vector, &turn->_versor, original_vector); } inline void bgc_fp64_turn3_vector_back(BGC_FP64_Vector3* turned_vector, const BGC_FP64_Turn3* turn, const BGC_FP64_Vector3* original_vector) { _bgc_fp64_quaternion_turn_vector_back_roughly(turned_vector, &turn->_versor, original_vector); } // ================== Are Close ================= // inline int bgc_fp32_turn3_are_close(const BGC_FP32_Turn3* turn1, const BGC_FP32_Turn3* turn2) { BGC_FP32_Quaternion difference; bgc_fp32_quaternion_subtract(&difference, &turn1->_versor, &turn2->_versor); return bgc_fp32_quaternion_get_square_modulus(&difference) <= BGC_FP32_SQUARE_EPSILON; } inline int bgc_fp64_turn3_are_close(const BGC_FP64_Turn3* turn1, const BGC_FP64_Turn3* turn2) { BGC_FP64_Quaternion difference; bgc_fp64_quaternion_subtract(&difference, &turn1->_versor, &turn2->_versor); return bgc_fp64_quaternion_get_square_modulus(&difference) <= BGC_FP64_SQUARE_EPSILON; } #endif