1#include "ArticulatedBody.h"
4#include "Collision/NeighborElementQuery.h"
5#include "Collision/CollistionDetectionTriangleSet.h"
8#include "Module/ContactsUnion.h"
9#include "Module/TJConstraintSolver.h"
10#include "Module/InstanceTransform.h"
11#include "Module/SharedFuncsForRigidBody.h"
14#include "Module/GLPhotorealisticInstanceRender.h"
17#include "helpers/tinyobj_helper.h"
21 template<typename TDataType>
22 ArticulatedBody<TDataType>::ArticulatedBody()
23 : ParametricModel<TDataType>()
24 , RigidBodySystem<TDataType>()
26 this->setAutoHidden(false);
28 this->stateTextureMesh()->setDataPtr(std::make_shared<TextureMesh>());
30 auto callback = std::make_shared<FCallBackFunc>(std::bind(&ArticulatedBody<TDataType>::varChanged, this));
31 this->varFilePath()->attach(callback);
33 this->animationPipeline()->clear();
35 auto transformer = std::make_shared<InstanceTransform<DataType3f>>();
36 this->stateCenter()->connect(transformer->inCenter());
37 this->stateRotationMatrix()->connect(transformer->inRotationMatrix());
38 this->stateBindingPair()->connect(transformer->inBindingPair());
39 this->stateBindingTag()->connect(transformer->inBindingTag());
40 this->stateInstanceTransform()->connect(transformer->inInstanceTransform());
41 this->graphicsPipeline()->pushModule(transformer);
43 auto prRender = std::make_shared<GLPhotorealisticInstanceRender>();
44 this->stateTextureMesh()->connect(prRender->inTextureMesh());
45 transformer->outInstanceTransform()->connect(prRender->inTransform());
46 this->graphicsPipeline()->pushModule(prRender);
48 this->setForceUpdate(true);
51 template<typename TDataType>
52 ArticulatedBody<TDataType>::~ArticulatedBody()
58 template<typename TDataType>
59 void ArticulatedBody<TDataType>::resetStates()
61 RigidBodySystem<TDataType>::resetStates();
63 auto topo = this->stateTopology()->getDataPtr();
65 int sizeOfRigids = this->stateCenter()->size();
67 auto texMesh = this->stateTextureMesh()->constDataPtr();
71 N = texMesh->shapes().size();
73 CArrayList<Transform3f> tms;
74 CArray<uint> instanceNum(N);
77 //Calculate instance number
78 for (uint i = 0; i < mBindingPair.size(); i++)
80 instanceNum[mBindingPair[i].first]++;
83 if (instanceNum.size() > 0)
84 tms.resize(instanceNum);
86 //Initialize CArrayList
87 for (uint i = 0; i < N; i++)
89 for (uint j = 0; j < instanceNum[i]; j++)
91 tms[i].insert(Transform3f());
95 this->stateInstanceTransform()->assign(tms);
97 auto deTopo = this->stateTopology()->constDataPtr();
98 auto offset = deTopo->calculateElementOffset();
100 std::vector<Pair<uint, uint>> bindingPair(sizeOfRigids);
101 std::vector<int> tags(sizeOfRigids, 0);
103 for (int i = 0; i < mBindingPair.size(); i++)
105 auto actor = mActors[i];
106 int idx = actor->idx + offset.checkElementOffset(actor->shapeType);
108 bindingPair[idx] = mBindingPair[i];
112 this->stateBindingPair()->assign(bindingPair);
113 this->stateBindingTag()->assign(tags);
115 this->updateInstanceTransform();
123 topo->setPosition(this->stateCenter()->constData());
124 topo->setRotation(this->stateRotationMatrix()->constData());
128 template<typename TDataType>
129 void ArticulatedBody<TDataType>::varChanged()
131 std::shared_ptr<TextureMesh> texMesh = this->stateTextureMesh()->getDataPtr();
132 auto filepath = this->varFilePath()->getValue();
134 auto ext = filepath.path().extension().string();
135 auto name = filepath.string();
139 loadGLTFTextureMesh(texMesh, name);
141 else if (ext == ".obj")
143 loadTextureMeshFromObj(texMesh, name);
147 template<typename TDataType>
148 void ArticulatedBody<TDataType>::transform()
150 ////************************** initial mInitialRot *************************//
152 CArray<Coord> hostCenter;
153 hostCenter.assign(this->stateCenter()->constData());
155 CArray<Quat<Real>> hostQuaternion;
156 hostQuaternion.assign(this->stateQuaternion()->constData());
158 CArray<Mat3f> hostRotation;
159 hostRotation.assign(this->stateRotationMatrix()->constData());
161 //for (size_t i = 0; i < vehicleNum; i++)
164 auto quat = this->computeQuaternion();
165 Coord location = this->varLocation()->getValue();
167 for (uint i = 0; i < hostCenter.size(); i++)
169 hostCenter[i] = quat.rotate(hostCenter[i]) + location;
172 //***************************** Rotation *************************//
174 for (uint i = 0; i < hostQuaternion.size(); i++)
176 hostQuaternion[i] = quat * hostQuaternion[i];
179 for (uint i = 0; i < hostRotation.size(); i++)
181 hostRotation[i] = quat.toMatrix3x3() * hostRotation[i];
185 this->stateCenter()->assign(hostCenter);
186 this->stateQuaternion()->assign(hostQuaternion);
187 this->stateRotationMatrix()->assign(hostRotation);
190 hostQuaternion.clear();
191 hostRotation.clear();
195 template<typename TDataType>
196 void ArticulatedBody<TDataType>::updateStates()
198 RigidBodySystem<TDataType>::updateStates();
201 template<typename TDataType>
202 void ArticulatedBody<TDataType>::updateInstanceTransform()
205 this->stateInstanceTransform()->getData(),
206 this->stateCenter()->getData(),
207 this->stateRotationMatrix()->getData(),
208 this->stateBindingPair()->constData(),
209 this->stateBindingTag()->constData());
212 template<typename TDataType>
213 void ArticulatedBody<TDataType>::bind(std::shared_ptr<PdActor> actor, Pair<uint, uint> shapeId)
215 mActors.push_back(actor);
216 mBindingPair.push_back(shapeId);
219 template<typename TDataType>
220 void ArticulatedBody<TDataType>::clearVechicle()
222 mBindingPair.clear();
226 DEFINE_CLASS(ArticulatedBody);