My Project
BVHSerializer.h
1 #pragma once
2 
3 #include <vector>
4 
5 #define MAX_NODE_NUM 100
6 #define MAX_CHILDBONE 6
7 namespace ParaEngine
8 {
9  using namespace std;
10 
11  class Quaternion;
12  class CParaXModel;
13 
24  {
25  public:
27  ~CBVHSerializer(void);
28 
29  enum BVH_NODES
30  {
31  Hips = 0,//
32 
33  LeftHip,//lfemur
34  LeftKnee,// ltibia
35  LeftAnkle,// lfoot
36  LeftToe,// ltoe
37 
38  RightHip,// rfemur
39  RightKnee,// rtibia
40  RightAnkle,// rfoot
41  RightToe,// rtoe
42 
43  Chest,// upperback
44  Chest2,// thorax
45  Chest3,// spine_bone3
46  Chest4,// spine_bone4
47  Chest5,// spine_bone5
48  Chest6,// spine_bone6
49  Chest7,// spine_bone7
50 
51  LeftCollar,// lshoulderjoint
52  LeftShoulder,// lhumerus
53  LeftElbow,// lradius
54  LeftWrist,// lwrist
55 
56 
57  LeftFinger0,// lThumb_0Bone
58  LeftFinger01,// lThumb_1Bone
59  LeftFinger02,// lThumb_2Bone
60 
61  LeftFinger1,// lfinger1_0Bone
62  LeftFinger11,// lfinger1_1Bone
63  LeftFinger12,// lfinger1_2Bone
64  //.
65  //.
66  //.
67  LeftFinger4,// lfinger4_0Bone
68  LeftFinger41,// lfinger4_1Bone
69  LeftFinger42,// lfinger4_2Bone
70 
71 
72  RightCollar,// rshoulderjoint
73  RightShoulder,// rhumerus
74  RightElbow,// rradius
75  RightWrist,// rwrist
76 
77  RightFinger0,// rThumb_0Bone
78  RightFinger01,// rThumb_1Bone
79  RightFinger02,// rThumb_2Bone
80 
81  RightFinger1,// rfinger1_0Bone
82  RightFinger11,// rfinger1_1Bone
83  RightFinger12,// rfinger1_2Bone
84  //.
85  //.
86  //.
87  RightFinger4,// rfinger4_0Bone
88  RightFinger41,// rfinger4_1Bone
89  RightFinger42,// rfinger4_2Bone
90 
91  Head,// head
92  Neck,// neck
93 
94  MAX_BVH_NODE,
95  };
96 
98  {
100  string m_nodeName;
103  };
104 
105  struct UniBone{
109  vector<int> m_childBones;
124  public:
125  inline int GetChildCount(){
126  return (int)m_childBones.size();
127  };
128  inline bool IsUnKnownBone(){return m_sMarkerName.empty();};
129  inline float GetBoneLength(){ return m_fBoneLength;}
131  inline const Vector3& GetOffset(){ return m_vOffsetToParent;}
132  };
133 
135  {
136  BoneChildCountCompare(vector<UniBone>& bones)
137  {
138  m_pBones = &bones;
139  }
140 
141  bool operator()(const int& _Left, const int& _Right) const
142  { // apply operator> to operands
143  return (((*m_pBones)[_Left].m_nSubLevels > (*m_pBones)[_Right].m_nSubLevels) ||
144  ((((*m_pBones)[_Left].m_nSubLevels == (*m_pBones)[_Right].m_nSubLevels)) && (*m_pBones)[_Left].m_fBoneLength > (*m_pBones)[_Right].m_fBoneLength) );
145  }
146  private:
147  vector<UniBone>* m_pBones;
148  };
149 
151  {
152  BoneMarkerCompare(vector<UniBone>& bones)
153  {
154  m_pBones = &bones;
155  }
156 
157  bool operator()(const int& _Left, const int& _Right) const
158  { // apply operator> to operands
159 
160  if((*m_pBones)[_Right].m_sMarkerName.empty())
161  return true;
162  else if((*m_pBones)[_Left].m_sMarkerName.empty())
163  return true;
164  else if((*m_pBones)[_Left].m_sMarkerName[0] == 'L' && (*m_pBones)[_Right].m_sMarkerName[0] == 'R')
165  return true;
166  else if((*m_pBones)[_Right].m_sMarkerName[0] == 'C' || (*m_pBones)[_Right].m_sMarkerName[0] == 'N')
167  return true;
168  else
169  return false;
170  }
171  private:
172  vector<UniBone>* m_pBones;
173  };
174 
177  C7=0,CLAV,LANK,LBHD, LBWT, LELB, LFHD, LFIN, LFWT, LKNE, LMT5, LSHO, LTOE, LWRE, LWRI, RANK, RBHD, RBWT, RELB, RFHD, RFIN, RFWT, RKNE, RMT5, RSHO, RTOE, RWRE, RWRI, STRN, T10,
178  };
179  enum UniversalNodeEnum{
180  UN_Root,
181  UN_Root1,
182  UN_Root2,
183  UN_Spine0,
184  UN_Left_Thigh,
185  UN_Left_Calf,
186  UN_Right_Thigh,
187  UN_Right_Calf,
188  UN_Spine1,UN_Spine2,UN_Spine3,UN_Spine4,
189  UN_Neck,
190  UN_Head,
191  UN_Left_Clavicle,
192  UN_Left_UpperArm,
193  UN_Right_Clavicle,
194  UN_Right_UpperArm,
195  };
196 
197  public:
199  void SaveCSM(const string& sFileName);
200 
207  bool InitHierachy(CParaXModel& xmesh);
208 
229  bool SaveBVH(const string& sFileName, const string& sMarkerNameFile);
230 
231  private:
233  vector <MotionCaptureNode> m_nodes;
234 
236  MotionCaptureNode m_UniversalNodes[MAX_NODE_NUM];
237 
239  vector<UniBone> m_bones;
240  int m_BVHnodes[MAX_BVH_NODE];
242  CParaXModel* m_pXMesh;
243 
245  int m_nRootBoneIndex;
246 
248  int m_nFPS;
249 
251  bool m_bRotateY;
252 
254  bool m_bEscapeUnknownBones;
255 
257  bool m_bEscapeUnmarkedBones;
258 
260  bool m_bExportBVHPosition;
261 
264  bool m_bSwapChestHip;
265 
266  int m_nHipBoneIndex;
267  int m_nChestBoneIndex;
268 
272  bool BuildInternalBones();
273 
275  bool BuildBipedUniversalBones();
276 
278  inline int GetBoneIndex(int nIndex){return m_UniversalNodes[(UniversalNodeEnum)nIndex].m_nBoneIndex;};
279 
281  int GetBoneCount(){return (int)m_nodes.size();};
282 
284  bool DumpUniversalBones();
286  bool DumpParaXBones(int nLevel, int nBoneIndex, CParaFile* pFile = NULL);
288  bool WriteBVHNode(int nLevel, int nBoneIndex, CParaFile* pFile, bool bEscapeUnknownBones = true);
289  bool WriteBVHNodeAnimation(int nLevel, int nBoneIndex, CParaFile* pFile, bool bEscapeUnknownBones = true);
290 
291  bool BeginBVHNode(CParaFile* pFile, int nLevel, const string& sMarker, const Vector3& vOffset, bool bExportPos, bool bExportRot);
292  bool EndBVHNode(CParaFile* pFile, int nLevel,const string& sMarker);
293  bool WriteQuaternion(CParaFile* pFile, const Quaternion& q, bool bRotY = false);
294  bool WriteEndSite(CParaFile* pFile, int nLevel, int nBoneIndex);
295 
296 
297  bool WriteHips(int nLevel, bool bHierachy, CParaFile* pFile);
298  bool WriteLeftHip(int nLevel, bool bHierachy, CParaFile* pFile);
299  bool WriteLeftKnee(int nLevel, bool bHierachy, CParaFile* pFile);
300  bool WriteLeftAnkle(int nLevel, bool bHierachy, CParaFile* pFile);
301  bool WriteLeftToe(int nLevel, bool bHierachy, CParaFile* pFile);
302 
303  bool WriteRightHip(int nLevel, bool bHierachy, CParaFile* pFile);
304  bool WriteRightKnee(int nLevel, bool bHierachy, CParaFile* pFile);
305  bool WriteRightAnkle(int nLevel, bool bHierachy, CParaFile* pFile);
306  bool WriteRightToe(int nLevel, bool bHierachy, CParaFile* pFile);
307 
308 
309  bool WriteChest(int nLevel, bool bHierachy, CParaFile* pFile);
310  bool WriteChest2(int nLevel, bool bHierachy, CParaFile* pFile);
311  bool WriteNeck(int nLevel, bool bHierachy, CParaFile* pFile);
312  bool WriteHead(int nLevel, bool bHierachy, CParaFile* pFile);
313  bool WriteLeftCollar(int nLevel, bool bHierachy, CParaFile* pFile);
314  bool WriteLeftShoulder(int nLevel, bool bHierachy, CParaFile* pFile);
315  bool WriteLeftElbow(int nLevel, bool bHierachy, CParaFile* pFile);
316  bool WriteLeftWrist(int nLevel, bool bHierachy, CParaFile* pFile);
317  bool WriteLeftFinger0(int nLevel, bool bHierachy, CParaFile* pFile);
318  bool WriteRightCollar(int nLevel, bool bHierachy, CParaFile* pFile);
319  bool WriteRightShoulder(int nLevel, bool bHierachy, CParaFile* pFile);
320  bool WriteRightElbow(int nLevel, bool bHierachy, CParaFile* pFile);
321  bool WriteRightWrist(int nLevel, bool bHierachy, CParaFile* pFile);
322  bool WriteRightFinger0(int nLevel, bool bHierachy, CParaFile* pFile);
323 
324 
325 
327  int ComputeSubBonesCount(int nBoneIndex);
328 
330  bool SaveMarkerFile(const string& sFileName);
333  bool LoadMarkerFile(const string& sFileName);
334 
340  int GetMarkedParentIndex(int nIndex);
341 
343  bool UpdateCriticalBone(int nBoneIndex);
344 
346  bool SortBVHMarker();
348  bool AutoGenerateBVHMarker(int nBoneIndex, bool bGenChest, bool bGenHips);
349 
351  bool GenHipMarker(int nBoneIndex, int* ChestBoneIndex);
353  bool GenChestMarker(int nBoneIndex);
354 
355  };
356 
357 }
const Vector3 & GetOffset()
get relative offset to the parent bone
Definition: BVHSerializer.h:131
Vector3 m_vAbsOffset
absolute offset in object space.
Definition: BVHSerializer.h:119
string m_sMarkerName
maker name for BVH or CSM, default is bone ID.
Definition: BVHSerializer.h:121
Definition: BVHSerializer.h:97
Vector3 m_vOffsetToParent
relative offset to the parent bone
Definition: BVHSerializer.h:115
Definition: BVHSerializer.h:150
int m_nSubLevels
how many levels are the child objects
Definition: BVHSerializer.h:113
different physics engine has different winding order.
Definition: EventBinding.h:32
bool m_bCriticalBone
a bone is a critical bone, if this bone or any of its child has a marker name.
Definition: BVHSerializer.h:123
vector< int > m_childBones
array of index of the child bones.
Definition: BVHSerializer.h:109
Implementation of a Quaternion, i.e.
Definition: ParaQuaternion.h:10
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
string m_nodeName
name of the motion capture node, it is mostly predefined.
Definition: BVHSerializer.h:100
int m_nParentIndex
index of the parent bone
Definition: BVHSerializer.h:107
Definition: BVHSerializer.h:105
it presents a real or virtual file in ParaEngine.
Definition: ParaFile.h:31
CharacterStudioNodeEnum
predefined CSM file node.
Definition: BVHSerializer.h:176
float m_fBoneLength
this is a pre-calculated value for |m_vOffsetToParent|
Definition: BVHSerializer.h:117
ParaX model is the model file for character animation, etc in the game world.
Definition: ParaXModel.h:30
int m_nBoneIndex
to which bone this motion capture node is mapped to.By default this is -1, which means that it maps t...
Definition: BVHSerializer.h:102
BioVision Hierarchy (BVH) motion capture file serializer for ParaX file.
Definition: BVHSerializer.h:23
int m_nSubBones
number of bones.
Definition: BVHSerializer.h:111