From d130682d431c438ba5a7135f2326f381a747338f Mon Sep 17 00:00:00 2001 From: Malin E Date: Tue, 28 Jun 2022 10:43:58 +0200 Subject: [PATCH 1/2] Add interpolation for TimelineRotation * Same way as TimelineTranslaiton already have --- modules/base/rotation/timelinerotation.cpp | 44 +++++++++++++++++----- modules/base/rotation/timelinerotation.h | 2 + 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/modules/base/rotation/timelinerotation.cpp b/modules/base/rotation/timelinerotation.cpp index 9f34f58412..12302a6ea8 100644 --- a/modules/base/rotation/timelinerotation.cpp +++ b/modules/base/rotation/timelinerotation.cpp @@ -28,13 +28,24 @@ #include #include #include +#include namespace { + constexpr openspace::properties::Property::PropertyInfo ShouldInterpolateInfo = { + "ShouldInterpolate", + "Should Interpolate", + "If this value is set to 'true', an interpolation is applied between the given " + "keyframes. If this value is set to 'false', the interpolation is not applied." + }; + struct [[codegen::Dictionary(TimelineRotation)]] Parameters { // A table of keyframes, with keys formatted as YYYY-MM-DDTHH:MM:SS and values // that are valid Rotation objects std::map keyframes [[codegen::reference("core_transform_rotation")]]; + + // [[codegen::verbatim(ShouldInterpolateInfo.description)]] + std::optional shouldInterpolate; }; #include "timelinerotation_codegen.cpp" } // namespace @@ -45,7 +56,9 @@ documentation::Documentation TimelineRotation::Documentation() { return codegen::doc("base_transform_rotation_keyframe"); } -TimelineRotation::TimelineRotation(const ghoul::Dictionary& dictionary) { +TimelineRotation::TimelineRotation(const ghoul::Dictionary& dictionary) + : _shouldInterpolate(ShouldInterpolateInfo, true) +{ const Parameters p = codegen::bake(dictionary); for (const std::pair& kf : p.keyframes) { @@ -58,6 +71,9 @@ TimelineRotation::TimelineRotation(const ghoul::Dictionary& dictionary) { _timeline.addKeyframe(t, std::move(rotation)); } } + + _shouldInterpolate = p.shouldInterpolate.value_or(_shouldInterpolate); + addProperty(_shouldInterpolate); } glm::dmat3 TimelineRotation::matrix(const UpdateData& data) const { @@ -78,16 +94,26 @@ glm::dmat3 TimelineRotation::matrix(const UpdateData& data) const { } const double prevTime = prev->timestamp; const double nextTime = next->timestamp; + if (_shouldInterpolate) { + double t = 0.0; + if (nextTime - prevTime > 0.0) { + t = (now - prevTime) / (nextTime - prevTime); + } - double t = 0.0; - if (nextTime - prevTime > 0.0) { - t = (now - prevTime) / (nextTime - prevTime); + const glm::dquat nextRot = glm::quat_cast(next->data->matrix(data)); + const glm::dquat prevRot = glm::quat_cast(prev->data->matrix(data)); + + return glm::dmat3(glm::slerp(prevRot, nextRot, t)); } - - const glm::dquat nextRot = glm::quat_cast(next->data->matrix(data)); - const glm::dquat prevRot = glm::quat_cast(prev->data->matrix(data)); - - return glm::dmat3(glm::slerp(prevRot, nextRot, t)); + else { + if (prevTime <= now && now < nextTime) { + return prev->data->matrix(data); + } + else if (nextTime <= now) { + return next->data->matrix(data); + } + } + return glm::dmat3(0.0); } } // namespace openspace diff --git a/modules/base/rotation/timelinerotation.h b/modules/base/rotation/timelinerotation.h index d8a5450edd..837ef54736 100644 --- a/modules/base/rotation/timelinerotation.h +++ b/modules/base/rotation/timelinerotation.h @@ -25,6 +25,7 @@ #ifndef __OPENSPACE_MODULE_BASE___TIMELINEROTATION___H__ #define __OPENSPACE_MODULE_BASE___TIMELINEROTATION___H__ +#include #include #include @@ -42,6 +43,7 @@ public: private: Timeline> _timeline; + properties::BoolProperty _shouldInterpolate; }; } // namespace openspace From f1b1c7a173b5aebb7b922f2f38cd12feb99ada98 Mon Sep 17 00:00:00 2001 From: Malin E Date: Tue, 28 Jun 2022 16:03:16 +0200 Subject: [PATCH 2/2] Address PR comments --- modules/base/rotation/timelinerotation.h | 3 ++- modules/base/translation/timelinetranslation.h | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/base/rotation/timelinerotation.h b/modules/base/rotation/timelinerotation.h index 837ef54736..1803f39e7a 100644 --- a/modules/base/rotation/timelinerotation.h +++ b/modules/base/rotation/timelinerotation.h @@ -25,8 +25,9 @@ #ifndef __OPENSPACE_MODULE_BASE___TIMELINEROTATION___H__ #define __OPENSPACE_MODULE_BASE___TIMELINEROTATION___H__ -#include #include + +#include #include namespace openspace { diff --git a/modules/base/translation/timelinetranslation.h b/modules/base/translation/timelinetranslation.h index 58df543e05..2041043db8 100644 --- a/modules/base/translation/timelinetranslation.h +++ b/modules/base/translation/timelinetranslation.h @@ -25,8 +25,9 @@ #ifndef __OPENSPACE_MODULE_BASE___TIMELINETRANSLATION___H__ #define __OPENSPACE_MODULE_BASE___TIMELINETRANSLATION___H__ -#include #include + +#include #include #include