462 lines
16 KiB
C
462 lines
16 KiB
C
#ifndef _BGC_TURN3_H_INCLUDED_
|
|
#define _BGC_TURN3_H_INCLUDED_
|
|
|
|
#include <stdint.h>
|
|
#include <math.h>
|
|
|
|
#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* const turn)
|
|
{
|
|
turn->_versor.s = 1.0f;
|
|
turn->_versor.x = 0.0f;
|
|
turn->_versor.y = 0.0f;
|
|
turn->_versor.z = 0.0f;
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_reset(BGC_FP64_Turn3* const turn)
|
|
{
|
|
turn->_versor.s = 1.0;
|
|
turn->_versor.x = 0.0;
|
|
turn->_versor.y = 0.0;
|
|
turn->_versor.z = 0.0;
|
|
}
|
|
|
|
// ============= Private: Normalize ============= //
|
|
|
|
inline void _bgc_fp32_turn3_normalize(BGC_FP32_Turn3* const turn)
|
|
{
|
|
const float square_magnitude = bgc_fp32_quaternion_get_square_magnitude(&turn->_versor);
|
|
|
|
if (bgc_fp32_is_square_unit(square_magnitude)) {
|
|
return;
|
|
}
|
|
|
|
if (square_magnitude <= BGC_FP32_SQUARE_EPSILON || isnan(square_magnitude)) {
|
|
turn->_versor.s = 1.0f;
|
|
turn->_versor.x = 0.0f;
|
|
turn->_versor.y = 0.0f;
|
|
turn->_versor.z = 0.0f;
|
|
return;
|
|
}
|
|
|
|
const float multiplier = sqrtf(1.0f / square_magnitude);
|
|
|
|
turn->_versor.s *= multiplier;
|
|
turn->_versor.x *= multiplier;
|
|
turn->_versor.y *= multiplier;
|
|
turn->_versor.z *= multiplier;
|
|
}
|
|
|
|
inline void _bgc_fp64_turn3_normalize(BGC_FP64_Turn3* const turn)
|
|
{
|
|
const double square_magnitude = bgc_fp64_quaternion_get_square_magnitude(&turn->_versor);
|
|
|
|
if (bgc_fp64_is_square_unit(square_magnitude)) {
|
|
return;
|
|
}
|
|
|
|
if (square_magnitude <= BGC_FP64_SQUARE_EPSILON || isnan(square_magnitude)) {
|
|
turn->_versor.s = 1.0;
|
|
turn->_versor.x = 0.0;
|
|
turn->_versor.y = 0.0;
|
|
turn->_versor.z = 0.0;
|
|
return;
|
|
}
|
|
|
|
const double multiplier = sqrt(1.0 / square_magnitude);
|
|
|
|
turn->_versor.s *= multiplier;
|
|
turn->_versor.x *= multiplier;
|
|
turn->_versor.y *= multiplier;
|
|
turn->_versor.z *= multiplier;
|
|
}
|
|
|
|
// ================= Set Values ================= //
|
|
|
|
inline void bgc_fp32_turn3_set_values(BGC_FP32_Turn3* const turn, const float s, const float x, const float y, const float z)
|
|
{
|
|
turn->_versor.s = s;
|
|
turn->_versor.x = x;
|
|
turn->_versor.y = y;
|
|
turn->_versor.z = z;
|
|
|
|
_bgc_fp32_turn3_normalize(turn);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_set_values(BGC_FP64_Turn3* const turn, const double s, const double x, const double y, const double z)
|
|
{
|
|
turn->_versor.s = s;
|
|
turn->_versor.x = x;
|
|
turn->_versor.y = y;
|
|
turn->_versor.z = z;
|
|
|
|
_bgc_fp64_turn3_normalize(turn);
|
|
}
|
|
|
|
// =============== Get Quaternion =============== //
|
|
|
|
inline void bgc_fp32_turn3_get_quaternion(BGC_FP32_Quaternion* const quaternion, const BGC_FP32_Turn3* const turn)
|
|
{
|
|
bgc_fp32_quaternion_copy(quaternion, &turn->_versor);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_get_quaternion(BGC_FP64_Quaternion* const quaternion, const BGC_FP64_Turn3* const turn)
|
|
{
|
|
bgc_fp64_quaternion_copy(quaternion, &turn->_versor);
|
|
}
|
|
|
|
// =============== Set Quaternion =============== //
|
|
|
|
inline void bgc_fp32_turn3_set_quaternion(BGC_FP32_Turn3* const turn, const BGC_FP32_Quaternion* const quaternion)
|
|
{
|
|
bgc_fp32_quaternion_copy(&turn->_versor, quaternion);
|
|
_bgc_fp32_turn3_normalize(turn);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_set_quaternion(BGC_FP64_Turn3* const turn, const BGC_FP64_Quaternion* const quaternion)
|
|
{
|
|
bgc_fp64_quaternion_copy(&turn->_versor, quaternion);
|
|
_bgc_fp64_turn3_normalize(turn);
|
|
}
|
|
|
|
// ================ Get Rotation ================ //
|
|
|
|
float bgc_fp32_turn3_get_rotation(BGC_FP32_Vector3* const axis, const BGC_FP32_Turn3* const turn, const int angle_unit);
|
|
|
|
double bgc_fp64_turn3_get_rotation(BGC_FP64_Vector3* const axis, const BGC_FP64_Turn3* const turn, const int angle_unit);
|
|
|
|
// ================ Set Rotation ================ //
|
|
|
|
void bgc_fp32_turn3_set_rotation(BGC_FP32_Turn3* const turn, const float x, const float y, const float z, const float angle, const int angle_unit);
|
|
|
|
void bgc_fp64_turn3_set_rotation(BGC_FP64_Turn3* const turn, const double x, const double y, const double z, const double angle, const int angle_unit);
|
|
|
|
// ========= Find Direction Difference ========== //
|
|
|
|
int bgc_fp32_turn3_find_direction_difference(BGC_FP32_Turn3* const turn, const BGC_FP32_Vector3* const start, const BGC_FP32_Vector3* const end);
|
|
|
|
int bgc_fp64_turn3_find_direction_difference(BGC_FP64_Turn3* const turn, const BGC_FP64_Vector3* const start, const BGC_FP64_Vector3* const end);
|
|
|
|
// ======= Find Direction Pair Difference ======= //
|
|
|
|
int bgc_fp32_turn3_find_pair_difference(
|
|
BGC_FP32_Turn3* const turn,
|
|
const BGC_FP32_Vector3* const first_pair_main,
|
|
const BGC_FP32_Vector3* const first_pair_branch,
|
|
const BGC_FP32_Vector3* const second_pair_main,
|
|
const BGC_FP32_Vector3* const second_pair_branch
|
|
);
|
|
|
|
int bgc_fp64_turn3_find_pair_difference(
|
|
BGC_FP64_Turn3* const turn,
|
|
const BGC_FP64_Vector3* const first_pair_main,
|
|
const BGC_FP64_Vector3* const first_pair_branch,
|
|
const BGC_FP64_Vector3* const second_pair_main,
|
|
const BGC_FP64_Vector3* const second_pair_branch
|
|
);
|
|
|
|
// ==================== Copy ==================== //
|
|
|
|
inline void bgc_fp32_turn3_copy(BGC_FP32_Turn3* const destination, const BGC_FP32_Turn3* const source)
|
|
{
|
|
bgc_fp32_quaternion_copy(&destination->_versor, &source->_versor);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_copy(BGC_FP64_Turn3* const destination, const BGC_FP64_Turn3* const source)
|
|
{
|
|
bgc_fp64_quaternion_copy(&destination->_versor, &source->_versor);
|
|
}
|
|
|
|
// ==================== Swap ==================== //
|
|
|
|
inline void bgc_fp32_turn3_swap(BGC_FP32_Turn3* const turn1, BGC_FP32_Turn3* const turn2)
|
|
{
|
|
bgc_fp32_quaternion_swap(&turn1->_versor, &turn2->_versor);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_swap(BGC_FP64_Turn3* const turn1, BGC_FP64_Turn3* const turn2)
|
|
{
|
|
bgc_fp64_quaternion_swap(&turn1->_versor, &turn2->_versor);
|
|
}
|
|
|
|
// ================= Comparison ================= //
|
|
|
|
inline int bgc_fp32_turn3_is_idle(const BGC_FP32_Turn3* const turn)
|
|
{
|
|
return bgc_fp32_quaternion_is_real(&turn->_versor);
|
|
}
|
|
|
|
inline int bgc_fp64_turn3_is_idle(const BGC_FP64_Turn3* const turn)
|
|
{
|
|
return bgc_fp64_quaternion_is_real(&turn->_versor);
|
|
}
|
|
|
|
// ================== Convert =================== //
|
|
|
|
inline void bgc_fp32_turn3_convert_to_fp64(BGC_FP64_Turn3* const destination, const BGC_FP32_Turn3* const source)
|
|
{
|
|
bgc_fp32_quaternion_convert_to_fp64(&destination->_versor, &source->_versor);
|
|
_bgc_fp64_turn3_normalize(destination);
|
|
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_convert_to_fp32(BGC_FP32_Turn3* const destination, const BGC_FP64_Turn3* const source)
|
|
{
|
|
bgc_fp64_quaternion_convert_to_fp32(&destination->_versor, &source->_versor);
|
|
_bgc_fp32_turn3_normalize(destination);
|
|
}
|
|
|
|
// ================== Shorten =================== //
|
|
|
|
inline void bgc_fp32_turn3_shorten(BGC_FP32_Turn3* const turn)
|
|
{
|
|
if (turn->_versor.s < 0.0f) {
|
|
bgc_fp32_quaternion_revert(&turn->_versor);
|
|
}
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_shorten(BGC_FP64_Turn3* const turn)
|
|
{
|
|
if (turn->_versor.s < 0.0) {
|
|
bgc_fp64_quaternion_revert(&turn->_versor);
|
|
}
|
|
}
|
|
|
|
inline void bgc_fp32_turn3_get_shortened(BGC_FP32_Turn3* const shortened, const BGC_FP32_Turn3* const turn)
|
|
{
|
|
if (turn->_versor.s >= 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* const shortened, const BGC_FP64_Turn3* const turn)
|
|
{
|
|
if (turn->_versor.s >= 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* const turn)
|
|
{
|
|
bgc_fp32_quaternion_revert(&turn->_versor);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_alternate(BGC_FP64_Turn3* const turn)
|
|
{
|
|
bgc_fp64_quaternion_revert(&turn->_versor);
|
|
}
|
|
|
|
inline void bgc_fp32_turn3_get_alternative(BGC_FP32_Turn3* const alternative, const BGC_FP32_Turn3* const turn)
|
|
{
|
|
bgc_fp32_quaternion_get_reverse(&alternative->_versor, &turn->_versor);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_get_alternative(BGC_FP64_Turn3* const alternative, const BGC_FP64_Turn3* const turn)
|
|
{
|
|
bgc_fp64_quaternion_get_reverse(&alternative->_versor, &turn->_versor);
|
|
}
|
|
|
|
// =================== Revert =================== //
|
|
|
|
inline void bgc_fp32_turn3_revert(BGC_FP32_Turn3* const turn)
|
|
{
|
|
bgc_fp32_quaternion_conjugate(&turn->_versor);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_revert(BGC_FP64_Turn3* const turn)
|
|
{
|
|
bgc_fp64_quaternion_conjugate(&turn->_versor);
|
|
}
|
|
|
|
inline void bgc_fp32_turn3_get_reverse(BGC_FP32_Turn3* const inverse, const BGC_FP32_Turn3* const turn)
|
|
{
|
|
bgc_fp32_quaternion_get_conjugate(&inverse->_versor, &turn->_versor);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_get_reverse(BGC_FP64_Turn3* const inverse, const BGC_FP64_Turn3* const turn)
|
|
{
|
|
bgc_fp64_quaternion_get_conjugate(&inverse->_versor, &turn->_versor);
|
|
}
|
|
|
|
// =============== Get Exponation =============== //
|
|
|
|
void bgc_fp32_turn3_get_power(BGC_FP32_Turn3* const power, const BGC_FP32_Turn3* const base, const float exponent);
|
|
|
|
void bgc_fp64_turn3_get_power(BGC_FP64_Turn3* const power, const BGC_FP64_Turn3* const base, const double exponent);
|
|
|
|
// ================ Combination ================= //
|
|
|
|
inline void bgc_fp32_turn3_combine(BGC_FP32_Turn3* const combination, const BGC_FP32_Turn3* const first, const BGC_FP32_Turn3* const second)
|
|
{
|
|
bgc_fp32_quaternion_multiply_by_quaternion(&combination->_versor, &second->_versor, &first->_versor);
|
|
_bgc_fp32_turn3_normalize(combination);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_combine(BGC_FP64_Turn3* const combination, const BGC_FP64_Turn3* const first, const BGC_FP64_Turn3* const second)
|
|
{
|
|
bgc_fp64_quaternion_multiply_by_quaternion(&combination->_versor, &second->_versor, &first->_versor);
|
|
_bgc_fp64_turn3_normalize(combination);
|
|
}
|
|
|
|
// ============ Combination of three ============ //
|
|
|
|
inline void bgc_fp32_turn3_combine3(BGC_FP32_Turn3* const combination, const BGC_FP32_Turn3* const first, const BGC_FP32_Turn3* const second, const BGC_FP32_Turn3* const 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);
|
|
|
|
_bgc_fp32_turn3_normalize(combination);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_combine3(BGC_FP64_Turn3* const combination, const BGC_FP64_Turn3* const first, const BGC_FP64_Turn3* const second, const BGC_FP64_Turn3* const 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);
|
|
|
|
_bgc_fp64_turn3_normalize(combination);
|
|
}
|
|
|
|
// ================= Exclusion ================== //
|
|
|
|
inline void bgc_fp32_turn3_exclude(BGC_FP32_Turn3* const difference, const BGC_FP32_Turn3* const base, const BGC_FP32_Turn3* const excludant)
|
|
{
|
|
bgc_fp32_quaternion_multiply_by_conjugate(&difference->_versor, &base->_versor, &excludant->_versor);
|
|
|
|
_bgc_fp32_turn3_normalize(difference);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_exclude(BGC_FP64_Turn3* const difference, const BGC_FP64_Turn3* const base, const BGC_FP64_Turn3* const excludant)
|
|
{
|
|
bgc_fp64_quaternion_multiply_by_conjugate(&difference->_versor, &base->_versor, &excludant->_versor);
|
|
|
|
_bgc_fp64_turn3_normalize(difference);
|
|
}
|
|
|
|
// ============ Sphere Interpolation ============ //
|
|
|
|
void bgc_fp32_turn3_spherically_interpolate(BGC_FP32_Turn3* const interpolation, const BGC_FP32_Turn3* const start, const BGC_FP32_Turn3* const end, const float phase);
|
|
|
|
void bgc_fp64_turn3_spherically_interpolate(BGC_FP64_Turn3* const interpolation, const BGC_FP64_Turn3* const start, const BGC_FP64_Turn3* const end, const double phase);
|
|
|
|
// ============ Get Rotation Matrix ============= //
|
|
|
|
inline void bgc_fp32_turn3_get_rotation_matrix(BGC_FP32_Matrix3x3* const matrix, const BGC_FP32_Turn3* const turn)
|
|
{
|
|
_bgc_fp32_versor_get_rotation_matrix(matrix, &turn->_versor);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_get_rotation_matrix(BGC_FP64_Matrix3x3* const matrix, const BGC_FP64_Turn3* const turn)
|
|
{
|
|
_bgc_fp64_versor_get_rotation_matrix(matrix, &turn->_versor);
|
|
}
|
|
|
|
// ============= Get Reverse Matrix ============= //
|
|
|
|
inline void bgc_fp32_turn3_get_reverse_matrix(BGC_FP32_Matrix3x3* const matrix, const BGC_FP32_Turn3* const turn)
|
|
{
|
|
_bgc_fp32_versor_get_reverse_matrix(matrix, &turn->_versor);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_get_reverse_matrix(BGC_FP64_Matrix3x3* const matrix, const BGC_FP64_Turn3* const turn)
|
|
{
|
|
_bgc_fp64_versor_get_reverse_matrix(matrix, &turn->_versor);
|
|
}
|
|
|
|
// ============= Get Both Matrixes ============== //
|
|
|
|
inline void bgc_fp32_turn3_get_both_matrices(BGC_FP32_Matrix3x3* const rotation, BGC_FP32_Matrix3x3* const reverse, const BGC_FP32_Turn3* const turn)
|
|
{
|
|
_bgc_fp32_versor_get_reverse_matrix(reverse, &turn->_versor);
|
|
bgc_fp32_matrix3x3_get_transposed(rotation, reverse);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_get_both_matrices(BGC_FP64_Matrix3x3* const rotation, BGC_FP64_Matrix3x3* const reverse, const BGC_FP64_Turn3* const turn)
|
|
{
|
|
_bgc_fp64_versor_get_reverse_matrix(reverse, &turn->_versor);
|
|
bgc_fp64_matrix3x3_get_transposed(rotation, reverse);
|
|
}
|
|
|
|
// ================ Turn Vector ================= //
|
|
|
|
inline void bgc_fp32_turn3_vector(BGC_FP32_Vector3* const turned_vector, const BGC_FP32_Turn3* const turn, const BGC_FP32_Vector3* const original_vector)
|
|
{
|
|
_bgc_fp32_versor_turn_vector(turned_vector, &turn->_versor, original_vector);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_vector(BGC_FP64_Vector3* const turned_vector, const BGC_FP64_Turn3* const turn, const BGC_FP64_Vector3* const original_vector)
|
|
{
|
|
_bgc_fp64_versor_turn_vector(turned_vector, &turn->_versor, original_vector);
|
|
}
|
|
|
|
// ============== Turn Vector Back ============== //
|
|
|
|
inline void bgc_fp32_turn3_vector_back(BGC_FP32_Vector3* const turned_vector, const BGC_FP32_Turn3* const turn, const BGC_FP32_Vector3* const original_vector)
|
|
{
|
|
_bgc_fp32_versor_turn_vector_back(turned_vector, &turn->_versor, original_vector);
|
|
}
|
|
|
|
inline void bgc_fp64_turn3_vector_back(BGC_FP64_Vector3* const turned_vector, const BGC_FP64_Turn3* const turn, const BGC_FP64_Vector3* const original_vector)
|
|
{
|
|
_bgc_fp64_versor_turn_vector_back(turned_vector, &turn->_versor, original_vector);
|
|
}
|
|
|
|
// ================== Are Close ================= //
|
|
|
|
inline int bgc_fp32_turn3_are_close(const BGC_FP32_Turn3* const turn1, const BGC_FP32_Turn3* const turn2)
|
|
{
|
|
BGC_FP32_Quaternion difference;
|
|
bgc_fp32_quaternion_subtract(&difference, &turn1->_versor, &turn2->_versor);
|
|
return bgc_fp32_quaternion_get_square_magnitude(&difference) <= BGC_FP32_SQUARE_EPSILON;
|
|
}
|
|
|
|
inline int bgc_fp64_turn3_are_close(const BGC_FP64_Turn3* const turn1, const BGC_FP64_Turn3* const turn2)
|
|
{
|
|
BGC_FP64_Quaternion difference;
|
|
bgc_fp64_quaternion_subtract(&difference, &turn1->_versor, &turn2->_versor);
|
|
return bgc_fp64_quaternion_get_square_magnitude(&difference) <= BGC_FP64_SQUARE_EPSILON;
|
|
}
|
|
|
|
#endif
|