Add server topic for delta times and send data to GUI

This commit is contained in:
Emma Broman
2020-08-11 13:14:08 +02:00
parent 457dabe9b7
commit 4293fad3e9
6 changed files with 281 additions and 10 deletions
+10
View File
@@ -29,6 +29,7 @@
#include <openspace/util/time.h>
#include <openspace/util/timeline.h>
#include <functional>
#include <optional>
#include <utility>
#include <vector>
@@ -81,6 +82,7 @@ public:
bool isPaused() const;
// TEST: delta time steps
std::vector<double> deltaTimeSteps() const;
void setDeltaTimeSteps(const std::vector<double> deltaTimes);
float defaultTimeInterpolationDuration() const;
@@ -94,6 +96,10 @@ public:
void interpolatePause(bool pause, double durationSeconds);
// TEST
std::optional<double> nextDeltaTimeStep();
std::optional<double> previousDeltaTimeStep();
bool hasNextDeltaTimeStep();
bool hasPreviousDeltaTimeStep();
void interpolateNextDeltaTimeStep(double durationSeconds);
void interpolatePreviousDeltaTimeStep(double durationSeconds);
@@ -106,11 +112,13 @@ public:
CallbackHandle addTimeChangeCallback(TimeChangeCallback cb);
CallbackHandle addDeltaTimeChangeCallback(TimeChangeCallback cb);
CallbackHandle addDeltaTimeStepsChangeCallback(TimeChangeCallback cb);
CallbackHandle addTimeJumpCallback(TimeChangeCallback cb);
CallbackHandle addTimelineChangeCallback(TimeChangeCallback cb);
void removeTimeChangeCallback(CallbackHandle handle);
void removeDeltaTimeChangeCallback(CallbackHandle handle);
void removeDeltaTimeStepsChangeCallback(CallbackHandle handle);
void triggerPlaybackStart();
void removeTimeJumpCallback(CallbackHandle handle);
void removeTimelineChangeCallback(CallbackHandle handle);
@@ -135,6 +143,7 @@ private:
// TEST: delta time steps
std::vector<double> _deltaTimeSteps;
bool _deltaTimeStepsChanged = false;
properties::FloatProperty _defaultTimeInterpolationDuration;
properties::FloatProperty _defaultDeltaTimeInterpolationDuration;
@@ -152,6 +161,7 @@ private:
std::vector<std::pair<CallbackHandle, TimeChangeCallback>> _timeChangeCallbacks;
std::vector<std::pair<CallbackHandle, TimeChangeCallback>> _deltaTimeChangeCallbacks;
std::vector<std::pair<CallbackHandle, TimeChangeCallback>> _deltaTimeStepsChangeCallbacks;
std::vector<std::pair<CallbackHandle, TimeChangeCallback>> _timeJumpCallbacks;
std::vector<std::pair<CallbackHandle, TimeChangeCallback>> _timelineChangeCallbacks;
+2
View File
@@ -33,6 +33,7 @@ set(HEADER_FILES
include/serverinterface.h
include/topics/authorizationtopic.h
include/topics/bouncetopic.h
include/topics/deltatimestepstopic.h
include/topics/documentationtopic.h
include/topics/flightcontrollertopic.h
include/topics/getpropertytopic.h
@@ -56,6 +57,7 @@ set(SOURCE_FILES
src/serverinterface.cpp
src/topics/authorizationtopic.cpp
src/topics/bouncetopic.cpp
src/topics/deltatimestepstopic.cpp
src/topics/documentationtopic.cpp
src/topics/flightcontrollertopic.cpp
src/topics/getpropertytopic.cpp
@@ -0,0 +1,57 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2020 *
* *
* 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_MODULE_SERVER___DELTA_TIME_STEPS_TOPIC___H__
#define __OPENSPACE_MODULE_SERVER___DELTA_TIME_STEPS_TOPIC___H__
#include <modules/server/include/topics/topic.h>
#include <optional>
namespace openspace {
class DeltaTimeStepsTopic : public Topic {
public:
DeltaTimeStepsTopic();
virtual ~DeltaTimeStepsTopic();
void handleJson(const nlohmann::json& json) override;
bool isDone() const override;
private:
const int UnsetOnChangeHandle = -1;
bool dataHasChanged();
void sendDeltaTimesData();
int _deltaTimeCallbackHandle = UnsetOnChangeHandle;
int _deltaTimesListCallbackHandle = UnsetOnChangeHandle;
bool _isDone = false;
std::optional<double> _lastNextDeltaTime = std::nullopt;
std::optional<double> _lastPrevDeltaTime = std::nullopt;
};
} // namespace openspace
#endif // __OPENSPACE_MODULE_SERVER___DELTA_TIME_STEPS_TOPIC___H__
+3
View File
@@ -26,6 +26,7 @@
#include <modules/server/include/topics/authorizationtopic.h>
#include <modules/server/include/topics/bouncetopic.h>
#include <modules/server/include/topics/deltatimestepstopic.h>
#include <modules/server/include/topics/documentationtopic.h>
#include <modules/server/include/topics/flightcontrollertopic.h>
#include <modules/server/include/topics/getpropertytopic.h>
@@ -56,6 +57,7 @@ namespace {
constexpr const char* VersionTopicKey = "version";
constexpr const char* AuthenticationTopicKey = "authorize";
constexpr const char* DeltaTimeStepsTopicKey = "deltatimesteps";
constexpr const char* DocumentationTopicKey = "documentation";
constexpr const char* GetPropertyTopicKey = "get";
constexpr const char* LuaScriptTopicKey = "luascript";
@@ -95,6 +97,7 @@ Connection::Connection(std::unique_ptr<ghoul::io::Socket> s,
);
_topicFactory.registerClass<DocumentationTopic>(DocumentationTopicKey);
_topicFactory.registerClass<DeltaTimeStepsTopic>(DeltaTimeStepsTopicKey);
_topicFactory.registerClass<GetPropertyTopic>(GetPropertyTopicKey);
_topicFactory.registerClass<LuaScriptTopic>(LuaScriptTopicKey);
_topicFactory.registerClass<SessionRecordingTopic>(SessionRecordingTopicKey);
@@ -0,0 +1,127 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2020 *
* *
* 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 "modules/server/include/topics/deltatimestepstopic.h"
#include <modules/server/include/connection.h>
#include <openspace/engine/globals.h>
#include <openspace/properties/property.h>
#include <openspace/query/query.h>
#include <openspace/util/timemanager.h>
#include <ghoul/logging/logmanager.h>
namespace {
constexpr const char* EventKey = "event";
constexpr const char* SubscribeEvent = "start_subscription";
constexpr const char* UnsubscribeEvent = "stop_subscription";
} // namespace
using nlohmann::json;
namespace openspace {
DeltaTimeStepsTopic::DeltaTimeStepsTopic() {}
DeltaTimeStepsTopic::~DeltaTimeStepsTopic() {
if (_deltaTimeCallbackHandle != UnsetOnChangeHandle) {
global::timeManager.removeDeltaTimeChangeCallback(
_deltaTimeCallbackHandle
);
}
if (_deltaTimesListCallbackHandle != UnsetOnChangeHandle) {
global::timeManager.removeDeltaTimeStepsChangeCallback(
_deltaTimesListCallbackHandle
);
}
}
bool DeltaTimeStepsTopic::isDone() const {
return _isDone;
}
bool DeltaTimeStepsTopic::dataHasChanged() {
std::optional<double> nextStep = global::timeManager.nextDeltaTimeStep();
std::optional<double> prevStep = global::timeManager.previousDeltaTimeStep();
return (nextStep != _lastNextDeltaTime || prevStep != _lastPrevDeltaTime);
}
void DeltaTimeStepsTopic::handleJson(const nlohmann::json& json) {
std::string event = json.at(EventKey).get<std::string>();
if (event == UnsubscribeEvent) {
_isDone = true;
return;
}
sendDeltaTimesData();
if (event != SubscribeEvent) {
_isDone = true;
return;
}
_deltaTimeCallbackHandle = global::timeManager.addDeltaTimeChangeCallback(
[this]() {
if (dataHasChanged()) {
sendDeltaTimesData();
}
}
);
_deltaTimesListCallbackHandle = global::timeManager.addDeltaTimeStepsChangeCallback(
[this]() {
if (dataHasChanged()) {
sendDeltaTimesData();
}
}
);
}
void DeltaTimeStepsTopic::sendDeltaTimesData() {
std::optional<double> nextStep = global::timeManager.nextDeltaTimeStep();
std::optional<double> prevStep = global::timeManager.previousDeltaTimeStep();
bool hasNext = nextStep.has_value();
bool hasPrev = prevStep.has_value();
json deltaTimesListJson = {
{ "hasNextStep", hasNext },
{ "hasPrevStep", hasPrev }
};
if (hasNext) {
deltaTimesListJson["nextStep"] = nextStep.value();
}
if (hasPrev) {
deltaTimesListJson["prevStep"] = prevStep.value();
}
_connection->sendJson(wrappedPayload(deltaTimesListJson));
_lastNextDeltaTime = nextStep;
_lastPrevDeltaTime = prevStep;
}
} // namespace openspace
+82 -10
View File
@@ -155,6 +155,13 @@ void TimeManager::preSynchronization(double dt) {
it.second();
}
}
if (_deltaTimeStepsChanged) {
using K = const CallbackHandle;
using V = TimeChangeCallback;
for (const std::pair<K, V>& it : _deltaTimeStepsChangeCallbacks) {
it.second();
}
}
if (_timelineChanged) {
using K = const CallbackHandle;
using V = TimeChangeCallback;
@@ -167,6 +174,7 @@ void TimeManager::preSynchronization(double dt) {
_lastDeltaTime = _deltaTime;
_lastTargetDeltaTime = _targetDeltaTime;
_lastTimePaused = _timePaused;
_deltaTimeStepsChanged = false;
_timelineChanged = false;
}
@@ -383,6 +391,7 @@ void TimeManager::setDeltaTime(double deltaTime) {
void TimeManager::setDeltaTimeSteps(std::vector<double> deltaTimes) {
std::sort(deltaTimes.begin(), deltaTimes.end());
_deltaTimeSteps = std::move(deltaTimes);
_deltaTimeStepsChanged = true;
}
size_t TimeManager::nKeyframes() const {
@@ -418,6 +427,14 @@ TimeManager::CallbackHandle TimeManager::addDeltaTimeChangeCallback(TimeChangeCa
return handle;
}
TimeManager::CallbackHandle
TimeManager::addDeltaTimeStepsChangeCallback(TimeChangeCallback cb)
{
CallbackHandle handle = _nextCallbackHandle++;
_deltaTimeStepsChangeCallbacks.emplace_back(handle, std::move(cb));
return handle;
}
TimeManager::CallbackHandle TimeManager::addTimeJumpCallback(TimeChangeCallback cb) {
CallbackHandle handle = _nextCallbackHandle++;
_timeJumpCallbacks.emplace_back(handle, std::move(cb));
@@ -465,6 +482,23 @@ void TimeManager::removeDeltaTimeChangeCallback(CallbackHandle handle) {
_deltaTimeChangeCallbacks.erase(it);
}
void TimeManager::removeDeltaTimeStepsChangeCallback(CallbackHandle handle) {
const auto it = std::find_if(
_deltaTimeStepsChangeCallbacks.begin(),
_deltaTimeStepsChangeCallbacks.end(),
[handle](const std::pair<CallbackHandle, std::function<void()>>& cb) {
return cb.first == handle;
}
);
ghoul_assert(
it != _deltaTimeStepsChangeCallbacks.end(),
"handle must be a valid callback handle"
);
_deltaTimeStepsChangeCallbacks.erase(it);
}
void TimeManager::triggerPlaybackStart() {
_playbackModeEnabled = true;
}
@@ -531,6 +565,10 @@ double TimeManager::targetDeltaTime() const {
return _targetDeltaTime;
}
std::vector<double> TimeManager::deltaTimeSteps() const {
return _deltaTimeSteps;
}
void TimeManager::interpolateDeltaTime(double newDeltaTime, double interpolationDuration)
{
if (newDeltaTime == _targetDeltaTime) {
@@ -558,33 +596,67 @@ void TimeManager::interpolateDeltaTime(double newDeltaTime, double interpolation
addKeyframe(now + interpolationDuration, futureKeyframe);
}
void TimeManager::interpolateNextDeltaTimeStep(double durationSeconds) {
double TimeManager::nextDeltaTimeStep() {
std::vector<double>::iterator nextStepIterator = std::upper_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
_deltaTime
_targetDeltaTime
);
if (nextStepIterator == _deltaTimeSteps.end()) {
// Current delta time is larger than last step
return;
return _targetDeltaTime; // not found, but gotta return something
}
interpolateDeltaTime(*nextStepIterator, durationSeconds);
return *nextStepIterator;
}
void TimeManager::interpolatePreviousDeltaTimeStep(double durationSeconds) {
double TimeManager::previousDeltaTimeStep() {
std::vector<double>::iterator lowerBoundIterator = std::lower_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
_deltaTime
);
_targetDeltaTime
);
if (lowerBoundIterator == _deltaTimeSteps.begin()) {
// Current delta time is smaller than first step
return;
return _targetDeltaTime; // not found, but gotta return something
}
std::vector<double>::iterator prevStepIterator = lowerBoundIterator - 1;
return *prevStepIterator;
}
bool TimeManager::hasNextDeltaTimeStep() {
if (_deltaTimeSteps.empty())
return false;
return _targetDeltaTime < _deltaTimeSteps.back();
}
bool TimeManager::hasPreviousDeltaTimeStep() {
if (_deltaTimeSteps.empty())
return false;
return _targetDeltaTime > _deltaTimeSteps.front();
}
void TimeManager::interpolateNextDeltaTimeStep(double durationSeconds) {
if (!hasNextDeltaTimeStep())
return;
double nextDeltaTime = nextDeltaTimeStep();
interpolateDeltaTime(nextDeltaTime, durationSeconds);
}
void TimeManager::interpolatePreviousDeltaTimeStep(double durationSeconds) {
if (!hasPreviousDeltaTimeStep())
return;
std::vector<double>::iterator lowerBoundIterator = std::lower_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
_targetDeltaTime
);
std::vector<double>::iterator prevStepIterator = lowerBoundIterator - 1;
interpolateDeltaTime(*prevStepIterator, durationSeconds);
}