bgc-c/basic-geometry/versor.c

102 lines
3.2 KiB
C

#include <math.h>
#include "angle.h"
#include "versor.h"
const bgc_versor_fp32_t BGC_IDLE_VERSOR_FP32 = { 1.0f, 0.0f, 0.0f, 0.0f };
const bgc_versor_fp64_t BGC_IDLE_VERSOR_FP64 = { 1.0, 0.0, 0.0, 0.0 };
// =============== Set Crude Turn =============== //
void bgc_versor_set_crude_turn_fp32(const float x1, const float x2, const float x3, const float angle, const bgc_angle_unit_t unit, bgc_versor_fp32_t* result)
{
const float square_vector = x1 * x1 + x2 * x2 + x3 * x3;
if (square_vector <= BGC_SQUARE_EPSYLON_FP32) {
bgc_versor_reset_fp32(result);
return;
}
const float half_angle = bgc_angle_to_radians_fp32(0.5f * angle, unit);
const float sine = sinf(half_angle);
if (-BGC_EPSYLON_FP32 <= sine && sine <= BGC_EPSYLON_FP32) {
bgc_versor_reset_fp32(result);
return;
}
const float multiplier = sine / sqrtf(square_vector);
bgc_versor_set_values_fp32(cosf(half_angle), x1 * multiplier, x2 * multiplier, x3 * multiplier, result);
}
void bgc_versor_set_crude_turn_fp64(const double x1, const double x2, const double x3, const double angle, const bgc_angle_unit_t unit, bgc_versor_fp64_t* result)
{
const double square_vector = x1 * x1 + x2 * x2 + x3 * x3;
if (square_vector <= BGC_SQUARE_EPSYLON_FP64) {
bgc_versor_reset_fp64(result);
return;
}
const double half_angle = bgc_angle_to_radians_fp64(0.5 * angle, unit);
const double sine = sin(half_angle);
if (-BGC_EPSYLON_FP64 <= sine && sine <= BGC_EPSYLON_FP64) {
bgc_versor_reset_fp64(result);
return;
}
const double multiplier = sine / sqrt(square_vector);
bgc_versor_set_values_fp64(cos(half_angle), x1 * multiplier, x2 * multiplier, x3 * multiplier, result);
}
// ================= Rotation3 ================== //
void bgc_versor_get_rotation_fp32(const bgc_versor_fp32_t* versor, bgc_rotation3_fp32_t* result)
{
if (versor == 0 || result == 0) {
return;
}
if (versor->s0 <= -(1.0f - BGC_EPSYLON_FP32) || 1.0f - BGC_EPSYLON_FP32 <= versor->s0) {
bgc_rotation3_reset_fp32(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 bgc_versor_get_rotation_fp64(const bgc_versor_fp64_t* versor, bgc_rotation3_fp64_t* result)
{
if (versor == 0 || result == 0) {
return;
}
if (versor->s0 <= -(1.0 - BGC_EPSYLON_FP64) || 1.0 - BGC_EPSYLON_FP64 <= versor->s0) {
bgc_rotation3_reset_fp64(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;
}