mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-05-07 20:09:58 -05:00
feature/time-refactor (#294)
- Change Time class to become a non-singleton - Move ownership of the current time to TimeManager(instead of singleton access). - Store the Time as a Syncable in TimeManager instead of representing all member variables of Time as Syncables. - Pass a Time object around in the update/render methods, so that renderables don't have to query the OpenSpaceEngine to know if time is paused or if it jumped. - Introduce Timeline and Keyframe classes - Make use of Timelineand Keyframeclasses in KeyframeInteractionMode and TimeManager - Added basic unit tests for Timelineand Keyframe Future work: Add interpolation schemes for keyframes. Possibly use keyframes+interpolation feature to tween/morph properties, or figure out if this should be a separate mechanism.
This commit is contained in:
@@ -65,7 +65,6 @@ public:
|
||||
|
||||
// Interaction mode setters
|
||||
void setCameraStateFromDictionary(const ghoul::Dictionary& cameraDict);
|
||||
void setInteractionMode(const std::string& interactionModeKey);
|
||||
InteractionMode* interactionMode();
|
||||
|
||||
void goToChunk(int x, int y, int level);
|
||||
@@ -73,9 +72,10 @@ public:
|
||||
|
||||
void resetKeyBindings();
|
||||
|
||||
void addKeyframe(const datamessagestructures::CameraKeyframe &kf);
|
||||
void addKeyframe(double timestamp, KeyframeInteractionMode::CameraPose pose);
|
||||
void removeKeyframesAfter(double timestamp);
|
||||
void clearKeyframes();
|
||||
size_t nKeyframes() const;
|
||||
const std::vector<datamessagestructures::CameraKeyframe>& keyframes() const;
|
||||
|
||||
void bindKeyLocal(
|
||||
@@ -134,7 +134,7 @@ private:
|
||||
|
||||
std::string generateJson() const override;
|
||||
|
||||
void setInteractionMode(std::shared_ptr<InteractionMode> interactionMode);
|
||||
void setInteractionMode(InteractionMode* interactionMode);
|
||||
|
||||
bool _cameraUpdatedFromScript = false;
|
||||
|
||||
@@ -143,14 +143,18 @@ private:
|
||||
std::unique_ptr<InputState> _inputState;
|
||||
Camera* _camera;
|
||||
|
||||
std::shared_ptr<InteractionMode> _currentInteractionMode;
|
||||
InteractionMode* _currentInteractionMode;
|
||||
|
||||
std::map<std::string, std::shared_ptr<InteractionMode>> _interactionModes;
|
||||
std::shared_ptr<OrbitalInteractionMode::MouseStates> _mouseStates;
|
||||
|
||||
std::unique_ptr<OrbitalInteractionMode> _orbitalInteractionMode;
|
||||
std::unique_ptr<GlobeBrowsingInteractionMode> _globeBrowsingInteractionMode;
|
||||
std::unique_ptr<KeyframeInteractionMode> _keyframeInteractionMode;
|
||||
|
||||
// Properties
|
||||
properties::StringProperty _origin;
|
||||
|
||||
properties::OptionProperty _interactionModeOption;
|
||||
|
||||
properties::BoolProperty _rotationalFriction;
|
||||
properties::BoolProperty _horizontalFriction;
|
||||
properties::BoolProperty _verticalFriction;
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <openspace/network/parallelconnection.h>
|
||||
#include <openspace/util/mouse.h>
|
||||
#include <openspace/util/keys.h>
|
||||
#include <openspace/util/timeline.h>
|
||||
|
||||
#ifdef OPENSPACE_MODULE_GLOBEBROWSING_ENABLED
|
||||
#include <modules/globebrowsing/tile/tileindex.h>
|
||||
@@ -80,14 +81,6 @@ namespace interaction {
|
||||
void mousePositionCallback(double mouseX, double mouseY);
|
||||
void mouseScrollWheelCallback(double mouseScrollDelta);
|
||||
|
||||
// Mutators
|
||||
void addKeyframe(const datamessagestructures::CameraKeyframe &kf);
|
||||
void removeKeyframesAfter(double timestamp);
|
||||
void clearKeyframes();
|
||||
void clearOldKeyframes();
|
||||
|
||||
static bool compareKeyframeTimes(const datamessagestructures::CameraKeyframe& a, const datamessagestructures::CameraKeyframe& b);
|
||||
|
||||
// Accessors
|
||||
const std::list<std::pair<Key, KeyModifier> >& getPressedKeys() const;
|
||||
const std::list<MouseButton>& getPressedMouseButtons() const;
|
||||
@@ -104,9 +97,6 @@ namespace interaction {
|
||||
std::list<MouseButton> _mouseButtonsDown;
|
||||
glm::dvec2 _mousePosition;
|
||||
double _mouseScrollDelta;
|
||||
|
||||
// Remote input via keyframes
|
||||
std::vector<datamessagestructures::CameraKeyframe> _keyframes;
|
||||
};
|
||||
|
||||
|
||||
@@ -194,16 +184,23 @@ protected:
|
||||
class KeyframeInteractionMode : public InteractionMode
|
||||
{
|
||||
public:
|
||||
struct CameraPose {
|
||||
glm::dvec3 position;
|
||||
glm::quat rotation;
|
||||
std::string focusNode;
|
||||
bool followFocusNodeRotation;
|
||||
};
|
||||
|
||||
KeyframeInteractionMode();
|
||||
~KeyframeInteractionMode();
|
||||
|
||||
virtual void updateMouseStatesFromInput(const InputState& inputState, double deltaTime);
|
||||
virtual void updateCameraStateFromMouseStates(Camera& camera, double deltaTime);
|
||||
bool followingNodeRotation() const override;
|
||||
Timeline<CameraPose>& timeline();
|
||||
|
||||
private:
|
||||
std::vector<datamessagestructures::CameraKeyframe> _keyframes;
|
||||
double _currentKeyframeTime;
|
||||
Timeline<CameraPose> _cameraPoseTimeline;
|
||||
};
|
||||
|
||||
class GlobeBrowsingInteractionMode;
|
||||
|
||||
@@ -55,8 +55,6 @@ namespace openspace {
|
||||
* The synchronization of the simulation time requires
|
||||
*/
|
||||
|
||||
class SyncBuffer;
|
||||
|
||||
class Time {
|
||||
public:
|
||||
/**
|
||||
@@ -90,20 +88,6 @@ public:
|
||||
|
||||
static Time now();
|
||||
|
||||
/**
|
||||
* Returns the reference to the Time singleton object.
|
||||
* \return The reference to the Time singleton object
|
||||
* \pre The Time singleton must have been initialized
|
||||
*/
|
||||
static Time& ref();
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if the singleton has been successfully initialized,
|
||||
* <code>false</code> otherwise
|
||||
* \return <code>true</code> if the singleton has been successfully initialized,
|
||||
* <code>false</code> otherwise
|
||||
*/
|
||||
static bool isInitialized();
|
||||
|
||||
/**
|
||||
* Sets the current time to the specified value in seconds past the J2000 epoch. This
|
||||
@@ -210,15 +194,10 @@ public:
|
||||
*/
|
||||
static scripting::LuaLibrary luaLibrary();
|
||||
|
||||
std::vector<Syncable*> getSyncables();
|
||||
|
||||
private:
|
||||
static Time* _instance; ///< The singleton instance
|
||||
|
||||
SyncData<double> _time;
|
||||
SyncData<double> _dt;
|
||||
SyncData<bool> _timeJumped;
|
||||
|
||||
double _time;
|
||||
double _dt;
|
||||
bool _timeJumped;
|
||||
bool _timePaused = false;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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 __OPENSPACE_CORE___TIMELINE___H__
|
||||
#define __OPENSPACE_CORE___TIMELINE___H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <deque>
|
||||
#include <cstddef>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
/**
|
||||
* Base class for keyframes
|
||||
*/
|
||||
struct KeyframeBase {
|
||||
size_t id;
|
||||
double timestamp;
|
||||
};
|
||||
|
||||
/**
|
||||
* Templated class for keyframes containing data
|
||||
*/
|
||||
template <typename T>
|
||||
struct Keyframe : public KeyframeBase {
|
||||
Keyframe(size_t i, double t, T p)
|
||||
: KeyframeBase{i, t}
|
||||
, data(p)
|
||||
{}
|
||||
T data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Templated class for timelines
|
||||
*/
|
||||
template <typename T>
|
||||
class Timeline {
|
||||
public:
|
||||
Timeline();
|
||||
virtual ~Timeline();
|
||||
void addKeyframe(double time, T data);
|
||||
void clearKeyframes();
|
||||
void removeKeyframe(size_t id);
|
||||
void removeKeyframesBefore(double timestamp, bool inclusive = false);
|
||||
void removeKeyframesAfter(double timestamp, bool inclusive = false);
|
||||
void removeKeyframesBetween(double begin, double end, bool inclusiveBegin = false, bool inclusiveEnd = false);
|
||||
size_t nKeyframes() const;
|
||||
const Keyframe<T>* firstKeyframeAfter(double timestamp, bool inclusive = false) const;
|
||||
const Keyframe<T>* lastKeyframeBefore(double timestamp, bool inclusive = false) const;
|
||||
const std::deque<Keyframe<T>>& keyframes() const;
|
||||
private:
|
||||
size_t _nextKeyframeId;
|
||||
std::deque<Keyframe<T>> _keyframes;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return true if the timestamp of a is smaller the timestamp of b.
|
||||
*/
|
||||
bool compareKeyframeTimes(const KeyframeBase& a, const KeyframeBase& b);
|
||||
|
||||
/**
|
||||
* Return true if a is smaller than the timestamp of b.
|
||||
*/
|
||||
bool compareTimeWithKeyframeTime(double a, const KeyframeBase& b);
|
||||
|
||||
/**
|
||||
* Return true if the timestamp of a is smaller than b.
|
||||
*/
|
||||
bool compareKeyframeTimeWithTime(const KeyframeBase& a, double b);
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
#include <openspace/util/timeline.inl>;
|
||||
|
||||
#endif // __OPENSPACE_CORE___TIMELINE___H__
|
||||
@@ -0,0 +1,121 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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 {
|
||||
|
||||
template <typename T>
|
||||
Timeline<T>::Timeline()
|
||||
: _nextKeyframeId(1)
|
||||
{}
|
||||
|
||||
template <typename T>
|
||||
Timeline<T>::~Timeline() {}
|
||||
|
||||
template <typename T>
|
||||
void Timeline<T>::addKeyframe(double timestamp, T data) {
|
||||
Keyframe<T> keyframe(++_nextKeyframeId, timestamp, data);
|
||||
auto iter = std::upper_bound(_keyframes.begin(), _keyframes.end(), keyframe, &compareKeyframeTimes);
|
||||
_keyframes.insert(iter, keyframe);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Timeline<T>::removeKeyframesAfter(double timestamp, bool inclusive) {
|
||||
auto iter = inclusive
|
||||
? std::lower_bound(_keyframes.begin(), _keyframes.end(), timestamp, &compareKeyframeTimeWithTime)
|
||||
: std::upper_bound(_keyframes.begin(), _keyframes.end(), timestamp, &compareTimeWithKeyframeTime);
|
||||
|
||||
_keyframes.erase(iter, _keyframes.end());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Timeline<T>::removeKeyframesBefore(double timestamp, bool inclusive) {
|
||||
auto iter = inclusive
|
||||
? std::upper_bound(_keyframes.begin(), _keyframes.end(), timestamp, &compareTimeWithKeyframeTime)
|
||||
: std::lower_bound(_keyframes.begin(), _keyframes.end(), timestamp, &compareKeyframeTimeWithTime);
|
||||
|
||||
_keyframes.erase(_keyframes.begin(), iter);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Timeline<T>::removeKeyframesBetween(double begin, double end, bool inclusiveBegin, bool inclusiveEnd) {
|
||||
auto beginIter = inclusiveBegin
|
||||
? std::lower_bound(_keyframes.begin(), _keyframes.end(), begin, &compareKeyframeTimeWithTime)
|
||||
: std::upper_bound(_keyframes.begin(), _keyframes.end(), begin, &compareTimeWithKeyframeTime);
|
||||
|
||||
auto endIter = inclusiveEnd
|
||||
? std::upper_bound(beginIter, _keyframes.end(), end, &compareTimeWithKeyframeTime)
|
||||
: std::lower_bound(beginIter, _keyframes.end(), end, &compareKeyframeTimeWithTime);
|
||||
|
||||
_keyframes.erase(beginIter, endIter);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Timeline<T>::clearKeyframes() {
|
||||
_keyframes.clear();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Timeline<T>::removeKeyframe(size_t id) {
|
||||
_keyframes.erase(std::remove_if(_keyframes.begin(), _keyframes.end(), [id] (Keyframe<T> keyframe) {
|
||||
return keyframe.id == id;
|
||||
}), _keyframes.end());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
size_t Timeline<T>::nKeyframes() const {
|
||||
return _keyframes.size();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const Keyframe<T>* Timeline<T>::firstKeyframeAfter(double timestamp, bool inclusive) const {
|
||||
auto it = inclusive
|
||||
? std::lower_bound(_keyframes.begin(), _keyframes.end(), timestamp, &compareKeyframeTimeWithTime)
|
||||
: std::upper_bound(_keyframes.begin(), _keyframes.end(), timestamp, &compareTimeWithKeyframeTime);
|
||||
if (it == _keyframes.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return &(*it);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const Keyframe<T>* Timeline<T>::lastKeyframeBefore(double timestamp, bool inclusive) const {
|
||||
auto it = inclusive
|
||||
? std::upper_bound(_keyframes.begin(), _keyframes.end(), timestamp, &compareTimeWithKeyframeTime)
|
||||
: std::lower_bound(_keyframes.begin(), _keyframes.end(), timestamp, &compareKeyframeTimeWithTime);
|
||||
if (it == _keyframes.begin()) {
|
||||
return nullptr;
|
||||
}
|
||||
it--;
|
||||
return &(*it);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
const std::deque<Keyframe<T>>& Timeline<T>::keyframes() const {
|
||||
return _keyframes;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -27,24 +27,26 @@
|
||||
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include <openspace/network/messagestructures.h>
|
||||
#include <openspace/util/timeline.h>
|
||||
#include <openspace/util/time.h>
|
||||
#include <openspace/util/syncdata.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class TimeManager {
|
||||
public:
|
||||
Time& time();
|
||||
std::vector<Syncable*> getSyncables();
|
||||
void preSynchronization(double dt);
|
||||
void addKeyframe(const datamessagestructures::TimeKeyframe& kf);
|
||||
void addKeyframe(double timestamp, Time kf);
|
||||
void removeKeyframesBefore(double timestamp);
|
||||
void removeKeyframesAfter(double timestamp);
|
||||
void clearKeyframes();
|
||||
const std::deque<datamessagestructures::TimeKeyframe>& keyframes() const;
|
||||
size_t nKeyframes() const;
|
||||
private:
|
||||
Timeline<Time> _timeline;
|
||||
SyncData<Time> _currentTime;
|
||||
void consumeKeyframes(double dt);
|
||||
std::deque<datamessagestructures::TimeKeyframe> _keyframes;
|
||||
static bool compareKeyframeTimes(
|
||||
const datamessagestructures::TimeKeyframe& a,
|
||||
const datamessagestructures::TimeKeyframe& b);
|
||||
double _latestConsumedTimestamp;
|
||||
};
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
|
||||
#include <openspace/util/camera.h>
|
||||
#include <openspace/util/powerscaledcoordinate.h>
|
||||
#include <openspace/util/time.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
@@ -44,11 +45,8 @@ struct TransformData {
|
||||
|
||||
struct UpdateData {
|
||||
TransformData modelTransform;
|
||||
double time;
|
||||
double delta;
|
||||
bool timePaused;
|
||||
bool isTimeJump;
|
||||
bool doPerformanceMeasurement;
|
||||
const Time time;
|
||||
const bool doPerformanceMeasurement;
|
||||
};
|
||||
|
||||
|
||||
@@ -57,6 +55,7 @@ struct RenderData {
|
||||
// psc position to be removed in favor of the double precision position defined in
|
||||
// the translation in transform.
|
||||
psc position;
|
||||
const Time time;
|
||||
bool doPerformanceMeasurement;
|
||||
int renderBinMask;
|
||||
TransformData modelTransform;
|
||||
|
||||
Reference in New Issue
Block a user