CodeDumper.cpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
3  * Modified work Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  * 1. The origin of this software must not be misrepresented; you must not
12  * claim that you wrote the original software. If you use this software
13  * in a product, an acknowledgment in the product documentation would be
14  * appreciated but is not required.
15  * 2. Altered source versions must be plainly marked as such, and must not be
16  * misrepresented as being the original software.
17  * 3. This notice may not be removed or altered from any source distribution.
18  */
19 
21 
45 
46 #include <cstdarg>
47 
48 namespace playrho {
49 namespace d2 {
50 
51 namespace {
52 
53  // You can modify this to use your logging facility.
54  void log(const char* string, ...)
55  {
56  va_list args;
57  va_start(args, string);
58  std::vprintf(string, args);
59  va_end(args);
60  }
61 
62 }
63 
64 void Dump(const World& world)
65 {
66  const auto& bodies = world.GetBodies();
67  log("Body** bodies = (Body**)Alloc(%d * sizeof(Body*));\n", size(bodies));
68  auto i = std::size_t{0};
69  for (auto&& body: bodies)
70  {
71  const auto& b = GetRef(body);
72  Dump(b, i);
73  ++i;
74  }
75 
76  const auto& joints = world.GetJoints();
77  log("Joint** joints = (Joint**)Alloc(%d * sizeof(Joint*));\n", size(joints));
78  i = 0;
79  for (auto&& j: joints)
80  {
81  log("{\n");
82  Dump(*j, i);
83  log("}\n");
84  ++i;
85  }
86 
87  log("Free(joints);\n");
88  log("Free(bodies);\n");
89  log("joints = nullptr;\n");
90  log("bodies = nullptr;\n");
91 }
92 
93 void Dump(const Body& body, std::size_t bodyIndex)
94 {
95  log("{\n");
96  log(" BodyConf bd;\n");
97  log(" bd.type = BodyType(%d);\n", body.GetType());
98  log(" bd.position = Vec2(%.15lef, %.15lef);\n",
99  static_cast<double>(Real(get<0>(body.GetLocation()) / Meter)),
100  static_cast<double>(Real(get<1>(body.GetLocation()) / Meter)));
101  log(" bd.angle = %.15lef;\n", static_cast<double>(Real{body.GetAngle() / Radian}));
102  log(" bd.linearVelocity = Vec2(%.15lef, %.15lef);\n",
103  static_cast<double>(Real{get<0>(body.GetVelocity().linear) / MeterPerSecond}),
104  static_cast<double>(Real{get<1>(body.GetVelocity().linear) / MeterPerSecond}));
105  log(" bd.angularVelocity = %.15lef;\n",
106  static_cast<double>(Real{body.GetVelocity().angular / RadianPerSecond}));
107  log(" bd.linearDamping = %.15lef;\n",
108  static_cast<double>(Real{body.GetLinearDamping() / Hertz}));
109  log(" bd.angularDamping = %.15lef;\n",
110  static_cast<double>(Real{body.GetAngularDamping() / Hertz}));
111  log(" bd.allowSleep = bool(%d);\n", body.IsSleepingAllowed());
112  log(" bd.awake = bool(%d);\n", body.IsAwake());
113  log(" bd.fixedRotation = bool(%d);\n", body.IsFixedRotation());
114  log(" bd.bullet = bool(%d);\n", body.IsImpenetrable());
115  log(" bd.enabled = bool(%d);\n", body.IsEnabled());
116  log(" bodies[%d] = m_world->CreateBody(bd);\n", bodyIndex);
117  log("\n");
118  for (auto&& fixture: body.GetFixtures())
119  {
120  log(" {\n");
121  Dump(GetRef(fixture), bodyIndex);
122  log(" }\n");
123  }
124  log("}\n");
125 }
126 
127 void Dump(const Joint& joint, std::size_t index)
128 {
129  switch (GetType(joint))
130  {
131  case JointType::Pulley:
132  Dump(static_cast<const PulleyJoint&>(joint), index);
133  break;
134  case JointType::Distance:
135  Dump(static_cast<const DistanceJoint&>(joint), index);
136  break;
137  case JointType::Friction:
138  Dump(static_cast<const FrictionJoint&>(joint), index);
139  break;
140  case JointType::Motor:
141  Dump(static_cast<const MotorJoint&>(joint), index);
142  break;
143  case JointType::Weld:
144  Dump(static_cast<const WeldJoint&>(joint), index);
145  break;
146  case JointType::Target:
147  Dump(static_cast<const TargetJoint&>(joint), index);
148  break;
149  case JointType::Revolute:
150  Dump(static_cast<const RevoluteJoint&>(joint), index);
151  break;
153  Dump(static_cast<const PrismaticJoint&>(joint), index);
154  break;
155  case JointType::Gear:
156  Dump(static_cast<const GearJoint&>(joint), index);
157  break;
158  case JointType::Rope:
159  Dump(static_cast<const RopeJoint&>(joint), index);
160  break;
161  case JointType::Wheel:
162  Dump(static_cast<const WheelJoint&>(joint), index);
163  break;
164  case JointType::Unknown:
165  assert(false);
166  break;
167  }
168 }
169 
170 void Dump(const Fixture& fixture, std::size_t bodyIndex)
171 {
172  log(" FixtureConf fd;\n");
173  log(" fd.friction = %.15lef;\n", static_cast<double>(fixture.GetFriction()));
174  log(" fd.restitution = %.15lef;\n", static_cast<double>(fixture.GetRestitution()));
175  log(" fd.density = %.15lef;\n",
176  static_cast<double>(Real{fixture.GetDensity() * SquareMeter / Kilogram}));
177  log(" fd.isSensor = bool(%d);\n", fixture.IsSensor());
178  log(" fd.filter.categoryBits = Filter::bits_type(%u);\n",
179  fixture.GetFilterData().categoryBits);
180  log(" fd.filter.maskBits = Filter::bits_type(%u);\n",
181  fixture.GetFilterData().maskBits);
182  log(" fd.filter.groupIndex = Filter::index_type(%d);\n",
183  fixture.GetFilterData().groupIndex);
184 #if 0
185  const auto shape = fixture.GetShape();
186  std::ostringstream os;
187  os << shape << "\n";
188  log(os.str().c_str());
189 #endif
190  log("\n");
191  log(" fd.shape = &shape;\n");
192  log("\n");
193  log(" bodies[%d]->CreateFixture(fd);\n", bodyIndex);
194 }
195 
196 void Dump(const DistanceJoint& joint, std::size_t index)
197 {
198  log(" DistanceJointConf jd;\n");
199  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
200  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
201  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
202  log(" jd.localAnchorA = Vec2(%.15lef, %.15lef);\n",
203  static_cast<double>(Real{get<0>(joint.GetLocalAnchorA()) / Meter}),
204  static_cast<double>(Real{get<1>(joint.GetLocalAnchorA()) / Meter}));
205  log(" jd.localAnchorB = Vec2(%.15lef, %.15lef);\n",
206  static_cast<double>(Real{get<0>(joint.GetLocalAnchorB()) / Meter}),
207  static_cast<double>(Real{get<1>(joint.GetLocalAnchorB()) / Meter}));
208  log(" jd.length = %.15lef;\n",
209  static_cast<double>(Real{joint.GetLength() / Meter}));
210  log(" jd.frequency = %.15lef;\n",
211  static_cast<double>(Real{joint.GetFrequency() / Hertz}));
212  log(" jd.dampingRatio = %.15lef;\n", joint.GetDampingRatio());
213  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
214 }
215 
216 void Dump(const FrictionJoint& joint, std::size_t index)
217 {
218  log(" FrictionJointConf jd;\n");
219  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
220  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
221  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
222  log(" jd.localAnchorA = Vec2(%.15lef, %.15lef);\n",
223  static_cast<double>(Real{get<0>(joint.GetLocalAnchorA()) / Meter}),
224  static_cast<double>(Real{get<1>(joint.GetLocalAnchorA()) / Meter}));
225  log(" jd.localAnchorB = Vec2(%.15lef, %.15lef);\n",
226  static_cast<double>(Real{get<0>(joint.GetLocalAnchorB()) / Meter}),
227  static_cast<double>(Real{get<1>(joint.GetLocalAnchorB()) / Meter}));
228  log(" jd.maxForce = %.15lef;\n",
229  static_cast<double>(Real{joint.GetMaxForce() / Newton}));
230  log(" jd.maxTorque = %.15lef;\n",
231  static_cast<double>(Real{joint.GetMaxTorque() / NewtonMeter}));
232  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
233 }
234 
235 void Dump(const GearJoint& joint, std::size_t index)
236 {
237  log(" GearJointConf jd;\n");
238  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
239  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
240  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
241  log(" jd.joint1 = joints[%d];\n", GetWorldIndex(joint.GetJoint1()));
242  log(" jd.joint2 = joints[%d];\n", GetWorldIndex(joint.GetJoint2()));
243  log(" jd.ratio = %.15lef;\n", joint.GetRatio());
244  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
245 }
246 
247 void Dump(const MotorJoint& joint, std::size_t index)
248 {
249  log(" MotorJointConf jd;\n");
250  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
251  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
252  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
253  log(" jd.linearOffset = Vec2(%.15lef, %.15lef);\n",
254  static_cast<double>(Real{get<0>(joint.GetLinearOffset()) / Meter}),
255  static_cast<double>(Real{get<1>(joint.GetLinearOffset()) / Meter}));
256  log(" jd.angularOffset = %.15lef;\n",
257  static_cast<double>(Real{joint.GetAngularOffset() / Radian}));
258  log(" jd.maxForce = %.15lef;\n",
259  static_cast<double>(Real{joint.GetMaxForce() / Newton}));
260  log(" jd.maxTorque = %.15lef;\n",
261  static_cast<double>(Real{joint.GetMaxTorque() / NewtonMeter}));
262  log(" jd.correctionFactor = %.15lef;\n", joint.GetCorrectionFactor());
263  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
264 }
265 
266 void Dump(const TargetJoint& joint, std::size_t index)
267 {
268  log(" TargetJointConf jd;\n");
269  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
270  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
271  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
272  log(" jd.localAnchorB = Vec2(%.15lef, %.15lef);\n",
273  static_cast<double>(Real{get<0>(joint.GetLocalAnchorB()) / Meter}),
274  static_cast<double>(Real{get<1>(joint.GetLocalAnchorB()) / Meter}));
275  log(" jd.frequency = %.15lef;\n",
276  static_cast<double>(Real{joint.GetFrequency() / Hertz}));
277  log(" jd.dampingRatio = %.15lef;\n", static_cast<double>(Real{joint.GetDampingRatio()}));
278  log(" jd.maxForce = %.15lef;\n",
279  static_cast<double>(Real{joint.GetMaxForce() / Newton}));
280  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
281 }
282 
283 void Dump(const PrismaticJoint& joint, std::size_t index)
284 {
285  log(" PrismaticJointConf jd;\n");
286  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
287  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
288  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
289  log(" jd.localAnchorA = Vec2(%.15lef, %.15lef);\n",
290  static_cast<double>(Real{get<0>(joint.GetLocalAnchorA()) / Meter}),
291  static_cast<double>(Real{get<1>(joint.GetLocalAnchorA()) / Meter}));
292  log(" jd.localAnchorB = Vec2(%.15lef, %.15lef);\n",
293  static_cast<double>(Real{get<0>(joint.GetLocalAnchorB()) / Meter}),
294  static_cast<double>(Real{get<1>(joint.GetLocalAnchorB()) / Meter}));
295  log(" jd.localAxisA = Vec2(%.15lef, %.15lef);\n",
296  GetX(joint.GetLocalAxisA()), GetY(joint.GetLocalAxisA()));
297  log(" jd.referenceAngle = %.15lef;\n",
298  static_cast<double>(Real{joint.GetReferenceAngle() / Radian}));
299  log(" jd.enableLimit = bool(%d);\n", joint.IsLimitEnabled());
300  log(" jd.lowerTranslation = %.15lef;\n",
301  static_cast<double>(Real{joint.GetLowerLimit() / Meter}));
302  log(" jd.upperTranslation = %.15lef;\n",
303  static_cast<double>(Real{joint.GetUpperLimit() / Meter}));
304  log(" jd.enableMotor = bool(%d);\n", joint.IsMotorEnabled());
305  log(" jd.motorSpeed = %.15lef;\n",
306  static_cast<double>(Real{joint.GetMotorSpeed() / RadianPerSecond}));
307  log(" jd.maxMotorForce = %.15lef;\n",
308  static_cast<double>(Real{joint.GetMaxMotorForce() / Newton}));
309  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
310 }
311 
312 void Dump(const PulleyJoint& joint, std::size_t index)
313 {
314  log(" PulleyJointConf jd;\n");
315  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
316  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
317  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
318  log(" jd.groundAnchorA = Vec2(%.15lef, %.15lef);\n",
319  static_cast<double>(Real{get<0>(joint.GetGroundAnchorA()) / Meter}),
320  static_cast<double>(Real{get<1>(joint.GetGroundAnchorA()) / Meter}));
321  log(" jd.groundAnchorB = Vec2(%.15lef, %.15lef);\n",
322  static_cast<double>(Real{get<0>(joint.GetGroundAnchorB()) / Meter}),
323  static_cast<double>(Real{get<1>(joint.GetGroundAnchorB()) / Meter}));
324  log(" jd.localAnchorA = Vec2(%.15lef, %.15lef);\n",
325  static_cast<double>(Real{get<0>(joint.GetLocalAnchorA()) / Meter}),
326  static_cast<double>(Real{get<1>(joint.GetLocalAnchorA()) / Meter}));
327  log(" jd.localAnchorB = Vec2(%.15lef, %.15lef);\n",
328  static_cast<double>(Real{get<0>(joint.GetLocalAnchorB()) / Meter}),
329  static_cast<double>(Real{get<1>(joint.GetLocalAnchorB()) / Meter}));
330  log(" jd.lengthA = %.15lef;\n",
331  static_cast<double>(Real{joint.GetLengthA() / Meter}));
332  log(" jd.lengthB = %.15lef;\n",
333  static_cast<double>(Real{joint.GetLengthB() / Meter}));
334  log(" jd.ratio = %.15lef;\n", joint.GetRatio());
335  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
336 }
337 
338 void Dump(const RevoluteJoint& joint, std::size_t index)
339 {
340  log(" RevoluteJointConf jd;\n");
341  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
342  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
343  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
344  log(" jd.localAnchorA = Vec2(%.15lef, %.15lef);\n",
345  static_cast<double>(Real{get<0>(joint.GetLocalAnchorA()) / Meter}),
346  static_cast<double>(Real{get<1>(joint.GetLocalAnchorA()) / Meter}));
347  log(" jd.localAnchorB = Vec2(%.15lef, %.15lef);\n",
348  static_cast<double>(Real{get<0>(joint.GetLocalAnchorB()) / Meter}),
349  static_cast<double>(Real{get<1>(joint.GetLocalAnchorB()) / Meter}));
350  log(" jd.referenceAngle = %.15lef;\n",
351  static_cast<double>(Real{joint.GetReferenceAngle() / Radian}));
352  log(" jd.enableLimit = bool(%d);\n", joint.IsLimitEnabled());
353  log(" jd.lowerAngle = %.15lef;\n",
354  static_cast<double>(Real{joint.GetLowerLimit() / Radian}));
355  log(" jd.upperAngle = %.15lef;\n",
356  static_cast<double>(Real{joint.GetUpperLimit() / Radian}));
357  log(" jd.enableMotor = bool(%d);\n", joint.IsMotorEnabled());
358  log(" jd.motorSpeed = %.15lef;\n",
359  static_cast<double>(Real{joint.GetMotorSpeed() / RadianPerSecond}));
360  log(" jd.maxMotorTorque = %.15lef;\n",
361  static_cast<double>(Real{joint.GetMaxMotorTorque() / NewtonMeter}));
362  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
363 }
364 
365 void Dump(const RopeJoint& joint, std::size_t index)
366 {
367  log(" RopeJointConf jd;\n");
368  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
369  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
370  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
371  log(" jd.localAnchorA = Vec2(%.15lef, %.15lef);\n",
372  static_cast<double>(Real{get<0>(joint.GetLocalAnchorA()) / Meter}),
373  static_cast<double>(Real{get<1>(joint.GetLocalAnchorA()) / Meter}));
374  log(" jd.localAnchorB = Vec2(%.15lef, %.15lef);\n",
375  static_cast<double>(Real{get<0>(joint.GetLocalAnchorB()) / Meter}),
376  static_cast<double>(Real{get<1>(joint.GetLocalAnchorB()) / Meter}));
377  log(" jd.maxLength = %.15lef;\n",
378  static_cast<double>(Real{joint.GetMaxLength() / Meter}));
379  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
380 }
381 
382 void Dump(const WeldJoint& joint, std::size_t index)
383 {
384  log(" WeldJointConf jd;\n");
385  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
386  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
387  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
388  log(" jd.localAnchorA = Vec2(%.15lef, %.15lef);\n",
389  static_cast<double>(Real{get<0>(joint.GetLocalAnchorA()) / Meter}),
390  static_cast<double>(Real{get<1>(joint.GetLocalAnchorA()) / Meter}));
391  log(" jd.localAnchorB = Vec2(%.15lef, %.15lef);\n",
392  static_cast<double>(Real{get<0>(joint.GetLocalAnchorB()) / Meter}),
393  static_cast<double>(Real{get<1>(joint.GetLocalAnchorB()) / Meter}));
394  log(" jd.referenceAngle = %.15lef;\n",
395  static_cast<double>(Real{joint.GetReferenceAngle() / Radian}));
396  log(" jd.frequency = %.15lef;\n",
397  static_cast<double>(Real{joint.GetFrequency() / Hertz}));
398  log(" jd.dampingRatio = %.15lef;\n", joint.GetDampingRatio());
399  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
400 }
401 
402 void Dump(const WheelJoint& joint, std::size_t index)
403 {
404  log(" WheelJointConf jd;\n");
405  log(" jd.bodyA = bodies[%d];\n", GetWorldIndex(joint.GetBodyA()));
406  log(" jd.bodyB = bodies[%d];\n", GetWorldIndex(joint.GetBodyB()));
407  log(" jd.collideConnected = bool(%d);\n", joint.GetCollideConnected());
408  log(" jd.localAnchorA = Vec2(%.15lef, %.15lef);\n",
409  static_cast<double>(Real{get<0>(joint.GetLocalAnchorA()) / Meter}),
410  static_cast<double>(Real{get<1>(joint.GetLocalAnchorA()) / Meter}));
411  log(" jd.localAnchorB = Vec2(%.15lef, %.15lef);\n",
412  static_cast<double>(Real{get<0>(joint.GetLocalAnchorB()) / Meter}),
413  static_cast<double>(Real{get<1>(joint.GetLocalAnchorB()) / Meter}));
414  log(" jd.localAxisA = Vec2(%.15lef, %.15lef);\n",
415  static_cast<double>(GetX(joint.GetLocalAxisA())),
416  static_cast<double>(GetY(joint.GetLocalAxisA())));
417  log(" jd.enableMotor = bool(%d);\n", joint.IsMotorEnabled());
418  log(" jd.motorSpeed = %.15lef;\n",
419  static_cast<double>(Real{joint.GetMotorSpeed() / RadianPerSecond}));
420  log(" jd.maxMotorTorque = %.15lef;\n",
421  static_cast<double>(Real{joint.GetMaxMotorTorque() / NewtonMeter}));
422  log(" jd.frequency = %.15lef;\n",
423  static_cast<double>(Real{joint.GetSpringFrequency() / Hertz}));
424  log(" jd.dampingRatio = %.15lef;\n", joint.GetSpringDampingRatio());
425  log(" joints[%d] = m_world->CreateJoint(jd);\n", index);
426 }
427 
428 } // namespace d2
429 } // namespace playrho