From b6246f653892d4814ddf8ee57b47b2db32c85b79 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Sat, 13 Sep 2014 18:02:39 +0200 Subject: [PATCH] Renamed methods in SpiceManager Started change of Time class --- include/openspace/tests/test_spicemanager.inl | 2 +- include/openspace/util/spicemanager.h | 14 ++- include/openspace/util/time.h | 64 +++++++--- src/engine/openspaceengine.cpp | 9 +- src/rendering/renderengine.cpp | 20 +++ src/util/spice.cpp | 6 +- src/util/spicemanager.cpp | 22 +++- src/util/time.cpp | 114 +++++++++++++----- 8 files changed, 194 insertions(+), 57 deletions(-) diff --git a/include/openspace/tests/test_spicemanager.inl b/include/openspace/tests/test_spicemanager.inl index e46e72d2a5..a1dbb8b9ed 100644 --- a/include/openspace/tests/test_spicemanager.inl +++ b/include/openspace/tests/test_spicemanager.inl @@ -226,7 +226,7 @@ TEST_F(SpiceManagerTest, stringToEphemerisTime){ char *date = "Thu Mar 20 12:53:29 PST 1997"; str2et_c(date, &control_ephemerisTime); - ephemerisTime = openspace::SpiceManager::ref().stringToEphemerisTime(date); + ephemerisTime = openspace::SpiceManager::ref().convertStringToTdbSeconds(date); EXPECT_EQ(ephemerisTime, control_ephemerisTime) << "Ephemeries times differ / not found"; unload_c(LSK.c_str()); diff --git a/include/openspace/util/spicemanager.h b/include/openspace/util/spicemanager.h index 328021bb3c..695dcd9dd1 100644 --- a/include/openspace/util/spicemanager.h +++ b/include/openspace/util/spicemanager.h @@ -119,7 +119,19 @@ public: * \param epochString, A string representing an epoch. * \return Corresponding ephemeris time, equivalent value in seconds past J2000, TDB. */ - double stringToEphemerisTime(const std::string& epochString) const; + double convertStringToTdbSeconds(const std::string& epochString) const; + + /** + * Convert the number of TDB seconds past the J2000 epoch into a human readable + * string representation. Fur further details, please refer to 'timout_c' in SPICE + * Documentation + * + * \param seconds The number of seconds that have passed since the J2000 epoch + * \param format The output format of the string + * (see ftp://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/timout_c.html) + * \return The formatted date string + */ + std::string convertTdbSecondsToString(double seconds, const std::string& format) const; // Computing Positions of Spacecraft and Natural Bodies(SPK) ---------------------------- // diff --git a/include/openspace/util/time.h b/include/openspace/util/time.h index 84303a136c..220f599ad1 100644 --- a/include/openspace/util/time.h +++ b/include/openspace/util/time.h @@ -1,31 +1,63 @@ -#ifndef ENGINETIME_H -#define ENGINETIME_H +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * 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. * + ****************************************************************************************/ -namespace openspace -{ +#ifndef __TIME_H__ +#define __TIME_H__ + +#include + +namespace openspace { class Time { public: - virtual ~Time(); - - static void init(); - static void deinit(); + static bool initialize(); + static void deinitialize(); static Time& ref(); static bool isInitialized(); + + double advanceTime(double tickTime); + + void setTime(double value); + double currentTime() const; + void setTimeUTC(std::string time); + std::string currentTimeUTC() const; - void setTime(const char* stringTime); - double getTime(); + void setDeltaTime(double deltaT); + double deltaTime() const; private: - static Time* this_; - Time(void); - Time(const Time& src); - Time& operator=(const Time& rhs); + Time(); + Time(const Time& src) = delete; + Time& operator=(const Time& rhs) = delete; - double time_; + static Time* _instance; + double _time; + + double _deltaTimePerSecond; }; } // namespace openspace -#endif \ No newline at end of file +#endif // __TIME_H__ diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 595ec959ca..7a0bf258ac 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -92,8 +93,9 @@ OpenSpaceEngine::~OpenSpaceEngine() delete _commandlineParser; _commandlineParser = nullptr; + SpiceManager::deinitialize(); Spice::deinit(); - Time::deinit(); + Time::deinitialize(); DeviceIdentifier::deinit(); FileSystem::deinitialize(); LogManager::deinitialize(); @@ -323,7 +325,8 @@ bool OpenSpaceEngine::initialize() SysCap.logCapabilities(); // initialize OpenSpace helpers - Time::init(); + SpiceManager::initialize(); + Time::initialize(); Spice::init(); Spice::ref().loadDefaultKernels(); FactoryManager::initialize(); @@ -463,6 +466,8 @@ void OpenSpaceEngine::preSynchronization() _interactionHandler->update(dt); _interactionHandler->lockControls(); + + Time::ref().advanceTime(dt); } #ifdef FLARE_ONLY _flare->preSync(); diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index 9c5524e0f0..85199e29f6 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "sgct.h" @@ -203,6 +204,24 @@ void RenderEngine::render() const psc origin = OsEng.interactionHandler().getOrigin(); const PowerScaledScalar pssl = (position - origin).length(); + const std::string time = Time::ref().currentTimeUTC().c_str(); + LINFO(time); + Freetype::print( + sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE), + FONT_SIZE, FONT_SIZE * 18, "Date: %s", time.c_str() + ); + Freetype::print( + sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE), + FONT_SIZE, FONT_SIZE * 16, "Avg. Frametime: %.10f", sgct::Engine::instance()->getAvgDt() + ); + Freetype::print( + sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE), + FONT_SIZE, FONT_SIZE * 14, "Drawtime: %.10f", sgct::Engine::instance()->getDrawTime() + ); + Freetype::print( + sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE), + FONT_SIZE, FONT_SIZE * 12, "Frametime: %.10f", sgct::Engine::instance()->getDt() + ); Freetype::print( sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE), FONT_SIZE, FONT_SIZE * 10, "Origin: (%.5f, %.5f, %.5f, %.5f)", origin[0], @@ -222,6 +241,7 @@ void RenderEngine::render() Freetype::print( sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE), FONT_SIZE, FONT_SIZE * 2, "Scaling: (%.10f, %.2f)", scaling[0], scaling[1]); + } #endif diff --git a/src/util/spice.cpp b/src/util/spice.cpp index 3be4c9a323..e7f8dc17fc 100644 --- a/src/util/spice.cpp +++ b/src/util/spice.cpp @@ -128,7 +128,7 @@ bool Spice::spk_getPosition(const std::string &target, const std::string &origin assert(this_); assert(Time::ref().isInitialized()); - SpiceDouble et = Time::ref().getTime(); + SpiceDouble et = Time::ref().currentTime(); SpiceDouble lt; spkpos_c(target.c_str(), et, "J2000", "LT+S", origin.c_str(), state, <); int failed = failed_c(); @@ -143,7 +143,7 @@ void Spice::spk_getPosition(int target, int origin, double state[3]) { assert(this_); assert(Time::ref().isInitialized()); - SpiceDouble et = Time::ref().getTime(); + SpiceDouble et = Time::ref().currentTime(); SpiceDouble lt; spkezp_c (target, et, "J2000", "NONE", origin, state, <); } @@ -156,7 +156,7 @@ bool Spice::spk_getOrientation(const std::string &target, double state[3][3]) { // ghoul logging std::string _loggerCat = "Spice::spk_getOrientation"; - SpiceDouble et = Time::ref().getTime(); + SpiceDouble et = Time::ref().currentTime(); std::string newname = "IAU_"; newname += target; pxform_c ( "J2000", newname.c_str(), et, state ); diff --git a/src/util/spicemanager.cpp b/src/util/spicemanager.cpp index 9ab3bb44ed..a6a70c8801 100644 --- a/src/util/spicemanager.cpp +++ b/src/util/spicemanager.cpp @@ -207,12 +207,32 @@ bool SpiceManager::getValueFromID(const std::string& bodyname, return true; } -double SpiceManager::stringToEphemerisTime(const std::string& epochString) const{ +double SpiceManager::convertStringToTdbSeconds(const std::string& epochString) const{ double et; str2et_c(epochString.c_str(), &et); return et; } +std::string SpiceManager::convertTdbSecondsToString(double seconds, + const std::string& format) const +{ + const int bufferSize = 128; + SpiceChar buffer[bufferSize]; + timout_c(seconds, format.c_str(), bufferSize - 1, buffer); + + int failed = failed_c(); + if (failed) { + char msg[1024]; + getmsg_c("LONG", 1024, msg); + //LERROR("Error retrieving position of target '" + target + "'"); + LERROR("Spice reported: " + std::string(msg)); + reset_c(); + return ""; + } + + return std::string(buffer); +} + bool SpiceManager::getTargetPosition(const std::string& target, double ephemerisTime, const std::string& referenceFrame, diff --git a/src/util/time.cpp b/src/util/time.cpp index d515affa5a..e4a6e9af4c 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -1,62 +1,110 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * 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. * + ****************************************************************************************/ -// open space includes #include -#include -// std includes +#include +#include + +#include + #include -// spice includes -#include "SpiceUsr.h" -#include +namespace { + const std::string _loggerCat = "Time"; +} namespace openspace { -Time* Time::this_ = nullptr; - -Time::Time() { - time_ = 0.0; +Time* Time::_instance = nullptr; +Time::Time() + : _time(-1.0) + , _deltaTimePerSecond(1.0) +{ + SpiceManager::ref().loadKernel(absPath("${OPENSPACE_DATA}/spice/naif0010.tls"), "leap"); // load spice time kernel - furnsh_c (absPath("${OPENSPACE_DATA}/spice/naif0010.tls").c_str()); + //furnsh_c (absPath("${OPENSPACE_DATA}/spice/naif0010.tls").c_str()); // convert UTC to ET - str2et_c ( "2006 JAN 31 01:00", &time_ ); + //str2et_c ( "2006 JAN 31 01:00", &_time ); } -Time::~Time() { - +bool Time::initialize() { + assert( _instance == nullptr); + _instance = new Time(); + return true; } -void Time::init() { - assert( this_ == nullptr); - this_ = new Time(); -} - -void Time::deinit() { - assert(this_); - delete this_; - this_ = nullptr; +void Time::deinitialize() { + assert(_instance); + delete _instance; + _instance = nullptr; } Time& Time::ref() { - assert(this_); - return *this_; + assert(_instance); + return *_instance; } bool Time::isInitialized() { - return this_ != nullptr; + return _instance != nullptr; } -void Time::setTime(const char* stringTime) { - assert(this_); - // convert UTC to ET - str2et_c ( stringTime, &time_ ); +//void Time::setTime(const char* stringTime) { +// assert(_instance); +// // convert UTC to ET +// //str2et_c ( stringTime, &_time ); +//} + +void Time::setTime(double value) { + _time = std::move(value); } -double Time::getTime() { - assert(this_); - return time_; +double Time::currentTime() const { + assert(_instance); + return _time; +} + +double Time::advanceTime(double tickTime) { + return _time += _deltaTimePerSecond * tickTime; +} + +void Time::setDeltaTime(double deltaT) { + _deltaTimePerSecond = std::move(deltaT); +} + +double Time::deltaTime() const { + return _deltaTimePerSecond; +} + +void Time::setTimeUTC(std::string time) { + _time = SpiceManager::ref().convertStringToTdbSeconds(std::move(time)); +} + +std::string Time::currentTimeUTC() const { + return SpiceManager::ref().convertTdbSecondsToString(_time, "MON DD,YYYY HR:MN:SC.#### (TDB) ::TDB"); } } // namespace openspace