Files
OpenSpace/modules/base/rotation/timelinerotation.cpp
Emma Broman 68ab41a241 Fix broken Apollo profile and transform initialization in general (#3704)
* Fix faulty log category and minor inconsistencies in GlobeTranslation and GlobeRotation

* Small refactor

* Remove unused return value from initialize function

* Fill attached node in globetranslation initialize and initialize all timeline

Should make the timeline translation work with globetranslations in other cases that the renderabletrails

* Do the same type of change for rotation and scale

* Update initialize function singatures

* Initialize and update translation in trail before use

* Screenspace renderable renderable - move initialize call to initialize

* Refactor trail position calls to make it clearer what is being done

* Call timeframe initialize functions in transform classes

* Do not call update in trail position after all - it borks up the performance

* GlobeTransform: Only fill attached node in init and onchange, and allow nodes without renderable

* Correctly initialize the multi-transform types

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Alexander Bock <alexander.bock@liu.se>

* Address code review comment

* Update comments in renderabletrail

---------

Co-authored-by: Alexander Bock <alexander.bock@liu.se>
2025-06-10 12:05:30 +02:00

142 lines
6.1 KiB
C++

/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2025 *
* *
* 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/base/rotation/timelinerotation.h>
#include <openspace/documentation/documentation.h>
#include <openspace/documentation/verifier.h>
#include <openspace/scene/scene.h>
#include <openspace/util/updatestructures.h>
#include <openspace/util/time.h>
#include <optional>
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.",
openspace::properties::Property::Visibility::AdvancedUser
};
// This `Rotation` uses a timeline of other `Rotation` classes to calculate the
// final rotation for the attached scene graph node. The current in-game time is
// used to determine which specific keyframe to currently use. It is also possible to
// interpolate between two adjacent keyframes.
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<std::string, ghoul::Dictionary> keyframes
[[codegen::reference("core_transform_rotation")]];
// [[codegen::verbatim(ShouldInterpolateInfo.description)]]
std::optional<bool> shouldInterpolate;
};
#include "timelinerotation_codegen.cpp"
} // namespace
namespace openspace {
documentation::Documentation TimelineRotation::Documentation() {
return codegen::doc<Parameters>("base_transform_rotation_keyframe");
}
TimelineRotation::TimelineRotation(const ghoul::Dictionary& dictionary)
: Rotation(dictionary)
, _shouldInterpolate(ShouldInterpolateInfo, true)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
for (const std::pair<const std::string, ghoul::Dictionary>& kf : p.keyframes) {
const double t = Time::convertTime(kf.first);
ghoul::mm_unique_ptr<Rotation> rotation = Rotation::createFromDictionary(
kf.second
);
rotation->setIdentifier(makeIdentifier(kf.first));
addPropertySubOwner(rotation.get());
_timeline.addKeyframe(t, std::move(rotation));
}
_shouldInterpolate = p.shouldInterpolate.value_or(_shouldInterpolate);
addProperty(_shouldInterpolate);
}
void TimelineRotation::initialize() {
Rotation::initialize();
for (const Keyframe<ghoul::mm_unique_ptr<Rotation>>& kf : _timeline.keyframes()) {
kf.data->initialize();
}
}
void TimelineRotation::update(const UpdateData& data) {
const double now = data.time.j2000Seconds();
using KeyframePointer = const Keyframe<ghoul::mm_unique_ptr<Rotation>>*;
if (KeyframePointer prev = _timeline.lastKeyframeBefore(now, true); prev) {
prev->data->update(data);
}
if (KeyframePointer next = _timeline.firstKeyframeAfter(now, true); next) {
next->data->update(data);
}
Rotation::update(data);
}
glm::dmat3 TimelineRotation::matrix(const UpdateData& data) const {
const double now = data.time.j2000Seconds();
using KeyframePointer = const Keyframe<ghoul::mm_unique_ptr<Rotation>>*;
KeyframePointer prev = _timeline.lastKeyframeBefore(now, true);
KeyframePointer next = _timeline.firstKeyframeAfter(now, true);
if (!prev && !next) {
return glm::dmat3(0.0);
}
if (!prev) {
prev = next;
}
if (!next) {
next = prev;
}
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);
}
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 {
return now < nextTime ? prev->data->matrix(data) : next->data->matrix(data);
}
}
} // namespace openspace