OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Target.h
Go to the documentation of this file.
1 /* -*-c++-*-
2  * Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
3  *
4  * This library is open source and may be redistributed and/or modified under
5  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6  * (at your option) any later version. The full license is in LICENSE file
7  * included with this distribution, and on the openscenegraph.org website.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * OpenSceneGraph Public License for more details.
13 */
14 
15 
16 #ifndef OSGANIMATION_TARGET
17 #define OSGANIMATION_TARGET 1
18 
19 #include <vector>
20 #include <osg/Quat>
21 #include <osg/Vec3>
22 #include <osg/Vec2>
23 #include <osg/Vec4>
24 #include <osg/Referenced>
25 #include <osgAnimation/Export>
26 
27 namespace osgAnimation
28 {
29 
30  class Channel;
31 
33  {
34  public:
35 
36  Target();
37  virtual ~Target() {}
38  void reset() { _weight = 0; _priorityWeight = 0; }
39  int getCount() const { return referenceCount(); }
40  float getWeight() const { return _weight; }
41  protected:
42  float _weight;
45  };
46 
47 
48  template <class T>
49  class TemplateTarget : public Target
50  {
51  public:
52 
54  TemplateTarget(const T& v) { setValue(v); }
56 
57  inline void lerp(float t, const T& a, const T& b);
58 
68  void update(float weight, const T& val, int priority)
69  {
70  if (_weight || _priorityWeight)
71  {
72  if (_lastPriority != priority)
73  {
74  // change in priority
75  // add to weight with the same previous priority cumulated weight
76  _weight += _priorityWeight * (1.0 - _weight);
77  _priorityWeight = 0;
78  _lastPriority = priority;
79  }
80 
81  _priorityWeight += weight;
82  float t = (1.0 - _weight) * weight / _priorityWeight;
83  lerp(t, _target, val);
84  }
85  else
86  {
87  _priorityWeight = weight;
88  _lastPriority = priority;
89  _target = val;
90  }
91  }
92  const T& getValue() const { return _target; }
93 
94  void setValue(const T& value) { _target = value; }
95 
96  protected:
97 
99  };
100 
101  template <class T>
102  inline void TemplateTarget<T>::lerp(float t, const T& a, const T& b)
103  {
104  _target = a * (1.0f - t) + b * t;
105  }
106 
107  template <>
108  inline void TemplateTarget<osg::Quat>::lerp(float t, const osg::Quat& a, const osg::Quat& b)
109  {
110  if (a.asVec4() * b.asVec4() < 0.0)
111  {
112  _target = a * (1.0f - t) + b * -t;
113  }
114  else
115  {
116  _target = a * (1.0f - t) + b * t;
117  }
118 
119  osg::Quat::value_type len2 = _target.length2();
120  if ( len2 != 1.0 && len2 != 0.0)
121  _target *= 1.0/sqrt(len2);
122  }
123 
131 
132 }
133 
134 #endif
#define OSGANIMATION_EXPORT
Definition: Export.h:40
float _priorityWeight
Definition: Target.h:43
void lerp(float t, const T &a, const T &b)
Definition: Target.h:102
double value_type
Definition: Quat.h:34
Vec4d asVec4() const
Definition: Quat.h:108
void update(float weight, const T &val, int priority)
Definition: Target.h:68
const T & getValue() const
Definition: Target.h:92
float getWeight() const
Definition: Target.h:40
TemplateTarget< osg::Matrixf > MatrixTarget
Definition: Target.h:124
TemplateTarget(const T &v)
Definition: Target.h:54
TemplateTarget< osg::Vec4 > Vec4Target
Definition: Target.h:127
void setValue(const T &value)
Definition: Target.h:94
TemplateTarget< osg::Vec3 > Vec3Target
Definition: Target.h:126
TemplateTarget< osg::Quat > QuatTarget
Definition: Target.h:125
TemplateTarget< double > DoubleTarget
Definition: Target.h:130
value_type length2() const
Length of the quaternion = vec . vec.
Definition: Quat.h:294
TemplateTarget< float > FloatTarget
Definition: Target.h:129
TemplateTarget(const TemplateTarget &v)
Definition: Target.h:55
Definition: Quat.h:29
virtual ~Target()
Definition: Target.h:37
TemplateTarget< osg::Vec2 > Vec2Target
Definition: Target.h:128
int getCount() const
Definition: Target.h:39