keyFrameInterpolator.h
1 /****************************************************************************
2 
3  Copyright (C) 2002-2008 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.3.10.
6 
7  http://www.libqglviewer.com - contact@libqglviewer.com
8 
9  This file may be used under the terms of the GNU General Public License
10  versions 2.0 or 3.0 as published by the Free Software Foundation and
11  appearing in the LICENSE file included in the packaging of this file.
12  In addition, as a special exception, Gilles Debunne gives you certain
13  additional rights, described in the file GPL_EXCEPTION in this package.
14 
15  libQGLViewer uses dual licensing. Commercial/proprietary software must
16  purchase a libQGLViewer Commercial License.
17 
18  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 
21 *****************************************************************************/
22 
23 #ifndef QGLVIEWER_KEY_FRAME_INTERPOLATOR_H
24 #define QGLVIEWER_KEY_FRAME_INTERPOLATOR_H
25 
26 #if QT_VERSION > 0x040000
27 # include <QObject>
28 # include <QTimer>
29 #else
30 # include <qobject.h>
31 # include <qtimer.h>
32 #endif
33 
34 #include "quaternion.h"
35 // Not actually needed, but some bad compilers (Microsoft VS6) complain.
36 #include "frame.h"
37 
38 // If you compiler complains about incomplete type, uncomment the next line
39 // #include "frame.h"
40 // and comment "class Frame;" 3 lines below
41 
42 namespace qglviewer {
43  class Camera;
44  class Frame;
131  class QGLVIEWER_EXPORT KeyFrameInterpolator : public QObject
132  {
133  // todo closedPath, insertKeyFrames, deleteKeyFrame, replaceKeyFrame
134  Q_OBJECT
135 
136  public:
137  KeyFrameInterpolator(Frame* fr=NULL);
138  virtual ~KeyFrameInterpolator();
139 
140  Q_SIGNALS:
155  void interpolated();
156 
162  void endReached();
163 
166  public Q_SLOTS:
167  void addKeyFrame(const Frame& frame);
168  void addKeyFrame(const Frame& frame, float time);
169 
170  void addKeyFrame(const Frame* const frame);
171  void addKeyFrame(const Frame* const frame, float time);
172 
173  void deletePath();
175 
178  public:
185  Frame* frame() const { return frame_; };
186 
187  public Q_SLOTS:
188  void setFrame(Frame* const frame);
190 
193  public:
194  Frame keyFrame(int index) const;
195  float keyFrameTime(int index) const;
197  int numberOfKeyFrames() const { return keyFrame_.count(); };
198  float duration() const;
199  float firstTime() const;
200  float lastTime() const;
202 
205  public:
210  float interpolationTime() const { return interpolationTime_; };
218  float interpolationSpeed() const { return interpolationSpeed_; };
227  int interpolationPeriod() const { return period_; };
237  bool loopInterpolation() const { return loopInterpolation_; };
238 #ifndef DOXYGEN
239 
244  bool closedPath() const { return closedPath_; };
245 #endif
246  public Q_SLOTS:
252  void setInterpolationTime(float time) { interpolationTime_ = time; };
254  void setInterpolationSpeed(float speed) { interpolationSpeed_ = speed; };
256  void setInterpolationPeriod(int period) { period_ = period; };
258  void setLoopInterpolation(bool loop=true) { loopInterpolation_ = loop; };
259 #ifndef DOXYGEN
260 
261  void setClosedPath(bool closed=true) { closedPath_ = closed; };
262 #endif
263 
264 
265 
268  public:
271  bool interpolationIsStarted() const { return interpolationStarted_; };
272  public Q_SLOTS:
273  void startInterpolation(int period = -1);
274  void stopInterpolation();
275  void resetInterpolation();
277  void toggleInterpolation() { if (interpolationIsStarted()) stopInterpolation(); else startInterpolation(); };
278  virtual void interpolateAtTime(float time);
280 
283  public:
284  virtual void drawPath(int mask=1, int nbFrames=6, float scale=1.0f);
286 
289  public:
290  virtual QDomElement domElement(const QString& name, QDomDocument& document) const;
291  virtual void initFromDOMElement(const QDomElement& element);
293 
294  private Q_SLOTS:
295  virtual void update();
296  virtual void invalidateValues() { valuesAreValid_ = false; pathIsValid_ = false; splineCacheIsValid_ = false; };
297 
298  private:
299  // Copy constructor and opertor= are declared private and undefined
300  // Prevents everyone from trying to use them
301  // KeyFrameInterpolator(const KeyFrameInterpolator& kfi);
302  // KeyFrameInterpolator& operator=(const KeyFrameInterpolator& kfi);
303 
304  void updateCurrentKeyFrameForTime(float time);
305  void updateModifiedFrameValues();
306  void updateSplineCache();
307 
308 #ifndef DOXYGEN
309  // Internal private KeyFrame representation
310  class KeyFrame
311  {
312  public:
313  KeyFrame(const Frame& fr, float t);
314  KeyFrame(const Frame* fr, float t);
315 
316  Vec position() const { return p_; }
317  Quaternion orientation() const { return q_; }
318  Vec tgP() const { return tgP_; }
319  Quaternion tgQ() const { return tgQ_; }
320  float time() const { return time_; }
321  const Frame* frame() const { return frame_; }
322  void updateValuesFromPointer();
323  void flipOrientationIfNeeded(const Quaternion& prev);
324  void computeTangent(const KeyFrame* const prev, const KeyFrame* const next);
325  private:
326  Vec p_, tgP_;
327  Quaternion q_, tgQ_;
328  float time_;
329  const Frame* const frame_;
330  };
331 #endif
332 
333  // K e y F r a m e s
334 #if QT_VERSION >= 0x040000
335  mutable QList<KeyFrame*> keyFrame_;
336  QMutableListIterator<KeyFrame*>* currentFrame_[4];
337  QList<Frame> path_;
338 #else
339  mutable QPtrList<KeyFrame> keyFrame_;
340  // 4 succesive frames. interpolationTime_ is between index 1 and 2.
341  QPtrListIterator<KeyFrame>* currentFrame_[4];
342 # if QT_VERSION >= 0x030000
343  // Cached path computed values (for drawPath()).
344  QValueVector<Frame> path_;
345 # else
346  QVector<Frame> path_;
347 # endif
348 #endif
349 
350  // A s s o c i a t e d f r a m e
351  Frame* frame_;
352 
353  // R h y t h m
354  QTimer timer_;
355  int period_;
356  float interpolationTime_;
357  float interpolationSpeed_;
358  bool interpolationStarted_;
359 
360  // M i s c
361  bool closedPath_;
362  bool loopInterpolation_;
363 
364  // C a c h e d v a l u e s a n d f l a g s
365  bool pathIsValid_;
366  bool valuesAreValid_;
367  bool currentFrameValid_;
368  bool splineCacheIsValid_;
369  Vec v1, v2;
370  };
371 
372 } // namespace qglviewer
373 
374 #endif // QGLVIEWER_KEY_FRAME_INTERPOLATOR_H