PeriDyno 1.0.0
Loading...
Searching...
No Matches
JointTree.cpp
Go to the documentation of this file.
1#pragma once
2#include "JointTree.h"
3
4#define cos_angle(angle) cos(double(angle * 3.1415926f / 180.0))
5#define sin_angle(angle) sin(double(angle * 3.1415926f / 180.0))
6#define radian(x) x * 3.1415926f / 180.0
7
8namespace dyno
9{
10 IMPLEMENT_TCLASS(JointTree, TDataType)
11
12 template<typename TDataType>
14 {
15 id = -1;
16 RotationActive = true;
17 PreRotation = Coord(0);
19 PreScaling = Coord(1);
20
22 LclRotation = Coord(0);
23 LclScaling = Coord(1);
24
25 tmp = Coord(0, 0, 0);
26
27 GlT = Quat<Real>(0, 0, 0, 0);
28 GlR = Quat<Real>(0, 0, 0, 0);
29 GlS = 1.f;
30 }
31
32 template<typename TDataType>
37
38
39 template<typename TDataType>
41 {
42 Mat translation = Mat(
43 1, 0, 0, T[0],
44 0, 1, 0, T[1],
45 0, 0, 1, T[2],
46 0, 0, 0, 1);
47
48 // R[X,Y,Z] -> [Z,X,Y]轴
49
50 double X = R[0];
51 Mat rotation_x = Mat(
52 1, 0, 0, 0,
53 0, cos_angle(X), -sin_angle(X), 0,
54 0, sin_angle(X), cos_angle(X), 0,
55 0, 0, 0, 1);
56
57 double Y = R[1];
58 Mat rotation_y = Mat(
59 cos_angle(Y), 0, sin_angle(Y), 0,
60 0, 1, 0, 0,
61 -sin_angle(Y), 0, cos_angle(Y), 0,
62 0, 0, 0, 1);
63
64 double Z = R[2];
65 Mat rotation_z = Mat(
66 cos_angle(Z), -sin_angle(Z), 0, 0,
67 sin_angle(Z), cos_angle(Z), 0, 0,
68 0, 0, 1, 0,
69 0, 0, 0, 1);
70
71 Mat scaling= Mat(
72 S[0], 0, 0, 0,
73 0, S[1], 0, 0,
74 0, 0, S[2], 0,
75 0, 0, 0, 1);
76
77 return translation * scaling * rotation_x * rotation_y * rotation_z;
78 }
79
80 // 遍历关节层次时,顺便更新
81 template<typename TDataType>
83 {
84 // 注意顺序
85 this->GlobalTransform = this->getTransform(this->CurTranslation, this->CurRotation, this->CurScaling);
86 if(this->parent != nullptr)
87 this->GlobalTransform = this->parent->GlobalTransform * this->GlobalTransform;
88 else
89 {
90 //Pre
91 this->GlobalTransform = this->getTransform(this->PreTranslation, this->PreRotation, this->PreScaling) * this->GlobalTransform;
92 }
93 //DEBUG
94 // printf("Mat:\n");
95 // for (int i = 0; i < 4; ++i)
96 // {
97 // auto v = this->GlobalTransform.row(i);
98 // printf("[%f, %f, %f, %f]\n", v[0], v[1], v[2], v[3]);
99 // }
100 }
101
102 template<typename TDataType>
104 {
105 Quat<Real> t(T[0], T[1], T[2], 0.f);
106 // Rotate
107 Quat<Real> q_x(radian(R[0]), Vec3f(1.f, 0.f, 0.f));
108 Quat<Real> q_y(radian(R[1]), Vec3f(0.f, 1.f, 0.f));
109 Quat<Real> q_z(radian(R[2]), Vec3f(0.f, 0.f, 1.f));
110
111 this->GlT = this->GlT + this->GlS * this->GlR * t * this->GlR.conjugate();
112 this->GlS = this->GlS * S;
113 this->GlR = this->GlR * (q_x * q_y * q_z);
114
115 // printf("Quat:\n");
116 // printf("T: [%f, %f, %f, %f]\n", this->GlT.x, this->GlT.y, this->GlT.z, this->GlT.w);
117 // printf("R: [%f, %f, %f, %f]\n", this->GlR.x, this->GlR.y, this->GlR.z, this->GlR.w);
118 // printf("S: [%f]\n", this->GlS);
119 // for (int i = 0; i < 4; ++i)
120 // {
121 // auto v = this->GlobalTransform.row(i);
122 // printf("[%f, %f, %f, %f]\n", v[0], v[1], v[2], v[3]);
123 // TODO:
124 // GlR.normalize();
125 }
126
127 // 遍历关节层次时,顺便更新
128 template<typename TDataType>
130 {
131 if(this->parent != nullptr)
132 {
133 this->GlT = this->parent->GlT;
134 this->GlS = this->parent->GlS;
135 this->GlR = this->parent->GlR;
136 getQuat(this->CurTranslation, this->CurRotation, this->CurScaling[0]);
137 }else
138 {
139 this->GlT = Quat<Real>(0.f, 0.f, 0.f, 0.f);
140 this->GlS = 1.f;
141 this->GlR = Quat<Real>(0.f, 0.f, 0.f, 1.f);
142 getQuat(this->PreTranslation, this->PreRotation, this->PreScaling[0]);
143 getQuat(this->CurTranslation, this->CurRotation, this->CurScaling[0]);
144
145 }
146 //DEBUG
147 // printf("[Root] Cur QuatR: (%f)\n", this->GlR.w);
148 // printf("[Root] T: (%f, %f, %f)\n", this->CurTranslation[0], this->CurTranslation[1], this->CurTranslation[2]);
149 // printf("[Root] R: (%f, %f, %f)\n", this->CurRotation[0], this->CurRotation[1], this->CurRotation[2]);
150 // printf("[Root] S: (%f)\n", this->CurScaling[0]);
151 }
152
153 template<typename TDataType>
155 {
156 Vec4f tmp = this->GlobalTransform * Vec4f(X[0], X[1], X[2], 1) ;
157 return Coord(tmp[0] / tmp[3], tmp[1] / tmp[3], tmp[2] / tmp[3]);
158 }
159
160 template<typename TDataType>
162 {
163 Quat<Real> tmp(X[0], X[1], X[2], 1) ;
164 tmp = this->GlS * this->GlR * tmp * this->GlR.conjugate() + this->GlT;
165 return Coord(tmp.x, tmp.y, tmp.z);
166 }
167
168 template<typename TDataType>
170 {
171 this->LastCoord = this->GlCoord;
172 this->GlCoord = this->getCoordByQuat(Coord(0, 0, 0));
173 }
174
175 template<typename TDataType>
177 {
178 this->id = jointTree.id;
179 this->PreRotation = jointTree.PreRotation;
180 this->LclTranslation = jointTree.LclTranslation;
181 this->LclRotation = jointTree.LclRotation;
182 this->LclScaling = jointTree.LclScaling;
183 this->AnimTranslation = jointTree.AnimTranslation;
184 this->AnimRotation = jointTree.AnimRotation;
185 this->AnimScaling = jointTree.AnimScaling;
186 this->GlCoord = jointTree.GlCoord;
187 this->GlobalTransform = jointTree.GlobalTransform;
188 this->GlT = jointTree.GlT;
189 this->GlR = jointTree.GlR;
190 this->GlS = jointTree.GlS;
191 this->RotationActive = jointTree.RotationActive;
192 this->children.assign(jointTree.children.begin(), jointTree.children.end());
193 this->parent = jointTree.parent;
194 }
195
196 template<typename TDataType>
198 {
199 PreScaling *= s;
200 }
201
202 template<typename TDataType>
207
208 template<typename TDataType>
210 {
211 cur = anim->getCurveValueAll(ptime);
212 // cur = anim->getCurveValueCycle(ptime);
213 }
214
215 template<typename TDataType>
217 {
218 if (AnimTranslation != nullptr)
220 if (AnimRotation != nullptr)
221 {
223 // if (AnimRotation->m_maxSize > 1 && CurRotation[2] < 90)
224 // CurRotation += Coord(0,0, 0.5);
225 }
226 if (AnimScaling != nullptr)
228 }
229
230#ifdef PRECISION_FLOAT
231 template class JointTree<DataType3f>;
232#else
233 template class JointTree<DataType3d>;
234#endif
235}
#define cos_angle(angle)
Definition JointTree.cpp:4
#define sin_angle(angle)
Definition JointTree.cpp:5
#define radian(x)
Definition JointTree.cpp:6
#define IMPLEMENT_TCLASS(name, T1)
Definition Object.h:103
A JointTree(Skeleton) represents a hierarchical tree structure of joints.
Definition JointTree.h:31
Coord PreRotation
Definition JointTree.h:79
TDataType::Coord Coord
Definition JointTree.h:40
unsigned long long id
Definition JointTree.h:77
Mat getTransform(Coord &T, Coord &R, Coord &S)
Definition JointTree.cpp:40
void applyAnimationAll(Real ptime)
void scale(Real s)
std::shared_ptr< AnimationCurve< TDataType > > AnimTranslation
Definition JointTree.h:89
void applyAnimationByOne(Coord &init, Coord &cur, std::shared_ptr< AnimationCurve< TDataType > > &anim, Real ptime)
std::shared_ptr< AnimationCurve< TDataType > > AnimScaling
Definition JointTree.h:91
Coord getCoordByMatrix(Coord X)
void getQuat(Coord &T, Coord &R, float &S)
TDataType::Real Real
Definition JointTree.h:39
void getGlobalCoord()
Coord PreTranslation
Definition JointTree.h:81
::dyno::Mat4f Mat
Definition JointTree.h:42
std::vector< std::shared_ptr< JointTree > > children
Definition JointTree.h:105
Quat< Real > GlR
Definition JointTree.h:103
Coord getCoordByQuat(Coord X)
void getGlobalTransform()
Definition JointTree.cpp:82
Coord CurRotation
Definition JointTree.h:94
Quat< Real > GlT
Definition JointTree.h:102
void translate(Coord t)
std::shared_ptr< AnimationCurve< TDataType > > AnimRotation
Definition JointTree.h:90
Coord LclTranslation
Definition JointTree.h:85
Coord CurTranslation
Definition JointTree.h:93
void copyFrom(JointTree< TDataType > jointTree)
std::shared_ptr< JointTree > parent
Definition JointTree.h:106
Coord LclRotation
Definition JointTree.h:86
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Vector< float, 4 > Vec4f
Definition Vector4D.h:86
Vector< float, 3 > Vec3f
Definition Vector3D.h:93