//---------------------------------------------------------------------------- // EgalTech 2014-2014 //---------------------------------------------------------------------------- // File : LUA_Aux.cpp Data : 29.09.14 Versione : 1.5i5 // Contenuto : Funzioni ausiliarie per LUA. // // // // Modifiche : 29.09.14 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "LUA.h" #include "AuxTools.h" #include "/EgtDev/Include/EGkFrame3d.h" #include "/EgtDev/Include/EGkColor.h" #include "/EgtDev/Include/EInConst.h" #include "/EgtDev/Include/EGnStringUtils.h" #include "/EgtDev/Extern/Lua/Include/lua.hpp" using namespace std ; //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, bool& bPar) { if ( ! lua_isboolean( L, nInd)) return false ; bPar = ( lua_toboolean( L, nInd) != 0) ; return true ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, int& nPar) { if ( ! lua_isnumber( L, nInd)) return false ; double dVal = lua_tonumber( L, nInd) ; nPar = int( dVal + (( dVal > 0) ? 0.5 : - 0.5)) ; return true ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, double& dPar) { if ( ! lua_isnumber( L, nInd)) return false ; dPar = lua_tonumber( L, nInd) ; return true ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, string& sPar) { if ( ! lua_isstring( L, nInd)) return false ; sPar = lua_tostring( L, nInd) ; return true ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, Vector3d& vtPar) { if ( ! lua_istable( L, nInd)) return false ; for ( int i = 1 ; i <= 3 ; ++ i) { lua_rawgeti( L, nInd, i) ; if ( ! lua_isnumber( L, -1)) return false ; vtPar.v[i-1] = lua_tonumber( L, -1) ; lua_pop( L, 1) ; } return true ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, Point3d& ptPar) { if ( ! lua_istable( L, nInd)) return false ; for ( int i = 1 ; i <= 3 ; ++ i) { lua_rawgeti( L, nInd, i) ; if ( ! lua_isnumber( L, -1)) return false ; ptPar.v[i-1] = lua_tonumber( L, -1) ; lua_pop( L, 1) ; } return true ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, POINTU& ptParW) { if ( ! lua_istable( L, nInd)) return false ; for ( int i = 1 ; i <= 4 ; ++ i) { lua_rawgeti( L, nInd, i) ; if ( ! lua_isnumber( L, -1)) return false ; if ( i <= 3) ptParW.first.v[i-1] = lua_tonumber( L, -1) ; else ptParW.second = lua_tonumber( L, -1) ; lua_pop( L, 1) ; } return true ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, Frame3d& frPar) { if ( ! lua_istable( L, nInd)) return false ; // recupero l'origine lua_rawgeti( L, nInd, 1) ; Point3d ptOrig ; if ( ! LuaGetParam( L, -1, ptOrig)) return false ; lua_pop( L, 1) ; // recupero il versore X lua_rawgeti( L, nInd, 2) ; Vector3d vtDirX ; if ( ! LuaGetParam( L, -1, vtDirX)) return false ; lua_pop( L, 1) ; // recupero il versore Y lua_rawgeti( L, nInd, 3) ; Vector3d vtDirY ; if ( ! LuaGetParam( L, -1, vtDirY)) return false ; lua_pop( L, 1) ; // recupero il versore Z lua_rawgeti( L, nInd, 4) ; Vector3d vtDirZ ; if ( ! LuaGetParam( L, -1, vtDirZ)) return false ; lua_pop( L, 1) ; // assegno il riferimento return frPar.Set( ptOrig, vtDirX, vtDirY, vtDirZ) ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, Color& colPar) { if ( ! lua_istable( L, nInd)) return false ; int nCol[4] ; // red, gree, blue for ( int i = 1 ; i <= 3 ; ++ i) { lua_rawgeti( L, nInd, i) ; if ( ! lua_isnumber( L, -1)) return false ; nCol[i-1] = int( lua_tonumber( L, -1) + 0.5) ; lua_pop( L, 1) ; } // alpha opzionale lua_rawgeti( L, nInd, 4) ; if ( lua_isnumber( L, -1)) nCol[3] = int( lua_tonumber( L, -1) + 0.5) ; else nCol[3] = 100 ; lua_pop( L, 1) ; // assegno il colore colPar.Set( nCol[0], nCol[1], nCol[2], nCol[3]) ; return true ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, INTVECTOR& vPar) { vPar.clear() ; if ( lua_isnumber( L, nInd)) { int nVal = int( lua_tointeger( L, nInd)) ; vPar.push_back( nVal) ; return true ; } else if ( lua_istable( L, nInd)) { // lunghezza della tavola lua_len( L, nInd) ; if ( ! lua_isnumber( L, -1)) return false ; int nLen = int( lua_tointeger( L, -1)) ; lua_pop( L, 1) ; vPar.reserve( nLen) ; for ( int i = 1 ; i <= nLen ; ++ i) { lua_rawgeti( L, nInd, i) ; if ( ! lua_isnumber( L, -1)) return false ; int nVal = int( lua_tointeger( L, -1)) ; vPar.push_back( nVal) ; lua_pop( L, 1) ; } return true ; } else return false ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, PNTVECTOR& vPar) { vPar.clear() ; Point3d ptP ; if ( LuaGetParam( L, nInd, ptP)) { vPar.push_back( ptP) ; return true ; } else if ( lua_istable( L, nInd)) { // lunghezza della tavola lua_len( L, nInd) ; if ( ! lua_isnumber( L, -1)) return false ; int nLen = int( lua_tointeger( L, -1)) ; lua_pop( L, 1) ; vPar.reserve( nLen) ; for ( int i = 1 ; i <= nLen ; ++ i) { lua_rawgeti( L, nInd, i) ; Point3d ptP ; if ( ! LuaGetParam( L, -1, ptP)) return false ; vPar.push_back( ptP) ; lua_pop( L, 1) ; } return true ; } else return false ; } //---------------------------------------------------------------------------- bool LuaGetParam( lua_State* L, int nInd, PNTUVECTOR& vParW) { vParW.clear() ; POINTU ptPW ; if ( LuaGetParam( L, nInd, ptPW)) { vParW.push_back( ptPW) ; return true ; } else if ( lua_istable( L, nInd)) { // lunghezza della tavola lua_len( L, nInd) ; if ( ! lua_isnumber( L, -1)) return false ; int nLen = int( lua_tointeger( L, -1)) ; lua_pop( L, 1) ; vParW.reserve( nLen) ; for ( int i = 1 ; i <= nLen ; ++ i) { lua_rawgeti( L, nInd, i) ; POINTU ptPW ; if ( ! LuaGetParam( L, -1, ptPW)) return false ; vParW.push_back( ptPW) ; lua_pop( L, 1) ; } return true ; } else return false ; } //---------------------------------------------------------------------------- bool LuaGetRefType( lua_State* L, int nInd, int& nRefType) { // se non c'è il tipo cercato, ritorno string sRefType ; if ( ! LuaGetParam( L, nInd, sRefType)) return false ; // interpreto il parametro nRefType = StringToRefType( sRefType) ; return true ; } //---------------------------------------------------------------------------- bool LuaGetRefId( lua_State* L, int nInd, int& nRefId) { // se parametro è numero intero if ( LuaGetParam( L, nInd, nRefId)) return true ; // se parametro è stringa string sFlag ; if ( LuaGetParam( L, nInd, sFlag)) { nRefId = StringToRefId( sFlag) ; return true ; } // non trovato return false ; } //---------------------------------------------------------------------------- bool LuaClearStack( lua_State* L) { int n = lua_gettop( L) ; if ( n > 0) lua_pop( L, n) ; return true ; } //---------------------------------------------------------------------------- bool LuaSetReturn( lua_State* L) { try { lua_pushnil( L) ; } catch ( ...) { return false ; } return true ; } //---------------------------------------------------------------------------- bool LuaSetReturn( lua_State* L, bool bPar) { try { lua_pushboolean( L, ( bPar ? 1 : 0)) ; } catch ( ...) { return false ; } return true ; } //---------------------------------------------------------------------------- bool LuaSetReturn( lua_State* L, int nPar) { try { lua_pushinteger( L, nPar) ; } catch ( ...) { return false ; } return true ; } //---------------------------------------------------------------------------- bool LuaSetReturn( lua_State* L, double dPar) { try { lua_pushnumber( L, dPar) ; } catch ( ...) { return false ; } return true ; } //---------------------------------------------------------------------------- bool LuaSetReturn( lua_State* L, const string& sPar) { try { lua_pushstring( L, sPar.c_str()) ; } catch ( ...) { return false ; } return true ; } //---------------------------------------------------------------------------- bool LuaSetReturn( lua_State* L, const Vector3d& vtPar) { try { lua_createtable( L, 3, 0) ; for ( int i = 1 ; i <= 3 ; ++ i) { lua_pushnumber( L, vtPar.v[i-1]) ; lua_rawseti( L, -2, i) ; } } catch( ...) { return false ; } return true ; } //---------------------------------------------------------------------------- bool LuaSetReturn( lua_State* L, const Point3d& ptPar) { try { lua_createtable( L, 3, 0) ; for ( int i = 1 ; i <= 3 ; ++ i) { lua_pushnumber( L, ptPar.v[i-1]) ; lua_rawseti( L, -2, i) ; } } catch( ...) { return false ; } return true ; } //------------------------------------------------------------------------------- bool LuaSetReturn( lua_State* L, const Frame3d& frPar) { try { // creo tavola per frame lua_createtable( L, 4, 0) ; // creo tavola per origine lua_createtable( L, 3, 0) ; lua_pushnumber( L, frPar.Orig().x) ; lua_rawseti( L, -2, 1) ; lua_pushnumber( L, frPar.Orig().y) ; lua_rawseti( L, -2, 2) ; lua_pushnumber( L, frPar.Orig().z) ; lua_rawseti( L, -2, 3) ; // la metto nel frame lua_rawseti( L, -2, 1) ; // creo tavola per versore X lua_createtable( L, 3, 0) ; lua_pushnumber( L, frPar.VersX().x) ; lua_rawseti( L, -2, 1) ; lua_pushnumber( L, frPar.VersX().y) ; lua_rawseti( L, -2, 2) ; lua_pushnumber( L, frPar.VersX().z) ; lua_rawseti( L, -2, 3) ; // la metto nel frame lua_rawseti( L, -2, 2) ; // creo tavola per versore Y lua_createtable( L, 3, 0) ; lua_pushnumber( L, frPar.VersY().x) ; lua_rawseti( L, -2, 1) ; lua_pushnumber( L, frPar.VersY().y) ; lua_rawseti( L, -2, 2) ; lua_pushnumber( L, frPar.VersY().z) ; lua_rawseti( L, -2, 3) ; // la metto nel frame lua_rawseti( L, -2, 3) ; // creo tavola per versore Z lua_createtable( L, 3, 0) ; lua_pushnumber( L, frPar.VersZ().x) ; lua_rawseti( L, -2, 1) ; lua_pushnumber( L, frPar.VersZ().y) ; lua_rawseti( L, -2, 2) ; lua_pushnumber( L, frPar.VersZ().z) ; lua_rawseti( L, -2, 3) ; // la metto nel frame lua_rawseti( L, -2, 4) ; } catch( ...) { return false ; } return true ; } //------------------------------------------------------------------------------- bool LuaSetReturn( lua_State* L, const Color& colPar) { try { lua_createtable( L, 4, 0) ; lua_pushinteger( L, colPar.GetIntRed()) ; lua_rawseti( L, -2, 1) ; lua_pushinteger( L, colPar.GetIntGreen()) ; lua_rawseti( L, -2, 2) ; lua_pushinteger( L, colPar.GetIntBlue()) ; lua_rawseti( L, -2, 3) ; lua_pushinteger( L, colPar.GetIntAlpha()) ; lua_rawseti( L, -2, 4) ; } catch( ...) { return false ; } return true ; }