bgc-c/basic-geometry/versor.h

275 lines
8.2 KiB
C

#ifndef _GEOMETRY_VERSOR_H_
#define _GEOMETRY_VERSOR_H_
#include <stdint.h>
#include "basis.h"
#include "angle.h"
#include "vector3.h"
#include "rotation3.h"
#include "matrix3x3.h"
// =================== Types ==================== //
typedef struct {
const float s0, x1, x2, x3;
} BgFP32Versor;
typedef struct {
const double s0, x1, x2, x3;
} BgFP64Versor;
// ================= Dark Twins ================= //
typedef struct {
float s0, x1, x2, x3;
} __BgFP32DarkTwinVersor;
typedef struct {
double s0, x1, x2, x3;
} __BgFP64DarkTwinVersor;
// ================= Constants ================== //
extern const BgFP32Versor BG_FP32_IDLE_VERSOR;
extern const BgFP64Versor BG_FP64_IDLE_VERSOR;
// =================== Reset ==================== //
static inline void bg_fp32_versor_reset(BgFP32Versor* versor)
{
__BgFP32DarkTwinVersor* twin = (__BgFP32DarkTwinVersor*)versor;
twin->s0 = 1.0f;
twin->x1 = 0.0f;
twin->x2 = 0.0f;
twin->x3 = 0.0f;
}
static inline void bg_fp64_versor_reset(BgFP64Versor* versor)
{
__BgFP64DarkTwinVersor* twin = (__BgFP64DarkTwinVersor*)versor;
twin->s0 = 1.0;
twin->x1 = 0.0;
twin->x2 = 0.0;
twin->x3 = 0.0;
}
// ==================== Set ===================== //
void bg_fp32_versor_set_values(const float s0, const float x1, const float x2, const float x3, BgFP32Versor* versor);
void bg_fp64_versor_set_values(const double s0, const double x1, const double x2, const double x3, BgFP64Versor* versor);
// ==================== Copy ==================== //
static inline void bg_fp32_versor_copy(const BgFP32Versor* from, BgFP32Versor* to)
{
__BgFP32DarkTwinVersor* twin = (__BgFP32DarkTwinVersor*)to;
twin->s0 = from->s0;
twin->x1 = from->x1;
twin->x2 = from->x2;
twin->x3 = from->x3;
}
static inline void bg_fp64_versor_copy(const BgFP64Versor* from, BgFP64Versor* to)
{
__BgFP64DarkTwinVersor* twin = (__BgFP64DarkTwinVersor*)to;
twin->s0 = from->s0;
twin->x1 = from->x1;
twin->x2 = from->x2;
twin->x3 = from->x3;
}
// =============== 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);
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);
// ================== Set Turn ================== //
static inline void bg_fp32_versor_set_turn(const BgFP32Vector3* axis, const float angle, const angle_unit_t unit, BgFP32Versor* result)
{
bg_fp32_versor_set_crude_turn(axis->x1, axis->x2, axis->x3, angle, unit, result);
}
static inline void bg_fp64_versor_set_turn(const BgFP32Vector3* axis, const double angle, const angle_unit_t unit, BgFP64Versor* result)
{
bg_fp64_versor_set_crude_turn(axis->x1, axis->x2, axis->x3, angle, unit, result);
}
// ================ Set Rotation ================ //
static inline void bg_fp32_versor_set_rotation(const BgFP32Rotation3* rotation, BgFP32Versor* result)
{
bg_fp32_versor_set_crude_turn(rotation->axis.x1, rotation->axis.x2, rotation->axis.x3, rotation->radians, BG_ANGLE_UNIT_RADIANS, result);
}
static inline void bg_fp64_versor_set_rotation(const BgFP64Rotation3* rotation, BgFP64Versor* result)
{
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)
{
return 1.0f - BG_FP32_EPSYLON <= versor->s0 || versor->s0 <= -(1.0 - BG_FP32_EPSYLON);
}
static inline int bg_fp64_versor_is_idle(const BgFP64Versor* versor)
{
return 1.0 - BG_FP64_EPSYLON <= versor->s0 || versor->s0 <= -(1.0 - BG_FP64_EPSYLON);
}
// ============= Copy to twin type ============== //
static inline void bg_fp32_versor_set_from_fp64(const BgFP64Versor* versor, BgFP32Versor* result)
{
bg_fp32_versor_set_values(
(float) versor->s0,
(float) versor->x1,
(float) versor->x2,
(float) versor->x3,
result
);
}
static inline void bg_fp64_versor_set_from_fp32(const BgFP32Versor* versor, BgFP64Versor* result)
{
bg_fp64_versor_set_values(
versor->s0,
versor->x1,
versor->x2,
versor->x3,
result
);
}
// ================= Inversion ================== //
static inline void bg_fp32_versor_invert(BgFP32Versor* versor)
{
__BgFP32DarkTwinVersor* twin = (__BgFP32DarkTwinVersor*)versor;
twin->x1 = -versor->x1;
twin->x2 = -versor->x2;
twin->x3 = -versor->x3;
}
static inline void bg_fp64_versor_invert(BgFP64Versor* versor)
{
__BgFP64DarkTwinVersor* twin = (__BgFP64DarkTwinVersor*)versor;
twin->x1 = -versor->x1;
twin->x2 = -versor->x2;
twin->x3 = -versor->x3;
}
// ================ Set Inverted ================ //
static inline void bg_fp32_versor_set_inverted(const BgFP32Versor* versor, BgFP32Versor* to)
{
__BgFP32DarkTwinVersor* twin = (__BgFP32DarkTwinVersor*)to;
twin->s0 = versor->s0;
twin->x1 = -versor->x1;
twin->x2 = -versor->x2;
twin->x3 = -versor->x3;
}
static inline void bg_fp64_versor_set_inverted(const BgFP64Versor* versor, BgFP64Versor* to)
{
__BgFP64DarkTwinVersor* twin = (__BgFP64DarkTwinVersor*)to;
twin->s0 = versor->s0;
twin->x1 = -versor->x1;
twin->x2 = -versor->x2;
twin->x3 = -versor->x3;
}
// ================ Set Inverted ================ //
static inline void bg_fp32_versor_set_inverted_fp64(const BgFP64Versor* versor, BgFP32Versor* to)
{
bg_fp32_versor_set_values(
(float) versor->s0,
(float) -versor->x1,
(float) -versor->x2,
(float) -versor->x3,
to
);
}
static inline void bg_fp64_versor_set_inverted_fp32(const BgFP32Versor* versor, BgFP64Versor* to)
{
bg_fp64_versor_set_values(
versor->s0,
-versor->x1,
-versor->x2,
-versor->x3,
to
);
}
// ================ Combination ================= //
void bg_fp32_versor_combine(const BgFP32Versor* second, const BgFP32Versor* first, BgFP32Versor* result);
void bg_fp64_versor_combine(const BgFP64Versor* second, const BgFP64Versor* first, BgFP64Versor* result);
// ================= Rotation3 ================== //
void bg_fp32_versor_get_rotation(const BgFP32Versor* versor, BgFP32Rotation3* result);
void bg_fp64_versor_get_rotation(const BgFP64Versor* versor, BgFP64Rotation3* result);
// =========== Make Rotation Matrix3x3 ========== //
void bg_fp32_versor_get_rotation_matrix(const BgFP32Versor* versor, BgFP32Matrix3x3* matrix);
void bg_fp64_versor_get_rotation_matrix(const BgFP64Versor* versor, BgFP64Matrix3x3* matrix);
// =========== Make Reverse Matrix3x3 =========== //
void bg_fp32_versor_get_reverse_matrix(const BgFP32Versor* versor, BgFP32Matrix3x3* matrix);
void bg_fp64_versor_get_reverse_matrix(const BgFP64Versor* versor, BgFP64Matrix3x3* matrix);
// ================ Turn Vector ================= //
void bg_fp32_versor_turn(const BgFP32Versor* versor, const BgFP32Vector3* vector, BgFP32Vector3* result);
void bg_fp64_versor_turn(const BgFP64Versor* versor, const BgFP64Vector3* vector, BgFP64Vector3* result);
// ============== Turn Vector Back ============== //
void bg_fp32_versor_turn_back(const BgFP32Versor* versor, const BgFP32Vector3* vector, BgFP32Vector3* result);
void bg_fp64_versor_turn_back(const BgFP64Versor* versor, const BgFP64Vector3* vector, BgFP64Vector3* result);
#endif