PeriDyno 1.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ArticulatedBody.cu
Go to the documentation of this file.
1#include "ArticulatedBody.h"
2
3//Collision
4#include "Collision/NeighborElementQuery.h"
5#include "Collision/CollistionDetectionTriangleSet.h"
6
7//RigidBody
8#include "Module/ContactsUnion.h"
9#include "Module/TJConstraintSolver.h"
10#include "Module/InstanceTransform.h"
11#include "Module/SharedFuncsForRigidBody.h"
12
13//Rendering
14#include "Module/GLPhotorealisticInstanceRender.h"
15
16#include "GltfFunc.h"
17#include "helpers/tinyobj_helper.h"
18
19namespace dyno
20{
21 template<typename TDataType>
22 ArticulatedBody<TDataType>::ArticulatedBody()
23 : ParametricModel<TDataType>()
24 , RigidBodySystem<TDataType>()
25 {
26 this->setAutoHidden(false);
27
28 this->stateTextureMesh()->setDataPtr(std::make_shared<TextureMesh>());
29
30 auto callback = std::make_shared<FCallBackFunc>(std::bind(&ArticulatedBody<TDataType>::varChanged, this));
31 this->varFilePath()->attach(callback);
32
33 this->animationPipeline()->clear();
34
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);
42
43 auto prRender = std::make_shared<GLPhotorealisticInstanceRender>();
44 this->stateTextureMesh()->connect(prRender->inTextureMesh());
45 transformer->outInstanceTransform()->connect(prRender->inTransform());
46 this->graphicsPipeline()->pushModule(prRender);
47
48 this->setForceUpdate(true);
49 }
50
51 template<typename TDataType>
52 ArticulatedBody<TDataType>::~ArticulatedBody()
53 {
54
55 }
56
57
58 template<typename TDataType>
59 void ArticulatedBody<TDataType>::resetStates()
60 {
61 RigidBodySystem<TDataType>::resetStates();
62
63 auto topo = this->stateTopology()->getDataPtr();
64
65 int sizeOfRigids = this->stateCenter()->size();
66
67 auto texMesh = this->stateTextureMesh()->constDataPtr();
68
69 uint N = 0;
70 if (texMesh != NULL);
71 N = texMesh->shapes().size();
72
73 CArrayList<Transform3f> tms;
74 CArray<uint> instanceNum(N);
75 instanceNum.reset();
76
77 //Calculate instance number
78 for (uint i = 0; i < mBindingPair.size(); i++)
79 {
80 instanceNum[mBindingPair[i].first]++;
81 }
82
83 if (instanceNum.size() > 0)
84 tms.resize(instanceNum);
85
86 //Initialize CArrayList
87 for (uint i = 0; i < N; i++)
88 {
89 for (uint j = 0; j < instanceNum[i]; j++)
90 {
91 tms[i].insert(Transform3f());
92 }
93 }
94
95 this->stateInstanceTransform()->assign(tms);
96
97 auto deTopo = this->stateTopology()->constDataPtr();
98 auto offset = deTopo->calculateElementOffset();
99
100 std::vector<Pair<uint, uint>> bindingPair(sizeOfRigids);
101 std::vector<int> tags(sizeOfRigids, 0);
102
103 for (int i = 0; i < mBindingPair.size(); i++)
104 {
105 auto actor = mActors[i];
106 int idx = actor->idx + offset.checkElementOffset(actor->shapeType);
107
108 bindingPair[idx] = mBindingPair[i];
109 tags[idx] = 1;
110 }
111
112 this->stateBindingPair()->assign(bindingPair);
113 this->stateBindingTag()->assign(tags);
114
115 this->updateInstanceTransform();
116
117 tms.clear();
118 bindingPair.clear();
119 tags.clear();
120
121 this->transform();
122
123 topo->setPosition(this->stateCenter()->constData());
124 topo->setRotation(this->stateRotationMatrix()->constData());
125 topo->update();
126 }
127
128 template<typename TDataType>
129 void ArticulatedBody<TDataType>::varChanged()
130 {
131 std::shared_ptr<TextureMesh> texMesh = this->stateTextureMesh()->getDataPtr();
132 auto filepath = this->varFilePath()->getValue();
133
134 auto ext = filepath.path().extension().string();
135 auto name = filepath.string();
136
137 if (ext == ".gltf")
138 {
139 loadGLTFTextureMesh(texMesh, name);
140 }
141 else if (ext == ".obj")
142 {
143 loadTextureMeshFromObj(texMesh, name);
144 }
145 }
146
147 template<typename TDataType>
148 void ArticulatedBody<TDataType>::transform()
149 {
150 ////************************** initial mInitialRot *************************//
151
152 CArray<Coord> hostCenter;
153 hostCenter.assign(this->stateCenter()->constData());
154
155 CArray<Quat<Real>> hostQuaternion;
156 hostQuaternion.assign(this->stateQuaternion()->constData());
157
158 CArray<Mat3f> hostRotation;
159 hostRotation.assign(this->stateRotationMatrix()->constData());
160
161 //for (size_t i = 0; i < vehicleNum; i++)
162 {
163 //get varTransform;
164 auto quat = this->computeQuaternion();
165 Coord location = this->varLocation()->getValue();
166
167 for (uint i = 0; i < hostCenter.size(); i++)
168 {
169 hostCenter[i] = quat.rotate(hostCenter[i]) + location;
170 }
171
172 //***************************** Rotation *************************//
173
174 for (uint i = 0; i < hostQuaternion.size(); i++)
175 {
176 hostQuaternion[i] = quat * hostQuaternion[i];
177 }
178
179 for (uint i = 0; i < hostRotation.size(); i++)
180 {
181 hostRotation[i] = quat.toMatrix3x3() * hostRotation[i];
182 }
183 }
184
185 this->stateCenter()->assign(hostCenter);
186 this->stateQuaternion()->assign(hostQuaternion);
187 this->stateRotationMatrix()->assign(hostRotation);
188
189 hostCenter.clear();
190 hostQuaternion.clear();
191 hostRotation.clear();
192 }
193
194
195 template<typename TDataType>
196 void ArticulatedBody<TDataType>::updateStates()
197 {
198 RigidBodySystem<TDataType>::updateStates();
199 }
200
201 template<typename TDataType>
202 void ArticulatedBody<TDataType>::updateInstanceTransform()
203 {
204 ApplyTransform(
205 this->stateInstanceTransform()->getData(),
206 this->stateCenter()->getData(),
207 this->stateRotationMatrix()->getData(),
208 this->stateBindingPair()->constData(),
209 this->stateBindingTag()->constData());
210 }
211
212 template<typename TDataType>
213 void ArticulatedBody<TDataType>::bind(std::shared_ptr<PdActor> actor, Pair<uint, uint> shapeId)
214 {
215 mActors.push_back(actor);
216 mBindingPair.push_back(shapeId);
217 }
218
219 template<typename TDataType>
220 void ArticulatedBody<TDataType>::clearVechicle()
221 {
222 mBindingPair.clear();
223 mActors.clear();
224 }
225
226 DEFINE_CLASS(ArticulatedBody);
227}