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