From e51b104ce86a25eabaa32cd117c1726440f1ccfc Mon Sep 17 00:00:00 2001 From: Sebastian Piwell Date: Tue, 10 May 2016 11:18:09 -0400 Subject: [PATCH] Transformation Manager --- .../openspace/util/transformationmanager.h | 52 +++++ modules/iswa/rendering/iswacygnet.cpp | 4 +- modules/iswa/util/iswamanager.cpp | 198 ++++++------------ modules/iswa/util/iswamanager.h | 2 - src/CMakeLists.txt | 2 + src/engine/openspaceengine.cpp | 3 + src/util/transformationmanager.cpp | 127 +++++++++++ 7 files changed, 249 insertions(+), 139 deletions(-) create mode 100644 include/openspace/util/transformationmanager.h create mode 100644 src/util/transformationmanager.cpp diff --git a/include/openspace/util/transformationmanager.h b/include/openspace/util/transformationmanager.h new file mode 100644 index 0000000000..9dd3c2f9e4 --- /dev/null +++ b/include/openspace/util/transformationmanager.h @@ -0,0 +1,52 @@ +/***************************************************************************************** +* * +* 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. * +****************************************************************************************/ + +#ifndef __TRANSFORMATIONMANAGER__ +#define __TRANSFORMATIONMANAGER__ + +#include +#include +#include + +namespace openspace { +class ccmc::Kameleon; + +class TransformationManager : public ghoul::Singleton { + friend class ghoul::Singleton; + +public: + TransformationManager(); + ~TransformationManager(); + + glm::dmat3 frameTransformationMatrix(std::string from, std::string to, double ephemerisTime) const; + +private: + std::shared_ptr _kameleon; + std::set _kameleonFrames; + std::set _dipoleFrames; +}; + +} + + #endif //__TRANSFORMATIONMANAGER__ \ No newline at end of file diff --git a/modules/iswa/rendering/iswacygnet.cpp b/modules/iswa/rendering/iswacygnet.cpp index 4e3c5ae5bd..2cecf2a311 100644 --- a/modules/iswa/rendering/iswacygnet.cpp +++ b/modules/iswa/rendering/iswacygnet.cpp @@ -27,6 +27,8 @@ #include #include #include +#include + namespace openspace{ @@ -175,7 +177,7 @@ void IswaCygnet::update(const UpdateData& data){ _realTime = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); - _stateMatrix = IswaManager::ref().getTransform(_data->frame, "GALACTIC", _openSpaceTime); + _stateMatrix = TransformationManager::ref().frameTransformationMatrix(_data->frame, "GALACTIC", _openSpaceTime); // glm::dmat3 spiceMatrix = SpiceManager::ref().positionTransformMatrix("J2000", "GALACTIC", _openSpaceTime); // = spiceMatrix*kameleonMatrix; diff --git a/modules/iswa/util/iswamanager.cpp b/modules/iswa/util/iswamanager.cpp index 2c2be87052..4842ef8ac8 100644 --- a/modules/iswa/util/iswamanager.cpp +++ b/modules/iswa/util/iswamanager.cpp @@ -70,11 +70,6 @@ IswaManager::IswaManager() _geom[CygnetGeometry::Plane] = "Plane"; _geom[CygnetGeometry::Sphere] = "Sphere"; - - _kameleon = std::make_shared(); - _kameleonFrames = { "J2000", "GEI", "GEO", "MAG", "GSE", "GSM", "SM", "RTN", "GSEQ", //geocentric - "HEE", "HAE", "HEEQ" //heliocentric - }; } IswaManager::~IswaManager(){ @@ -358,74 +353,74 @@ std::string IswaManager::parseJSONToLuaTable(std::shared_ptr dat return ""; } -void IswaManager::createKameleonPlane(std::string kwPath, int group){ - kwPath = "${OPENSPACE_DATA}/" + kwPath; - const std::string& extension = ghoul::filesystem::File(absPath(kwPath)).fileExtension(); +// void IswaManager::createKameleonPlane(std::string kwPath, int group){ +// kwPath = "${OPENSPACE_DATA}/" + kwPath; +// const std::string& extension = ghoul::filesystem::File(absPath(kwPath)).fileExtension(); - if(FileSys.fileExists(absPath(kwPath)) && extension == "cdf"){ - std::string luaTable = parseKWToLuaTable(kwPath, group); - if(!luaTable.empty()){ - std::cout << luaTable << std::endl; - std::string script = "openspace.addSceneGraphNode(" + luaTable + ");"; - OsEng.scriptEngine().queueScript(script); - } - }else{ - LWARNING( kwPath + " is not a cdf file or can't be found."); - } -} - -std::string IswaManager::parseKWToLuaTable(std::string kwPath, int group){ - if(kwPath != ""){ - const std::string& extension = ghoul::filesystem::File(absPath(kwPath)).fileExtension(); - if(extension == "cdf"){ - KameleonWrapper kw = KameleonWrapper(absPath(kwPath)); +// if(FileSys.fileExists(absPath(kwPath)) && extension == "cdf"){ +// std::string luaTable = parseKWToLuaTable(kwPath, group); +// if(!luaTable.empty()){ +// std::cout << luaTable << std::endl; +// std::string script = "openspace.addSceneGraphNode(" + luaTable + ");"; +// OsEng.scriptEngine().queueScript(script); +// } +// }else{ +// LWARNING( kwPath + " is not a cdf file or can't be found."); +// } +// } +// +// std::string IswaManager::parseKWToLuaTable(std::string kwPath, int group){ +// if(kwPath != ""){ +// const std::string& extension = ghoul::filesystem::File(absPath(kwPath)).fileExtension(); +// if(extension == "cdf"){ +// KameleonWrapper kw = KameleonWrapper(absPath(kwPath)); - std::string parent = kw.getParent(); - std::string frame = kw.getFrame(); - glm::vec3 min = kw.getGridMin(); - glm::vec3 max = kw.getGridMax(); +// std::string parent = kw.getParent(); +// std::string frame = kw.getFrame(); +// glm::vec3 min = kw.getGridMin(); +// glm::vec3 max = kw.getGridMax(); - glm::vec4 spatialScale; - std::string coordinateType; +// glm::vec4 spatialScale; +// std::string coordinateType; - std::tuple < std::string, std::string, std::string > gridUnits = kw.getGridUnits(); - if (std::get<0>(gridUnits) == "R" && std::get<1>(gridUnits) == "R" && std::get<2>(gridUnits) == "R") { - spatialScale.x = 6.371f; - spatialScale.y = 6.371f; - spatialScale.z = 6.371f; - spatialScale.w = 6; +// std::tuple < std::string, std::string, std::string > gridUnits = kw.getGridUnits(); +// if (std::get<0>(gridUnits) == "R" && std::get<1>(gridUnits) == "R" && std::get<2>(gridUnits) == "R") { +// spatialScale.x = 6.371f; +// spatialScale.y = 6.371f; +// spatialScale.z = 6.371f; +// spatialScale.w = 6; - coordinateType = "Cartesian"; - }else{ - spatialScale = glm::vec4(1.0); - spatialScale.w = 1; //-log10(1.0f/max.x); +// coordinateType = "Cartesian"; +// }else{ +// spatialScale = glm::vec4(1.0); +// spatialScale.w = 1; //-log10(1.0f/max.x); - coordinateType = "Polar"; - } - std::string table = "{" - "Name = 'KameleonPlane0'," - "Parent = '" + parent + "', " - "Renderable = {" - "Type = 'KameleonPlane', " - "Id = 0 ," - "Frame = '" + frame + "' , " - "GridMin = " + std::to_string(min) + ", " - "GridMax = " + std::to_string(max) + ", " - "SpatialScale = " + std::to_string(spatialScale) + ", " - "UpdateTime = 0, " - "kwPath = '" + kwPath + "' ," - "axisCut = 'y' ," - "CoordinateType = '" + coordinateType + "', " - "Group = "+ std::to_string(group) + " ," - "}" - "}" - ; - // std::cout << table << std::endl; - return table; - } - } - return ""; -} +// coordinateType = "Polar"; +// } +// std::string table = "{" +// "Name = 'KameleonPlane0'," +// "Parent = '" + parent + "', " +// "Renderable = {" +// "Type = 'KameleonPlane', " +// "Id = 0 ," +// "Frame = '" + frame + "' , " +// "GridMin = " + std::to_string(min) + ", " +// "GridMax = " + std::to_string(max) + ", " +// "SpatialScale = " + std::to_string(spatialScale) + ", " +// "UpdateTime = 0, " +// "kwPath = '" + kwPath + "' ," +// "axisCut = 'y' ," +// "CoordinateType = '" + coordinateType + "', " +// "Group = "+ std::to_string(group) + " ," +// "}" +// "}" +// ; +// // std::cout << table << std::endl; +// return table; +// } +// } +// return ""; +// } void IswaManager::registerGroup(int id){ @@ -467,75 +462,6 @@ std::shared_ptr IswaManager::iswaGroup(std::string name){ return nullptr; } -glm::dmat3 IswaManager::getTransform(std::string from, std::string to, double et){ - std::set _diopoleFrames = {"GSM", "SM", "MAG"}; - - auto fromit = _diopoleFrames.find(from); - auto toit = _diopoleFrames.find(to); - - //diopole frame to J200 makes the frame rotate. - if(fromit != _diopoleFrames.end()) from = "GSE"; - if(toit != _diopoleFrames.end()) to = "GSE"; - - fromit = _kameleonFrames.find(from); - toit = _kameleonFrames.find(to); - - bool fromKameleon = (fromit != _kameleonFrames.end()); - bool toKameleon = (toit != _kameleonFrames.end()); - - ccmc::Position in0 = {1.f, 0.f, 0.f}; - ccmc::Position in1 = {0.f, 1.f, 0.f}; - ccmc::Position in2 = {0.f, 0.f , 1.f}; - - ccmc::Position out0; - ccmc::Position out1; - ccmc::Position out2; - - if(fromKameleon && toKameleon){ - _kameleon->_cxform(from.c_str(), to.c_str(), et, &in0, &out0); - _kameleon->_cxform(from.c_str(), to.c_str(), et, &in1, &out1); - _kameleon->_cxform(from.c_str(), to.c_str(), et, &in2, &out2); - - return glm::dmat3( - out0.c0 , out0.c1 , out0.c2, - out1.c0 , out1.c1 , out1.c2, - out2.c0 , out2.c1 , out2.c2 - ); - - }else if(fromKameleon && !toKameleon){ - _kameleon->_cxform(from.c_str(), "J2000", et, &in0, &out0); - _kameleon->_cxform(from.c_str(), "J2000", et, &in1, &out1); - _kameleon->_cxform(from.c_str(), "J2000", et, &in2, &out2); - - glm::dmat3 kameleonTrans( - out0.c0 , out0.c1 , out0.c2, - out1.c0 , out1.c1 , out1.c2, - out2.c0 , out2.c1 , out2.c2 - ); - - glm::dmat3 spiceTrans = SpiceManager::ref().frameTransformationMatrix("J2000", to, et); - - return spiceTrans*kameleonTrans; - - }else if(!fromKameleon && toKameleon){ - glm::dmat3 spiceTrans = SpiceManager::ref().frameTransformationMatrix(from, "J2000", et); - - _kameleon->_cxform("J2000", to.c_str(), et, &in0, &out0); - _kameleon->_cxform("J2000", to.c_str(), et, &in1, &out1); - _kameleon->_cxform("J2000", to.c_str(), et, &in2, &out2); - - glm::dmat3 kameleonTrans( - out0.c0 , out0.c1 , out0.c2, - out1.c0 , out1.c1 , out1.c2, - out2.c0 , out2.c1 , out2.c2 - ); - - return kameleonTrans*spiceTrans; - }else{ - return SpiceManager::ref().frameTransformationMatrix(from, to, et); - } -} - scripting::ScriptEngine::LuaLibrary IswaManager::luaLibrary() { return { "iswa", diff --git a/modules/iswa/util/iswamanager.h b/modules/iswa/util/iswamanager.h index 8e66af4392..383d60de6d 100644 --- a/modules/iswa/util/iswamanager.h +++ b/modules/iswa/util/iswamanager.h @@ -85,8 +85,6 @@ public: void registerOptionsToGroup(int id, const std::vector& options); std::shared_ptr iswaGroup(std::string name); - glm::dmat3 getTransform(std::string from, std::string to, double et); - static scripting::ScriptEngine::LuaLibrary luaLibrary(); std::string iswaUrl(int id, std::string type = "image"); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0c5612b595..4434b5755d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -89,6 +89,7 @@ set(OPENSPACE_SOURCE ${OPENSPACE_BASE_DIR}/src/util/syncbuffer.cpp ${OPENSPACE_BASE_DIR}/src/util/time.cpp ${OPENSPACE_BASE_DIR}/src/util/time_lua.inl + ${OPENSPACE_BASE_DIR}/src/util/transformationmanager.cpp ) set(OPENSPACE_HEADER @@ -166,6 +167,7 @@ set(OPENSPACE_HEADER ${OPENSPACE_BASE_DIR}/include/openspace/util/syncbuffer.h ${OPENSPACE_BASE_DIR}/include/openspace/util/time.h ${OPENSPACE_BASE_DIR}/include/openspace/util/updatestructures.h + ${OPENSPACE_BASE_DIR}/include/openspace/util/transformationmanager.h ) # Place files into source groups diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index a832cd84d5..1dedebdda5 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -142,6 +143,8 @@ OpenSpaceEngine::OpenSpaceEngine(std::string programName, SpiceManager::initialize(); Time::initialize(); ghoul::systemcapabilities::SystemCapabilities::initialize(); + TransformationManager::initialize(); + #ifdef OPENSPACE_MODULE_ISWA_ENABLED IswaManager::initialize(); #endif diff --git a/src/util/transformationmanager.cpp b/src/util/transformationmanager.cpp new file mode 100644 index 0000000000..d6a56d37b4 --- /dev/null +++ b/src/util/transformationmanager.cpp @@ -0,0 +1,127 @@ +/***************************************************************************************** + * * + * 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 +#include +#include + + + namespace { + const std::string _loggerCat = "TransformationManager"; + } + + namespace openspace{ + TransformationManager::TransformationManager(){ +#ifdef OPENSPACE_MODULE_KAMELEON_ENABLED + _kameleon = std::make_shared(); +#else + LWARNING("Kameleon module needed for transformations with dynamic frames"); +#endif + + _kameleonFrames = { "J2000", "GEI", "GEO", "MAG", "GSE", "GSM", "SM", "RTN", "GSEQ", //geocentric + "HEE", "HAE", "HEEQ" //heliocentric + }; + _dipoleFrames = {"GSM", "SM", "MAG"}; + } + + TransformationManager::~TransformationManager(){ + _kameleon = nullptr; + } + + glm::dmat3 TransformationManager::frameTransformationMatrix(std::string from, + std::string to, + double ephemerisTime) const + { + auto fromit = _dipoleFrames.find(from); + auto toit = _dipoleFrames.find(to); + + //diopole frame to J200 makes the frame rotate. + if(fromit != _dipoleFrames.end()) from = "GSE"; + if(toit != _dipoleFrames.end()) to = "GSE"; + + fromit = _kameleonFrames.find(from); + toit = _kameleonFrames.find(to); + + bool fromKameleon = (fromit != _kameleonFrames.end()); + bool toKameleon = (toit != _kameleonFrames.end()); + + ccmc::Position in0 = {1.f, 0.f, 0.f}; + ccmc::Position in1 = {0.f, 1.f, 0.f}; + ccmc::Position in2 = {0.f, 0.f , 1.f}; + + ccmc::Position out0; + ccmc::Position out1; + ccmc::Position out2; + + if(!fromKameleon && !toKameleon){ + return SpiceManager::ref().frameTransformationMatrix(from, to, ephemerisTime); + } + +#ifdef OPENSPACE_MODULE_KAMELEON_ENABLED + if(fromKameleon && toKameleon){ + _kameleon->_cxform(from.c_str(), to.c_str(), ephemerisTime, &in0, &out0); + _kameleon->_cxform(from.c_str(), to.c_str(), ephemerisTime, &in1, &out1); + _kameleon->_cxform(from.c_str(), to.c_str(), ephemerisTime, &in2, &out2); + + return glm::dmat3( + out0.c0 , out0.c1 , out0.c2, + out1.c0 , out1.c1 , out1.c2, + out2.c0 , out2.c1 , out2.c2 + ); + + }else if(fromKameleon && !toKameleon){ + _kameleon->_cxform(from.c_str(), "J2000", ephemerisTime, &in0, &out0); + _kameleon->_cxform(from.c_str(), "J2000", ephemerisTime, &in1, &out1); + _kameleon->_cxform(from.c_str(), "J2000", ephemerisTime, &in2, &out2); + + glm::dmat3 kameleonTransformation( + out0.c0 , out0.c1 , out0.c2, + out1.c0 , out1.c1 , out1.c2, + out2.c0 , out2.c1 , out2.c2 + ); + + glm::dmat3 spiceTransformation = SpiceManager::ref().frameTransformationMatrix("J2000", to, ephemerisTime); + + return spiceTransformation*kameleonTransformation; + + }else if(!fromKameleon && toKameleon){ + glm::dmat3 spiceTransformation = SpiceManager::ref().frameTransformationMatrix(from, "J2000", ephemerisTime); + + _kameleon->_cxform("J2000", to.c_str(), ephemerisTime, &in0, &out0); + _kameleon->_cxform("J2000", to.c_str(), ephemerisTime, &in1, &out1); + _kameleon->_cxform("J2000", to.c_str(), ephemerisTime, &in2, &out2); + + glm::dmat3 kameleonTransformation( + out0.c0 , out0.c1 , out0.c2, + out1.c0 , out1.c1 , out1.c2, + out2.c0 , out2.c1 , out2.c2 + ); + + return kameleonTransformation*spiceTransformation; + } +#else + LERROR("Can not transform dynamic frames without kameleon module enabled"); +#endif + return glm::dmat3(1.0); + } + } \ No newline at end of file