From d5a12f813e2ca03023b07e65594c6cfb21be45d5 Mon Sep 17 00:00:00 2001 From: Malin Ejdbo Date: Mon, 28 Jun 2021 12:05:21 +0200 Subject: [PATCH] Enable both strings and numbers to lua ra dec conversion function --- include/openspace/util/coordinateconversion.h | 10 +++--- modules/space/spacemodule_lua.inl | 31 ++++++++++++++----- src/util/coordinateconversion.cpp | 21 +++++++------ 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/include/openspace/util/coordinateconversion.h b/include/openspace/util/coordinateconversion.h index d33a46c2ae..009fd3c562 100644 --- a/include/openspace/util/coordinateconversion.h +++ b/include/openspace/util/coordinateconversion.h @@ -35,17 +35,18 @@ namespace openspace { * \param ra Right ascension, given in decimal degrees * \param dec Declination, given in decimal degrees * \param distance The distance, or radius, to the position given in any unit. + * \param boolean to say if the incoming ra and dec are in degrees or radians * \return A position in galactic cartesian coordinates, given in the same unit as the * distance parameter. */ -glm::dvec3 icrsToGalacticCartesian(double ra, double dec, double distance); +glm::dvec3 icrsToGalacticCartesian(double ra, double dec, double distance, bool isDegrees = true); /** * Converts from ICRS (hms and dms) coordinates to decimal degrees. * \param ra Right ascension, given as a string in format "XXhYYmZZs" * \param dec Declination, given as a string in format "XXdYYmZZs", "-XXdYYmZZs", * "XdYmZs" or "-XdYmZs" - * \return The decimal degrees coordinate + * \return The decimal degrees coordinate in degrees */ glm::dvec2 icrsToDecimalDegrees(const std::string& ra, const std::string& dec); @@ -55,7 +56,7 @@ glm::dvec2 icrsToDecimalDegrees(const std::string& ra, const std::string& dec); * \param x X coordinate * \param y Y coordinate * \param z Z coordinate - * \return A vector with the ra and dec decimal degrees and distance. + * \return A vector with the ra and dec decimal degrees in degrees and distance. */ glm::dvec3 galacticCartesianToIcrs(double x, double y, double z); @@ -63,9 +64,10 @@ glm::dvec3 galacticCartesianToIcrs(double x, double y, double z); * Converts from ICRS (decimal degrees) coordinates to ICRS (hms and dms) coordinates. * \param ra Right ascension, given in decimal degrees * \param dec Declination, given in decimal degrees + * \param boolean to say if the incoming ra and dec are in degrees or radians * \return A pair with the ra and dec strings in hms and dms format. */ -std::pair decimalDegreesToIcrs(double ra, double dec); +std::pair decimalDegreesToIcrs(double ra, double dec, bool isDegrees = true); } // namespace openspace diff --git a/modules/space/spacemodule_lua.inl b/modules/space/spacemodule_lua.inl index 44ac4ae9a9..696b9dd344 100644 --- a/modules/space/spacemodule_lua.inl +++ b/modules/space/spacemodule_lua.inl @@ -32,16 +32,32 @@ namespace openspace::space::luascriptfunctions { int convertFromRaDec(lua_State* L) { - ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::convertFromRaDec"); + ghoul::lua::checkArgumentsAndThrow(L, { 3, 4 }, "lua::convertFromRaDec"); + + glm::dvec2 degrees = glm::dvec2(0.0); + bool isDegrees = true; + if (lua_type(L, 1) == LUA_TSTRING && lua_type(L, 2) == LUA_TSTRING) { + std::string s_ra = ghoul::lua::value(L, 1); + std::string s_dec = ghoul::lua::value(L, 2); + degrees = icrsToDecimalDegrees(s_ra, s_dec); + } + else if (lua_type(L, 1) == LUA_TNUMBER && lua_type(L, 2) == LUA_TNUMBER) { + degrees.x = ghoul::lua::value(L, 1); + degrees.y = ghoul::lua::value(L, 2); + if (lua_gettop(L) >= 4) { + isDegrees = ghoul::lua::value(L, 4); + } + } + else { + throw ghoul::lua::LuaRuntimeException("lua::convertFromRaDec: Ra and Dec have to " + "be of the same type, either String or Number" + ); + } - 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); - + glm::dvec3 pos = icrsToGalacticCartesian(degrees.x, degrees.y, distance, isDegrees); ghoul::lua::push(L, pos); ghoul_assert(lua_gettop(L) == 1, "Incorrect number of items left on stack"); @@ -57,7 +73,8 @@ int convertToRaDec(lua_State* L) { lua_settop(L, 0); glm::dvec3 degrees = galacticCartesianToIcrs(x, y, z); - std::pair raDecPair = decimalDegreesToIcrs(degrees.x, degrees.y); + std::pair raDecPair + = decimalDegreesToIcrs(degrees.x, degrees.y); ghoul::lua::push(L, raDecPair.first); // Ra ghoul::lua::push(L, raDecPair.second); // Dec diff --git a/src/util/coordinateconversion.cpp b/src/util/coordinateconversion.cpp index e6885339bd..9428119d76 100644 --- a/src/util/coordinateconversion.cpp +++ b/src/util/coordinateconversion.cpp @@ -62,7 +62,6 @@ namespace { try { // Hours or degrees must be an integer double temp = std::stod(s_hours_or_degrees); - std::cout << "double h: " << temp << std::endl; if (std::floor(temp) != temp) { throw(ghoul::lua::LuaRuntimeException(fmt::format( "Ra or Dec '{}' format is incorrect. Correct format is: Ra 'XhYmZs', " @@ -73,7 +72,6 @@ namespace { // Minutes must be an integer temp = std::stod(s_minutes); - std::cout << "double m: " << temp << std::endl; if (std::floor(temp) != temp) { throw(ghoul::lua::LuaRuntimeException(fmt::format( "Ra or Dec '{}' format is incorrect. Correct format is: Ra 'XhYmZs', " @@ -170,13 +168,15 @@ namespace openspace { // Convert Equatorial coordinates ICRS right ascension and declination (a, d) // into Galactic coordinates (l, b) -glm::dvec3 icrsToGalacticCartesian(double ra, double dec, double distance) { +glm::dvec3 icrsToGalacticCartesian(double ra, double dec, double distance, + bool isDegrees) +{ // Reference: // https://www.atnf.csiro.au/people/Tobias.Westmeier/tools_coords.php // (Ra, Dec) -> (a, d) - double a = glm::radians(ra); - double d = glm::radians(dec); + double a = isDegrees ? glm::radians(ra) : ra; + double d = isDegrees ? glm::radians(dec) : dec; // J2000 Galactic reference frame constexpr double a0 = glm::radians(192.8595); // Equatorial coordinates of the Galactic north pole @@ -273,20 +273,21 @@ glm::dvec3 galacticCartesianToIcrs(double x, double y, double z) { ) + a0; double d = asin(sin(b) * sin(d0) + cos(b) * cos(d0) * cos(l0 - l)); - glm::dvec3 rEquatorial = glm::dvec3(a, d, distance); - + glm::dvec3 rEquatorial = glm::dvec3(glm::degrees(a), glm::degrees(d), distance); return rEquatorial; } // Return a pair with two formatted strings from the decimal degrees ra and dec -std::pair decimalDegreesToIcrs(double ra, double dec) { +std::pair decimalDegreesToIcrs(double ra, double dec, + bool isDegrees) +{ // References: // https://www.rapidtables.com/convert/number/degrees-to-degrees-minutes-seconds.html, // https://math.stackexchange.com/questions/15323/how-do-i-calculate-the-cartesian-coordinates-of-stars // Radians to degrees - double ra_deg = glm::degrees(ra); - double dec_deg = glm::degrees(dec); + double ra_deg = isDegrees ? ra : glm::degrees(ra); + double dec_deg = isDegrees ? dec : glm::degrees(dec); // Check input if (ra_deg < 0 || ra_deg > 360 || dec_deg < -90 || dec_deg > 90) {