Add initial version of lua ra dec conversion function

This commit is contained in:
Malin Ejdbo
2021-06-15 09:18:33 +02:00
parent b46dec3e1b
commit 9043f7d3cd
4 changed files with 78 additions and 0 deletions

View File

@@ -26,6 +26,7 @@
#define __OPENSPACE_CORE___COORDINATECONVERSION___H__
#include <ghoul/glm.h>
#include <string>
namespace openspace {
@@ -39,6 +40,14 @@ namespace openspace {
*/
glm::dvec3 icrsToGalacticCartesian(float ra, float dec, double distance);
/**
* 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" or "-XXdYYmZZs"
* \return The decimal degrees coordinate
*/
glm::dvec2 icrsToDecimalDegrees(std::string ra, std::string dec);
} // namespace openspace
#endif // __OPENSPACE_CORE___COORDINATECONVERSION___H__

View File

@@ -731,6 +731,13 @@ scripting::LuaLibrary Scene::luaLibrary() {
{},
"string",
"Returns the world rotation matrix of the scene graph node with the given string as identifier"
},
{
"convertRaDec",
&luascriptfunctions::convertRaDec,
{},
"string, string, float",
"Returns the cartesian world position of a ra dec coordinate with distance"
}
}
};

View File

@@ -28,6 +28,8 @@
#include <ghoul/misc/defer.h>
#include <ghoul/misc/easing.h>
#include <openspace/util/coordinateconversion.h>
namespace openspace {
namespace {
@@ -948,4 +950,21 @@ int worldRotation(lua_State* L) {
return 1;
}
int convertRaDec(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::convertRaDec");
std::string ra = ghoul::lua::value<std::string>(L, 1);
std::string dec = ghoul::lua::value<std::string>(L, 2);
float distance = ghoul::lua::value<float>(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;
}
} // namespace openspace::luascriptfunctions

View File

@@ -58,4 +58,47 @@ glm::dvec3 icrsToGalacticCartesian(float ra, float dec, double distance) {
return distance * rGalactic;
}
// ra format "XXhYYmZZs" or "XhYmZs"
// dec format "+XXdYYmZZs", "-XXdYYmZZs" or "-XdYmZs"
glm::dvec2 icrsToDecimalDegrees(std::string ra, std::string dec) {
if (ra.size() > 9 || ra.size() < 6 || dec.size() > 10 || dec.size() < 7) {
// Stirngs too big or small
}
// Parse right ascension
size_t ra_h_index = ra.find('h');
size_t ra_m_index = ra.find('m');
size_t ra_s_index = ra.find('s');
if (ra_h_index == std::string::npos || ra_m_index == std::string::npos ||
ra_s_index == std::string::npos)
{
// Format not correct
}
float ra_hours = std::stof(ra.substr(0, ra_h_index));
float ra_minutes = std::stof(ra.substr(ra_h_index + 1, ra_m_index - ra_h_index - 1));
float ra_seconds = std::stof(ra.substr(ra_m_index + 1, ra_s_index - ra_m_index - 1));
// Parse declination
size_t dec_d_index = dec.find('d');
size_t dec_m_index = dec.find('m');
size_t dec_s_index = dec.find('s');
if (dec_d_index == std::string::npos || dec_m_index == std::string::npos ||
dec_s_index == std::string::npos)
{
// Format not correct
}
float dec_degrees = std::stof(dec.substr(0, dec_d_index));
float dec_minutes = std::stof(dec.substr(dec_d_index + 1, dec_m_index - dec_d_index - 1));
float dec_seconds = std::stof(dec.substr(dec_m_index + 1, dec_s_index - dec_m_index - 1));
// Convert from hours, minutes, sewconds to degrees
float sign = signbit(dec_degrees) ? -1.f : 1.f;
float ra_deg = (ra_hours * 15.0) + (ra_minutes * 15 / 60.0) + (ra_seconds * 15 / 3600.0);
float dec_deg = (abs(dec_degrees) + (dec_minutes / 60.0) + (dec_seconds / 3600.0)) * sign;
return glm::dvec2(ra_deg, dec_deg);
}
} // namespace openspace