9 template<
typename TDataType>
13 auto defaultTopo = std::make_shared<DiscreteElements<TDataType>>();
14 this->stateTopology()->setDataPtr(defaultTopo);
17 template<
typename TDataType>
23 template<
typename TDataType>
26 FILE* fp = fopen(filepath,
"rb");
28 if (!fp)
return false;
30 fseek(fp, 0, SEEK_END);
31 long file_size = ftell(fp);
32 fseek(fp, 0, SEEK_SET);
33 auto* content =
new ofbx::u8[file_size];
34 fread(content, 1, file_size, fp);
45 template<
typename TDataType>
49 if (strlen(
object.name) != 1)
return;
52 d[0] = AnimObject->getAnimationDX();
53 d[1] = AnimObject->getAnimationDY();
54 d[2] = AnimObject->getAnimationDZ();
55 auto curve0 = AnimObject->getCurve(0);
56 int key_allsize = (curve0 ==
nullptr)? 1: curve0->getKeyCount();
57 auto animCurve = std::make_shared<dyno::AnimationCurve<TDataType>>(key_allsize, d[0], d[1], d[2]);
59 for (
int i = 0; i < 3; ++i)
61 std::vector<long long> times;
62 std::vector<float> values;
64 auto curve = AnimObject->getCurve(i);
68 values.push_back(d[i]);
71 int key_count = curve->getKeyCount();
75 const long long* t = curve->getKeyTime();
76 const float* v = curve->getKeyValue();
77 times.assign(t, t + key_count);
78 values.assign(v, v + key_count);
81 animCurve->set(i, times, values);
84 switch (
object.name[0])
87 parent->setAnimTranslation(animCurve);
90 parent->setAnimRotation(animCurve);
93 parent->setAnimScaling(animCurve);
101 template<
typename TDataType>
107 copyVecR(cur->PreRotation,
object.getPreRotation());
108 copyVecT(cur->LclTranslation,
object.getLocalTranslation());
109 copyVecR(cur->LclRotation,
object.getLocalRotation());
110 copyVec(cur->LclScaling,
object.getLocalScaling());
111 cur->CurTranslation = cur->LclTranslation;
112 cur->CurRotation = cur->LclRotation;
113 cur->CurScaling = cur->LclScaling;
116 m_jointMap.push_back(cur);
119 template<
typename TDataType>
124 std::shared_ptr<JointTree<TDataType>> cur;
128 cur = std::make_shared<JointTree<TDataType>>();
129 if(parent !=
nullptr) parent->children.push_back(cur);
130 cur->parent = parent;
131 getModelProperties(
object, cur);
134 while (
ofbx::Object* child =
object.resolveObjectLink(i))
138 getLimbNode(*child, cur);
140 getAnimationCurve(*child, cur);
142 else getLimbNode(*child, parent);
146 template<
typename TDataType>
154 getLimbNode(*child,
nullptr);
160 template<
typename TDataType>
163 auto filename = this->varFileName()->getData();
164 std::string filepath = filename.string();
166 initFBX(filepath.c_str());
171 template<
typename TDataType>
174 Coord center = (v0 + v1) / 2.f;
176 Vec3f dir =
Vec3f(tmp[0], tmp[1], tmp[2]).normalize();
184 axis =
Vec3f(0, -dir.z, dir.y).normalize();
188 axis =
Vec3f(dir.z, 0, -dir.x).normalize();
192 axis =
Vec3f(-dir.y, dir.x, 0).normalize();
195 float cos1 = sqrtf((1 + cos2) / 2.0);
196 float sin1 = sqrtf((1 - cos2) / 2.0);
198 Quat<Real> t(center[0], center[1], center[2], 0.f);
203 template<
typename TDataType>
206 this->m_jointMap[0]->scale(s);
211 template<
typename TDataType>
214 this->m_jointMap[0]->translate(t);
220 template<
typename TDataType>
224 if (m_jointMap.empty())
226 printf(
"Load Skeleton failed.");
232 std::vector<Coord> v0;
233 std::vector<Coord> v1;
234 for (
auto joint : this->m_jointMap)
236 joint->getGlobalQuat();
237 joint->getGlobalCoord();
246 for (
auto joint : this->m_jointMap)
248 for (
auto joint_son : joint->children)
250 m_capLists.push_back(
JCapsule{id_joint, id_cap,
251 joint->GlCoord, joint_son->GlCoord});
252 Vec3f t0 = joint->GlCoord;
253 Vec3f t1 = joint_son->GlCoord;
260 getCenterQuat(joint->GlCoord, joint_son->GlCoord, t, r);
268 m_numjoints = id_joint;
272 this->outCapsule()->allocate();
273 this->outCapsule()->getData().resize(m_numCaps);
274 this->outCapsule()->getData().assign(m_capLists);
276 this->outTranslate()->allocate();
277 this->outTranslate()->getData().resize(m_numCaps);
278 this->outTranslate()->getData().assign(m_T);
280 this->outRotate()->allocate();
281 this->outRotate()->getData().resize(m_numCaps);
282 this->outRotate()->getData().assign(m_R);
284 this->outPosV()->allocate();
285 this->outPosV()->getData().resize(m_numCaps);
286 this->outPosV()->getData().assign(v0);
288 this->outPosU()->allocate();
289 this->outPosU()->getData().resize(m_numCaps);
290 this->outPosU()->getData().assign(v1);
296 auto topo = TypeInfo::cast<DiscreteElements<DataType3f>>(this->stateTopology()->getDataPtr());
298 for (
auto& cap : m_capLists)
301 cap3d.
radius = this->varRadius()->getData();
304 mHostCap3D.push_back(cap3d);
307 auto& caps = topo->getCaps();
308 caps.resize(mHostCap3D.size());
309 caps.assign(mHostCap3D);
314 template<
typename TDataType>
317 if (this->m_jointMap.empty())
319 printf(
"Load Skeleton failed.");
324 for (
auto joint : this->m_jointMap)
329 joint->applyAnimationAll(this->stateElapsedTime()->getData());
335 for (
auto joint : m_jointMap)
337 joint->getGlobalQuat();
338 joint->getGlobalCoord();
344 std::vector<Coord> v0;
345 std::vector<Coord> v1;
346 for (
auto joint : this->m_jointMap)
348 for (
auto joint_son : joint->children)
350 m_capLists[index].v0 = joint->GlCoord;
351 m_capLists[index].v1 = joint_son->GlCoord;
353 getCenterQuat(joint->GlCoord, joint_son->GlCoord, t, r);
357 Vec3f t0 = joint->GlCoord;
358 Vec3f t1 = joint_son->GlCoord;
368 this->outCapsule()->getData().assign(m_capLists);
369 this->outTranslate()->getData().assign(m_T);
370 this->outRotate()->getData().assign(m_R);
372 this->outPosV()->getData().assign(v0);
373 this->outPosU()->getData().assign(v1);
378 auto topo = TypeInfo::cast<DiscreteElements<DataType3f>>(this->stateTopology()->getDataPtr());
380 for (
auto& cap : m_capLists)
382 auto &cap3d = mHostCap3D[index++];
383 cap3d.segment.v0 = cap.v0;
384 cap3d.segment.v1 = cap.v1;
387 auto& caps = topo->getCaps();
388 caps.assign(mHostCap3D);
A JointTree(Skeleton) represents a hierarchical tree structure of joints.
void resetStates() override
void getCenterQuat(Coord v0, Coord v1, Quat< Real > &T, Quat< Real > &R)
void getAnimationCurve(const ofbx::Object &object, std::shared_ptr< JointTree< TDataType > > parent)
bool initFBX(const char *filepath)
void getNodes(const ofbx::IScene &scene)
void updateTopology() override
virtual ~SkeletonLoader()
void getModelProperties(const ofbx::Object &object, std::shared_ptr< JointTree< TDataType > > cur)
void getLimbNode(const ofbx::Object &object, std::shared_ptr< JointTree< TDataType > > parent)
TSegment3D< Real > segment
This is an implementation of AdditiveCCD based on peridyno.
Vec3f DEBUG_T(0.25, 0.0, 0.0)
IMPLEMENT_TCLASS(FloatingNumber, TDataType)
DEFINE_CLASS(CircularEmitter)
IScene * load(const u8 *data, int size, u64 flags, JobProcessor job_processor, void *job_user_ptr)
TEMPLATE_TYPENAME_T VEC3_T axis(QUAT_T const &q)
virtual const Object * getRoot() const =0
Object * resolveObjectLink(int idx) const