#ifndef _GEOMETRY_VERSOR_H_ #define _GEOMETRY_VERSOR_H_ #include #include "basis.h" #include "vector3.h" #include "rotation3.h" #include "matrix3x3.h" typedef struct { float _s0, _x1, _x2, _x3; } SPVersor; typedef struct { double _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->_s0 = 1.0f; versor->_x1 = 0.0f; versor->_x2 = 0.0f; versor->_x3 = 0.0f; } static inline void dp_versor_reset(DPVersor* versor) { 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; } // ==================== 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); } } 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); } } // ==================== Copy ==================== // static inline void sp_versor_copy(const SPVersor* from, SPVersor* to) { 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->_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_from_double(const DPVersor* versor, SPVersor* result) { sp_versor_set( (float)versor->_s0, (float)versor->_x1, (float)versor->_x2, (float)versor->_x3, result ); } static inline void dp_versor_from_single(const SPVersor* versor, DPVersor* result) { dp_versor_set( (double)versor->_s0, (double)versor->_x1, (double)versor->_x2, (double)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->_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->_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 s0 = (second->_s0 * first->_s0 - second->_x1 * first->_x1) - (second->_x2 * first->_x2 + second->_x3 * first->_x3); const float x1 = (second->_x1 * first->_s0 + second->_s0 * first->_x1) - (second->_x3 * first->_x2 - second->_x2 * first->_x3); const float x2 = (second->_x2 * first->_s0 + second->_s0 * first->_x2) - (second->_x1 * first->_x3 - second->_x3 * first->_x1); const float x3 = (second->_x3 * first->_s0 + second->_s0 * first->_x3) - (second->_x2 * first->_x1 - second->_x1 * first->_x2); const float square_module = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); result->_s0 = s0; result->_x1 = x1; result->_x2 = x2; result->_x3 = x3; if (square_module < 1.0f - SP_TWO_EPSYLON || 1.0f + SP_TWO_EPSYLON < square_module) { __sp_versor_normalize(square_module, result); } } static inline void dp_versor_combine(const DPVersor* second, const DPVersor* first, DPVersor* result) { const double s0 = (second->_s0 * first->_s0 - second->_x1 * first->_x1) - (second->_x2 * first->_x2 + second->_x3 * first->_x3); const double x1 = (second->_x1 * first->_s0 + second->_s0 * first->_x1) - (second->_x3 * first->_x2 - second->_x2 * first->_x3); const double x2 = (second->_x2 * first->_s0 + second->_s0 * first->_x2) - (second->_x1 * first->_x3 - second->_x3 * first->_x1); const double x3 = (second->_x3 * first->_s0 + second->_s0 * first->_x3) - (second->_x2 * first->_x1 - second->_x1 * first->_x2); const double square_module = (s0 * s0 + x1 * x1) + (x2 * x2 + x3 * x3); result->_s0 = s0; result->_x1 = x1; result->_x2 = x2; result->_x3 = x3; if (square_module < 1.0 - DP_TWO_EPSYLON || 1.0 + DP_TWO_EPSYLON < square_module) { __dp_versor_normalize(square_module, 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 = 2.0f * versor->_s0 * versor->_x1; const float s0x2 = 2.0f * versor->_s0 * versor->_x2; const float s0x3 = 2.0f * versor->_s0 * versor->_x3; const float x1x2 = 2.0f * versor->_x1 * versor->_x2; const float x1x3 = 2.0f * versor->_x1 * versor->_x3; const float x2x3 = 2.0f * versor->_x2 * versor->_x3; matrix->r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); matrix->r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); matrix->r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); matrix->r1c2 = x1x2 - s0x3; matrix->r2c3 = x2x3 - s0x1; matrix->r3c1 = x1x3 - s0x2; matrix->r2c1 = x1x2 + s0x3; matrix->r3c2 = x2x3 + s0x1; matrix->r1c3 = 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 = 2.0 * versor->_s0 * versor->_x1; const double s0x2 = 2.0 * versor->_s0 * versor->_x2; const double s0x3 = 2.0 * versor->_s0 * versor->_x3; const double x1x2 = 2.0 * versor->_x1 * versor->_x2; const double x1x3 = 2.0 * versor->_x1 * versor->_x3; const double x2x3 = 2.0 * versor->_x2 * versor->_x3; matrix->r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); matrix->r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); matrix->r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); matrix->r1c2 = x1x2 - s0x3; matrix->r2c3 = x2x3 - s0x1; matrix->r3c1 = x1x3 - s0x2; matrix->r2c1 = x1x2 + s0x3; matrix->r3c2 = x2x3 + s0x1; matrix->r1c3 = 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 = 2.0f * versor->_s0 * versor->_x1; const float s0x2 = 2.0f * versor->_s0 * versor->_x2; const float s0x3 = 2.0f * versor->_s0 * versor->_x3; const float x1x2 = 2.0f * versor->_x1 * versor->_x2; const float x1x3 = 2.0f * versor->_x1 * versor->_x3; const float x2x3 = 2.0f * versor->_x2 * versor->_x3; matrix->r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); matrix->r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); matrix->r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); matrix->r1c2 = x1x2 + s0x3; matrix->r2c3 = x2x3 + s0x1; matrix->r3c1 = x1x3 + s0x2; matrix->r2c1 = x1x2 - s0x3; matrix->r3c2 = x2x3 - s0x1; matrix->r1c3 = 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 = 2.0 * versor->_s0 * versor->_x1; const double s0x2 = 2.0 * versor->_s0 * versor->_x2; const double s0x3 = 2.0 * versor->_s0 * versor->_x3; const double x1x2 = 2.0 * versor->_x1 * versor->_x2; const double x1x3 = 2.0 * versor->_x1 * versor->_x3; const double x2x3 = 2.0 * versor->_x2 * versor->_x3; matrix->r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); matrix->r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); matrix->r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); matrix->r1c2 = x1x2 + s0x3; matrix->r2c3 = x2x3 + s0x1; matrix->r3c1 = x1x3 + s0x2; matrix->r2c1 = x1x2 - s0x3; matrix->r3c2 = x2x3 - s0x1; matrix->r1c3 = x1x3 - s0x2; } // ================ Turn Vector ================= // static inline void sp_versor_turn(const SPVersor* versor, const SPVector3* vector, SPVector3* result) { const float tx1 = 2.0f * (versor->_x2 * vector->x3 - versor->_x3 * vector->x2); const float tx2 = 2.0f * (versor->_x3 * vector->x1 - versor->_x1 * vector->x3); const float tx3 = 2.0f * (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 tx1 = 2.0 * (versor->_x2 * vector->x3 - versor->_x3 * vector->x2); const double tx2 = 2.0 * (versor->_x3 * vector->x1 - versor->_x1 * vector->x3); const double tx3 = 2.0 * (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; } // ================ Turn2 Vector ================ // static inline void sp_versor_turn2(const SPVersor* versor, const SPVector3* vector, SPVector3* result) { 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 = 2.0f * versor->_s0 * versor->_x1; const float s0x2 = 2.0f * versor->_s0 * versor->_x2; const float s0x3 = 2.0f * versor->_s0 * versor->_x3; const float x1x2 = 2.0f * versor->_x1 * versor->_x2; const float x1x3 = 2.0f * versor->_x1 * versor->_x3; const float x2x3 = 2.0f * versor->_x2 * versor->_x3; const float r2c1 = x1x2 + s0x3; const float r3c2 = x2x3 + s0x1; const float r1c3 = x1x3 + s0x2; const float r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); const float r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); const float r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); const float r1c2 = x1x2 - s0x3; const float r2c3 = x2x3 - s0x1; const float r3c1 = x1x3 - s0x2; const float x1 = r1c1 * vector->x1 + r1c2 * vector->x2 + r1c3 * vector->x3; const float x2 = r2c1 * vector->x1 + r2c2 * vector->x2 + r2c3 * vector->x3; const float x3 = r3c1 * vector->x1 + r3c2 * vector->x2 + r3c3 * vector->x3; result->x1 = x1; result->x2 = x2; result->x3 = x3; } static inline void dp_versor_turn2(const DPVersor* versor, const DPVector3* vector, DPVector3* result) { 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 = 2.0f * versor->_s0 * versor->_x1; const double s0x2 = 2.0f * versor->_s0 * versor->_x2; const double s0x3 = 2.0f * versor->_s0 * versor->_x3; const double x1x2 = 2.0f * versor->_x1 * versor->_x2; const double x1x3 = 2.0f * versor->_x1 * versor->_x3; const double x2x3 = 2.0f * versor->_x2 * versor->_x3; const double r2c1 = x1x2 + s0x3; const double r3c2 = x2x3 + s0x1; const double r1c3 = x1x3 + s0x2; const double r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); const double r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); const double r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); const double r1c2 = x1x2 - s0x3; const double r2c3 = x2x3 - s0x1; const double r3c1 = x1x3 - s0x2; const double x1 = r1c1 * vector->x1 + r1c2 * vector->x2 + r1c3 * vector->x3; const double x2 = r2c1 * vector->x1 + r2c2 * vector->x2 + r2c3 * vector->x3; const double x3 = r3c1 * vector->x1 + r3c2 * vector->x2 + r3c3 * vector->x3; 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 tx1 = 2.0f * (versor->_x2 * vector->x3 - versor->_x3 * vector->x2); const float tx2 = 2.0f * (versor->_x3 * vector->x1 - versor->_x1 * vector->x3); const float tx3 = 2.0f * (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 tx1 = 2.0 * (versor->_x2 * vector->x3 - versor->_x3 * vector->x2); const double tx2 = 2.0 * (versor->_x3 * vector->x1 - versor->_x1 * vector->x3); const double tx3 = 2.0 * (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 Back2 ============= // static inline void sp_versor_turn_back2(const SPVersor* versor, const SPVector3* vector, SPVector3* result) { 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 = 2.0f * versor->_s0 * versor->_x1; const float s0x2 = 2.0f * versor->_s0 * versor->_x2; const float s0x3 = 2.0f * versor->_s0 * versor->_x3; const float x1x2 = 2.0f * versor->_x1 * versor->_x2; const float x1x3 = 2.0f * versor->_x1 * versor->_x3; const float x2x3 = 2.0f * versor->_x2 * versor->_x3; const float r1c2 = x1x2 + s0x3; const float r2c3 = x2x3 + s0x1; const float r3c1 = x1x3 + s0x2; const float r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); const float r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); const float r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); const float r2c1 = x1x2 - s0x3; const float r3c2 = x2x3 - s0x1; const float r1c3 = x1x3 - s0x2; const float x1 = r1c1 * vector->x1 + r1c2 * vector->x2 + r1c3 * vector->x3; const float x2 = r2c1 * vector->x1 + r2c2 * vector->x2 + r2c3 * vector->x3; const float x3 = r3c1 * vector->x1 + r3c2 * vector->x2 + r3c3 * vector->x3; result->x1 = x1; result->x2 = x2; result->x3 = x3; } static inline void dp_versor_turn_back2(const DPVersor* versor, const DPVector3* vector, DPVector3* result) { 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 = 2.0f * versor->_s0 * versor->_x1; const double s0x2 = 2.0f * versor->_s0 * versor->_x2; const double s0x3 = 2.0f * versor->_s0 * versor->_x3; const double x1x2 = 2.0f * versor->_x1 * versor->_x2; const double x1x3 = 2.0f * versor->_x1 * versor->_x3; const double x2x3 = 2.0f * versor->_x2 * versor->_x3; const double r1c2 = x1x2 + s0x3; const double r2c3 = x2x3 + s0x1; const double r3c1 = x1x3 + s0x2; const double r1c1 = (s0s0 + x1x1) - (x2x2 + x3x3); const double r2c2 = (s0s0 + x2x2) - (x1x1 + x3x3); const double r3c3 = (s0s0 + x3x3) - (x1x1 + x2x2); const double r2c1 = x1x2 - s0x3; const double r3c2 = x2x3 - s0x1; const double r1c3 = x1x3 - s0x2; const double x1 = r1c1 * vector->x1 + r1c2 * vector->x2 + r1c3 * vector->x3; const double x2 = r2c1 * vector->x1 + r2c2 * vector->x2 + r2c3 * vector->x3; const double x3 = r3c1 * vector->x1 + r3c2 * vector->x2 + r3c3 * vector->x3; result->x1 = x1; result->x2 = x2; result->x3 = x3; } #endif