Приаведение тангенса (tangent) к единому виду с другими сущностями, добавлено комплексное произведение двумерных векторов

This commit is contained in:
Andrey Pokidov 2024-12-25 13:37:23 +07:00
parent 896c8615f5
commit fcbec62024
5 changed files with 182 additions and 114 deletions

View file

@ -93,7 +93,7 @@ BgFP32Versor * make_random_versors(const unsigned int amount)
void print_versor(const BgFP32Versor* versor)
{
printf("Versor (%f, %f, %f, %f); Delta = %e\n", versor->s0, versor->x1, versor->x2, versor->x3, bg_fp32_versor_get_modulus(versor) - 1.0f);
printf("Versor (%f, %f, %f, %f)\n", versor->s0, versor->x1, versor->x2, versor->x3);
}
void print_vector(const BgFP32Vector3* vector)

View file

@ -74,6 +74,10 @@
<Option compilerVar="CC" />
</Unit>
<Unit filename="rotation3.h" />
<Unit filename="tangent.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="tangent.h" />
<Unit filename="vector2.c">
<Option compilerVar="CC" />
</Unit>

View file

@ -8,16 +8,30 @@
#include "vector2.h"
#include "matrix2x2.h"
// =================== Types ==================== //
typedef struct
{
float _cos, _sin;
const float cos, sin;
} BgFP32Tangent;
typedef struct
{
double _cos, _sin;
const double cos, sin;
} BgFP64Tangent;
// ================= Dark Twins ================= //
typedef struct {
float cos, sin;
} __BgFP32DarkTwinTangent;
typedef struct {
double cos, sin;
} __BgFP64DarkTwinTangent;
// ================= Constants ================== //
extern const BgFP32Tangent BG_FP32_IDLE_TANGENT;
extern const BgFP64Tangent BG_FP64_IDLE_TANGENT;
@ -25,212 +39,266 @@ extern const BgFP64Tangent BG_FP64_IDLE_TANGENT;
static inline void bg_fp32_tangent_reset(BgFP32Tangent* tangent)
{
tangent->_cos = 1.0f;
tangent->_sin = 0.0f;
__BgFP32DarkTwinTangent* twin = (__BgFP32DarkTwinTangent*)tangent;
twin->cos = 1.0f;
twin->sin = 0.0f;
}
static inline void bg_fp64_tangent_reset(BgFP64Tangent* tangent)
{
tangent->_cos = 1.0;
tangent->_sin = 0.0;
__BgFP64DarkTwinTangent* twin = (__BgFP64DarkTwinTangent*)tangent;
twin->cos = 1.0;
twin->sin = 0.0;
}
// ==================== Copy ==================== //
static inline void bg_fp32_tangent_copy(const BgFP32Tangent* from, BgFP32Tangent* to)
{
to->_cos = from->_cos;
to->_sin = from->_sin;
}
static inline void bg_fp64_tangent_copy(const BgFP64Tangent* from, BgFP64Tangent* to)
{
to->_cos = from->_cos;
to->_sin = from->_sin;
}
// ==================== Make ==================== //
// ==================== Set ===================== //
static inline void bg_fp32_tangent_set_values(const float x1, const float x2, BgFP32Tangent* tangent)
{
const float square_module = x1 * x1 + x2 * x2;
tangent->_cos = x1;
tangent->_sin = x2;
__BgFP32DarkTwinTangent* twin = (__BgFP32DarkTwinTangent*)tangent;
twin->cos = x1;
twin->sin = x2;
if (1.0f - BG_FP32_TWO_EPSYLON <= square_module && square_module <= 1.0f + BG_FP32_TWO_EPSYLON) {
return;
}
if (square_module <= BG_FP32_SQUARE_EPSYLON) {
tangent->_cos = 1.0f;
tangent->_sin = 0.0f;
twin->cos = 1.0f;
twin->sin = 0.0f;
return;
}
const float multiplier = sqrtf(1.0f / square_module);
tangent->_cos = x1 * multiplier;
tangent->_sin = x2 * multiplier;
twin->cos = x1 * multiplier;
twin->sin = x2 * multiplier;
}
static inline void bg_fp64_tangent_set_values(const double x1, const double x2, BgFP64Tangent* tangent)
{
const double square_module = x1 * x1 + x2 * x2;
tangent->_cos = x1;
tangent->_sin = x2;
__BgFP64DarkTwinTangent* twin = (__BgFP64DarkTwinTangent*)tangent;
twin->cos = x1;
twin->sin = x2;
if (1.0 - BG_FP64_TWO_EPSYLON <= square_module && square_module <= 1.0 + BG_FP64_TWO_EPSYLON) {
return;
}
if (square_module <= BG_FP64_SQUARE_EPSYLON) {
tangent->_cos = 1.0;
tangent->_sin = 0.0;
twin->cos = 1.0;
twin->sin = 0.0;
return;
}
const double multiplier = sqrt(1.0 / square_module);
tangent->_cos = x1 * multiplier;
tangent->_sin = x2 * multiplier;
twin->cos = x1 * multiplier;
twin->sin = x2 * multiplier;
}
// ================== Set Angle ================= //
// ==================== Copy ==================== //
static inline void bg_fp32_tangent_set_angle(const float angle, const angle_unit_t unit, BgFP32Tangent* tangent)
static inline void bg_fp32_tangent_copy(const BgFP32Tangent* from, BgFP32Tangent* to)
{
__BgFP32DarkTwinTangent* twin = (__BgFP32DarkTwinTangent*)to;
twin->cos = from->cos;
twin->sin = from->sin;
}
static inline void bg_fp64_tangent_copy(const BgFP64Tangent* from, BgFP64Tangent* to)
{
__BgFP64DarkTwinTangent* twin = (__BgFP64DarkTwinTangent*)to;
twin->cos = from->cos;
twin->sin = from->sin;
}
// ==================== Swap ==================== //
static inline void bg_fp32_tangent_swap(BgFP32Tangent* tangent1, BgFP32Tangent* tangent2)
{
const float cos = tangent1->cos;
const float sin = tangent1->sin;
__BgFP32DarkTwinTangent* twin1 = (__BgFP32DarkTwinTangent*)tangent1;
twin1->cos = tangent2->cos;
twin1->sin = tangent2->sin;
__BgFP32DarkTwinTangent* twin2 = (__BgFP32DarkTwinTangent*)tangent2;
twin2->cos = cos;
twin2->sin = sin;
}
static inline void bg_fp64_tangent_swap(BgFP64Tangent* tangent1, BgFP64Tangent* tangent2)
{
const double cos = tangent1->cos;
const double sin = tangent1->sin;
__BgFP64DarkTwinTangent* twin1 = (__BgFP64DarkTwinTangent*)tangent1;
twin1->cos = tangent2->cos;
twin1->sin = tangent2->sin;
__BgFP64DarkTwinTangent* twin2 = (__BgFP64DarkTwinTangent*)tangent2;
twin2->cos = cos;
twin2->sin = sin;
}
// ================== Set Turn ================== //
static inline void bg_fp32_tangent_set_turn(const float angle, const angle_unit_t unit, BgFP32Tangent* tangent)
{
const float radians = bg_fp32_angle_to_radians(angle, unit);
tangent->_cos = cosf(radians);
tangent->_sin = sinf(radians);
__BgFP32DarkTwinTangent* twin = (__BgFP32DarkTwinTangent*)tangent;
twin->cos = cosf(radians);
twin->sin = sinf(radians);
}
static inline void bg_fp64_tangent_set_angle(const double angle, const angle_unit_t unit, BgFP64Tangent* tangent)
static inline void bg_fp64_tangent_set_turn(const double angle, const angle_unit_t unit, BgFP64Tangent* tangent)
{
const double radians = bg_fp64_angle_to_radians(angle, unit);
tangent->_cos = cos(radians);
tangent->_sin = sin(radians);
__BgFP64DarkTwinTangent* twin = (__BgFP64DarkTwinTangent*)tangent;
twin->cos = cos(radians);
twin->sin = sin(radians);
}
// ============= Copy to twin type ============== //
static inline void bg_fp32_tangent_copy_to_double(const BgFP32Tangent* from, BgFP64Tangent* to)
static inline void bg_fp32_tangent_set_from_fp64(const BgFP64Tangent* from, BgFP32Tangent* to)
{
bg_fp64_tangent_set_values((double)from->_cos, (double)from->_sin, to);
bg_fp32_tangent_set_values((float)from->cos, (float)from->sin, to);
}
static inline void bg_fp64_tangent_copy_to_single(const BgFP64Tangent* from, BgFP32Tangent* to)
static inline void bg_fp64_tangent_set_from_fp32(const BgFP32Tangent* from, BgFP64Tangent* to)
{
bg_fp32_tangent_set_values((float)from->_cos, (float)from->_sin, to);
bg_fp64_tangent_set_values((double)from->cos, (double)from->sin, to);
}
// ================= Inversion ================== //
static inline void bg_fp32_tangent_invert(BgFP32Tangent* tangent)
{
tangent->_sin = -tangent->_sin;
((__BgFP32DarkTwinTangent*)tangent)->sin = -tangent->sin;
}
static inline void bg_fp64_tangent_invert(BgFP64Tangent* tangent)
{
tangent->_sin = -tangent->_sin;
((__BgFP64DarkTwinTangent*)tangent)->sin = -tangent->sin;
}
// ================ Set Inverted ================ //
static inline void bg_fp32_tangent_set_inverted(const BgFP32Tangent* tangent, BgFP32Tangent* result)
{
result->_cos = tangent->_cos;
result->_sin = -tangent->_sin;
__BgFP32DarkTwinTangent* twin = (__BgFP32DarkTwinTangent*)result;
twin->cos = tangent->cos;
twin->sin = -tangent->sin;
}
static inline void bg_fp64_tangent_set_inverted(const BgFP64Tangent* tangent, BgFP64Tangent* result)
{
result->_cos = tangent->_cos;
result->_sin = -tangent->_sin;
__BgFP64DarkTwinTangent* twin = (__BgFP64DarkTwinTangent*)result;
twin->cos = tangent->cos;
twin->sin = -tangent->sin;
}
// ============== Rotation Matrix =============== //
static inline void bg_fp32_tangent_make_rotation_matrix(const BgFP32Tangent* tangent, BgFP32Matrix2x2* matrix)
{
matrix->r1c1 = tangent->_cos;
matrix->r1c2 = -tangent->_sin;
matrix->r2c1 = tangent->_sin;
matrix->r2c2 = tangent->_cos;
matrix->r1c1 = tangent->cos;
matrix->r1c2 = -tangent->sin;
matrix->r2c1 = tangent->sin;
matrix->r2c2 = tangent->cos;
}
static inline void bg_fp64_tangent_make_rotation_matrix(const BgFP64Tangent* tangent, BgFP64Matrix2x2* matrix)
{
matrix->r1c1 = tangent->_cos;
matrix->r1c2 = -tangent->_sin;
matrix->r2c1 = tangent->_sin;
matrix->r2c2 = tangent->_cos;
matrix->r1c1 = tangent->cos;
matrix->r1c2 = -tangent->sin;
matrix->r2c1 = tangent->sin;
matrix->r2c2 = tangent->cos;
}
// ============== Reverse Matrix ================ //
static inline void bg_fp32_tangent_make_reverse_matrix(const BgFP32Tangent* tangent, BgFP32Matrix2x2* matrix)
{
matrix->r1c1 = tangent->_cos;
matrix->r1c2 = tangent->_sin;
matrix->r2c1 = -tangent->_sin;
matrix->r2c2 = tangent->_cos;
matrix->r1c1 = tangent->cos;
matrix->r1c2 = tangent->sin;
matrix->r2c1 = -tangent->sin;
matrix->r2c2 = tangent->cos;
}
static inline void bg_fp64_tangent_make_reverse_matrix(const BgFP64Tangent* tangent, BgFP64Matrix2x2* matrix)
{
matrix->r1c1 = tangent->_cos;
matrix->r1c2 = tangent->_sin;
matrix->r2c1 = -tangent->_sin;
matrix->r2c2 = tangent->_cos;
matrix->r1c1 = tangent->cos;
matrix->r1c2 = tangent->sin;
matrix->r2c1 = -tangent->sin;
matrix->r2c2 = tangent->cos;
}
// =================== Angle =================== //
static inline float bg_fp32_tangent_get_angle(const BgFP32Tangent* tangent, const angle_unit_t unit)
{
if (tangent->_cos >= 1.0f - BG_FP32_TWO_EPSYLON) {
if (tangent->cos >= 1.0f - BG_FP32_TWO_EPSYLON) {
return 0.0f;
}
if (tangent->_cos <= -1.0f + BG_FP32_TWO_EPSYLON) {
if (tangent->cos <= -1.0f + BG_FP32_TWO_EPSYLON) {
return bg_fp32_angle_get_half_circle(unit);
}
if (tangent->_sin >= 1.0f - BG_FP32_TWO_EPSYLON) {
if (tangent->sin >= 1.0f - BG_FP32_TWO_EPSYLON) {
return bg_fp32_angle_get_quater_circle(unit);
}
if (tangent->_sin <= -1.0f + BG_FP32_TWO_EPSYLON) {
if (tangent->sin <= -1.0f + BG_FP32_TWO_EPSYLON) {
return 0.75f * bg_fp32_angle_get_full_circle(unit);
}
return bg_fp32_radians_to_units(atan2f(tangent->_cos, tangent->_sin), unit);
return bg_fp32_radians_to_units(atan2f(tangent->cos, tangent->sin), unit);
}
static inline double bg_fp64_tangent_get_angle(const BgFP64Tangent* tangent, const angle_unit_t unit)
{
if (tangent->_cos >= 1.0 - BG_FP64_TWO_EPSYLON) {
if (tangent->cos >= 1.0 - BG_FP64_TWO_EPSYLON) {
return 0.0;
}
if (tangent->_cos <= -1.0 + BG_FP64_TWO_EPSYLON) {
if (tangent->cos <= -1.0 + BG_FP64_TWO_EPSYLON) {
return bg_fp64_angle_get_half_circle(unit);
}
if (tangent->_sin >= 1.0 - BG_FP64_TWO_EPSYLON) {
if (tangent->sin >= 1.0 - BG_FP64_TWO_EPSYLON) {
return bg_fp64_angle_get_quater_circle(unit);
}
if (tangent->_sin <= -1.0 + BG_FP64_TWO_EPSYLON) {
if (tangent->sin <= -1.0 + BG_FP64_TWO_EPSYLON) {
return 0.75 * bg_fp64_angle_get_full_circle(unit);
}
return bg_fp64_radians_to_units(atan2(tangent->_cos, tangent->_sin), unit);
return bg_fp64_radians_to_units(atan2(tangent->cos, tangent->sin), unit);
}
// ================ Combination ================= //
@ -238,8 +306,8 @@ static inline double bg_fp64_tangent_get_angle(const BgFP64Tangent* tangent, con
static inline void bg_fp32_tangent_combine(const BgFP32Tangent* tangent1, const BgFP32Tangent* tangent2, BgFP32Tangent* result)
{
bg_fp32_tangent_set_values(
tangent1->_cos * tangent2->_cos - tangent1->_sin * tangent2->_sin,
tangent1->_cos * tangent2->_sin + tangent1->_sin * tangent2->_cos,
tangent1->cos * tangent2->cos - tangent1->sin * tangent2->sin,
tangent1->cos * tangent2->sin + tangent1->sin * tangent2->cos,
result
);
}
@ -247,8 +315,8 @@ static inline void bg_fp32_tangent_combine(const BgFP32Tangent* tangent1, const
static inline void bg_fp64_tangent_combine(const BgFP64Tangent* tangent1, const BgFP64Tangent* tangent2, BgFP64Tangent* result)
{
bg_fp64_tangent_set_values(
tangent1->_cos * tangent2->_cos - tangent1->_sin * tangent2->_sin,
tangent1->_cos * tangent2->_sin + tangent1->_sin * tangent2->_cos,
tangent1->cos * tangent2->cos - tangent1->sin * tangent2->sin,
tangent1->cos * tangent2->sin + tangent1->sin * tangent2->cos,
result
);
}
@ -257,8 +325,8 @@ static inline void bg_fp64_tangent_combine(const BgFP64Tangent* tangent1, const
static inline void bg_fp32_tangent_turn(const BgFP32Tangent* tangent, const BgFP32Vector2* vector, BgFP32Vector2* result)
{
const float x1 = tangent->_cos * vector->x1 - tangent->_sin * vector->x2;
const float x2 = tangent->_sin * vector->x1 + tangent->_cos * vector->x2;
const float x1 = tangent->cos * vector->x1 - tangent->sin * vector->x2;
const float x2 = tangent->sin * vector->x1 + tangent->cos * vector->x2;
result->x1 = x1;
result->x2 = x2;
@ -266,8 +334,8 @@ static inline void bg_fp32_tangent_turn(const BgFP32Tangent* tangent, const BgFP
static inline void bg_fp64_tangent_turn(const BgFP64Tangent* tangent, const BgFP64Vector2* vector, BgFP64Vector2* result)
{
const double x1 = tangent->_cos * vector->x1 - tangent->_sin * vector->x2;
const double x2 = tangent->_sin * vector->x1 + tangent->_cos * vector->x2;
const double x1 = tangent->cos * vector->x1 - tangent->sin * vector->x2;
const double x2 = tangent->sin * vector->x1 + tangent->cos * vector->x2;
result->x1 = x1;
result->x2 = x2;
@ -277,8 +345,8 @@ static inline void bg_fp64_tangent_turn(const BgFP64Tangent* tangent, const BgFP
static inline void bg_fp32_tangent_turn_back(const BgFP32Tangent* tangent, const BgFP32Vector2* vector, BgFP32Vector2* result)
{
const float x1 = tangent->_sin * vector->x2 + tangent->_cos * vector->x1;
const float x2 = tangent->_cos * vector->x2 - tangent->_sin * vector->x1;
const float x1 = tangent->sin * vector->x2 + tangent->cos * vector->x1;
const float x2 = tangent->cos * vector->x2 - tangent->sin * vector->x1;
result->x1 = x1;
result->x2 = x2;
@ -286,8 +354,8 @@ static inline void bg_fp32_tangent_turn_back(const BgFP32Tangent* tangent, const
static inline void bg_fp64_tangent_turn_back(const BgFP64Tangent* tangent, const BgFP64Vector2* vector, BgFP64Vector2* result)
{
const double x1 = tangent->_sin * vector->x2 + tangent->_cos * vector->x1;
const double x2 = tangent->_cos * vector->x2 - tangent->_sin * vector->x1;
const double x1 = tangent->sin * vector->x2 + tangent->cos * vector->x1;
const double x2 = tangent->cos * vector->x2 - tangent->sin * vector->x1;
result->x1 = x1;
result->x2 = x2;

View file

@ -289,11 +289,31 @@ static inline float bg_fp32_vector2_cross_product(const BgFP32Vector2* vector1,
return vector1->x1 * vector2->x2 - vector1->x2 * vector2->x1;
}
static inline double bg_fp64_vector2_cross_product(const BgFP64Vector2* vector1, const BgFP64Vector2* vector2, BgFP64Vector2* result)
static inline double bg_fp64_vector2_cross_product(const BgFP64Vector2* vector1, const BgFP64Vector2* vector2)
{
return vector1->x1 * vector2->x2 - vector1->x2 * vector2->x1;
}
// ============== Complex Product =============== //
static inline void bg_fp32_vector2_complex_product(const BgFP32Vector2* vector1, const BgFP32Vector2* vector2, BgFP32Vector2* result)
{
const float x1 = vector1->x1 * vector2->x1 - vector1->x2 * vector2->x2;
const float x2 = vector1->x1 * vector2->x2 + vector1->x2 * vector2->x1;
result->x1 = x1;
result->x2 = x2;
}
static inline void bg_fp64_vector2_complex_product(const BgFP64Vector2* vector1, const BgFP64Vector2* vector2, BgFP64Vector2* result)
{
const double x1 = vector1->x1 * vector2->x1 - vector1->x2 * vector2->x2;
const double x2 = vector1->x1 * vector2->x2 + vector1->x2 * vector2->x1;
result->x1 = x1;
result->x2 = x2;
}
// =============== Normalization ================ //
static inline int bg_fp32_vector2_normalize(BgFP32Vector2* vector)

View file

@ -218,30 +218,6 @@ static inline void bg_fp64_versor_set_rotation(const BgFP64Rotation3* rotation,
bg_fp64_versor_set_crude_turn(rotation->axis.x1, rotation->axis.x2, rotation->axis.x3, rotation->radians, BG_ANGLE_UNIT_RADIANS, result);
}
// =============== Square modulus =============== //
static inline float bg_fp32_versor_get_square_modulus(const BgFP32Versor* versor)
{
return (versor->s0 * versor->s0 + versor->x1 * versor->x1) + (versor->x2 * versor->x2 + versor->x3 * versor->x3);
}
static inline double bg_fp64_versor_get_square_modulus(const BgFP64Versor* versor)
{
return (versor->s0 * versor->s0 + versor->x1 * versor->x1) + (versor->x2 * versor->x2 + versor->x3 * versor->x3);
}
// =================== Modulus ================== //
static inline float bg_fp32_versor_get_modulus(const BgFP32Versor* versor)
{
return sqrtf(bg_fp32_versor_get_square_modulus(versor));
}
static inline double bg_fp64_versor_get_modulus(const BgFP64Versor* versor)
{
return sqrt(bg_fp64_versor_get_square_modulus(versor));
}
// ================= Comparison ================= //
static inline int bg_fp32_versor_is_idle(const BgFP32Versor* versor)