PeriDyno 1.2.1
Loading...
Searching...
No Matches
VehicleInfo.inl
Go to the documentation of this file.
1#ifndef VEHICLEINFO_SERIALIZATION
2#define VEHICLEINFO_SERIALIZATION
3
4#include "Field.h"
5#include "VehicleInfo.h"
6
7namespace dyno {
8
9 inline void replaceSpacesWithEquals(std::string& substr) {
10 for (char& c : substr) {
11 if (c == ' ') {
12 c = '=';
13 }
14 }
15 }
16
17 inline void replaceEqualsWithSpaces(std::string& substr) {
18 for (char& c : substr) {
19 if (c == '=') {
20 c = ' ';
21 }
22 }
23 }
24
25 inline void convertRigidInfoToStr(std::string VarName, std::vector<VehicleRigidBodyInfo> Array, std::string& Str)
26 {
27 Str.append(VarName + " ");
28 for (int i = 0; i < Array.size(); i++)
29 {
30 auto& it = Array[i];
31 std::string tempName= it.shapeName.name;
33 Str.append(tempName + " "); //1
34 Str.append(std::to_string(it.shapeName.rigidBodyId) + " "); //1
35 Str.append(std::to_string(it.meshShapeId) + " "); //1
36 Str.append(std::to_string(static_cast<int>(it.shapeType)) + " "); //1
37 auto& t = it.transform.translation();
38 auto& r = it.transform.rotation();
39 auto& s = it.transform.scale();
40 Str.append(std::to_string(t.x) + " " + std::to_string(t.y) + " " + std::to_string(t.z) + " ");
41 Str.append(std::to_string(r(0, 0)) + " " + std::to_string(r(0, 1)) + " " + std::to_string(r(0, 2)) + " ");
42 Str.append(std::to_string(r(1, 0)) + " " + std::to_string(r(1, 1)) + " " + std::to_string(r(1, 2)) + " ");
43 Str.append(std::to_string(r(2, 0)) + " " + std::to_string(r(2, 1)) + " " + std::to_string(r(2, 2)) + " ");
44 Str.append(std::to_string(s.x) + " " + std::to_string(s.y) + " " + std::to_string(s.z) + " ");
45
46 Str.append(std::to_string(it.Offset.x) + " " + std::to_string(it.Offset.y) + " " + std::to_string(it.Offset.z) + " ");
47 Str.append(std::to_string(it.mHalfLength.x) + " " + std::to_string(it.mHalfLength.y) + " " + std::to_string(it.mHalfLength.z) + " ");
48 Str.append(std::to_string(it.radius) + " ");
49
50 Str.append(std::to_string(it.tet[0].x) + " " + std::to_string(it.tet[0].y) + " " + std::to_string(it.tet[0].z) + " ");
51 Str.append(std::to_string(it.tet[1].x) + " " + std::to_string(it.tet[1].y) + " " + std::to_string(it.tet[1].z) + " ");
52 Str.append(std::to_string(it.tet[2].x) + " " + std::to_string(it.tet[2].y) + " " + std::to_string(it.tet[2].z) + " ");
53 Str.append(std::to_string(it.tet[3].x) + " " + std::to_string(it.tet[3].y) + " " + std::to_string(it.tet[3].z) + " ");
54
55 Str.append(std::to_string(it.capsuleLength) + " ");
56 Str.append(std::to_string(static_cast<int>(it.motion)) + " ");
57 Str.append(std::to_string(it.mDensity) + " ");
58 Str.append(std::to_string(int(it.rigidGroup)) + " ");
59
60 if (i != Array.size() - 1)
61 {
62 Str.append(" \n");
63 }
64 }
65 Str.append(" ");
66 }
67
68 inline void convertJointInfoToStr(std::string VarName, std::vector<VehicleJointInfo> Array, std::string& Str)
69 {
70 Str.append(VarName + " ");
71 for (int i = 0; i < Array.size(); i++)
72 {
73 auto& it = Array[i];
74
75 Str.append(std::to_string(static_cast<int>(it.mJointType)) + " ");
76 std::string tempName = it.mRigidBodyName_1.name;
78 Str.append(tempName + " "); //1
79 Str.append(std::to_string(it.mRigidBodyName_1.rigidBodyId) + " ");
80 tempName = it.mRigidBodyName_2.name;
82 Str.append(tempName + " "); //1
83 Str.append(std::to_string(it.mRigidBodyName_2.rigidBodyId) + " ");
84 Str.append(std::to_string(int(it.mUseMoter)) + " ");
85 Str.append(std::to_string(int(it.mUseRange)) + " ");
86 Str.append(std::to_string(it.mAnchorPoint[0]) + " " + std::to_string(it.mAnchorPoint[1]) + " " + std::to_string(it.mAnchorPoint[2]) + " ");
87 Str.append(std::to_string(it.mMin) + " ");
88 Str.append(std::to_string(it.mMax) + " ");
89 Str.append(std::to_string(it.mMoter) + " ");
90 Str.append(std::to_string(it.mAxis[0]) + " " + std::to_string(it.mAxis[1]) + " " + std::to_string(it.mAxis[2]) + " ");
91
92 if (i != Array.size() - 1)
93 {
94 Str.append(" \n");
95 }
96 }
97 Str.append(" ");
98 }
99
100 template<>
101 inline std::string FVar<VehicleBind>::serialize()
102 {
103
104 std::string finalText;
105 //serialize Array
106 finalText.append(std::to_string(this->getValue().mVehicleRigidBodyInfo.size()) + " ");
107 finalText.append(std::to_string(this->getValue().mVehicleJointInfo.size()) + " ");
108 finalText.append("\nVehicleRigidBodyInfo \n");
109 convertRigidInfoToStr("", this->getValue().mVehicleRigidBodyInfo, finalText);
110 finalText.append("\nVehicleJointInfo \n");
111 convertJointInfoToStr("", this->getValue().mVehicleJointInfo, finalText);
112
113 std::stringstream ss;
114 ss << finalText;
115
116 return ss.str();
117 }
118
119 template<>
120 inline bool FVar<VehicleBind>::deserialize(const std::string& str)
121 {
122 if (str.empty())
123 return false;
124
125 std::stringstream ss(str);
126 std::string substr;
127
128 auto field = std::make_shared<VehicleBind>();
129 std::vector<VehicleRigidBodyInfo> rigids;
130 std::vector<VehicleJointInfo> joints;
131
132 int dataID = 0;
133 bool isRigid = false;
134 bool isJoint = false;
135
136 int arrayID = -1;
137 int dId = -1;;
138
139 while (ss >> substr)
140 {
141 std::cout << substr << "\n";
142 if (substr == " " || substr == "\n")
143 continue;
144
145 {
146 if (!isRigid && !isJoint)
147 {
148 switch (dataID)
149 {
150 case 0:
151 if (std::isdigit(substr[0]))
152 {
153 rigids.resize(std::stoi(substr));
154 }
155
156 break;
157 case 1:
158 if (std::isdigit(substr[0]))
159 {
160 joints.resize(std::stoi(substr));
161 }
162 break;
163 default:
164 break;
165 }
166 }
167 }
168
169 {
170 if (isRigid)
171 {
172 arrayID = dataID / 42;
173 dId = dataID % 42;
174 }
175 if (arrayID < rigids.size()&&isRigid)
176 {
177 switch (dId)
178 {
179 case 0:
181 rigids[arrayID].shapeName.name = substr;
182 break;
183 case 1:
184 rigids[arrayID].shapeName.rigidBodyId = std::stoi(substr);
185 break;
186 case 2:
187 rigids[arrayID].meshShapeId = std::stoi(substr);
188 break;
189 case 3:
190 rigids[arrayID].shapeType = static_cast<ConfigShapeType>(std::stoi(substr));
191 break;
192 case 4:
193 rigids[arrayID].transform.translation().x = std::stod(substr);
194 break;
195 case 5:
196 rigids[arrayID].transform.translation().y = std::stod(substr);
197 break;
198 case 6:
199 rigids[arrayID].transform.translation().z = std::stod(substr);
200 break;
201 case 7:
202 rigids[arrayID].transform.rotation()(0, 0) = std::stod(substr);
203 break;
204 case 8:
205 rigids[arrayID].transform.rotation()(0, 1) = std::stod(substr);
206 break;
207 case 9:
208 rigids[arrayID].transform.rotation()(0, 2) = std::stod(substr);
209 break;
210 case 10:
211 rigids[arrayID].transform.rotation()(1, 0) = std::stod(substr);
212 break;
213 case 11:
214 rigids[arrayID].transform.rotation()(1, 1) = std::stod(substr);
215 break;
216 case 12:
217 rigids[arrayID].transform.rotation()(1, 2) = std::stod(substr);
218 break;
219 case 13:
220 rigids[arrayID].transform.rotation()(2, 0) = std::stod(substr);
221 break;
222 case 14:
223 rigids[arrayID].transform.rotation()(2, 1) = std::stod(substr);
224 break;
225 case 15:
226 rigids[arrayID].transform.rotation()(2, 2) = std::stod(substr);
227 break;
228 case 16:
229 rigids[arrayID].transform.scale().x = std::stod(substr);
230 break;
231 case 17:
232 rigids[arrayID].transform.scale().y = std::stod(substr);
233 break;
234 case 18:
235 rigids[arrayID].transform.scale().z = std::stod(substr);
236 break;
237 case 19:
238 rigids[arrayID].Offset.x = std::stod(substr);
239 break;
240 case 20:
241 rigids[arrayID].Offset.y = std::stod(substr);
242 break;
243 case 21:
244 rigids[arrayID].Offset.z = std::stod(substr);
245 break;
246 case 22:
247 rigids[arrayID].mHalfLength.x = std::stod(substr);
248 break;
249 case 23:
250 rigids[arrayID].mHalfLength.y = std::stod(substr);
251 break;
252 case 24:
253 rigids[arrayID].mHalfLength.z = std::stod(substr);
254 break;
255 case 25:
256 rigids[arrayID].radius = std::stod(substr);
257 break;
258 case 26:
259 rigids[arrayID].tet[0].x = std::stod(substr);
260 break;
261 case 27:
262 rigids[arrayID].tet[0].y = std::stod(substr);
263 break;
264 case 28:
265 rigids[arrayID].tet[0].z = std::stod(substr);
266 break;
267 case 29:
268 rigids[arrayID].tet[1].x = std::stod(substr);
269 break;
270 case 30:
271 rigids[arrayID].tet[1].y = std::stod(substr);
272 break;
273 case 31:
274 rigids[arrayID].tet[1].z = std::stod(substr);
275 break;
276 case 32:
277 rigids[arrayID].tet[2].x = std::stod(substr);
278 break;
279 case 33:
280 rigids[arrayID].tet[2].y = std::stod(substr);
281 break;
282 case 34:
283 rigids[arrayID].tet[2].z = std::stod(substr);
284 break;
285 case 35:
286 rigids[arrayID].tet[3].x = std::stod(substr);
287 break;
288 case 36:
289 rigids[arrayID].tet[3].y = std::stod(substr);
290 break;
291 case 37:
292 rigids[arrayID].tet[3].z = std::stod(substr);
293 break;
294 case 38:
295 rigids[arrayID].capsuleLength = std::stod(substr);
296 break;
297 case 39:
298 rigids[arrayID].motion = static_cast<ConfigMotionType>(std::stoi(substr));
299 break;
300 case 40:
301 rigids[arrayID].mDensity = std::stoi(substr);
302 break;
303 case 41:
304 rigids[arrayID].rigidGroup = std::stoi(substr);
305 break;
306
307 default:
308 break;
309 }
310 arrayID = -1;
311 dId = -1;
312 }
313
314 if (substr == "VehicleRigidBodyInfo")
315 {
316 dataID = -1;
317 isRigid = true;
318 isJoint = false;
319 }
320
321 }
322
323 {
324 if (isJoint)
325 {
326 arrayID = dataID / 16;
327 dId = dataID % 16;
328 }
329
330 if (arrayID < joints.size() && isJoint)
331 {
332 switch (dId)
333 {
334 case 0:
335 joints[arrayID].mJointType = static_cast<ConfigJointType>(std::stoi(substr));
336 break;
337 case 1:
339 joints[arrayID].mRigidBodyName_1.name = substr;
340 break;
341 case 2:
342 joints[arrayID].mRigidBodyName_1.rigidBodyId = std::stoi(substr);
343 break;
344 case 3:
346 joints[arrayID].mRigidBodyName_2.name = substr;
347 break;
348 case 4:
349 joints[arrayID].mRigidBodyName_2.rigidBodyId = std::stoi(substr);
350 break;
351 case 5:
352 joints[arrayID].mUseMoter = bool(std::stoi(substr));
353 break;
354 case 6:
355 joints[arrayID].mUseRange = bool(std::stoi(substr));
356 break;
357 case 7:
358 joints[arrayID].mAnchorPoint.x = std::stod(substr);
359 break;
360 case 8:
361 joints[arrayID].mAnchorPoint.y = std::stod(substr);
362 break;
363 case 9:
364 joints[arrayID].mAnchorPoint.z = std::stod(substr);
365 break;
366 case 10:
367 joints[arrayID].mMin = std::stod(substr);
368 break;
369 case 11:
370 joints[arrayID].mMax = std::stod(substr);
371 break;
372 case 12:
373 joints[arrayID].mMoter = std::stod(substr);
374 break;
375 case 13:
376 joints[arrayID].mAxis.x = std::stod(substr);
377 break;
378 case 14:
379 joints[arrayID].mAxis.y = std::stod(substr);
380 break;
381 case 15:
382 joints[arrayID].mAxis.z = std::stod(substr);
383 break;
384
385 default:
386 break;
387 }
388 }
389
390 if (substr == "VehicleJointInfo")
391 {
392 dataID = -1;
393 isJoint = true;
394 isRigid = false;
395 }
396 }
397
398 dataID++;
399 }
400
401 field->mVehicleRigidBodyInfo = rigids;
402 field->mVehicleJointInfo = joints;
403 this->setValue(*field);
404
405 return true;
406 }
407
408 inline void convertAnimationJointConfigToStr(Animation2JointConfig& bind, std::string& Str)
409 {
410 Str.append(bind.JointName + " ");
411 Str.append(std::to_string(bind.JointId) + " ");
412 Str.append(std::to_string(bind.Axis) + " ");
413 Str.append(std::to_string(bind.Intensity) + " ");
414 }
415
416
417 template<>
419 {
420 std::string finalText;
421 //serialize Array
422 auto values = this->getValue();
423 finalText.append(std::to_string(values.size()) + " ");
424 for (auto it : values)
425 {
427 finalText.append("\n");
428 }
429
430 return finalText;
431 }
432 template<>
433 inline bool FVar<std::vector<Animation2JointConfig>>::deserialize(const std::string& str)
434 {
435 if (str.empty())
436 return false;
437
438 std::stringstream ss(str);
439 std::string substr;
440
441 std::vector<Animation2JointConfig> animBinds;
442
443
444 int strID = -1;
445
446 int arrayID = -1;
447 int dataID = -1;
448
449 while (ss >> substr)
450 {
451 std::cout << substr << "\n";
452 if (substr == " " || substr == "\n")
453 continue;
454
455 strID++;
456
457 switch (strID)
458 {
459 case 0:
460 if (std::isdigit(substr[0]))
461 animBinds.resize(std::stoi(substr));
462
463 break;
464
465 }
466
467 arrayID = (strID - 1) / 4;
468 dataID = (strID - 1) % 4;
469
470 switch (dataID)
471 {
472
473 case 0:
474 animBinds[arrayID].JointName = substr;
475 break;
476
477 case 1:
478 animBinds[arrayID].JointId = std::stoi(substr);
479 break;
480
481 case 2:
482 animBinds[arrayID].Axis = std::stoi(substr);
483 break;
484
485 case 3:
486 animBinds[arrayID].Intensity = std::stod(substr);
487 break;
488 }
489
490
491 }
492
493 this->setValue(animBinds);
494 return true;
495 }
496
498}
499
500#endif // !VEHICLEINFO_SERIALIZATION
This class is designed to be elegant, so it can be directly passed to GPU as parameters.
Definition Array.h:27
bool deserialize(const std::string &str)
Definition Color.inl:23
std::string serialize()
Definition Color.inl:9
std::string serialize() override
Definition Field.h:60
bool deserialize(const std::string &str) override
Definition Field.h:61
float getValue()
Definition Field.h:136
void setValue(float val, bool notify=true)
Definition Field.h:116
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
ConfigJointType
Definition VehicleInfo.h:32
void replaceEqualsWithSpaces(std::string &substr)
void replaceSpacesWithEquals(std::string &substr)
ConfigShapeType
Definition VehicleInfo.h:22
void convertJointInfoToStr(std::string VarName, std::vector< VehicleJointInfo > Array, std::string &Str)
void convertAnimationJointConfigToStr(Animation2JointConfig &bind, std::string &Str)
void convertRigidInfoToStr(std::string VarName, std::vector< VehicleRigidBodyInfo > Array, std::string &Str)
ConfigMotionType
Definition VehicleInfo.h:15