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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
/****************************************************************************
Copyright (c) 2018 GeometryFactory Sarl (France).
Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.
This file is part of a fork of the QGLViewer library version 2.7.0.
*****************************************************************************/
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/GraphicsView/include/CGAL/Qt/keyFrameInterpolator_impl.h $
// $Id: include/CGAL/Qt/keyFrameInterpolator_impl.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-only
#ifdef CGAL_HEADER_ONLY
#define CGAL_INLINE_FUNCTION inline
#include <CGAL/license/GraphicsView.h>
#else
#define CGAL_INLINE_FUNCTION
#endif
#include <CGAL/Qt/keyFrameInterpolator.h>
namespace CGAL{
namespace qglviewer{
/*! Creates a KeyFrameInterpolator, with \p frame as associated frame().
The frame() can be set or changed using setFrame().
interpolationTime(), interpolationSpeed() and interpolationPeriod() are set to
their default values. */
CGAL_INLINE_FUNCTION
KeyFrameInterpolator::KeyFrameInterpolator(Frame *frame)
: frame_(nullptr), period_(40), interpolationTime_(0.0),
interpolationSpeed_(1.0), interpolationStarted_(false),
closedPath_(false), loopInterpolation_(false), pathIsValid_(false),
valuesAreValid_(true), currentFrameValid_(false)
{
setFrame(frame);
for (int i = 0; i < 4; ++i)
currentFrame_[i] = new QMutableListIterator<KeyFrame *>(keyFrame_);
connect(&timer_, SIGNAL(timeout()), SLOT(update()));
}
/*! Virtual destructor. Clears the keyFrame path. */
CGAL_INLINE_FUNCTION
KeyFrameInterpolator::~KeyFrameInterpolator() {
deletePath();
for (int i = 0; i < 4; ++i)
delete currentFrame_[i];
}
/*! Sets the frame() associated to the KeyFrameInterpolator. */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::setFrame(Frame *const frame) {
if (this->frame())
disconnect(this, SIGNAL(interpolated()), this->frame(),
SIGNAL(interpolated()));
frame_ = frame;
if (this->frame())
connect(this, SIGNAL(interpolated()), this->frame(),
SIGNAL(interpolated()));
}
/*! Updates frame() state according to current interpolationTime(). Then adds
interpolationPeriod()*interpolationSpeed() to interpolationTime().
This internal method is called by a timer when interpolationIsStarted(). It
can be used for debugging purpose. stopInterpolation() is called when
interpolationTime() reaches firstTime() or lastTime(), unless
loopInterpolation() is \c true. */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::update() {
interpolateAtTime(interpolationTime());
interpolationTime_ += interpolationSpeed() * interpolationPeriod() / 1000.0;
if (interpolationTime() > keyFrame_.last()->time()) {
if (loopInterpolation())
setInterpolationTime(keyFrame_.first()->time() + interpolationTime_ -
keyFrame_.last()->time());
else {
// Make sure last KeyFrame is reached and displayed
interpolateAtTime(keyFrame_.last()->time());
stopInterpolation();
}
Q_EMIT endReached();
} else if (interpolationTime() < keyFrame_.first()->time()) {
if (loopInterpolation())
setInterpolationTime(keyFrame_.last()->time() -
keyFrame_.first()->time() + interpolationTime_);
else {
// Make sure first KeyFrame is reached and displayed
interpolateAtTime(keyFrame_.first()->time());
stopInterpolation();
}
Q_EMIT endReached();
}
}
/*! Starts the interpolation process.
A timer is started with an interpolationPeriod() period that updates the
frame()'s position and orientation. interpolationIsStarted() will return \c
true until stopInterpolation() or toggleInterpolation() is called.
If \p period is positive, it is set as the new interpolationPeriod(). The
previous interpolationPeriod() is used otherwise (default).
If interpolationTime() is larger than lastTime(), interpolationTime() is reset
to firstTime() before interpolation starts (and inversely for negative
interpolationSpeed()).
Use setInterpolationTime() before calling this method to change the starting
interpolationTime().
See the <a href="../examples/keyFrames.html">keyFrames example</a> for an
illustration.
You may also be interested in CGAL::QGLViewer::animate() and
CGAL::QGLViewer::startAnimation().
\attention The keyFrames must be defined (see addKeyFrame()) \e before you
startInterpolation(), or else the interpolation will naturally immediately
stop. */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::startInterpolation(int period) {
if (period >= 0)
setInterpolationPeriod(period);
if (!keyFrame_.isEmpty()) {
if ((interpolationSpeed() > 0.0) &&
(interpolationTime() >= keyFrame_.last()->time()))
setInterpolationTime(keyFrame_.first()->time());
if ((interpolationSpeed() < 0.0) &&
(interpolationTime() <= keyFrame_.first()->time()))
setInterpolationTime(keyFrame_.last()->time());
timer_.start(interpolationPeriod());
interpolationStarted_ = true;
update();
}
}
/*! Stops an interpolation started with startInterpolation(). See
* interpolationIsStarted() and toggleInterpolation(). */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::stopInterpolation() {
timer_.stop();
interpolationStarted_ = false;
}
/*! Stops the interpolation and resets interpolationTime() to the firstTime().
If desired, call interpolateAtTime() after this method to actually move the
frame() to firstTime(). */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::resetInterpolation() {
stopInterpolation();
setInterpolationTime(firstTime());
}
/*! Appends a new keyFrame to the path, with its associated \p time (in
seconds).
The keyFrame is given as a pointer to a Frame, which will be connected to the
KeyFrameInterpolator: when \p frame is modified, the KeyFrameInterpolator path
is updated accordingly. This allows for dynamic paths, where keyFrame can be
edited, even during the interpolation. See the <a
href="../examples/keyFrames.html">keyFrames example</a> for an illustration.
\c nullptr \p frame pointers are silently ignored. The keyFrameTime() has to be
monotonously increasing over keyFrames.
Use addKeyFrame(const Frame&, qreal) to add keyFrame by values. */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::addKeyFrame(const Frame *const frame, qreal time) {
if (!frame)
return;
if (keyFrame_.isEmpty())
interpolationTime_ = time;
if ((!keyFrame_.isEmpty()) && (keyFrame_.last()->time() > time))
qWarning(
"Error in KeyFrameInterpolator::addKeyFrame: time is not monotone");
else
keyFrame_.append(new KeyFrame(frame, time));
connect(frame, SIGNAL(modified()), SLOT(invalidateValues()));
valuesAreValid_ = false;
pathIsValid_ = false;
currentFrameValid_ = false;
resetInterpolation();
}
/*! Appends a new keyFrame to the path, with its associated \p time (in
seconds).
The path will use the current \p frame state. If you want the path to change
when \p frame is modified, you need to pass a \e pointer to the Frame instead
(see addKeyFrame(const Frame*, qreal)).
The keyFrameTime() have to be monotonously increasing over keyFrames. */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::addKeyFrame(const Frame &frame, qreal time) {
if (keyFrame_.isEmpty())
interpolationTime_ = time;
if ((!keyFrame_.isEmpty()) && (keyFrame_.last()->time() > time))
qWarning(
"Error in KeyFrameInterpolator::addKeyFrame: time is not monotone");
else
keyFrame_.append(new KeyFrame(frame, time));
valuesAreValid_ = false;
pathIsValid_ = false;
currentFrameValid_ = false;
resetInterpolation();
}
/*! Appends a new keyFrame to the path.
Same as addKeyFrame(const Frame* frame, qreal), except that the keyFrameTime()
is set to the previous keyFrameTime() plus one second (or 0.0 if there is no
previous keyFrame). */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::addKeyFrame(const Frame *const frame) {
qreal time;
if (keyFrame_.isEmpty())
time = 0.0;
else
time = lastTime() + 1.0;
addKeyFrame(frame, time);
}
/*! Appends a new keyFrame to the path.
Same as addKeyFrame(const Frame& frame, qreal), except that the keyFrameTime()
is automatically set to previous keyFrameTime() plus one second (or 0.0 if
there is no previous keyFrame). */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::addKeyFrame(const Frame &frame) {
qreal time;
if (keyFrame_.isEmpty())
time = 0.0;
else
time = keyFrame_.last()->time() + 1.0;
addKeyFrame(frame, time);
}
/*! Removes all keyFrames from the path. The numberOfKeyFrames() is set to 0. */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::deletePath() {
stopInterpolation();
qDeleteAll(keyFrame_);
keyFrame_.clear();
pathIsValid_ = false;
valuesAreValid_ = false;
currentFrameValid_ = false;
}
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::updateModifiedFrameValues() {
Quaternion prevQ = keyFrame_.first()->orientation();
KeyFrame *kf;
for (int i = 0; i < keyFrame_.size(); ++i) {
kf = keyFrame_.at(i);
if (kf->frame())
kf->updateValuesFromPointer();
kf->flipOrientationIfNeeded(prevQ);
prevQ = kf->orientation();
}
KeyFrame *prev = keyFrame_.first();
kf = keyFrame_.first();
int index = 1;
while (kf) {
KeyFrame *next = (index < keyFrame_.size()) ? keyFrame_.at(index) : nullptr;
index++;
if (next)
kf->computeTangent(prev, next);
else
kf->computeTangent(prev, kf);
prev = kf;
kf = next;
}
valuesAreValid_ = true;
}
/*! Returns the Frame associated with the keyFrame at index \p index.
See also keyFrameTime(). \p index has to be in the range
0..numberOfKeyFrames()-1.
\note If this keyFrame was defined using a pointer to a Frame (see
addKeyFrame(const Frame* const)), the \e current pointed Frame state is
returned. */
CGAL_INLINE_FUNCTION
Frame KeyFrameInterpolator::keyFrame(int index) const {
const KeyFrame *const kf = keyFrame_.at(index);
return Frame(kf->position(), kf->orientation());
}
/*! Returns the time corresponding to the \p index keyFrame.
See also keyFrame(). \p index has to be in the range 0..numberOfKeyFrames()-1.
*/
CGAL_INLINE_FUNCTION
qreal KeyFrameInterpolator::keyFrameTime(int index) const {
return keyFrame_.at(index)->time();
}
/*! Returns the duration of the KeyFrameInterpolator path, expressed in seconds.
Simply corresponds to lastTime() - firstTime(). Returns 0.0 if the path has
fewer than 2 keyFrames. See also keyFrameTime(). */
CGAL_INLINE_FUNCTION
qreal KeyFrameInterpolator::duration() const {
return lastTime() - firstTime();
}
/*! Returns the time corresponding to the first keyFrame, expressed in seconds.
Returns 0.0 if the path is empty. See also lastTime(), duration() and
keyFrameTime(). */
CGAL_INLINE_FUNCTION
qreal KeyFrameInterpolator::firstTime() const {
if (keyFrame_.isEmpty())
return 0.0;
else
return keyFrame_.first()->time();
}
/*! Returns the time corresponding to the last keyFrame, expressed in seconds.
Returns 0.0 if the path is empty. See also firstTime(), duration() and
keyFrameTime(). */
CGAL_INLINE_FUNCTION
qreal KeyFrameInterpolator::lastTime() const {
if (keyFrame_.isEmpty())
return 0.0;
else
return keyFrame_.last()->time();
}
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::updateCurrentKeyFrameForTime(qreal time) {
// Assertion: times are sorted in monotone order.
// Assertion: keyFrame_ is not empty
// TODO: Special case for loops when closed path is implemented !!
if (!currentFrameValid_)
// Recompute everything from scratch
currentFrame_[1]->toFront();
while (currentFrame_[1]->peekNext()->time() > time) {
currentFrameValid_ = false;
if (!currentFrame_[1]->hasPrevious())
break;
currentFrame_[1]->previous();
}
if (!currentFrameValid_)
*currentFrame_[2] = *currentFrame_[1];
while (currentFrame_[2]->peekNext()->time() < time) {
currentFrameValid_ = false;
if (!currentFrame_[2]->hasNext())
break;
currentFrame_[2]->next();
}
if (!currentFrameValid_) {
*currentFrame_[1] = *currentFrame_[2];
if ((currentFrame_[1]->hasPrevious()) &&
(time < currentFrame_[2]->peekNext()->time()))
currentFrame_[1]->previous();
*currentFrame_[0] = *currentFrame_[1];
if (currentFrame_[0]->hasPrevious())
currentFrame_[0]->previous();
*currentFrame_[3] = *currentFrame_[2];
if (currentFrame_[3]->hasNext())
currentFrame_[3]->next();
currentFrameValid_ = true;
splineCacheIsValid_ = false;
}
// cout << "Time = " << time << " : " << currentFrame_[0]->peekNext()->time()
// << " , " << currentFrame_[1]->peekNext()->time() << " , " <<
// currentFrame_[2]->peekNext()->time() << " , " <<
// currentFrame_[3]->peekNext()->time() << endl;
}
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::updateSplineCache() {
Vec delta = currentFrame_[2]->peekNext()->position() -
currentFrame_[1]->peekNext()->position();
v1 = 3.0 * delta - 2.0 * currentFrame_[1]->peekNext()->tgP() -
currentFrame_[2]->peekNext()->tgP();
v2 = -2.0 * delta + currentFrame_[1]->peekNext()->tgP() +
currentFrame_[2]->peekNext()->tgP();
splineCacheIsValid_ = true;
}
/*! Interpolate frame() at time \p time (expressed in seconds).
interpolationTime() is set to \p time and frame() is set accordingly.
If you simply want to change interpolationTime() but not the frame() state,
use setInterpolationTime() instead.
Emits the interpolated() signal and makes the frame() emit the
Frame::interpolated() signal. */
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::interpolateAtTime(qreal time) {
setInterpolationTime(time);
if ((keyFrame_.isEmpty()) || (!frame()))
return;
if (!valuesAreValid_)
updateModifiedFrameValues();
updateCurrentKeyFrameForTime(time);
if (!splineCacheIsValid_)
updateSplineCache();
qreal alpha;
qreal dt = currentFrame_[2]->peekNext()->time() -
currentFrame_[1]->peekNext()->time();
if (dt == 0.0)
alpha = 0.0;
else
alpha = (time - currentFrame_[1]->peekNext()->time()) / dt;
// Linear interpolation - debug
// Vec pos = alpha*(currentFrame_[2]->peekNext()->position()) +
// (1.0-alpha)*(currentFrame_[1]->peekNext()->position());
Vec pos =
currentFrame_[1]->peekNext()->position() +
alpha * (currentFrame_[1]->peekNext()->tgP() + alpha * (v1 + alpha * v2));
Quaternion q = Quaternion::squad(
currentFrame_[1]->peekNext()->orientation(),
currentFrame_[1]->peekNext()->tgQ(), currentFrame_[2]->peekNext()->tgQ(),
currentFrame_[2]->peekNext()->orientation(), alpha);
frame()->setPositionAndOrientationWithConstraint(pos, q);
Q_EMIT interpolated();
}
#ifndef DOXYGEN
//////////// KeyFrame private class implementation /////////
CGAL_INLINE_FUNCTION
KeyFrameInterpolator::KeyFrame::KeyFrame(const Frame &fr, qreal t)
: time_(t), frame_(nullptr) {
p_ = fr.position();
q_ = fr.orientation();
}
CGAL_INLINE_FUNCTION
KeyFrameInterpolator::KeyFrame::KeyFrame(const Frame *fr, qreal t)
: time_(t), frame_(fr) {
updateValuesFromPointer();
}
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::KeyFrame::updateValuesFromPointer() {
p_ = frame()->position();
q_ = frame()->orientation();
}
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::KeyFrame::computeTangent(
const KeyFrame *const prev, const KeyFrame *const next) {
tgP_ = 0.5 * (next->position() - prev->position());
tgQ_ = Quaternion::squadTangent(prev->orientation(), q_, next->orientation());
}
CGAL_INLINE_FUNCTION
void KeyFrameInterpolator::KeyFrame::flipOrientationIfNeeded(
const Quaternion &prev) {
if (Quaternion::dot(prev, q_) < 0.0)
q_.negate();
}
#endif // DOXYGEN
}}
|