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:
92 Iterator &operator++();
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
114 class AnimationIterator {
115 public:
116 explicit AnimationIterator(std::vector<Animation*>& _animations );
117 AnimationIterator(std::vector<Animation*>& _animations, size_t _animationIndex );
118
119 public:
120 AnimationIterator &operator++();
121 AnimationHandle operator*() const;
122 AnimationIterator &operator=(const AnimationIterator &other);
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
200 AnimationIterator animationsBegin();
201 AnimationIterator animationsEnd();
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
223 Pose referencePose_;
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
SkeletonT::names_
std::map< std::string, size_t > names_
Binds a name to each animation.
Definition: SkeletonT.hh:218
SkeletonT::animationsBegin
AnimationIterator animationsBegin()
Iterator over the animations.
Definition: SkeletonT_impl.hh:939
SkeletonT::addAnimation
AnimationHandle addAnimation(std::string _name, Animation *_animation)
Adds a new animation to the list.
Definition: SkeletonT_impl.hh:769
SkeletonT::Iterator::operator==
bool operator==(const Iterator &other) const
Compares the iterators.
Definition: SkeletonT_impl.hh:200
SkeletonT::childCount
size_t childCount(size_t _joint)
Returns the number of children of the given node.
Definition: SkeletonT_impl.hh:665
SkeletonT::clear
void clear()
Removes all joints from the skeleton.
Definition: SkeletonT_impl.hh:593
SkeletonT
Definition: JointT.hh:50
SkeletonT::referencePose_
Pose referencePose_
The skeletons reference pose.
Definition: SkeletonT.hh:223
PoseT
A general pose, used to store the frames of the animation.
Definition: PoseT.hh:58
SkeletonT::Iterator::operator!=
bool operator!=(const Iterator &other) const
Compares the iterators.
Definition: SkeletonT_impl.hh:187
SkeletonT::animationCount
size_t animationCount()
Returns the number of animations stored in this skeleton.
Definition: SkeletonT_impl.hh:965
SkeletonT::Iterator
Iterator class for the skeleton.
Definition: SkeletonT.hh:82
SkeletonT::end
Iterator end()
Compare an iterator with the return value of this method to test if it is done.
Definition: SkeletonT_impl.hh:719
SkeletonT::root
Joint * root()
Returns the root joint.
Definition: SkeletonT_impl.hh:618
SkeletonT::parent
int parent(size_t _joint)
Returns the parents id of the given node.
Definition: SkeletonT_impl.hh:652
SkeletonT::animationName
const std::string & animationName(size_t _index)
Returns the name of the animation with the given index.
Definition: SkeletonT_impl.hh:978
SkeletonT::cloneAnimation
AnimationHandle cloneAnimation(std::string _name, const AnimationHandle &_hAni)
Creates a new animation by cloning an existing one.
Definition: SkeletonT_impl.hh:803
SkeletonT::Iterator::~Iterator
~Iterator()
Destructor.
Definition: SkeletonT_impl.hh:113
SkeletonT::insertJoint
void insertJoint(typename SkeletonT< PointT >::Joint *_pChild, typename SkeletonT< PointT >::Joint *_pInsert)
insert a Joint given its future child joint
Definition: SkeletonT_impl.hh:1016
SkeletonT::Iterator::operator->
Joint * operator->() const
Returns a pointer to the current joint.
Definition: SkeletonT_impl.hh:229
AnimationHandle
A handle used to refer to an animation or to a specific frame in an animation.
Definition: AnimationHandle.hh:47
SkeletonT::~SkeletonT
~SkeletonT()
Destructor.
Definition: SkeletonT_impl.hh:470
SkeletonT::referencePose
Pose * referencePose()
Returns a pointer to the reference pose.
Definition: SkeletonT_impl.hh:755
SkeletonT::begin
Iterator begin()
Iterator over joints of the skeletal tree in TOP-DOWN order (from root to leafs)
Definition: SkeletonT_impl.hh:708
Properties
The properties storage class.
Definition: Properties.hh:92
SkeletonT::pose
Pose * pose(const AnimationHandle &_hAni)
Returns a pointer to the pose with the given animation handle.
Definition: SkeletonT_impl.hh:735
SkeletonT::replaceAnimationName
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
JointT
Represents a single joint in the skeleton.
Definition: JointT.hh:60
SkeletonT::animationsEnd
AnimationIterator animationsEnd()
Returns an iterator pointing behind the last animation.
Definition: SkeletonT_impl.hh:950
ACG::Matrix4x4T
Definition: Matrix4x4T.hh:92
SkeletonT::SkeletonT
SkeletonT()
Default constructor.
Definition: SkeletonT_impl.hh:371
SkeletonT::removeAnimation
void removeAnimation(std::string _name)
Removes an animation from the list.
Definition: SkeletonT_impl.hh:880
SkeletonT::animations_
std::vector< Animation * > animations_
Animations defined on the skeleton.
Definition: SkeletonT.hh:220
SkeletonT::clearAnimations
void clearAnimations()
Removes all animations.
Definition: SkeletonT_impl.hh:923
SkeletonT::animationHandle
AnimationHandle animationHandle(std::string _name)
Get an AnimationHandle to the animation with the given name.
Definition: SkeletonT_impl.hh:837
SkeletonT::jointCount
size_t jointCount()
Returns the number of joints.
Definition: SkeletonT_impl.hh:695
AnimationT
Stores a single animation.
Definition: AnimationT.hh:58
SkeletonT::child
size_t child(size_t _joint, size_t _child)
Returns the child with the given index.
Definition: SkeletonT_impl.hh:684
SkeletonT::animation
Animation * animation(std::string _name)
Returns a pointer to the animation to the given name.
Definition: SkeletonT_impl.hh:852
Properties.hh
The header for the properties, one file for all objects.
SkeletonT::AnimationIterator
Iterator class for the animations attached to a skeleton.
Definition: SkeletonT.hh:114
SkeletonT::Iterator::nextSibling
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_impl.hh:258
SkeletonT::joint
Joint * joint(const size_t &_index)
Returns the joint with the given index.
Definition: SkeletonT_impl.hh:636
SkeletonT::Iterator::operator=
Iterator & operator=(const Iterator &other)
Assignment Operator.
Definition: SkeletonT_impl.hh:126
SkeletonT::Iterator::operator++
Iterator & operator++()
Increase the iterator.
Definition: SkeletonT_impl.hh:141
SkeletonT::updateFromGlobal
void updateFromGlobal(size_t _idJoint)
update the structure when parent changes for a joint
Definition: SkeletonT_impl.hh:998
SkeletonT::Iterator::Iterator
Iterator()
Default constructor.
Definition: SkeletonT_impl.hh:73
SkeletonT::addJoint
void addJoint(typename SkeletonT< PointT >::Joint *_pParent, typename SkeletonT< PointT >::Joint *_pJoint)
Adds a joint as child of a given parent joint.
Definition: SkeletonT_impl.hh:488
SkeletonT::Iterator::operator*
Joint * operator*() const
Returns a pointer to the current joint.
Definition: SkeletonT_impl.hh:211
SkeletonT::joints_
std::vector< Joint * > joints_
Joints of the skeleton.
Definition: SkeletonT.hh:215
SkeletonT::removeJoint
void removeJoint(typename SkeletonT< PointT >::Joint *_pJoint)
Remove the given joint from the tree.
Definition: SkeletonT_impl.hh:532