Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
SkeletonT.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #ifndef SKELETONT_HH
51 #define SKELETONT_HH
52 
53 
54 //== INCLUDES =================================================================
55 #include <map>
56 #include <vector>
57 #include <iostream>
58 #include "JointT.hh"
59 #include "Properties.hh"
60 #include "Animation/AnimationT.hh"
61 #include "PoseT.hh"
62 
63 #include <stack>
64 
65 template <class PointT>
66 class SkeletonT : public Properties
67 {
68  template<typename>
69  friend class JointT;
70 
71 public:
72  typedef PointT Point;
73  typedef typename Point::value_type Scalar;
74  typedef JointT<Point> Joint;
75  typedef PoseT<PointT> Pose;
76  typedef AnimationT<PointT> Animation;
77  typedef typename ACG::Matrix4x4T<Scalar> Matrix;
78 
79 public:
80 
88  class Iterator
89  {
90  public:
91  Iterator();
92  Iterator(Joint *_root);
93  Iterator(const Iterator &other);
94  ~Iterator();
95  Iterator &operator=(const Iterator &other);
96 
97  public:
99  bool operator!=(const Iterator &other) const;
100  bool operator==(const Iterator &other) const;
101  Joint *operator*() const;
102  Joint *operator->() const;
103  operator bool() const;
104 
105  private:
106  Joint *nextSibling(Joint *_pParent, Joint *_pJoint);
107 
108  private:
109  // Holds the current position in the tree
110  Joint *pCurrent_;
111  // The stack of joints, marking a path back to the root joint
112  std::stack<Joint*> stJoints_;
113  };
114 
121  public:
122  AnimationIterator(std::vector<Animation*>& _animations );
123  AnimationIterator(std::vector<Animation*>& _animations, unsigned int _animationIndex );
124 
125  public:
127  AnimationHandle operator*() const;
129  operator bool() const;
130 
131  private:
132  unsigned int currentIndex_;
133 
134  std::vector<Animation*>& animations_;
135  };
136 
137 public:
139  SkeletonT();
141  SkeletonT(const SkeletonT<PointT>& _other);
143  SkeletonT& operator= (const SkeletonT<PointT>& _other);
144 
146  ~SkeletonT();
147 
148 public:
153  void addJoint(typename SkeletonT<PointT>::Joint *_pParent, typename SkeletonT<PointT>::Joint *_pJoint);
155  void insertJoint(typename SkeletonT<PointT>::Joint *_pChild, typename SkeletonT<PointT>::Joint *_pInsert);
156  void removeJoint(typename SkeletonT<PointT>::Joint *_pJoint);
157  inline void clear();
159 
165  inline Joint *root();
167  inline Joint *joint(const unsigned int &_index);
168  int parent(unsigned int _joint);
169  unsigned int childCount(unsigned int _joint);
170  unsigned int child(unsigned int _joint, unsigned int _child);
171  unsigned int jointCount();
172 
174  Iterator begin();
175  Iterator end();
177 
182 
184  inline Pose *pose(const AnimationHandle &_hAni);
185  inline Pose *referencePose();
186 
187  AnimationHandle addAnimation(std::string _name, Animation* _animation);
188  AnimationHandle cloneAnimation(std::string _name, const AnimationHandle &_hAni);
189  AnimationHandle animationHandle(std::string _name);
190  Animation *animation(std::string _name);
191  Animation *animation(const AnimationHandle &_hAni);
192  void removeAnimation(std::string _name);
193  void removeAnimation(const AnimationHandle &_hAni);
194  void clearAnimations();
195 
196  void replaceAnimationName(const std::string& _strOld, const std::string& _strNew) {
197  std::map<std::string,unsigned int>::iterator f = names_.find(_strOld);
198  if(f != names_.end()) {
199  unsigned int c = f->second;
200  names_.erase(f);
201  names_[_strNew] = c;
202  }
203  }
204 
206  AnimationIterator animationsBegin();
207  AnimationIterator animationsEnd();
208 
209  unsigned int animationCount();
210  const std::string &animationName(unsigned int _index);
212 
213 
214 protected:
216  void updateFromGlobal(unsigned int _idJoint);
217 
218 protected:
219 
221  std::vector<Joint*> joints_;
222 
224  std::map<std::string, unsigned int> names_;
226  std::vector<Animation*> animations_;
227 
230 };
231 
232 //=============================================================================
233 //=============================================================================
234 #if defined(INCLUDE_TEMPLATES) && !defined(SKELETON_C)
235 #define SKELETONT_TEMPLATES
236 #include "SkeletonT.cc"
237 #endif
238 //=============================================================================
239 #endif // SKELETONT_HH defined
240 //=============================================================================
241 
unsigned int child(unsigned int _joint, unsigned int _child)
Returns the child with the given index.
Definition: SkeletonT.cc:690
unsigned int childCount(unsigned int _joint)
Returns the number of children of the given node.
Definition: SkeletonT.cc:671
AnimationIterator(std::vector< Animation * > &_animations)
Default constructor.
Definition: SkeletonT.cc:294
std::vector< Joint * > joints_
Joints of the skeleton.
Definition: SkeletonT.hh:221
int parent(unsigned int _joint)
Returns the parents id of the given node.
Definition: SkeletonT.cc:658
Iterator class for the animations attached to a skeleton.
Definition: SkeletonT.hh:120
void addJoint(typename SkeletonT< PointT >::Joint *_pParent, typename SkeletonT< PointT >::Joint *_pJoint)
Adds a joint as child of a given parent joint.
Definition: SkeletonT.cc:494
void clearAnimations()
Removes all animations.
Definition: SkeletonT.cc:929
bool operator==(const Iterator &other) const
Compares the iterators.
Definition: SkeletonT.cc:206
Pose referencePose_
The skeletons reference pose.
Definition: SkeletonT.hh:229
~Iterator()
Destructor.
Definition: SkeletonT.cc:119
The properties storage class.
Definition: Properties.hh:100
Pose * pose(const AnimationHandle &_hAni)
Returns a pointer to the pose with the given animation handle.
Definition: SkeletonT.cc:741
Joint * root()
Returns the root joint.
Definition: SkeletonT.cc:624
AnimationIterator animationsEnd()
Returns an iterator pointing behind the last animation.
Definition: SkeletonT.cc:956
Pose * referencePose()
Returns a pointer to the reference pose.
Definition: SkeletonT.cc:761
Joint * nextSibling(Joint *_pParent, Joint *_pJoint)
Given a parent and one of its child nodes this method finds and returns the next sibling.
Definition: SkeletonT.cc:264
Animation * animation(std::string _name)
Returns a pointer to the animation to the given name.
Definition: SkeletonT.cc:858
Iterator & operator=(const Iterator &other)
Assignment Operator.
Definition: SkeletonT.cc:132
void removeAnimation(std::string _name)
Removes an animation from the list.
Definition: SkeletonT.cc:886
~SkeletonT()
Destructor.
Definition: SkeletonT.cc:476
A general pose, used to store the frames of the animation.
Definition: PoseT.hh:68
unsigned int jointCount()
Returns the number of joints.
Definition: SkeletonT.cc:701
Iterator end()
Compare an iterator with the return value of this method to test if it is done.
Definition: SkeletonT.cc:725
Iterator class for the skeleton.
Definition: SkeletonT.hh:88
Iterator()
Default constructor.
Definition: SkeletonT.cc:79
Joint * joint(const unsigned int &_index)
Returns the joint with the given index.
Definition: SkeletonT.cc:642
void removeJoint(typename SkeletonT< PointT >::Joint *_pJoint)
Remove the given joint from the tree.
Definition: SkeletonT.cc:538
AnimationHandle animationHandle(std::string _name)
Get an AnimationHandle to the animation with the given name.
Definition: SkeletonT.cc:843
AnimationHandle cloneAnimation(std::string _name, const AnimationHandle &_hAni)
Creates a new animation by cloning an existing one.
Definition: SkeletonT.cc:809
AnimationIterator & operator=(const AnimationIterator &other)
Operator =.
Definition: SkeletonT.cc:344
Iterator begin()
Iterator over joints of the skeletal tree in TOP-DOWN order (from root to leafs)
Definition: SkeletonT.cc:714
The header for the properties, one file for all objects.
AnimationIterator & operator++()
Increase the iterator.
Definition: SkeletonT.cc:328
void replaceAnimationName(const std::string &_strOld, const std::string &_strNew)
Returns a pointer to the pose with the given animation handle.
Definition: SkeletonT.hh:196
std::map< std::string, unsigned int > names_
Binds a name to each animation.
Definition: SkeletonT.hh:224
unsigned int animationCount()
Returns the number of animations stored in this skeleton.
Definition: SkeletonT.cc:971
Joint * operator->() const
Returns a pointer to the current joint.
Definition: SkeletonT.cc:235
void updateFromGlobal(unsigned int _idJoint)
update the structure when parent changes for a joint
Definition: SkeletonT.cc:1006
bool operator!=(const Iterator &other) const
Compares the iterators.
Definition: SkeletonT.cc:193
void insertJoint(typename SkeletonT< PointT >::Joint *_pChild, typename SkeletonT< PointT >::Joint *_pInsert)
insert a Joint given its future child joint
Definition: SkeletonT.cc:1024
Iterator & operator++()
Increase the iterator.
Definition: SkeletonT.cc:147
AnimationHandle operator*() const
Get an animation handle for the current animation.
Definition: SkeletonT.cc:365
A handle used to refer to an animation or to a specific frame in an animation.
SkeletonT & operator=(const SkeletonT< PointT > &_other)
Assignment operator.
Definition: SkeletonT.cc:431
Stores a single animation.
Definition: AnimationT.hh:67
Joint * operator*() const
Returns a pointer to the current joint.
Definition: SkeletonT.cc:217
const std::string & animationName(unsigned int _index)
Returns the name of the animation with the given index.
Definition: SkeletonT.cc:984
AnimationHandle addAnimation(std::string _name, Animation *_animation)
Adds a new animation to the list.
Definition: SkeletonT.cc:775
Represents a single joint in the skeleton.
Definition: JointT.hh:66
std::vector< Animation * > animations_
Animations defined on the skeleton.
Definition: SkeletonT.hh:226
AnimationIterator animationsBegin()
Iterator over the animations.
Definition: SkeletonT.cc:945
void clear()
Removes all joints from the skeleton.
Definition: SkeletonT.cc:599
SkeletonT()
Default constructor.
Definition: SkeletonT.cc:377