Atoms Crowd  7.0.0
Skeleton.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <AtomsCore/Globals.h>
11 #include <AtomsCore/AtomsMath.h>
12 #include <AtomsCore/Joint.h>
13 #include <AtomsCore/Serialiser.h>
14 #include <AtomsCore/Metadata/Metadata.h>
15 #include <AtomsCore/Metadata/MapMetadata.h>
16 #include <string>
17 #include <unordered_map>
18 #include <vector>
19 #include <algorithm>
20 
21 namespace AtomsCore
22 {
23 
24  struct SkeletonLod
25  {
26  std::vector<short> activeJoints;
27  double screenSize;
28  bool disableIk;
29  bool disableFootLock;
30  };
31 
32  struct FootStruct
33  {
34  FootStruct(unsigned short footIK, unsigned short footRoot, unsigned short footTip) :
35  footIK(footIK), footTip(footTip), footRoot(footRoot), pelvis(0), numJoints(3), poleVector(0.0, 0.0, 0.0), hasPoleVector(false){};
36 
37  unsigned short footIK, footTip, footRoot, pelvis, numJoints;
38  std::vector<unsigned short> midJoints;
39  std::vector<unsigned short> legJoints;
40  AtomsCore::Vector3 poleVector;
41  bool hasPoleVector;
42  };
43 
44  struct PelvisChain
45  {
46  unsigned int parent;
47  unsigned int child;
48  std::vector<unsigned int> chain;
49  };
50 
51  class Skeleton;
52 
53  typedef Skeleton* SkeletonPtr;
54 
55  typedef const Skeleton* SkeletonCPtr;
56 
58 
67  class ATOMSCORE_EXPORT Skeleton
68  {
69 
70  public:
72  typedef std::map<unsigned short, MapMetadata> JointMetadataMap;
73 
75  typedef std::vector<FootStruct> FeetVector;
76 
78 
82  Skeleton(unsigned short numberOfJoints = 1);
83 
85  Skeleton(const Skeleton& rhs);
86 
89 
91  Skeleton& operator=(const Skeleton& rhs);
92 
94 
97  inline unsigned short numJoints() const;
98 
100 
103  void setNumJoints(unsigned int numJoints);
104 
106 
110  inline Joint& operator[](unsigned short index);
111 
113 
117  inline const Joint& operator[](unsigned short index) const;
118 
120 
125  Joint& joint(unsigned short index);
126 
128 
133  const Joint& joint(unsigned short index) const;
134 
136 
140  inline JointCPtr root() const;
141 
143 
147  inline JointPtr root();
148 
150 
153  inline void setRoot(unsigned short id);
154 
156 
159  inline unsigned short numFeet() const;
160 
162  inline void clearFeet();
163 
165 
171  inline void addFoot(unsigned short footIK, unsigned short footRoot, unsigned short footTip);
172 
174 
178  inline JointPtr footIK(unsigned short id);
179 
181 
186  inline JointCPtr footIK(unsigned short id) const;
187 
189 
192  bool isFootIK(unsigned short id) const;
193 
195 
200  inline JointPtr footRoot(unsigned short id);
201 
203 
208  inline JointCPtr footRoot(unsigned short id) const;
209 
211 
214  bool isFootRoot(unsigned short id) const;
215 
217 
222  inline JointPtr footTip(unsigned short id);
223 
225 
230  inline JointCPtr footTip(unsigned short id) const;
231 
233 
236  bool isFootTip(unsigned short id) const;
237 
239  inline bool footHasPoleVector(unsigned short id) const;
240 
242  inline AtomsCore::Vector3 footPoleVector(unsigned short id) const;
243 
245  inline void setFootPoleVector(unsigned short id, const AtomsCore::Vector3& value);
246 
248 
253  inline unsigned short footPelvis(unsigned short id) const;
254 
256 
261  inline const std::vector<unsigned short>& midJoints(unsigned short id) const;
262 
264 
269  inline const std::vector<unsigned short>& legJoints(unsigned short id) const;
270 
272 
275  inline unsigned short numPelvises() const;
276 
278  inline void clearPelvises();
279 
281 
284  inline void addPelvis(unsigned short pelvisJoint);
285 
287 
290  inline JointPtr pelvis(unsigned short id);
291 
293 
296  inline JointCPtr pelvis(unsigned short id) const;
297 
299 
302  bool isPelvis(unsigned short id) const;
303 
305 
309  inline unsigned int numPelvisChains(unsigned int id) const;
310 
312 
317  PelvisChain* pelvisChain(unsigned int id, unsigned int chain);
318 
320 
325  const PelvisChain* pelvisChain(unsigned int id, unsigned int chain) const;
326 
328 
331  inline const std::vector<unsigned short>& detachedJoints() const;
332 
334 
337  void buildIkData();
338 
341 
344 
346 
350  inline int jointId(const std::string& name) const;
351 
353  inline void clear();
354 
356  size_t memSize() const;
357 
359  std::vector<unsigned short> jointMetadataIds() const;
360 
362 
366  bool jointHasMetadata(unsigned short index);
367 
369 
373  bool jointHasMetadata(unsigned short index) const;
374 
376 
380  inline MapMetadata& jointMetadata(unsigned short index);
381 
383 
387  const MapMetadata& jointMetadata(unsigned short index) const;
388 
390 
394  inline void setJointMetadata(unsigned short index, const MapMetadata& data);
395 
397 
402  inline void addJointMetadata(unsigned short index, const std::string& key, Metadata *data);
403 
405 
410  inline void addJointMetadata(unsigned short index, const std::string& key, const AtomsPtr<Metadata> data);
411 
413 
416  void clearJointMetadata(unsigned short index);
417 
419  inline void clearJointsMetadata();
420 
422  void hash(MurmurHash3& hash) const;
423 
425  inline std::vector<SkeletonLod>& lods();
426 
428  inline const std::vector<SkeletonLod>& lods() const;
429 
431  /*
432  Replace . with _ inside the joint names
433  */
435 
436  private:
437 
439  JointPtr m_joints;
440 
442  FeetVector m_feet;
443 
445  std::unordered_map<std::string, int> m_jointNameIdMap;
446 
447  //TODO: This should be replaced with a MapMetadata when MapMetadata will support integers as keys
449  JointMetadataMap m_jointMetadata;
450 
452  std::vector<unsigned short> m_pelvises;
453 
455  std::vector<std::vector<PelvisChain>> m_pelvisChains;
456 
458  std::vector<unsigned short> m_detachedJoints;
459 
461  std::vector<SkeletonLod> m_lods;
462 
464  unsigned short m_root;
465 
467  unsigned short m_numJoints;
468 
469  };
470 }
471 
473 ATOMSCORE_EXPORT std::ostream& operator<<(std::ostream& os, const AtomsCore::Skeleton& skeleton);
474 
475 namespace AtomsCore
476 {
478  ATOMSCORE_EXPORT AtomsCore::Archive& operator<<(AtomsCore::Archive& os, const AtomsCore::Skeleton& skeleton);
479 
481  ATOMSCORE_EXPORT AtomsCore::Archive& operator >> (AtomsCore::Archive& is, AtomsCore::Skeleton& skeleton);
482 }
483 
484 #include "Skeleton.impl.h"
Archive class.
Definition: Serialiser.h:29
Joint class.
Definition: Joint.h:30
MapMetadata class.
Definition: MapMetadata.h:24
Base Metadata class.
Definition: Metadata.h:24
Definition: Hash.h:23
Skeleton class.
Definition: Skeleton.h:68
std::map< unsigned short, MapMetadata > JointMetadataMap
Joint metadata map.
Definition: Skeleton.h:72
Skeleton(const Skeleton &rhs)
Copy constructor.
void buildDetachedJointsList()
Build detached joints list.
void setNumJoints(unsigned int numJoints)
Sets the number of joints.
bool jointHasMetadata(unsigned short index)
Check if a joint has some metadata.
size_t memSize() const
Get the memory size.
bool isPelvis(unsigned short id) const
Check if a joint is a pelvis.
bool jointHasMetadata(unsigned short index) const
Check if a joint has some metadata.
bool isFootTip(unsigned short id) const
Check if a joint is a foot tip.
void buildIkData()
Build Ik data.
Skeleton(unsigned short numberOfJoints=1)
Constructor.
void buildJointNameMap()
Build Joint name -> id map.
PelvisChain * pelvisChain(unsigned int id, unsigned int chain)
Get a palvis chain.
void clearJointMetadata(unsigned short index)
Clears joint metadata.
void hash(MurmurHash3 &hash) const
Hash.
const MapMetadata & jointMetadata(unsigned short index) const
Gets joint metadata.
void sanitizeJointNames()
Sanitize joint names.
std::vector< unsigned short > jointMetadataIds() const
Gets the ids of the joints having some metadatas.
~Skeleton()
Destructor.
std::vector< FootStruct > FeetVector
Vector of pair foot-foot root.
Definition: Skeleton.h:75
bool isFootIK(unsigned short id) const
Check if a joint is a foot ik.
const Joint & joint(unsigned short index) const
Gets the joint.
const PelvisChain * pelvisChain(unsigned int id, unsigned int chain) const
Get a palvis chain.
Skeleton & operator=(const Skeleton &rhs)
Assign operator.
Joint & joint(unsigned short index)
Gets the joint.
bool isFootRoot(unsigned short id) const
Check if a joint is a foot root.
AtomsCore namespace.
Definition: Agent.h:344
const Skeleton * SkeletonCPtr
Skeleton const pointer.
Definition: Skeleton.h:55
AtomsMath::Vector3 Vector3
Vector3 class.
Definition: AtomsMath.h:57
Skeleton * SkeletonPtr
Skeleton pointer.
Definition: Skeleton.h:51
Definition: Skeleton.h:33
Definition: Skeleton.h:45
Definition: Skeleton.h:25