diff --git a/EGkLuaAux.h b/EGkLuaAux.h index 6598856..9cd5510 100644 --- a/EGkLuaAux.h +++ b/EGkLuaAux.h @@ -1,13 +1,13 @@ //---------------------------------------------------------------------------- -// EgalTech 2015-2015 +// EgalTech 2015-2024 //---------------------------------------------------------------------------- -// File : EGkLuaAux.h Data : 21.03.15 Versione : 1.6c6 +// File : EGkLuaAux.h Data : 14.04.24 Versione : 2.6d4 // Contenuto : Funzioni per gestione parametri geometrici con LUA. // // // // Modifiche : 21.03.15 DS Creazione modulo. -// +// 14.04.24 DS Aggiunta gestione Quaternion. // //---------------------------------------------------------------------------- @@ -17,6 +17,7 @@ #include "/EgtDev/Include/EGkPoint3d.h" #include "/EgtDev/Include/EGkFrame3d.h" #include "/EgtDev/Include/EGkBBox3d.h" +#include "/EgtDev/Include/EGkQuaternion.h" #include "/EgtDev/Include/EGkColor.h" #include "/EgtDev/Include/EGkGeoCollection.h" #include "/EgtDev/Include/EGkSelection.h" @@ -25,12 +26,16 @@ //---------------------------------------------------------------------------- inline bool LuaGetParam( lua_State* L, int nInd, Vector3d& vtPar) -{ return LuaGetParam( L, nInd, vtPar.v) ; } +{ + return LuaGetParam( L, nInd, vtPar.v) ; +} //---------------------------------------------------------------------------- inline bool LuaGetParam( lua_State* L, int nInd, Point3d& ptPar) -{ return LuaGetParam( L, nInd, ptPar.v) ; } +{ + return LuaGetParam( L, nInd, ptPar.v) ; +} //---------------------------------------------------------------------------- inline bool @@ -114,6 +119,13 @@ LuaGetParam( lua_State* L, int nInd, BBox3d& b3Par) return true ; } +//---------------------------------------------------------------------------- +inline bool +LuaGetParam( lua_State* L, int nInd, Quaternion& qtPar) +{ + return LuaGetParam( L, nInd, qtPar.v) ; +} + //---------------------------------------------------------------------------- inline bool LuaGetParam( lua_State* L, int nInd, Color& colPar) @@ -369,6 +381,23 @@ LuaSetParam( lua_State* L, const BBox3d& b3Par) return true ; } +//---------------------------------------------------------------------------- +inline bool +LuaSetParam( lua_State* L, const Quaternion& qtPar) +{ + try { + lua_createtable( L, 4, 0) ; + for ( int i = 1 ; i <= 4 ; ++ i) { + lua_pushnumber( L, qtPar.v[i-1]) ; + lua_rawseti( L, -2, i) ; + } + } + catch( ...) { + return false ; + } + return true ; +} + //------------------------------------------------------------------------------- inline bool LuaSetParam( lua_State* L, const Color& colPar) diff --git a/EGkQuaternion.h b/EGkQuaternion.h new file mode 100644 index 0000000..bd96c88 --- /dev/null +++ b/EGkQuaternion.h @@ -0,0 +1,174 @@ +//---------------------------------------------------------------------------- +// EgalTech 2024-2024 +//---------------------------------------------------------------------------- +// File : EGkQuaternion.h Data : 13.04.243 Versione : 2.6d4 +// Contenuto : Dichiarazione della classe Quaternion. +// +// +// +// Modifiche : 31.12.13 DS Creazione modulo. +// +// +// +//---------------------------------------------------------------------------- + +#pragma once + +#include "/EgtDev/Include/EGkFrame3d.h" + +//----------------------- Macro per import/export ----------------------------- +#undef EGK_EXPORT +#if defined( I_AM_EGK) // da definirsi solo nella DLL + #define EGK_EXPORT __declspec( dllexport) +#else + #define EGK_EXPORT __declspec( dllimport) +#endif + +//----------------------------------------------------------------------------- +class EGK_EXPORT Quaternion +{ + public : + Quaternion( double dW = 0, double dX = 0, double dY = 0, double dZ = 0) : w( dW), x( dX), y( dY), z( dZ) {} + + public : + bool IsValid( void) const + { return ( std::isfinite( w) && std::isfinite( x) && std::isfinite( y) && std::isfinite( z)) ; } + bool IsSmall( void) const + { return ( ( w * w + x * x + y * y + z * z) < SQ_EPS_SMALL) ; } + bool IsZero( void) const + { return ( ( w * w + x * x + y * y + z * z) < SQ_EPS_ZERO) ; } + bool IsUnit( void) const + { return ( abs( w - 1) < EPS_ZERO && ( x * x + y * y + z * z) < SQ_EPS_ZERO) ; } + double SqLen( void) const + { return ( w * w + x * x + y * y + z * z) ; } + double Len( void) const ; + bool Normalize( double dEps = EPS_SMALL) ; + bool IsNormalized( void) const + { return ( abs( 1.0 - ( w * w + x * x + y * y + z * z)) < ( 2 * EPS_ZERO)) ; } + Quaternion& operator +=( const Quaternion& qtQ) + { w += qtQ.w ; x += qtQ.x ; y += qtQ.y ; z += qtQ.z ; return *this ; } + Quaternion& operator -=( const Quaternion& qtQ) + { w -= qtQ.w ; x -= qtQ.x ; y -= qtQ.y ; z -= qtQ.z ; return *this ; } + Quaternion& operator *=( double dMul) + { w *= dMul ; x *= dMul ; y *= dMul ; z *= dMul ; return *this ; } + Quaternion& operator /=( double dDiv) + { double dMul = 1 / dDiv ; w *= dMul ; x *= dMul ; y *= dMul ; z *= dMul ; return *this ; } + + public : + union { + struct { + double w ; + double x ; + double y ; + double z ; + } ; + double v[4] ; + } ; +} ; + +//---------------------------------------------------------------------------- +// Quaternioni notevoli +//---------------------------------------------------------------------------- +// Quaternione non valido +const Quaternion Q_INVALID( NAN, NAN, NAN, NAN) ; +// Quaternione unità +const Quaternion Q_UNIT( 1, 0, 0, 0) ; +// Quaternione nullo +const Quaternion Q_NULL( 0, 0, 0, 0) ; + +//---------------------------------------------------------------------------- +// Definizione da rotazione definita con asse e angolo e inverso +//---------------------------------------------------------------------------- +EGK_EXPORT Quaternion FromAxisAngle( const Vector3d& vtAx, double dAngDeg) ; +EGK_EXPORT bool ToAxisAngle( const Quaternion& qtQ, Vector3d& vtAx, double& dAngDeg) ; + +//---------------------------------------------------------------------------- +// Definizione da parte rotazione di un sistema di riferimento e inverso +//---------------------------------------------------------------------------- +EGK_EXPORT Quaternion FromFrame( const Frame3d& frRef) ; +EGK_EXPORT bool ToFrame( const Quaternion& qtQ, Frame3d& frRef) ; + +//---------------------------------------------------------------------------- +// Coniugato di un quaternione +//---------------------------------------------------------------------------- +inline const Quaternion +Conjugate( const Quaternion& qtQ) +{ + return ( Quaternion( qtQ.w, -qtQ.x, -qtQ.y, -qtQ.z)) ; +} + +//---------------------------------------------------------------------------- +// Opposto di un quaternione +//---------------------------------------------------------------------------- +inline const Quaternion +operator-( const Quaternion& qtQ) +{ + return ( Quaternion( -qtQ.w, - qtQ.x, - qtQ.y, - qtQ.z)) ; +} + +//---------------------------------------------------------------------------- +// Somma di due quaternioni +//---------------------------------------------------------------------------- +inline const Quaternion +operator+( const Quaternion& qtQ1, const Quaternion& qtQ2) +{ + return ( Quaternion( qtQ1.w + qtQ2.w, qtQ1.x + qtQ2.x, qtQ1.y + qtQ2.y, qtQ1.z + qtQ2.z)) ; +} + +//---------------------------------------------------------------------------- +// Sottrazione di due quaternioni +//---------------------------------------------------------------------------- +inline const Quaternion +operator-( const Quaternion& qtQ1, const Quaternion& qtQ2) +{ + return ( Quaternion( qtQ1.w - qtQ2.w, qtQ1.x - qtQ2.x, qtQ1.y - qtQ2.y, qtQ1.z - qtQ2.z)) ; +} + +//---------------------------------------------------------------------------- +// Prodotto di un quaternione con uno scalare +//---------------------------------------------------------------------------- +inline const Quaternion +operator*( const Quaternion& qtQ, double dMul) +{ + return ( Quaternion( qtQ.w * dMul, qtQ.x * dMul, qtQ.y * dMul, qtQ.z * dMul)) ; +} + +//---------------------------------------------------------------------------- +// Prodotto di uno scalare con un quaternione +//---------------------------------------------------------------------------- +inline const Quaternion +operator*( double dMul, const Quaternion& qtQ) +{ + return ( Quaternion( qtQ.w * dMul, qtQ.x * dMul, qtQ.y * dMul, qtQ.z * dMul)) ; +} + +//---------------------------------------------------------------------------- +// Divisione con uno scalare +//---------------------------------------------------------------------------- +inline const Quaternion +operator/( const Quaternion& qtQ, double dDiv) +{ + double dMul = 1 / dDiv ; + return ( Quaternion( qtQ.w * dMul, qtQ.x * dMul, qtQ.y * dMul, qtQ.z * dMul)) ; +} + +//---------------------------------------------------------------------------- +// Prodotto scalare due quaternioni +//---------------------------------------------------------------------------- +inline double +Scalar( const Quaternion& qtQ1, const Quaternion& qtQ2) +{ + return ( qtQ1.w * qtQ2.w + qtQ1.x * qtQ2.x + qtQ2.y * qtQ2.y + qtQ1.z * qtQ2.z) ; +} + +//---------------------------------------------------------------------------- +// Prodotto di due quaternioni +//---------------------------------------------------------------------------- +inline const Quaternion +operator*( const Quaternion& qtQ1, const Quaternion& qtQ2) +{ + return ( Quaternion( qtQ1.w * qtQ2.w - qtQ1.x * qtQ2.x - qtQ1.y * qtQ2.y - qtQ1.z * qtQ2.z, + qtQ1.w * qtQ2.x + qtQ1.x * qtQ2.w + qtQ1.y * qtQ2.z - qtQ1.z * qtQ2.y, + qtQ1.w * qtQ2.y + qtQ1.y * qtQ2.w + qtQ1.z * qtQ2.x - qtQ1.x * qtQ2.z, + qtQ1.w * qtQ2.z + qtQ1.z * qtQ2.w + qtQ1.x * qtQ2.y - qtQ1.y * qtQ2.x)) ; +}