keyFrameInterpolator.h
1 /****************************************************************************
2 
3  Copyright (C) 2002-2013 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.5.2.
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 #include <QObject>
27 #include <QTimer>
28 
29 #include "quaternion.h"
30 // Not actually needed, but some bad compilers (Microsoft VS6) complain.
31 #include "frame.h"
32 
33 // If you compiler complains about incomplete type, uncomment the next line
34 // #include "frame.h"
35 // and comment "class Frame;" 3 lines below
36 
37 namespace qglviewer {
38 class Camera;
39 class Frame;
126 class QGLVIEWER_EXPORT KeyFrameInterpolator : public QObject
127 {
128  // todo closedPath, insertKeyFrames, deleteKeyFrame, replaceKeyFrame
129  Q_OBJECT
130 
131 public:
132  KeyFrameInterpolator(Frame* fr=NULL);
133  virtual ~KeyFrameInterpolator();
134 
135 Q_SIGNALS:
150  void interpolated();
151 
157  void endReached();
158 
161 public Q_SLOTS:
162  void addKeyFrame(const Frame& frame);
163  void addKeyFrame(const Frame& frame, float time);
164 
165  void addKeyFrame(const Frame* const frame);
166  void addKeyFrame(const Frame* const frame, float time);
167 
168  void deletePath();
170 
173 public:
180  Frame* frame() const { return frame_; }
181 
182 public Q_SLOTS:
183  void setFrame(Frame* const frame);
185 
188 public:
189  Frame keyFrame(int index) const;
190  float keyFrameTime(int index) const;
192  int numberOfKeyFrames() const { return keyFrame_.count(); }
193  float duration() const;
194  float firstTime() const;
195  float lastTime() const;
197 
200 public:
205  float interpolationTime() const { return interpolationTime_; }
213  float interpolationSpeed() const { return interpolationSpeed_; }
222  int interpolationPeriod() const { return period_; }
232  bool loopInterpolation() const { return loopInterpolation_; }
233 #ifndef DOXYGEN
234 
239  bool closedPath() const { return closedPath_; }
240 #endif
241 public Q_SLOTS:
247  void setInterpolationTime(float time) { interpolationTime_ = time; }
249  void setInterpolationSpeed(float speed) { interpolationSpeed_ = speed; }
251  void setInterpolationPeriod(int period) { period_ = period; }
253  void setLoopInterpolation(bool loop=true) { loopInterpolation_ = loop; }
254 #ifndef DOXYGEN
255 
256  void setClosedPath(bool closed=true) { closedPath_ = closed; }
257 #endif
258 
259 
260 
263 public:
266  bool interpolationIsStarted() const { return interpolationStarted_; }
267 public Q_SLOTS:
268  void startInterpolation(int period = -1);
269  void stopInterpolation();
270  void resetInterpolation();
272  void toggleInterpolation() { if (interpolationIsStarted()) stopInterpolation(); else startInterpolation(); }
273  virtual void interpolateAtTime(float time);
275 
278 public:
279  virtual void drawPath(int mask=1, int nbFrames=6, float scale=1.0f);
281 
284 public:
285  virtual QDomElement domElement(const QString& name, QDomDocument& document) const;
286  virtual void initFromDOMElement(const QDomElement& element);
288 
289 private Q_SLOTS:
290  virtual void update();
291  virtual void invalidateValues() { valuesAreValid_ = false; pathIsValid_ = false; splineCacheIsValid_ = false; }
292 
293 private:
294  // Copy constructor and opertor= are declared private and undefined
295  // Prevents everyone from trying to use them
296  // KeyFrameInterpolator(const KeyFrameInterpolator& kfi);
297  // KeyFrameInterpolator& operator=(const KeyFrameInterpolator& kfi);
298 
299  void updateCurrentKeyFrameForTime(float time);
300  void updateModifiedFrameValues();
301  void updateSplineCache();
302 
303 #ifndef DOXYGEN
304  // Internal private KeyFrame representation
305  class KeyFrame
306  {
307  public:
308  KeyFrame(const Frame& fr, float t);
309  KeyFrame(const Frame* fr, float t);
310 
311  Vec position() const { return p_; }
312  Quaternion orientation() const { return q_; }
313  Vec tgP() const { return tgP_; }
314  Quaternion tgQ() const { return tgQ_; }
315  float time() const { return time_; }
316  const Frame* frame() const { return frame_; }
317  void updateValuesFromPointer();
318  void flipOrientationIfNeeded(const Quaternion& prev);
319  void computeTangent(const KeyFrame* const prev, const KeyFrame* const next);
320  private:
321  Vec p_, tgP_;
322  Quaternion q_, tgQ_;
323  float time_;
324  const Frame* const frame_;
325  };
326 #endif
327 
328  // K e y F r a m e s
329  mutable QList<KeyFrame*> keyFrame_;
330  QMutableListIterator<KeyFrame*>* currentFrame_[4];
331  QList<Frame> path_;
332 
333  // A s s o c i a t e d f r a m e
334  Frame* frame_;
335 
336  // R h y t h m
337  QTimer timer_;
338  int period_;
339  float interpolationTime_;
340  float interpolationSpeed_;
341  bool interpolationStarted_;
342 
343  // M i s c
344  bool closedPath_;
345  bool loopInterpolation_;
346 
347  // C a c h e d v a l u e s a n d f l a g s
348  bool pathIsValid_;
349  bool valuesAreValid_;
350  bool currentFrameValid_;
351  bool splineCacheIsValid_;
352  Vec v1, v2;
353 };
354 
355 } // namespace qglviewer
356 
357 #endif // QGLVIEWER_KEY_FRAME_INTERPOLATOR_H
A keyFrame Catmull-Rom Frame interpolator.
void setInterpolationTime(float time)
Sets the interpolationTime().
void setInterpolationPeriod(int period)
Sets the interpolationPeriod().
float interpolationSpeed() const
Returns the current interpolation speed.
int interpolationPeriod() const
Returns the current interpolation period, expressed in milliseconds.
int numberOfKeyFrames() const
Returns the number of keyFrames used by the interpolation.
Frame * frame() const
Returns the associated Frame and that is interpolated by the KeyFrameInterpolator.
bool interpolationIsStarted() const
Returns true when the interpolation is being performed.
float interpolationTime() const
Returns the current interpolation time (in seconds) along the KeyFrameInterpolator path...
void setInterpolationSpeed(float speed)
Sets the interpolationSpeed().
void setClosedPath(bool closed=true)
Sets the closedPath() value.
bool loopInterpolation() const
Returns true when the interpolation is played in an infinite loop.
bool closedPath() const
Whether or not (default) the path defined by the keyFrames is a closed loop.
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition: frame.h:121
void setLoopInterpolation(bool loop=true)
Sets the loopInterpolation() value.
void toggleInterpolation()
Calls startInterpolation() or stopInterpolation(), depending on interpolationIsStarted().