From cd7bd0fc4b0e3da85f201966867d0efd8f807fec Mon Sep 17 00:00:00 2001 From: Malin Ejdbo Date: Mon, 28 Jun 2021 09:20:27 +0200 Subject: [PATCH] Move ra dec conversion lua function to the space module --- modules/space/CMakeLists.txt | 1 + modules/space/spacemodule.cpp | 26 ++++++++++++ modules/space/spacemodule.h | 2 + modules/space/spacemodule_lua.inl | 70 +++++++++++++++++++++++++++++++ src/scene/scene.cpp | 14 ------- src/scene/scene_lua.inl | 38 ----------------- 6 files changed, 99 insertions(+), 52 deletions(-) create mode 100644 modules/space/spacemodule_lua.inl diff --git a/modules/space/CMakeLists.txt b/modules/space/CMakeLists.txt index 09e56ed641..a6ce4f383c 100644 --- a/modules/space/CMakeLists.txt +++ b/modules/space/CMakeLists.txt @@ -44,6 +44,7 @@ set(HEADER_FILES source_group("Header Files" FILES ${HEADER_FILES}) set(SOURCE_FILES + spacemodule_lua.inl speckloader.cpp rendering/planetgeometry.cpp rendering/renderableconstellationbounds.cpp diff --git a/modules/space/spacemodule.cpp b/modules/space/spacemodule.cpp index 0ebba746cd..98d4a88be4 100644 --- a/modules/space/spacemodule.cpp +++ b/modules/space/spacemodule.cpp @@ -39,11 +39,14 @@ #include #include #include +#include #include #include #include #include +#include "spacemodule_lua.inl" + namespace { constexpr openspace::properties::Property::PropertyInfo SpiceExceptionInfo = { "ShowExceptions", @@ -133,4 +136,27 @@ std::vector SpaceModule::documentations() const { }; } +scripting::LuaLibrary SpaceModule::luaLibrary() const { + scripting::LuaLibrary res; + res.name = "space"; + res.functions = { + { + "convertFromRaDec", + &space::luascriptfunctions::convertFromRaDec, + {}, + "string, string, double", + "Returns the cartesian world position of a ra dec coordinate with distance" + }, + { + "convertToRaDec", + &space::luascriptfunctions::convertToRaDec, + {}, + "double, double, double", + "Returns the ra, dec strings and distance for a given cartesian world coordinate" + } + }; + + return res; +} + } // namespace openspace diff --git a/modules/space/spacemodule.h b/modules/space/spacemodule.h index b2630b6277..bfa65f22b4 100644 --- a/modules/space/spacemodule.h +++ b/modules/space/spacemodule.h @@ -42,6 +42,8 @@ public: static ghoul::opengl::ProgramObjectManager ProgramObjectManager; + scripting::LuaLibrary luaLibrary() const override; + private: void internalInitialize(const ghoul::Dictionary&) override; void internalDeinitializeGL() override; diff --git a/modules/space/spacemodule_lua.inl b/modules/space/spacemodule_lua.inl new file mode 100644 index 0000000000..44ac4ae9a9 --- /dev/null +++ b/modules/space/spacemodule_lua.inl @@ -0,0 +1,70 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2021 * + * * + * 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 +#include +#include +#include +#include +#include + +namespace openspace::space::luascriptfunctions { + +int convertFromRaDec(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::convertFromRaDec"); + + std::string ra = ghoul::lua::value(L, 1); + std::string dec = ghoul::lua::value(L, 2); + double distance = ghoul::lua::value(L, 3); + lua_settop(L, 0); + + glm::dvec2 degrees = icrsToDecimalDegrees(ra, dec); + glm::dvec3 pos = icrsToGalacticCartesian(degrees.x, degrees.y, distance); + + ghoul::lua::push(L, pos); + + ghoul_assert(lua_gettop(L) == 1, "Incorrect number of items left on stack"); + return 1; +} + +int convertToRaDec(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::convertToRaDec"); + + double x = ghoul::lua::value(L, 1); + double y = ghoul::lua::value(L, 2); + double z = ghoul::lua::value(L, 3); + lua_settop(L, 0); + + glm::dvec3 degrees = galacticCartesianToIcrs(x, y, z); + std::pair raDecPair = decimalDegreesToIcrs(degrees.x, degrees.y); + + ghoul::lua::push(L, raDecPair.first); // Ra + ghoul::lua::push(L, raDecPair.second); // Dec + ghoul::lua::push(L, degrees.z); // Distance + + ghoul_assert(lua_gettop(L) == 3, "Incorrect number of items left on stack"); + return 3; +} + +} // namespace openspace::space::luascriptfunctions diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp index c8e23f9573..d39208d1a1 100644 --- a/src/scene/scene.cpp +++ b/src/scene/scene.cpp @@ -731,20 +731,6 @@ scripting::LuaLibrary Scene::luaLibrary() { {}, "string", "Returns the world rotation matrix of the scene graph node with the given string as identifier" - }, - { - "convertFromRaDec", - &luascriptfunctions::convertFromRaDec, - {}, - "string, string, double", - "Returns the cartesian world position of a ra dec coordinate with distance" - }, - { - "convertToRaDec", - &luascriptfunctions::convertToRaDec, - {}, - "double, double, double", - "Returns the ra, dec strings and distance for a given cartesian world coordinate" } } }; diff --git a/src/scene/scene_lua.inl b/src/scene/scene_lua.inl index bdaf0c1f7c..cb1d33fd52 100644 --- a/src/scene/scene_lua.inl +++ b/src/scene/scene_lua.inl @@ -28,8 +28,6 @@ #include #include -#include - namespace openspace { namespace { @@ -950,40 +948,4 @@ int worldRotation(lua_State* L) { return 1; } -int convertFromRaDec(lua_State* L) { - ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::convertFromRaDec"); - - std::string ra = ghoul::lua::value(L, 1); - std::string dec = ghoul::lua::value(L, 2); - double distance = ghoul::lua::value(L, 3); - lua_settop(L, 0); - - glm::dvec2 degrees = icrsToDecimalDegrees(ra, dec); - glm::dvec3 pos = icrsToGalacticCartesian(degrees.x, degrees.y, distance); - - ghoul::lua::push(L, pos); - - ghoul_assert(lua_gettop(L) == 1, "Incorrect number of items left on stack"); - return 1; -} - -int convertToRaDec(lua_State* L) { - ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::convertToRaDec"); - - double x = ghoul::lua::value(L, 1); - double y = ghoul::lua::value(L, 2); - double z = ghoul::lua::value(L, 3); - lua_settop(L, 0); - - glm::dvec3 degrees = galacticCartesianToIcrs(x, y, z); - std::pair raDecPair = decimalDegreesToIcrs(degrees.x, degrees.y); - - ghoul::lua::push(L, raDecPair.first); // Ra - ghoul::lua::push(L, raDecPair.second); // Dec - ghoul::lua::push(L, degrees.z); // Distance - - ghoul_assert(lua_gettop(L) == 3, "Incorrect number of items left on stack"); - return 3; -} - } // namespace openspace::luascriptfunctions