PhysicsDirect.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "PhysicsDirect.h"
  2. #include "PhysicsClientSharedMemory.h"
  3. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  4. #include "SharedMemoryCommands.h"
  5. #include "PhysicsServerCommandProcessor.h"
  6. #include "LinearMath/btHashMap.h"
  7. #include "LinearMath/btAlignedObjectArray.h"
  8. #include "../../Extras/Serialize/BulletFileLoader/btBulletFile.h"
  9. #include "../../Extras/Serialize/BulletFileLoader/autogenerated/bullet.h"
  10. #include "BodyJointInfoUtility.h"
  11. struct BodyJointInfoCache2
  12. {
  13. btAlignedObjectArray<b3JointInfo> m_jointInfo;
  14. };
  15. struct PhysicsDirectInternalData
  16. {
  17. DummyGUIHelper m_noGfx;
  18. SharedMemoryCommand m_command;
  19. SharedMemoryStatus m_serverStatus;
  20. bool m_hasStatus;
  21. bool m_verboseOutput;
  22. btAlignedObjectArray<TmpFloat3> m_debugLinesFrom;
  23. btAlignedObjectArray<TmpFloat3> m_debugLinesTo;
  24. btAlignedObjectArray<TmpFloat3> m_debugLinesColor;
  25. btHashMap<btHashInt,BodyJointInfoCache2*> m_bodyJointMap;
  26. char m_bulletStreamDataServerToClient[SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE];
  27. PhysicsServerCommandProcessor* m_commandProcessor;
  28. PhysicsDirectInternalData()
  29. :m_hasStatus(false),
  30. m_verboseOutput(false)
  31. {
  32. }
  33. };
  34. PhysicsDirect::PhysicsDirect()
  35. {
  36. m_data = new PhysicsDirectInternalData;
  37. m_data->m_commandProcessor = new PhysicsServerCommandProcessor;
  38. }
  39. PhysicsDirect::~PhysicsDirect()
  40. {
  41. delete m_data->m_commandProcessor;
  42. delete m_data;
  43. }
  44. // return true if connection succesfull, can also check 'isConnected'
  45. bool PhysicsDirect::connect()
  46. {
  47. m_data->m_commandProcessor->setGuiHelper(&m_data->m_noGfx);
  48. return true;
  49. }
  50. ////todo: rename to 'disconnect'
  51. void PhysicsDirect::disconnectSharedMemory()
  52. {
  53. m_data->m_commandProcessor->setGuiHelper(0);
  54. }
  55. bool PhysicsDirect::isConnected() const
  56. {
  57. return true;
  58. }
  59. // return non-null if there is a status, nullptr otherwise
  60. const SharedMemoryStatus* PhysicsDirect::processServerStatus()
  61. {
  62. SharedMemoryStatus* stat = 0;
  63. if (m_data->m_hasStatus)
  64. {
  65. stat = &m_data->m_serverStatus;
  66. m_data->m_hasStatus = false;
  67. }
  68. return stat;
  69. }
  70. SharedMemoryCommand* PhysicsDirect::getAvailableSharedMemoryCommand()
  71. {
  72. return &m_data->m_command;
  73. }
  74. bool PhysicsDirect::canSubmitCommand() const
  75. {
  76. return true;
  77. }
  78. bool PhysicsDirect::processDebugLines(const struct SharedMemoryCommand& orgCommand)
  79. {
  80. SharedMemoryCommand command = orgCommand;
  81. const SharedMemoryStatus& serverCmd = m_data->m_serverStatus;
  82. do
  83. {
  84. bool hasStatus = m_data->m_commandProcessor->processCommand(command,m_data->m_serverStatus,&m_data->m_bulletStreamDataServerToClient[0],SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
  85. m_data->m_hasStatus = hasStatus;
  86. if (hasStatus)
  87. {
  88. btAssert(m_data->m_serverStatus.m_type == CMD_DEBUG_LINES_COMPLETED);
  89. if (m_data->m_verboseOutput)
  90. {
  91. b3Printf("Success receiving %d debug lines",
  92. serverCmd.m_sendDebugLinesArgs.m_numDebugLines);
  93. }
  94. int numLines = serverCmd.m_sendDebugLinesArgs.m_numDebugLines;
  95. float* linesFrom =
  96. (float*)&m_data->m_bulletStreamDataServerToClient[0];
  97. float* linesTo =
  98. (float*)(&m_data->m_bulletStreamDataServerToClient[0] +
  99. numLines * 3 * sizeof(float));
  100. float* linesColor =
  101. (float*)(&m_data->m_bulletStreamDataServerToClient[0] +
  102. 2 * numLines * 3 * sizeof(float));
  103. m_data->m_debugLinesFrom.resize(serverCmd.m_sendDebugLinesArgs.m_startingLineIndex +
  104. numLines);
  105. m_data->m_debugLinesTo.resize(serverCmd.m_sendDebugLinesArgs.m_startingLineIndex +
  106. numLines);
  107. m_data->m_debugLinesColor.resize(
  108. serverCmd.m_sendDebugLinesArgs.m_startingLineIndex + numLines);
  109. for (int i = 0; i < numLines; i++)
  110. {
  111. TmpFloat3 from = CreateTmpFloat3(linesFrom[i * 3], linesFrom[i * 3 + 1],
  112. linesFrom[i * 3 + 2]);
  113. TmpFloat3 to =
  114. CreateTmpFloat3(linesTo[i * 3], linesTo[i * 3 + 1], linesTo[i * 3 + 2]);
  115. TmpFloat3 color = CreateTmpFloat3(linesColor[i * 3], linesColor[i * 3 + 1],
  116. linesColor[i * 3 + 2]);
  117. m_data
  118. ->m_debugLinesFrom[serverCmd.m_sendDebugLinesArgs.m_startingLineIndex + i] =
  119. from;
  120. m_data->m_debugLinesTo[serverCmd.m_sendDebugLinesArgs.m_startingLineIndex + i] =
  121. to;
  122. m_data->m_debugLinesColor[serverCmd.m_sendDebugLinesArgs.m_startingLineIndex +
  123. i] = color;
  124. }
  125. if (serverCmd.m_sendDebugLinesArgs.m_numRemainingDebugLines > 0)
  126. {
  127. command.m_type = CMD_REQUEST_DEBUG_LINES;
  128. command.m_requestDebugLinesArguments.m_startingLineIndex =
  129. serverCmd.m_sendDebugLinesArgs.m_numDebugLines +
  130. serverCmd.m_sendDebugLinesArgs.m_startingLineIndex;
  131. }
  132. }
  133. } while (serverCmd.m_sendDebugLinesArgs.m_numRemainingDebugLines > 0);
  134. return m_data->m_hasStatus;
  135. }
  136. bool PhysicsDirect::submitClientCommand(const struct SharedMemoryCommand& command)
  137. {
  138. if (command.m_type==CMD_REQUEST_DEBUG_LINES)
  139. {
  140. return processDebugLines(command);
  141. }
  142. bool hasStatus = m_data->m_commandProcessor->processCommand(command,m_data->m_serverStatus,&m_data->m_bulletStreamDataServerToClient[0],SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
  143. m_data->m_hasStatus = hasStatus;
  144. if (hasStatus)
  145. {
  146. const SharedMemoryStatus& serverCmd = m_data->m_serverStatus;
  147. switch (m_data->m_serverStatus.m_type)
  148. {
  149. case CMD_RESET_SIMULATION_COMPLETED:
  150. {
  151. m_data->m_debugLinesFrom.clear();
  152. m_data->m_debugLinesTo.clear();
  153. m_data->m_debugLinesColor.clear();
  154. for (int i=0;i<m_data->m_bodyJointMap.size();i++)
  155. {
  156. BodyJointInfoCache2** bodyJointsPtr = m_data->m_bodyJointMap.getAtIndex(i);
  157. if (bodyJointsPtr && *bodyJointsPtr)
  158. {
  159. BodyJointInfoCache2* bodyJoints = *bodyJointsPtr;
  160. for (int j=0;j<bodyJoints->m_jointInfo.size();j++) {
  161. if (bodyJoints->m_jointInfo[j].m_jointName)
  162. {
  163. free(bodyJoints->m_jointInfo[j].m_jointName);
  164. }
  165. if (bodyJoints->m_jointInfo[j].m_linkName)
  166. {
  167. free(bodyJoints->m_jointInfo[j].m_linkName);
  168. }
  169. }
  170. delete (*bodyJointsPtr);
  171. }
  172. }
  173. m_data->m_bodyJointMap.clear();
  174. break;
  175. }
  176. case CMD_URDF_LOADING_COMPLETED:
  177. {
  178. if (serverCmd.m_dataStreamArguments.m_streamChunkLength > 0)
  179. {
  180. bParse::btBulletFile bf(
  181. &m_data->m_bulletStreamDataServerToClient[0],
  182. serverCmd.m_dataStreamArguments.m_streamChunkLength);
  183. bf.setFileDNAisMemoryDNA();
  184. bf.parse(false);
  185. int bodyIndex = serverCmd.m_dataStreamArguments.m_bodyUniqueId;
  186. BodyJointInfoCache2* bodyJoints = new BodyJointInfoCache2;
  187. m_data->m_bodyJointMap.insert(bodyIndex,bodyJoints);
  188. for (int i = 0; i < bf.m_multiBodies.size(); i++)
  189. {
  190. int flag = bf.getFlags();
  191. if ((flag & bParse::FD_DOUBLE_PRECISION) != 0)
  192. {
  193. Bullet::btMultiBodyDoubleData* mb =
  194. (Bullet::btMultiBodyDoubleData*)bf.m_multiBodies[i];
  195. addJointInfoFromMultiBodyData(mb,bodyJoints, m_data->m_verboseOutput);
  196. } else
  197. {
  198. Bullet::btMultiBodyFloatData* mb =
  199. (Bullet::btMultiBodyFloatData*)bf.m_multiBodies[i];
  200. addJointInfoFromMultiBodyData(mb,bodyJoints, m_data->m_verboseOutput);
  201. }
  202. }
  203. if (bf.ok()) {
  204. if (m_data->m_verboseOutput)
  205. {
  206. b3Printf("Received robot description ok!\n");
  207. }
  208. } else
  209. {
  210. b3Warning("Robot description not received");
  211. }
  212. }
  213. break;
  214. }
  215. default:
  216. {
  217. // b3Error("Unknown server status type");
  218. }
  219. };
  220. }
  221. return hasStatus;
  222. }
  223. int PhysicsDirect::getNumJoints(int bodyIndex) const
  224. {
  225. BodyJointInfoCache2** bodyJointsPtr = m_data->m_bodyJointMap[bodyIndex];
  226. if (bodyJointsPtr && *bodyJointsPtr)
  227. {
  228. BodyJointInfoCache2* bodyJoints = *bodyJointsPtr;
  229. return bodyJoints->m_jointInfo.size();
  230. }
  231. btAssert(0);
  232. return 0;
  233. }
  234. void PhysicsDirect::getJointInfo(int bodyIndex, int jointIndex, struct b3JointInfo& info) const
  235. {
  236. BodyJointInfoCache2** bodyJointsPtr = m_data->m_bodyJointMap[bodyIndex];
  237. if (bodyJointsPtr && *bodyJointsPtr)
  238. {
  239. BodyJointInfoCache2* bodyJoints = *bodyJointsPtr;
  240. info = bodyJoints->m_jointInfo[jointIndex];
  241. }
  242. }
  243. ///todo: move this out of the
  244. void PhysicsDirect::setSharedMemoryKey(int key)
  245. {
  246. //m_data->m_physicsServer->setSharedMemoryKey(key);
  247. //m_data->m_physicsClient->setSharedMemoryKey(key);
  248. }
  249. void PhysicsDirect::uploadBulletFileToSharedMemory(const char* data, int len)
  250. {
  251. //m_data->m_physicsClient->uploadBulletFileToSharedMemory(data,len);
  252. }
  253. int PhysicsDirect::getNumDebugLines() const
  254. {
  255. return m_data->m_debugLinesFrom.size();
  256. }
  257. const float* PhysicsDirect::getDebugLinesFrom() const
  258. {
  259. if (getNumDebugLines())
  260. {
  261. return &m_data->m_debugLinesFrom[0].m_x;
  262. }
  263. return 0;
  264. }
  265. const float* PhysicsDirect::getDebugLinesTo() const
  266. {
  267. if (getNumDebugLines())
  268. {
  269. return &m_data->m_debugLinesTo[0].m_x;
  270. }
  271. return 0;
  272. }
  273. const float* PhysicsDirect::getDebugLinesColor() const
  274. {
  275. if (getNumDebugLines())
  276. {
  277. return &m_data->m_debugLinesColor[0].m_x;
  278. }
  279. return 0;
  280. }