File size: 6,136 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | // SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* Copyright (c) 2023 Bas Ruigrok (Rexbas) <Rexbas@proton.me> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#include "NavigationAnimation.h"
#include <Inventor/nodes/SoCamera.h>
#include <numbers>
using namespace Gui;
NavigationAnimation::NavigationAnimation(NavigationStyle* navigation)
: navigation(navigation)
{}
void NavigationAnimation::updateCurrentValue(const QVariant& value)
{
if (state() != QAbstractAnimation::State::Running) {
return;
}
update(value);
}
void NavigationAnimation::onStop([[maybe_unused]] bool finished)
{}
FixedTimeAnimation::FixedTimeAnimation(
NavigationStyle* navigation,
const SbRotation& orientation,
const SbVec3f& rotationCenter,
const SbVec3f& translation,
int duration,
const QEasingCurve::Type easingCurve
)
: NavigationAnimation(navigation)
, targetOrientation(orientation)
, targetTranslation(translation)
, rotationCenter(rotationCenter)
{
setDuration(duration);
setStartValue(0.0);
setEndValue(duration * 1.0);
setEasingCurve(easingCurve);
}
void FixedTimeAnimation::initialize()
{
#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
navigation->findBoundingSphere();
#endif
prevAngle = 0;
prevTranslation = SbVec3f(0, 0, 0);
// Find an axis and angle to rotate from the camera orientation to the target orientation using
// post-multiplication
SbVec3f rotationAxisPost;
float angle;
SbRotation(navigation->getCamera()->orientation.getValue().inverse() * targetOrientation)
.getValue(rotationAxisPost, angle);
if (angle > std::numbers::pi) {
angle -= float(2 * std::numbers::pi);
}
// Convert post-multiplication axis to a pre-multiplication axis
navigation->getCamera()->orientation.getValue().inverse().multVec(rotationAxisPost, rotationAxis);
angularVelocity = angle / duration();
linearVelocity = targetTranslation / duration();
}
/**
* @param value The elapsed time
*/
void FixedTimeAnimation::update(const QVariant& value)
{
SoCamera* camera = navigation->getCamera();
if (!camera) {
return;
}
float angle = value.toFloat() * angularVelocity;
SbVec3f translation = value.toFloat() * linearVelocity;
SbRotation rotation(rotationAxis, angle - prevAngle);
camera->position = camera->position.getValue() - prevTranslation;
navigation->reorientCamera(camera, rotation, rotationCenter);
camera->position = camera->position.getValue() + translation;
prevAngle = angle;
prevTranslation = translation;
}
/**
* @param finished True when the animation is finished, false when interrupted
*/
void FixedTimeAnimation::onStop(bool finished)
{
if (finished) {
SoCamera* camera = navigation->getCamera();
if (!camera) {
return;
}
// Set exact target orientation
camera->orientation = targetOrientation;
camera->position = camera->position.getValue() + targetTranslation - prevTranslation;
}
}
/**
* @param navigation The navigation style
* @param axis The rotation axis in screen coordinates
* @param velocity The angular velocity in radians per second
*/
SpinningAnimation::SpinningAnimation(NavigationStyle* navigation, const SbVec3f& axis, float velocity)
: NavigationAnimation(navigation)
, rotationAxis(axis)
{
setDuration((2 * std::numbers::pi / velocity) * 1000.0);
setStartValue(0.0);
setEndValue(2 * std::numbers::pi);
setLoopCount(-1);
}
void SpinningAnimation::initialize()
{
#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
navigation->findBoundingSphere();
#endif
prevAngle = 0;
navigation->setViewing(true);
navigation->setViewingMode(NavigationStyle::SPINNING);
}
/**
* @param value The angle in radians
*/
void SpinningAnimation::update(const QVariant& value)
{
SoCamera* camera = navigation->getCamera();
if (!camera) {
return;
}
SbRotation deltaRotation = SbRotation(rotationAxis, value.toFloat() - prevAngle);
navigation->reorientCamera(camera, deltaRotation);
prevAngle = value.toFloat();
}
/**
* @param finished True when the animation is finished, false when interrupted
*/
void SpinningAnimation::onStop([[maybe_unused]] bool finished)
{
if (navigation->getViewingMode() != NavigationStyle::SPINNING) {
return;
}
navigation->setViewingMode(
navigation->isViewing() ? NavigationStyle::IDLE : NavigationStyle::INTERACT
);
}
|