Developer Documentation
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 #ifndef SKELETONT_HH
45 #define SKELETONT_HH
46 
47 
48 //== INCLUDES =================================================================
49 #include <map>
50 #include <vector>
51 #include <iostream>
52 #include "JointT.hh"
53 #include "Properties.hh"
54 #include "Animation/AnimationT.hh"
55 #include "PoseT.hh"
56 
57 #include <stack>
58 
59 template <class PointT>
60 class SkeletonT : public Properties
61 {
62  template<typename>
63  friend class JointT;
64 
65 public:
66  typedef PointT Point;
67  typedef typename Point::value_type Scalar;
68  typedef JointT<Point> Joint;
69  typedef PoseT<PointT> Pose;
70  typedef AnimationT<PointT> Animation;
71  typedef typename ACG::Matrix4x4T<Scalar> Matrix;
72 
73 public:
74 
82  class Iterator
83  {
84  public:
85  Iterator();
86  Iterator(Joint *_root);
87  Iterator(const Iterator &other);
88  ~Iterator();
89  Iterator &operator=(const Iterator &other);
90 
91  public:
93  bool operator!=(const Iterator &other) const;
94  bool operator==(const Iterator &other) const;
95  Joint *operator*() const;
96  Joint *operator->() const;
97  operator bool() const;
98 
99  private:
100  Joint *nextSibling(Joint *_pParent, Joint *_pJoint);
101 
102  private:
103  // Holds the current position in the tree
104  Joint *pCurrent_;
105  // The stack of joints, marking a path back to the root joint
106  std::stack<Joint*> stJoints_;
107  };
108 
115  public:
116  explicit AnimationIterator(std::vector<Animation*>& _animations );
117  AnimationIterator(std::vector<Animation*>& _animations, size_t _animationIndex );
118 
119  public:
121  AnimationHandle operator*() const;
123  operator bool() const;
124 
125  private:
126  size_t currentIndex_;
127 
128  std::vector<Animation*>& animations_;
129  };
130 
131 public:
133  SkeletonT();
135  explicit SkeletonT(const SkeletonT<PointT>& _other);
137  SkeletonT& operator= (const SkeletonT<PointT>& _other);
138 
140  ~SkeletonT();
141 
142 public:
147  void addJoint(typename SkeletonT<PointT>::Joint *_pParent, typename SkeletonT<PointT>::Joint *_pJoint);
149  void insertJoint(typename SkeletonT<PointT>::Joint *_pChild, typename SkeletonT<PointT>::Joint *_pInsert);
150  void removeJoint(typename SkeletonT<PointT>::Joint *_pJoint);
151  inline void clear();
153 
159  inline Joint *root();
161  inline Joint *joint(const size_t &_index);
162  int parent(size_t _joint);
163  size_t childCount(size_t _joint);
164  size_t child(size_t _joint, size_t _child);
165  size_t jointCount();
166 
168  Iterator begin();
169  Iterator end();
171 
176 
178  inline Pose *pose(const AnimationHandle &_hAni);
179  inline Pose *referencePose();
180 
181  AnimationHandle addAnimation(std::string _name, Animation* _animation);
182  AnimationHandle cloneAnimation(std::string _name, const AnimationHandle &_hAni);
183  AnimationHandle animationHandle(std::string _name);
184  Animation *animation(std::string _name);
185  Animation *animation(const AnimationHandle &_hAni);
186  void removeAnimation(std::string _name);
187  void removeAnimation(const AnimationHandle &_hAni);
188  void clearAnimations();
189 
190  void replaceAnimationName(const std::string& _strOld, const std::string& _strNew) {
191  std::map<std::string,size_t>::iterator f = names_.find(_strOld);
192  if(f != names_.end()) {
193  size_t c = f->second;
194  names_.erase(f);
195  names_[_strNew] = c;
196  }
197  }
198 
202 
203  size_t animationCount();
204  const std::string &animationName(size_t _index);
206 
207 
208 protected:
210  void updateFromGlobal(size_t _idJoint);
211 
212 protected:
213 
215  std::vector<Joint*> joints_;
216 
218  std::map<std::string, size_t> names_;
220  std::vector<Animation*> animations_;
221 
224 };
225 
226 //=============================================================================
227 //=============================================================================
228 #if defined(INCLUDE_TEMPLATES) && !defined(SKELETON_C)
229 #define SKELETONT_TEMPLATES
230 #include "SkeletonT_impl.hh"
231 #endif
232 //=============================================================================
233 #endif // SKELETONT_HH defined
234 //=============================================================================
235 
std::map< std::string, size_t > names_
Binds a name to each animation.
Definition: SkeletonT.hh:218
AnimationIterator animationsBegin()
Iterator over the animations.
AnimationHandle addAnimation(std::string _name, Animation *_animation)
Adds a new animation to the list.
bool operator==(const Iterator &other) const
Compares the iterators.
size_t childCount(size_t _joint)
Returns the number of children of the given node.
void clear()
Removes all joints from the skeleton.
Pose referencePose_
The skeletons reference pose.
Definition: SkeletonT.hh:223
A general pose, used to store the frames of the animation.
Definition: PoseT.hh:58
bool operator!=(const Iterator &other) const
Compares the iterators.
size_t animationCount()
Returns the number of animations stored in this skeleton.
Iterator class for the skeleton.
Definition: SkeletonT.hh:82
Iterator end()
Compare an iterator with the return value of this method to test if it is done.
Joint * root()
Returns the root joint.
int parent(size_t _joint)
Returns the parents id of the given node.
const std::string & animationName(size_t _index)
Returns the name of the animation with the given index.
AnimationHandle cloneAnimation(std::string _name, const AnimationHandle &_hAni)
Creates a new animation by cloning an existing one.
~Iterator()
Destructor.
void insertJoint(typename SkeletonT< PointT >::Joint *_pChild, typename SkeletonT< PointT >::Joint *_pInsert)
insert a Joint given its future child joint
Joint * operator->() const
Returns a pointer to the current joint.
A handle used to refer to an animation or to a specific frame in an animation.
~SkeletonT()
Destructor.
Pose * referencePose()
Returns a pointer to the reference pose.
Iterator begin()
Iterator over joints of the skeletal tree in TOP-DOWN order (from root to leafs)
The properties storage class.
Definition: Properties.hh:92
Pose * pose(const AnimationHandle &_hAni)
Returns a pointer to the pose with the given animation handle.
void replaceAnimationName(const std::string &_strOld, const std::string &_strNew)
Returns a pointer to the pose with the given animation handle.
Definition: SkeletonT.hh:190
Represents a single joint in the skeleton.
Definition: JointT.hh:60
AnimationIterator animationsEnd()
Returns an iterator pointing behind the last animation.
SkeletonT()
Default constructor.
void removeAnimation(std::string _name)
Removes an animation from the list.
std::vector< Animation * > animations_
Animations defined on the skeleton.
Definition: SkeletonT.hh:220
void clearAnimations()
Removes all animations.
AnimationHandle animationHandle(std::string _name)
Get an AnimationHandle to the animation with the given name.
size_t jointCount()
Returns the number of joints.
Stores a single animation.
Definition: AnimationT.hh:58
size_t child(size_t _joint, size_t _child)
Returns the child with the given index.
Animation * animation(std::string _name)
Returns a pointer to the animation to the given name.
The header for the properties, one file for all objects.
Iterator class for the animations attached to a skeleton.
Definition: SkeletonT.hh:114
Joint * nextSibling(Joint *_pParent, Joint *_pJoint)
Given a parent and one of its child nodes this method finds and returns the next sibling.
Joint * joint(const size_t &_index)
Returns the joint with the given index.
Iterator & operator=(const Iterator &other)
Assignment Operator.
Iterator & operator++()
Increase the iterator.
void updateFromGlobal(size_t _idJoint)
update the structure when parent changes for a joint
Iterator()
Default constructor.
void addJoint(typename SkeletonT< PointT >::Joint *_pParent, typename SkeletonT< PointT >::Joint *_pJoint)
Adds a joint as child of a given parent joint.
Joint * operator*() const
Returns a pointer to the current joint.
std::vector< Joint * > joints_
Joints of the skeleton.
Definition: SkeletonT.hh:215
void removeJoint(typename SkeletonT< PointT >::Joint *_pJoint)
Remove the given joint from the tree.