diff --git a/include/openspace/util/spicemanager.h b/include/openspace/util/spicemanager.h index 4e0eb64fcf..f0041b6679 100644 --- a/include/openspace/util/spicemanager.h +++ b/include/openspace/util/spicemanager.h @@ -25,6 +25,7 @@ #ifndef __SPICEMANAGER_H__ #define __SPICEMANAGER_H__ +#include #include #include @@ -47,7 +48,6 @@ namespace openspace { class SpiceManager : public ghoul::Singleton { friend class ghoul::Singleton; - public: using TransformMatrix = std::array; using KernelHandle = unsigned int; @@ -809,6 +809,8 @@ public: */ std::string frameFromBody(const std::string& body) const; + static scripting::LuaLibrary luaLibrary(); + private: /// Struct storing the information about all loaded kernels struct KernelInformation { diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 9cb627b5c2..f308efdb12 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -382,6 +382,7 @@ bool OpenSpaceEngine::initialize() { // Register Lua script functions LDEBUG("Registering Lua libraries"); _scriptEngine->addLibrary(OpenSpaceEngine::luaLibrary()); + _scriptEngine->addLibrary(SpiceManager::luaLibrary()); _scriptEngine->addLibrary(RenderEngine::luaLibrary()); _scriptEngine->addLibrary(Scene::luaLibrary()); _scriptEngine->addLibrary(Time::luaLibrary()); diff --git a/src/util/spicemanager.cpp b/src/util/spicemanager.cpp index b76b21bf55..fbc87d1012 100644 --- a/src/util/spicemanager.cpp +++ b/src/util/spicemanager.cpp @@ -78,6 +78,8 @@ namespace { using fmt::format; using std::string; +#include "spicemanager_lua.inl" + namespace openspace { @@ -1109,6 +1111,28 @@ glm::dmat3 SpiceManager::getEstimatedTransformMatrix(const std::string& fromFram return result; } +scripting::LuaLibrary SpiceManager::luaLibrary() { + return { + "spice", + { + { + "loadKernel", + &luascriptfunctions::loadKernel, + "string", + "Loads the provided SPICE kernel by name. The name can contain path " + "tokens, which are automatically resolved" + }, + { + "unloadKernel", + &luascriptfunctions::unloadKernel, + "{string, number}", + "Unloads the provided SPICE kernel. The name can contain path tokens, " + "which are automatically resolved" + } + } + }; +} + } // namespace openspace diff --git a/src/util/spicemanager_lua.inl b/src/util/spicemanager_lua.inl new file mode 100644 index 0000000000..b37d6965d7 --- /dev/null +++ b/src/util/spicemanager_lua.inl @@ -0,0 +1,91 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2016 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +#include + +namespace openspace { +namespace luascriptfunctions { + +/** + * \ingroup LuaScripts + * loadKernel(string): + * Loads the provided SPICE kernel by name. The name can contain path tokens, which are + * automatically resolved. + */ + +int loadKernel(lua_State* L) { + const std::string _loggerCat = "loadKernel"; + + int nArguments = lua_gettop(L); + SCRIPT_CHECK_ARGUMENTS(L, 1, nArguments); + + bool isString = (lua_isstring(L, -1) == 1); + if (!isString) { + LERROR(ghoul::lua::errorLocation(L) << "Expected argument of type 'string'"); + return 0; + } + + std::string argument = lua_tostring(L, -1); + unsigned int result = SpiceManager::ref().loadKernel(argument); + + lua_pushnumber(L, result); + return 1; +} + +/** + * unloadKernel({string, number}): + * Unloads the provided SPICE kernel. The name can contain path tokens, which are + * automatically resolved. + */ +int unloadKernel(lua_State* L) { + const std::string _loggerCat = "loadKernel"; + + int nArguments = lua_gettop(L); + SCRIPT_CHECK_ARGUMENTS(L, 1, nArguments); + + bool isString = (lua_isstring(L, -1) == 1); + bool isNumber = (lua_isnumber(L, -1) == 1); + + if (!isString && !isNumber) { + LERROR( + ghoul::lua::errorLocation(L) << + "Expected argument of type 'string' or 'number'" + ); + return 0; + } + + if (isString) { + std::string argument = lua_tostring(L, -1); + SpiceManager::ref().unloadKernel(argument); + return 0; + } + + if (isNumber) { + unsigned int argument = static_cast(lua_tonumber(L, -1)); + SpiceManager::ref().unloadKernel(argument); + } +} + +} // luascriptfunctions +} // namespace openspace