Базовая версия библиотеки. Версия 0.2.0-dev

This commit is contained in:
Andrey Pokidov 2024-11-12 00:55:03 +07:00
parent b086af7f66
commit 6a56e85052
39 changed files with 6200 additions and 1 deletions

550
src/versor.h Normal file
View file

@ -0,0 +1,550 @@
#ifndef _GEOMETRY_VERSOR_H_
#define _GEOMETRY_VERSOR_H_
#include <stdint.h>
#include "basis.h"
#include "vector3.h"
#include "rotation3.h"
#include "matrix3x3.h"
typedef struct {
float _corrector, _s0, _x1, _x2, _x3;
} SPVersor;
typedef struct {
double _corrector, _s0, _x1, _x2, _x3;
} DPVersor;
extern const SPVersor SP_IDLE_VERSOR;
extern const DPVersor DP_IDLE_VERSOR;
// =================== Reset ==================== //
static inline void sp_versor_reset(SPVersor* versor)
{
versor->_corrector = 1.0f;
versor->_s0 = 1.0f;
versor->_x1 = 0.0f;
versor->_x2 = 0.0f;
versor->_x3 = 0.0f;
}
static inline void dp_versor_reset(DPVersor* versor)
{
versor->_corrector = 1.0;
versor->_s0 = 1.0;
versor->_x1 = 0.0;
versor->_x2 = 0.0;
versor->_x3 = 0.0;
}
// ============== Get Scalar Part =============== //
static inline float sp_get_scalar_part(const SPVersor* versor)
{
return versor->_s0;
}
static inline double dp_get_scalar_part(const DPVersor* versor)
{
return versor->_s0;
}
// ============== Get Vector Part =============== //
static inline void sp_get_vector_part(const SPVersor* versor, SPVector3 * result)
{
result->x1 = versor->_x1;
result->x2 = versor->_x2;
result->x3 = versor->_x3;
}
static inline void dp_get_vector_part(const DPVersor* versor, DPVector3 * result)
{
result->x1 = versor->_x1;
result->x2 = versor->_x2;
result->x3 = versor->_x3;
}
// =================== Get x1 =================== //
static inline float sp_get_x1(const SPVersor* versor)
{
return versor->_x1;
}
static inline double dp_get_x1(const DPVersor* versor)
{
return versor->_x1;
}
// =================== Get x2 =================== //
static inline float sp_get_x2(const SPVersor* versor)
{
return versor->_x2;
}
static inline double dp_get_x2(const DPVersor* versor)
{
return versor->_x2;
}
// =================== Get x3 =================== //
static inline float sp_get_x3(const SPVersor* versor)
{
return versor->_x3;
}
static inline double dp_get_x3(const DPVersor* versor)
{
return versor->_x3;
}
// ================ Get Corrector =============== //
static inline float sp_get_corrector(const SPVersor* versor)
{
return versor->_corrector;
}
static inline double dp_get_corrector(const DPVersor* versor)
{
return versor->_corrector;
}
// ==================== Set ===================== //
void __sp_versor_normalize(const float square_module, SPVersor* versor);
void __dp_versor_normalize(const double square_module, DPVersor* versor);
static inline void sp_versor_set(const float s0, const float x1, const float x2, const float x3, SPVersor* result)
{
result->_s0 = s0;
result->_x1 = x1;
result->_x2 = x2;
result->_x3 = x3;
const float square_module = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3);
if (square_module < 1.0f - SP_TWO_EPSYLON || 1.0f + SP_TWO_EPSYLON < square_module) {
__sp_versor_normalize(square_module, result);
return;
}
if (-1.0f + SP_EPSYLON < s0 && s0 < 1.0f - SP_EPSYLON) {
result->_corrector = 2.0f - square_module;
return;
}
sp_versor_reset(result);
}
static inline void dp_versor_set(const double s0, const double x1, const double x2, const double x3, DPVersor* result)
{
result->_s0 = s0;
result->_x1 = x1;
result->_x2 = x2;
result->_x3 = x3;
const double square_module = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3);
if (square_module < 1.0 - DP_TWO_EPSYLON || 1.0 + DP_TWO_EPSYLON < square_module) {
__dp_versor_normalize(square_module, result);
return;
}
if (s0 < -1.0 + DP_EPSYLON || 1.0 - DP_EPSYLON < s0) {
dp_versor_reset(result);
return;
}
result->_corrector = 2.0 - square_module;
}
// ==================== Copy ==================== //
static inline void sp_versor_copy(const SPVersor* from, SPVersor* to)
{
to->_corrector = from->_corrector;
to->_s0 = from->_s0;
to->_x1 = from->_x1;
to->_x2 = from->_x2;
to->_x3 = from->_x3;
}
static inline void dp_versor_copy(const DPVersor* from, DPVersor* to)
{
to->_corrector = from->_corrector;
to->_s0 = from->_s0;
to->_x1 = from->_x1;
to->_x2 = from->_x2;
to->_x3 = from->_x3;
}
// ==================== Make ==================== //
void sp_versor_make_from_crude_turn(const float x1, const float x2, const float x3, const float angle, const angle_unit_t unit, SPVersor* result);
void dp_versor_make_from_crude_turn(const double x1, const double x2, const double x3, const double angle, const angle_unit_t unit, DPVersor* result);
static inline void sp_versor_make_from_turn(const SPVector3* axis, const float angle, const angle_unit_t unit, SPVersor* result)
{
sp_versor_make_from_crude_turn(axis->x1, axis->x2, axis->x3, angle, unit, result);
}
static inline void dp_versor_make_from_turn(const DPVector3* axis, const double angle, const angle_unit_t unit, DPVersor* result)
{
dp_versor_make_from_crude_turn(axis->x1, axis->x2, axis->x3, angle, unit, result);
}
static inline void sp_versor_make_from_rotation(const SPRotation3* rotation, SPVersor* result)
{
sp_versor_make_from_crude_turn(rotation->axis.x1, rotation->axis.x2, rotation->axis.x3, rotation->radians, ANGLE_UNIT_RADIANS, result);
}
static inline void dp_versor_make_from_rotation(const DPRotation3* rotation, DPVersor* result)
{
dp_versor_make_from_crude_turn(rotation->axis.x1, rotation->axis.x2, rotation->axis.x3, rotation->radians, ANGLE_UNIT_RADIANS, result);
}
// ================= Comparison ================= //
static inline int sp_versor_is_idle(const SPVersor* versor)
{
return 1.0f - SP_EPSYLON <= versor->_s0 || versor->_s0 <= -(1.0 - SP_EPSYLON);
}
static inline int dp_versor_is_idle(const DPVersor* versor)
{
return 1.0 - DP_EPSYLON <= versor->_s0 || versor->_s0 <= -(1.0 - DP_EPSYLON);
}
// ============= Copy to twin type ============== //
static inline void sp_versor_copy_to_double(const SPVersor* versor, DPVersor* result)
{
dp_versor_set(
(double)versor->_s0,
(double)versor->_x1,
(double)versor->_x2,
(double)versor->_x3,
result
);
}
static inline void dp_versor_copy_to_single(const DPVersor* versor, SPVersor* result)
{
sp_versor_set(
(float)versor->_s0,
(float)versor->_x1,
(float)versor->_x2,
(float)versor->_x3,
result
);
}
// ================= Inversion ================== //
static inline void sp_versor_invert(SPVersor* versor)
{
versor->_x1 = -versor->_x1;
versor->_x2 = -versor->_x2;
versor->_x3 = -versor->_x3;
}
static inline void dp_versor_invert(DPVersor* versor)
{
versor->_x1 = -versor->_x1;
versor->_x2 = -versor->_x2;
versor->_x3 = -versor->_x3;
}
// ================ Make Inverted =============== //
static inline void sp_versor_make_inverted(const SPVersor* versor, SPVersor* result)
{
result->_corrector = versor->_corrector;
result->_s0 = versor->_s0;
result->_x1 = -versor->_x1;
result->_x2 = -versor->_x2;
result->_x3 = -versor->_x3;
}
static inline void dp_versor_make_inverted(const DPVersor* versor, DPVersor* result)
{
result->_corrector = versor->_corrector;
result->_s0 = versor->_s0;
result->_x1 = -versor->_x1;
result->_x2 = -versor->_x2;
result->_x3 = -versor->_x3;
}
// ================ Combination ================= //
static inline void sp_versor_combine(const SPVersor* second, const SPVersor* first, SPVersor* result)
{
const float s0s0 = second->_s0 * first->_s0;
const float x1s0 = second->_x1 * first->_s0;
const float x2s0 = second->_x2 * first->_s0;
const float x3s0 = second->_x3 * first->_s0;
const float s0x1 = second->_s0 * first->_x1;
const float x1x1 = second->_x1 * first->_x1;
const float x2x1 = second->_x2 * first->_x1;
const float x3x1 = second->_x3 * first->_x1;
const float s0x2 = second->_s0 * first->_x2;
const float x1x2 = second->_x1 * first->_x2;
const float x2x2 = second->_x2 * first->_x2;
const float x3x2 = second->_x3 * first->_x2;
const float s0x3 = second->_s0 * first->_x3;
const float x1x3 = second->_x1 * first->_x3;
const float x2x3 = second->_x2 * first->_x3;
const float x3x3 = second->_x3 * first->_x3;
const float s0b = (x2x2 + x3x3);
const float x1a = (x1s0 + s0x1);
const float x2a = (x2s0 + s0x2);
const float x3a = (x3s0 + s0x3);
const float s0a = (s0s0 - x1x1);
const float x1b = (x3x2 - x2x3);
const float x2b = (x1x3 - x3x1);
const float x3b = (x2x1 - x1x2);
sp_versor_set(
s0a - s0b,
x1a - x1b,
x2a - x2b,
x3a - x3b,
result
);
}
static inline void sp_versor_combine2(const SPVersor* second, const SPVersor* first, SPVersor* result)
{
sp_versor_set(
((second->_s0 * first->_s0 - second->_x1 * first->_x1) - (second->_x2 * first->_x2 + second->_x3 * first->_x3)),
((second->_x1 * first->_s0 + second->_s0 * first->_x1) - (second->_x3 * first->_x2 - second->_x2 * first->_x3)),
((second->_x2 * first->_s0 + second->_s0 * first->_x2) - (second->_x1 * first->_x3 - second->_x3 * first->_x1)),
((second->_x3 * first->_s0 + second->_s0 * first->_x3) - (second->_x2 * first->_x1 - second->_x1 * first->_x2)),
result
);
}
static inline void dp_versor_combine(const DPVersor* second, const DPVersor* first, DPVersor* result)
{
dp_versor_set(
(second->_s0 * first->_s0 - second->_x1 * first->_x1) - (second->_x2 * first->_x2 + second->_x3 * first->_x3),
(second->_x1 * first->_s0 + second->_s0 * first->_x1) - (second->_x3 * first->_x2 - second->_x2 * first->_x3),
(second->_x2 * first->_s0 + second->_s0 * first->_x2) - (second->_x1 * first->_x3 - second->_x3 * first->_x1),
(second->_x3 * first->_s0 + second->_s0 * first->_x3) - (second->_x2 * first->_x1 - second->_x1 * first->_x2),
result
);
}
// ================= Rotation3 ================== //
void sp_versor_get_rotation(const SPVersor* versor, SPRotation3* result);
void dp_versor_get_rotation(const DPVersor* versor, DPRotation3* result);
// =========== Make Rotation Matrix3x3 ========== //
static inline void sp_versor_make_matrix(const SPVersor* versor, SPMatrix3x3* matrix)
{
const float s0s0 = versor->_s0 * versor->_s0;
const float x1x1 = versor->_x1 * versor->_x1;
const float x2x2 = versor->_x2 * versor->_x2;
const float x3x3 = versor->_x3 * versor->_x3;
const float s0x1 = versor->_s0 * versor->_x1;
const float s0x2 = versor->_s0 * versor->_x2;
const float s0x3 = versor->_s0 * versor->_x3;
const float x1x2 = versor->_x1 * versor->_x2;
const float x1x3 = versor->_x1 * versor->_x3;
const float x2x3 = versor->_x2 * versor->_x3;
const float corrector2 = 2.0f * versor->_corrector;
matrix->r1c1 = versor->_corrector * ((s0s0 + x1x1) - (x2x2 + x3x3));
matrix->r2c2 = versor->_corrector * ((s0s0 + x2x2) - (x1x1 + x3x3));
matrix->r3c3 = versor->_corrector * ((s0s0 + x3x3) - (x1x1 + x2x2));
matrix->r1c2 = corrector2 * (x1x2 - s0x3);
matrix->r2c3 = corrector2 * (x2x3 - s0x1);
matrix->r3c1 = corrector2 * (x1x3 - s0x2);
matrix->r2c1 = corrector2 * (x1x2 + s0x3);
matrix->r3c2 = corrector2 * (x2x3 + s0x1);
matrix->r1c3 = corrector2 * (x1x3 + s0x2);
}
static inline void dp_versor_make_matrix(const DPVersor* versor, DPMatrix3x3* matrix)
{
const double s0s0 = versor->_s0 * versor->_s0;
const double x1x1 = versor->_x1 * versor->_x1;
const double x2x2 = versor->_x2 * versor->_x2;
const double x3x3 = versor->_x3 * versor->_x3;
const double s0x1 = versor->_s0 * versor->_x1;
const double s0x2 = versor->_s0 * versor->_x2;
const double s0x3 = versor->_s0 * versor->_x3;
const double x1x2 = versor->_x1 * versor->_x2;
const double x1x3 = versor->_x1 * versor->_x3;
const double x2x3 = versor->_x2 * versor->_x3;
const double corrector2 = 2.0 * versor->_corrector;
matrix->r1c1 = versor->_corrector * ((s0s0 + x1x1) - (x2x2 + x3x3));
matrix->r2c2 = versor->_corrector * ((s0s0 + x2x2) - (x1x1 + x3x3));
matrix->r3c3 = versor->_corrector * ((s0s0 + x3x3) - (x1x1 + x2x2));
matrix->r1c2 = corrector2 * (x1x2 - s0x3);
matrix->r2c3 = corrector2 * (x2x3 - s0x1);
matrix->r3c1 = corrector2 * (x1x3 - s0x2);
matrix->r2c1 = corrector2 * (x1x2 + s0x3);
matrix->r3c2 = corrector2 * (x2x3 + s0x1);
matrix->r1c3 = corrector2 * (x1x3 + s0x2);
}
// =========== Make Reverse Matrix3x3 =========== //
static inline void sp_versor_make_reverse_matrix(const SPVersor* versor, SPMatrix3x3* matrix)
{
const float s0s0 = versor->_s0 * versor->_s0;
const float x1x1 = versor->_x1 * versor->_x1;
const float x2x2 = versor->_x2 * versor->_x2;
const float x3x3 = versor->_x3 * versor->_x3;
const float s0x1 = versor->_s0 * versor->_x1;
const float s0x2 = versor->_s0 * versor->_x2;
const float s0x3 = versor->_s0 * versor->_x3;
const float x1x2 = versor->_x1 * versor->_x2;
const float x1x3 = versor->_x1 * versor->_x3;
const float x2x3 = versor->_x2 * versor->_x3;
const float corrector2 = 2.0f * versor->_corrector;
matrix->r1c1 = versor->_corrector * ((s0s0 + x1x1) - (x2x2 + x3x3));
matrix->r2c2 = versor->_corrector * ((s0s0 + x2x2) - (x1x1 + x3x3));
matrix->r3c3 = versor->_corrector * ((s0s0 + x3x3) - (x1x1 + x2x2));
matrix->r1c2 = corrector2 * (x1x2 + s0x3);
matrix->r2c3 = corrector2 * (x2x3 + s0x1);
matrix->r3c1 = corrector2 * (x1x3 + s0x2);
matrix->r2c1 = corrector2 * (x1x2 - s0x3);
matrix->r3c2 = corrector2 * (x2x3 - s0x1);
matrix->r1c3 = corrector2 * (x1x3 - s0x2);
}
static inline void dp_versor_make_reverse_matrix(const DPVersor* versor, DPMatrix3x3* matrix)
{
const double s0s0 = versor->_s0 * versor->_s0;
const double x1x1 = versor->_x1 * versor->_x1;
const double x2x2 = versor->_x2 * versor->_x2;
const double x3x3 = versor->_x3 * versor->_x3;
const double s0x1 = versor->_s0 * versor->_x1;
const double s0x2 = versor->_s0 * versor->_x2;
const double s0x3 = versor->_s0 * versor->_x3;
const double x1x2 = versor->_x1 * versor->_x2;
const double x1x3 = versor->_x1 * versor->_x3;
const double x2x3 = versor->_x2 * versor->_x3;
const double corrector2 = 2.0 * versor->_corrector;
matrix->r1c1 = versor->_corrector * ((s0s0 + x1x1) - (x2x2 + x3x3));
matrix->r2c2 = versor->_corrector * ((s0s0 + x2x2) - (x1x1 + x3x3));
matrix->r3c3 = versor->_corrector * ((s0s0 + x3x3) - (x1x1 + x2x2));
matrix->r1c2 = corrector2 * (x1x2 + s0x3);
matrix->r2c3 = corrector2 * (x2x3 + s0x1);
matrix->r3c1 = corrector2 * (x1x3 + s0x2);
matrix->r2c1 = corrector2 * (x1x2 - s0x3);
matrix->r3c2 = corrector2 * (x2x3 - s0x1);
matrix->r1c3 = corrector2 * (x1x3 - s0x2);
}
// ================ Turn Vector ================= //
static inline void sp_versor_turn(const SPVersor* versor, const SPVector3* vector, SPVector3* result)
{
const float multiplier = 2.0f * versor->_corrector;
const float tx1 = multiplier * (versor->_x2 * vector->x3 - versor->_x3 * vector->x2);
const float tx2 = multiplier * (versor->_x3 * vector->x1 - versor->_x1 * vector->x3);
const float tx3 = multiplier * (versor->_x1 * vector->x2 - versor->_x2 * vector->x1);
const float x1 = (vector->x1 + tx1 * versor->_s0) + (versor->_x2 * tx3 - versor->_x3 * tx2);
const float x2 = (vector->x2 + tx2 * versor->_s0) + (versor->_x3 * tx1 - versor->_x1 * tx3);
const float x3 = (vector->x3 + tx3 * versor->_s0) + (versor->_x1 * tx2 - versor->_x2 * tx1);
result->x1 = x1;
result->x2 = x2;
result->x3 = x3;
}
static inline void dp_versor_turn(const DPVersor* versor, const DPVector3* vector, DPVector3* result)
{
const double multiplier = 2.0 * versor->_corrector;
const double tx1 = multiplier * (versor->_x2 * vector->x3 - versor->_x3 * vector->x2);
const double tx2 = multiplier * (versor->_x3 * vector->x1 - versor->_x1 * vector->x3);
const double tx3 = multiplier * (versor->_x1 * vector->x2 - versor->_x2 * vector->x1);
const double x1 = (vector->x1 + tx1 * versor->_s0) + (versor->_x2 * tx3 - versor->_x3 * tx2);
const double x2 = (vector->x2 + tx2 * versor->_s0) + (versor->_x3 * tx1 - versor->_x1 * tx3);
const double x3 = (vector->x3 + tx3 * versor->_s0) + (versor->_x1 * tx2 - versor->_x2 * tx1);
result->x1 = x1;
result->x2 = x2;
result->x3 = x3;
}
// ============== Turn Vector Back ============== //
static inline void sp_versor_turn_back(const SPVersor* versor, const SPVector3* vector, SPVector3* result)
{
const float multiplier = 2.0f * versor->_corrector;
const float tx1 = multiplier * (versor->_x2 * vector->x3 - versor->_x3 * vector->x2);
const float tx2 = multiplier * (versor->_x3 * vector->x1 - versor->_x1 * vector->x3);
const float tx3 = multiplier * (versor->_x1 * vector->x2 - versor->_x2 * vector->x1);
const float x1 = (vector->x1 - tx1 * versor->_s0) + (versor->_x2 * tx3 - versor->_x3 * tx2);
const float x2 = (vector->x2 - tx2 * versor->_s0) + (versor->_x3 * tx1 - versor->_x1 * tx3);
const float x3 = (vector->x3 - tx3 * versor->_s0) + (versor->_x1 * tx2 - versor->_x2 * tx1);
result->x1 = x1;
result->x2 = x2;
result->x3 = x3;
}
static inline void dp_versor_turn_back(const DPVersor* versor, const DPVector3* vector, DPVector3* result)
{
const double multiplier = 2.0 * versor->_corrector;
const double tx1 = multiplier * (versor->_x2 * vector->x3 - versor->_x3 * vector->x2);
const double tx2 = multiplier * (versor->_x3 * vector->x1 - versor->_x1 * vector->x3);
const double tx3 = multiplier * (versor->_x1 * vector->x2 - versor->_x2 * vector->x1);
const double x1 = (vector->x1 - tx1 * versor->_s0) + (versor->_x2 * tx3 - versor->_x3 * tx2);
const double x2 = (vector->x2 - tx2 * versor->_s0) + (versor->_x3 * tx1 - versor->_x1 * tx3);
const double x3 = (vector->x3 - tx3 * versor->_s0) + (versor->_x1 * tx2 - versor->_x2 * tx1);
result->x1 = x1;
result->x2 = x2;
result->x3 = x3;
}
#endif