SkeletonT.hh 7.43 KB
Newer Older
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

Dirk Wilden's avatar
Dirk Wilden committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#ifndef SKELETONT_HH
#define SKELETONT_HH


//== INCLUDES =================================================================
#include <map>
#include <vector>
#include <iostream>
#include "JointT.hh"
#include "Properties.hh"
#include "Animation/AnimationT.hh"
#include "PoseT.hh"

#include <stack>

template <class PointT>
class SkeletonT : public Properties
{
  template<typename>
  friend class JointT;

public:
  typedef PointT                                  Point;
  typedef typename Point::value_type             Scalar;
  typedef JointT<Point>                           Joint;
  typedef PoseT<PointT>                            Pose;
  typedef AnimationT<PointT>                  Animation;
  typedef typename ACG::Matrix4x4T<Scalar>       Matrix;

public:

  /**
    * @brief Iterator class for the skeleton
    *
    * This iterator can be used to iterate over all joints in a top-down (root to leaf), left-to-right order.
    * It is possible to iterate over subtrees by constructing an iterator passing the root of the subtree
    * as parameter. Cast the iterator to boolean to test if it iterated all joints.
    */
  class Iterator
  {
  public:
    Iterator();
    Iterator(Joint *_root);
    Iterator(const Iterator &other);
    ~Iterator();
    Iterator &operator=(const Iterator &other);

  public:
    Iterator &operator++();
    bool operator!=(const Iterator &other) const;
    bool operator==(const Iterator &other) const;
    Joint *operator*() const;
    Joint *operator->() const;
    operator bool() const;

  private:
    Joint *nextSibling(Joint *_pParent, Joint *_pJoint);

  private:
    // Holds the current position in the tree
    Joint *pCurrent_;
    // The stack of joints, marking a path back to the root joint
    stack<Joint*> stJoints_;
  };


public:
  /// Default constructor
  SkeletonT();
  /// Copy constructor
113 114 115 116
  SkeletonT(const SkeletonT<PointT>& _other);
  /// Assignment operator
  SkeletonT& operator= (const SkeletonT<PointT>& _other);

Dirk Wilden's avatar
Dirk Wilden committed
117 118 119 120 121 122 123 124
  /// Destructor
  ~SkeletonT();

public:
  /**
    * @name Modifying the tree structure
    * Use these methods to edit the skeleton tree.
    */
Jan Möbius's avatar
Jan Möbius committed
125
  ///@{
Dirk Wilden's avatar
Dirk Wilden committed
126 127 128
  void addJoint(typename SkeletonT<PointT>::Joint *_pParent, typename SkeletonT<PointT>::Joint *_pJoint);
  void removeJoint(typename SkeletonT<PointT>::Joint *_pJoint);
  inline void clear();
Jan Möbius's avatar
Jan Möbius committed
129
  ///@}
Dirk Wilden's avatar
Dirk Wilden committed
130

Dirk Wilden's avatar
Dirk Wilden committed
131 132 133
  /** \anchor JointAccess
    * @name   Basic Joint Access
    *
Dirk Wilden's avatar
Dirk Wilden committed
134 135
    * Use these methods to access joints in the skeleton.
    */
Jan Möbius's avatar
Jan Möbius committed
136
  ///@{
Dirk Wilden's avatar
Dirk Wilden committed
137 138 139 140 141 142 143 144 145 146
  inline Joint *root();
  inline Joint *joint(const unsigned int &_index);
  int parent(unsigned int _joint);
  unsigned int childCount(unsigned int _joint);
  unsigned int child(unsigned int _joint, unsigned int _child);
  unsigned int jointCount();
  
  /// Iterator over joints of the skeletal tree in TOP-DOWN order (from root to leafs)
  Iterator begin();
  Iterator end();
Jan Möbius's avatar
Jan Möbius committed
147
  ///@}
Dirk Wilden's avatar
Dirk Wilden committed
148 149
 

Dirk Wilden's avatar
Dirk Wilden committed
150 151
  /** \anchor AnimationAccess
    * @name   Animation
Dirk Wilden's avatar
Dirk Wilden committed
152 153
    * Use these methods to equip the skeleton with animation data.
    */
Jan Möbius's avatar
Jan Möbius committed
154
  ///@{
Dirk Wilden's avatar
Dirk Wilden committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

  inline Pose *pose(const AnimationHandle &_hAni);
  inline Pose *referencePose();

  AnimationHandle addAnimation(std::string _name, Animation* _animation);
  AnimationHandle cloneAnimation(std::string _name, const AnimationHandle &_hAni);
  AnimationHandle animationHandle(std::string _name);
  Animation *animation(std::string _name);
  Animation *animation(const AnimationHandle &_hAni);
  void removeAnimation(std::string _name);
  void removeAnimation(const AnimationHandle &_hAni);
  void clearAnimations();

  unsigned int animationCount();
  const std::string &animationName(unsigned int _index);
Jan Möbius's avatar
Jan Möbius committed
170
  ///@}
Dirk Wilden's avatar
Dirk Wilden committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200


protected:
  /// update the structure when parent changes for a joint
  void updateFromGlobal(unsigned int _idJoint);

protected:
  
  /// Joints of the skeleton
  std::vector<Joint*> joints_;

  /// Binds a name to each animation
  std::map<std::string, unsigned int> names_;
  /// Animations defined on the skeleton
  std::vector<Animation*> animations_;

  /// The skeletons reference pose
  Pose referencePose_;
};

//=============================================================================
//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(SKELETON_C)
#define SKELETONT_TEMPLATES
#include "SkeletonT.cc"
#endif
//=============================================================================
#endif // SKELETONT_HH defined
//=============================================================================