From 11425dfdb7a8bfcfb9de1b9af6b4670524ce217b Mon Sep 17 00:00:00 2001 From: Emma Broman Date: Thu, 15 Oct 2020 16:03:38 +0200 Subject: [PATCH] Replace unused system rotation computation with the one that is actually used --- modules/exoplanets/exoplanetshelper.cpp | 66 ++++++++++----------- modules/exoplanets/exoplanetshelper.h | 2 +- modules/exoplanets/exoplanetsmodule_lua.inl | 36 +---------- 3 files changed, 34 insertions(+), 70 deletions(-) diff --git a/modules/exoplanets/exoplanetshelper.cpp b/modules/exoplanets/exoplanetshelper.cpp index 1db7a0f564..99af61238f 100644 --- a/modules/exoplanets/exoplanetshelper.cpp +++ b/modules/exoplanets/exoplanetshelper.cpp @@ -24,6 +24,7 @@ #include +#include #include #include #include @@ -137,44 +138,41 @@ glm::dmat4 computeOrbitPlaneRotationMatrix(float i, float bigom, float omega) { return orbitPlaneRotation; } -// Rotate the original coordinate system (where x is pointing to First Point of Aries) -// so that x is pointing from star to the sun. -// Modified from "http://www.opengl-tutorial.org/intermediate-tutorials/ -// tutorial-17-quaternions/ #how-do-i-find-the-rotation-between-2-vectors" -glm::dmat3 exoplanetSystemRotation(glm::dvec3 start, glm::dvec3 end) { - glm::quat rotationQuat; - glm::dvec3 rotationAxis; - const float cosTheta = static_cast(glm::dot(start, end)); - constexpr float Epsilon = 1E-3f; +glm::dmat3 computeSystemRotation(glm::dvec3 starPosition) { + const glm::dvec3 sunPosition = glm::dvec3(0.0, 0.0, 0.0); + const glm::dvec3 starToSunVec = glm::normalize(sunPosition - starPosition); + const glm::dvec3 galacticNorth = glm::dvec3(0.0, 0.0, 1.0); - if (cosTheta < -1.f + Epsilon) { - // special case when vectors in opposite directions: - // there is no "ideal" rotation axis - // So guess one; any will do as long as it's perpendicular to start vector - rotationAxis = glm::cross(glm::dvec3(0.0, 0.0, 1.0), start); - if (glm::length2(rotationAxis) < 0.01f) { - // bad luck, they were parallel, try again! - rotationAxis = glm::cross(glm::dvec3(1.0, 0.0, 0.0), start); - } + const glm::dmat3 galacticToCelestialMatrix = + SpiceManager::ref().positionTransformMatrix("GALACTIC", "J2000", 0.0); - rotationAxis = glm::normalize(rotationAxis); - rotationQuat = glm::quat(glm::radians(180.f), rotationAxis); - return glm::dmat3(glm::toMat4(rotationQuat)); - } - - rotationAxis = glm::cross(start, end); - - const float s = sqrt((1.f + cosTheta) * 2.f); - const float invs = 1.f / s; - - rotationQuat = glm::quat( - s * 0.5f, - static_cast(rotationAxis.x * invs), - static_cast(rotationAxis.y * invs), - static_cast(rotationAxis.z * invs) + const glm::dvec3 celestialNorth = glm::normalize( + galacticToCelestialMatrix * galacticNorth ); - return glm::dmat3(glm::toMat4(rotationQuat)); + // Earth's north vector projected onto the skyplane, the plane perpendicular to the + // viewing vector (starToSunVec) + const float celestialAngle = static_cast(glm::dot( + celestialNorth, + starToSunVec + )); + glm::dvec3 northProjected = glm::normalize( + celestialNorth - (celestialAngle / glm::length(starToSunVec)) * starToSunVec + ); + + const glm::dvec3 beta = glm::normalize(glm::cross(starToSunVec, northProjected)); + + return glm::dmat3( + northProjected.x, + northProjected.y, + northProjected.z, + beta.x, + beta.y, + beta.z, + starToSunVec.x, + starToSunVec.y, + starToSunVec.z + ); } std::string createIdentifier(std::string name) { diff --git a/modules/exoplanets/exoplanetshelper.h b/modules/exoplanets/exoplanetshelper.h index a8637d6cfe..e5a0fcc729 100644 --- a/modules/exoplanets/exoplanetshelper.h +++ b/modules/exoplanets/exoplanetshelper.h @@ -82,7 +82,7 @@ glm::dmat4 computeOrbitPlaneRotationMatrix(float i, float bigom, float omega); // Rotate the original coordinate system (where x is pointing to First Point of Aries) // so that x is pointing from star to the sun. -glm::dmat3 exoplanetSystemRotation(glm::dvec3 start, glm::dvec3 end); +glm::dmat3 computeSystemRotation(glm::dvec3 starPosition); // Create an identifier without whitespaces std::string createIdentifier(std::string name); diff --git a/modules/exoplanets/exoplanetsmodule_lua.inl b/modules/exoplanets/exoplanetsmodule_lua.inl index fec7f88706..79884f0b5a 100644 --- a/modules/exoplanets/exoplanetsmodule_lua.inl +++ b/modules/exoplanets/exoplanetsmodule_lua.inl @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -146,40 +145,7 @@ void createExoplanetSystem(std::string_view starName) { p.positionZ * distanceconstants::Parsec ); - const glm::dvec3 sunPosition = glm::dvec3(0.0, 0.0, 0.0); - const glm::dvec3 starToSunVec = glm::normalize(sunPosition - starPosition); - const glm::dvec3 galacticNorth = glm::dvec3(0.0, 0.0, 1.0); - - const glm::dmat3 galaxticToCelestialMatrix = - SpiceManager::ref().positionTransformMatrix("GALACTIC", "J2000", 0.0); - - const glm::dvec3 celestialNorth = glm::normalize( - galaxticToCelestialMatrix * galacticNorth - ); - - // Earth's north vector projected onto the skyplane, the plane perpendicular to the - // viewing vector (starToSunVec) - const float celestialAngle = static_cast(glm::dot( - celestialNorth, - starToSunVec - )); - glm::dvec3 northProjected = glm::normalize( - celestialNorth - (celestialAngle / glm::length(starToSunVec)) * starToSunVec - ); - - const glm::dvec3 beta = glm::normalize(glm::cross(starToSunVec, northProjected)); - - const glm::dmat3 exoplanetSystemRotation = glm::dmat3( - northProjected.x, - northProjected.y, - northProjected.z, - beta.x, - beta.y, - beta.z, - starToSunVec.x, - starToSunVec.y, - starToSunVec.z - ); + const glm::dmat3 exoplanetSystemRotation = computeSystemRotation(starPosition); // Star renderable globe, if we have a radius and bv color index std::string starGlobeRenderableString;