Skeleton.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GAZEBO_COMMON_SKELETON_HH_
18 #define GAZEBO_COMMON_SKELETON_HH_
19 
20 #include <vector>
21 #include <string>
22 #include <map>
23 #include <utility>
24 
25 #include <ignition/math/Matrix4.hh>
26 
28 #include "gazebo/util/system.hh"
29 
30 namespace gazebo
31 {
32  namespace common
33  {
34  class SkeletonNode;
35  class NodeTransform;
36  class SkeletonAnimation;
37 
38  typedef std::map<unsigned int, SkeletonNode*> NodeMap;
39  typedef std::map<unsigned int, SkeletonNode*>::iterator NodeMapIter;
40 
41  typedef std::map<double, std::vector<NodeTransform> > RawNodeAnim;
42  typedef std::map<std::string, RawNodeAnim> RawSkeletonAnim;
43 
44  typedef std::vector<std::vector<std::pair<std::string, double> > >
46 
49 
52  class GZ_COMMON_VISIBLE Skeleton
53  {
55  public: Skeleton();
56 
59  public: explicit Skeleton(SkeletonNode *_root);
60 
62  public: virtual ~Skeleton();
63 
66  public: void SetRootNode(SkeletonNode* _node);
67 
71 
74  public: const SkeletonNode* GetRootNode() const;
75 
79  public: SkeletonNode* GetNodeByName(std::string _name);
80 
84  public: SkeletonNode* GetNodeById(std::string _id);
85 
89  public: SkeletonNode* GetNodeByHandle(unsigned int _handle);
90 
93  public: unsigned int GetNumNodes();
94 
97  public: unsigned int GetNumJoints();
98 
101  public: void Scale(double _scale);
102 
105  public: void SetBindShapeTransform(
106  const ignition::math::Matrix4d &_trans);
107 
110  public: ignition::math::Matrix4d BindShapeTransform();
111 
113  public: void PrintTransforms();
114 
116  public: NodeMap GetNodes();
117 
121  public: void SetNumVertAttached(unsigned int _vertices);
122 
127  public: void AddVertNodeWeight(unsigned int _vertex, std::string _node,
128  double _weight);
129 
133  public: unsigned int GetNumVertNodeWeights(unsigned int _vertex);
134 
139  public: std::pair<std::string, double> GetVertNodeWeight(unsigned int _v,
140  unsigned int _i);
141 
144  public: unsigned int GetNumAnimations();
145 
149  public: SkeletonAnimation* GetAnimation(const unsigned int _i);
150 
154  public: void AddAnimation(SkeletonAnimation *_anim);
155 
158  protected: void BuildNodeMap();
159 
161  protected: SkeletonNode *root;
162 
164  protected: NodeMap nodes;
165 
167  protected: ignition::math::Matrix4d bindShapeTransform =
168  ignition::math::Matrix4d::Identity;
169 
171  protected: RawNodeWeights rawNW;
172 
174  protected: std::vector<SkeletonAnimation*> anims;
175  };
176 
179  class GZ_COMMON_VISIBLE SkeletonNode
180  {
182  public: enum SkeletonNodeType {NODE, JOINT};
183 
186  public: explicit SkeletonNode(SkeletonNode* _parent);
187 
193  public: SkeletonNode(SkeletonNode* _parent, std::string _name,
194  std::string _id, SkeletonNodeType _type = JOINT);
195 
197  public: virtual ~SkeletonNode();
198 
201  public: void SetName(std::string _name);
202 
207  public: std::string GetName();
208 
214  public: std::string Name() const;
215 
218  public: void SetId(std::string _id);
219 
222  public: std::string GetId();
223 
226  public: void SetType(SkeletonNodeType _type);
227 
230  public: bool IsJoint();
231 
236  public: void SetTransform(const ignition::math::Matrix4d &_trans,
237  bool _updateChildren = true);
238 
243  public: void SetModelTransform(const ignition::math::Matrix4d &_trans,
244  bool _updateChildren = true);
245 
247  public: void UpdateChildrenTransforms();
248 
251  public: void SetInitialTransform(const ignition::math::Matrix4d &_tras);
252 
256  public: void Reset(bool _resetChildren);
257 
260  public: ignition::math::Matrix4d Transform();
261 
264  public: void SetParent(SkeletonNode* _parent);
265 
269 
272  public: bool IsRootNode();
273 
276  public: void AddChild(SkeletonNode* _child);
277 
280  public: unsigned int GetChildCount();
281 
285  public: SkeletonNode* GetChild(unsigned int _index);
286 
290  public: SkeletonNode* GetChildByName(std::string _name);
291 
295  public: SkeletonNode* GetChildById(std::string _id);
296 
299  public: void SetHandle(unsigned int _h);
300 
303  public: unsigned int GetHandle();
304 
308  const ignition::math::Matrix4d &_invBM);
309 
312  public: ignition::math::Matrix4d InverseBindTransform();
313 
318  public: bool HasInvBindTransform();
319 
322  public: ignition::math::Matrix4d ModelTransform() const;
323 
326  public: std::vector<NodeTransform> GetRawTransforms();
327 
330  public: unsigned int GetNumRawTrans();
331 
335  public: NodeTransform GetRawTransform(unsigned int _i);
336 
339  public: void AddRawTransform(NodeTransform _t);
340 
343  public: std::vector<NodeTransform> GetTransforms();
344 
346  protected: std::string name;
347 
349  protected: std::string id;
350 
352  protected: SkeletonNodeType type;
353 
355  protected: ignition::math::Matrix4d transform;
356 
358  protected: ignition::math::Matrix4d initialTransform;
359 
361  protected: ignition::math::Matrix4d modelTransform;
362 
364  protected: ignition::math::Matrix4d invBindTransform;
365 
367  protected: SkeletonNode *parent;
368 
370  protected: std::vector<SkeletonNode*> children;
371 
373  protected: unsigned int handle;
374 
376  protected: std::vector<NodeTransform> rawTransforms;
377  };
378 
381  class GZ_COMMON_VISIBLE NodeTransform
382  {
384  public: enum TransformType {TRANSLATE, ROTATE, SCALE, MATRIX};
385 
388  public: explicit NodeTransform(TransformType _type = MATRIX);
389 
394  public: NodeTransform(const ignition::math::Matrix4d &_mat,
395  const std::string &_sid = "_default_",
396  TransformType _type = MATRIX);
397 
399  public: ~NodeTransform();
400 
403  public: void Set(const ignition::math::Matrix4d &_mat);
404 
407  public: void SetType(TransformType _type);
408 
411  public: void SetSID(std::string _sid);
412 
415  public: ignition::math::Matrix4d GetTransform() const;
416 
420 
423  public: std::string GetSID();
424 
428  public: void SetComponent(unsigned int _idx, double _value);
429 
432  public: void SetSourceValues(const ignition::math::Matrix4d &_mat);
433 
436  public: void SetSourceValues(const ignition::math::Vector3d &_vec);
437 
441  public: void SetSourceValues(const ignition::math::Vector3d &_axis,
442  const double _angle);
443 
445  public: void RecalculateMatrix();
446 
448  public: void PrintSource();
449 
452  public: ignition::math::Matrix4d operator()();
453 
457  public: ignition::math::Matrix4d operator*(NodeTransform _t);
458 
462  public: ignition::math::Matrix4d operator*(
463  const ignition::math::Matrix4d &_m);
464 
466  protected: std::string sid;
467 
469  protected: TransformType type;
470 
472  protected: ignition::math::Matrix4d transform;
473 
475  protected: std::vector<double> source;
476  };
478  }
479 }
480 #endif
481 
common
Definition: FuelModelDatabase.hh:42
\clas NodeTransform Skeleton.hh common/common.hh
Definition: Skeleton.hh:382
void RecalculateMatrix()
Sets the transform matrix from the source according to the type.
std::string GetSID()
Returns thr SID.
void SetType(TransformType _type)
Set transform type.
TransformType type
transform type
Definition: Skeleton.hh:469
ignition::math::Matrix4d operator()()
Matrix cast operator.
ignition::math::Matrix4d transform
transform
Definition: Skeleton.hh:472
void SetSourceValues(const ignition::math::Vector3d &_axis, const double _angle)
Sets source matrix values from roation.
TransformType GetType()
Returns the transformation type.
void Set(const ignition::math::Matrix4d &_mat)
Assign a transformation.
NodeTransform(TransformType _type=MATRIX)
Constructor.
void PrintSource()
Prints the transform matrix to std::err stream.
ignition::math::Matrix4d GetTransform() const
Returns the transformation matrix.
std::vector< double > source
source data values (can be a matrix, a position or rotation)
Definition: Skeleton.hh:475
void SetSourceValues(const ignition::math::Matrix4d &_mat)
Set source data values param[in] _mat the values.
ignition::math::Matrix4d operator*(NodeTransform _t)
Node transform multiplication operator.
TransformType
Enumeration of the transform types.
Definition: Skeleton.hh:384
void SetSID(std::string _sid)
Set the SID.
~NodeTransform()
Destructor. It does nothing.
ignition::math::Matrix4d operator*(const ignition::math::Matrix4d &_m)
Matrix multiplication operator.
void SetSourceValues(const ignition::math::Vector3d &_vec)
Set source data values.
NodeTransform(const ignition::math::Matrix4d &_mat, const std::string &_sid="_default_", TransformType _type=MATRIX)
Constructor.
std::string sid
the sid
Definition: Skeleton.hh:466
void SetComponent(unsigned int _idx, double _value)
Set a transformation matrix component value.
Skeleton animation.
Definition: SkeletonAnimation.hh:127
A skeleton node.
Definition: Skeleton.hh:180
void Reset(bool _resetChildren)
Reset the transformation to the initial transformation.
std::vector< NodeTransform > rawTransforms
the raw transformation
Definition: Skeleton.hh:376
void AddRawTransform(NodeTransform _t)
Add a raw transform.
void SetName(std::string _name)
Change the name.
std::vector< SkeletonNode * > children
the children nodes
Definition: Skeleton.hh:370
ignition::math::Matrix4d modelTransform
the model transformation
Definition: Skeleton.hh:361
void SetTransform(const ignition::math::Matrix4d &_trans, bool _updateChildren=true)
Set a transformation.
SkeletonNodeType type
the type fo node
Definition: Skeleton.hh:352
SkeletonNode * parent
the parent node
Definition: Skeleton.hh:367
bool IsJoint()
Is a joint query.
bool IsRootNode()
Queries wether a node has no parent parent.
ignition::math::Matrix4d transform
the transform
Definition: Skeleton.hh:355
std::string Name() const
Returns the name.
void SetType(SkeletonNodeType _type)
Change the skeleton node type.
void SetModelTransform(const ignition::math::Matrix4d &_trans, bool _updateChildren=true)
Set the model transformation.
void SetId(std::string _id)
Change the id string.
SkeletonNode * GetChildById(std::string _id)
Get child by string id.
void UpdateChildrenTransforms()
Apply model transformations in order for each node in the tree.
void SetInitialTransform(const ignition::math::Matrix4d &_tras)
Sets the initial transformation.
SkeletonNode(SkeletonNode *_parent)
Constructor.
std::vector< NodeTransform > GetRawTransforms()
Retrieve the raw transformations.
ignition::math::Matrix4d ModelTransform() const
Retrieve the model transform.
unsigned int handle
handle index number
Definition: Skeleton.hh:373
void SetHandle(unsigned int _h)
Assign a handle number.
std::string GetName()
Returns the name.
virtual ~SkeletonNode()
Destructor.
unsigned int GetNumRawTrans()
Return the raw transformations count.
std::string name
the name of the skeletal node
Definition: Skeleton.hh:346
ignition::math::Matrix4d Transform()
Get transform relative to parent.
ignition::math::Matrix4d initialTransform
the initial transformation
Definition: Skeleton.hh:358
unsigned int GetHandle()
Get the handle index.
SkeletonNode * GetChildByName(std::string _name)
Get child by name.
SkeletonNode * GetParent()
Returns the parent node.
NodeTransform GetRawTransform(unsigned int _i)
Find a raw transformation.
ignition::math::Matrix4d invBindTransform
the inverse of the bind pose skeletal transform
Definition: Skeleton.hh:364
unsigned int GetChildCount()
Returns the children count.
SkeletonNodeType
enumeration of node types
Definition: Skeleton.hh:182
void AddChild(SkeletonNode *_child)
Add a new child.
SkeletonNode(SkeletonNode *_parent, std::string _name, std::string _id, SkeletonNodeType _type=JOINT)
Constructor.
void SetParent(SkeletonNode *_parent)
Set the parent node.
SkeletonNode * GetChild(unsigned int _index)
Find a child by index.
void SetInverseBindTransform(const ignition::math::Matrix4d &_invBM)
Assign the inverse of the bind pose skeletal transform.
std::vector< NodeTransform > GetTransforms()
Returns a copy of the array of transformations.
std::string GetId()
Returns the index.
bool HasInvBindTransform()
returns true if the node has inv bind transform.
std::string id
a string identifier
Definition: Skeleton.hh:349
ignition::math::Matrix4d InverseBindTransform()
Retrieve the inverse of the bind pose skeletal transform.
A skeleton.
Definition: Skeleton.hh:53
void SetRootNode(SkeletonNode *_node)
Change the root node.
unsigned int GetNumNodes()
Returns the node count.
RawNodeWeights rawNW
the node weight table
Definition: Skeleton.hh:171
unsigned int GetNumVertNodeWeights(unsigned int _vertex)
Returns the number of bone weights for a vertex.
unsigned int GetNumJoints()
Returns the number of joints.
void AddAnimation(SkeletonAnimation *_anim)
Add an animation.
SkeletonNode * GetNodeByHandle(unsigned int _handle)
Find or create node with handle.
virtual ~Skeleton()
Destructor.
std::pair< std::string, double > GetVertNodeWeight(unsigned int _v, unsigned int _i)
Weight of a bone for a vertex.
const SkeletonNode * GetRootNode() const
Return the root.
SkeletonNode * GetNodeById(std::string _id)
Find node by index.
ignition::math::Matrix4d BindShapeTransform()
Return bind pose skeletal transform.
void BuildNodeMap()
Initializes the hande numbers for each node in the map using breadth first traversal.
Skeleton(SkeletonNode *_root)
Constructor.
unsigned int GetNumAnimations()
Returns the number of animations.
SkeletonAnimation * GetAnimation(const unsigned int _i)
Find animation.
void SetBindShapeTransform(const ignition::math::Matrix4d &_trans)
Set the bind pose skeletal transform.
NodeMap GetNodes()
Get a copy or the node dictionary.
void Scale(double _scale)
Scale all nodes, transforms and animation data.
void PrintTransforms()
Outputs the transforms to std::err stream.
SkeletonNode * GetNodeByName(std::string _name)
Find a node.
void SetNumVertAttached(unsigned int _vertices)
Resizes the raw node weight array, this also clears all the node weights assigned.
void AddVertNodeWeight(unsigned int _vertex, std::string _node, double _weight)
Add a new weight to a node (bone)
SkeletonNode * GetRootNode()
Return the root.
std::vector< SkeletonAnimation * > anims
the array of animations
Definition: Skeleton.hh:174
SkeletonNode * root
the root node
Definition: Skeleton.hh:161
NodeMap nodes
The dictionary of nodes, indexed by name.
Definition: Skeleton.hh:164
std::map< unsigned int, SkeletonNode * > NodeMap
Definition: Skeleton.hh:36
std::map< double, std::vector< NodeTransform > > RawNodeAnim
Definition: Skeleton.hh:41
std::vector< std::vector< std::pair< std::string, double > > > RawNodeWeights
Definition: Skeleton.hh:45
std::map< unsigned int, SkeletonNode * >::iterator NodeMapIter
Definition: Skeleton.hh:39
std::map< std::string, RawNodeAnim > RawSkeletonAnim
Definition: Skeleton.hh:42
Forward declarations for the common classes.
Definition: Animation.hh:27