assistive-rehab
skeleton.h
1 /******************************************************************************
2  * *
3  * Copyright (C) 2018 Fondazione Istituto Italiano di Tecnologia (IIT) *
4  * All Rights Reserved. *
5  * *
6  ******************************************************************************/
7 
45 #ifndef ASSISTIVE_REHAB_SKELETON_H
46 #define ASSISTIVE_REHAB_SKELETON_H
47 
48 #include <string>
49 #include <vector>
50 #include <unordered_map>
51 #include <utility>
52 #include <ostream>
53 #include <iostream>
54 #include <limits>
55 #include <yarp/os/Bottle.h>
56 #include <yarp/os/Property.h>
57 #include <yarp/sig/Vector.h>
58 #include <yarp/sig/Matrix.h>
59 
60 namespace assistive_rehab
61 {
62 
63 namespace KeyPointTag
64 {
65 extern const std::string shoulder_center;
66 extern const std::string head;
67 extern const std::string shoulder_left;
68 extern const std::string elbow_left;
69 extern const std::string hand_left;
70 extern const std::string shoulder_right;
71 extern const std::string elbow_right;
72 extern const std::string hand_right;
73 extern const std::string hip_center;
74 extern const std::string hip_left;
75 extern const std::string knee_left;
76 extern const std::string ankle_left;
77 extern const std::string foot_left;
78 extern const std::string hip_right;
79 extern const std::string knee_right;
80 extern const std::string ankle_right;
81 extern const std::string foot_right;
82 }
83 
84 namespace SkeletonType
85 {
86 extern const std::string Skeleton;
87 extern const std::string SkeletonStd;
88 }
89 
90 class Skeleton;
91 class SkeletonStd;
92 
96 class KeyPoint
97 {
98  friend class Skeleton;
99  friend class SkeletonStd;
100 
101  bool updated;
102  std::string tag;
103  yarp::sig::Vector point;
104  yarp::sig::Vector pixel;
105  std::vector<KeyPoint*> parent;
106  std::vector<KeyPoint*> child;
107 
108  void stale() { updated=false; }
109 
110 public:
114  KeyPoint();
115 
125  KeyPoint(const std::string &tag_, const yarp::sig::Vector &point_=yarp::sig::Vector(3,std::numeric_limits<double>::quiet_NaN()),
126  const yarp::sig::Vector &pixel_=yarp::sig::Vector(2,std::numeric_limits<double>::quiet_NaN()),const bool updated_=false);
127 
131  KeyPoint(const KeyPoint&) = delete;
132 
136  KeyPoint& operator=(const KeyPoint&) = delete;
137 
141  virtual ~KeyPoint() { }
142 
147  bool isUpdated() const { return updated; }
148 
153  const std::string& getTag() const { return tag; }
154 
161  const yarp::sig::Vector &getPoint() const { return point; }
162 
168  bool setPoint(const yarp::sig::Vector &point);
169 
177  bool setPoint(const yarp::sig::Vector &point,
178  const yarp::sig::Vector &pixel);
179 
186  const yarp::sig::Vector &getPixel() const { return pixel; }
187 
192  unsigned int getNumParent() const { return (unsigned int)parent.size(); }
193 
199  const KeyPoint* getParent(const unsigned int i) const;
200 
205  unsigned int getNumChild() const { return (unsigned int)child.size(); }
206 
212  const KeyPoint* getChild(const unsigned int i) const;
213 };
214 
220 class Skeleton
221 {
222 protected:
223  std::string type;
224  std::string tag;
225  std::vector<KeyPoint*> keypoints;
226  std::unordered_map<std::string,KeyPoint*> tag2key;
227  std::unordered_map<KeyPoint*,unsigned int> key2id;
229  yarp::sig::Matrix T;
230  yarp::sig::Vector coronal;
231  yarp::sig::Vector sagittal;
232  yarp::sig::Vector transverse;
234  yarp::os::Property helper_toproperty(KeyPoint *k) const;
235  void helper_fromproperty(yarp::os::Bottle *prop, KeyPoint *parent);
236  void helper_updatefromproperty(yarp::os::Bottle *prop);
237  void helper_normalize(KeyPoint* k, const std::vector<yarp::sig::Vector> &helperpoints, const double n);
238  void helper_scale(KeyPoint* k, const std::vector<yarp::sig::Vector> &helperpoints, const double s);
239  double helper_getmaxpath(KeyPoint* k, std::vector<bool> &visited) const;
240 
241 public:
245  Skeleton();
246 
250  Skeleton(const Skeleton&) = delete;
251 
255  Skeleton& operator=(const Skeleton&) = delete;
256 
260  virtual ~Skeleton();
261 
266  const std::string& getType() const { return type; }
267 
272  void setTag(const std::string &tag) { this->tag=tag; }
273 
278  const std::string& getTag() const { return tag; }
279 
284  bool setTransformation(const yarp::sig::Matrix &T);
285 
291  const yarp::sig::Matrix& getTransformation() const { return T; }
292 
297  bool setCoronal(const yarp::sig::Vector &coronal);
298 
303  bool setSagittal(const yarp::sig::Vector &sagittal);
304 
309  bool setTransverse(const yarp::sig::Vector &transverse);
310 
315  yarp::sig::Vector getCoronal() const;
316 
321  yarp::sig::Vector getSagittal() const;
322 
327  yarp::sig::Vector getTransverse() const;
328 
333  double getMaxPath() const;
334 
355  virtual yarp::os::Property toProperty();
356 
377  virtual void fromProperty(const yarp::os::Property &prop);
378 
383  unsigned int getNumKeyPoints() const { return (unsigned int)keypoints.size(); }
384 
390  int getNumFromKey(const std::string &tag) const;
391 
397  const KeyPoint* operator[](const std::string &tag) const;
398 
404  const KeyPoint* operator[](const unsigned int i) const;
405 
409  virtual void update();
410 
416  virtual void update(const std::vector<yarp::sig::Vector> &ordered);
417 
424  virtual void update(const std::vector<std::pair<std::string,yarp::sig::Vector>> &unordered);
425 
433  virtual void update_withpixels(const std::vector<std::pair<yarp::sig::Vector,yarp::sig::Vector>> &ordered);
434 
442  virtual void update_withpixels(const std::vector<std::pair<std::string,std::pair<yarp::sig::Vector,yarp::sig::Vector>>> &unordered);
443 
464  virtual void update(const yarp::os::Property &prop);
465 
470  virtual bool update_planes() = 0;
471 
478  virtual std::vector<yarp::sig::Vector> get_ordered() const;
479 
486  virtual std::vector<std::pair<std::string,yarp::sig::Vector>> get_unordered() const;
487 
494  virtual std::vector<std::pair<yarp::sig::Vector,yarp::sig::Vector>> get_ordered_withpixels() const;
495 
503  virtual std::vector<std::pair<std::string,std::pair<yarp::sig::Vector,yarp::sig::Vector>>> get_unordered_withpixels() const;
504 
509  void normalize(const double n=1.0);
510 
515  void scale(const double s);
516 
520  void print(std::ostream &os=std::cout) const;
521 };
522 
528 class SkeletonStd : public Skeleton
529 {
530 public:
534  SkeletonStd();
535 
540  bool update_planes() override;
541 };
542 
567 Skeleton *skeleton_factory(const yarp::os::Property &prop);
568 
569 }
570 
571 #endif
Basic class for single keypoint of a skeleton.
Definition: skeleton.h:97
const KeyPoint * getParent(const unsigned int i) const
Get a pointer to the parent of the keypoint specified by index.
Definition: skeleton.cpp:96
const yarp::sig::Vector & getPoint() const
Return a reference to the vector containing the keypoint's x,y,z camera coordinates.
Definition: skeleton.h:161
virtual ~KeyPoint()
Virtual destructor.
Definition: skeleton.h:141
bool setPoint(const yarp::sig::Vector &point)
Set keypoint's x,y,z camera coordinates to a desired value.
const KeyPoint * getChild(const unsigned int i) const
Get a pointer to the child of the keypoint specified by index.
Definition: skeleton.cpp:101
KeyPoint()
Default constructor.
Definition: skeleton.cpp:55
bool isUpdated() const
Return true if the keypoint has been updated.
Definition: skeleton.h:147
const std::string & getTag() const
Return a reference to the tag of the keypoint.
Definition: skeleton.h:153
unsigned int getNumParent() const
Retrieve the number of parents of the keypoint.
Definition: skeleton.h:192
KeyPoint(const std::string &tag_, const yarp::sig::Vector &point_=yarp::sig::Vector(3, std::numeric_limits< double >::quiet_NaN()), const yarp::sig::Vector &pixel_=yarp::sig::Vector(2, std::numeric_limits< double >::quiet_NaN()), const bool updated_=false)
Overloaded constructor.
bool setPoint(const yarp::sig::Vector &point, const yarp::sig::Vector &pixel)
Set keypoint's x,y,z camera coordinates as well as u,v, image coordinates to a desired value.
KeyPoint(const KeyPoint &)=delete
Deleted copy constructor.
unsigned int getNumChild() const
Retrieve the number of children of the keypoint.
Definition: skeleton.h:205
KeyPoint & operator=(const KeyPoint &)=delete
Deleted copy operator.
const yarp::sig::Vector & getPixel() const
Return a reference to the vector containing the keypoint's u,v image coordinates.
Definition: skeleton.h:186
Basic class for skeleton standard.
Definition: skeleton.h:529
bool update_planes() override
Update skeleton planes.
Definition: skeleton.cpp:767
SkeletonStd()
Default constructor.
Definition: skeleton.cpp:656
Abstract class for skeleton.
Definition: skeleton.h:221
const std::string & getTag() const
Return a reference to the tag of the skeleton.
Definition: skeleton.h:278
bool setTransverse(const yarp::sig::Vector &transverse)
Set a new transverse plane to the skeleton.
Definition: skeleton.cpp:329
virtual void update()
Update skeleton.
Definition: skeleton.cpp:454
virtual void update_withpixels(const std::vector< std::pair< yarp::sig::Vector, yarp::sig::Vector >> &ordered)
Update skeleton from ordered list.
void scale(const double s)
Rescale skeleton.
Definition: skeleton.cpp:624
virtual std::vector< std::pair< yarp::sig::Vector, yarp::sig::Vector > > get_ordered_withpixels() const
Retrieve the ordered list of keypoints.
Definition: skeleton.cpp:597
const yarp::sig::Matrix & getTransformation() const
Retrieve the transformation matrix of the skeleton.
Definition: skeleton.h:291
bool setTransformation(const yarp::sig::Matrix &T)
Set a new transformation matrix of the skeleton.
Definition: skeleton.cpp:296
yarp::sig::Matrix T
transformation matrix
Definition: skeleton.h:229
yarp::sig::Vector getTransverse() const
Retrieve the transverse plane of the skeleton.
Definition: skeleton.cpp:350
virtual void update_withpixels(const std::vector< std::pair< std::string, std::pair< yarp::sig::Vector, yarp::sig::Vector >>> &unordered)
Update skeleton from unordered list.
std::string tag
skeleton's tag
Definition: skeleton.h:224
const KeyPoint * operator[](const std::string &tag) const
Keypoint access.
std::unordered_map< KeyPoint *, unsigned int > key2id
map associating a pointer to a KeyPoint to an index
Definition: skeleton.h:227
const std::string & getType() const
Return a reference to the type of the skeleton.
Definition: skeleton.h:266
virtual std::vector< std::pair< std::string, std::pair< yarp::sig::Vector, yarp::sig::Vector > > > get_unordered_withpixels() const
Retrieve the unordered list of keypoints.
Definition: skeleton.cpp:605
void print(std::ostream &os=std::cout) const
Print skeleton information.
Definition: skeleton.cpp:635
Skeleton(const Skeleton &)=delete
Deleted copy constructor.
std::vector< KeyPoint * > keypoints
vector of pointer to KeyPoint
Definition: skeleton.h:225
std::string type
skeleton's type ("assistive_rehab::SkeletonStd")
Definition: skeleton.h:223
yarp::sig::Vector sagittal
vector containing the normal to the sagittal plane
Definition: skeleton.h:231
void normalize(const double n=1.0)
Normalize skeleton in order to have unitary/desired distance among keypoints.
Definition: skeleton.cpp:613
virtual std::vector< std::pair< std::string, yarp::sig::Vector > > get_unordered() const
Retrieve the unordered list of keypoints.
Definition: skeleton.cpp:589
yarp::sig::Vector getSagittal() const
Retrieve the sagittal plane of the skeleton.
Definition: skeleton.cpp:345
int getNumFromKey(const std::string &tag) const
Retrieve the index of the keypoint from its tag.
Definition: skeleton.cpp:431
virtual std::vector< yarp::sig::Vector > get_ordered() const
Retrieve the ordered list of keypoints.
Definition: skeleton.cpp:581
yarp::sig::Vector getCoronal() const
Retrieve the coronal plane of the skeleton.
Definition: skeleton.cpp:340
bool setCoronal(const yarp::sig::Vector &coronal)
Set a new coronal plane to the skeleton.
Definition: skeleton.cpp:307
yarp::sig::Vector transverse
vector containing the normal to the transverse plane
Definition: skeleton.h:232
virtual void update(const std::vector< std::pair< std::string, yarp::sig::Vector >> &unordered)
Update skeleton from unordered list.
Skeleton & operator=(const Skeleton &)=delete
Deleted copy operator.
unsigned int getNumKeyPoints() const
Retrieve the number of keypoints of the skeleton.
Definition: skeleton.h:383
virtual void fromProperty(const yarp::os::Property &prop)
Import the skeleton structure from its properties.
Definition: skeleton.cpp:400
double getMaxPath() const
Retrieve the skeleton's maximum path.
Definition: skeleton.cpp:355
yarp::sig::Vector coronal
vector containing the normal to the coronal plane
Definition: skeleton.h:230
virtual yarp::os::Property toProperty()
Export the skeleton structure as a property.
Definition: skeleton.cpp:366
std::unordered_map< std::string, KeyPoint * > tag2key
map associating a tag to a pointer to a KeyPoint
Definition: skeleton.h:226
virtual void update(const std::vector< yarp::sig::Vector > &ordered)
Update skeleton from ordered list.
void setTag(const std::string &tag)
Set the tag of the skeleton.
Definition: skeleton.h:272
virtual void update(const yarp::os::Property &prop)
Update skeleton from properties.
virtual ~Skeleton()
Virtual destructor.
Definition: skeleton.cpp:114
Skeleton()
Default constructor.
Definition: skeleton.cpp:106
virtual bool update_planes()=0
Update skeleton planes.
bool setSagittal(const yarp::sig::Vector &sagittal)
Set a new sagittal plane to the skeleton.
Definition: skeleton.cpp:318
Skeleton * skeleton_factory(const yarp::os::Property &prop)
Populate skeleton from a Property object.