iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Skeleton.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33 
34  This code modified and converted to C++11/STL/boost from the Motion capture player for ASF/AMC motion capture
35  sequences at http://www-bcf.usc.edu/~jbarbic/
36 
37  Revision 1 - Steve Lin (CMU), Jan 14, 2002
38  Revision 2 - Alla Safonova and Kiran Bhat (CMU), Jan 18, 2002
39  Revision 3 - Jernej Barbic and Yili Zhao (USC), Feb, 2012
40 
41  @endverbatim */
42 
43 #ifndef INCLUDE_NRT_GRAPHICS_MOCAP_SKELETON_H
44 #define INCLUDE_NRT_GRAPHICS_MOCAP_SKELETON_H
45 
47 #include <nrt/Eigen/Eigen.H>
48 #include <nrt/Core/Typing/Enum.H>
49 
50 #include <nrt/External/cereal/types/vector.hpp>
51 #include <nrt/External/cereal/types/list.hpp>
52 
53 /*! \defgroup mocap Motion capture/rendering related classes and functions
54 
55  The nrt mocap graphics primitives allow one to draw articulated skeletons and to animate them.
56  \ingroup graphics */
57 namespace nrt
58 {
59  namespace graphics
60  {
61  namespace mocap
62  {
63  // ######################################################################
64  //! A bone segment in a 3D graphics Skeleton
65  /*! This structure defines the property of each bone segment, including its connection to other bones,
66  DOF (degrees of freedom), relative orientation and distance to the outboard bone.
67  \ingroup mocap */
68  struct Bone
69  {
70  //! List of chlidren bones in the (loop-free) hierarchy
71  std::list<Bone> children;
72 
73  //! Unit vector describes the direction from local origin to the origin of the child bone
74  /*! \note Notice: stored in local coordinate system of the bone */
75  Eigen::Vector3d dir;
76 
77  //! Bone length
78  double length;
79 
80  //! Orientation of each bone's local coordinate system as specified in ASF file (axis field)
81  Eigen::Vector3d axis;
82 
83  //! aspect ratio of bone shape
84  double aspx, aspy;
85 
86  //! Possible types of degrees of freedom
87  enum doftype { dof_rx, dof_ry, dof_rz, dof_tx, dof_ty, dof_tz, dof_l };
88 
89  //! Specification of a degree of freedom (dof)
90  struct dofspec
91  {
92  doftype type; //! Type of degree of freedom
93  double value; //! Current value
94  double min; //! Minimum value of std::numeric_limits<double>::quiet_NaN() if unlimited
95  double max; //! Minimum value of std::numeric_limits<double>::quiet_NaN() if unlimited
96 
97  //! Serialization
98  template <class Archive> inline void serialize(Archive & ar)
99  { ar( type, value, min, max ); }
100  };
101 
102  //! Vector of types, numerical values, and limits for each of our existing dof
103  /*! The order in which the DOFs are listed here is what is assumed when we read in .AMC motion capture files
104  for this skeleton, and thus also what is assumed in Posture data. */
105  std::vector<dofspec> dof;
106 
107  //! Bone name
108  std::string name;
109 
110  //! Rotation matrix from the local coordinate of this bone to the local coordinate system of it's parent
111  Eigen::Affine3d rot_parent_current;
112 
113  //! Number of children in the sub-tree under this bone, including this bone
114  size_t size() const;
115 
116  //! Number of movable children in the sub-tree under this bone, including this bone if movable
117  size_t movsize() const;
118 
119  //! Serialization
120  template <class Archive> inline void serialize(Archive & ar)
121  { ar( children, dir, length, axis, aspx, aspy, dof, name, rot_parent_current ); }
122  };
123 
124  // ######################################################################
125  //! Enum to indicate either the starting point or the end point of a bone, used by Skeleton::getTransform()
126  /*! \class BoneEndType
127  \ingroup mocap */
128  NRT_DEFINE_ENUM_CLASS(BoneEndType, (Start) (End));
129 
130  // ######################################################################
131  //! An articulated skeleton as a hierarchy of Bone objects
132  /*! \ingroup mocap */
133  class Skeleton
134  {
135  public:
136  //! Default constructor. Only action is to name our root bone "root"
137  Skeleton();
138 
139  //! Construct by loading a .asf file
140  Skeleton(std::string const & asf_filename);
141 
142  //! Set the skeleton's pose based on the given posture
143  void setPosture(Posture const & posture);
144 
145  //! Number of bones in the skeleton
146  size_t size() const;
147 
148  //! Number of movable bones in the skeleton
149  size_t movsize() const;
150 
151  //! Find a bone by name, will throw if not found
152  Bone & bone(std::string const & name);
153 
154  //! Get the compounded transform from root to a given bone
155  /*! The returned transform is either for the start or the end of the bone. Note how the start of a child bone
156  is different from the end of its parent in that the transform for the start of the child bone already
157  includes re-orienting along that child bone, while the end of a parent would still be oriented according
158  to that parent. */
159  Eigen::Affine3d getTransform(std::string const & bonename, BoneEndType endtype) const;
160 
161  //! Our root bone. Its name is fixed to "root"
163 
164  //! Serialization
165  template <class Archive> void serialize(Archive & ar)
166  { ar( root ); }
167  };
168 
169  } // namespace mocap
170  } // namespace graphics
171 } // namespace nrt
172 
173 #endif // INCLUDE_NRT_GRAPHICS_MOCAP_SKELETON_H
174